OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2013, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
11 // copyright notice, this list of conditions and the following disclaimer | 11 // copyright notice, this list of conditions and the following disclaimer |
12 // in the documentation and/or other materials provided with the | 12 // in the documentation and/or other materials provided with the |
13 // distribution. | 13 // distribution. |
14 // * Neither the name of Google Inc. nor the names of its | 14 // * Neither the name of Google Inc. nor the names of its |
15 // contributors may be used to endorse or promote products derived from | 15 // contributors may be used to endorse or promote products derived from |
16 // this software without specific prior written permission. | 16 // this software without specific prior written permission. |
17 // | 17 // |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 // --- | 30 // --- |
31 // Author: Arun Sharma | 31 // Author: Marcus Bulach |
| 32 // This is inspired by Doug Kwan's ARM's stacktrace code and Dai Mikurube's |
| 33 // stack trace for chromium on android. |
32 // | 34 // |
33 // Produce stack trace using libunwind | |
34 | 35 |
35 #ifndef BASE_STACKTRACE_LIBINWIND_INL_H_ | 36 #ifndef BASE_STACKTRACE_ANDROID_INL_H_ |
36 #define BASE_STACKTRACE_LIBINWIND_INL_H_ | 37 #define BASE_STACKTRACE_ANDROID_INL_H_ |
37 // Note: this file is included into stacktrace.cc more than once. | 38 // Note: this file is included into stacktrace.cc more than once. |
38 // Anything that should only be defined once should be here: | 39 // Anything that should only be defined once should be here: |
39 | 40 |
40 // We only need local unwinder. | 41 #include <stdint.h> // for uintptr_t |
41 #define UNW_LOCAL_ONLY | 42 // See http://crbug.com/236855, would be better to use Bionic's |
| 43 // new get_backtrace(). |
| 44 #include <unwind.h> |
42 | 45 |
43 extern "C" { | 46 /* Depends on the system definition for _Unwind_Context */ |
44 #include <assert.h> | 47 #ifdef HAVE_UNWIND_CONTEXT_STRUCT |
45 #include <string.h> // for memset() | 48 typedef struct _Unwind_Context __unwind_context; |
46 #include <libunwind.h> | 49 #else |
| 50 typedef _Unwind_Context __unwind_context; |
| 51 #endif |
| 52 |
| 53 struct stack_crawl_state_t { |
| 54 uintptr_t* frames; |
| 55 size_t frame_count; |
| 56 int max_depth; |
| 57 int skip_count; |
| 58 bool have_skipped_self; |
| 59 |
| 60 stack_crawl_state_t(uintptr_t* frames, int max_depth, int skip_count) |
| 61 : frames(frames), |
| 62 frame_count(0), |
| 63 max_depth(max_depth), |
| 64 skip_count(skip_count), |
| 65 have_skipped_self(false) { |
| 66 } |
| 67 }; |
| 68 |
| 69 static _Unwind_Reason_Code tracer(__unwind_context* context, void* arg) { |
| 70 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); |
| 71 |
| 72 #if defined(__clang__) |
| 73 // Vanilla Clang's unwind.h doesn't have _Unwind_GetIP for ARM. |
| 74 // See http://crbug.com/236855, too. |
| 75 uintptr_t ip = 0; |
| 76 _Unwind_VRS_Get(context, _UVRSC_CORE, 15, _UVRSD_UINT32, &ip); |
| 77 ip &= ~(uintptr_t)0x1; // remove thumb mode bit |
| 78 #else |
| 79 uintptr_t ip = _Unwind_GetIP(context); |
| 80 #endif |
| 81 |
| 82 // The first stack frame is this function itself. Skip it. |
| 83 if (ip != 0 && !state->have_skipped_self) { |
| 84 state->have_skipped_self = true; |
| 85 return _URC_NO_REASON; |
| 86 } |
| 87 |
| 88 if (state->skip_count) { |
| 89 --state->skip_count; |
| 90 return _URC_NO_REASON; |
| 91 } |
| 92 |
| 93 state->frames[state->frame_count++] = ip; |
| 94 if (state->frame_count >= state->max_depth) |
| 95 return _URC_END_OF_STACK; |
| 96 else |
| 97 return _URC_NO_REASON; |
47 } | 98 } |
48 #include "gperftools/stacktrace.h" | |
49 #include "base/logging.h" | |
50 | 99 |
51 // Sometimes, we can try to get a stack trace from within a stack | 100 #endif // BASE_STACKTRACE_ANDROID_INL_H_ |
52 // trace, because libunwind can call mmap (maybe indirectly via an | |
53 // internal mmap based memory allocator), and that mmap gets trapped | |
54 // and causes a stack-trace request. If were to try to honor that | |
55 // recursive request, we'd end up with infinite recursion or deadlock. | |
56 // Luckily, it's safe to ignore those subsequent traces. In such | |
57 // cases, we return 0 to indicate the situation. | |
58 static __thread int recursive; | |
59 | |
60 #endif // BASE_STACKTRACE_LIBINWIND_INL_H_ | |
61 | 101 |
62 // Note: this part of the file is included several times. | 102 // Note: this part of the file is included several times. |
63 // Do not put globals below. | 103 // Do not put globals below. |
64 | 104 |
65 // The following 4 functions are generated from the code below: | 105 // The following 4 functions are generated from the code below: |
66 // GetStack{Trace,Frames}() | 106 // GetStack{Trace,Frames}() |
67 // GetStack{Trace,Frames}WithContext() | 107 // GetStack{Trace,Frames}WithContext() |
68 // | 108 // |
69 // These functions take the following args: | 109 // These functions take the following args: |
70 // void** result: the stack-trace, as an array | 110 // void** result: the stack-trace, as an array |
71 // int* sizes: the size of each stack frame, as an array | 111 // int* sizes: the size of each stack frame, as an array |
72 // (GetStackFrames* only) | 112 // (GetStackFrames* only) |
73 // int max_depth: the size of the result (and sizes) array(s) | 113 // int max_depth: the size of the result (and sizes) array(s) |
74 // int skip_count: how many stack pointers to skip before storing in result | 114 // int skip_count: how many stack pointers to skip before storing in result |
75 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) | 115 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
76 int GET_STACK_TRACE_OR_FRAMES { | 116 int GET_STACK_TRACE_OR_FRAMES { |
77 void *ip; | 117 stack_crawl_state_t state( |
78 int n = 0; | 118 reinterpret_cast<uintptr_t*>(result), max_depth, skip_count); |
79 unw_cursor_t cursor; | 119 _Unwind_Backtrace(tracer, &state); |
80 unw_context_t uc; | 120 return state.frame_count; |
81 #if IS_STACK_FRAMES | |
82 unw_word_t sp = 0, next_sp = 0; | |
83 #endif | |
84 | |
85 if (recursive) { | |
86 return 0; | |
87 } | |
88 ++recursive; | |
89 | |
90 unw_getcontext(&uc); | |
91 int ret = unw_init_local(&cursor, &uc); | |
92 assert(ret >= 0); | |
93 skip_count++; // Do not include current frame | |
94 | |
95 while (skip_count--) { | |
96 if (unw_step(&cursor) <= 0) { | |
97 goto out; | |
98 } | |
99 #if IS_STACK_FRAMES | |
100 if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp)) { | |
101 goto out; | |
102 } | |
103 #endif | |
104 } | |
105 | |
106 while (n < max_depth) { | |
107 if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip) < 0) { | |
108 break; | |
109 } | |
110 #if IS_STACK_FRAMES | |
111 sizes[n] = 0; | |
112 #endif | |
113 result[n++] = ip; | |
114 if (unw_step(&cursor) <= 0) { | |
115 break; | |
116 } | |
117 #if IS_STACK_FRAMES | |
118 sp = next_sp; | |
119 if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp) , 0) { | |
120 break; | |
121 } | |
122 sizes[n - 1] = next_sp - sp; | |
123 #endif | |
124 } | |
125 out: | |
126 --recursive; | |
127 return n; | |
128 } | 121 } |
OLD | NEW |