Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-05

Debug info at runtime?

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Serguei TARASSOV

Hi All,

How is possible to get some debug information at runtime? Assuming, the program compiled with required debug information/symbols.

I.e. how to get actual call stack state and called procedures names within the procedure or method?

Regards,
Serguei

Tomas Hajny
to Serguei TARASSOV

On Tue, May 26, 2015 19:30, Serguei TARASSOV wrote:

Hi,

Show quoted text 5 lines
How is possible to get some debug information at runtime? Assuming, the program compiled with required debug information/symbols. I.e. how to get actual call stack state and called procedures names within the procedure or method?

Units lineinfo (Stabs debug format) and lnfodwrf (Dwarf debug format) provide means for accessing this information.

Tomas

Serguei TARASSOV
to Serguei TARASSOV: How is possible to get some debug information at runtime? Assuming, the program compiled…

I answered myself :) Hope this helps someone.
Here is an example how to get N-2 calling procedure

Warning: code seeking debugging info is slow so be aware to use it in loops. Make sure to compile with
-gl (not requierd but show file and line numbers info) -gs (show proc name after address)

var
   i: Longint;
   PrevBP: Pointer;
   CallerFrame, CallerAddr, BP: Pointer;
   CallerName: string = '';
begin
   // get N-2 caller proc name
   BP := get_frame;
   PrevBP := BP - 1;
   i := 0;
   while BP > PrevBP do
   begin
     CallerAddr := get_caller_addr(BP);
     CallerFrame := get_caller_frame(BP);
     if (CallerAddr = nil) or (CallerFrame = nil) then
       break;
     if i = 2 then
     begin
       CallerName := BackTraceStrFunc(CallerAddr);
       break;
     end;
     PrevBP := BP;
     BP := CallerFrame;
     Inc(i);
   end;
...

CallerName should contains something like
$0043EF0A TSCRIPTSTEST__TESTPARSER, line 749 of ScriptsTest.pas

Regards,
Serguei

Serguei TARASSOV wrote 10 lines
Hi All, How is possible to get some debug information at runtime? Assuming, the program compiled with required debug information/symbols. I.e. how to get actual call stack state and called procedures names within the procedure or method? Regards, Serguei