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 3 // found in the LICENSE file. 4 5 #ifndef BASE_DEBUG_STACK_TRACE_H_ 6 #define BASE_DEBUG_STACK_TRACE_H_ 7 8 #include <stddef.h> 9 10 #include <iosfwd> 11 #include <string> 12 13 #include "base/base_export.h" 14 #include "build/build_config.h" 15 16 #if defined(OS_POSIX) 17 #include <unistd.h> 18 #endif 19 20 #if defined(OS_WIN) 21 struct _EXCEPTION_POINTERS; 22 struct _CONTEXT; 23 #endif 24 25 #if defined(OS_POSIX) && ( \ 26 defined(__i386__) || defined(__x86_64__) || \ 27 (defined(__arm__) && !defined(__thumb__))) 28 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 29 #else 30 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 31 #endif 32 33 namespace base { 34 namespace debug { 35 36 // Enables stack dump to console output on exception and signals. 37 // When enabled, the process will quit immediately. This is meant to be used in 38 // unit_tests only! This is not thread-safe: only call from main thread. 39 // In sandboxed processes, this has to be called before the sandbox is turned 40 // on. 41 // Calling this function on Linux opens /proc/self/maps and caches its 42 // contents. In non-official builds, this function also opens the object files 43 // that are loaded in memory and caches their file descriptors (this cannot be 44 // done in official builds because it has security implications). 45 BASE_EXPORT bool EnableInProcessStackDumping(); 46 47 // A stacktrace can be helpful in debugging. For example, you can include a 48 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you 49 // can later see where the given object was created from. 50 class BASE_EXPORT StackTrace { 51 public: 52 // Creates a stacktrace from the current location. 53 StackTrace(); 54 55 // Creates a stacktrace from an existing array of instruction 56 // pointers (such as returned by Addresses()). |count| will be 57 // trimmed to |kMaxTraces|. 58 StackTrace(const void* const* trace, size_t count); 59 60 #if defined(OS_WIN) 61 // Creates a stacktrace for an exception. 62 // Note: this function will throw an import not found (StackWalk64) exception 63 // on system without dbghelp 5.1. 64 StackTrace(_EXCEPTION_POINTERS* exception_pointers); 65 StackTrace(const _CONTEXT* context); 66 #endif 67 68 // Copying and assignment are allowed with the default functions. 69 70 ~StackTrace(); 71 72 // Gets an array of instruction pointer values. |*count| will be set to the 73 // number of elements in the returned array. 74 const void* const* Addresses(size_t* count) const; 75 76 // Prints the stack trace to stderr. 77 void Print() const; 78 79 #if !defined(__UCLIBC__) 80 // Resolves backtrace to symbols and write to stream. 81 void OutputToStream(std::ostream* os) const; 82 #endif 83 84 // Resolves backtrace to symbols and returns as string. 85 std::string ToString() const; 86 87 private: 88 #if defined(OS_WIN) 89 void InitTrace(const _CONTEXT* context_record); 90 #endif 91 92 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, 93 // the sum of FramesToSkip and FramesToCapture must be less than 63, 94 // so set it to 62. Even if on POSIX it could be a larger value, it usually 95 // doesn't give much more information. 96 static const int kMaxTraces = 62; 97 98 void* trace_[kMaxTraces]; 99 100 // The number of valid frames in |trace_|. 101 size_t count_; 102 }; 103 104 #if HAVE_TRACE_STACK_FRAME_POINTERS 105 // Traces the stack by using frame pointers. This function is faster but less 106 // reliable than StackTrace. It should work for debug and profiling builds, 107 // but not for release builds (although there are some exceptions). 108 // 109 // 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 // added to the trace so |skip_initial| should be 0 in most cases. 112 // Returns number of frames written. 113 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, 114 size_t max_depth, 115 size_t skip_initial); 116 #endif // HAVE_TRACE_STACK_FRAME_POINTERS 117 118 namespace internal { 119 120 #if defined(OS_POSIX) && !defined(OS_ANDROID) 121 // POSIX doesn't define any async-signal safe function for converting 122 // 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 124 // 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 126 // appended. 127 BASE_EXPORT char *itoa_r(intptr_t i, 128 char *buf, 129 size_t sz, 130 int base, 131 size_t padding); 132 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) 133 134 } // namespace internal 135 136 } // namespace debug 137 } // namespace base 138 139 #endif // BASE_DEBUG_STACK_TRACE_H_ 140