1 // Copyright 2005 - 2007 Google Inc.
2 // All rights reserved.
6 // Produce stack trace using libunwind
11 #include "base/stacktrace.h"
12 #include "base/raw_logging.h"
13 #include "base/spinlock.h"
15 _START_GOOGLE_NAMESPACE_
17 // Sometimes, we can try to get a stack trace from within a stack
18 // trace, because libunwind can call mmap/sbrk (maybe indirectly via
19 // malloc), and that mmap gets trapped and causes a stack-trace
20 // request. If were to try to honor that recursive request, we'd end
21 // up with infinite recursion or deadlock. Luckily, it's safe to
22 // ignore those subsequent traces. In such cases, we return 0 to
23 // indicate the situation.
24 static SpinLock libunwind_lock(SpinLock::LINKER_INITIALIZED);
26 // If you change this function, also change GetStackFrames below.
27 int GetStackTrace(void** result, int max_depth, int skip_count) {
33 if (!libunwind_lock.TryLock()) {
38 RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
39 skip_count++; // Do not include the "GetStackTrace" frame
41 while (n < max_depth) {
42 int ret = unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip);
50 ret = unw_step(&cursor);
55 libunwind_lock.Unlock();
59 // If you change this function, also change GetStackTrace above:
61 // This GetStackFrames routine shares a lot of code with GetStackTrace
62 // above. This code could have been refactored into a common routine,
63 // and then both GetStackTrace/GetStackFrames could call that routine.
64 // There are two problems with that:
66 // (1) The performance of the refactored-code suffers substantially - the
67 // refactored needs to be able to record the stack trace when called
68 // from GetStackTrace, and both the stack trace and stack frame sizes,
69 // when called from GetStackFrames - this introduces enough new
70 // conditionals that GetStackTrace performance can degrade by as much
73 // (2) Whether the refactored routine gets inlined into GetStackTrace and
74 // GetStackFrames depends on the compiler, and we can't guarantee the
75 // behavior either-way, even with "__attribute__ ((always_inline))"
76 // or "__attribute__ ((noinline))". But we need this guarantee or the
77 // frame counts may be off by one.
79 // Both (1) and (2) can be addressed without this code duplication, by
80 // clever use of template functions, and by defining GetStackTrace and
81 // GetStackFrames as macros that expand to these template functions.
82 // However, this approach comes with its own set of problems - namely,
83 // macros and preprocessor trouble - for example, if GetStackTrace
84 // and/or GetStackFrames is ever defined as a member functions in some
85 // class, we are in trouble.
86 int GetStackFrames(void** pcs, int* sizes, int max_depth, int skip_count) {
91 unw_word_t sp = 0, next_sp = 0;
93 if (!libunwind_lock.TryLock()) {
98 RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
99 skip_count++; // Do not include the "GetStackFrames" frame
101 while (skip_count--) {
102 if (unw_step(&cursor) <= 0 ||
103 unw_get_reg(&cursor, UNW_REG_SP, &next_sp) < 0) {
107 while (n < max_depth) {
109 if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip) < 0)
111 if (unw_step(&cursor) <= 0 ||
112 unw_get_reg(&cursor, UNW_REG_SP, &next_sp)) {
113 // We couldn't step any further (possibly because we reached _start).
114 // Provide the last good PC we've got, and get out.
119 sizes[n] = next_sp - sp;
123 libunwind_lock.Unlock();
127 _END_GOOGLE_NAMESPACE_