Ultimate guide to Debug in Delphi

后端 未结 4 1746
耶瑟儿~
耶瑟儿~ 2020-12-15 10:46

Is there a complete resource for debugging in Delphi that instructs on how to use all the IDE debugging tools? There used to be a guide from Marco Cantù but it was updated t

相关标签:
4条回答
  • 2020-12-15 11:20

    IMO the official documentation on debugging is comprehensive: Debugging Applications and Debugging Applications. AFAICS the two sites have similar content but the latter may be more up to date.

    I also would like to note Warren Postma's tutorial on Remote Debugging which has helped me start at no time.

    0 讨论(0)
  • 2020-12-15 11:21

    The debugging resource PDF didn't mention my favorite debugging technique:

    Let's say you wanted to break if a certain, complex, only-available at runtime condition was met.

    You could say

    if <MyExpressionA> then
    asm
      int 3; // Enter CPU Debugger
    end;
    
    Or you could say
    
    if not <MyExpressionB> then
    asm
      int 3; // Enter CPU Debugger
    end;
    

    Where ExpressionA is something you NEVER expect to be true (i.e., if it's true, it signals an anomalous condition), OR where ExpressionB is something you ALWAYS expect to be true (i.e., if it's false, it signals an anomalous condition).

    Remember that either expression can contain multiple function calls -- if you need them.

    You could put them inside of a block, inside of {$IFDEF DEBUG}, like this:

    procedure MyProcedure;
    var X: Integer;
    begin
      X := GetTheAnswerToLifeTheUniverseAndEverything();
    
      {$IFDEF DEBUG}
      if X <> 42 then // Highly contrived example
      asm
        int 3; // Enter CPU Debugger -- Press F8 when here to step back into source...
      end;
      {$ENDIF}
    
      // More code here...
    
    end;
    

    You can also use

    ASSERT(Expression, "Message"); ASSERT(not Expression, "Message");

    To make sure things function as expected in your code.

    If ASSERTs are enabled in the IDE and an ASSERT fails -- the ASSERT will create an exception, which will unwind the stack to the last exception handler for its type...

    Using my int3 method -- you get immediately into the CPU debugger -- where, if you hit F8 (step over), you'll step to the next line of code -- you can inspect variables, see the whole call stack, and even continue stepping in your code...

    0 讨论(0)
  • 2020-12-15 11:24

    Internet is your friend, here are two links about debugging

    Delphi - Debugging techniques

    [PDF] http://www.scip.be/ScipViewFile.php?Page=ArticlesDelphi11

    The content in there is still very relevant

    0 讨论(0)
  • 2020-12-15 11:27

    Also invest some time in an exception handling framework such as:

    • madSHI (www.madshi.net)
    • JDebug - Part of JCL (http://sourceforge.net/projects/jcl/)
    • Eurekalog (http://www.eurekalog.com/)

    Has all the good stuff in there like stack traces, line numbers etc.

    0 讨论(0)
提交回复
热议问题