stacktrace_libgcc-inl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2016, gperftools Contributors
  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. // This file implements backtrace capturing via libgcc's
  31. // _Unwind_Backtrace. This generally works almost always. It will fail
  32. // sometimes when we're trying to capture backtrace from signal
  33. // handler (i.e. in cpu profiler) while some C++ code is throwing
  34. // exception.
  35. #ifndef BASE_STACKTRACE_LIBGCC_INL_H_
  36. #define BASE_STACKTRACE_LIBGCC_INL_H_
  37. // Note: this file is included into stacktrace.cc more than once.
  38. // Anything that should only be defined once should be here:
  39. extern "C" {
  40. #include <assert.h>
  41. #include <string.h> // for memset()
  42. }
  43. #include <unwind.h>
  44. #include "gperftools/stacktrace.h"
  45. struct libgcc_backtrace_data {
  46. void **array;
  47. int skip;
  48. int pos;
  49. int limit;
  50. };
  51. static _Unwind_Reason_Code libgcc_backtrace_helper(struct _Unwind_Context *ctx,
  52. void *_data) {
  53. libgcc_backtrace_data *data =
  54. reinterpret_cast<libgcc_backtrace_data *>(_data);
  55. if (data->skip > 0) {
  56. data->skip--;
  57. return _URC_NO_REASON;
  58. }
  59. if (data->pos < data->limit) {
  60. void *ip = reinterpret_cast<void *>(_Unwind_GetIP(ctx));;
  61. data->array[data->pos++] = ip;
  62. }
  63. return _URC_NO_REASON;
  64. }
  65. #endif // BASE_STACKTRACE_LIBGCC_INL_H_
  66. // Note: this part of the file is included several times.
  67. // Do not put globals below.
  68. // The following 4 functions are generated from the code below:
  69. // GetStack{Trace,Frames}()
  70. // GetStack{Trace,Frames}WithContext()
  71. //
  72. // These functions take the following args:
  73. // void** result: the stack-trace, as an array
  74. // int* sizes: the size of each stack frame, as an array
  75. // (GetStackFrames* only)
  76. // int max_depth: the size of the result (and sizes) array(s)
  77. // int skip_count: how many stack pointers to skip before storing in result
  78. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  79. static int GET_STACK_TRACE_OR_FRAMES {
  80. libgcc_backtrace_data data;
  81. data.array = result;
  82. // we're also skipping current and parent's frame
  83. data.skip = skip_count + 2;
  84. data.pos = 0;
  85. data.limit = max_depth;
  86. _Unwind_Backtrace(libgcc_backtrace_helper, &data);
  87. if (data.pos > 1 && data.array[data.pos - 1] == NULL)
  88. --data.pos;
  89. #if IS_STACK_FRAMES
  90. // No implementation for finding out the stack frame sizes.
  91. memset(sizes, 0, sizeof(*sizes) * data.pos);
  92. #endif
  93. return data.pos;
  94. }