1 // Copyright (c) 2013, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
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.
36 #ifndef BASE_STACKTRACE_ANDROID_INL_H_
37 #define BASE_STACKTRACE_ANDROID_INL_H_
38 // Note: this file is included into stacktrace.cc more than once.
39 // Anything that should only be defined once should be here:
41 #include <stdint.h> // for uintptr_t
42 // See http://crbug.com/236855, would be better to use Bionic's
43 // new get_backtrace().
46 /* Depends on the system definition for _Unwind_Context */
47 #ifdef HAVE_UNWIND_CONTEXT_STRUCT
48 typedef struct _Unwind_Context __unwind_context
;
50 typedef _Unwind_Context __unwind_context
;
53 struct stack_crawl_state_t
{
58 bool have_skipped_self
;
60 stack_crawl_state_t(uintptr_t* frames
, int max_depth
, int skip_count
)
64 skip_count(skip_count
),
65 have_skipped_self(false) {
69 static _Unwind_Reason_Code
tracer(__unwind_context
* context
, void* arg
) {
70 stack_crawl_state_t
* state
= static_cast<stack_crawl_state_t
*>(arg
);
72 #if defined(__clang__)
73 // Vanilla Clang's unwind.h doesn't have _Unwind_GetIP for ARM.
74 // See http://crbug.com/236855, too.
76 _Unwind_VRS_Get(context
, _UVRSC_CORE
, 15, _UVRSD_UINT32
, &ip
);
77 ip
&= ~(uintptr_t)0x1; // remove thumb mode bit
79 uintptr_t ip
= _Unwind_GetIP(context
);
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
;
88 if (state
->skip_count
) {
90 return _URC_NO_REASON
;
93 state
->frames
[state
->frame_count
++] = ip
;
94 if (state
->frame_count
>= state
->max_depth
)
95 return _URC_END_OF_STACK
;
97 return _URC_NO_REASON
;
100 #endif // BASE_STACKTRACE_ANDROID_INL_H_
102 // Note: this part of the file is included several times.
103 // Do not put globals below.
105 // The following 4 functions are generated from the code below:
106 // GetStack{Trace,Frames}()
107 // GetStack{Trace,Frames}WithContext()
109 // These functions take the following args:
110 // void** result: the stack-trace, as an array
111 // int* sizes: the size of each stack frame, as an array
112 // (GetStackFrames* only)
113 // int max_depth: the size of the result (and sizes) array(s)
114 // int skip_count: how many stack pointers to skip before storing in result
115 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
116 int GET_STACK_TRACE_OR_FRAMES
{
117 stack_crawl_state_t
state(
118 reinterpret_cast<uintptr_t*>(result
), max_depth
, skip_count
);
119 _Unwind_Backtrace(tracer
, &state
);
120 return state
.frame_count
;