stack_trace.h 3.7 KB

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