[email protected] | e645c7a | 2012-07-25 21:43:44 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_DEBUG_STACK_TRACE_H_ |
| 6 | #define BASE_DEBUG_STACK_TRACE_H_ |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 7 | |
avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame^] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 10 | #include <iosfwd> |
[email protected] | d4114ba | 2011-10-12 16:13:40 | [diff] [blame] | 11 | #include <string> |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 12 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 13 | #include "base/base_export.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 14 | #include "build/build_config.h" |
| 15 | |
[email protected] | 47c4556 | 2012-11-17 14:33:25 | [diff] [blame] | 16 | #if defined(OS_POSIX) |
| 17 | #include <unistd.h> |
| 18 | #endif |
| 19 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 20 | #if defined(OS_WIN) |
| 21 | struct _EXCEPTION_POINTERS; |
wittman | 7808da7 | 2015-02-03 17:46:51 | [diff] [blame] | 22 | struct _CONTEXT; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | namespace base { |
| 26 | namespace debug { |
| 27 | |
[email protected] | 11b93faa | 2012-11-01 21:58:30 | [diff] [blame] | 28 | // Enables stack dump to console output on exception and signals. |
| 29 | // When enabled, the process will quit immediately. This is meant to be used in |
| 30 | // unit_tests only! This is not thread-safe: only call from main thread. |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 31 | // In sandboxed processes, this has to be called before the sandbox is turned |
| 32 | // on. |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 33 | // Calling this function on Linux opens /proc/self/maps and caches its |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 34 | // contents. In non-official builds, this function also opens the object files |
| 35 | // that are loaded in memory and caches their file descriptors (this cannot be |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 36 | // done in official builds because it has security implications). |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 37 | BASE_EXPORT bool EnableInProcessStackDumping(); |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 38 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 39 | // A stacktrace can be helpful in debugging. For example, you can include a |
| 40 | // stacktrace member in a object (probably around #ifndef NDEBUG) so that you |
| 41 | // can later see where the given object was created from. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 42 | class BASE_EXPORT StackTrace { |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 43 | public: |
| 44 | // Creates a stacktrace from the current location. |
| 45 | StackTrace(); |
| 46 | |
[email protected] | f01ae981 | 2011-08-30 19:33:04 | [diff] [blame] | 47 | // Creates a stacktrace from an existing array of instruction |
| 48 | // pointers (such as returned by Addresses()). |count| will be |
| 49 | // trimmed to |kMaxTraces|. |
| 50 | StackTrace(const void* const* trace, size_t count); |
| 51 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 52 | #if defined(OS_WIN) |
| 53 | // Creates a stacktrace for an exception. |
| 54 | // Note: this function will throw an import not found (StackWalk64) exception |
| 55 | // on system without dbghelp 5.1. |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 56 | StackTrace(_EXCEPTION_POINTERS* exception_pointers); |
rnk | 0565cdd6 | 2015-10-06 16:48:30 | [diff] [blame] | 57 | StackTrace(const _CONTEXT* context); |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | // Copying and assignment are allowed with the default functions. |
| 61 | |
| 62 | ~StackTrace(); |
| 63 | |
| 64 | // Gets an array of instruction pointer values. |*count| will be set to the |
| 65 | // number of elements in the returned array. |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame] | 66 | const void* const* Addresses(size_t* count) const; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 67 | |
[email protected] | 5ddbf1c | 2013-08-29 01:59:38 | [diff] [blame] | 68 | // Prints the stack trace to stderr. |
| 69 | void Print() const; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 70 | |
[email protected] | 7cf10ed | 2014-06-27 09:21:03 | [diff] [blame] | 71 | #if !defined(__UCLIBC__) |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 72 | // Resolves backtrace to symbols and write to stream. |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame] | 73 | void OutputToStream(std::ostream* os) const; |
[email protected] | 816e5045 | 2014-04-09 22:29:38 | [diff] [blame] | 74 | #endif |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 75 | |
[email protected] | d4114ba | 2011-10-12 16:13:40 | [diff] [blame] | 76 | // Resolves backtrace to symbols and returns as string. |
| 77 | std::string ToString() const; |
| 78 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 79 | private: |
wittman | 7808da7 | 2015-02-03 17:46:51 | [diff] [blame] | 80 | #if defined(OS_WIN) |
rnk | 0565cdd6 | 2015-10-06 16:48:30 | [diff] [blame] | 81 | void InitTrace(const _CONTEXT* context_record); |
wittman | 7808da7 | 2015-02-03 17:46:51 | [diff] [blame] | 82 | #endif |
| 83 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 84 | // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, |
| 85 | // the sum of FramesToSkip and FramesToCapture must be less than 63, |
| 86 | // so set it to 62. Even if on POSIX it could be a larger value, it usually |
| 87 | // doesn't give much more information. |
| 88 | static const int kMaxTraces = 62; |
| 89 | |
| 90 | void* trace_[kMaxTraces]; |
[email protected] | e645c7a | 2012-07-25 21:43:44 | [diff] [blame] | 91 | |
| 92 | // The number of valid frames in |trace_|. |
| 93 | size_t count_; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 94 | }; |
| 95 | |
[email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 96 | namespace internal { |
| 97 | |
| 98 | #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 99 | // POSIX doesn't define any async-signal safe function for converting |
| 100 | // an integer to ASCII. We'll have to define our own version. |
| 101 | // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
| 102 | // conversion was successful or NULL otherwise. It never writes more than "sz" |
| 103 | // bytes. Output will be truncated as needed, and a NUL character is always |
| 104 | // appended. |
[email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 105 | BASE_EXPORT char *itoa_r(intptr_t i, |
| 106 | char *buf, |
| 107 | size_t sz, |
| 108 | int base, |
| 109 | size_t padding); |
[email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 110 | #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 111 | |
| 112 | } // namespace internal |
| 113 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 114 | } // namespace debug |
| 115 | } // namespace base |
| 116 | |
| 117 | #endif // BASE_DEBUG_STACK_TRACE_H_ |