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_
11 #include "base/base_export.h"
12 #include "build/build_config.h"
19 struct _EXCEPTION_POINTERS
;
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 BASE_EXPORT
bool EnableInProcessStackDumping();
31 // A different version of EnableInProcessStackDumping that also works for
32 // sandboxed processes. For more details take a look at the description
33 // of EnableInProcessStackDumping.
34 // Calling this function on Linux opens /proc/self/maps and caches its
35 // contents. In DEBUG builds, this function also opens the object files that
36 // are loaded in memory and caches their file descriptors (this cannot be
37 // done in official builds because it has security implications).
38 BASE_EXPORT
bool EnableInProcessStackDumpingForSandbox();
40 // A stacktrace can be helpful in debugging. For example, you can include a
41 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you
42 // can later see where the given object was created from.
43 class BASE_EXPORT StackTrace
{
45 // Creates a stacktrace from the current location.
48 // Creates a stacktrace from an existing array of instruction
49 // pointers (such as returned by Addresses()). |count| will be
50 // trimmed to |kMaxTraces|.
51 StackTrace(const void* const* trace
, size_t count
);
54 // Creates a stacktrace for an exception.
55 // Note: this function will throw an import not found (StackWalk64) exception
56 // on system without dbghelp 5.1.
57 StackTrace(const _EXCEPTION_POINTERS
* exception_pointers
);
58 StackTrace(const _CONTEXT
* context
);
61 // Copying and assignment are allowed with the default functions.
65 // Gets an array of instruction pointer values. |*count| will be set to the
66 // number of elements in the returned array.
67 const void* const* Addresses(size_t* count
) const;
69 // Prints the stack trace to stderr.
72 #if !defined(__UCLIBC__)
73 // Resolves backtrace to symbols and write to stream.
74 void OutputToStream(std::ostream
* os
) const;
77 // Resolves backtrace to symbols and returns as string.
78 std::string
ToString() const;
82 void InitTrace(_CONTEXT
* context_record
);
85 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx,
86 // the sum of FramesToSkip and FramesToCapture must be less than 63,
87 // so set it to 62. Even if on POSIX it could be a larger value, it usually
88 // doesn't give much more information.
89 static const int kMaxTraces
= 62;
91 void* trace_
[kMaxTraces
];
93 // The number of valid frames in |trace_|.
99 #if defined(OS_POSIX) && !defined(OS_ANDROID)
100 // POSIX doesn't define any async-signal safe function for converting
101 // an integer to ASCII. We'll have to define our own version.
102 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
103 // conversion was successful or NULL otherwise. It never writes more than "sz"
104 // bytes. Output will be truncated as needed, and a NUL character is always
106 BASE_EXPORT
char *itoa_r(intptr_t i
,
111 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
113 } // namespace internal
118 #endif // BASE_DEBUG_STACK_TRACE_H_