Reputation: 139
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.
Upvotes: 2
Views: 4595
Reputation: 30597
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.
Upvotes: 3