Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 is that I want a map of the exact code path that led me to the point that I am. I have been working with madExcept, tooled around with jclDebug and while I can get some of the call stack, I can't seem to get EVERY method/procedure/function call that is made in the application to show up in the log.

I've got stack frames turned on, debug info, etc enabled on the project. I even tried turning on stack frames on individual methods that weren't getting included in the call stack to no avail.

Is what I'm trying to do even possible? I'm really trying to avoid having to add logging code all over our millions of lines of code in order to log the code path.

like image 429
Martin Binder Avatar asked Feb 24 '10 15:02

Martin Binder


People also ask

What is call stack stack trace?

a call stack is a stack data structure that stores information about the active subroutines of a computer program. A stack trace is a report of the active stack frames at a certain point in time during the execution of a program.

What is the function of stack trace?

A stack trace is a report that provides information about program subroutines. It is commonly used for certain kinds of debugging, where a stack trace can help software engineers figure out where a problem lies or how various subroutines work together during execution.

How do I get stack trace in Visual Studio?

To open the Call Stack window in Visual Studio, from the Debug menu, choose Windows>Call Stack. To set the local context to a particular row in the stack trace display, select and hold (or double click) the first column of the row.

What are functions should be used to get a printout of the current call stack?

Description. By default traceback() prints the call stack of the last uncaught error, i.e., the sequence of calls that lead to the error. This is useful when an error occurs with an unidentifiable error message. It can also be used to print the current stack or arbitrary lists of calls.


2 Answers

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.

like image 75
Robert Love Avatar answered Sep 19 '22 19:09

Robert Love


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

like image 44
Uwe Raabe Avatar answered Sep 18 '22 19:09

Uwe Raabe