Skip to content

Instantly share code, notes, and snippets.

@mooware
Last active April 25, 2024 12:29
Show Gist options
  • Save mooware/1198160 to your computer and use it in GitHub Desktop.
Save mooware/1198160 to your computer and use it in GitHub Desktop.
Print a stacktrace on Linux with GNU libc
// this code fragment shows how to print a stack trace (to stderr)
// on Linux using the functions provided by the GNU libc
#include <execinfo.h>
#define MAX_STACK_LEVELS 50
// helper-function to print the current stack trace
void print_stacktrace()
{
void *buffer[MAX_STACK_LEVELS];
int levels = backtrace(buffer, MAX_STACK_LEVELS);
// print to stderr (fd = 2), and remove this function from the trace
backtrace_symbols_fd(buffer + 1, levels - 1, 2);
}
@mooware
Copy link
Author

mooware commented Sep 28, 2019

The constant definition should be as follows:
#define MAX_STACK_LEVELS 50

Oops. Of course. How did that get in there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment