Difference between revisions of "Logging exceptions"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
=Introduction=
+
==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.
 
'''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.
Line 11: Line 11:
  
  
Unit SysUtils contains some routines useful for debugging exceptions.
+
===Unit SysUtils===
 +
Contains some routines useful for debugging exceptions.
  
 
<delphi>{ Exception handling routines }
 
<delphi>{ Exception handling routines }
Line 19: Line 20:
 
function ExceptFrames: PPointer;</delphi>
 
function ExceptFrames: PPointer;</delphi>
  
Unit System contains some stack related routines:
+
===Unit System===
 +
Contains some stack related routines:
 
<delphi>function SysBackTraceStr(Addr:Pointer): ShortString; // Default address to string converter assigned to BackTraceStrFunc
 
<delphi>function SysBackTraceStr(Addr:Pointer): ShortString; // Default address to string converter assigned to BackTraceStrFunc
 
procedure Dump_Stack(var f : text;bp:pointer); // Dump stack to text file
 
procedure Dump_Stack(var f : text;bp:pointer); // Dump stack to text file
 
procedure DumpExceptionBackTrace(var f:text); // Dump backtrace to text file</delphi>
 
procedure DumpExceptionBackTrace(var f:text); // Dump backtrace to text file</delphi>
  
FPC help:
+
===Unit LCLProc===
 +
 
 +
<delphi>// Debugging
 +
procedure RaiseGDBException(const Msg: string);
 +
procedure RaiseAndCatchException;
 +
procedure DumpExceptionBackTrace;
 +
procedure DumpStack;
 +
function GetStackTrace(UseCache: boolean): string;
 +
procedure GetStackTracePointers(var AStack: TStackTracePointers);
 +
function StackTraceAsString(const AStack: TStackTracePointers;
 +
                            UseCache: boolean): string;
 +
function GetLineInfo(Addr: Pointer; UseCache: boolean): string;</delphi>
 +
 
 +
===Unit LineInfo===
 +
 
 +
Is automatically included to application if -gl debug line number info is activated.
 +
 
 +
<delphi>function GetLineInfo(addr:ptruint;var func,source:string;var line:longint) : boolean;</delphi>
 +
 
 +
It also replace address to line info function '''BackTraceStrFunc''' by '''StabBackTraceStr'''.
 +
 
 +
 
 +
'''FPC help:'''
 
* [http://www.freepascal.org/docs-html/rtl/system/dumpexceptionbacktrace.html DumpExceptionBackTrace]
 
* [http://www.freepascal.org/docs-html/rtl/system/dumpexceptionbacktrace.html DumpExceptionBackTrace]
 
* [http://www.freepascal.org/docs-html/rtl/system/dump_stack.html dump_stack]
 
* [http://www.freepascal.org/docs-html/rtl/system/dump_stack.html dump_stack]
  
  
=Handling exceptions=
+
==Handling exceptions==
 +
 
 +
===Application.OnException===
  
==Application.OnException==
+
This event can be used to override default application wide exceptions handling. Custom logging mechanism could provide show custom dialog, log to file, console, sending report to mail, logging to HTTP server, e.g.
  
 
<delphi>procedure TMainForm.CustomExceptionHandler(Sender: TObject; E: Exception);
 
<delphi>procedure TMainForm.CustomExceptionHandler(Sender: TObject; E: Exception);
Line 65: Line 91:
 
end;</delphi>
 
end;</delphi>
  
==Using map file==
+
===Using map file===
  
 
Use compiler switch -Xm to generate map file.
 
Use compiler switch -Xm to generate map file.
  
==Exceptions in DLL==
+
===Exceptions in DLL===
  
=Getting stacktrace=
+
==Getting stacktrace==
  
  
=See also=
+
==See also==
  
 
* [[Profiling]]
 
* [[Profiling]]
 
* [[Creating a Backtrace with GDB]]
 
* [[Creating a Backtrace with GDB]]
  
=External links=
+
==External links==
  
 
* [http://www.ciuly.com/tools/programming/esprinter/index.html esprinter] - tool to get stacktrace from running FreePascal program specified by process id and thread id (for Win32)
 
* [http://www.ciuly.com/tools/programming/esprinter/index.html esprinter] - tool to get stacktrace from running FreePascal program specified by process id and thread id (for Win32)

Revision as of 08:55, 3 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.


Compiler have to be directed to get debug information by switches.

  • -g - generate debug informations
  • -gl - generate line numbers for debug informations


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>

Unit System

Contains some stack related routines: <delphi>function SysBackTraceStr(Addr:Pointer): ShortString; // Default address to string converter assigned to BackTraceStrFunc procedure Dump_Stack(var f : text;bp:pointer); // Dump stack to text file procedure DumpExceptionBackTrace(var f:text); // Dump backtrace to text file</delphi>

Unit LCLProc

<delphi>// Debugging procedure RaiseGDBException(const Msg: string); procedure RaiseAndCatchException; procedure DumpExceptionBackTrace; procedure DumpStack; function GetStackTrace(UseCache: boolean): string; procedure GetStackTracePointers(var AStack: TStackTracePointers); function StackTraceAsString(const AStack: TStackTracePointers;

                           UseCache: boolean): string;

function GetLineInfo(Addr: Pointer; UseCache: boolean): string;</delphi>

Unit LineInfo

Is automatically included to application if -gl debug line number info is activated.

<delphi>function GetLineInfo(addr:ptruint;var func,source:string;var line:longint) : boolean;</delphi>

It also replace address to line info function BackTraceStrFunc by StabBackTraceStr.


FPC help:


Handling exceptions

Application.OnException

This event can be used to override default application wide exceptions handling. Custom logging mechanism could provide show custom dialog, log to file, console, sending report to mail, logging to HTTP server, e.g.

<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

Use compiler switch -Xm to generate map file.

Exceptions in DLL

Getting stacktrace

See also

External links

  • esprinter - tool to get stacktrace from running FreePascal program specified by process id and thread id (for Win32)