Print Call Stack in C or C++

print call stack in C or C++

For a linux-only solution you can use backtrace(3) that simply returns an array of void * (in fact each of these point to the return address from the corresponding stack frame). To translate these to something of use, there's backtrace_symbols(3).

Pay attention to the notes section in backtrace(3):

The symbol names may be unavailable
without the use of special linker
options.
For systems using the GNU linker, it is necessary to use the
-rdynamic linker
option. Note that names of "static" functions are not exposed,
and won't be
available in the backtrace.

How to print call stack in C/C++ more beautifully?

Disclaimer: The following is mostly for Linux using GCC or Clang with libstdc++, you might need different ways on other systems.

The most important thing is to add -rdynamic to the command line when linking. I don't know if this is required on all systems, but for me, this actually turned all those addresses into symbols.

Now that you get some information, you probably want to demangle the symbols. You start with a stand-alone function to demangle any symbol:

// you most likely need these headers (plus stuff for std::cout, ...)
#include <cxxabi.h>
#include <execinfo.h>

std::string demangle( const char* const symbol )
{
const std::unique_ptr< char, decltype( &std::free ) > demangled(
abi::__cxa_demangle( symbol, 0, 0, 0 ), &std::free );
if( demangled ) {
return demangled.get();
}
else {
return symbol;
}
}

And now for the real thing. I don't know if the format of the output from backtrace_symbols is specified, but the following works quite well for me:

void backtrace()
{
// TODO: replace hardcoded limit?
void* addresses[ 256 ];
const int n = ::backtrace( addresses, std::extent< decltype( addresses ) >::value );
const std::unique_ptr< char*, decltype( &std::free ) > symbols(
::backtrace_symbols( addresses, n ), &std::free );
for( int i = 0; i < n; ++i ) {
// we parse the symbols retrieved from backtrace_symbols() to
// extract the "real" symbols that represent the mangled names.
char* const symbol = symbols.get()[ i ];
char* end = symbol;
while( *end ) {
++end;
}
// scanning is done backwards, since the module name
// might contain both '+' or '(' characters.
while( end != symbol && *end != '+' ) {
--end;
}
char* begin = end;
while( begin != symbol && *begin != '(' ) {
--begin;
}

if( begin != symbol ) {
std::cout << std::string( symbol, ++begin - symbol );
*end++ = '\0';
std::cout << demangle( begin ) << '+' << end;
}
else {
std::cout << symbol;
}
std::cout << std::endl;
}
}

(I had to adapt my code slightly since I'm not using std::cout, so there might be some little quirks there - check the code if it works for you and if not and you need help fixing it, let me know).

Base of Global Call Stack in C/C++

First of all, there is no such thing as a "global call stack". Each thread has a stack, and the stack for the main thread is often looking quite different from the thread of any thread spawned later on. And mostly, each of these "stacks" is just an arbitrary memory segment currently declared to be used as such, sub-allocated from any arbitrary suitable memory pool.

And due to compiler optimizations, many function calls will not even end up on the stack, usually. Meaning there isn't necessarily a distinguishable stack frame. You are only guaranteed that you can reference variables you put on the stack, but not that the compiler must preserve anything you didn't explicitly reference.

There is not even a guarantee that the memory layout for your call stack must even be organized in distinguishable frames. Function pointers are never guaranteed to be part of the stack frame, just happens to be an implementation detail in architectures where data and function pointers may co-exist in the address space. (As there are architectures which require return addresses to be stored in a different address space than the data used in the call stack.)

That aside, yes, there is code which is executed outside of the main() function. Specifically initializers for global static variables, code to set up the runtime environment (env, call parameters, stdin/stdout) etc.

E.g. when having linked to libc, there is __libc_start_main which will call your main function after initialization is done. And clean up when your main function returns.

__libc_start_main is about the point where "stack" starts being used, as far as you can see from within the program. That's not actually true though, there has already been some loader code been executed in kernel space, for reserving memory for your process to operate in initially (including memory for the future stack), initializing registers and memory to well defined values etc.

Right before actually "starting" your process, after dropping out of kernel mode, arbitrary pointers to a future stack, and the first instruction of your program, are loaded into the corresponding processor registers. Effectively, that's where __libc_start_main (or any other initialization function, depending on your runtime) starts running, and the stack visible to you starts building up.

Getting back into the kernel usually involves an interrupt now, which doesn't follow the stack either, but may just directly access processor registers to simply swap the contents of the corresponding processor registers. (E.g. if you call a function from the kernel, the memory required by the call stack inside the function call is not allocated from your stack, but from one you don't even have access to.)

Either way, everything that happens before main() is called, and whenever you enter a syscall, is implementation dependent, and you are not guaranteed any specific observable behavior. And messing around with processor registers, and thereby alternating the program flow, is also far outside defined behavior as far as a pure C / C++ run time is concerned.

Print n levels of callstack?

There is a number of ways to do this.

See How to Log Stack Frames with Windows x64

In my opinion, the simplest and as well the most reliable way is the Win32 API function:

USHORT WINAPI CaptureStackBackTrace(
__in ULONG FramesToSkip,
__in ULONG FramesToCapture,
__out PVOID *BackTrace,
__out_opt PULONG BackTraceHash
);

This FramesToCapture parameter, determines the maximum call stack depth returned.



Related Topics



Leave a reply



Submit