StackWalk.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* API for getting a stack trace of the C/C++ stack on the current thread */
  7. #ifndef mozilla_StackWalk_h
  8. #define mozilla_StackWalk_h
  9. /* WARNING: This file is intended to be included from C or C++ files. */
  10. #include "mozilla/Types.h"
  11. #include <stdint.h>
  12. /**
  13. * The callback for MozStackWalk.
  14. *
  15. * @param aFrameNumber The frame number (starts at 1, not 0).
  16. * @param aPC The program counter value.
  17. * @param aSP The best approximation possible of what the stack
  18. * pointer will be pointing to when the execution returns
  19. * to executing that at aPC. If no approximation can
  20. * be made it will be nullptr.
  21. * @param aClosure Extra data passed in via MozStackWalk().
  22. */
  23. typedef void
  24. (*MozWalkStackCallback)(uint32_t aFrameNumber, void* aPC, void* aSP,
  25. void* aClosure);
  26. /**
  27. * Call aCallback for the C/C++ stack frames on the current thread, from
  28. * the caller of MozStackWalk to main (or above).
  29. *
  30. * @param aCallback Callback function, called once per frame.
  31. * @param aSkipFrames Number of initial frames to skip. 0 means that
  32. * the first callback will be for the caller of
  33. * MozStackWalk.
  34. * @param aMaxFrames Maximum number of frames to trace. 0 means no limit.
  35. * @param aClosure Caller-supplied data passed through to aCallback.
  36. * @param aThread The thread for which the stack is to be retrieved.
  37. * Passing null causes us to walk the stack of the
  38. * current thread. On Windows, this is a thread HANDLE.
  39. * It is currently not supported on any other platform.
  40. * @param aPlatformData Platform specific data that can help in walking the
  41. * stack, this should be nullptr unless you really know
  42. * what you're doing! This needs to be a pointer to a
  43. * CONTEXT on Windows and should not be passed on other
  44. * platforms.
  45. *
  46. * May skip some stack frames due to compiler optimizations or code
  47. * generation.
  48. *
  49. */
  50. MFBT_API bool
  51. MozStackWalk(MozWalkStackCallback aCallback, uint32_t aSkipFrames,
  52. uint32_t aMaxFrames, void* aClosure, uintptr_t aThread,
  53. void* aPlatformData);
  54. typedef struct
  55. {
  56. /*
  57. * The name of the shared library or executable containing an
  58. * address and the address's offset within that library, or empty
  59. * string and zero if unknown.
  60. */
  61. char library[256];
  62. ptrdiff_t loffset;
  63. /*
  64. * The name of the file name and line number of the code
  65. * corresponding to the address, or empty string and zero if
  66. * unknown.
  67. */
  68. char filename[256];
  69. unsigned long lineno;
  70. /*
  71. * The name of the function containing an address and the address's
  72. * offset within that function, or empty string and zero if unknown.
  73. */
  74. char function[256];
  75. ptrdiff_t foffset;
  76. } MozCodeAddressDetails;
  77. /**
  78. * For a given pointer to code, fill in the pieces of information used
  79. * when printing a stack trace.
  80. *
  81. * @param aPC The code address.
  82. * @param aDetails A structure to be filled in with the result.
  83. */
  84. MFBT_API bool
  85. MozDescribeCodeAddress(void* aPC, MozCodeAddressDetails* aDetails);
  86. /**
  87. * Format the information about a code address in a format suitable for
  88. * stack traces on the current platform. When available, this string
  89. * should contain the function name, source file, and line number. When
  90. * these are not available, library and offset should be reported, if
  91. * possible.
  92. *
  93. * Note that this output is parsed by several scripts including the fix*.py and
  94. * make-tree.pl scripts in tools/rb/. It should only be change with care, and
  95. * in conjunction with those scripts.
  96. *
  97. * @param aBuffer A string to be filled in with the description.
  98. * The string will always be null-terminated.
  99. * @param aBufferSize The size, in bytes, of aBuffer, including
  100. * room for the terminating null. If the information
  101. * to be printed would be larger than aBuffer, it
  102. * will be truncated so that aBuffer[aBufferSize-1]
  103. * is the terminating null.
  104. * @param aFrameNumber The frame number.
  105. * @param aPC The code address.
  106. * @param aFunction The function name. Possibly null or the empty string.
  107. * @param aLibrary The library name. Possibly null or the empty string.
  108. * @param aLOffset The library offset.
  109. * @param aFileName The filename. Possibly null or the empty string.
  110. * @param aLineNo The line number. Possibly zero.
  111. */
  112. MFBT_API void
  113. MozFormatCodeAddress(char* aBuffer, uint32_t aBufferSize, uint32_t aFrameNumber,
  114. const void* aPC, const char* aFunction,
  115. const char* aLibrary, ptrdiff_t aLOffset,
  116. const char* aFileName, uint32_t aLineNo);
  117. /**
  118. * Format the information about a code address in the same fashion as
  119. * MozFormatCodeAddress.
  120. *
  121. * @param aBuffer A string to be filled in with the description.
  122. * The string will always be null-terminated.
  123. * @param aBufferSize The size, in bytes, of aBuffer, including
  124. * room for the terminating null. If the information
  125. * to be printed would be larger than aBuffer, it
  126. * will be truncated so that aBuffer[aBufferSize-1]
  127. * is the terminating null.
  128. * @param aFrameNumber The frame number.
  129. * @param aPC The code address.
  130. * @param aDetails The value filled in by MozDescribeCodeAddress(aPC).
  131. */
  132. MFBT_API void
  133. MozFormatCodeAddressDetails(char* aBuffer, uint32_t aBufferSize,
  134. uint32_t aFrameNumber, void* aPC,
  135. const MozCodeAddressDetails* aDetails);
  136. namespace mozilla {
  137. MFBT_API bool
  138. FramePointerStackWalk(MozWalkStackCallback aCallback, uint32_t aSkipFrames,
  139. uint32_t aMaxFrames, void* aClosure, void** aBp,
  140. void* aStackEnd);
  141. } // namespace mozilla
  142. /**
  143. * Initialize the critical sections for this platform so that we can
  144. * abort stack walks when needed.
  145. */
  146. MFBT_API void
  147. StackWalkInitCriticalAddress(void);
  148. #endif