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 "base/macros.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 | 16 |
17 #if defined(OS_POSIX) | 17 #if defined(OS_POSIX) |
18 #include <unistd.h> | 18 #include <unistd.h> |
19 #endif | 19 #endif |
20 | 20 |
21 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
22 struct _EXCEPTION_POINTERS; | 22 struct _EXCEPTION_POINTERS; |
23 struct _CONTEXT; | 23 struct _CONTEXT; |
24 #endif | 24 #endif |
25 | 25 |
26 #if defined(OS_POSIX) && ( \ | 26 // TODO(699863): Clean up HAVE_TRACE_STACK_FRAME_POINTERS. |
27 defined(__i386__) || defined(__x86_64__) || \ | 27 #if defined(OS_POSIX) |
28 (defined(__arm__) && !defined(__thumb__))) | 28 |
| 29 #if defined(__i386__) || defined(__x86_64__) |
29 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 30 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 |
30 #else | 31 #elif defined(__arm__) && !defined(__thumb__) |
| 32 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 |
| 33 #else // defined(__arm__) && !defined(__thumb__) |
31 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | 34 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 |
32 #endif | 35 #endif // defined(__arm__) && !defined(__thumb__) |
| 36 |
| 37 #elif defined(OS_WIN) |
| 38 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 |
| 39 |
| 40 #else // defined(OS_WIN) |
| 41 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 |
| 42 #endif // defined(OS_WIN) |
33 | 43 |
34 namespace base { | 44 namespace base { |
35 namespace debug { | 45 namespace debug { |
36 | 46 |
37 // Enables stack dump to console output on exception and signals. | 47 // Enables stack dump to console output on exception and signals. |
38 // When enabled, the process will quit immediately. This is meant to be used in | 48 // When enabled, the process will quit immediately. This is meant to be used in |
39 // unit_tests only! This is not thread-safe: only call from main thread. | 49 // unit_tests only! This is not thread-safe: only call from main thread. |
40 // In sandboxed processes, this has to be called before the sandbox is turned | 50 // In sandboxed processes, this has to be called before the sandbox is turned |
41 // on. | 51 // on. |
42 // Calling this function on Linux opens /proc/self/maps and caches its | 52 // Calling this function on Linux opens /proc/self/maps and caches its |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // but not for release builds (although there are some exceptions). | 125 // but not for release builds (although there are some exceptions). |
116 // | 126 // |
117 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| | 127 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| |
118 // after skipping |skip_initial| frames. Note that the function itself is not | 128 // after skipping |skip_initial| frames. Note that the function itself is not |
119 // added to the trace so |skip_initial| should be 0 in most cases. | 129 // added to the trace so |skip_initial| should be 0 in most cases. |
120 // Returns number of frames written. | 130 // Returns number of frames written. |
121 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, | 131 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, |
122 size_t max_depth, | 132 size_t max_depth, |
123 size_t skip_initial); | 133 size_t skip_initial); |
124 | 134 |
| 135 #if !defined(OS_WIN) |
125 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding | 136 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding |
126 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. | 137 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. |
127 // Both frame pointers must come from __builtin_frame_address(). | 138 // Both frame pointers must come from __builtin_frame_address(). |
128 // Destructor restores original linkage of |fp| to avoid corrupting caller's | 139 // Destructor restores original linkage of |fp| to avoid corrupting caller's |
129 // frame register on return. | 140 // frame register on return. |
130 // | 141 // |
131 // This class can be used to repair broken stack frame chain in cases | 142 // This class can be used to repair broken stack frame chain in cases |
132 // when execution flow goes into code built without frame pointers: | 143 // when execution flow goes into code built without frame pointers: |
133 // | 144 // |
134 // void DoWork() { | 145 // void DoWork() { |
(...skipping 29 matching lines...) Expand all Loading... |
164 ScopedStackFrameLinker(void* fp, void* parent_fp); | 175 ScopedStackFrameLinker(void* fp, void* parent_fp); |
165 ~ScopedStackFrameLinker(); | 176 ~ScopedStackFrameLinker(); |
166 | 177 |
167 private: | 178 private: |
168 void* fp_; | 179 void* fp_; |
169 void* parent_fp_; | 180 void* parent_fp_; |
170 void* original_parent_fp_; | 181 void* original_parent_fp_; |
171 | 182 |
172 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); | 183 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); |
173 }; | 184 }; |
| 185 #endif // !defined(OS_WIN) |
174 | 186 |
175 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 187 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
176 | 188 |
177 namespace internal { | 189 namespace internal { |
178 | 190 |
179 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 191 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
180 // POSIX doesn't define any async-signal safe function for converting | 192 // POSIX doesn't define any async-signal safe function for converting |
181 // an integer to ASCII. We'll have to define our own version. | 193 // an integer to ASCII. We'll have to define our own version. |
182 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the | 194 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
183 // conversion was successful or NULL otherwise. It never writes more than "sz" | 195 // conversion was successful or NULL otherwise. It never writes more than "sz" |
184 // bytes. Output will be truncated as needed, and a NUL character is always | 196 // bytes. Output will be truncated as needed, and a NUL character is always |
185 // appended. | 197 // appended. |
186 BASE_EXPORT char *itoa_r(intptr_t i, | 198 BASE_EXPORT char *itoa_r(intptr_t i, |
187 char *buf, | 199 char *buf, |
188 size_t sz, | 200 size_t sz, |
189 int base, | 201 int base, |
190 size_t padding); | 202 size_t padding); |
191 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 203 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
192 | 204 |
193 } // namespace internal | 205 } // namespace internal |
194 | 206 |
195 } // namespace debug | 207 } // namespace debug |
196 } // namespace base | 208 } // namespace base |
197 | 209 |
198 #endif // BASE_DEBUG_STACK_TRACE_H_ | 210 #endif // BASE_DEBUG_STACK_TRACE_H_ |
OLD | NEW |