7
#include "../include/libpst.h"
15
void Fun2(int arg1, uint32_t arg2, int* ptr)
22
uint32_t Fun1(const int arg1, my_int arg2, uint32_t arg3)
24
int my_local = arg1 + 2;
25
printf("%d\n", my_local);
26
Fun2(my_local, arg3, (int*)0x12345);
39
typedef void (*sig_handler_t)(int sig);
41
void SetSignalHandler(sig_handler_t handler);
43
void SigusrHandler(int sig)
45
// just do nothing on SIGUSR1 & SIGUSR2
46
SetSignalHandler(SigusrHandler);
49
volatile sig_atomic_t fatal_error_in_progress = 0;
51
void FatalSignalHandler(int sig, siginfo_t* info, void* context)
53
// Since this handler is established for more than one kind of signal,
54
// it might still get invoked recursively by delivery of some other kind
55
// of signal. Use a static variable to keep track of that.
56
if (fatal_error_in_progress) {
60
fatal_error_in_progress = 1;
62
printf("%s signal handled\n", strsignal(sig));
63
if((context != 0) && (sig == SIGSEGV || sig == SIGABRT || sig == SIGBUS || sig == SIGFPE))
65
pst_handler* handler = pst_lib_init((ucontext_t*)context, NULL, 0);
67
printf("Failed to allocate pst handler\n");
71
if(pst_unwind_simple(handler)) {
72
printf("Simple unwind-based stack trace:\n");
73
printf("--------------------------------\n");
74
printf("%s\n", pst_print_simple(handler));
75
if(pst_unwind_pretty(handler)) {
76
printf("DWARF-based stack trace information:\n");
77
printf("------------------------------------\n");
78
printf("%s", pst_print_pretty(handler));
80
printf("Failed to use DWARF for unwind stack trace\n");
83
printf("No stack trace obtained\n");
86
pst_lib_fini(handler);
90
// comment out line below to prevent coredump
91
// exit(EXIT_FAILURE);
93
// Now reraise the signal.
94
// We reactivate the signal’s default handling, which is to terminate the process.
95
// We could just call exit or abort, but reraising the signal sets the return status
96
// from the process correctly.
97
signal (sig, SIG_DFL);
101
void SignalHandler(int sig)
103
// do nothing and reset to our handler back
108
#include <sys/resource.h>
110
void SetSignalHandler(sig_handler_t handler)
116
//remove previous handlers
117
sigdelset(&ss, SIGUSR1);
118
sigdelset(&ss, SIGUSR2);
119
sigdelset(&ss, SIGSEGV);
120
sigdelset(&ss, SIGABRT);
121
sigdelset(&ss, SIGBUS);
122
sigdelset(&ss, SIGFPE);
123
sigdelset(&ss, SIGCHLD);
124
sigdelset(&ss, SIGTERM); //without replacement
128
sa.sa_flags = SA_SIGINFO;
132
sa.sa_handler = NULL;
133
sa.sa_sigaction = NULL;
136
sa.sa_handler = (handler == NULL) ? SignalHandler : handler;
137
sigaction(SIGUSR1, &sa, 0);
139
sa.sa_handler = SignalHandler;
140
sigaction(SIGUSR2, &sa, 0);
143
sa.sa_handler = NULL;
144
sa.sa_sigaction = NULL;
147
sa.sa_sigaction = FatalSignalHandler;
148
sigaction(SIGSEGV, &sa, 0);
151
sa.sa_sigaction = FatalSignalHandler;
152
sigaction(SIGABRT, &sa, 0);
155
sa.sa_sigaction = FatalSignalHandler;
156
sigaction(SIGBUS, &sa, 0);
159
sa.sa_sigaction = FatalSignalHandler;
160
sigaction(SIGFPE, &sa, 0);
163
sigprocmask(SIG_BLOCK, &ss, 0);
165
//set core dump size to 1Gb
167
limit.rlim_cur = 1073741824;
168
limit.rlim_max = 1073741824;
169
if (setrlimit(RLIMIT_CORE, &limit))
171
printf("Failed to set limit for core dump file size\n");
175
void ResetSignalHandler() {
176
signal(SIGINT, SIG_DFL);
177
signal(SIGABRT, SIG_DFL);
178
signal(SIGTERM, SIG_DFL);
179
signal(SIGSEGV, SIG_DFL);
180
signal(SIGUSR1, SIG_DFL);
181
signal(SIGUSR2, SIG_DFL);
182
signal(SIGCHLD, SIG_DFL);
185
int main(int argc, char* argv[])
187
SetSignalHandler(SigusrHandler);