Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2013, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      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
     13 // distribution.
     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.
     17 //
     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.
     29 
     30 // ---
     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.
     34 //
     35 
     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:
     40 
     41 #include <stdint.h>   // for uintptr_t
     42 // See http://crbug.com/236855, would be better to use Bionic's
     43 // new get_backtrace().
     44 #include <unwind.h>
     45 
     46 /* Depends on the system definition for _Unwind_Context */
     47 #ifdef HAVE_UNWIND_CONTEXT_STRUCT
     48 typedef struct _Unwind_Context __unwind_context;
     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;
     98 }
     99 
    100 #endif  // BASE_STACKTRACE_ANDROID_INL_H_
    101 
    102 // Note: this part of the file is included several times.
    103 // Do not put globals below.
    104 
    105 // The following 4 functions are generated from the code below:
    106 //   GetStack{Trace,Frames}()
    107 //   GetStack{Trace,Frames}WithContext()
    108 //
    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;
    121 }
    122