stacktrace.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Sanjay Ghemawat
  32. //
  33. // Routines to extract the current stack trace. These functions are
  34. // thread-safe.
  35. #ifndef GOOGLE_STACKTRACE_H_
  36. #define GOOGLE_STACKTRACE_H_
  37. // Annoying stuff for windows -- makes sure clients can import these functions
  38. #ifndef PERFTOOLS_DLL_DECL
  39. # ifdef _WIN32
  40. # define PERFTOOLS_DLL_DECL __declspec(dllimport)
  41. # else
  42. # define PERFTOOLS_DLL_DECL
  43. # endif
  44. #endif
  45. // Skips the most recent "skip_count" stack frames (also skips the
  46. // frame generated for the "GetStackFrames" routine itself), and then
  47. // records the pc values for up to the next "max_depth" frames in
  48. // "result", and the corresponding stack frame sizes in "sizes".
  49. // Returns the number of values recorded in "result"/"sizes".
  50. //
  51. // Example:
  52. // main() { foo(); }
  53. // foo() { bar(); }
  54. // bar() {
  55. // void* result[10];
  56. // int sizes[10];
  57. // int depth = GetStackFrames(result, sizes, 10, 1);
  58. // }
  59. //
  60. // The GetStackFrames call will skip the frame for "bar". It will
  61. // return 2 and will produce pc values that map to the following
  62. // procedures:
  63. // result[0] foo
  64. // result[1] main
  65. // (Actually, there may be a few more entries after "main" to account for
  66. // startup procedures.)
  67. // And corresponding stack frame sizes will also be recorded:
  68. // sizes[0] 16
  69. // sizes[1] 16
  70. // (Stack frame sizes of 16 above are just for illustration purposes.)
  71. // Stack frame sizes of 0 or less indicate that those frame sizes couldn't
  72. // be identified.
  73. //
  74. // This routine may return fewer stack frame entries than are
  75. // available. Also note that "result" and "sizes" must both be non-NULL.
  76. extern PERFTOOLS_DLL_DECL int GetStackFrames(void** result, int* sizes, int max_depth,
  77. int skip_count);
  78. // Same as above, but to be used from a signal handler. The "uc" parameter
  79. // should be the pointer to ucontext_t which was passed as the 3rd parameter
  80. // to sa_sigaction signal handler. It may help the unwinder to get a
  81. // better stack trace under certain conditions. The "uc" may safely be NULL.
  82. extern PERFTOOLS_DLL_DECL int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
  83. int skip_count, const void *uc);
  84. // This is similar to the GetStackFrames routine, except that it returns
  85. // the stack trace only, and not the stack frame sizes as well.
  86. // Example:
  87. // main() { foo(); }
  88. // foo() { bar(); }
  89. // bar() {
  90. // void* result[10];
  91. // int depth = GetStackTrace(result, 10, 1);
  92. // }
  93. //
  94. // This produces:
  95. // result[0] foo
  96. // result[1] main
  97. // .... ...
  98. //
  99. // "result" must not be NULL.
  100. extern PERFTOOLS_DLL_DECL int GetStackTrace(void** result, int max_depth,
  101. int skip_count);
  102. // Same as above, but to be used from a signal handler. The "uc" parameter
  103. // should be the pointer to ucontext_t which was passed as the 3rd parameter
  104. // to sa_sigaction signal handler. It may help the unwinder to get a
  105. // better stack trace under certain conditions. The "uc" may safely be NULL.
  106. extern PERFTOOLS_DLL_DECL int GetStackTraceWithContext(void** result, int max_depth,
  107. int skip_count, const void *uc);
  108. #endif /* GOOGLE_STACKTRACE_H_ */