1 /*
2  * backward.hpp
3  * Copyright 2013 Google Inc. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #ifndef H_6B9572DA_A64B_49E6_B234_051480991C89
25 #define H_6B9572DA_A64B_49E6_B234_051480991C89
26 
27 #ifndef __cplusplus
28 #error "It's not going to compile without a C++ compiler..."
29 #endif
30 
31 #if defined(BACKWARD_CXX11)
32 #elif defined(BACKWARD_CXX98)
33 #else
34 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
35 #define BACKWARD_CXX11
36 #define BACKWARD_ATLEAST_CXX11
37 #define BACKWARD_ATLEAST_CXX98
38 #else
39 #define BACKWARD_CXX98
40 #define BACKWARD_ATLEAST_CXX98
41 #endif
42 #endif
43 
44 // You can define one of the following (or leave it to the auto-detection):
45 //
46 // #define BACKWARD_SYSTEM_LINUX
47 //	- specialization for linux
48 //
49 // #define BACKWARD_SYSTEM_DARWIN
50 //	- specialization for Mac OS X 10.5 and later.
51 //
52 // #define BACKWARD_SYSTEM_UNKNOWN
53 //	- placebo implementation, does nothing.
54 //
55 #if defined(BACKWARD_SYSTEM_LINUX)
56 #elif defined(BACKWARD_SYSTEM_DARWIN)
57 #elif defined(BACKWARD_SYSTEM_UNKNOWN)
58 #elif defined(BACKWARD_SYSTEM_WINDOWS)
59 #else
60 #if defined(__linux) || defined(__linux__)
61 #define BACKWARD_SYSTEM_LINUX
62 #elif defined(__APPLE__)
63 #define BACKWARD_SYSTEM_DARWIN
64 #elif defined(_WIN32)
65 #define BACKWARD_SYSTEM_WINDOWS
66 #else
67 #define BACKWARD_SYSTEM_UNKNOWN
68 #endif
69 #endif
70 
71 #define NOINLINE __attribute__((noinline))
72 
73 #include <algorithm>
74 #include <cctype>
75 #include <cstdio>
76 #include <cstdlib>
77 #include <cstring>
78 #include <fstream>
79 #include <iomanip>
80 #include <iostream>
81 #include <limits>
82 #include <new>
83 #include <sstream>
84 #include <streambuf>
85 #include <string>
86 #include <vector>
87 
88 #if defined(BACKWARD_SYSTEM_LINUX)
89 
90 // On linux, backtrace can back-trace or "walk" the stack using the following
91 // libraries:
92 //
93 // #define BACKWARD_HAS_UNWIND 1
94 //  - unwind comes from libgcc, but I saw an equivalent inside clang itself.
95 //  - with unwind, the stacktrace is as accurate as it can possibly be, since
96 //  this is used by the C++ runtine in gcc/clang for stack unwinding on
97 //  exception.
98 //  - normally libgcc is already linked to your program by default.
99 //
100 // #define BACKWARD_HAS_BACKTRACE == 1
101 //  - backtrace seems to be a little bit more portable than libunwind, but on
102 //  linux, it uses unwind anyway, but abstract away a tiny information that is
103 //  sadly really important in order to get perfectly accurate stack traces.
104 //  - backtrace is part of the (e)glib library.
105 //
106 // The default is:
107 // #define BACKWARD_HAS_UNWIND == 1
108 //
109 // Note that only one of the define should be set to 1 at a time.
110 //
111 #if BACKWARD_HAS_UNWIND == 1
112 #elif BACKWARD_HAS_BACKTRACE == 1
113 #else
114 #undef BACKWARD_HAS_UNWIND
115 #define BACKWARD_HAS_UNWIND 1
116 #undef BACKWARD_HAS_BACKTRACE
117 #define BACKWARD_HAS_BACKTRACE 0
118 #endif
119 
120 // On linux, backward can extract detailed information about a stack trace
121 // using one of the following libraries:
122 //
123 // #define BACKWARD_HAS_DW 1
124 //  - libdw gives you the most juicy details out of your stack traces:
125 //    - object filename
126 //    - function name
127 //    - source filename
128 //	  - line and column numbers
129 //	  - source code snippet (assuming the file is accessible)
130 //	  - variables name and values (if not optimized out)
131 //  - You need to link with the lib "dw":
132 //    - apt-get install libdw-dev
133 //    - g++/clang++ -ldw ...
134 //
135 // #define BACKWARD_HAS_BFD 1
136 //  - With libbfd, you get a fair amount of details:
137 //    - object filename
138 //    - function name
139 //    - source filename
140 //	  - line numbers
141 //	  - source code snippet (assuming the file is accessible)
142 //  - You need to link with the lib "bfd":
143 //    - apt-get install binutils-dev
144 //    - g++/clang++ -lbfd ...
145 //
146 // #define BACKWARD_HAS_DWARF 1
147 //  - libdwarf gives you the most juicy details out of your stack traces:
148 //    - object filename
149 //    - function name
150 //    - source filename
151 //    - line and column numbers
152 //    - source code snippet (assuming the file is accessible)
153 //    - variables name and values (if not optimized out)
154 //  - You need to link with the lib "dwarf":
155 //    - apt-get install libdwarf-dev
156 //    - g++/clang++ -ldwarf ...
157 //
158 // #define BACKWARD_HAS_BACKTRACE_SYMBOL 1
159 //  - backtrace provides minimal details for a stack trace:
160 //    - object filename
161 //    - function name
162 //  - backtrace is part of the (e)glib library.
163 //
164 // The default is:
165 // #define BACKWARD_HAS_BACKTRACE_SYMBOL == 1
166 //
167 // Note that only one of the define should be set to 1 at a time.
168 //
169 #if BACKWARD_HAS_DW == 1
170 #elif BACKWARD_HAS_BFD == 1
171 #elif BACKWARD_HAS_DWARF == 1
172 #elif BACKWARD_HAS_BACKTRACE_SYMBOL == 1
173 #else
174 #undef BACKWARD_HAS_DW
175 #define BACKWARD_HAS_DW 0
176 #undef BACKWARD_HAS_BFD
177 #define BACKWARD_HAS_BFD 0
178 #undef BACKWARD_HAS_DWARF
179 #define BACKWARD_HAS_DWARF 0
180 #undef BACKWARD_HAS_BACKTRACE_SYMBOL
181 #define BACKWARD_HAS_BACKTRACE_SYMBOL 1
182 #endif
183 
184 #include <cxxabi.h>
185 #include <fcntl.h>
186 #ifdef __ANDROID__
187 //		Old Android API levels define _Unwind_Ptr in both link.h and
188 // unwind.h 		Rename the one in link.h as we are not going to be using
189 // it
190 #define _Unwind_Ptr _Unwind_Ptr_Custom
191 #include <link.h>
192 #undef _Unwind_Ptr
193 #else
194 #include <link.h>
195 #endif
196 #include <signal.h>
197 #include <sys/stat.h>
198 #include <syscall.h>
199 #include <unistd.h>
200 
201 #if BACKWARD_HAS_BFD == 1
202 //              NOTE: defining PACKAGE{,_VERSION} is required before including
203 //                    bfd.h on some platforms, see also:
204 //                    https://sourceware.org/bugzilla/show_bug.cgi?id=14243
205 #ifndef PACKAGE
206 #define PACKAGE
207 #endif
208 #ifndef PACKAGE_VERSION
209 #define PACKAGE_VERSION
210 #endif
211 #include <bfd.h>
212 #ifndef _GNU_SOURCE
213 #define _GNU_SOURCE
214 #include <dlfcn.h>
215 #undef _GNU_SOURCE
216 #else
217 #include <dlfcn.h>
218 #endif
219 #endif
220 
221 #if BACKWARD_HAS_DW == 1
222 #include <dwarf.h>
223 #include <elfutils/libdw.h>
224 #include <elfutils/libdwfl.h>
225 #endif
226 
227 #if BACKWARD_HAS_DWARF == 1
228 #include <algorithm>
229 #include <dwarf.h>
230 #include <libdwarf.h>
231 #include <libelf.h>
232 #include <map>
233 #ifndef _GNU_SOURCE
234 #define _GNU_SOURCE
235 #include <dlfcn.h>
236 #undef _GNU_SOURCE
237 #else
238 #include <dlfcn.h>
239 #endif
240 #endif
241 
242 #if (BACKWARD_HAS_BACKTRACE == 1) || (BACKWARD_HAS_BACKTRACE_SYMBOL == 1)
243 // then we shall rely on backtrace
244 #include <execinfo.h>
245 #endif
246 
247 #endif // defined(BACKWARD_SYSTEM_LINUX)
248 
249 #if defined(BACKWARD_SYSTEM_DARWIN)
250 // On Darwin, backtrace can back-trace or "walk" the stack using the following
251 // libraries:
252 //
253 // #define BACKWARD_HAS_UNWIND 1
254 //  - unwind comes from libgcc, but I saw an equivalent inside clang itself.
255 //  - with unwind, the stacktrace is as accurate as it can possibly be, since
256 //  this is used by the C++ runtine in gcc/clang for stack unwinding on
257 //  exception.
258 //  - normally libgcc is already linked to your program by default.
259 //
260 // #define BACKWARD_HAS_BACKTRACE == 1
261 //  - backtrace is available by default, though it does not produce as much
262 //  information as another library might.
263 //
264 // The default is:
265 // #define BACKWARD_HAS_UNWIND == 1
266 //
267 // Note that only one of the define should be set to 1 at a time.
268 //
269 #if BACKWARD_HAS_UNWIND == 1
270 #elif BACKWARD_HAS_BACKTRACE == 1
271 #else
272 #undef BACKWARD_HAS_UNWIND
273 #define BACKWARD_HAS_UNWIND 1
274 #undef BACKWARD_HAS_BACKTRACE
275 #define BACKWARD_HAS_BACKTRACE 0
276 #endif
277 
278 // On Darwin, backward can extract detailed information about a stack trace
279 // using one of the following libraries:
280 //
281 // #define BACKWARD_HAS_BACKTRACE_SYMBOL 1
282 //  - backtrace provides minimal details for a stack trace:
283 //    - object filename
284 //    - function name
285 //
286 // The default is:
287 // #define BACKWARD_HAS_BACKTRACE_SYMBOL == 1
288 //
289 #if BACKWARD_HAS_BACKTRACE_SYMBOL == 1
290 #else
291 #undef BACKWARD_HAS_BACKTRACE_SYMBOL
292 #define BACKWARD_HAS_BACKTRACE_SYMBOL 1
293 #endif
294 
295 #include <cxxabi.h>
296 #include <fcntl.h>
297 #include <pthread.h>
298 #include <signal.h>
299 #include <sys/stat.h>
300 #include <unistd.h>
301 
302 #if (BACKWARD_HAS_BACKTRACE == 1) || (BACKWARD_HAS_BACKTRACE_SYMBOL == 1)
303 #include <execinfo.h>
304 #endif
305 #endif // defined(BACKWARD_SYSTEM_DARWIN)
306 
307 #if defined(BACKWARD_SYSTEM_WINDOWS)
308 
309 #include <condition_variable>
310 #include <mutex>
311 #include <thread>
312 
313 #include <BaseTsd.h>
314 typedef SSIZE_T ssize_t;
315 
316 #define NOMINMAX
317 #include <Windows.h>
318 #include <winnt.h>
319 
320 #include <Psapi.h>
321 #include <signal.h>
322 
323 #ifndef __clang__
324 #undef NOINLINE
325 #define NOINLINE __declspec(noinline)
326 #endif
327 
328 #pragma comment(lib, "psapi.lib")
329 #pragma comment(lib, "dbghelp.lib")
330 
331 // Comment / packing is from stackoverflow:
332 // https://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app/28276227#28276227
333 // Some versions of imagehlp.dll lack the proper packing directives themselves
334 // so we need to do it.
335 #pragma pack(push, before_imagehlp, 8)
336 #include <imagehlp.h>
337 #pragma pack(pop, before_imagehlp)
338 
339 // TODO maybe these should be undefined somewhere else?
340 #undef BACKWARD_HAS_UNWIND
341 #undef BACKWARD_HAS_BACKTRACE
342 #if BACKWARD_HAS_PDB_SYMBOL == 1
343 #else
344 #undef BACKWARD_HAS_PDB_SYMBOL
345 #define BACKWARD_HAS_PDB_SYMBOL 1
346 #endif
347 
348 #endif
349 
350 #if BACKWARD_HAS_UNWIND == 1
351 
352 #include <unwind.h>
353 // while gcc's unwind.h defines something like that:
354 //  extern _Unwind_Ptr _Unwind_GetIP (struct _Unwind_Context *);
355 //  extern _Unwind_Ptr _Unwind_GetIPInfo (struct _Unwind_Context *, int *);
356 //
357 // clang's unwind.h defines something like this:
358 //  uintptr_t _Unwind_GetIP(struct _Unwind_Context* __context);
359 //
360 // Even if the _Unwind_GetIPInfo can be linked to, it is not declared, worse we
361 // cannot just redeclare it because clang's unwind.h doesn't define _Unwind_Ptr
362 // anyway.
363 //
364 // Luckily we can play on the fact that the guard macros have a different name:
365 #ifdef __CLANG_UNWIND_H
366 // In fact, this function still comes from libgcc (on my different linux boxes,
367 // clang links against libgcc).
368 #include <inttypes.h>
369 extern "C" uintptr_t _Unwind_GetIPInfo(_Unwind_Context *, int *);
370 #endif
371 
372 #endif // BACKWARD_HAS_UNWIND == 1
373 
374 #ifdef BACKWARD_ATLEAST_CXX11
375 #include <unordered_map>
376 #include <utility> // for std::swap
377 namespace backward {
378 namespace details {
379 template <typename K, typename V> struct hashtable {
380   typedef std::unordered_map<K, V> type;
381 };
382 using std::move;
383 } // namespace details
384 } // namespace backward
385 #else // NOT BACKWARD_ATLEAST_CXX11
386 #define nullptr NULL
387 #define override
388 #include <map>
389 namespace backward {
390 namespace details {
391 template <typename K, typename V> struct hashtable {
392   typedef std::map<K, V> type;
393 };
move(const T & v)394 template <typename T> const T &move(const T &v) { return v; }
move(T & v)395 template <typename T> T &move(T &v) { return v; }
396 } // namespace details
397 } // namespace backward
398 #endif // BACKWARD_ATLEAST_CXX11
399 
400 namespace backward {
401 namespace details {
402 #if defined(BACKWARD_SYSTEM_WINDOWS)
403 const char kBackwardPathDelimiter[] = ";";
404 #else
405 const char kBackwardPathDelimiter[] = ":";
406 #endif
407 } // namespace details
408 } // namespace backward
409 
410 namespace backward {
411 
412 namespace system_tag {
413 struct linux_tag; // seems that I cannot call that "linux" because the name
414 // is already defined... so I am adding _tag everywhere.
415 struct darwin_tag;
416 struct windows_tag;
417 struct unknown_tag;
418 
419 #if defined(BACKWARD_SYSTEM_LINUX)
420 typedef linux_tag current_tag;
421 #elif defined(BACKWARD_SYSTEM_DARWIN)
422 typedef darwin_tag current_tag;
423 #elif defined(BACKWARD_SYSTEM_WINDOWS)
424 typedef windows_tag current_tag;
425 #elif defined(BACKWARD_SYSTEM_UNKNOWN)
426 typedef unknown_tag current_tag;
427 #else
428 #error "May I please get my system defines?"
429 #endif
430 } // namespace system_tag
431 
432 namespace trace_resolver_tag {
433 #if defined(BACKWARD_SYSTEM_LINUX)
434 struct libdw;
435 struct libbfd;
436 struct libdwarf;
437 struct backtrace_symbol;
438 
439 #if BACKWARD_HAS_DW == 1
440 typedef libdw current;
441 #elif BACKWARD_HAS_BFD == 1
442 typedef libbfd current;
443 #elif BACKWARD_HAS_DWARF == 1
444 typedef libdwarf current;
445 #elif BACKWARD_HAS_BACKTRACE_SYMBOL == 1
446 typedef backtrace_symbol current;
447 #else
448 #error "You shall not pass, until you know what you want."
449 #endif
450 #elif defined(BACKWARD_SYSTEM_DARWIN)
451 struct backtrace_symbol;
452 
453 #if BACKWARD_HAS_BACKTRACE_SYMBOL == 1
454 typedef backtrace_symbol current;
455 #else
456 #error "You shall not pass, until you know what you want."
457 #endif
458 #elif defined(BACKWARD_SYSTEM_WINDOWS)
459 struct pdb_symbol;
460 #if BACKWARD_HAS_PDB_SYMBOL == 1
461 typedef pdb_symbol current;
462 #else
463 #error "You shall not pass, until you know what you want."
464 #endif
465 #endif
466 } // namespace trace_resolver_tag
467 
468 namespace details {
469 
470 template <typename T> struct rm_ptr { typedef T type; };
471 
472 template <typename T> struct rm_ptr<T *> { typedef T type; };
473 
474 template <typename T> struct rm_ptr<const T *> { typedef const T type; };
475 
476 template <typename R, typename T, R (*F)(T)> struct deleter {
operator ()backward::details::deleter477   template <typename U> void operator()(U &ptr) const { (*F)(ptr); }
478 };
479 
480 template <typename T> struct default_delete {
operator ()backward::details::default_delete481   void operator()(T &ptr) const { delete ptr; }
482 };
483 
484 template <typename T, typename Deleter = deleter<void, void *, &::free>>
485 class handle {
486   struct dummy;
487   T _val;
488   bool _empty;
489 
490 #ifdef BACKWARD_ATLEAST_CXX11
491   handle(const handle &) = delete;
492   handle &operator=(const handle &) = delete;
493 #endif
494 
495 public:
~handle()496   ~handle() {
497     if (!_empty) {
498       Deleter()(_val);
499     }
500   }
501 
handle()502   explicit handle() : _val(), _empty(true) {}
handle(T val)503   explicit handle(T val) : _val(val), _empty(false) {
504     if (!_val)
505       _empty = true;
506   }
507 
508 #ifdef BACKWARD_ATLEAST_CXX11
handle(handle && from)509   handle(handle &&from) : _empty(true) { swap(from); }
operator =(handle && from)510   handle &operator=(handle &&from) {
511     swap(from);
512     return *this;
513   }
514 #else
handle(const handle & from)515   explicit handle(const handle &from) : _empty(true) {
516     // some sort of poor man's move semantic.
517     swap(const_cast<handle &>(from));
518   }
operator =(const handle & from)519   handle &operator=(const handle &from) {
520     // some sort of poor man's move semantic.
521     swap(const_cast<handle &>(from));
522     return *this;
523   }
524 #endif
525 
reset(T new_val)526   void reset(T new_val) {
527     handle tmp(new_val);
528     swap(tmp);
529   }
530 
update(T new_val)531   void update(T new_val) {
532     _val = new_val;
533     _empty = static_cast<bool>(new_val);
534   }
535 
operator const dummy*() const536   operator const dummy *() const {
537     if (_empty) {
538       return nullptr;
539     }
540     return reinterpret_cast<const dummy *>(_val);
541   }
get()542   T get() { return _val; }
release()543   T release() {
544     _empty = true;
545     return _val;
546   }
swap(handle & b)547   void swap(handle &b) {
548     using std::swap;
549     swap(b._val, _val);     // can throw, we are safe here.
550     swap(b._empty, _empty); // should not throw: if you cannot swap two
551     // bools without throwing... It's a lost cause anyway!
552   }
553 
operator ->()554   T &operator->() { return _val; }
operator ->() const555   const T &operator->() const { return _val; }
556 
557   typedef typename rm_ptr<T>::type &ref_t;
558   typedef const typename rm_ptr<T>::type &const_ref_t;
operator *()559   ref_t operator*() { return *_val; }
operator *() const560   const_ref_t operator*() const { return *_val; }
operator [](size_t idx)561   ref_t operator[](size_t idx) { return _val[idx]; }
562 
563   // Watch out, we've got a badass over here
operator &()564   T *operator&() {
565     _empty = false;
566     return &_val;
567   }
568 };
569 
570 // Default demangler implementation (do nothing).
571 template <typename TAG> struct demangler_impl {
demanglebackward::details::demangler_impl572   static std::string demangle(const char *funcname) { return funcname; }
573 };
574 
575 #if defined(BACKWARD_SYSTEM_LINUX) || defined(BACKWARD_SYSTEM_DARWIN)
576 
577 template <> struct demangler_impl<system_tag::current_tag> {
demangler_implbackward::details::demangler_impl578   demangler_impl() : _demangle_buffer_length(0) {}
579 
demanglebackward::details::demangler_impl580   std::string demangle(const char *funcname) {
581     using namespace details;
582     char *result = abi::__cxa_demangle(funcname, _demangle_buffer.get(),
583                                        &_demangle_buffer_length, nullptr);
584     if (result) {
585       _demangle_buffer.update(result);
586       return result;
587     }
588     return funcname;
589   }
590 
591 private:
592   details::handle<char *> _demangle_buffer;
593   size_t _demangle_buffer_length;
594 };
595 
596 #endif // BACKWARD_SYSTEM_LINUX || BACKWARD_SYSTEM_DARWIN
597 
598 struct demangler : public demangler_impl<system_tag::current_tag> {};
599 
600 // Split a string on the platform's PATH delimiter.  Example: if delimiter
601 // is ":" then:
602 //   ""              --> []
603 //   ":"             --> ["",""]
604 //   "::"            --> ["","",""]
605 //   "/a/b/c"        --> ["/a/b/c"]
606 //   "/a/b/c:/d/e/f" --> ["/a/b/c","/d/e/f"]
607 //   etc.
split_source_prefixes(const std::string & s)608 inline std::vector<std::string> split_source_prefixes(const std::string &s) {
609   std::vector<std::string> out;
610   size_t last = 0;
611   size_t next = 0;
612   size_t delimiter_size = sizeof(kBackwardPathDelimiter)-1;
613   while ((next = s.find(kBackwardPathDelimiter, last)) != std::string::npos) {
614     out.push_back(s.substr(last, next-last));
615     last = next + delimiter_size;
616   }
617   if (last <= s.length()) {
618     out.push_back(s.substr(last));
619   }
620   return out;
621 }
622 
623 } // namespace details
624 
625 /*************** A TRACE ***************/
626 
627 struct Trace {
628   void *addr;
629   size_t idx;
630 
Tracebackward::Trace631   Trace() : addr(nullptr), idx(0) {}
632 
Tracebackward::Trace633   explicit Trace(void *_addr, size_t _idx) : addr(_addr), idx(_idx) {}
634 };
635 
636 struct ResolvedTrace : public Trace {
637 
638   struct SourceLoc {
639     std::string function;
640     std::string filename;
641     unsigned line;
642     unsigned col;
643 
SourceLocbackward::ResolvedTrace::SourceLoc644     SourceLoc() : line(0), col(0) {}
645 
operator ==backward::ResolvedTrace::SourceLoc646     bool operator==(const SourceLoc &b) const {
647       return function == b.function && filename == b.filename &&
648              line == b.line && col == b.col;
649     }
650 
operator !=backward::ResolvedTrace::SourceLoc651     bool operator!=(const SourceLoc &b) const { return !(*this == b); }
652   };
653 
654   // In which binary object this trace is located.
655   std::string object_filename;
656 
657   // The function in the object that contain the trace. This is not the same
658   // as source.function which can be an function inlined in object_function.
659   std::string object_function;
660 
661   // The source location of this trace. It is possible for filename to be
662   // empty and for line/col to be invalid (value 0) if this information
663   // couldn't be deduced, for example if there is no debug information in the
664   // binary object.
665   SourceLoc source;
666 
667   // An optionals list of "inliners". All the successive sources location
668   // from where the source location of the trace (the attribute right above)
669   // is inlined. It is especially useful when you compiled with optimization.
670   typedef std::vector<SourceLoc> source_locs_t;
671   source_locs_t inliners;
672 
ResolvedTracebackward::ResolvedTrace673   ResolvedTrace() : Trace() {}
ResolvedTracebackward::ResolvedTrace674   ResolvedTrace(const Trace &mini_trace) : Trace(mini_trace) {}
675 };
676 
677 /*************** STACK TRACE ***************/
678 
679 // default implemention.
680 template <typename TAG> class StackTraceImpl {
681 public:
size() const682   size_t size() const { return 0; }
operator [](size_t) const683   Trace operator[](size_t) const { return Trace(); }
load_here(size_t=0)684   size_t load_here(size_t = 0) { return 0; }
load_from(void *,size_t=0)685   size_t load_from(void *, size_t = 0) { return 0; }
thread_id() const686   size_t thread_id() const { return 0; }
skip_n_firsts(size_t)687   void skip_n_firsts(size_t) {}
688 };
689 
690 class StackTraceImplBase {
691 public:
StackTraceImplBase()692   StackTraceImplBase() : _thread_id(0), _skip(0) {}
693 
thread_id() const694   size_t thread_id() const { return _thread_id; }
695 
skip_n_firsts(size_t n)696   void skip_n_firsts(size_t n) { _skip = n; }
697 
698 protected:
load_thread_info()699   void load_thread_info() {
700 #ifdef BACKWARD_SYSTEM_LINUX
701 #ifndef __ANDROID__
702     _thread_id = static_cast<size_t>(syscall(SYS_gettid));
703 #else
704     _thread_id = static_cast<size_t>(gettid());
705 #endif
706     if (_thread_id == static_cast<size_t>(getpid())) {
707       // If the thread is the main one, let's hide that.
708       // I like to keep little secret sometimes.
709       _thread_id = 0;
710     }
711 #elif defined(BACKWARD_SYSTEM_DARWIN)
712     _thread_id = reinterpret_cast<size_t>(pthread_self());
713     if (pthread_main_np() == 1) {
714       // If the thread is the main one, let's hide that.
715       _thread_id = 0;
716     }
717 #endif
718   }
719 
skip_n_firsts() const720   size_t skip_n_firsts() const { return _skip; }
721 
722 private:
723   size_t _thread_id;
724   size_t _skip;
725 };
726 
727 class StackTraceImplHolder : public StackTraceImplBase {
728 public:
size() const729   size_t size() const {
730     return _stacktrace.size() ? _stacktrace.size() - skip_n_firsts() : 0;
731   }
operator [](size_t idx) const732   Trace operator[](size_t idx) const {
733     if (idx >= size()) {
734       return Trace();
735     }
736     return Trace(_stacktrace[idx + skip_n_firsts()], idx);
737   }
begin() const738   void *const *begin() const {
739     if (size()) {
740       return &_stacktrace[skip_n_firsts()];
741     }
742     return nullptr;
743   }
744 
745 protected:
746   std::vector<void *> _stacktrace;
747 };
748 
749 #if BACKWARD_HAS_UNWIND == 1
750 
751 namespace details {
752 
753 template <typename F> class Unwinder {
754 public:
operator ()(F & f,size_t depth)755   size_t operator()(F &f, size_t depth) {
756     _f = &f;
757     _index = -1;
758     _depth = depth;
759     _Unwind_Backtrace(&this->backtrace_trampoline, this);
760     return static_cast<size_t>(_index);
761   }
762 
763 private:
764   F *_f;
765   ssize_t _index;
766   size_t _depth;
767 
backtrace_trampoline(_Unwind_Context * ctx,void * self)768   static _Unwind_Reason_Code backtrace_trampoline(_Unwind_Context *ctx,
769                                                   void *self) {
770     return (static_cast<Unwinder *>(self))->backtrace(ctx);
771   }
772 
backtrace(_Unwind_Context * ctx)773   _Unwind_Reason_Code backtrace(_Unwind_Context *ctx) {
774     if (_index >= 0 && static_cast<size_t>(_index) >= _depth)
775       return _URC_END_OF_STACK;
776 
777     int ip_before_instruction = 0;
778     uintptr_t ip = _Unwind_GetIPInfo(ctx, &ip_before_instruction);
779 
780     if (!ip_before_instruction) {
781       // calculating 0-1 for unsigned, looks like a possible bug to sanitiziers,
782       // so let's do it explicitly:
783       if (ip == 0) {
784         ip = std::numeric_limits<uintptr_t>::max(); // set it to 0xffff... (as
785                                                     // from casting 0-1)
786       } else {
787         ip -= 1; // else just normally decrement it (no overflow/underflow will
788                  // happen)
789       }
790     }
791 
792     if (_index >= 0) { // ignore first frame.
793       (*_f)(static_cast<size_t>(_index), reinterpret_cast<void *>(ip));
794     }
795     _index += 1;
796     return _URC_NO_REASON;
797   }
798 };
799 
unwind(F f,size_t depth)800 template <typename F> size_t unwind(F f, size_t depth) {
801   Unwinder<F> unwinder;
802   return unwinder(f, depth);
803 }
804 
805 } // namespace details
806 
807 template <>
808 class StackTraceImpl<system_tag::current_tag> : public StackTraceImplHolder {
809 public:
810   NOINLINE
load_here(size_t depth=32)811   size_t load_here(size_t depth = 32) {
812     load_thread_info();
813     if (depth == 0) {
814       return 0;
815     }
816     _stacktrace.resize(depth);
817     size_t trace_cnt = details::unwind(callback(*this), depth);
818     _stacktrace.resize(trace_cnt);
819     skip_n_firsts(0);
820     return size();
821   }
load_from(void * addr,size_t depth=32)822   size_t load_from(void *addr, size_t depth = 32) {
823     load_here(depth + 8);
824 
825     for (size_t i = 0; i < _stacktrace.size(); ++i) {
826       if (_stacktrace[i] == addr) {
827         skip_n_firsts(i);
828         break;
829       }
830     }
831 
832     _stacktrace.resize(std::min(_stacktrace.size(), skip_n_firsts() + depth));
833     return size();
834   }
835 
836 private:
837   struct callback {
838     StackTraceImpl &self;
callbackbackward::StackTraceImpl::callback839     callback(StackTraceImpl &_self) : self(_self) {}
840 
operator ()backward::StackTraceImpl::callback841     void operator()(size_t idx, void *addr) { self._stacktrace[idx] = addr; }
842   };
843 };
844 
845 #elif defined(BACKWARD_HAS_BACKTRACE)
846 
847 template <>
848 class StackTraceImpl<system_tag::current_tag> : public StackTraceImplHolder {
849 public:
850   NOINLINE
load_here(size_t depth=32)851   size_t load_here(size_t depth = 32) {
852     load_thread_info();
853     if (depth == 0) {
854       return 0;
855     }
856     _stacktrace.resize(depth + 1);
857     size_t trace_cnt = backtrace(&_stacktrace[0], _stacktrace.size());
858     _stacktrace.resize(trace_cnt);
859     skip_n_firsts(1);
860     return size();
861   }
862 
load_from(void * addr,size_t depth=32)863   size_t load_from(void *addr, size_t depth = 32) {
864     load_here(depth + 8);
865 
866     for (size_t i = 0; i < _stacktrace.size(); ++i) {
867       if (_stacktrace[i] == addr) {
868         skip_n_firsts(i);
869         _stacktrace[i] = (void *)((uintptr_t)_stacktrace[i] + 1);
870         break;
871       }
872     }
873 
874     _stacktrace.resize(std::min(_stacktrace.size(), skip_n_firsts() + depth));
875     return size();
876   }
877 };
878 
879 #elif defined(BACKWARD_SYSTEM_WINDOWS)
880 
881 template <>
882 class StackTraceImpl<system_tag::current_tag> : public StackTraceImplHolder {
883 public:
884   // We have to load the machine type from the image info
885   // So we first initialize the resolver, and it tells us this info
set_machine_type(DWORD machine_type)886   void set_machine_type(DWORD machine_type) { machine_type_ = machine_type; }
set_context(CONTEXT * ctx)887   void set_context(CONTEXT *ctx) { ctx_ = ctx; }
set_thread_handle(HANDLE handle)888   void set_thread_handle(HANDLE handle) { thd_ = handle; }
889 
890   NOINLINE
load_here(size_t depth=32)891   size_t load_here(size_t depth = 32) {
892 
893     CONTEXT localCtx; // used when no context is provided
894 
895     if (depth == 0) {
896       return 0;
897     }
898 
899     if (!ctx_) {
900       ctx_ = &localCtx;
901       RtlCaptureContext(ctx_);
902     }
903 
904     if (!thd_) {
905       thd_ = GetCurrentThread();
906     }
907 
908     HANDLE process = GetCurrentProcess();
909 
910     STACKFRAME64 s;
911     memset(&s, 0, sizeof(STACKFRAME64));
912 
913     // TODO: 32 bit context capture
914     s.AddrStack.Mode = AddrModeFlat;
915     s.AddrFrame.Mode = AddrModeFlat;
916     s.AddrPC.Mode = AddrModeFlat;
917 #ifdef _M_X64
918     s.AddrPC.Offset = ctx_->Rip;
919     s.AddrStack.Offset = ctx_->Rsp;
920     s.AddrFrame.Offset = ctx_->Rbp;
921 #else
922     s.AddrPC.Offset = ctx_->Eip;
923     s.AddrStack.Offset = ctx_->Esp;
924     s.AddrFrame.Offset = ctx_->Ebp;
925 #endif
926 
927     if (!machine_type_) {
928 #ifdef _M_X64
929       machine_type_ = IMAGE_FILE_MACHINE_AMD64;
930 #else
931       machine_type_ = IMAGE_FILE_MACHINE_I386;
932 #endif
933     }
934 
935     for (;;) {
936       // NOTE: this only works if PDBs are already loaded!
937       SetLastError(0);
938       if (!StackWalk64(machine_type_, process, thd_, &s, ctx_, NULL,
939                        SymFunctionTableAccess64, SymGetModuleBase64, NULL))
940         break;
941 
942       if (s.AddrReturn.Offset == 0)
943         break;
944 
945       _stacktrace.push_back(reinterpret_cast<void *>(s.AddrPC.Offset));
946 
947       if (size() >= depth)
948         break;
949     }
950 
951     return size();
952   }
953 
load_from(void * addr,size_t depth=32)954   size_t load_from(void *addr, size_t depth = 32) {
955     load_here(depth + 8);
956 
957     for (size_t i = 0; i < _stacktrace.size(); ++i) {
958       if (_stacktrace[i] == addr) {
959         skip_n_firsts(i);
960         break;
961       }
962     }
963 
964     _stacktrace.resize(std::min(_stacktrace.size(), skip_n_firsts() + depth));
965     return size();
966   }
967 
968 private:
969   DWORD machine_type_ = 0;
970   HANDLE thd_ = 0;
971   CONTEXT *ctx_ = nullptr;
972 };
973 
974 #endif
975 
976 class StackTrace : public StackTraceImpl<system_tag::current_tag> {};
977 
978 /*************** TRACE RESOLVER ***************/
979 
980 template <typename TAG> class TraceResolverImpl;
981 
982 #ifdef BACKWARD_SYSTEM_UNKNOWN
983 
984 template <> class TraceResolverImpl<system_tag::unknown_tag> {
985 public:
load_stacktrace(ST &)986   template <class ST> void load_stacktrace(ST &) {}
resolve(ResolvedTrace t)987   ResolvedTrace resolve(ResolvedTrace t) { return t; }
988 };
989 
990 #endif
991 
992 class TraceResolverImplBase {
993 protected:
demangle(const char * funcname)994   std::string demangle(const char *funcname) {
995     return _demangler.demangle(funcname);
996   }
997 
998 private:
999   details::demangler _demangler;
1000 };
1001 
1002 #ifdef BACKWARD_SYSTEM_LINUX
1003 
1004 class TraceResolverLinuxBase
1005     : public TraceResolverImplBase {
1006 public:
TraceResolverLinuxBase()1007   TraceResolverLinuxBase()
1008     : argv0_(get_argv0()), exec_path_(read_symlink("/proc/self/exe")) {
1009   }
resolve_exec_path(Dl_info & symbol_info) const1010   std::string resolve_exec_path(Dl_info &symbol_info) const {
1011     // mutates symbol_info.dli_fname to be filename to open and returns filename to display
1012     if(symbol_info.dli_fname == argv0_) {
1013       // dladdr returns argv[0] in dli_fname for symbols contained in
1014       // the main executable, which is not a valid path if the
1015       // executable was found by a search of the PATH environment
1016       // variable; In that case, we actually open /proc/self/exe, which
1017       // is always the actual executable (even if it was deleted/replaced!)
1018       // but display the path that /proc/self/exe links to.
1019       symbol_info.dli_fname = "/proc/self/exe";
1020       return exec_path_;
1021     } else {
1022       return symbol_info.dli_fname;
1023     }
1024   }
1025 private:
1026   std::string argv0_;
1027   std::string exec_path_;
1028 
get_argv0()1029   static std::string get_argv0() {
1030     std::string argv0;
1031     std::ifstream ifs("/proc/self/cmdline");
1032     std::getline(ifs, argv0, '\0');
1033     return argv0;
1034   }
1035 
read_symlink(std::string const & symlink_path)1036   static std::string read_symlink(std::string const &symlink_path) {
1037     std::string path;
1038     path.resize(100);
1039 
1040     while (true) {
1041       ssize_t len =
1042           ::readlink(symlink_path.c_str(), &*path.begin(), path.size());
1043       if (len < 0) {
1044         return "";
1045       }
1046       if (static_cast<size_t>(len) == path.size()) {
1047         path.resize(path.size() * 2);
1048       } else {
1049         path.resize(static_cast<std::string::size_type>(len));
1050         break;
1051       }
1052     }
1053 
1054     return path;
1055   }
1056 };
1057 
1058 template <typename STACKTRACE_TAG> class TraceResolverLinuxImpl;
1059 
1060 #if BACKWARD_HAS_BACKTRACE_SYMBOL == 1
1061 
1062 template <>
1063 class TraceResolverLinuxImpl<trace_resolver_tag::backtrace_symbol>
1064     : public TraceResolverLinuxBase {
1065 public:
load_stacktrace(ST & st)1066   template <class ST> void load_stacktrace(ST &st) {
1067     using namespace details;
1068     if (st.size() == 0) {
1069       return;
1070     }
1071     _symbols.reset(backtrace_symbols(st.begin(), (int)st.size()));
1072   }
1073 
resolve(ResolvedTrace trace)1074   ResolvedTrace resolve(ResolvedTrace trace) {
1075     char *filename = _symbols[trace.idx];
1076     char *funcname = filename;
1077     while (*funcname && *funcname != '(') {
1078       funcname += 1;
1079     }
1080     trace.object_filename.assign(filename,
1081                                  funcname); // ok even if funcname is the ending
1082                                             // \0 (then we assign entire string)
1083 
1084     if (*funcname) { // if it's not end of string (e.g. from last frame ip==0)
1085       funcname += 1;
1086       char *funcname_end = funcname;
1087       while (*funcname_end && *funcname_end != ')' && *funcname_end != '+') {
1088         funcname_end += 1;
1089       }
1090       *funcname_end = '\0';
1091       trace.object_function = this->demangle(funcname);
1092       trace.source.function = trace.object_function; // we cannot do better.
1093     }
1094     return trace;
1095   }
1096 
1097 private:
1098   details::handle<char **> _symbols;
1099 };
1100 
1101 #endif // BACKWARD_HAS_BACKTRACE_SYMBOL == 1
1102 
1103 #if BACKWARD_HAS_BFD == 1
1104 
1105 template <>
1106 class TraceResolverLinuxImpl<trace_resolver_tag::libbfd>
1107     : public TraceResolverLinuxBase {
1108 public:
TraceResolverLinuxImpl()1109   TraceResolverLinuxImpl() : _bfd_loaded(false) {}
1110 
load_stacktrace(ST &)1111   template <class ST> void load_stacktrace(ST &) {}
1112 
resolve(ResolvedTrace trace)1113   ResolvedTrace resolve(ResolvedTrace trace) {
1114     Dl_info symbol_info;
1115 
1116     // trace.addr is a virtual address in memory pointing to some code.
1117     // Let's try to find from which loaded object it comes from.
1118     // The loaded object can be yourself btw.
1119     if (!dladdr(trace.addr, &symbol_info)) {
1120       return trace; // dat broken trace...
1121     }
1122 
1123     // Now we get in symbol_info:
1124     // .dli_fname:
1125     //		pathname of the shared object that contains the address.
1126     // .dli_fbase:
1127     //		where the object is loaded in memory.
1128     // .dli_sname:
1129     //		the name of the nearest symbol to trace.addr, we expect a
1130     //		function name.
1131     // .dli_saddr:
1132     //		the exact address corresponding to .dli_sname.
1133 
1134     if (symbol_info.dli_sname) {
1135       trace.object_function = demangle(symbol_info.dli_sname);
1136     }
1137 
1138     if (!symbol_info.dli_fname) {
1139       return trace;
1140     }
1141 
1142     trace.object_filename = resolve_exec_path(symbol_info);
1143     bfd_fileobject &fobj = load_object_with_bfd(symbol_info.dli_fname);
1144     if (!fobj.handle) {
1145       return trace; // sad, we couldn't load the object :(
1146     }
1147 
1148     find_sym_result *details_selected; // to be filled.
1149 
1150     // trace.addr is the next instruction to be executed after returning
1151     // from the nested stack frame. In C++ this usually relate to the next
1152     // statement right after the function call that leaded to a new stack
1153     // frame. This is not usually what you want to see when printing out a
1154     // stacktrace...
1155     find_sym_result details_call_site =
1156         find_symbol_details(fobj, trace.addr, symbol_info.dli_fbase);
1157     details_selected = &details_call_site;
1158 
1159 #if BACKWARD_HAS_UNWIND == 0
1160     // ...this is why we also try to resolve the symbol that is right
1161     // before the return address. If we are lucky enough, we will get the
1162     // line of the function that was called. But if the code is optimized,
1163     // we might get something absolutely not related since the compiler
1164     // can reschedule the return address with inline functions and
1165     // tail-call optimisation (among other things that I don't even know
1166     // or cannot even dream about with my tiny limited brain).
1167     find_sym_result details_adjusted_call_site = find_symbol_details(
1168         fobj, (void *)(uintptr_t(trace.addr) - 1), symbol_info.dli_fbase);
1169 
1170     // In debug mode, we should always get the right thing(TM).
1171     if (details_call_site.found && details_adjusted_call_site.found) {
1172       // Ok, we assume that details_adjusted_call_site is a better estimation.
1173       details_selected = &details_adjusted_call_site;
1174       trace.addr = (void *)(uintptr_t(trace.addr) - 1);
1175     }
1176 
1177     if (details_selected == &details_call_site && details_call_site.found) {
1178       // we have to re-resolve the symbol in order to reset some
1179       // internal state in BFD... so we can call backtrace_inliners
1180       // thereafter...
1181       details_call_site =
1182           find_symbol_details(fobj, trace.addr, symbol_info.dli_fbase);
1183     }
1184 #endif // BACKWARD_HAS_UNWIND
1185 
1186     if (details_selected->found) {
1187       if (details_selected->filename) {
1188         trace.source.filename = details_selected->filename;
1189       }
1190       trace.source.line = details_selected->line;
1191 
1192       if (details_selected->funcname) {
1193         // this time we get the name of the function where the code is
1194         // located, instead of the function were the address is
1195         // located. In short, if the code was inlined, we get the
1196         // function correspoding to the code. Else we already got in
1197         // trace.function.
1198         trace.source.function = demangle(details_selected->funcname);
1199 
1200         if (!symbol_info.dli_sname) {
1201           // for the case dladdr failed to find the symbol name of
1202           // the function, we might as well try to put something
1203           // here.
1204           trace.object_function = trace.source.function;
1205         }
1206       }
1207 
1208       // Maybe the source of the trace got inlined inside the function
1209       // (trace.source.function). Let's see if we can get all the inlined
1210       // calls along the way up to the initial call site.
1211       trace.inliners = backtrace_inliners(fobj, *details_selected);
1212 
1213 #if 0
1214 			if (trace.inliners.size() == 0) {
1215 				// Maybe the trace was not inlined... or maybe it was and we
1216 				// are lacking the debug information. Let's try to make the
1217 				// world better and see if we can get the line number of the
1218 				// function (trace.source.function) now.
1219 				//
1220 				// We will get the location of where the function start (to be
1221 				// exact: the first instruction that really start the
1222 				// function), not where the name of the function is defined.
1223 				// This can be quite far away from the name of the function
1224 				// btw.
1225 				//
1226 				// If the source of the function is the same as the source of
1227 				// the trace, we cannot say if the trace was really inlined or
1228 				// not.  However, if the filename of the source is different
1229 				// between the function and the trace... we can declare it as
1230 				// an inliner.  This is not 100% accurate, but better than
1231 				// nothing.
1232 
1233 				if (symbol_info.dli_saddr) {
1234 					find_sym_result details = find_symbol_details(fobj,
1235 							symbol_info.dli_saddr,
1236 							symbol_info.dli_fbase);
1237 
1238 					if (details.found) {
1239 						ResolvedTrace::SourceLoc diy_inliner;
1240 						diy_inliner.line = details.line;
1241 						if (details.filename) {
1242 							diy_inliner.filename = details.filename;
1243 						}
1244 						if (details.funcname) {
1245 							diy_inliner.function = demangle(details.funcname);
1246 						} else {
1247 							diy_inliner.function = trace.source.function;
1248 						}
1249 						if (diy_inliner != trace.source) {
1250 							trace.inliners.push_back(diy_inliner);
1251 						}
1252 					}
1253 				}
1254 			}
1255 #endif
1256     }
1257 
1258     return trace;
1259   }
1260 
1261 private:
1262   bool _bfd_loaded;
1263 
1264   typedef details::handle<bfd *,
1265                           details::deleter<bfd_boolean, bfd *, &bfd_close>>
1266       bfd_handle_t;
1267 
1268   typedef details::handle<asymbol **> bfd_symtab_t;
1269 
1270   struct bfd_fileobject {
1271     bfd_handle_t handle;
1272     bfd_vma base_addr;
1273     bfd_symtab_t symtab;
1274     bfd_symtab_t dynamic_symtab;
1275   };
1276 
1277   typedef details::hashtable<std::string, bfd_fileobject>::type fobj_bfd_map_t;
1278   fobj_bfd_map_t _fobj_bfd_map;
1279 
load_object_with_bfd(const std::string & filename_object)1280   bfd_fileobject &load_object_with_bfd(const std::string &filename_object) {
1281     using namespace details;
1282 
1283     if (!_bfd_loaded) {
1284       using namespace details;
1285       bfd_init();
1286       _bfd_loaded = true;
1287     }
1288 
1289     fobj_bfd_map_t::iterator it = _fobj_bfd_map.find(filename_object);
1290     if (it != _fobj_bfd_map.end()) {
1291       return it->second;
1292     }
1293 
1294     // this new object is empty for now.
1295     bfd_fileobject &r = _fobj_bfd_map[filename_object];
1296 
1297     // we do the work temporary in this one;
1298     bfd_handle_t bfd_handle;
1299 
1300     int fd = open(filename_object.c_str(), O_RDONLY);
1301     bfd_handle.reset(bfd_fdopenr(filename_object.c_str(), "default", fd));
1302     if (!bfd_handle) {
1303       close(fd);
1304       return r;
1305     }
1306 
1307     if (!bfd_check_format(bfd_handle.get(), bfd_object)) {
1308       return r; // not an object? You lose.
1309     }
1310 
1311     if ((bfd_get_file_flags(bfd_handle.get()) & HAS_SYMS) == 0) {
1312       return r; // that's what happen when you forget to compile in debug.
1313     }
1314 
1315     ssize_t symtab_storage_size = bfd_get_symtab_upper_bound(bfd_handle.get());
1316 
1317     ssize_t dyn_symtab_storage_size =
1318         bfd_get_dynamic_symtab_upper_bound(bfd_handle.get());
1319 
1320     if (symtab_storage_size <= 0 && dyn_symtab_storage_size <= 0) {
1321       return r; // weird, is the file is corrupted?
1322     }
1323 
1324     bfd_symtab_t symtab, dynamic_symtab;
1325     ssize_t symcount = 0, dyn_symcount = 0;
1326 
1327     if (symtab_storage_size > 0) {
1328       symtab.reset(static_cast<bfd_symbol **>(
1329           malloc(static_cast<size_t>(symtab_storage_size))));
1330       symcount = bfd_canonicalize_symtab(bfd_handle.get(), symtab.get());
1331     }
1332 
1333     if (dyn_symtab_storage_size > 0) {
1334       dynamic_symtab.reset(static_cast<bfd_symbol **>(
1335           malloc(static_cast<size_t>(dyn_symtab_storage_size))));
1336       dyn_symcount = bfd_canonicalize_dynamic_symtab(bfd_handle.get(),
1337                                                      dynamic_symtab.get());
1338     }
1339 
1340     if (symcount <= 0 && dyn_symcount <= 0) {
1341       return r; // damned, that's a stripped file that you got there!
1342     }
1343 
1344     r.handle = move(bfd_handle);
1345     r.symtab = move(symtab);
1346     r.dynamic_symtab = move(dynamic_symtab);
1347     return r;
1348   }
1349 
1350   struct find_sym_result {
1351     bool found;
1352     const char *filename;
1353     const char *funcname;
1354     unsigned int line;
1355   };
1356 
1357   struct find_sym_context {
1358     TraceResolverLinuxImpl *self;
1359     bfd_fileobject *fobj;
1360     void *addr;
1361     void *base_addr;
1362     find_sym_result result;
1363   };
1364 
find_symbol_details(bfd_fileobject & fobj,void * addr,void * base_addr)1365   find_sym_result find_symbol_details(bfd_fileobject &fobj, void *addr,
1366                                       void *base_addr) {
1367     find_sym_context context;
1368     context.self = this;
1369     context.fobj = &fobj;
1370     context.addr = addr;
1371     context.base_addr = base_addr;
1372     context.result.found = false;
1373     bfd_map_over_sections(fobj.handle.get(), &find_in_section_trampoline,
1374                           static_cast<void *>(&context));
1375     return context.result;
1376   }
1377 
find_in_section_trampoline(bfd *,asection * section,void * data)1378   static void find_in_section_trampoline(bfd *, asection *section, void *data) {
1379     find_sym_context *context = static_cast<find_sym_context *>(data);
1380     context->self->find_in_section(
1381         reinterpret_cast<bfd_vma>(context->addr),
1382         reinterpret_cast<bfd_vma>(context->base_addr), *context->fobj, section,
1383         context->result);
1384   }
1385 
find_in_section(bfd_vma addr,bfd_vma base_addr,bfd_fileobject & fobj,asection * section,find_sym_result & result)1386   void find_in_section(bfd_vma addr, bfd_vma base_addr, bfd_fileobject &fobj,
1387                        asection *section, find_sym_result &result) {
1388     if (result.found)
1389       return;
1390 
1391 #ifdef bfd_get_section_flags
1392     if ((bfd_get_section_flags(fobj.handle.get(), section) & SEC_ALLOC) == 0)
1393 #else
1394     if ((bfd_section_flags(section) & SEC_ALLOC) == 0)
1395 #endif
1396       return; // a debug section is never loaded automatically.
1397 
1398 #ifdef bfd_get_section_vma
1399     bfd_vma sec_addr = bfd_get_section_vma(fobj.handle.get(), section);
1400 #else
1401     bfd_vma sec_addr = bfd_section_vma(section);
1402 #endif
1403 #ifdef bfd_get_section_size
1404     bfd_size_type size = bfd_get_section_size(section);
1405 #else
1406     bfd_size_type size = bfd_section_size(section);
1407 #endif
1408 
1409     // are we in the boundaries of the section?
1410     if (addr < sec_addr || addr >= sec_addr + size) {
1411       addr -= base_addr; // oups, a relocated object, lets try again...
1412       if (addr < sec_addr || addr >= sec_addr + size) {
1413         return;
1414       }
1415     }
1416 
1417 #if defined(__clang__)
1418 #pragma clang diagnostic push
1419 #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
1420 #endif
1421     if (!result.found && fobj.symtab) {
1422       result.found = bfd_find_nearest_line(
1423           fobj.handle.get(), section, fobj.symtab.get(), addr - sec_addr,
1424           &result.filename, &result.funcname, &result.line);
1425     }
1426 
1427     if (!result.found && fobj.dynamic_symtab) {
1428       result.found = bfd_find_nearest_line(
1429           fobj.handle.get(), section, fobj.dynamic_symtab.get(),
1430           addr - sec_addr, &result.filename, &result.funcname, &result.line);
1431     }
1432 #if defined(__clang__)
1433 #pragma clang diagnostic pop
1434 #endif
1435   }
1436 
1437   ResolvedTrace::source_locs_t
backtrace_inliners(bfd_fileobject & fobj,find_sym_result previous_result)1438   backtrace_inliners(bfd_fileobject &fobj, find_sym_result previous_result) {
1439     // This function can be called ONLY after a SUCCESSFUL call to
1440     // find_symbol_details. The state is global to the bfd_handle.
1441     ResolvedTrace::source_locs_t results;
1442     while (previous_result.found) {
1443       find_sym_result result;
1444       result.found = bfd_find_inliner_info(fobj.handle.get(), &result.filename,
1445                                            &result.funcname, &result.line);
1446 
1447       if (result
1448               .found) /* and not (
1449                             cstrings_eq(previous_result.filename,
1450                          result.filename) and
1451                          cstrings_eq(previous_result.funcname, result.funcname)
1452                             and result.line == previous_result.line
1453                             )) */
1454       {
1455         ResolvedTrace::SourceLoc src_loc;
1456         src_loc.line = result.line;
1457         if (result.filename) {
1458           src_loc.filename = result.filename;
1459         }
1460         if (result.funcname) {
1461           src_loc.function = demangle(result.funcname);
1462         }
1463         results.push_back(src_loc);
1464       }
1465       previous_result = result;
1466     }
1467     return results;
1468   }
1469 
cstrings_eq(const char * a,const char * b)1470   bool cstrings_eq(const char *a, const char *b) {
1471     if (!a || !b) {
1472       return false;
1473     }
1474     return strcmp(a, b) == 0;
1475   }
1476 };
1477 #endif // BACKWARD_HAS_BFD == 1
1478 
1479 #if BACKWARD_HAS_DW == 1
1480 
1481 template <>
1482 class TraceResolverLinuxImpl<trace_resolver_tag::libdw>
1483     : public TraceResolverLinuxBase {
1484 public:
TraceResolverLinuxImpl()1485   TraceResolverLinuxImpl() : _dwfl_handle_initialized(false) {}
1486 
load_stacktrace(ST &)1487   template <class ST> void load_stacktrace(ST &) {}
1488 
resolve(ResolvedTrace trace)1489   ResolvedTrace resolve(ResolvedTrace trace) {
1490     using namespace details;
1491 
1492     Dwarf_Addr trace_addr = (Dwarf_Addr)trace.addr;
1493 
1494     if (!_dwfl_handle_initialized) {
1495       // initialize dwfl...
1496       _dwfl_cb.reset(new Dwfl_Callbacks);
1497       _dwfl_cb->find_elf = &dwfl_linux_proc_find_elf;
1498       _dwfl_cb->find_debuginfo = &dwfl_standard_find_debuginfo;
1499       _dwfl_cb->debuginfo_path = 0;
1500 
1501       _dwfl_handle.reset(dwfl_begin(_dwfl_cb.get()));
1502       _dwfl_handle_initialized = true;
1503 
1504       if (!_dwfl_handle) {
1505         return trace;
1506       }
1507 
1508       // ...from the current process.
1509       dwfl_report_begin(_dwfl_handle.get());
1510       int r = dwfl_linux_proc_report(_dwfl_handle.get(), getpid());
1511       dwfl_report_end(_dwfl_handle.get(), NULL, NULL);
1512       if (r < 0) {
1513         return trace;
1514       }
1515     }
1516 
1517     if (!_dwfl_handle) {
1518       return trace;
1519     }
1520 
1521     // find the module (binary object) that contains the trace's address.
1522     // This is not using any debug information, but the addresses ranges of
1523     // all the currently loaded binary object.
1524     Dwfl_Module *mod = dwfl_addrmodule(_dwfl_handle.get(), trace_addr);
1525     if (mod) {
1526       // now that we found it, lets get the name of it, this will be the
1527       // full path to the running binary or one of the loaded library.
1528       const char *module_name = dwfl_module_info(mod, 0, 0, 0, 0, 0, 0, 0);
1529       if (module_name) {
1530         trace.object_filename = module_name;
1531       }
1532       // We also look after the name of the symbol, equal or before this
1533       // address. This is found by walking the symtab. We should get the
1534       // symbol corresponding to the function (mangled) containing the
1535       // address. If the code corresponding to the address was inlined,
1536       // this is the name of the out-most inliner function.
1537       const char *sym_name = dwfl_module_addrname(mod, trace_addr);
1538       if (sym_name) {
1539         trace.object_function = demangle(sym_name);
1540       }
1541     }
1542 
1543     // now let's get serious, and find out the source location (file and
1544     // line number) of the address.
1545 
1546     // This function will look in .debug_aranges for the address and map it
1547     // to the location of the compilation unit DIE in .debug_info and
1548     // return it.
1549     Dwarf_Addr mod_bias = 0;
1550     Dwarf_Die *cudie = dwfl_module_addrdie(mod, trace_addr, &mod_bias);
1551 
1552 #if 1
1553     if (!cudie) {
1554       // Sadly clang does not generate the section .debug_aranges, thus
1555       // dwfl_module_addrdie will fail early. Clang doesn't either set
1556       // the lowpc/highpc/range info for every compilation unit.
1557       //
1558       // So in order to save the world:
1559       // for every compilation unit, we will iterate over every single
1560       // DIEs. Normally functions should have a lowpc/highpc/range, which
1561       // we will use to infer the compilation unit.
1562 
1563       // note that this is probably badly inefficient.
1564       while ((cudie = dwfl_module_nextcu(mod, cudie, &mod_bias))) {
1565         Dwarf_Die die_mem;
1566         Dwarf_Die *fundie =
1567             find_fundie_by_pc(cudie, trace_addr - mod_bias, &die_mem);
1568         if (fundie) {
1569           break;
1570         }
1571       }
1572     }
1573 #endif
1574 
1575 //#define BACKWARD_I_DO_NOT_RECOMMEND_TO_ENABLE_THIS_HORRIBLE_PIECE_OF_CODE
1576 #ifdef BACKWARD_I_DO_NOT_RECOMMEND_TO_ENABLE_THIS_HORRIBLE_PIECE_OF_CODE
1577     if (!cudie) {
1578       // If it's still not enough, lets dive deeper in the shit, and try
1579       // to save the world again: for every compilation unit, we will
1580       // load the corresponding .debug_line section, and see if we can
1581       // find our address in it.
1582 
1583       Dwarf_Addr cfi_bias;
1584       Dwarf_CFI *cfi_cache = dwfl_module_eh_cfi(mod, &cfi_bias);
1585 
1586       Dwarf_Addr bias;
1587       while ((cudie = dwfl_module_nextcu(mod, cudie, &bias))) {
1588         if (dwarf_getsrc_die(cudie, trace_addr - bias)) {
1589 
1590           // ...but if we get a match, it might be a false positive
1591           // because our (address - bias) might as well be valid in a
1592           // different compilation unit. So we throw our last card on
1593           // the table and lookup for the address into the .eh_frame
1594           // section.
1595 
1596           handle<Dwarf_Frame *> frame;
1597           dwarf_cfi_addrframe(cfi_cache, trace_addr - cfi_bias, &frame);
1598           if (frame) {
1599             break;
1600           }
1601         }
1602       }
1603     }
1604 #endif
1605 
1606     if (!cudie) {
1607       return trace; // this time we lost the game :/
1608     }
1609 
1610     // Now that we have a compilation unit DIE, this function will be able
1611     // to load the corresponding section in .debug_line (if not already
1612     // loaded) and hopefully find the source location mapped to our
1613     // address.
1614     Dwarf_Line *srcloc = dwarf_getsrc_die(cudie, trace_addr - mod_bias);
1615 
1616     if (srcloc) {
1617       const char *srcfile = dwarf_linesrc(srcloc, 0, 0);
1618       if (srcfile) {
1619         trace.source.filename = srcfile;
1620       }
1621       int line = 0, col = 0;
1622       dwarf_lineno(srcloc, &line);
1623       dwarf_linecol(srcloc, &col);
1624       trace.source.line = line;
1625       trace.source.col = col;
1626     }
1627 
1628     deep_first_search_by_pc(cudie, trace_addr - mod_bias,
1629                             inliners_search_cb(trace));
1630     if (trace.source.function.size() == 0) {
1631       // fallback.
1632       trace.source.function = trace.object_function;
1633     }
1634 
1635     return trace;
1636   }
1637 
1638 private:
1639   typedef details::handle<Dwfl *, details::deleter<void, Dwfl *, &dwfl_end>>
1640       dwfl_handle_t;
1641   details::handle<Dwfl_Callbacks *, details::default_delete<Dwfl_Callbacks *>>
1642       _dwfl_cb;
1643   dwfl_handle_t _dwfl_handle;
1644   bool _dwfl_handle_initialized;
1645 
1646   // defined here because in C++98, template function cannot take locally
1647   // defined types... grrr.
1648   struct inliners_search_cb {
operator ()backward::TraceResolverLinuxImpl::inliners_search_cb1649     void operator()(Dwarf_Die *die) {
1650       switch (dwarf_tag(die)) {
1651         const char *name;
1652       case DW_TAG_subprogram:
1653         if ((name = dwarf_diename(die))) {
1654           trace.source.function = name;
1655         }
1656         break;
1657 
1658       case DW_TAG_inlined_subroutine:
1659         ResolvedTrace::SourceLoc sloc;
1660         Dwarf_Attribute attr_mem;
1661 
1662         if ((name = dwarf_diename(die))) {
1663           sloc.function = name;
1664         }
1665         if ((name = die_call_file(die))) {
1666           sloc.filename = name;
1667         }
1668 
1669         Dwarf_Word line = 0, col = 0;
1670         dwarf_formudata(dwarf_attr(die, DW_AT_call_line, &attr_mem), &line);
1671         dwarf_formudata(dwarf_attr(die, DW_AT_call_column, &attr_mem), &col);
1672         sloc.line = (unsigned)line;
1673         sloc.col = (unsigned)col;
1674 
1675         trace.inliners.push_back(sloc);
1676         break;
1677       };
1678     }
1679     ResolvedTrace &trace;
inliners_search_cbbackward::TraceResolverLinuxImpl::inliners_search_cb1680     inliners_search_cb(ResolvedTrace &t) : trace(t) {}
1681   };
1682 
die_has_pc(Dwarf_Die * die,Dwarf_Addr pc)1683   static bool die_has_pc(Dwarf_Die *die, Dwarf_Addr pc) {
1684     Dwarf_Addr low, high;
1685 
1686     // continuous range
1687     if (dwarf_hasattr(die, DW_AT_low_pc) && dwarf_hasattr(die, DW_AT_high_pc)) {
1688       if (dwarf_lowpc(die, &low) != 0) {
1689         return false;
1690       }
1691       if (dwarf_highpc(die, &high) != 0) {
1692         Dwarf_Attribute attr_mem;
1693         Dwarf_Attribute *attr = dwarf_attr(die, DW_AT_high_pc, &attr_mem);
1694         Dwarf_Word value;
1695         if (dwarf_formudata(attr, &value) != 0) {
1696           return false;
1697         }
1698         high = low + value;
1699       }
1700       return pc >= low && pc < high;
1701     }
1702 
1703     // non-continuous range.
1704     Dwarf_Addr base;
1705     ptrdiff_t offset = 0;
1706     while ((offset = dwarf_ranges(die, offset, &base, &low, &high)) > 0) {
1707       if (pc >= low && pc < high) {
1708         return true;
1709       }
1710     }
1711     return false;
1712   }
1713 
find_fundie_by_pc(Dwarf_Die * parent_die,Dwarf_Addr pc,Dwarf_Die * result)1714   static Dwarf_Die *find_fundie_by_pc(Dwarf_Die *parent_die, Dwarf_Addr pc,
1715                                       Dwarf_Die *result) {
1716     if (dwarf_child(parent_die, result) != 0) {
1717       return 0;
1718     }
1719 
1720     Dwarf_Die *die = result;
1721     do {
1722       switch (dwarf_tag(die)) {
1723       case DW_TAG_subprogram:
1724       case DW_TAG_inlined_subroutine:
1725         if (die_has_pc(die, pc)) {
1726           return result;
1727         }
1728       };
1729       bool declaration = false;
1730       Dwarf_Attribute attr_mem;
1731       dwarf_formflag(dwarf_attr(die, DW_AT_declaration, &attr_mem),
1732                      &declaration);
1733       if (!declaration) {
1734         // let's be curious and look deeper in the tree,
1735         // function are not necessarily at the first level, but
1736         // might be nested inside a namespace, structure etc.
1737         Dwarf_Die die_mem;
1738         Dwarf_Die *indie = find_fundie_by_pc(die, pc, &die_mem);
1739         if (indie) {
1740           *result = die_mem;
1741           return result;
1742         }
1743       }
1744     } while (dwarf_siblingof(die, result) == 0);
1745     return 0;
1746   }
1747 
1748   template <typename CB>
deep_first_search_by_pc(Dwarf_Die * parent_die,Dwarf_Addr pc,CB cb)1749   static bool deep_first_search_by_pc(Dwarf_Die *parent_die, Dwarf_Addr pc,
1750                                       CB cb) {
1751     Dwarf_Die die_mem;
1752     if (dwarf_child(parent_die, &die_mem) != 0) {
1753       return false;
1754     }
1755 
1756     bool branch_has_pc = false;
1757     Dwarf_Die *die = &die_mem;
1758     do {
1759       bool declaration = false;
1760       Dwarf_Attribute attr_mem;
1761       dwarf_formflag(dwarf_attr(die, DW_AT_declaration, &attr_mem),
1762                      &declaration);
1763       if (!declaration) {
1764         // let's be curious and look deeper in the tree, function are
1765         // not necessarily at the first level, but might be nested
1766         // inside a namespace, structure, a function, an inlined
1767         // function etc.
1768         branch_has_pc = deep_first_search_by_pc(die, pc, cb);
1769       }
1770       if (!branch_has_pc) {
1771         branch_has_pc = die_has_pc(die, pc);
1772       }
1773       if (branch_has_pc) {
1774         cb(die);
1775       }
1776     } while (dwarf_siblingof(die, &die_mem) == 0);
1777     return branch_has_pc;
1778   }
1779 
die_call_file(Dwarf_Die * die)1780   static const char *die_call_file(Dwarf_Die *die) {
1781     Dwarf_Attribute attr_mem;
1782     Dwarf_Sword file_idx = 0;
1783 
1784     dwarf_formsdata(dwarf_attr(die, DW_AT_call_file, &attr_mem), &file_idx);
1785 
1786     if (file_idx == 0) {
1787       return 0;
1788     }
1789 
1790     Dwarf_Die die_mem;
1791     Dwarf_Die *cudie = dwarf_diecu(die, &die_mem, 0, 0);
1792     if (!cudie) {
1793       return 0;
1794     }
1795 
1796     Dwarf_Files *files = 0;
1797     size_t nfiles;
1798     dwarf_getsrcfiles(cudie, &files, &nfiles);
1799     if (!files) {
1800       return 0;
1801     }
1802 
1803     return dwarf_filesrc(files, file_idx, 0, 0);
1804   }
1805 };
1806 #endif // BACKWARD_HAS_DW == 1
1807 
1808 #if BACKWARD_HAS_DWARF == 1
1809 
1810 template <>
1811 class TraceResolverLinuxImpl<trace_resolver_tag::libdwarf>
1812     : public TraceResolverLinuxBase {
1813 public:
TraceResolverLinuxImpl()1814   TraceResolverLinuxImpl() : _dwarf_loaded(false) {}
1815 
load_stacktrace(ST &)1816   template <class ST> void load_stacktrace(ST &) {}
1817 
resolve(ResolvedTrace trace)1818   ResolvedTrace resolve(ResolvedTrace trace) {
1819     // trace.addr is a virtual address in memory pointing to some code.
1820     // Let's try to find from which loaded object it comes from.
1821     // The loaded object can be yourself btw.
1822 
1823     Dl_info symbol_info;
1824     int dladdr_result = 0;
1825 #if defined(__GLIBC__)
1826     link_map *link_map;
1827     // We request the link map so we can get information about offsets
1828     dladdr_result =
1829         dladdr1(trace.addr, &symbol_info, reinterpret_cast<void **>(&link_map),
1830                 RTLD_DL_LINKMAP);
1831 #else
1832     // Android doesn't have dladdr1. Don't use the linker map.
1833     dladdr_result = dladdr(trace.addr, &symbol_info);
1834 #endif
1835     if (!dladdr_result) {
1836       return trace; // dat broken trace...
1837     }
1838 
1839     // Now we get in symbol_info:
1840     // .dli_fname:
1841     //      pathname of the shared object that contains the address.
1842     // .dli_fbase:
1843     //      where the object is loaded in memory.
1844     // .dli_sname:
1845     //      the name of the nearest symbol to trace.addr, we expect a
1846     //      function name.
1847     // .dli_saddr:
1848     //      the exact address corresponding to .dli_sname.
1849     //
1850     // And in link_map:
1851     // .l_addr:
1852     //      difference between the address in the ELF file and the address
1853     //      in memory
1854     // l_name:
1855     //      absolute pathname where the object was found
1856 
1857     if (symbol_info.dli_sname) {
1858       trace.object_function = demangle(symbol_info.dli_sname);
1859     }
1860 
1861     if (!symbol_info.dli_fname) {
1862       return trace;
1863     }
1864 
1865     trace.object_filename = resolve_exec_path(symbol_info);
1866     dwarf_fileobject &fobj = load_object_with_dwarf(symbol_info.dli_fname);
1867     if (!fobj.dwarf_handle) {
1868       return trace; // sad, we couldn't load the object :(
1869     }
1870 
1871 #if defined(__GLIBC__)
1872     // Convert the address to a module relative one by looking at
1873     // the module's loading address in the link map
1874     Dwarf_Addr address = reinterpret_cast<uintptr_t>(trace.addr) -
1875                          reinterpret_cast<uintptr_t>(link_map->l_addr);
1876 #else
1877     Dwarf_Addr address = reinterpret_cast<uintptr_t>(trace.addr);
1878 #endif
1879 
1880     if (trace.object_function.empty()) {
1881       symbol_cache_t::iterator it = fobj.symbol_cache.lower_bound(address);
1882 
1883       if (it != fobj.symbol_cache.end()) {
1884         if (it->first != address) {
1885           if (it != fobj.symbol_cache.begin()) {
1886             --it;
1887           }
1888         }
1889         trace.object_function = demangle(it->second.c_str());
1890       }
1891     }
1892 
1893     // Get the Compilation Unit DIE for the address
1894     Dwarf_Die die = find_die(fobj, address);
1895 
1896     if (!die) {
1897       return trace; // this time we lost the game :/
1898     }
1899 
1900     // libdwarf doesn't give us direct access to its objects, it always
1901     // allocates a copy for the caller. We keep that copy alive in a cache
1902     // and we deallocate it later when it's no longer required.
1903     die_cache_entry &die_object = get_die_cache(fobj, die);
1904     if (die_object.isEmpty())
1905       return trace; // We have no line section for this DIE
1906 
1907     die_linemap_t::iterator it = die_object.line_section.lower_bound(address);
1908 
1909     if (it != die_object.line_section.end()) {
1910       if (it->first != address) {
1911         if (it == die_object.line_section.begin()) {
1912           // If we are on the first item of the line section
1913           // but the address does not match it means that
1914           // the address is below the range of the DIE. Give up.
1915           return trace;
1916         } else {
1917           --it;
1918         }
1919       }
1920     } else {
1921       return trace; // We didn't find the address.
1922     }
1923 
1924     // Get the Dwarf_Line that the address points to and call libdwarf
1925     // to get source file, line and column info.
1926     Dwarf_Line line = die_object.line_buffer[it->second];
1927     Dwarf_Error error = DW_DLE_NE;
1928 
1929     char *filename;
1930     if (dwarf_linesrc(line, &filename, &error) == DW_DLV_OK) {
1931       trace.source.filename = std::string(filename);
1932       dwarf_dealloc(fobj.dwarf_handle.get(), filename, DW_DLA_STRING);
1933     }
1934 
1935     Dwarf_Unsigned number = 0;
1936     if (dwarf_lineno(line, &number, &error) == DW_DLV_OK) {
1937       trace.source.line = number;
1938     } else {
1939       trace.source.line = 0;
1940     }
1941 
1942     if (dwarf_lineoff_b(line, &number, &error) == DW_DLV_OK) {
1943       trace.source.col = number;
1944     } else {
1945       trace.source.col = 0;
1946     }
1947 
1948     std::vector<std::string> namespace_stack;
1949     deep_first_search_by_pc(fobj, die, address, namespace_stack,
1950                             inliners_search_cb(trace, fobj, die));
1951 
1952     dwarf_dealloc(fobj.dwarf_handle.get(), die, DW_DLA_DIE);
1953 
1954     return trace;
1955   }
1956 
1957 public:
close_dwarf(Dwarf_Debug dwarf)1958   static int close_dwarf(Dwarf_Debug dwarf) {
1959     return dwarf_finish(dwarf, NULL);
1960   }
1961 
1962 private:
1963   bool _dwarf_loaded;
1964 
1965   typedef details::handle<int, details::deleter<int, int, &::close>>
1966       dwarf_file_t;
1967 
1968   typedef details::handle<Elf *, details::deleter<int, Elf *, &elf_end>>
1969       dwarf_elf_t;
1970 
1971   typedef details::handle<Dwarf_Debug,
1972                           details::deleter<int, Dwarf_Debug, &close_dwarf>>
1973       dwarf_handle_t;
1974 
1975   typedef std::map<Dwarf_Addr, int> die_linemap_t;
1976 
1977   typedef std::map<Dwarf_Off, Dwarf_Off> die_specmap_t;
1978 
1979   struct die_cache_entry {
1980     die_specmap_t spec_section;
1981     die_linemap_t line_section;
1982     Dwarf_Line *line_buffer;
1983     Dwarf_Signed line_count;
1984     Dwarf_Line_Context line_context;
1985 
isEmptybackward::TraceResolverLinuxImpl::die_cache_entry1986     inline bool isEmpty() {
1987       return line_buffer == NULL || line_count == 0 || line_context == NULL ||
1988              line_section.empty();
1989     }
1990 
die_cache_entrybackward::TraceResolverLinuxImpl::die_cache_entry1991     die_cache_entry() : line_buffer(0), line_count(0), line_context(0) {}
1992 
~die_cache_entrybackward::TraceResolverLinuxImpl::die_cache_entry1993     ~die_cache_entry() {
1994       if (line_context) {
1995         dwarf_srclines_dealloc_b(line_context);
1996       }
1997     }
1998   };
1999 
2000   typedef std::map<Dwarf_Off, die_cache_entry> die_cache_t;
2001 
2002   typedef std::map<uintptr_t, std::string> symbol_cache_t;
2003 
2004   struct dwarf_fileobject {
2005     dwarf_file_t file_handle;
2006     dwarf_elf_t elf_handle;
2007     dwarf_handle_t dwarf_handle;
2008     symbol_cache_t symbol_cache;
2009 
2010     // Die cache
2011     die_cache_t die_cache;
2012     die_cache_entry *current_cu;
2013   };
2014 
2015   typedef details::hashtable<std::string, dwarf_fileobject>::type
2016       fobj_dwarf_map_t;
2017   fobj_dwarf_map_t _fobj_dwarf_map;
2018 
cstrings_eq(const char * a,const char * b)2019   static bool cstrings_eq(const char *a, const char *b) {
2020     if (!a || !b) {
2021       return false;
2022     }
2023     return strcmp(a, b) == 0;
2024   }
2025 
load_object_with_dwarf(const std::string & filename_object)2026   dwarf_fileobject &load_object_with_dwarf(const std::string &filename_object) {
2027 
2028     if (!_dwarf_loaded) {
2029       // Set the ELF library operating version
2030       // If that fails there's nothing we can do
2031       _dwarf_loaded = elf_version(EV_CURRENT) != EV_NONE;
2032     }
2033 
2034     fobj_dwarf_map_t::iterator it = _fobj_dwarf_map.find(filename_object);
2035     if (it != _fobj_dwarf_map.end()) {
2036       return it->second;
2037     }
2038 
2039     // this new object is empty for now
2040     dwarf_fileobject &r = _fobj_dwarf_map[filename_object];
2041 
2042     dwarf_file_t file_handle;
2043     file_handle.reset(open(filename_object.c_str(), O_RDONLY));
2044     if (file_handle.get() < 0) {
2045       return r;
2046     }
2047 
2048     // Try to get an ELF handle. We need to read the ELF sections
2049     // because we want to see if there is a .gnu_debuglink section
2050     // that points to a split debug file
2051     dwarf_elf_t elf_handle;
2052     elf_handle.reset(elf_begin(file_handle.get(), ELF_C_READ, NULL));
2053     if (!elf_handle) {
2054       return r;
2055     }
2056 
2057     const char *e_ident = elf_getident(elf_handle.get(), 0);
2058     if (!e_ident) {
2059       return r;
2060     }
2061 
2062     // Get the number of sections
2063     // We use the new APIs as elf_getshnum is deprecated
2064     size_t shdrnum = 0;
2065     if (elf_getshdrnum(elf_handle.get(), &shdrnum) == -1) {
2066       return r;
2067     }
2068 
2069     // Get the index to the string section
2070     size_t shdrstrndx = 0;
2071     if (elf_getshdrstrndx(elf_handle.get(), &shdrstrndx) == -1) {
2072       return r;
2073     }
2074 
2075     std::string debuglink;
2076     // Iterate through the ELF sections to try to get a gnu_debuglink
2077     // note and also to cache the symbol table.
2078     // We go the preprocessor way to avoid having to create templated
2079     // classes or using gelf (which might throw a compiler error if 64 bit
2080     // is not supported
2081 #define ELF_GET_DATA(ARCH)                                                     \
2082   Elf_Scn *elf_section = 0;                                                    \
2083   Elf_Data *elf_data = 0;                                                      \
2084   Elf##ARCH##_Shdr *section_header = 0;                                        \
2085   Elf_Scn *symbol_section = 0;                                                 \
2086   size_t symbol_count = 0;                                                     \
2087   size_t symbol_strings = 0;                                                   \
2088   Elf##ARCH##_Sym *symbol = 0;                                                 \
2089   const char *section_name = 0;                                                \
2090                                                                                \
2091   while ((elf_section = elf_nextscn(elf_handle.get(), elf_section)) != NULL) { \
2092     section_header = elf##ARCH##_getshdr(elf_section);                         \
2093     if (section_header == NULL) {                                              \
2094       return r;                                                                \
2095     }                                                                          \
2096                                                                                \
2097     if ((section_name = elf_strptr(elf_handle.get(), shdrstrndx,               \
2098                                    section_header->sh_name)) == NULL) {        \
2099       return r;                                                                \
2100     }                                                                          \
2101                                                                                \
2102     if (cstrings_eq(section_name, ".gnu_debuglink")) {                         \
2103       elf_data = elf_getdata(elf_section, NULL);                               \
2104       if (elf_data && elf_data->d_size > 0) {                                  \
2105         debuglink =                                                            \
2106             std::string(reinterpret_cast<const char *>(elf_data->d_buf));      \
2107       }                                                                        \
2108     }                                                                          \
2109                                                                                \
2110     switch (section_header->sh_type) {                                         \
2111     case SHT_SYMTAB:                                                           \
2112       symbol_section = elf_section;                                            \
2113       symbol_count = section_header->sh_size / section_header->sh_entsize;     \
2114       symbol_strings = section_header->sh_link;                                \
2115       break;                                                                   \
2116                                                                                \
2117     /* We use .dynsyms as a last resort, we prefer .symtab */                  \
2118     case SHT_DYNSYM:                                                           \
2119       if (!symbol_section) {                                                   \
2120         symbol_section = elf_section;                                          \
2121         symbol_count = section_header->sh_size / section_header->sh_entsize;   \
2122         symbol_strings = section_header->sh_link;                              \
2123       }                                                                        \
2124       break;                                                                   \
2125     }                                                                          \
2126   }                                                                            \
2127                                                                                \
2128   if (symbol_section && symbol_count && symbol_strings) {                      \
2129     elf_data = elf_getdata(symbol_section, NULL);                              \
2130     symbol = reinterpret_cast<Elf##ARCH##_Sym *>(elf_data->d_buf);             \
2131     for (size_t i = 0; i < symbol_count; ++i) {                                \
2132       int type = ELF##ARCH##_ST_TYPE(symbol->st_info);                         \
2133       if (type == STT_FUNC && symbol->st_value > 0) {                          \
2134         r.symbol_cache[symbol->st_value] = std::string(                        \
2135             elf_strptr(elf_handle.get(), symbol_strings, symbol->st_name));    \
2136       }                                                                        \
2137       ++symbol;                                                                \
2138     }                                                                          \
2139   }
2140 
2141     if (e_ident[EI_CLASS] == ELFCLASS32) {
2142       ELF_GET_DATA(32)
2143     } else if (e_ident[EI_CLASS] == ELFCLASS64) {
2144       // libelf might have been built without 64 bit support
2145 #if __LIBELF64
2146       ELF_GET_DATA(64)
2147 #endif
2148     }
2149 
2150     if (!debuglink.empty()) {
2151       // We have a debuglink section! Open an elf instance on that
2152       // file instead. If we can't open the file, then return
2153       // the elf handle we had already opened.
2154       dwarf_file_t debuglink_file;
2155       debuglink_file.reset(open(debuglink.c_str(), O_RDONLY));
2156       if (debuglink_file.get() > 0) {
2157         dwarf_elf_t debuglink_elf;
2158         debuglink_elf.reset(elf_begin(debuglink_file.get(), ELF_C_READ, NULL));
2159 
2160         // If we have a valid elf handle, return the new elf handle
2161         // and file handle and discard the original ones
2162         if (debuglink_elf) {
2163           elf_handle = move(debuglink_elf);
2164           file_handle = move(debuglink_file);
2165         }
2166       }
2167     }
2168 
2169     // Ok, we have a valid ELF handle, let's try to get debug symbols
2170     Dwarf_Debug dwarf_debug;
2171     Dwarf_Error error = DW_DLE_NE;
2172     dwarf_handle_t dwarf_handle;
2173 
2174     int dwarf_result = dwarf_elf_init(elf_handle.get(), DW_DLC_READ, NULL, NULL,
2175                                       &dwarf_debug, &error);
2176 
2177     // We don't do any special handling for DW_DLV_NO_ENTRY specially.
2178     // If we get an error, or the file doesn't have debug information
2179     // we just return.
2180     if (dwarf_result != DW_DLV_OK) {
2181       return r;
2182     }
2183 
2184     dwarf_handle.reset(dwarf_debug);
2185 
2186     r.file_handle = move(file_handle);
2187     r.elf_handle = move(elf_handle);
2188     r.dwarf_handle = move(dwarf_handle);
2189 
2190     return r;
2191   }
2192 
get_die_cache(dwarf_fileobject & fobj,Dwarf_Die die)2193   die_cache_entry &get_die_cache(dwarf_fileobject &fobj, Dwarf_Die die) {
2194     Dwarf_Error error = DW_DLE_NE;
2195 
2196     // Get the die offset, we use it as the cache key
2197     Dwarf_Off die_offset;
2198     if (dwarf_dieoffset(die, &die_offset, &error) != DW_DLV_OK) {
2199       die_offset = 0;
2200     }
2201 
2202     die_cache_t::iterator it = fobj.die_cache.find(die_offset);
2203 
2204     if (it != fobj.die_cache.end()) {
2205       fobj.current_cu = &it->second;
2206       return it->second;
2207     }
2208 
2209     die_cache_entry &de = fobj.die_cache[die_offset];
2210     fobj.current_cu = &de;
2211 
2212     Dwarf_Addr line_addr;
2213     Dwarf_Small table_count;
2214 
2215     // The addresses in the line section are not fully sorted (they might
2216     // be sorted by block of code belonging to the same file), which makes
2217     // it necessary to do so before searching is possible.
2218     //
2219     // As libdwarf allocates a copy of everything, let's get the contents
2220     // of the line section and keep it around. We also create a map of
2221     // program counter to line table indices so we can search by address
2222     // and get the line buffer index.
2223     //
2224     // To make things more difficult, the same address can span more than
2225     // one line, so we need to keep the index pointing to the first line
2226     // by using insert instead of the map's [ operator.
2227 
2228     // Get the line context for the DIE
2229     if (dwarf_srclines_b(die, 0, &table_count, &de.line_context, &error) ==
2230         DW_DLV_OK) {
2231       // Get the source lines for this line context, to be deallocated
2232       // later
2233       if (dwarf_srclines_from_linecontext(de.line_context, &de.line_buffer,
2234                                           &de.line_count,
2235                                           &error) == DW_DLV_OK) {
2236 
2237         // Add all the addresses to our map
2238         for (int i = 0; i < de.line_count; i++) {
2239           if (dwarf_lineaddr(de.line_buffer[i], &line_addr, &error) !=
2240               DW_DLV_OK) {
2241             line_addr = 0;
2242           }
2243           de.line_section.insert(std::pair<Dwarf_Addr, int>(line_addr, i));
2244         }
2245       }
2246     }
2247 
2248     // For each CU, cache the function DIEs that contain the
2249     // DW_AT_specification attribute. When building with -g3 the function
2250     // DIEs are separated in declaration and specification, with the
2251     // declaration containing only the name and parameters and the
2252     // specification the low/high pc and other compiler attributes.
2253     //
2254     // We cache those specifications so we don't skip over the declarations,
2255     // because they have no pc, and we can do namespace resolution for
2256     // DWARF function names.
2257     Dwarf_Debug dwarf = fobj.dwarf_handle.get();
2258     Dwarf_Die current_die = 0;
2259     if (dwarf_child(die, &current_die, &error) == DW_DLV_OK) {
2260       for (;;) {
2261         Dwarf_Die sibling_die = 0;
2262 
2263         Dwarf_Half tag_value;
2264         dwarf_tag(current_die, &tag_value, &error);
2265 
2266         if (tag_value == DW_TAG_subprogram ||
2267             tag_value == DW_TAG_inlined_subroutine) {
2268 
2269           Dwarf_Bool has_attr = 0;
2270           if (dwarf_hasattr(current_die, DW_AT_specification, &has_attr,
2271                             &error) == DW_DLV_OK) {
2272             if (has_attr) {
2273               Dwarf_Attribute attr_mem;
2274               if (dwarf_attr(current_die, DW_AT_specification, &attr_mem,
2275                              &error) == DW_DLV_OK) {
2276                 Dwarf_Off spec_offset = 0;
2277                 if (dwarf_formref(attr_mem, &spec_offset, &error) ==
2278                     DW_DLV_OK) {
2279                   Dwarf_Off spec_die_offset;
2280                   if (dwarf_dieoffset(current_die, &spec_die_offset, &error) ==
2281                       DW_DLV_OK) {
2282                     de.spec_section[spec_offset] = spec_die_offset;
2283                   }
2284                 }
2285               }
2286               dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2287             }
2288           }
2289         }
2290 
2291         int result = dwarf_siblingof(dwarf, current_die, &sibling_die, &error);
2292         if (result == DW_DLV_ERROR) {
2293           break;
2294         } else if (result == DW_DLV_NO_ENTRY) {
2295           break;
2296         }
2297 
2298         if (current_die != die) {
2299           dwarf_dealloc(dwarf, current_die, DW_DLA_DIE);
2300           current_die = 0;
2301         }
2302 
2303         current_die = sibling_die;
2304       }
2305     }
2306     return de;
2307   }
2308 
get_referenced_die(Dwarf_Debug dwarf,Dwarf_Die die,Dwarf_Half attr,bool global)2309   static Dwarf_Die get_referenced_die(Dwarf_Debug dwarf, Dwarf_Die die,
2310                                       Dwarf_Half attr, bool global) {
2311     Dwarf_Error error = DW_DLE_NE;
2312     Dwarf_Attribute attr_mem;
2313 
2314     Dwarf_Die found_die = NULL;
2315     if (dwarf_attr(die, attr, &attr_mem, &error) == DW_DLV_OK) {
2316       Dwarf_Off offset;
2317       int result = 0;
2318       if (global) {
2319         result = dwarf_global_formref(attr_mem, &offset, &error);
2320       } else {
2321         result = dwarf_formref(attr_mem, &offset, &error);
2322       }
2323 
2324       if (result == DW_DLV_OK) {
2325         if (dwarf_offdie(dwarf, offset, &found_die, &error) != DW_DLV_OK) {
2326           found_die = NULL;
2327         }
2328       }
2329       dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2330     }
2331     return found_die;
2332   }
2333 
get_referenced_die_name(Dwarf_Debug dwarf,Dwarf_Die die,Dwarf_Half attr,bool global)2334   static std::string get_referenced_die_name(Dwarf_Debug dwarf, Dwarf_Die die,
2335                                              Dwarf_Half attr, bool global) {
2336     Dwarf_Error error = DW_DLE_NE;
2337     std::string value;
2338 
2339     Dwarf_Die found_die = get_referenced_die(dwarf, die, attr, global);
2340 
2341     if (found_die) {
2342       char *name;
2343       if (dwarf_diename(found_die, &name, &error) == DW_DLV_OK) {
2344         if (name) {
2345           value = std::string(name);
2346         }
2347         dwarf_dealloc(dwarf, name, DW_DLA_STRING);
2348       }
2349       dwarf_dealloc(dwarf, found_die, DW_DLA_DIE);
2350     }
2351 
2352     return value;
2353   }
2354 
2355   // Returns a spec DIE linked to the passed one. The caller should
2356   // deallocate the DIE
get_spec_die(dwarf_fileobject & fobj,Dwarf_Die die)2357   static Dwarf_Die get_spec_die(dwarf_fileobject &fobj, Dwarf_Die die) {
2358     Dwarf_Debug dwarf = fobj.dwarf_handle.get();
2359     Dwarf_Error error = DW_DLE_NE;
2360     Dwarf_Off die_offset;
2361     if (fobj.current_cu &&
2362         dwarf_die_CU_offset(die, &die_offset, &error) == DW_DLV_OK) {
2363       die_specmap_t::iterator it =
2364           fobj.current_cu->spec_section.find(die_offset);
2365 
2366       // If we have a DIE that completes the current one, check if
2367       // that one has the pc we are looking for
2368       if (it != fobj.current_cu->spec_section.end()) {
2369         Dwarf_Die spec_die = 0;
2370         if (dwarf_offdie(dwarf, it->second, &spec_die, &error) == DW_DLV_OK) {
2371           return spec_die;
2372         }
2373       }
2374     }
2375 
2376     // Maybe we have an abstract origin DIE with the function information?
2377     return get_referenced_die(fobj.dwarf_handle.get(), die,
2378                               DW_AT_abstract_origin, true);
2379   }
2380 
die_has_pc(dwarf_fileobject & fobj,Dwarf_Die die,Dwarf_Addr pc)2381   static bool die_has_pc(dwarf_fileobject &fobj, Dwarf_Die die, Dwarf_Addr pc) {
2382     Dwarf_Addr low_pc = 0, high_pc = 0;
2383     Dwarf_Half high_pc_form = 0;
2384     Dwarf_Form_Class return_class;
2385     Dwarf_Error error = DW_DLE_NE;
2386     Dwarf_Debug dwarf = fobj.dwarf_handle.get();
2387     bool has_lowpc = false;
2388     bool has_highpc = false;
2389     bool has_ranges = false;
2390 
2391     if (dwarf_lowpc(die, &low_pc, &error) == DW_DLV_OK) {
2392       // If we have a low_pc check if there is a high pc.
2393       // If we don't have a high pc this might mean we have a base
2394       // address for the ranges list or just an address.
2395       has_lowpc = true;
2396 
2397       if (dwarf_highpc_b(die, &high_pc, &high_pc_form, &return_class, &error) ==
2398           DW_DLV_OK) {
2399         // We do have a high pc. In DWARF 4+ this is an offset from the
2400         // low pc, but in earlier versions it's an absolute address.
2401 
2402         has_highpc = true;
2403         // In DWARF 2/3 this would be a DW_FORM_CLASS_ADDRESS
2404         if (return_class == DW_FORM_CLASS_CONSTANT) {
2405           high_pc = low_pc + high_pc;
2406         }
2407 
2408         // We have low and high pc, check if our address
2409         // is in that range
2410         return pc >= low_pc && pc < high_pc;
2411       }
2412     } else {
2413       // Reset the low_pc, in case dwarf_lowpc failing set it to some
2414       // undefined value.
2415       low_pc = 0;
2416     }
2417 
2418     // Check if DW_AT_ranges is present and search for the PC in the
2419     // returned ranges list. We always add the low_pc, as it not set it will
2420     // be 0, in case we had a DW_AT_low_pc and DW_AT_ranges pair
2421     bool result = false;
2422 
2423     Dwarf_Attribute attr;
2424     if (dwarf_attr(die, DW_AT_ranges, &attr, &error) == DW_DLV_OK) {
2425 
2426       Dwarf_Off offset;
2427       if (dwarf_global_formref(attr, &offset, &error) == DW_DLV_OK) {
2428         Dwarf_Ranges *ranges;
2429         Dwarf_Signed ranges_count = 0;
2430         Dwarf_Unsigned byte_count = 0;
2431 
2432         if (dwarf_get_ranges_a(dwarf, offset, die, &ranges, &ranges_count,
2433                                &byte_count, &error) == DW_DLV_OK) {
2434           has_ranges = ranges_count != 0;
2435           for (int i = 0; i < ranges_count; i++) {
2436             if (ranges[i].dwr_addr1 != 0 &&
2437                 pc >= ranges[i].dwr_addr1 + low_pc &&
2438                 pc < ranges[i].dwr_addr2 + low_pc) {
2439               result = true;
2440               break;
2441             }
2442           }
2443           dwarf_ranges_dealloc(dwarf, ranges, ranges_count);
2444         }
2445       }
2446     }
2447 
2448     // Last attempt. We might have a single address set as low_pc.
2449     if (!result && low_pc != 0 && pc == low_pc) {
2450       result = true;
2451     }
2452 
2453     // If we don't have lowpc, highpc and ranges maybe this DIE is a
2454     // declaration that relies on a DW_AT_specification DIE that happens
2455     // later. Use the specification cache we filled when we loaded this CU.
2456     if (!result && (!has_lowpc && !has_highpc && !has_ranges)) {
2457       Dwarf_Die spec_die = get_spec_die(fobj, die);
2458       if (spec_die) {
2459         result = die_has_pc(fobj, spec_die, pc);
2460         dwarf_dealloc(dwarf, spec_die, DW_DLA_DIE);
2461       }
2462     }
2463 
2464     return result;
2465   }
2466 
get_type(Dwarf_Debug dwarf,Dwarf_Die die,std::string & type)2467   static void get_type(Dwarf_Debug dwarf, Dwarf_Die die, std::string &type) {
2468     Dwarf_Error error = DW_DLE_NE;
2469 
2470     Dwarf_Die child = 0;
2471     if (dwarf_child(die, &child, &error) == DW_DLV_OK) {
2472       get_type(dwarf, child, type);
2473     }
2474 
2475     if (child) {
2476       type.insert(0, "::");
2477       dwarf_dealloc(dwarf, child, DW_DLA_DIE);
2478     }
2479 
2480     char *name;
2481     if (dwarf_diename(die, &name, &error) == DW_DLV_OK) {
2482       type.insert(0, std::string(name));
2483       dwarf_dealloc(dwarf, name, DW_DLA_STRING);
2484     } else {
2485       type.insert(0, "<unknown>");
2486     }
2487   }
2488 
get_type_by_signature(Dwarf_Debug dwarf,Dwarf_Die die)2489   static std::string get_type_by_signature(Dwarf_Debug dwarf, Dwarf_Die die) {
2490     Dwarf_Error error = DW_DLE_NE;
2491 
2492     Dwarf_Sig8 signature;
2493     Dwarf_Bool has_attr = 0;
2494     if (dwarf_hasattr(die, DW_AT_signature, &has_attr, &error) == DW_DLV_OK) {
2495       if (has_attr) {
2496         Dwarf_Attribute attr_mem;
2497         if (dwarf_attr(die, DW_AT_signature, &attr_mem, &error) == DW_DLV_OK) {
2498           if (dwarf_formsig8(attr_mem, &signature, &error) != DW_DLV_OK) {
2499             return std::string("<no type signature>");
2500           }
2501         }
2502         dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2503       }
2504     }
2505 
2506     Dwarf_Unsigned next_cu_header;
2507     Dwarf_Sig8 tu_signature;
2508     std::string result;
2509     bool found = false;
2510 
2511     while (dwarf_next_cu_header_d(dwarf, 0, 0, 0, 0, 0, 0, 0, &tu_signature, 0,
2512                                   &next_cu_header, 0, &error) == DW_DLV_OK) {
2513 
2514       if (strncmp(signature.signature, tu_signature.signature, 8) == 0) {
2515         Dwarf_Die type_cu_die = 0;
2516         if (dwarf_siblingof_b(dwarf, 0, 0, &type_cu_die, &error) == DW_DLV_OK) {
2517           Dwarf_Die child_die = 0;
2518           if (dwarf_child(type_cu_die, &child_die, &error) == DW_DLV_OK) {
2519             get_type(dwarf, child_die, result);
2520             found = !result.empty();
2521             dwarf_dealloc(dwarf, child_die, DW_DLA_DIE);
2522           }
2523           dwarf_dealloc(dwarf, type_cu_die, DW_DLA_DIE);
2524         }
2525       }
2526     }
2527 
2528     if (found) {
2529       while (dwarf_next_cu_header_d(dwarf, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2530                                     &next_cu_header, 0, &error) == DW_DLV_OK) {
2531         // Reset the cu header state. Unfortunately, libdwarf's
2532         // next_cu_header API keeps its own iterator per Dwarf_Debug
2533         // that can't be reset. We need to keep fetching elements until
2534         // the end.
2535       }
2536     } else {
2537       // If we couldn't resolve the type just print out the signature
2538       std::ostringstream string_stream;
2539       string_stream << "<0x" << std::hex << std::setfill('0');
2540       for (int i = 0; i < 8; ++i) {
2541         string_stream << std::setw(2) << std::hex
2542                       << (int)(unsigned char)(signature.signature[i]);
2543       }
2544       string_stream << ">";
2545       result = string_stream.str();
2546     }
2547     return result;
2548   }
2549 
2550   struct type_context_t {
2551     bool is_const;
2552     bool is_typedef;
2553     bool has_type;
2554     bool has_name;
2555     std::string text;
2556 
type_context_tbackward::TraceResolverLinuxImpl::type_context_t2557     type_context_t()
2558         : is_const(false), is_typedef(false), has_type(false), has_name(false) {
2559     }
2560   };
2561 
2562   // Types are resolved from right to left: we get the variable name first
2563   // and then all specifiers (like const or pointer) in a chain of DW_AT_type
2564   // DIEs. Call this function recursively until we get a complete type
2565   // string.
set_parameter_string(dwarf_fileobject & fobj,Dwarf_Die die,type_context_t & context)2566   static void set_parameter_string(dwarf_fileobject &fobj, Dwarf_Die die,
2567                                    type_context_t &context) {
2568     char *name;
2569     Dwarf_Error error = DW_DLE_NE;
2570 
2571     // typedefs contain also the base type, so we skip it and only
2572     // print the typedef name
2573     if (!context.is_typedef) {
2574       if (dwarf_diename(die, &name, &error) == DW_DLV_OK) {
2575         if (!context.text.empty()) {
2576           context.text.insert(0, " ");
2577         }
2578         context.text.insert(0, std::string(name));
2579         dwarf_dealloc(fobj.dwarf_handle.get(), name, DW_DLA_STRING);
2580       }
2581     } else {
2582       context.is_typedef = false;
2583       context.has_type = true;
2584       if (context.is_const) {
2585         context.text.insert(0, "const ");
2586         context.is_const = false;
2587       }
2588     }
2589 
2590     bool next_type_is_const = false;
2591     bool is_keyword = true;
2592 
2593     Dwarf_Half tag = 0;
2594     Dwarf_Bool has_attr = 0;
2595     if (dwarf_tag(die, &tag, &error) == DW_DLV_OK) {
2596       switch (tag) {
2597       case DW_TAG_structure_type:
2598       case DW_TAG_union_type:
2599       case DW_TAG_class_type:
2600       case DW_TAG_enumeration_type:
2601         context.has_type = true;
2602         if (dwarf_hasattr(die, DW_AT_signature, &has_attr, &error) ==
2603             DW_DLV_OK) {
2604           // If we have a signature it means the type is defined
2605           // in .debug_types, so we need to load the DIE pointed
2606           // at by the signature and resolve it
2607           if (has_attr) {
2608             std::string type =
2609                 get_type_by_signature(fobj.dwarf_handle.get(), die);
2610             if (context.is_const)
2611               type.insert(0, "const ");
2612 
2613             if (!context.text.empty())
2614               context.text.insert(0, " ");
2615             context.text.insert(0, type);
2616           }
2617 
2618           // Treat enums like typedefs, and skip printing its
2619           // base type
2620           context.is_typedef = (tag == DW_TAG_enumeration_type);
2621         }
2622         break;
2623       case DW_TAG_const_type:
2624         next_type_is_const = true;
2625         break;
2626       case DW_TAG_pointer_type:
2627         context.text.insert(0, "*");
2628         break;
2629       case DW_TAG_reference_type:
2630         context.text.insert(0, "&");
2631         break;
2632       case DW_TAG_restrict_type:
2633         context.text.insert(0, "restrict ");
2634         break;
2635       case DW_TAG_rvalue_reference_type:
2636         context.text.insert(0, "&&");
2637         break;
2638       case DW_TAG_volatile_type:
2639         context.text.insert(0, "volatile ");
2640         break;
2641       case DW_TAG_typedef:
2642         // Propagate the const-ness to the next type
2643         // as typedefs are linked to its base type
2644         next_type_is_const = context.is_const;
2645         context.is_typedef = true;
2646         context.has_type = true;
2647         break;
2648       case DW_TAG_base_type:
2649         context.has_type = true;
2650         break;
2651       case DW_TAG_formal_parameter:
2652         context.has_name = true;
2653         break;
2654       default:
2655         is_keyword = false;
2656         break;
2657       }
2658     }
2659 
2660     if (!is_keyword && context.is_const) {
2661       context.text.insert(0, "const ");
2662     }
2663 
2664     context.is_const = next_type_is_const;
2665 
2666     Dwarf_Die ref =
2667         get_referenced_die(fobj.dwarf_handle.get(), die, DW_AT_type, true);
2668     if (ref) {
2669       set_parameter_string(fobj, ref, context);
2670       dwarf_dealloc(fobj.dwarf_handle.get(), ref, DW_DLA_DIE);
2671     }
2672 
2673     if (!context.has_type && context.has_name) {
2674       context.text.insert(0, "void ");
2675       context.has_type = true;
2676     }
2677   }
2678 
2679   // Resolve the function return type and parameters
set_function_parameters(std::string & function_name,std::vector<std::string> & ns,dwarf_fileobject & fobj,Dwarf_Die die)2680   static void set_function_parameters(std::string &function_name,
2681                                       std::vector<std::string> &ns,
2682                                       dwarf_fileobject &fobj, Dwarf_Die die) {
2683     Dwarf_Debug dwarf = fobj.dwarf_handle.get();
2684     Dwarf_Error error = DW_DLE_NE;
2685     Dwarf_Die current_die = 0;
2686     std::string parameters;
2687     bool has_spec = true;
2688     // Check if we have a spec DIE. If we do we use it as it contains
2689     // more information, like parameter names.
2690     Dwarf_Die spec_die = get_spec_die(fobj, die);
2691     if (!spec_die) {
2692       has_spec = false;
2693       spec_die = die;
2694     }
2695 
2696     std::vector<std::string>::const_iterator it = ns.begin();
2697     std::string ns_name;
2698     for (it = ns.begin(); it < ns.end(); ++it) {
2699       ns_name.append(*it).append("::");
2700     }
2701 
2702     if (!ns_name.empty()) {
2703       function_name.insert(0, ns_name);
2704     }
2705 
2706     // See if we have a function return type. It can be either on the
2707     // current die or in its spec one (usually true for inlined functions)
2708     std::string return_type =
2709         get_referenced_die_name(dwarf, die, DW_AT_type, true);
2710     if (return_type.empty()) {
2711       return_type = get_referenced_die_name(dwarf, spec_die, DW_AT_type, true);
2712     }
2713     if (!return_type.empty()) {
2714       return_type.append(" ");
2715       function_name.insert(0, return_type);
2716     }
2717 
2718     if (dwarf_child(spec_die, &current_die, &error) == DW_DLV_OK) {
2719       for (;;) {
2720         Dwarf_Die sibling_die = 0;
2721 
2722         Dwarf_Half tag_value;
2723         dwarf_tag(current_die, &tag_value, &error);
2724 
2725         if (tag_value == DW_TAG_formal_parameter) {
2726           // Ignore artificial (ie, compiler generated) parameters
2727           bool is_artificial = false;
2728           Dwarf_Attribute attr_mem;
2729           if (dwarf_attr(current_die, DW_AT_artificial, &attr_mem, &error) ==
2730               DW_DLV_OK) {
2731             Dwarf_Bool flag = 0;
2732             if (dwarf_formflag(attr_mem, &flag, &error) == DW_DLV_OK) {
2733               is_artificial = flag != 0;
2734             }
2735             dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2736           }
2737 
2738           if (!is_artificial) {
2739             type_context_t context;
2740             set_parameter_string(fobj, current_die, context);
2741 
2742             if (parameters.empty()) {
2743               parameters.append("(");
2744             } else {
2745               parameters.append(", ");
2746             }
2747             parameters.append(context.text);
2748           }
2749         }
2750 
2751         int result = dwarf_siblingof(dwarf, current_die, &sibling_die, &error);
2752         if (result == DW_DLV_ERROR) {
2753           break;
2754         } else if (result == DW_DLV_NO_ENTRY) {
2755           break;
2756         }
2757 
2758         if (current_die != die) {
2759           dwarf_dealloc(dwarf, current_die, DW_DLA_DIE);
2760           current_die = 0;
2761         }
2762 
2763         current_die = sibling_die;
2764       }
2765     }
2766     if (parameters.empty())
2767       parameters = "(";
2768     parameters.append(")");
2769 
2770     // If we got a spec DIE we need to deallocate it
2771     if (has_spec)
2772       dwarf_dealloc(dwarf, spec_die, DW_DLA_DIE);
2773 
2774     function_name.append(parameters);
2775   }
2776 
2777   // defined here because in C++98, template function cannot take locally
2778   // defined types... grrr.
2779   struct inliners_search_cb {
operator ()backward::TraceResolverLinuxImpl::inliners_search_cb2780     void operator()(Dwarf_Die die, std::vector<std::string> &ns) {
2781       Dwarf_Error error = DW_DLE_NE;
2782       Dwarf_Half tag_value;
2783       Dwarf_Attribute attr_mem;
2784       Dwarf_Debug dwarf = fobj.dwarf_handle.get();
2785 
2786       dwarf_tag(die, &tag_value, &error);
2787 
2788       switch (tag_value) {
2789         char *name;
2790       case DW_TAG_subprogram:
2791         if (!trace.source.function.empty())
2792           break;
2793         if (dwarf_diename(die, &name, &error) == DW_DLV_OK) {
2794           trace.source.function = std::string(name);
2795           dwarf_dealloc(dwarf, name, DW_DLA_STRING);
2796         } else {
2797           // We don't have a function name in this DIE.
2798           // Check if there is a referenced non-defining
2799           // declaration.
2800           trace.source.function =
2801               get_referenced_die_name(dwarf, die, DW_AT_abstract_origin, true);
2802           if (trace.source.function.empty()) {
2803             trace.source.function =
2804                 get_referenced_die_name(dwarf, die, DW_AT_specification, true);
2805           }
2806         }
2807 
2808         // Append the function parameters, if available
2809         set_function_parameters(trace.source.function, ns, fobj, die);
2810 
2811         // If the object function name is empty, it's possible that
2812         // there is no dynamic symbol table (maybe the executable
2813         // was stripped or not built with -rdynamic). See if we have
2814         // a DWARF linkage name to use instead. We try both
2815         // linkage_name and MIPS_linkage_name because the MIPS tag
2816         // was the unofficial one until it was adopted in DWARF4.
2817         // Old gcc versions generate MIPS_linkage_name
2818         if (trace.object_function.empty()) {
2819           details::demangler demangler;
2820 
2821           if (dwarf_attr(die, DW_AT_linkage_name, &attr_mem, &error) !=
2822               DW_DLV_OK) {
2823             if (dwarf_attr(die, DW_AT_MIPS_linkage_name, &attr_mem, &error) !=
2824                 DW_DLV_OK) {
2825               break;
2826             }
2827           }
2828 
2829           char *linkage;
2830           if (dwarf_formstring(attr_mem, &linkage, &error) == DW_DLV_OK) {
2831             trace.object_function = demangler.demangle(linkage);
2832             dwarf_dealloc(dwarf, linkage, DW_DLA_STRING);
2833           }
2834           dwarf_dealloc(dwarf, name, DW_DLA_ATTR);
2835         }
2836         break;
2837 
2838       case DW_TAG_inlined_subroutine:
2839         ResolvedTrace::SourceLoc sloc;
2840 
2841         if (dwarf_diename(die, &name, &error) == DW_DLV_OK) {
2842           sloc.function = std::string(name);
2843           dwarf_dealloc(dwarf, name, DW_DLA_STRING);
2844         } else {
2845           // We don't have a name for this inlined DIE, it could
2846           // be that there is an abstract origin instead.
2847           // Get the DW_AT_abstract_origin value, which is a
2848           // reference to the source DIE and try to get its name
2849           sloc.function =
2850               get_referenced_die_name(dwarf, die, DW_AT_abstract_origin, true);
2851         }
2852 
2853         set_function_parameters(sloc.function, ns, fobj, die);
2854 
2855         std::string file = die_call_file(dwarf, die, cu_die);
2856         if (!file.empty())
2857           sloc.filename = file;
2858 
2859         Dwarf_Unsigned number = 0;
2860         if (dwarf_attr(die, DW_AT_call_line, &attr_mem, &error) == DW_DLV_OK) {
2861           if (dwarf_formudata(attr_mem, &number, &error) == DW_DLV_OK) {
2862             sloc.line = number;
2863           }
2864           dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2865         }
2866 
2867         if (dwarf_attr(die, DW_AT_call_column, &attr_mem, &error) ==
2868             DW_DLV_OK) {
2869           if (dwarf_formudata(attr_mem, &number, &error) == DW_DLV_OK) {
2870             sloc.col = number;
2871           }
2872           dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2873         }
2874 
2875         trace.inliners.push_back(sloc);
2876         break;
2877       };
2878     }
2879     ResolvedTrace &trace;
2880     dwarf_fileobject &fobj;
2881     Dwarf_Die cu_die;
inliners_search_cbbackward::TraceResolverLinuxImpl::inliners_search_cb2882     inliners_search_cb(ResolvedTrace &t, dwarf_fileobject &f, Dwarf_Die c)
2883         : trace(t), fobj(f), cu_die(c) {}
2884   };
2885 
find_fundie_by_pc(dwarf_fileobject & fobj,Dwarf_Die parent_die,Dwarf_Addr pc,Dwarf_Die result)2886   static Dwarf_Die find_fundie_by_pc(dwarf_fileobject &fobj,
2887                                      Dwarf_Die parent_die, Dwarf_Addr pc,
2888                                      Dwarf_Die result) {
2889     Dwarf_Die current_die = 0;
2890     Dwarf_Error error = DW_DLE_NE;
2891     Dwarf_Debug dwarf = fobj.dwarf_handle.get();
2892 
2893     if (dwarf_child(parent_die, &current_die, &error) != DW_DLV_OK) {
2894       return NULL;
2895     }
2896 
2897     for (;;) {
2898       Dwarf_Die sibling_die = 0;
2899       Dwarf_Half tag_value;
2900       dwarf_tag(current_die, &tag_value, &error);
2901 
2902       switch (tag_value) {
2903       case DW_TAG_subprogram:
2904       case DW_TAG_inlined_subroutine:
2905         if (die_has_pc(fobj, current_die, pc)) {
2906           return current_die;
2907         }
2908       };
2909       bool declaration = false;
2910       Dwarf_Attribute attr_mem;
2911       if (dwarf_attr(current_die, DW_AT_declaration, &attr_mem, &error) ==
2912           DW_DLV_OK) {
2913         Dwarf_Bool flag = 0;
2914         if (dwarf_formflag(attr_mem, &flag, &error) == DW_DLV_OK) {
2915           declaration = flag != 0;
2916         }
2917         dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2918       }
2919 
2920       if (!declaration) {
2921         // let's be curious and look deeper in the tree, functions are
2922         // not necessarily at the first level, but might be nested
2923         // inside a namespace, structure, a function, an inlined
2924         // function etc.
2925         Dwarf_Die die_mem = 0;
2926         Dwarf_Die indie = find_fundie_by_pc(fobj, current_die, pc, die_mem);
2927         if (indie) {
2928           result = die_mem;
2929           return result;
2930         }
2931       }
2932 
2933       int res = dwarf_siblingof(dwarf, current_die, &sibling_die, &error);
2934       if (res == DW_DLV_ERROR) {
2935         return NULL;
2936       } else if (res == DW_DLV_NO_ENTRY) {
2937         break;
2938       }
2939 
2940       if (current_die != parent_die) {
2941         dwarf_dealloc(dwarf, current_die, DW_DLA_DIE);
2942         current_die = 0;
2943       }
2944 
2945       current_die = sibling_die;
2946     }
2947     return NULL;
2948   }
2949 
2950   template <typename CB>
deep_first_search_by_pc(dwarf_fileobject & fobj,Dwarf_Die parent_die,Dwarf_Addr pc,std::vector<std::string> & ns,CB cb)2951   static bool deep_first_search_by_pc(dwarf_fileobject &fobj,
2952                                       Dwarf_Die parent_die, Dwarf_Addr pc,
2953                                       std::vector<std::string> &ns, CB cb) {
2954     Dwarf_Die current_die = 0;
2955     Dwarf_Debug dwarf = fobj.dwarf_handle.get();
2956     Dwarf_Error error = DW_DLE_NE;
2957 
2958     if (dwarf_child(parent_die, &current_die, &error) != DW_DLV_OK) {
2959       return false;
2960     }
2961 
2962     bool branch_has_pc = false;
2963     bool has_namespace = false;
2964     for (;;) {
2965       Dwarf_Die sibling_die = 0;
2966 
2967       Dwarf_Half tag;
2968       if (dwarf_tag(current_die, &tag, &error) == DW_DLV_OK) {
2969         if (tag == DW_TAG_namespace || tag == DW_TAG_class_type) {
2970           char *ns_name = NULL;
2971           if (dwarf_diename(current_die, &ns_name, &error) == DW_DLV_OK) {
2972             if (ns_name) {
2973               ns.push_back(std::string(ns_name));
2974             } else {
2975               ns.push_back("<unknown>");
2976             }
2977             dwarf_dealloc(dwarf, ns_name, DW_DLA_STRING);
2978           } else {
2979             ns.push_back("<unknown>");
2980           }
2981           has_namespace = true;
2982         }
2983       }
2984 
2985       bool declaration = false;
2986       Dwarf_Attribute attr_mem;
2987       if (tag != DW_TAG_class_type &&
2988           dwarf_attr(current_die, DW_AT_declaration, &attr_mem, &error) ==
2989               DW_DLV_OK) {
2990         Dwarf_Bool flag = 0;
2991         if (dwarf_formflag(attr_mem, &flag, &error) == DW_DLV_OK) {
2992           declaration = flag != 0;
2993         }
2994         dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
2995       }
2996 
2997       if (!declaration) {
2998         // let's be curious and look deeper in the tree, function are
2999         // not necessarily at the first level, but might be nested
3000         // inside a namespace, structure, a function, an inlined
3001         // function etc.
3002         branch_has_pc = deep_first_search_by_pc(fobj, current_die, pc, ns, cb);
3003       }
3004 
3005       if (!branch_has_pc) {
3006         branch_has_pc = die_has_pc(fobj, current_die, pc);
3007       }
3008 
3009       if (branch_has_pc) {
3010         cb(current_die, ns);
3011       }
3012 
3013       int result = dwarf_siblingof(dwarf, current_die, &sibling_die, &error);
3014       if (result == DW_DLV_ERROR) {
3015         return false;
3016       } else if (result == DW_DLV_NO_ENTRY) {
3017         break;
3018       }
3019 
3020       if (current_die != parent_die) {
3021         dwarf_dealloc(dwarf, current_die, DW_DLA_DIE);
3022         current_die = 0;
3023       }
3024 
3025       if (has_namespace) {
3026         has_namespace = false;
3027         ns.pop_back();
3028       }
3029       current_die = sibling_die;
3030     }
3031 
3032     if (has_namespace) {
3033       ns.pop_back();
3034     }
3035     return branch_has_pc;
3036   }
3037 
die_call_file(Dwarf_Debug dwarf,Dwarf_Die die,Dwarf_Die cu_die)3038   static std::string die_call_file(Dwarf_Debug dwarf, Dwarf_Die die,
3039                                    Dwarf_Die cu_die) {
3040     Dwarf_Attribute attr_mem;
3041     Dwarf_Error error = DW_DLE_NE;
3042     Dwarf_Signed file_index;
3043 
3044     std::string file;
3045 
3046     if (dwarf_attr(die, DW_AT_call_file, &attr_mem, &error) == DW_DLV_OK) {
3047       if (dwarf_formsdata(attr_mem, &file_index, &error) != DW_DLV_OK) {
3048         file_index = 0;
3049       }
3050       dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR);
3051 
3052       if (file_index == 0) {
3053         return file;
3054       }
3055 
3056       char **srcfiles = 0;
3057       Dwarf_Signed file_count = 0;
3058       if (dwarf_srcfiles(cu_die, &srcfiles, &file_count, &error) == DW_DLV_OK) {
3059         if (file_index <= file_count)
3060           file = std::string(srcfiles[file_index - 1]);
3061 
3062         // Deallocate all strings!
3063         for (int i = 0; i < file_count; ++i) {
3064           dwarf_dealloc(dwarf, srcfiles[i], DW_DLA_STRING);
3065         }
3066         dwarf_dealloc(dwarf, srcfiles, DW_DLA_LIST);
3067       }
3068     }
3069     return file;
3070   }
3071 
find_die(dwarf_fileobject & fobj,Dwarf_Addr addr)3072   Dwarf_Die find_die(dwarf_fileobject &fobj, Dwarf_Addr addr) {
3073     // Let's get to work! First see if we have a debug_aranges section so
3074     // we can speed up the search
3075 
3076     Dwarf_Debug dwarf = fobj.dwarf_handle.get();
3077     Dwarf_Error error = DW_DLE_NE;
3078     Dwarf_Arange *aranges;
3079     Dwarf_Signed arange_count;
3080 
3081     Dwarf_Die returnDie;
3082     bool found = false;
3083     if (dwarf_get_aranges(dwarf, &aranges, &arange_count, &error) !=
3084         DW_DLV_OK) {
3085       aranges = NULL;
3086     }
3087 
3088     if (aranges) {
3089       // We have aranges. Get the one where our address is.
3090       Dwarf_Arange arange;
3091       if (dwarf_get_arange(aranges, arange_count, addr, &arange, &error) ==
3092           DW_DLV_OK) {
3093 
3094         // We found our address. Get the compilation-unit DIE offset
3095         // represented by the given address range.
3096         Dwarf_Off cu_die_offset;
3097         if (dwarf_get_cu_die_offset(arange, &cu_die_offset, &error) ==
3098             DW_DLV_OK) {
3099           // Get the DIE at the offset returned by the aranges search.
3100           // We set is_info to 1 to specify that the offset is from
3101           // the .debug_info section (and not .debug_types)
3102           int dwarf_result =
3103               dwarf_offdie_b(dwarf, cu_die_offset, 1, &returnDie, &error);
3104 
3105           found = dwarf_result == DW_DLV_OK;
3106         }
3107         dwarf_dealloc(dwarf, arange, DW_DLA_ARANGE);
3108       }
3109     }
3110 
3111     if (found)
3112       return returnDie; // The caller is responsible for freeing the die
3113 
3114     // The search for aranges failed. Try to find our address by scanning
3115     // all compilation units.
3116     Dwarf_Unsigned next_cu_header;
3117     Dwarf_Half tag = 0;
3118     returnDie = 0;
3119 
3120     while (!found &&
3121            dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0,
3122                                   &next_cu_header, 0, &error) == DW_DLV_OK) {
3123 
3124       if (returnDie)
3125         dwarf_dealloc(dwarf, returnDie, DW_DLA_DIE);
3126 
3127       if (dwarf_siblingof(dwarf, 0, &returnDie, &error) == DW_DLV_OK) {
3128         if ((dwarf_tag(returnDie, &tag, &error) == DW_DLV_OK) &&
3129             tag == DW_TAG_compile_unit) {
3130           if (die_has_pc(fobj, returnDie, addr)) {
3131             found = true;
3132           }
3133         }
3134       }
3135     }
3136 
3137     if (found) {
3138       while (dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0,
3139                                     &next_cu_header, 0, &error) == DW_DLV_OK) {
3140         // Reset the cu header state. Libdwarf's next_cu_header API
3141         // keeps its own iterator per Dwarf_Debug that can't be reset.
3142         // We need to keep fetching elements until the end.
3143       }
3144     }
3145 
3146     if (found)
3147       return returnDie;
3148 
3149     // We couldn't find any compilation units with ranges or a high/low pc.
3150     // Try again by looking at all DIEs in all compilation units.
3151     Dwarf_Die cudie;
3152     while (dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0,
3153                                   &next_cu_header, 0, &error) == DW_DLV_OK) {
3154       if (dwarf_siblingof(dwarf, 0, &cudie, &error) == DW_DLV_OK) {
3155         Dwarf_Die die_mem = 0;
3156         Dwarf_Die resultDie = find_fundie_by_pc(fobj, cudie, addr, die_mem);
3157 
3158         if (resultDie) {
3159           found = true;
3160           break;
3161         }
3162       }
3163     }
3164 
3165     if (found) {
3166       while (dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0,
3167                                     &next_cu_header, 0, &error) == DW_DLV_OK) {
3168         // Reset the cu header state. Libdwarf's next_cu_header API
3169         // keeps its own iterator per Dwarf_Debug that can't be reset.
3170         // We need to keep fetching elements until the end.
3171       }
3172     }
3173 
3174     if (found)
3175       return cudie;
3176 
3177     // We failed.
3178     return NULL;
3179   }
3180 };
3181 #endif // BACKWARD_HAS_DWARF == 1
3182 
3183 template <>
3184 class TraceResolverImpl<system_tag::linux_tag>
3185     : public TraceResolverLinuxImpl<trace_resolver_tag::current> {};
3186 
3187 #endif // BACKWARD_SYSTEM_LINUX
3188 
3189 #ifdef BACKWARD_SYSTEM_DARWIN
3190 
3191 template <typename STACKTRACE_TAG> class TraceResolverDarwinImpl;
3192 
3193 template <>
3194 class TraceResolverDarwinImpl<trace_resolver_tag::backtrace_symbol>
3195     : public TraceResolverImplBase {
3196 public:
load_stacktrace(ST & st)3197   template <class ST> void load_stacktrace(ST &st) {
3198     using namespace details;
3199     if (st.size() == 0) {
3200       return;
3201     }
3202     _symbols.reset(backtrace_symbols(st.begin(), st.size()));
3203   }
3204 
resolve(ResolvedTrace trace)3205   ResolvedTrace resolve(ResolvedTrace trace) {
3206     // parse:
3207     // <n>  <file>  <addr>  <mangled-name> + <offset>
3208     char *filename = _symbols[trace.idx];
3209 
3210     // skip "<n>  "
3211     while (*filename && *filename != ' ')
3212       filename++;
3213     while (*filename == ' ')
3214       filename++;
3215 
3216     // find start of <mangled-name> from end (<file> may contain a space)
3217     char *p = filename + strlen(filename) - 1;
3218     // skip to start of " + <offset>"
3219     while (p > filename && *p != ' ')
3220       p--;
3221     while (p > filename && *p == ' ')
3222       p--;
3223     while (p > filename && *p != ' ')
3224       p--;
3225     while (p > filename && *p == ' ')
3226       p--;
3227     char *funcname_end = p + 1;
3228 
3229     // skip to start of "<manged-name>"
3230     while (p > filename && *p != ' ')
3231       p--;
3232     char *funcname = p + 1;
3233 
3234     // skip to start of "  <addr>  "
3235     while (p > filename && *p == ' ')
3236       p--;
3237     while (p > filename && *p != ' ')
3238       p--;
3239     while (p > filename && *p == ' ')
3240       p--;
3241 
3242     // skip "<file>", handling the case where it contains a
3243     char *filename_end = p + 1;
3244     if (p == filename) {
3245       // something went wrong, give up
3246       filename_end = filename + strlen(filename);
3247       funcname = filename_end;
3248     }
3249     trace.object_filename.assign(
3250         filename, filename_end); // ok even if filename_end is the ending \0
3251                                  // (then we assign entire string)
3252 
3253     if (*funcname) { // if it's not end of string
3254       *funcname_end = '\0';
3255 
3256       trace.object_function = this->demangle(funcname);
3257       trace.object_function += " ";
3258       trace.object_function += (funcname_end + 1);
3259       trace.source.function = trace.object_function; // we cannot do better.
3260     }
3261     return trace;
3262   }
3263 
3264 private:
3265   details::handle<char **> _symbols;
3266 };
3267 
3268 template <>
3269 class TraceResolverImpl<system_tag::darwin_tag>
3270     : public TraceResolverDarwinImpl<trace_resolver_tag::current> {};
3271 
3272 #endif // BACKWARD_SYSTEM_DARWIN
3273 
3274 #ifdef BACKWARD_SYSTEM_WINDOWS
3275 
3276 // Load all symbol info
3277 // Based on:
3278 // https://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app/28276227#28276227
3279 
3280 struct module_data {
3281   std::string image_name;
3282   std::string module_name;
3283   void *base_address;
3284   DWORD load_size;
3285 };
3286 
3287 class get_mod_info {
3288   HANDLE process;
3289   static const int buffer_length = 4096;
3290 
3291 public:
get_mod_info(HANDLE h)3292   get_mod_info(HANDLE h) : process(h) {}
3293 
operator ()(HMODULE module)3294   module_data operator()(HMODULE module) {
3295     module_data ret;
3296     char temp[buffer_length];
3297     MODULEINFO mi;
3298 
3299     GetModuleInformation(process, module, &mi, sizeof(mi));
3300     ret.base_address = mi.lpBaseOfDll;
3301     ret.load_size = mi.SizeOfImage;
3302 
3303     GetModuleFileNameEx(process, module, temp, sizeof(temp));
3304     ret.image_name = temp;
3305     GetModuleBaseName(process, module, temp, sizeof(temp));
3306     ret.module_name = temp;
3307     std::vector<char> img(ret.image_name.begin(), ret.image_name.end());
3308     std::vector<char> mod(ret.module_name.begin(), ret.module_name.end());
3309     SymLoadModule64(process, 0, &img[0], &mod[0], (DWORD64)ret.base_address,
3310                     ret.load_size);
3311     return ret;
3312   }
3313 };
3314 
3315 template <> class TraceResolverImpl<system_tag::windows_tag> {
3316 public:
TraceResolverImpl()3317   TraceResolverImpl() {
3318 
3319     HANDLE process = GetCurrentProcess();
3320 
3321     std::vector<module_data> modules;
3322     DWORD cbNeeded;
3323     std::vector<HMODULE> module_handles(1);
3324     SymInitialize(process, NULL, false);
3325     DWORD symOptions = SymGetOptions();
3326     symOptions |= SYMOPT_LOAD_LINES | SYMOPT_UNDNAME;
3327     SymSetOptions(symOptions);
3328     EnumProcessModules(process, &module_handles[0],
3329                        module_handles.size() * sizeof(HMODULE), &cbNeeded);
3330     module_handles.resize(cbNeeded / sizeof(HMODULE));
3331     EnumProcessModules(process, &module_handles[0],
3332                        module_handles.size() * sizeof(HMODULE), &cbNeeded);
3333     std::transform(module_handles.begin(), module_handles.end(),
3334                    std::back_inserter(modules), get_mod_info(process));
3335     void *base = modules[0].base_address;
3336     IMAGE_NT_HEADERS *h = ImageNtHeader(base);
3337     image_type = h->FileHeader.Machine;
3338   }
3339 
load_stacktrace(ST &)3340   template <class ST> void load_stacktrace(ST &) {}
3341 
3342   static const int max_sym_len = 255;
3343   struct symbol_t {
3344     SYMBOL_INFO sym;
3345     char buffer[max_sym_len];
3346   } sym;
3347 
3348   DWORD64 displacement;
3349 
resolve(ResolvedTrace t)3350   ResolvedTrace resolve(ResolvedTrace t) {
3351     HANDLE process = GetCurrentProcess();
3352 
3353     char name[256];
3354 
3355     memset(&sym, sizeof(sym), 0);
3356     sym.sym.SizeOfStruct = sizeof(SYMBOL_INFO);
3357     sym.sym.MaxNameLen = max_sym_len;
3358 
3359     if (!SymFromAddr(process, (ULONG64)t.addr, &displacement, &sym.sym)) {
3360       // TODO:  error handling everywhere
3361       LPTSTR lpMsgBuf;
3362       DWORD dw = GetLastError();
3363 
3364       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
3365                         FORMAT_MESSAGE_FROM_SYSTEM |
3366                         FORMAT_MESSAGE_IGNORE_INSERTS,
3367                     NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
3368                     (LPTSTR)&lpMsgBuf, 0, NULL);
3369 
3370       printf(lpMsgBuf);
3371 
3372       // abort();
3373     }
3374     UnDecorateSymbolName(sym.sym.Name, (PSTR)name, 256, UNDNAME_COMPLETE);
3375 
3376     DWORD offset = 0;
3377     IMAGEHLP_LINE line;
3378     if (SymGetLineFromAddr(process, (ULONG64)t.addr, &offset, &line)) {
3379       t.object_filename = line.FileName;
3380       t.source.filename = line.FileName;
3381       t.source.line = line.LineNumber;
3382       t.source.col = offset;
3383     }
3384 
3385     t.source.function = name;
3386     t.object_filename = "";
3387     t.object_function = name;
3388 
3389     return t;
3390   }
3391 
machine_type() const3392   DWORD machine_type() const { return image_type; }
3393 
3394 private:
3395   DWORD image_type;
3396 };
3397 
3398 #endif
3399 
3400 class TraceResolver : public TraceResolverImpl<system_tag::current_tag> {};
3401 
3402 /*************** CODE SNIPPET ***************/
3403 
3404 class SourceFile {
3405 public:
3406   typedef std::vector<std::pair<unsigned, std::string>> lines_t;
3407 
SourceFile()3408   SourceFile() {}
SourceFile(const std::string & path)3409   SourceFile(const std::string &path) {
3410     // 1. If BACKWARD_CXX_SOURCE_PREFIXES is set then assume it contains
3411     //    a colon-separated list of path prefixes.  Try prepending each
3412     //    to the given path until a valid file is found.
3413     const std::vector<std::string>& prefixes = get_paths_from_env_variable();
3414     for (size_t i = 0; i < prefixes.size(); ++i) {
3415       // Double slashes (//) should not be a problem.
3416       std::string new_path = prefixes[i] + '/' + path;
3417       _file.reset(new std::ifstream(new_path.c_str()));
3418       if (is_open()) break;
3419     }
3420     // 2. If no valid file found then fallback to opening the path as-is.
3421     if (!_file || !is_open()) {
3422       _file.reset(new std::ifstream(path.c_str()));
3423     }
3424   }
is_open() const3425   bool is_open() const { return _file->is_open(); }
3426 
get_lines(unsigned line_start,unsigned line_count,lines_t & lines)3427   lines_t &get_lines(unsigned line_start, unsigned line_count, lines_t &lines) {
3428     using namespace std;
3429     // This function make uses of the dumbest algo ever:
3430     //	1) seek(0)
3431     //	2) read lines one by one and discard until line_start
3432     //	3) read line one by one until line_start + line_count
3433     //
3434     // If you are getting snippets many time from the same file, it is
3435     // somewhat a waste of CPU, feel free to benchmark and propose a
3436     // better solution ;)
3437 
3438     _file->clear();
3439     _file->seekg(0);
3440     string line;
3441     unsigned line_idx;
3442 
3443     for (line_idx = 1; line_idx < line_start; ++line_idx) {
3444       std::getline(*_file, line);
3445       if (!*_file) {
3446         return lines;
3447       }
3448     }
3449 
3450     // think of it like a lambda in C++98 ;)
3451     // but look, I will reuse it two times!
3452     // What a good boy am I.
3453     struct isspace {
3454       bool operator()(char c) { return std::isspace(c); }
3455     };
3456 
3457     bool started = false;
3458     for (; line_idx < line_start + line_count; ++line_idx) {
3459       getline(*_file, line);
3460       if (!*_file) {
3461         return lines;
3462       }
3463       if (!started) {
3464         if (std::find_if(line.begin(), line.end(), not_isspace()) == line.end())
3465           continue;
3466         started = true;
3467       }
3468       lines.push_back(make_pair(line_idx, line));
3469     }
3470 
3471     lines.erase(
3472         std::find_if(lines.rbegin(), lines.rend(), not_isempty()).base(),
3473         lines.end());
3474     return lines;
3475   }
3476 
get_lines(unsigned line_start,unsigned line_count)3477   lines_t get_lines(unsigned line_start, unsigned line_count) {
3478     lines_t lines;
3479     return get_lines(line_start, line_count, lines);
3480   }
3481 
3482   // there is no find_if_not in C++98, lets do something crappy to
3483   // workaround.
3484   struct not_isspace {
operator ()backward::SourceFile::not_isspace3485     bool operator()(char c) { return !std::isspace(c); }
3486   };
3487   // and define this one here because C++98 is not happy with local defined
3488   // struct passed to template functions, fuuuu.
3489   struct not_isempty {
operator ()backward::SourceFile::not_isempty3490     bool operator()(const lines_t::value_type &p) {
3491       return !(std::find_if(p.second.begin(), p.second.end(), not_isspace()) ==
3492                p.second.end());
3493     }
3494   };
3495 
swap(SourceFile & b)3496   void swap(SourceFile &b) { _file.swap(b._file); }
3497 
3498 #ifdef BACKWARD_ATLEAST_CXX11
SourceFile(SourceFile && from)3499   SourceFile(SourceFile &&from) : _file(nullptr) { swap(from); }
operator =(SourceFile && from)3500   SourceFile &operator=(SourceFile &&from) {
3501     swap(from);
3502     return *this;
3503   }
3504 #else
SourceFile(const SourceFile & from)3505   explicit SourceFile(const SourceFile &from) {
3506     // some sort of poor man's move semantic.
3507     swap(const_cast<SourceFile &>(from));
3508   }
operator =(const SourceFile & from)3509   SourceFile &operator=(const SourceFile &from) {
3510     // some sort of poor man's move semantic.
3511     swap(const_cast<SourceFile &>(from));
3512     return *this;
3513   }
3514 #endif
3515 
3516 private:
3517   details::handle<std::ifstream *, details::default_delete<std::ifstream *>>
3518       _file;
3519 
get_paths_from_env_variable_impl()3520   std::vector<std::string> get_paths_from_env_variable_impl() {
3521     std::vector<std::string> paths;
3522     const char* prefixes_str = std::getenv("BACKWARD_CXX_SOURCE_PREFIXES");
3523     if (prefixes_str && prefixes_str[0]) {
3524       paths = details::split_source_prefixes(prefixes_str);
3525     }
3526     return paths;
3527   }
3528 
get_paths_from_env_variable()3529   const std::vector<std::string>& get_paths_from_env_variable() {
3530     static std::vector<std::string> paths = get_paths_from_env_variable_impl();
3531     return paths;
3532   }
3533 
3534 #ifdef BACKWARD_ATLEAST_CXX11
3535   SourceFile(const SourceFile &) = delete;
3536   SourceFile &operator=(const SourceFile &) = delete;
3537 #endif
3538 };
3539 
3540 class SnippetFactory {
3541 public:
3542   typedef SourceFile::lines_t lines_t;
3543 
get_snippet(const std::string & filename,unsigned line_start,unsigned context_size)3544   lines_t get_snippet(const std::string &filename, unsigned line_start,
3545                       unsigned context_size) {
3546 
3547     SourceFile &src_file = get_src_file(filename);
3548     unsigned start = line_start - context_size / 2;
3549     return src_file.get_lines(start, context_size);
3550   }
3551 
get_combined_snippet(const std::string & filename_a,unsigned line_a,const std::string & filename_b,unsigned line_b,unsigned context_size)3552   lines_t get_combined_snippet(const std::string &filename_a, unsigned line_a,
3553                                const std::string &filename_b, unsigned line_b,
3554                                unsigned context_size) {
3555     SourceFile &src_file_a = get_src_file(filename_a);
3556     SourceFile &src_file_b = get_src_file(filename_b);
3557 
3558     lines_t lines =
3559         src_file_a.get_lines(line_a - context_size / 4, context_size / 2);
3560     src_file_b.get_lines(line_b - context_size / 4, context_size / 2, lines);
3561     return lines;
3562   }
3563 
get_coalesced_snippet(const std::string & filename,unsigned line_a,unsigned line_b,unsigned context_size)3564   lines_t get_coalesced_snippet(const std::string &filename, unsigned line_a,
3565                                 unsigned line_b, unsigned context_size) {
3566     SourceFile &src_file = get_src_file(filename);
3567 
3568     using std::max;
3569     using std::min;
3570     unsigned a = min(line_a, line_b);
3571     unsigned b = max(line_a, line_b);
3572 
3573     if ((b - a) < (context_size / 3)) {
3574       return src_file.get_lines((a + b - context_size + 1) / 2, context_size);
3575     }
3576 
3577     lines_t lines = src_file.get_lines(a - context_size / 4, context_size / 2);
3578     src_file.get_lines(b - context_size / 4, context_size / 2, lines);
3579     return lines;
3580   }
3581 
3582 private:
3583   typedef details::hashtable<std::string, SourceFile>::type src_files_t;
3584   src_files_t _src_files;
3585 
get_src_file(const std::string & filename)3586   SourceFile &get_src_file(const std::string &filename) {
3587     src_files_t::iterator it = _src_files.find(filename);
3588     if (it != _src_files.end()) {
3589       return it->second;
3590     }
3591     SourceFile &new_src_file = _src_files[filename];
3592     new_src_file = SourceFile(filename);
3593     return new_src_file;
3594   }
3595 };
3596 
3597 /*************** PRINTER ***************/
3598 
3599 namespace ColorMode {
3600 enum type { automatic, never, always };
3601 }
3602 
3603 class cfile_streambuf : public std::streambuf {
3604 public:
cfile_streambuf(FILE * _sink)3605   cfile_streambuf(FILE *_sink) : sink(_sink) {}
underflow()3606   int_type underflow() override { return traits_type::eof(); }
overflow(int_type ch)3607   int_type overflow(int_type ch) override {
3608     if (traits_type::not_eof(ch) && fwrite(&ch, sizeof ch, 1, sink) == 1) {
3609       return ch;
3610     }
3611     return traits_type::eof();
3612   }
3613 
xsputn(const char_type * s,std::streamsize count)3614   std::streamsize xsputn(const char_type *s, std::streamsize count) override {
3615     return static_cast<std::streamsize>(
3616         fwrite(s, sizeof *s, static_cast<size_t>(count), sink));
3617   }
3618 
3619 #ifdef BACKWARD_ATLEAST_CXX11
3620 public:
3621   cfile_streambuf(const cfile_streambuf &) = delete;
3622   cfile_streambuf &operator=(const cfile_streambuf &) = delete;
3623 #else
3624 private:
3625   cfile_streambuf(const cfile_streambuf &);
3626   cfile_streambuf &operator=(const cfile_streambuf &);
3627 #endif
3628 
3629 private:
3630   FILE *sink;
3631   std::vector<char> buffer;
3632 };
3633 
3634 #ifdef BACKWARD_SYSTEM_LINUX
3635 
3636 namespace Color {
3637 enum type { yellow = 33, purple = 35, reset = 39 };
3638 } // namespace Color
3639 
3640 class Colorize {
3641 public:
Colorize(std::ostream & os)3642   Colorize(std::ostream &os) : _os(os), _reset(false), _enabled(false) {}
3643 
activate(ColorMode::type mode)3644   void activate(ColorMode::type mode) { _enabled = mode == ColorMode::always; }
3645 
activate(ColorMode::type mode,FILE * fp)3646   void activate(ColorMode::type mode, FILE *fp) { activate(mode, fileno(fp)); }
3647 
set_color(Color::type ccode)3648   void set_color(Color::type ccode) {
3649     if (!_enabled)
3650       return;
3651 
3652     // I assume that the terminal can handle basic colors. Seriously I
3653     // don't want to deal with all the termcap shit.
3654     _os << "\033[" << static_cast<int>(ccode) << "m";
3655     _reset = (ccode != Color::reset);
3656   }
3657 
~Colorize()3658   ~Colorize() {
3659     if (_reset) {
3660       set_color(Color::reset);
3661     }
3662   }
3663 
3664 private:
activate(ColorMode::type mode,int fd)3665   void activate(ColorMode::type mode, int fd) {
3666     activate(mode == ColorMode::automatic && isatty(fd) ? ColorMode::always
3667                                                         : mode);
3668   }
3669 
3670   std::ostream &_os;
3671   bool _reset;
3672   bool _enabled;
3673 };
3674 
3675 #else // ndef BACKWARD_SYSTEM_LINUX
3676 
3677 namespace Color {
3678 enum type { yellow = 0, purple = 0, reset = 0 };
3679 } // namespace Color
3680 
3681 class Colorize {
3682 public:
Colorize(std::ostream &)3683   Colorize(std::ostream &) {}
activate(ColorMode::type)3684   void activate(ColorMode::type) {}
activate(ColorMode::type,FILE *)3685   void activate(ColorMode::type, FILE *) {}
set_color(Color::type)3686   void set_color(Color::type) {}
3687 };
3688 
3689 #endif // BACKWARD_SYSTEM_LINUX
3690 
3691 class Printer {
3692 public:
3693   bool snippet;
3694   ColorMode::type color_mode;
3695   bool address;
3696   bool object;
3697   int inliner_context_size;
3698   int trace_context_size;
3699 
Printer()3700   Printer()
3701       : snippet(true), color_mode(ColorMode::automatic), address(false),
3702         object(false), inliner_context_size(5), trace_context_size(7) {}
3703 
print(ST & st,FILE * fp=stderr)3704   template <typename ST> FILE *print(ST &st, FILE *fp = stderr) {
3705     cfile_streambuf obuf(fp);
3706     std::ostream os(&obuf);
3707     Colorize colorize(os);
3708     colorize.activate(color_mode, fp);
3709     print_stacktrace(st, os, colorize);
3710     return fp;
3711   }
3712 
print(ST & st,std::ostream & os)3713   template <typename ST> std::ostream &print(ST &st, std::ostream &os) {
3714     Colorize colorize(os);
3715     colorize.activate(color_mode);
3716     print_stacktrace(st, os, colorize);
3717     return os;
3718   }
3719 
3720   template <typename IT>
print(IT begin,IT end,FILE * fp=stderr,size_t thread_id=0)3721   FILE *print(IT begin, IT end, FILE *fp = stderr, size_t thread_id = 0) {
3722     cfile_streambuf obuf(fp);
3723     std::ostream os(&obuf);
3724     Colorize colorize(os);
3725     colorize.activate(color_mode, fp);
3726     print_stacktrace(begin, end, os, thread_id, colorize);
3727     return fp;
3728   }
3729 
3730   template <typename IT>
print(IT begin,IT end,std::ostream & os,size_t thread_id=0)3731   std::ostream &print(IT begin, IT end, std::ostream &os,
3732                       size_t thread_id = 0) {
3733     Colorize colorize(os);
3734     colorize.activate(color_mode);
3735     print_stacktrace(begin, end, os, thread_id, colorize);
3736     return os;
3737   }
3738 
resolver() const3739   TraceResolver const &resolver() const { return _resolver; }
3740 
3741 private:
3742   TraceResolver _resolver;
3743   SnippetFactory _snippets;
3744 
3745   template <typename ST>
print_stacktrace(ST & st,std::ostream & os,Colorize & colorize)3746   void print_stacktrace(ST &st, std::ostream &os, Colorize &colorize) {
3747     print_header(os, st.thread_id());
3748     _resolver.load_stacktrace(st);
3749     for (size_t trace_idx = st.size(); trace_idx > 0; --trace_idx) {
3750       print_trace(os, _resolver.resolve(st[trace_idx - 1]), colorize);
3751     }
3752   }
3753 
3754   template <typename IT>
print_stacktrace(IT begin,IT end,std::ostream & os,size_t thread_id,Colorize & colorize)3755   void print_stacktrace(IT begin, IT end, std::ostream &os, size_t thread_id,
3756                         Colorize &colorize) {
3757     print_header(os, thread_id);
3758     for (; begin != end; ++begin) {
3759       print_trace(os, *begin, colorize);
3760     }
3761   }
3762 
print_header(std::ostream & os,size_t thread_id)3763   void print_header(std::ostream &os, size_t thread_id) {
3764     os << "Stack trace (most recent call last)";
3765     if (thread_id) {
3766       os << " in thread " << thread_id;
3767     }
3768     os << ":\n";
3769   }
3770 
print_trace(std::ostream & os,const ResolvedTrace & trace,Colorize & colorize)3771   void print_trace(std::ostream &os, const ResolvedTrace &trace,
3772                    Colorize &colorize) {
3773     os << "#" << std::left << std::setw(2) << trace.idx << std::right;
3774     bool already_indented = true;
3775 
3776     if (!trace.source.filename.size() || object) {
3777       os << "   Object \"" << trace.object_filename << "\", at " << trace.addr
3778          << ", in " << trace.object_function << "\n";
3779       already_indented = false;
3780     }
3781 
3782     for (size_t inliner_idx = trace.inliners.size(); inliner_idx > 0;
3783          --inliner_idx) {
3784       if (!already_indented) {
3785         os << "   ";
3786       }
3787       const ResolvedTrace::SourceLoc &inliner_loc =
3788           trace.inliners[inliner_idx - 1];
3789       print_source_loc(os, " | ", inliner_loc);
3790       if (snippet) {
3791         print_snippet(os, "    | ", inliner_loc, colorize, Color::purple,
3792                       inliner_context_size);
3793       }
3794       already_indented = false;
3795     }
3796 
3797     if (trace.source.filename.size()) {
3798       if (!already_indented) {
3799         os << "   ";
3800       }
3801       print_source_loc(os, "   ", trace.source, trace.addr);
3802       if (snippet) {
3803         print_snippet(os, "      ", trace.source, colorize, Color::yellow,
3804                       trace_context_size);
3805       }
3806     }
3807   }
3808 
print_snippet(std::ostream & os,const char * indent,const ResolvedTrace::SourceLoc & source_loc,Colorize & colorize,Color::type color_code,int context_size)3809   void print_snippet(std::ostream &os, const char *indent,
3810                      const ResolvedTrace::SourceLoc &source_loc,
3811                      Colorize &colorize, Color::type color_code,
3812                      int context_size) {
3813     using namespace std;
3814     typedef SnippetFactory::lines_t lines_t;
3815 
3816     lines_t lines = _snippets.get_snippet(source_loc.filename, source_loc.line,
3817                                           static_cast<unsigned>(context_size));
3818 
3819     for (lines_t::const_iterator it = lines.begin(); it != lines.end(); ++it) {
3820       if (it->first == source_loc.line) {
3821         colorize.set_color(color_code);
3822         os << indent << ">";
3823       } else {
3824         os << indent << " ";
3825       }
3826       os << std::setw(4) << it->first << ": " << it->second << "\n";
3827       if (it->first == source_loc.line) {
3828         colorize.set_color(Color::reset);
3829       }
3830     }
3831   }
3832 
print_source_loc(std::ostream & os,const char * indent,const ResolvedTrace::SourceLoc & source_loc,void * addr=nullptr)3833   void print_source_loc(std::ostream &os, const char *indent,
3834                         const ResolvedTrace::SourceLoc &source_loc,
3835                         void *addr = nullptr) {
3836     os << indent << "Source \"" << source_loc.filename << "\", line "
3837        << source_loc.line << ", in " << source_loc.function;
3838 
3839     if (address && addr != nullptr) {
3840       os << " [" << addr << "]";
3841     }
3842     os << "\n";
3843   }
3844 };
3845 
3846 /*************** SIGNALS HANDLING ***************/
3847 
3848 #if defined(BACKWARD_SYSTEM_LINUX) || defined(BACKWARD_SYSTEM_DARWIN)
3849 
3850 class SignalHandling {
3851 public:
make_default_signals()3852   static std::vector<int> make_default_signals() {
3853     const int posix_signals[] = {
3854       // Signals for which the default action is "Core".
3855       SIGABRT, // Abort signal from abort(3)
3856       SIGBUS,  // Bus error (bad memory access)
3857       SIGFPE,  // Floating point exception
3858       SIGILL,  // Illegal Instruction
3859       SIGIOT,  // IOT trap. A synonym for SIGABRT
3860       SIGQUIT, // Quit from keyboard
3861       SIGSEGV, // Invalid memory reference
3862       SIGSYS,  // Bad argument to routine (SVr4)
3863       SIGTRAP, // Trace/breakpoint trap
3864       SIGXCPU, // CPU time limit exceeded (4.2BSD)
3865       SIGXFSZ, // File size limit exceeded (4.2BSD)
3866 #if defined(BACKWARD_SYSTEM_DARWIN)
3867       SIGEMT, // emulation instruction executed
3868 #endif
3869     };
3870     return std::vector<int>(posix_signals,
3871                             posix_signals +
3872                                 sizeof posix_signals / sizeof posix_signals[0]);
3873   }
3874 
SignalHandling(const std::vector<int> & posix_signals=make_default_signals ())3875   SignalHandling(const std::vector<int> &posix_signals = make_default_signals())
3876       : _loaded(false) {
3877     bool success = true;
3878 
3879     const size_t stack_size = 1024 * 1024 * 8;
3880     _stack_content.reset(static_cast<char *>(malloc(stack_size)));
3881     if (_stack_content) {
3882       stack_t ss;
3883       ss.ss_sp = _stack_content.get();
3884       ss.ss_size = stack_size;
3885       ss.ss_flags = 0;
3886       if (sigaltstack(&ss, nullptr) < 0) {
3887         success = false;
3888       }
3889     } else {
3890       success = false;
3891     }
3892 
3893     for (size_t i = 0; i < posix_signals.size(); ++i) {
3894       struct sigaction action;
3895       memset(&action, 0, sizeof action);
3896       action.sa_flags =
3897           static_cast<int>(SA_SIGINFO | SA_ONSTACK | SA_NODEFER | SA_RESETHAND);
3898       sigfillset(&action.sa_mask);
3899       sigdelset(&action.sa_mask, posix_signals[i]);
3900 #if defined(__clang__)
3901 #pragma clang diagnostic push
3902 #pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
3903 #endif
3904       action.sa_sigaction = &sig_handler;
3905 #if defined(__clang__)
3906 #pragma clang diagnostic pop
3907 #endif
3908 
3909       int r = sigaction(posix_signals[i], &action, nullptr);
3910       if (r < 0)
3911         success = false;
3912     }
3913 
3914     _loaded = success;
3915   }
3916 
loaded() const3917   bool loaded() const { return _loaded; }
3918 
handleSignal(int,siginfo_t * info,void * _ctx)3919   static void handleSignal(int, siginfo_t *info, void *_ctx) {
3920     ucontext_t *uctx = static_cast<ucontext_t *>(_ctx);
3921 
3922     StackTrace st;
3923     void *error_addr = nullptr;
3924 #ifdef REG_RIP // x86_64
3925     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext.gregs[REG_RIP]);
3926 #elif defined(REG_EIP) // x86_32
3927     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext.gregs[REG_EIP]);
3928 #elif defined(__arm__)
3929     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext.arm_pc);
3930 #elif defined(__aarch64__)
3931     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext.pc);
3932 #elif defined(__mips__)
3933     error_addr = reinterpret_cast<void *>(reinterpret_cast<struct sigcontext*>(&uctx->uc_mcontext)->sc_pc);
3934 #elif defined(__ppc__) || defined(__powerpc) || defined(__powerpc__) ||        \
3935     defined(__POWERPC__)
3936     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext.regs->nip);
3937 #elif defined(__s390x__)
3938     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext.psw.addr);
3939 #elif defined(__APPLE__) && defined(__x86_64__)
3940     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext->__ss.__rip);
3941 #elif defined(__APPLE__)
3942     error_addr = reinterpret_cast<void *>(uctx->uc_mcontext->__ss.__eip);
3943 #else
3944 #warning ":/ sorry, ain't know no nothing none not of your architecture!"
3945 #endif
3946     if (error_addr) {
3947       st.load_from(error_addr, 32);
3948     } else {
3949       st.load_here(32);
3950     }
3951 
3952     Printer printer;
3953     printer.address = true;
3954     printer.print(st, stderr);
3955 
3956 #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
3957     psiginfo(info, nullptr);
3958 #else
3959     (void)info;
3960 #endif
3961   }
3962 
3963 private:
3964   details::handle<char *> _stack_content;
3965   bool _loaded;
3966 
3967 #ifdef __GNUC__
3968   __attribute__((noreturn))
3969 #endif
3970   static void
sig_handler(int signo,siginfo_t * info,void * _ctx)3971   sig_handler(int signo, siginfo_t *info, void *_ctx) {
3972     handleSignal(signo, info, _ctx);
3973 
3974     // try to forward the signal.
3975     raise(info->si_signo);
3976 
3977     // terminate the process immediately.
3978     puts("watf? exit");
3979     _exit(EXIT_FAILURE);
3980   }
3981 };
3982 
3983 #endif // BACKWARD_SYSTEM_LINUX || BACKWARD_SYSTEM_DARWIN
3984 
3985 #ifdef BACKWARD_SYSTEM_WINDOWS
3986 
3987 class SignalHandling {
3988 public:
SignalHandling(const std::vector<int> &=std::vector<int> ())3989   SignalHandling(const std::vector<int> & = std::vector<int>())
3990       : reporter_thread_([]() {
3991           /* We handle crashes in a utility thread:
3992             backward structures and some Windows functions called here
3993             need stack space, which we do not have when we encounter a
3994             stack overflow.
3995             To support reporting stack traces during a stack overflow,
3996             we create a utility thread at startup, which waits until a
3997             crash happens or the program exits normally. */
3998 
3999           {
4000             std::unique_lock<std::mutex> lk(mtx());
4001             cv().wait(lk, [] { return crashed() != crash_status::running; });
4002           }
4003           if (crashed() == crash_status::crashed) {
4004             handle_stacktrace(skip_recs());
4005           }
4006           {
4007             std::unique_lock<std::mutex> lk(mtx());
4008             crashed() = crash_status::ending;
4009           }
4010           cv().notify_one();
4011         }) {
4012     SetUnhandledExceptionFilter(crash_handler);
4013 
4014     signal(SIGABRT, signal_handler);
4015     _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
4016 
4017     set_terminate(&terminator);
4018     set_unexpected(&terminator);
4019     _set_purecall_handler(&terminator);
4020     _set_invalid_parameter_handler(&invalid_parameter_handler);
4021   }
loaded() const4022   bool loaded() const { return true; }
4023 
~SignalHandling()4024   ~SignalHandling() {
4025     {
4026       std::unique_lock<std::mutex> lk(mtx());
4027       crashed() = crash_status::normal_exit;
4028     }
4029 
4030     cv().notify_one();
4031 
4032     reporter_thread_.join();
4033   }
4034 
4035 private:
ctx()4036   static CONTEXT *ctx() {
4037     static CONTEXT data;
4038     return &data;
4039   }
4040 
4041   enum class crash_status { running, crashed, normal_exit, ending };
4042 
crashed()4043   static crash_status &crashed() {
4044     static crash_status data;
4045     return data;
4046   }
4047 
mtx()4048   static std::mutex &mtx() {
4049     static std::mutex data;
4050     return data;
4051   }
4052 
cv()4053   static std::condition_variable &cv() {
4054     static std::condition_variable data;
4055     return data;
4056   }
4057 
thread_handle()4058   static HANDLE &thread_handle() {
4059     static HANDLE handle;
4060     return handle;
4061   }
4062 
4063   std::thread reporter_thread_;
4064 
4065   // TODO: how not to hardcode these?
4066   static const constexpr int signal_skip_recs =
4067 #ifdef __clang__
4068       // With clang, RtlCaptureContext also captures the stack frame of the
4069       // current function Below that, there ar 3 internal Windows functions
4070       4
4071 #else
4072       // With MSVC cl, RtlCaptureContext misses the stack frame of the current
4073       // function The first entries during StackWalk are the 3 internal Windows
4074       // functions
4075       3
4076 #endif
4077       ;
4078 
skip_recs()4079   static int &skip_recs() {
4080     static int data;
4081     return data;
4082   }
4083 
terminator()4084   static inline void terminator() {
4085     crash_handler(signal_skip_recs);
4086     abort();
4087   }
4088 
signal_handler(int)4089   static inline void signal_handler(int) {
4090     crash_handler(signal_skip_recs);
4091     abort();
4092   }
4093 
invalid_parameter_handler(const wchar_t *,const wchar_t *,const wchar_t *,unsigned int,uintptr_t)4094   static inline void __cdecl invalid_parameter_handler(const wchar_t *,
4095                                                        const wchar_t *,
4096                                                        const wchar_t *,
4097                                                        unsigned int,
4098                                                        uintptr_t) {
4099     crash_handler(signal_skip_recs);
4100     abort();
4101   }
4102 
crash_handler(EXCEPTION_POINTERS * info)4103   NOINLINE static LONG WINAPI crash_handler(EXCEPTION_POINTERS *info) {
4104     // The exception info supplies a trace from exactly where the issue was,
4105     // no need to skip records
4106     crash_handler(0, info->ContextRecord);
4107     return EXCEPTION_CONTINUE_SEARCH;
4108   }
4109 
crash_handler(int skip,CONTEXT * ct=nullptr)4110   NOINLINE static void crash_handler(int skip, CONTEXT *ct = nullptr) {
4111 
4112     if (ct == nullptr) {
4113       RtlCaptureContext(ctx());
4114     } else {
4115       memcpy(ctx(), ct, sizeof(CONTEXT));
4116     }
4117     DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
4118                     GetCurrentProcess(), &thread_handle(), 0, FALSE,
4119                     DUPLICATE_SAME_ACCESS);
4120 
4121     skip_recs() = skip;
4122 
4123     {
4124       std::unique_lock<std::mutex> lk(mtx());
4125       crashed() = crash_status::crashed;
4126     }
4127 
4128     cv().notify_one();
4129 
4130     {
4131       std::unique_lock<std::mutex> lk(mtx());
4132       cv().wait(lk, [] { return crashed() != crash_status::crashed; });
4133     }
4134   }
4135 
handle_stacktrace(int skip_frames=0)4136   static void handle_stacktrace(int skip_frames = 0) {
4137     // printer creates the TraceResolver, which can supply us a machine type
4138     // for stack walking. Without this, StackTrace can only guess using some
4139     // macros.
4140     // StackTrace also requires that the PDBs are already loaded, which is done
4141     // in the constructor of TraceResolver
4142     Printer printer;
4143 
4144     StackTrace st;
4145     st.set_machine_type(printer.resolver().machine_type());
4146     st.set_context(ctx());
4147     st.set_thread_handle(thread_handle());
4148     st.load_here(32 + skip_frames);
4149     st.skip_n_firsts(skip_frames);
4150 
4151     printer.address = true;
4152     printer.print(st, std::cerr);
4153   }
4154 };
4155 
4156 #endif // BACKWARD_SYSTEM_WINDOWS
4157 
4158 #ifdef BACKWARD_SYSTEM_UNKNOWN
4159 
4160 class SignalHandling {
4161 public:
SignalHandling(const std::vector<int> &=std::vector<int> ())4162   SignalHandling(const std::vector<int> & = std::vector<int>()) {}
init()4163   bool init() { return false; }
loaded()4164   bool loaded() { return false; }
4165 };
4166 
4167 #endif // BACKWARD_SYSTEM_UNKNOWN
4168 
4169 } // namespace backward
4170 
4171 #endif /* H_GUARD */
4172