mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-22 02:40:08 +00:00
Example for backtrace() functions.
This commit is contained in:
parent
ee4905312f
commit
17abb551ce
37
manual/examples/execinfo.c
Normal file
37
manual/examples/execinfo.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <execinfo.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Obtain a backtrace and print it to @code{stdout}. */
|
||||||
|
void
|
||||||
|
print_trace (void)
|
||||||
|
{
|
||||||
|
void *array[10];
|
||||||
|
size_t size;
|
||||||
|
char **strings;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
size = backtrace (array, 10);
|
||||||
|
strings = backtrace_symbols (array, size);
|
||||||
|
|
||||||
|
printf ("Obtained %zd stack frames.\n", size);
|
||||||
|
|
||||||
|
for (i = 0; i < size; i++)
|
||||||
|
printf ("%s\n", strings[i]);
|
||||||
|
|
||||||
|
free (strings);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* A dummy function to make the backtrace more interesting. */
|
||||||
|
void
|
||||||
|
dummy_function (void)
|
||||||
|
{
|
||||||
|
print_trace ();
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (void)
|
||||||
|
{
|
||||||
|
dummy_function ();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user