Difference between revisions of "Logging exceptions"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 +
=Introduction=
 +
 +
'''Stacktrace''' is sometimes called '''Backtrace''' or '''Call stack''' and have meaning of list of stack frames placed on stack containing return address and local variables. Stacktrace is useful to trace back path of execution of nesting procedures.
 +
 +
 +
Unit SysUtils contains some routines useful for debugging exceptions.
 +
 +
<delphi>{ Exception handling routines }
 +
function ExceptObject: TObject;
 +
function ExceptAddr: Pointer;
 +
function ExceptFrameCount: Longint;
 +
function ExceptFrames: PPointer;</delphi>
 +
 +
 
=Handling exceptions=
 
=Handling exceptions=
  
Line 35: Line 49:
 
end;</delphi>
 
end;</delphi>
  
 +
==Using map file==
  
 +
==Exceptions in DLL==
  
 
=Getting stacktrace=
 
=Getting stacktrace=
 +
  
 
=Compiler parameters=
 
=Compiler parameters=
Line 43: Line 60:
 
*-gl - generate line numbers for debug informations
 
*-gl - generate line numbers for debug informations
  
*-gm
+
 
 +
=See also=
 +
 
 +
* [[Profiling]]
 +
* [[Creating a Backtrace with GDB]]
  
  
 
[[Category:Debugging]]
 
[[Category:Debugging]]

Revision as of 20:40, 2 September 2010

Introduction

Stacktrace is sometimes called Backtrace or Call stack and have meaning of list of stack frames placed on stack containing return address and local variables. Stacktrace is useful to trace back path of execution of nesting procedures.


Unit SysUtils contains some routines useful for debugging exceptions.

<delphi>{ Exception handling routines } function ExceptObject: TObject; function ExceptAddr: Pointer; function ExceptFrameCount: Longint; function ExceptFrames: PPointer;</delphi>


Handling exceptions

Application.OnException

<delphi>procedure TMainForm.CustomExceptionHandler(Sender: TObject; E: Exception); const

 NewLine = #13#10;

var

 I: Integer;
 Frames: PPointer;
 Report: string;

begin

 Report := 'Program exception! ' + NewLine +
   'Stacktrace:' + NewLine + NewLine;
 if E <> nil then begin
   Report := Report + 'Exception class: ' + E.ClassName + NewLine +
   'Message: ' + E.Message + NewLine;
 end;
 Report := Report + BackTraceStrFunc(ExceptAddr);
 Frames := ExceptFrames;
 for I := 0 to ExceptFrameCount - 1 do
   Report := Report + NewLine + BackTraceStrFunc(Frames);
 ShowMessage(Report);
 Halt; // End of program execution

end;

procedure TMainForm.FormCreate(Sender: TObject); begin

 Application.OnException := @CustomExceptionHandler;

end;

procedure TMainForm.ButtonClick(Sender: TObject); begin

 raise Exception.Create('Test');

end;</delphi>

Using map file

Exceptions in DLL

Getting stacktrace

Compiler parameters

  • -gl - generate line numbers for debug informations


See also