stacktrace_instrument-inl.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2013, 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: Jean Lee <xiaoyur347@gmail.com>
  32. // based on gcc Code-Gen-Options "-finstrument-functions" listed in
  33. // http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html .
  34. // Should run configure with CXXFLAGS="-finstrument-functions".
  35. // This file is a backtrace implementation for systems :
  36. // * The glibc implementation of backtrace() may cause a call to malloc,
  37. // and cause a deadlock in HeapProfiler.
  38. // * The libunwind implementation prints no backtrace.
  39. // The backtrace arrays are stored in "thread_back_trace" variable.
  40. // Maybe to use thread local storage is better and should save memorys.
  41. #ifndef BASE_STACKTRACE_INSTRUMENT_INL_H_
  42. #define BASE_STACKTRACE_INSTRUMENT_INL_H_
  43. // Note: this file is included into stacktrace.cc more than once.
  44. // Anything that should only be defined once should be here:
  45. #include <execinfo.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include <sys/syscall.h>
  49. #include "gperftools/stacktrace.h"
  50. #define gettid() syscall(__NR_gettid)
  51. #ifndef __x86_64__
  52. #define MAX_THREAD (32768)
  53. #else
  54. #define MAX_THREAD (65536)
  55. #endif
  56. #define MAX_DEPTH (30)
  57. #define ATTRIBUTE_NOINSTRUMENT __attribute__ ((no_instrument_function))
  58. typedef struct {
  59. int stack_depth;
  60. void* frame[MAX_DEPTH];
  61. }BACK_TRACE;
  62. static BACK_TRACE thread_back_trace[MAX_THREAD];
  63. extern "C" {
  64. void __cyg_profile_func_enter(void *func_address,
  65. void *call_site) ATTRIBUTE_NOINSTRUMENT;
  66. void __cyg_profile_func_enter(void *func_address, void *call_site) {
  67. (void)func_address;
  68. BACK_TRACE* backtrace = thread_back_trace + gettid();
  69. int stack_depth = backtrace->stack_depth;
  70. backtrace->stack_depth = stack_depth + 1;
  71. if ( stack_depth >= MAX_DEPTH ) {
  72. return;
  73. }
  74. backtrace->frame[stack_depth] = call_site;
  75. }
  76. void __cyg_profile_func_exit(void *func_address,
  77. void *call_site) ATTRIBUTE_NOINSTRUMENT;
  78. void __cyg_profile_func_exit(void *func_address, void *call_site) {
  79. (void)func_address;
  80. (void)call_site;
  81. BACK_TRACE* backtrace = thread_back_trace + gettid();
  82. int stack_depth = backtrace->stack_depth;
  83. backtrace->stack_depth = stack_depth - 1;
  84. if ( stack_depth >= MAX_DEPTH ) {
  85. return;
  86. }
  87. backtrace->frame[stack_depth] = 0;
  88. }
  89. } // extern "C"
  90. static int cyg_backtrace(void **buffer, int size) {
  91. BACK_TRACE* backtrace = thread_back_trace + gettid();
  92. int stack_depth = backtrace->stack_depth;
  93. if ( stack_depth >= MAX_DEPTH ) {
  94. stack_depth = MAX_DEPTH;
  95. }
  96. int nSize = (size > stack_depth) ? stack_depth : size;
  97. for (int i = 0; i < nSize; i++) {
  98. buffer[i] = backtrace->frame[nSize - i - 1];
  99. }
  100. return nSize;
  101. }
  102. #endif // BASE_STACKTRACE_INSTRUMENT_INL_H_
  103. // Note: this part of the file is included several times.
  104. // Do not put globals below.
  105. // The following 4 functions are generated from the code below:
  106. // GetStack{Trace,Frames}()
  107. // GetStack{Trace,Frames}WithContext()
  108. //
  109. // These functions take the following args:
  110. // void** result: the stack-trace, as an array
  111. // int* sizes: the size of each stack frame, as an array
  112. // (GetStackFrames* only)
  113. // int max_depth: the size of the result (and sizes) array(s)
  114. // int skip_count: how many stack pointers to skip before storing in result
  115. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  116. static int GET_STACK_TRACE_OR_FRAMES {
  117. static const int kStackLength = 64;
  118. void * stack[kStackLength];
  119. int size;
  120. memset(stack, 0, sizeof(stack));
  121. size = cyg_backtrace(stack, kStackLength);
  122. skip_count += 2; // we want to skip the current and parent frame as well
  123. int result_count = size - skip_count;
  124. if (result_count < 0)
  125. result_count = 0;
  126. if (result_count > max_depth)
  127. result_count = max_depth;
  128. for (int i = 0; i < result_count; i++)
  129. result[i] = stack[i + skip_count];
  130. #if IS_STACK_FRAMES
  131. // No implementation for finding out the stack frame sizes yet.
  132. memset(sizes, 0, sizeof(*sizes) * result_count);
  133. #endif
  134. return result_count;
  135. }