stacktrace_libunwind-inl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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: Arun Sharma
  32. //
  33. // Produce stack trace using libunwind
  34. #ifndef BASE_STACKTRACE_LIBINWIND_INL_H_
  35. #define BASE_STACKTRACE_LIBINWIND_INL_H_
  36. // Note: this file is included into stacktrace.cc more than once.
  37. // Anything that should only be defined once should be here:
  38. // We only need local unwinder.
  39. #define UNW_LOCAL_ONLY
  40. extern "C" {
  41. #include <assert.h>
  42. #include <string.h> // for memset()
  43. #include <libunwind.h>
  44. }
  45. #include "gperftools/stacktrace.h"
  46. #include "base/logging.h"
  47. // Sometimes, we can try to get a stack trace from within a stack
  48. // trace, because libunwind can call mmap (maybe indirectly via an
  49. // internal mmap based memory allocator), and that mmap gets trapped
  50. // and causes a stack-trace request. If were to try to honor that
  51. // recursive request, we'd end up with infinite recursion or deadlock.
  52. // Luckily, it's safe to ignore those subsequent traces. In such
  53. // cases, we return 0 to indicate the situation.
  54. static __thread int recursive;
  55. #if defined(TCMALLOC_ENABLE_UNWIND_FROM_UCONTEXT) && (defined(__i386__) || defined(__x86_64__)) && defined(__GNU_LIBRARY__)
  56. #define BASE_STACKTRACE_UNW_CONTEXT_IS_UCONTEXT 1
  57. #endif
  58. #endif // BASE_STACKTRACE_LIBINWIND_INL_H_
  59. // Note: this part of the file is included several times.
  60. // Do not put globals below.
  61. // The following 4 functions are generated from the code below:
  62. // GetStack{Trace,Frames}()
  63. // GetStack{Trace,Frames}WithContext()
  64. //
  65. // These functions take the following args:
  66. // void** result: the stack-trace, as an array
  67. // int* sizes: the size of each stack frame, as an array
  68. // (GetStackFrames* only)
  69. // int max_depth: the size of the result (and sizes) array(s)
  70. // int skip_count: how many stack pointers to skip before storing in result
  71. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  72. static int GET_STACK_TRACE_OR_FRAMES {
  73. void *ip;
  74. int n = 0;
  75. unw_cursor_t cursor;
  76. unw_context_t uc;
  77. #if IS_STACK_FRAMES
  78. unw_word_t sp = 0, next_sp = 0;
  79. #endif
  80. if (recursive) {
  81. return 0;
  82. }
  83. ++recursive;
  84. #if (IS_WITH_CONTEXT && defined(BASE_STACKTRACE_UNW_CONTEXT_IS_UCONTEXT))
  85. if (ucp) {
  86. uc = *(static_cast<unw_context_t *>(const_cast<void *>(ucp)));
  87. /* this is a bit weird. profiler.cc calls us with signal's ucontext
  88. * yet passing us 2 as skip_count and essentially assuming we won't
  89. * use ucontext. */
  90. /* In order to fix that I'm going to assume that if ucp is
  91. * non-null we're asked to ignore skip_count in case we're
  92. * able to use ucp */
  93. skip_count = 0;
  94. } else {
  95. unw_getcontext(&uc);
  96. skip_count += 2; // Do not include current and parent frame
  97. }
  98. #else
  99. unw_getcontext(&uc);
  100. skip_count += 2; // Do not include current and parent frame
  101. #endif
  102. int ret = unw_init_local(&cursor, &uc);
  103. assert(ret >= 0);
  104. while (skip_count--) {
  105. if (unw_step(&cursor) <= 0) {
  106. goto out;
  107. }
  108. #if IS_STACK_FRAMES
  109. if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp)) {
  110. goto out;
  111. }
  112. #endif
  113. }
  114. while (n < max_depth) {
  115. if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip) < 0) {
  116. break;
  117. }
  118. #if IS_STACK_FRAMES
  119. sizes[n] = 0;
  120. #endif
  121. result[n++] = ip;
  122. if (unw_step(&cursor) <= 0) {
  123. break;
  124. }
  125. #if IS_STACK_FRAMES
  126. sp = next_sp;
  127. if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp) , 0) {
  128. break;
  129. }
  130. sizes[n - 1] = next_sp - sp;
  131. #endif
  132. }
  133. out:
  134. --recursive;
  135. return n;
  136. }