elemy-utils 1.0.0
callstack.h
Go to the documentation of this file.
1#ifndef CALLSTACK_H
2#define CALLSTACK_H
3
4#ifdef __amd64__
5#include <sys/wait.h>
6#include <unistd.h>
7#include <sys/prctl.h>
8#include <execinfo.h>
9
10#include "c_decls.h"
11__BEGIN_DECLS /* till __END_DECLS */
12
13static inline void callstack() {
14 char pid_buf[30];
15 sprintf(pid_buf, "%d", getpid());
16 char name_buf[512];
17 name_buf[readlink("/proc/self/exe", name_buf, 511)]=0;
18 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
19 int child_pid = fork();
20 if (!child_pid) {
21 dup2(2,1); // redirect output to stderr - edit: unnecessary?
22 execl("/usr/bin/gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL);
23 abort(); /* If gdb failed to start */
24 } else {
25 waitpid(child_pid,NULL,0);
26 }
27} //static inline void print_trace() =================
28
29# define CALLSTACK() callstack()
30#else
31# define CALLSTACK()
32#endif
33
35#endif /* CALLSTACK_H */
36
#define __END_DECLS
Definition: c_decls.h:10
#define __BEGIN_DECLS
Definition: c_decls.h:9