tmate/tmate-debug.c

101 lines
1.8 KiB
C
Raw Normal View History

#ifdef HAVE_EXECINFO_H
2013-06-12 23:57:53 +02:00
#include <execinfo.h>
#endif
2013-06-12 23:57:53 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
2015-12-23 11:41:41 +01:00
#include <signal.h>
2013-06-12 23:57:53 +02:00
#include "tmate.h"
#ifndef HAVE_BACKTRACE
void tmate_print_stack_trace(void) {}
void tmate_catch_sigsegv(void) {}
#else
2013-06-12 23:57:53 +02:00
#if DEBUG
static int print_resolved_stack_frame(const char *frame)
{
char file[100];
char cmd[200];
char output[300];
char address[20];
char *line;
FILE *ps;
static regex_t _regex;
static regex_t *regex;
regmatch_t matches[3];
if (!regex) {
if (regcomp(&_regex, "(.+)\\(\\) \\[([^]]+)\\]", REG_EXTENDED))
return -1;
regex = &_regex;
}
if (regexec(regex, frame, 3, matches, 0))
return -1;
memcpy(file, &frame[matches[1].rm_so], matches[1].rm_eo - matches[1].rm_so);
file[matches[1].rm_eo - matches[1].rm_so] = 0;
memcpy(address, &frame[matches[2].rm_so], matches[2].rm_eo - matches[2].rm_so);
address[matches[2].rm_eo - matches[2].rm_so] = 0;
sprintf(cmd, "addr2line -e %s %s -f -p -s", file, address);
ps = popen(cmd, "r");
if (!ps)
return -1;
line = fgets(output, sizeof(output), ps);
pclose(ps);
if (!line)
return -1;
line[strlen(line)-1] = 0; /* remove \n */
tmate_debug("%s(%s) [%s]", file, line, address);
return 0;
}
#endif
2016-01-01 22:44:01 +01:00
void tmate_print_stack_trace(void)
2013-06-12 23:57:53 +02:00
{
void *array[20];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 20);
strings = backtrace_symbols (array, size);
2013-07-29 15:50:42 +02:00
tmate_info ("============ %zd stack frames ============", size);
2013-06-12 23:57:53 +02:00
for (i = 1; i < size; i++) {
#if DEBUG
if (print_resolved_stack_frame(strings[i]) < 0)
#endif
2013-07-29 15:50:42 +02:00
tmate_info("%s", strings[i]);
2013-06-12 23:57:53 +02:00
}
free (strings);
}
2013-07-22 23:06:28 +02:00
2015-12-23 11:41:41 +01:00
static void handle_sigsegv(__unused int sig)
2013-07-22 23:06:28 +02:00
{
/* TODO send stack trace to server */
tmate_info("CRASH, printing stack trace");
2016-01-01 22:44:01 +01:00
tmate_print_stack_trace();
2013-07-22 23:06:28 +02:00
tmate_fatal("CRASHED");
}
void tmate_catch_sigsegv(void)
{
signal(SIGSEGV, handle_sigsegv);
}
#endif