glog 0.1
[platform/upstream/glog.git] / src / stacktrace_libunwind-inl.h
1 // Copyright 2005 - 2007 Google Inc.
2 // All rights reserved.
3 //
4 // Author: Arun Sharma
5 //
6 // Produce stack trace using libunwind
7
8 extern "C" {
9 #include <libunwind.h>
10 }
11 #include "base/stacktrace.h"
12 #include "base/raw_logging.h"
13 #include "base/spinlock.h"
14
15 _START_GOOGLE_NAMESPACE_
16
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);
25
26 // If you change this function, also change GetStackFrames below.
27 int GetStackTrace(void** result, int max_depth, int skip_count) {
28   void *ip;
29   int n = 0;
30   unw_cursor_t cursor;
31   unw_context_t uc;
32
33   if (!libunwind_lock.TryLock()) {
34     return 0;
35   }
36
37   unw_getcontext(&uc);
38   RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
39   skip_count++;         // Do not include the "GetStackTrace" frame
40
41   while (n < max_depth) {
42     int ret = unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip);
43     if (ret < 0)
44       break;
45     if (skip_count > 0) {
46       skip_count--;
47     } else {
48       result[n++] = ip;
49     }
50     ret = unw_step(&cursor);
51     if (ret <= 0)
52       break;
53   }
54
55   libunwind_lock.Unlock();
56   return n;
57 }
58
59 // If you change this function, also change GetStackTrace above:
60 //
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:
65 //
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
71 //     as 50%.
72 //
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.
78 //
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) {
87   void *ip;
88   int n = 0;
89   unw_cursor_t cursor;
90   unw_context_t uc;
91   unw_word_t sp = 0, next_sp = 0;
92
93   if (!libunwind_lock.TryLock()) {
94     return 0;
95   }
96
97   unw_getcontext(&uc);
98   RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
99   skip_count++;         // Do not include the "GetStackFrames" frame
100
101   while (skip_count--) {
102     if (unw_step(&cursor) <= 0 ||
103         unw_get_reg(&cursor, UNW_REG_SP, &next_sp) < 0) {
104       goto out;
105     }
106   }
107   while (n < max_depth) {
108     sp = next_sp;
109     if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip) < 0)
110       break;
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.
115       sizes[n] = 0;
116       pcs[n++] = ip;
117       break;
118     }
119     sizes[n] = next_sp - sp;
120     pcs[n++] = ip;
121   }
122  out:
123   libunwind_lock.Unlock();
124   return n;
125 }
126
127 _END_GOOGLE_NAMESPACE_