Need a way to periodically log the call stack/stack trace for EVERY method/procedure/function called

前端 未结 5 1356
我寻月下人不归
我寻月下人不归 2020-12-08 11:51

I\'m working on a very large application where periodically I\'d like to log the ENTIRE call stack up until the current execution point (not on an exception). The idea here

相关标签:
5条回答
  • 2020-12-08 11:54

    If it is a complete trace you want, I believe a tool like SmartInspect could take you a long way.

    It would require you to add logging to your code but for what you need, that would be unavoidable.

    Some of its highlights

    Monitor in Real-Time
    High-performance live logging via TCP or named-pipes to the Console

    Watch and Monitor Resources
    Track variable values, session data and other application resources.

    Rich Logging & Tracing
    Track messages, exceptions, objects, files, database results & more.

    0 讨论(0)
  • 2020-12-08 11:57

    I use JCLDebug from the JCL to do just this.

    The following will get the call stack for the current location and return it as a string.

    function GetCurrentStack: string;
    var
       stackList: TJclStackInfoList; //JclDebug.pas
       sl: TStringList;
    begin
       stackList := JclCreateStackList(False, 0, Caller(0, False));
       sl := TStringList.Create;
       stackList.AddToStrings(sl, True, True, True, True);
       Result := sl.Text;
       sl.Free;
       stacklist.Free; 
    end;
    

    To make this work as expected, you must enable one of supported ways for Debug Information for JCL such as:

    • Turbo Debugger Information
    • JDBG Files (Generated from the MAP Files)
    • JBDG Files Inserted into the EXE.

    I recently switched between JDBG files inserted into the EXE to just shipping the external JDBG files as it was easier to maintain.

    There are also routines that are useful for tracing such as:

    function ProcByLevel(Level : Integer) : String;

    This allows you to determine the current method/procedure name looking back in the call stack "N" number of levels.

    0 讨论(0)
  • 2020-12-08 12:12

    When you return from a method it is removed from the stack. So presumably your Partial call stack is every method that has not yet returned?

    e.g.

    DoSomething
    begin
        MiniSubMethod
        DomeSomethingMore
        begin
            InnerDoSomething
            begin
                ShowCallStack
            end
        end
    end
    

    I would think in this situation the call stack would be

    InnerDoSomething  
    DoSomethingMore  
    DoSomething  
    

    MiniSubMethod is no longer on the stack as it returned before DoSomethingMore was called.

    I think FastMM4 includes a Stack Trace so you could try that.

    You would definitely need some kind of logging/stack trace instead of just the call stack.

    0 讨论(0)
  • 2020-12-08 12:13

    You can use madExcept - it includes a method named GetThreadStackTrace. MadExcept is free for non-commercial use and definitely worth the price otherwise.

    0 讨论(0)
  • 2020-12-08 12:20

    From the responses and comments to other answers it sounds like you need a CALL LOG, not a CALL STACK. The information you want simply isn't present in a call stack.

    In which case I suggest you investigate a tool such as SmartInspect or AQ Time. Of the two I think SmartInspect is most likely to be relevant. AQ Time is more of an interactive profiling tool, where-as SmartInspect has facilities specifically for remote inspection.

    0 讨论(0)
提交回复
热议问题