2

In a big code that has many functions and many calls I would like to print at screen or logger a back trace of any function with any additional info for example:

main calls function A and A calls function B that calls function C.

If I put a print at function C I would like it to print main::A::B::C - [any parameter value for debugging] or any other format.

This will help me debug the issue I am facing faster.

Note: for those who will answer me that you can put a print on each function, as I said many calls and many flows.

Noam Rathaus
  • 5,405
  • 2
  • 28
  • 37
Nizarazo
  • 139
  • 2
  • 10
  • 3
    Do you know of the existence of `gdb`. It has these capabilities built in. I bet almost every debugger worth its salt does. – Floris Feb 06 '14 at 20:49
  • 4
    Might want to specify platform/toolchain, as any answer would be platform-specific. – Eugene Feb 06 '14 at 20:49
  • Possibly [this](http://stackoverflow.com/questions/3899870/print-call-stack-in-c-or-c) would help you. – Predelnik Feb 06 '14 at 20:52

1 Answers1

3

If you are using g++, then the stack backtrace can be obtained using the functions backtrace or backtrace_symbols.

The challenge is to capture the content of the stack trace at the point where the exception is raised, and then transport that to where the exception is caught. For that you can use Boost.Exception. Create a subclass of boost::exception, then in your constructor, capture the stack backtrace. Later on, when catching the exception, you can print out the trace or otherwise make it available.

Another catch is that the C++ symbol names will be mangled. If you want to make them readable, you can call abi::__cxa_demangle (again, g++ specific).

As you can see from the above, the answer is platform specific. There may be similar solutions for other compilers and runtime environments.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • On Windows XP and later, you want [CaptureStackBackTrace](http://msdn.microsoft.com/en-us/library/windows/desktop/bb204633(v=vs.85).aspx) and [SymFromAddr](http://msdn.microsoft.com/en-us/library/windows/desktop/ms681323(v=vs.85).aspx). – MooseBoys Feb 06 '14 at 23:51
  • harmic thanks for your reply, I used it, it is very helpful. do you know any way to print the backtrace with line and file name of the function that is called ? – Nizarazo Feb 09 '14 at 15:05
  • 1
    @Nizarazo if you compiled your code with -g then the mapping between symbols and source should be present in the compiled program. In theory you can use the addr2line command to convert to file:line. I have not had much success with that though. – harmic Feb 09 '14 at 22:22
  • 1
    @harmic I have succeeded in using addr2line, I would like to ask you this question since I am banned for asking questions, I have this backtrace: ./a.out(_ZN5Debug7myfunc3Ev+0x2f) [0x401161] What does +0x2f means ? can I know which line it refers by using this address? I tried to convert 0x2f to decimal it is 47 I looked from function name till 47 lines didn't see that it fits the place where the function is called? – Nizarazo Mar 03 '14 at 12:46