-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaddump.c
More file actions
98 lines (88 loc) · 2.9 KB
/
threaddump.c
File metadata and controls
98 lines (88 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "threaddump_shared.h"
#include "threaddump.h"
#define THREADDUMP_HANDLER_THREAD_NAME "threaddump_handler_thread"
pthread_t signal_handler_thread;
static void threaddump_run_gdb_backtrace() {
// hacky but works
char pidbuf[32] = {0};
pid_t parent_pid = getppid();
snprintf(pidbuf, 32, "%d", parent_pid);
pidbuf[31] = 0;
const char* argv[] = {
THREADDUMP_GDB_ATTACH(pidbuf),
THREADDUMP_GDB_EXEC("set logging redirect on"),
THREADDUMP_GDB_EXEC("set logging debugredirect on"),
THREADDUMP_GDB_EXEC("set logging file td.log"),
THREADDUMP_GDB_EXEC("set logging enabled on"),
THREADDUMP_GDB_EXEC("thread apply all bt"),
THREADDUMP_GDB_EXEC("quit"),
NULL
};
int execv_ret = 0;
#ifdef __cplusplus
// this ugly const_cast is needed to bypass C's old non const api
// and to make gcc/g++ shut up
// don't blame me
execv_ret = execve(argv[0], const_cast<char**> (&argv[0]), NULL);
#else
execv_ret = execve(argv[0], (char* const*) &argv[0], NULL);
#endif
// if we return from execve, the call failed
THREADDUMP_ERROR_ABORT("execve() failed, aborting.", execv_ret);
}
static void* threaddump_wait_and_backtrace(void* arg) {
int sig, r;
while (1) {
r = sigwait((const sigset_t *) arg, &sig);
if (r < 0) {
THREADDUMP_ERROR_ABORT("Failed waiting on signal handler, aborting", -1);
}
break;
}
free(arg);
pid_t pid = fork();
if (pid == -1) {
THREADDUMP_ERROR_ABORT("Failed forking process", pid);
}
if (pid == 0) {
threaddump_run_gdb_backtrace();
}
wait(&pid);
exit(sig);
}
static THREADDUMP_INIT threaddump_init(void) {
THREADDUMP_LOG("THREADDUMP INIT\n");
sigset_t* set = (sigset_t*) malloc(sizeof(*set));
if (set == NULL) {
THREADDUMP_ERROR_ABORT("Failed to malloc signal mask\n", -1);
}
// initialize and add SIGINT to the set of signals to handle by the signal_handler_thread
sigemptyset(set);
sigaddset(set, SIGINT);
sigaddset(set, SIGTERM);
int r = pthread_sigmask(SIG_BLOCK, set, NULL);
if (r < 0) {
free(set);
THREADDUMP_ERROR_ABORT("Failed to set sigmask", -1);
}
r = pthread_create(&signal_handler_thread, NULL, &threaddump_wait_and_backtrace, set);
if (r < 0) {
free(set);
THREADDUMP_ERROR_ABORT("Failed to create signal handling thread", -1);
}
THREADDUMP_LOG("CREATED SIGNAL HANDLING THREAD\n");
}
static THREADDUMP_DESTROY threaddump_destroy(void) {
THREADDUMP_LOG("THREADDUMP DESTROY\n");
int r = pthread_join(signal_handler_thread, NULL);
if (r < 0) {
THREADDUMP_ERROR_ABORT("Failed joining signal handling thread", -1);
}
THREADDUMP_LOG("JOINED THREAD WITH CODE %d\n", r);
}