[fpc-pascal] Get all caller adresses of a procedure/function

Martin lazarus at mfriebe.de
Mon Aug 6 20:15:53 CEST 2012


On 06/08/2012 18:28, Rainer Stratmann wrote:
> Am Monday 06 August 2012 18:43:04 schrieb Martin:
>>> How to get all caller adresses of p1 in a program before p1 is executed?
>>>
>>> For example p1 is called from 20 different places in a program.
>>> Then I need the 20 caller adresses.
>> At run time, or design time?
> At runtime.
> I ment the (memory) caller adresses.
>

You can probably reuse sone code from the system unit.

below is a copy of dump_stack from fpc. It gets all the addresses


{$ifdef FPC_HAS_FEATURE_CONSOLEIO}
Procedure dump_stack(var f : text;bp : Pointer);
var
   i : Longint;
   prevbp : Pointer;
   is_dev : boolean;
   caller_frame,
   caller_addr : Pointer;
Begin
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
   try
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
     prevbp:=bp-1;
     i:=0;
     is_dev:=do_isdevice(textrec(f).Handle);
     while bp > prevbp Do
      Begin
        caller_addr := get_caller_addr(bp);
        caller_frame := get_caller_frame(bp);
        if (caller_addr=nil) then
          break;
        Writeln(f,BackTraceStrFunc(caller_addr));
        if (caller_frame=nil) then
          break;
        Inc(i);
        If ((i>max_frame_dump) and is_dev) or (i>256) Then
          break;
        prevbp:=bp;
        bp:=caller_frame;
      End;
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
    except
      { prevent endless dump if an exception occured }
    end;
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
End;




More information about the fpc-pascal mailing list