Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / base / debug / stack_trace.h
blob85c91777b49737dd2eb1fbd2fa2d24eaa285d438
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.
5 #ifndef BASE_DEBUG_STACK_TRACE_H_
6 #define BASE_DEBUG_STACK_TRACE_H_
8 #include <iosfwd>
9 #include <string>
11 #include "base/base_export.h"
12 #include "build/build_config.h"
14 #if defined(OS_POSIX)
15 #include <unistd.h>
16 #endif
18 #if defined(OS_WIN)
19 struct _EXCEPTION_POINTERS;
20 struct _CONTEXT;
21 #endif
23 namespace base {
24 namespace debug {
26 // Enables stack dump to console output on exception and signals.
27 // When enabled, the process will quit immediately. This is meant to be used in
28 // unit_tests only! This is not thread-safe: only call from main thread.
29 // In sandboxed processes, this has to be called before the sandbox is turned
30 // on.
31 // Calling this function on Linux opens /proc/self/maps and caches its
32 // contents. In non-official builds, this function also opens the object files
33 // that are loaded in memory and caches their file descriptors (this cannot be
34 // done in official builds because it has security implications).
35 BASE_EXPORT bool EnableInProcessStackDumping();
37 // A stacktrace can be helpful in debugging. For example, you can include a
38 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you
39 // can later see where the given object was created from.
40 class BASE_EXPORT StackTrace {
41 public:
42 // Creates a stacktrace from the current location.
43 StackTrace();
45 // Creates a stacktrace from an existing array of instruction
46 // pointers (such as returned by Addresses()). |count| will be
47 // trimmed to |kMaxTraces|.
48 StackTrace(const void* const* trace, size_t count);
50 #if defined(OS_WIN)
51 // Creates a stacktrace for an exception.
52 // Note: this function will throw an import not found (StackWalk64) exception
53 // on system without dbghelp 5.1.
54 StackTrace(_EXCEPTION_POINTERS* exception_pointers);
55 StackTrace(_CONTEXT* context);
56 #endif
58 // Copying and assignment are allowed with the default functions.
60 ~StackTrace();
62 // Gets an array of instruction pointer values. |*count| will be set to the
63 // number of elements in the returned array.
64 const void* const* Addresses(size_t* count) const;
66 // Prints the stack trace to stderr.
67 void Print() const;
69 #if !defined(__UCLIBC__)
70 // Resolves backtrace to symbols and write to stream.
71 void OutputToStream(std::ostream* os) const;
72 #endif
74 // Resolves backtrace to symbols and returns as string.
75 std::string ToString() const;
77 private:
78 #if defined(OS_WIN)
79 void InitTrace(_CONTEXT* context_record);
80 #endif
82 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx,
83 // the sum of FramesToSkip and FramesToCapture must be less than 63,
84 // so set it to 62. Even if on POSIX it could be a larger value, it usually
85 // doesn't give much more information.
86 static const int kMaxTraces = 62;
88 void* trace_[kMaxTraces];
90 // The number of valid frames in |trace_|.
91 size_t count_;
94 namespace internal {
96 #if defined(OS_POSIX) && !defined(OS_ANDROID)
97 // POSIX doesn't define any async-signal safe function for converting
98 // an integer to ASCII. We'll have to define our own version.
99 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
100 // conversion was successful or NULL otherwise. It never writes more than "sz"
101 // bytes. Output will be truncated as needed, and a NUL character is always
102 // appended.
103 BASE_EXPORT char *itoa_r(intptr_t i,
104 char *buf,
105 size_t sz,
106 int base,
107 size_t padding);
108 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
110 } // namespace internal
112 } // namespace debug
113 } // namespace base
115 #endif // BASE_DEBUG_STACK_TRACE_H_