Skip to content

Instantly share code, notes, and snippets.

@mooware
Last active April 25, 2024 12:29
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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);
}
@a-rose
Copy link

a-rose commented Oct 2, 2018

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

@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