/
exceptions.cpp
221 lines (155 loc) · 5.26 KB
/
exceptions.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <config.h>
#if defined(SYSTEM_UNIX) && !defined(SYSTEM_MAC)
#include <sys/prctl.h>
#include <sys/wait.h>
#endif
#include <boost/lexical_cast.hpp>
#include "typename.h"
#include "Logger.h"
#include "demangle.h"
#include "exceptions.h"
logger::LogChannel tracelog("tracelog", "[trace] ");
stack_trace_::stack_trace_() {
#if defined(SYSTEM_UNIX) && !defined(SYSTEM_MAC)
std::string programName = get_program_name();
std::string pid = get_pid();
_stack_trace.push_back(std::string("\t[trace] back trace for ") + programName + " (" + pid + "):");
// create a pipe to read gdb's output
int pipefds[2];
int res = pipe(pipefds);
if (res < 0)
return;
// create a pipe to be used as a barrier
int barrierfds[2];
res = pipe(barrierfds);
if (res < 0)
return;
// fork
int childPid = fork();
// child:
if (!childPid) {
LOG_ALL(tracelog) << "[child] waiting for parent process to allow attaching" << std::endl;
// close writing end of barrier pipe
close(barrierfds[1]);
// wait until parent closes barrier pipe
char buf[1];
while (read(barrierfds[0], buf, sizeof(buf)) > 0)
LOG_ALL(tracelog) << "[child] " << buf[0] << std::endl;
LOG_ALL(tracelog) << "[child] parent process closed barrier pipe, preparing gdb invocation" << std::endl;
// close barrier pipe
close(barrierfds[0]);
// close reading end of output pipe
close(pipefds[0]);
// redirect stdout and stderr to output pipe
dup2(pipefds[1], 1);
dup2(pipefds[1], 2);
// close writing end of pipe (_we_ don't need it any longer)
close(pipefds[1]);
// start gdb
execlp("gdb", "gdb", "--batch", "-n", "-ex", "bt full", programName.c_str(), pid.c_str(), NULL);
// parent:
} else {
LOG_ALL(tracelog) << "[parent] allowing child to attach" << std::endl;
// allow our child process to attach
prctl(PR_SET_PTRACER, childPid, 0, 0, 0);
LOG_ALL(tracelog) << "[parent] closing barrier pipe" << std::endl;
// close barrier pipe to let child proceed
close(barrierfds[0]);
close(barrierfds[1]);
LOG_ALL(tracelog) << "[parent] barrier pipe closed" << std::endl;
// close the write end of pipe
close(pipefds[1]);
// capture child's output
std::string output;
// read the whole output of gdb
char buf[1];
size_t n;
while ((n = read(pipefds[0], buf, sizeof(buf))))
output += std::string(buf, n);
LOG_ALL(tracelog) << "[parent] end of pipe; I read: " << std::endl << output << std::endl;
// split it at newline characters
std::stringstream oss(output);
std::string line;
// ignore every line until '#0 ...'
while (std::getline(oss, line) && (line.size() < 2 || (line[0] != '#' && line[1] != '0')));
// copy remaining lines to stack trace
do {
if (line.size() > 0 && line[0] != '\n')
_stack_trace.push_back(std::string("\t[trace] ") + line);
} while (std::getline(oss, line));
// wait for the child to finish
waitpid(childPid, NULL, 0);
}
#endif // SYSTEM_UNIX && !SYSTEM_MAC
return;
}
const std::vector<std::string>&
stack_trace_::get_stack_trace() const {
return _stack_trace;
}
const std::string&
stack_trace_::get_program_name() {
if (_program_name == "")
initialise_program_name();
return _program_name;
}
std::string
stack_trace_::get_pid() {
#if defined(SYSTEM_UNIX) && !defined(SYSTEM_MAC)
return boost::lexical_cast<std::string>(getpid());
#else
return "unknown";
#endif
}
void
stack_trace_::initialise_program_name() {
#if defined(SYSTEM_UNIX) && !defined(SYSTEM_MAC)
char link[1024];
char name[1024];
snprintf(link, sizeof link, "/proc/%d/exe", getpid());
int size = readlink(link, name, sizeof link);
if (size == -1) {
_program_name = "[program name not found]";
return;
}
for (int i = 0; i < size; i++)
_program_name += name[i];
#endif
}
void handleException(const boost::exception& e, std::ostream& out) {
out << std::endl;
out << "caught exception" << std::endl << std::endl;
if (boost::get_error_info<stack_trace>(e)) {
out << "###############" << std::endl;
out << "# STACK TRACE #" << std::endl;
out << "###############" << std::endl;
out << std::endl << std::endl;
out << *boost::get_error_info<stack_trace>(e);
out << std::endl << std::endl;
}
out << "#####################" << std::endl;
out << "# EXCEPTION SUMMARY #" << std::endl;
out << "#####################" << std::endl;
out << std::endl << std::endl;
out << "exception type:" << std::endl << std::endl << "\t" << typeName(e) << std::endl << std::endl;
if (boost::get_error_info<boost::throw_function>(e)) {
out << "throw location:" << std::endl << std::endl;
out << "\t" << *boost::get_error_info<boost::throw_function>(e) << std::endl;
if (boost::get_error_info<boost::throw_file>(e)) {
out << "\tin " << *boost::get_error_info<boost::throw_file>(e);
if (boost::get_error_info<boost::throw_line>(e))
out << "(" << *boost::get_error_info<boost::throw_line>(e) << ")";
}
out << std::endl;
}
if (boost::get_error_info<error_message>(e)) {
out << std::endl << "error message:" << std::endl << std::endl;;
out << "\t" << *boost::get_error_info<error_message>(e);
out << std::endl << std::endl;
}
}
std::ostream& operator<<(std::ostream& out, const stack_trace_& trace) {
for (unsigned int i = 0; i < trace.get_stack_trace().size(); i++)
out << trace.get_stack_trace()[i] << std::endl;
return out;
}