Skip to content

Commit

Permalink
Other backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Aug 11, 2024
1 parent eb4d18f commit 7b9d29b
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/shc.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,19 +758,46 @@ static const char * RTC[] = {
0};

void handler(int sig) {
void *array[10];
size_t size;
void *trace[10];
int trace_size;
int i;
char **messages;


// get void*'s for all entries on the stack
size = backtrace(array, 10);
trace_size = backtrace(trace, 10);

// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
backtrace_symbols_fd(trace, size, STDERR_FILENO);

messages = backtrace_symbols(trace, trace_size);


/* skip first stack frame (points here) */
printf("[bt] Execution path:\n");
for (i=1; i<trace_size; ++i)
{
printf("[bt] #%d %s\n", i, messages[i]);

/* find first occurrence of '(' or ' ' in message[i] and assume
* everything before that is the file name. (Don't go beyond 0 though
* (string terminator)*/
size_t p = 0;
while(messages[i][p] != '(' && messages[i][p] != ' '
&& messages[i][p] != 0)
++p;

char syscom[256];
snprintf(syscom, sizeof(syscom), "addr2line %p -e %.*s", trace[i], p, messages[i]);
//last parameter is the file name of the symbol
system(syscom);
}
exit(1);
}



static int parse_an_arg(int argc, char * argv[])
{
extern char * optarg;
Expand Down

0 comments on commit 7b9d29b

Please sign in to comment.