Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_DEBUG_STACK_TRACE_H_ | 5 #ifndef BASE_DEBUG_STACK_TRACE_H_ |
| 6 #define BASE_DEBUG_STACK_TRACE_H_ | 6 #define BASE_DEBUG_STACK_TRACE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <iosfwd> | 10 #include <iosfwd> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 14 #include "base/macros.h" | |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 | 16 |
| 16 #if defined(OS_POSIX) | 17 #if defined(OS_POSIX) |
| 17 #include <unistd.h> | 18 #include <unistd.h> |
| 18 #endif | 19 #endif |
| 19 | 20 |
| 20 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
| 21 struct _EXCEPTION_POINTERS; | 22 struct _EXCEPTION_POINTERS; |
| 22 struct _CONTEXT; | 23 struct _CONTEXT; |
| 23 #endif | 24 #endif |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 // reliable than StackTrace. It should work for debug and profiling builds, | 107 // reliable than StackTrace. It should work for debug and profiling builds, |
| 107 // but not for release builds (although there are some exceptions). | 108 // but not for release builds (although there are some exceptions). |
| 108 // | 109 // |
| 109 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| | 110 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| |
| 110 // after skipping |skip_initial| frames. Note that the function itself is not | 111 // after skipping |skip_initial| frames. Note that the function itself is not |
| 111 // added to the trace so |skip_initial| should be 0 in most cases. | 112 // added to the trace so |skip_initial| should be 0 in most cases. |
| 112 // Returns number of frames written. | 113 // Returns number of frames written. |
| 113 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, | 114 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, |
| 114 size_t max_depth, | 115 size_t max_depth, |
| 115 size_t skip_initial); | 116 size_t skip_initial); |
| 117 | |
| 118 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding | |
| 119 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. | |
| 120 // Both frame pointers must come from __builtin_frame_address(). | |
|
Primiano Tucci (use gerrit)
2016/10/13 20:03:21
+0 (i.e. __builtin_frame_address(0))
Dmitry Skiba
2016/10/17 22:09:11
Not necessarily, if __builtin_frame_address(7) wor
| |
| 121 // Destructor restores original linkage of |fp| to avoid corrupting caller's | |
| 122 // frame register on return. | |
| 123 // | |
| 124 // This class can be used to repair broken stack frame chain in cases | |
| 125 // when execution flow goes into code built without frame pointers: | |
| 126 // | |
| 127 // void DoWork() { | |
| 128 // CallJava(); | |
| 129 // } | |
| 130 // static __thread void* g_saved_fp; | |
| 131 // void CallJava() { | |
| 132 // g_saved_fp = __builtin_frame_address(0); | |
| 133 // JNI->Call(...); // indirectly calls JavaCallback() | |
| 134 // } | |
| 135 // void JavaCallback() { | |
| 136 // ScopedStackFrameLinker linker(__builtin_frame_address(0), g_saved_fp); | |
| 137 // ... | |
| 138 // TraceStackFramePointers(...); | |
| 139 // } | |
| 140 // | |
| 141 // This produces the following trace: | |
| 142 // | |
| 143 // #0 JavaCallback() | |
| 144 // #1 <address of the code inside JVM that called JavaCallback> | |
| 145 // #2 DoWork() | |
| 146 // ...rest of the trace... | |
| 147 // | |
| 148 // JVM doesn't use frame pointers, so when JavaCallback() is called back by | |
|
Primiano Tucci (use gerrit)
2016/10/13 20:03:21
Honestly I think that these lines of comment (148-
Dmitry Skiba
2016/10/17 22:09:11
The fact that JNI is used in explanation how Scope
| |
| 149 // JVM, stack frame register contains bogus value that becomes JavaCallback's | |
| 150 // parent frame address. Without LinkStackFrames() unwinding would've stopped | |
| 151 // at that bogus frame address yielding just two first frames (#0 and #1). | |
| 152 // LinkStackFrames() overwrites JavaCallback's parent frame address with | |
| 153 // CallJava's frame, so unwinder produces full trace without even noticing | |
| 154 // that stack frame chain was broken. | |
| 155 class BASE_EXPORT ScopedStackFrameLinker { | |
| 156 public: | |
| 157 ScopedStackFrameLinker(void* fp, void* parent_fp); | |
| 158 ~ScopedStackFrameLinker(); | |
| 159 private: | |
|
Primiano Tucci (use gerrit)
2016/10/13 20:03:21
nit \n here and below (before disallow)
Dmitry Skiba
2016/10/17 22:09:11
Done.
| |
| 160 void* fp_; | |
| 161 void* original_parent_fp_; | |
| 162 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); | |
| 163 }; | |
| 164 | |
| 116 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 165 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
| 117 | 166 |
| 118 namespace internal { | 167 namespace internal { |
| 119 | 168 |
| 120 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 169 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 121 // POSIX doesn't define any async-signal safe function for converting | 170 // POSIX doesn't define any async-signal safe function for converting |
| 122 // an integer to ASCII. We'll have to define our own version. | 171 // an integer to ASCII. We'll have to define our own version. |
| 123 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the | 172 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
| 124 // conversion was successful or NULL otherwise. It never writes more than "sz" | 173 // conversion was successful or NULL otherwise. It never writes more than "sz" |
| 125 // bytes. Output will be truncated as needed, and a NUL character is always | 174 // bytes. Output will be truncated as needed, and a NUL character is always |
| 126 // appended. | 175 // appended. |
| 127 BASE_EXPORT char *itoa_r(intptr_t i, | 176 BASE_EXPORT char *itoa_r(intptr_t i, |
| 128 char *buf, | 177 char *buf, |
| 129 size_t sz, | 178 size_t sz, |
| 130 int base, | 179 int base, |
| 131 size_t padding); | 180 size_t padding); |
| 132 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 181 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 133 | 182 |
| 134 } // namespace internal | 183 } // namespace internal |
| 135 | 184 |
| 136 } // namespace debug | 185 } // namespace debug |
| 137 } // namespace base | 186 } // namespace base |
| 138 | 187 |
| 139 #endif // BASE_DEBUG_STACK_TRACE_H_ | 188 #endif // BASE_DEBUG_STACK_TRACE_H_ |
| OLD | NEW |