-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbacktrace.cpp
More file actions
71 lines (58 loc) · 1.31 KB
/
Copy pathbacktrace.cpp
File metadata and controls
71 lines (58 loc) · 1.31 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
#include "backtrace.h"
#include <execinfo.h>
#include <signal.h>
#include <time.h>
void writeStackTrace(int sig)
{
char time_buf[64];
time_t time_ = time(NULL);
struct tm ltm = {0};
localtime_r(&time_, <m);
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", <m);
std::ofstream fs_;
std::string stack_info_;
fs_.open("Trace.log", std::ios::app);
fs_<<"===============get signal: " << sig <<". "<< time_buf<<"=============" <<std::endl;
const int len = 200;
void* buffer[len];
int nptrs = ::backtrace(buffer, len);
char** strings = ::backtrace_symbols(buffer, nptrs);
if (strings)
{
for (int i = 0; i < nptrs; ++i)
{
stack_info_.append(strings[i]);
stack_info_.push_back('\n');
}
free(strings);
}
fs_<<stack_info_;
fs_.close();
exit(1);
}
void setbacktrace(int sig)
{
signal(sig, writeStackTrace);
}
/*
void backtrace_info::writeStackTrace()
{
const int len = 200;
void* buffer[len];
int nptrs = ::backtrace(buffer, len);
char** strings = ::backtrace_symbols(buffer, nptrs);
if (strings)
{
for (int i = 0; i < nptrs; ++i)
{
// TODO demangle funcion name with abi::__cxa_demangle
stack_info_.append(strings[i]);
stack_info_.push_back('\n');
}
free(strings);
}
fs_.open("Trace.log", std::ios::app);
fs_<<stack_info_;
fs_.close();
}
*/