Ignore:
Timestamp:
Feb 10, 2011, 4:07:44 PM (14 years ago)
Author:
george
Message:
  • Added: Method for obtaining call stack from specified address.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ExceptionLogger/UStackTrace.pas

    r39 r167  
    2424    MaxDepth: Integer;
    2525    procedure GetExceptionBackTrace;
    26     procedure GetCallStack;
     26    procedure GetCallStack(BP: Pointer);
     27    procedure GetCurrentCallStack;
    2728    constructor Create;
    2829  end;
     
    5455end;
    5556
    56 procedure TStackTrace.GetCallStack;
     57procedure TStackTrace.GetCallStack(BP: Pointer);
    5758var
    5859  I: Longint;
    5960  prevbp: Pointer;
    60   CallerFrame,
    61   CallerAddress,
    62   bp: Pointer;
     61  CallerFrame: Pointer;
     62  CallerAddress: Pointer;
    6363  StackFrameInfo: TStackFrameInfo;
    6464begin
    6565  Clear;
    66   //routine adapted from fpc source
    67 
    68   bp := get_frame;
    69   //This trick skip SendCallstack item
    70   // bp := get_caller_frame(get_frame);
    7166  try
    72     prevbp := bp - 1;
    7367    I := 0;
    74     //is_dev:=do_isdevice(textrec(f).Handle);
    75     while bp > prevbp do begin
    76        CallerAddress := get_caller_addr(bp);
    77        CallerFrame := get_caller_frame(bp);
    78        if (CallerAddress = nil) then
    79          Break;
    80        StackFrameInfo := TStackFrameInfo.Create;
    81        StackFrameInfo.GetFrameInfo(CallerAddress);
    82        StackFrameInfo.Index := I + 1;
    83        Add(StackFrameInfo);
    84        Inc(I);
    85        if (I >= MaxDepth) or (CallerFrame = nil) then
    86          Break;
    87        prevbp := bp;
    88        bp := CallerFrame;
    89      end;
    90    except
    91      { prevent endless dump if an exception occured }
    92    end;
     68    while (BP <> nil) and (I < MaxDepth) do begin
     69      CallerAddress := TStackFrameInfo(get_caller_addr(BP));
     70      StackFrameInfo := TStackFrameInfo.Create;
     71      StackFrameInfo.GetFrameInfo(CallerAddress);
     72      StackFrameInfo.Index := I + 1;
     73      Add(StackFrameInfo);
     74      Inc(I);
     75      BP := TStackFrameInfo(get_caller_frame(BP));
     76    end;
     77  except
     78    { prevent endless dump if an exception occured }
     79  end;
    9380end;
    9481
     
    122109end;
    123110
    124 
     111procedure TStackTrace.GetCurrentCallStack;
     112begin
     113  GetCallStack(get_frame);
     114end;
    125115
    126116end.
Note: See TracChangeset for help on using the changeset viewer.