Last active
April 25, 2024 12:29
-
-
Save mooware/1198160 to your computer and use it in GitHub Desktop.
Print a stacktrace on Linux with GNU libc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops. Of course. How did that get in there?