stacktrace_generic-inl.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Portable implementation - just use glibc
  34. //
  35. // Note: The glibc implementation may cause a call to malloc.
  36. // This can cause a deadlock in HeapProfiler.
  37. #ifndef BASE_STACKTRACE_GENERIC_INL_H_
  38. #define BASE_STACKTRACE_GENERIC_INL_H_
  39. // Note: this file is included into stacktrace.cc more than once.
  40. // Anything that should only be defined once should be here:
  41. #include <execinfo.h>
  42. #include <string.h>
  43. #include "gperftools/stacktrace.h"
  44. #endif // BASE_STACKTRACE_GENERIC_INL_H_
  45. // Note: this part of the file is included several times.
  46. // Do not put globals below.
  47. // The following 4 functions are generated from the code below:
  48. // GetStack{Trace,Frames}()
  49. // GetStack{Trace,Frames}WithContext()
  50. //
  51. // These functions take the following args:
  52. // void** result: the stack-trace, as an array
  53. // int* sizes: the size of each stack frame, as an array
  54. // (GetStackFrames* only)
  55. // int max_depth: the size of the result (and sizes) array(s)
  56. // int skip_count: how many stack pointers to skip before storing in result
  57. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  58. static int GET_STACK_TRACE_OR_FRAMES {
  59. static const int kStackLength = 64;
  60. void * stack[kStackLength];
  61. int size;
  62. size = backtrace(stack, kStackLength);
  63. skip_count += 2; // we want to skip the current and it's parent frame as well
  64. int result_count = size - skip_count;
  65. if (result_count < 0)
  66. result_count = 0;
  67. if (result_count > max_depth)
  68. result_count = max_depth;
  69. for (int i = 0; i < result_count; i++)
  70. result[i] = stack[i + skip_count];
  71. #if IS_STACK_FRAMES
  72. // No implementation for finding out the stack frame sizes yet.
  73. memset(sizes, 0, sizeof(*sizes) * result_count);
  74. #endif
  75. return result_count;
  76. }