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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
// Copyright 2007 Edd Dawson.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <ostream>
#include <stdexcept>
#include <sstream>
#include "stack.hpp"
#if defined(_WIN32)
# include <windows.h>
# include <imagehlp.h>
# if defined(__MINGW32__)
# define PACKAGE 1
# define PACKAGE_VERSION 1
# include <bfd.h> // link against libbfd and libiberty
# include <psapi.h> // link against psapi
# include <cxxabi.h>
# endif
#elif defined(__GNUC__)
# include <dlfcn.h>
# include <cxxabi.h>
#endif
namespace
{
const char * const unknown_function = "[unknown function]";
const char * const unknown_module = "[unknown module]";
#if defined(__GNUC__)
std::string demangle(const char *name)
{
if (!name)
return unknown_function;
int status = 0;
char *d = 0;
std::string ret = name;
try
{
if ((d = abi::__cxa_demangle(name, 0, 0, &status)))
ret = d;
}
catch (const std::bad_alloc &) { }
std::free(d);
return ret;
}
#endif
#if defined(_WIN32)
// Derive from this to disallow copying of your class.
// c.f. boost::noncopyable
class uncopyable
{
protected:
uncopyable() { }
private:
uncopyable(const uncopyable &);
uncopyable &operator= (const uncopyable &);
};
#if defined(__MINGW32__)
// Provides a means to translate a program counter offset in to the name of the corresponding function.
class bfd_context : uncopyable
{
private:
struct find_data
{
std::string func;
unsigned int line;
asymbol **symbol_table;
bfd_vma counter;
};
public:
bfd_context() :
abfd_(0),
sec_(0),
symbol_table_(0)
{
char procname[MAX_PATH];
GetModuleFileNameA(NULL, procname, sizeof procname);
bfd_init();
abfd_ = bfd_openr(procname, 0);
if (!abfd_)
throw std::runtime_error("Failed to parse object data for the executable");
char **formats = 0;
bool b1 = bfd_check_format(abfd_, bfd_object);
bool b2 = bfd_check_format_matches(abfd_, bfd_object, &formats);
bool b3 = bfd_get_file_flags(abfd_) & HAS_SYMS;
if (!(b1 && b2 && b3))
{
bfd_close(abfd_);
free(formats);
throw std::runtime_error("Failed to parse object data for the executable");
}
free(formats);
// Load symbol table
unsigned dummy = 0;
if (bfd_read_minisymbols(abfd_, FALSE, reinterpret_cast<void **>(&symbol_table_), &dummy) == 0 &&
bfd_read_minisymbols(abfd_, TRUE, reinterpret_cast<void **>(&symbol_table_), &dummy) < 0)
{
free(symbol_table_);
bfd_close(abfd_);
throw std::runtime_error("Failed to parse object data for the executable");
}
}
~bfd_context()
{
free(symbol_table_);
bfd_close(abfd_);
}
std::pair<std::string, unsigned int> get_function_name_and_line(DWORD offset)
{
find_data data;
data.symbol_table = symbol_table_;
data.counter = offset;
bfd_map_over_sections(abfd_, &find_function_name_in_section, &data);
return std::make_pair(data.func, data.line);
}
private:
static void find_function_name_in_section(bfd *abfd, asection *sec, void *opaque_data)
{
assert(sec);
assert(opaque_data);
find_data &data = *static_cast<find_data *>(opaque_data);
if (!data.func.empty()) return; // already found it
if (!(bfd_get_section_flags(abfd, sec) & SEC_ALLOC)) return;
bfd_vma vma = bfd_get_section_vma(abfd, sec);
if (data.counter < vma || vma + bfd_get_section_size(sec) <= data.counter) return;
const char *func = 0;
const char *file = 0;
unsigned line = 0;
if (bfd_find_nearest_line(abfd, sec, data.symbol_table, data.counter - vma, &file, &func, &line) && func) {
data.func = demangle(func);
data.line = line;
}
}
private:
bfd *abfd_;
asection *sec_;
asymbol **symbol_table_;
};
#endif // __MINGW32__
// g++ spouts warnings if you use {0} to initialize PODs. So we use this instead:
const struct
{
template<typename POD>
operator POD () const { POD p; std::memset(&p, 0, sizeof p); return p; }
}
empty_pod = { };
// Wraps a FARPROC. Implicitly convertible to any kind of pointer-to-function.
// Avoids having reinterpret casts all over the place.
struct auto_cast_function_ptr
{
auto_cast_function_ptr(FARPROC f) : fptr_(f) { }
template<typename FuncPtr>
operator FuncPtr() const { return reinterpret_cast<FuncPtr>(fptr_); }
FARPROC fptr_;
};
// A wrapper around a DLL. Can dynamically get function pointers with the function() function!
class windows_dll : uncopyable
{
public:
explicit windows_dll(const std::string &libname) :
name_(libname),
lib_(LoadLibraryA(name_.c_str()))
{
if (!lib_) throw std::runtime_error("Failed to load dll " + name_);
}
~windows_dll() { FreeLibrary(lib_); }
const std::string &name() const { return name_; }
auto_cast_function_ptr function(const char *func_name) const
{
FARPROC proc = GetProcAddress(lib_, func_name);
if (!proc) throw std::runtime_error(std::string("failed to load function ") + func_name + " from library " + name_);
return proc;
}
private:
std::string name_;
HMODULE lib_;
};
// An object that makes sure debugging symbols are available
class symbol_context : uncopyable
{
public:
symbol_context()
{
if (!SymInitialize(GetCurrentProcess(), 0, TRUE))
throw std::runtime_error("Failed to initialize symbol context");
}
~symbol_context() { SymCleanup(GetCurrentProcess()); }
};
// A simple Windows mutex class. Use a lock object to lock the mutex for the duration of a scope.
class mutex : uncopyable
{
public:
mutex() { InitializeCriticalSection(&cs_); }
~mutex() { DeleteCriticalSection(&cs_); }
private:
friend class lock;
void lock() { EnterCriticalSection(&cs_); }
void unlock() { LeaveCriticalSection(&cs_); }
CRITICAL_SECTION cs_;
}
g_fill_frames_mtx;
// A lock for the mutex
class lock : uncopyable
{
public:
lock(mutex &m) : m_(m) { m.lock(); }
~lock() { m_.unlock(); }
private:
mutex &m_;
};
void fill_frames(std::list<dbg::stack_frame> &frames, dbg::stack::depth_type limit)
{
lock lk(g_fill_frames_mtx);
symbol_context sc;
#ifdef __MINGW32__
bfd_context bfdc;
#endif
STACKFRAME frame = empty_pod;
CONTEXT context = empty_pod;
context.ContextFlags = CONTEXT_FULL;
windows_dll kernel32("kernel32.dll");
void (WINAPI *RtlCaptureContext_)(CONTEXT*) = kernel32.function("RtlCaptureContext");
RtlCaptureContext_(&context);
#if defined(_M_AMD64)
frame.AddrPC.Offset = context.Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context.Rsp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context.Rbp;
frame.AddrFrame.Mode = AddrModeFlat;
#else
frame.AddrPC.Offset = context.Eip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context.Esp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context.Ebp;
frame.AddrFrame.Mode = AddrModeFlat;
#endif
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
bool skip = true;
bool has_limit = limit != 0;
char symbol_buffer[sizeof(IMAGEHLP_SYMBOL) + 255];
char module_name_raw[MAX_PATH];
#if defined(_M_AMD64)
const DWORD machine = IMAGE_FILE_MACHINE_AMD64;
#else
const DWORD machine = IMAGE_FILE_MACHINE_I386;
#endif
while(StackWalk(machine, process, thread, &frame, &context, 0, SymFunctionTableAccess, SymGetModuleBase, 0))
{
if (skip)
{
skip = false;
continue;
}
if (has_limit && limit-- == 0) break;
IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(symbol_buffer);
symbol->SizeOfStruct = (sizeof *symbol) + 255;
symbol->MaxNameLength = 254;
#if defined(_WIN64)
DWORD64 module_base = SymGetModuleBase(process, frame.AddrPC.Offset);
#else
DWORD module_base = SymGetModuleBase(process, frame.AddrPC.Offset);
#endif
std::string module_name = unknown_module;
if (module_base && GetModuleFileNameA(reinterpret_cast<HINSTANCE>(module_base), module_name_raw, MAX_PATH))
module_name = module_name_raw;
#if defined(__MINGW32__)
std::pair<std::string, unsigned int> func_and_line = bfdc.get_function_name_and_line(frame.AddrPC.Offset);
if (func_and_line.first.empty())
{
#if defined(_WIN64)
DWORD64 dummy = 0;
#else
DWORD dummy = 0;
#endif
BOOL got_symbol = SymGetSymFromAddr(process, frame.AddrPC.Offset, &dummy, symbol);
func_and_line.first = got_symbol ? symbol->Name : unknown_function;
}
#else
DWORD dummy = 0;
BOOL got_symbol = SymGetSymFromAddr(process, frame.AddrPC.Offset, &dummy, symbol);
std::string func = got_symbol ? symbol->Name : unknown_function;
#endif
dbg::stack_frame f(reinterpret_cast<const void *>(frame.AddrPC.Offset), func_and_line.first, func_and_line.second, module_name);
frames.push_back(f);
}
}
#elif defined(__GNUC__)
# if defined(__i386__) || defined(__amd64__)
void fill_frames(std::list<dbg::stack_frame> &frames, dbg::stack::depth_type limit)
{
// Based on code found at:
// http://www.tlug.org.za/wiki/index.php/Obtaining_a_stack_trace_in_C_upon_SIGSEGV
Dl_info info;
void **frame = static_cast<void **>(__builtin_frame_address(0));
void **bp = static_cast<void **>(*frame);
void *ip = frame[1];
bool has_limit = limit != 0;
bool skip = true;
while(bp && ip && dladdr(ip, &info))
{
if (skip)
skip = false;
else
{
if (has_limit && limit-- == 0) break;
frames.push_back(dbg::stack_frame(ip, demangle(info.dli_sname), info.dli_fname));
if(info.dli_sname && !std::strcmp(info.dli_sname, "main")) break;
}
ip = bp[1];
bp = static_cast<void**>(bp[0]);
}
}
# elif defined(__ppc__)
void fill_frames(std::list<dbg::stack_frame> &frames, dbg::stack::depth_type limit)
{
// Based on code found at:
// http://www.informit.com/articles/article.aspx?p=606582&seqNum=4&rl=1
void *ip = __builtin_return_address(0);
void **frame = static_cast<void **>(__builtin_frame_address(1));
bool has_limit = limit != 0;
Dl_info info;
do
{
if (has_limit && limit-- == 0) break;
if (dladdr(ip, &info))
frames.push_back(dbg::stack_frame(ip, demangle(info.dli_sname), info.dli_fname));
if (frame && (frame = static_cast<void**>(*frame))) ip = *(frame + 2);
}
while (frame && ip);
}
# else
// GNU, but not x86, x64 nor PPC
# error "Sorry but dbg::stack is not supported on this architecture"
# endif
#else
// Unsupported compiler
# error "Sorry but dbg::stack is not supported on this compiler"
#endif
} // close anonymous namespace
namespace dbg
{
stack_frame::stack_frame(const void *instruction, const std::string &function, unsigned int line, const std::string &module) :
instruction(instruction),
function(function),
line(line),
module(module)
{
}
std::ostream &operator<< (std::ostream &out, const stack_frame &frame)
{
return out << frame.instruction << ": " << frame.function << ":" << frame.line << " in " << frame.module;
}
stack::stack(depth_type limit)
{
fill_frames(frames_, limit);
}
stack::const_iterator stack::begin() const
{
return frames_.begin();
}
stack::const_iterator stack::end() const
{
return frames_.end();
}
stack::depth_type stack::depth() const
{
return frames_.size();
}
} // close namespace dbg
|