Chromium Code Reviews| 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: Sanjay Ghemawat | 31 // Author: Marcus Bulach |
| 32 // This is inspired by Doug Kwan's ARM's stacktrace code. | |
| 32 // | 33 // |
| 33 // Portable implementation - just use glibc | |
| 34 // | |
| 35 // Note: The glibc implementation may cause a call to malloc. | |
| 36 // This can cause a deadlock in HeapProfiler. | |
| 37 | 34 |
| 38 #ifndef BASE_STACKTRACE_GENERIC_INL_H_ | 35 #ifndef BASE_STACKTRACE_ANDROID_INL_H_ |
| 39 #define BASE_STACKTRACE_GENERIC_INL_H_ | 36 #define BASE_STACKTRACE_ANDROID_INL_H_ |
| 40 // Note: this file is included into stacktrace.cc more than once. | 37 // Note: this file is included into stacktrace.cc more than once. |
| 41 // Anything that should only be defined once should be here: | 38 // Anything that should only be defined once should be here: |
| 42 | 39 |
| 43 #include <execinfo.h> | 40 #include <stdint.h> // for uintptr_t |
| 44 #include <string.h> | 41 #include <unwind.h> |
|
Dai Mikurube (NOT FULLTIME)
2013/05/03 11:02:35
Import comments mentioning http://crbug.com/236855
bulach
2013/05/07 14:55:51
Done.
| |
| 45 #include "gperftools/stacktrace.h" | 42 |
| 46 #endif // BASE_STACKTRACE_GENERIC_INL_H_ | 43 typedef _Unwind_Context __unwind_context; |
| 44 | |
| 45 struct stack_crawl_state_t { | |
| 46 uintptr_t* frames; | |
| 47 size_t frame_count; | |
| 48 int max_depth; | |
| 49 int skip_count; | |
| 50 bool have_skipped_self; | |
| 51 | |
| 52 stack_crawl_state_t(uintptr_t* frames, int max_depth, int skip_count) | |
| 53 : frames(frames), | |
| 54 frame_count(0), | |
| 55 max_depth(max_depth), | |
| 56 skip_count(skip_count), | |
| 57 have_skipped_self(false) { | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 static _Unwind_Reason_Code tracer(__unwind_context* context, void* arg) { | |
| 62 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); | |
| 63 | |
| 64 uintptr_t ip = _Unwind_GetIP(context); | |
|
Dai Mikurube (NOT FULLTIME)
2013/05/03 11:02:35
Need to import the hack for Clang. (See the lates
bulach
2013/05/07 14:55:51
Done.
| |
| 65 | |
| 66 // The first stack frame is this function itself. Skip it. | |
| 67 if (ip != 0 && !state->have_skipped_self) { | |
| 68 state->have_skipped_self = true; | |
| 69 return _URC_NO_REASON; | |
| 70 } | |
| 71 | |
| 72 if (state->skip_count) { | |
| 73 --state->skip_count; | |
| 74 return _URC_NO_REASON; | |
| 75 } | |
| 76 | |
| 77 state->frames[state->frame_count++] = ip; | |
| 78 if (state->frame_count >= state->max_depth) | |
| 79 return _URC_END_OF_STACK; | |
| 80 else | |
| 81 return _URC_NO_REASON; | |
| 82 } | |
| 83 | |
| 84 #endif // BASE_STACKTRACE_ANDROID_INL_H_ | |
| 47 | 85 |
| 48 // Note: this part of the file is included several times. | 86 // Note: this part of the file is included several times. |
| 49 // Do not put globals below. | 87 // Do not put globals below. |
| 50 | 88 |
| 51 // The following 4 functions are generated from the code below: | 89 // The following 4 functions are generated from the code below: |
| 52 // GetStack{Trace,Frames}() | 90 // GetStack{Trace,Frames}() |
| 53 // GetStack{Trace,Frames}WithContext() | 91 // GetStack{Trace,Frames}WithContext() |
| 54 // | 92 // |
| 55 // These functions take the following args: | 93 // These functions take the following args: |
| 56 // void** result: the stack-trace, as an array | 94 // void** result: the stack-trace, as an array |
| 57 // int* sizes: the size of each stack frame, as an array | 95 // int* sizes: the size of each stack frame, as an array |
| 58 // (GetStackFrames* only) | 96 // (GetStackFrames* only) |
| 59 // int max_depth: the size of the result (and sizes) array(s) | 97 // int max_depth: the size of the result (and sizes) array(s) |
| 60 // int skip_count: how many stack pointers to skip before storing in result | 98 // int skip_count: how many stack pointers to skip before storing in result |
| 61 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) | 99 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
| 62 int GET_STACK_TRACE_OR_FRAMES { | 100 int GET_STACK_TRACE_OR_FRAMES { |
| 63 static const int kStackLength = 64; | 101 stack_crawl_state_t state( |
| 64 void * stack[kStackLength]; | 102 reinterpret_cast<uintptr_t*>(result), max_depth, skip_count); |
|
Dai Mikurube (NOT FULLTIME)
2013/05/03 11:02:35
nit: indent by 4
bulach
2013/05/07 14:55:51
Done.
| |
| 65 int size; | 103 _Unwind_Backtrace(tracer, &state); |
|
Dai Mikurube (NOT FULLTIME)
2013/05/03 11:02:35
We need
return state.frame_count;
here. (It's the
bulach
2013/05/07 14:55:51
ahn, great!!! done.
| |
| 66 | |
| 67 size = backtrace(stack, kStackLength); | |
| 68 skip_count++; // we want to skip the current frame as well | |
| 69 int result_count = size - skip_count; | |
| 70 if (result_count < 0) | |
| 71 result_count = 0; | |
| 72 if (result_count > max_depth) | |
| 73 result_count = max_depth; | |
| 74 for (int i = 0; i < result_count; i++) | |
| 75 result[i] = stack[i + skip_count]; | |
| 76 | |
| 77 #if IS_STACK_FRAMES | |
| 78 // No implementation for finding out the stack frame sizes yet. | |
| 79 memset(sizes, 0, sizeof(*sizes) * result_count); | |
| 80 #endif | |
| 81 | |
| 82 return result_count; | |
| 83 } | 104 } |
| OLD | NEW |