/thirdparty/breakpad/third_party/glog/src/stacktrace_x86_64-inl.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 105 lines · 48 code · 19 blank · 38 comment · 5 complexity · c78826647455a50be83a37fd4ff680da MD5 · raw file

  1. // Copyright (c) 2005 - 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: Arun Sharma
  31. //
  32. // Produce stack trace using libgcc
  33. extern "C" {
  34. #include <stdlib.h> // for NULL
  35. #include <unwind.h> // ABI defined unwinder
  36. }
  37. #include "stacktrace.h"
  38. _START_GOOGLE_NAMESPACE_
  39. typedef struct {
  40. void **result;
  41. int max_depth;
  42. int skip_count;
  43. int count;
  44. } trace_arg_t;
  45. // Workaround for the malloc() in _Unwind_Backtrace() issue.
  46. static _Unwind_Reason_Code nop_backtrace(struct _Unwind_Context *uc, void *opq) {
  47. return _URC_NO_REASON;
  48. }
  49. // This code is not considered ready to run until
  50. // static initializers run so that we are guaranteed
  51. // that any malloc-related initialization is done.
  52. static bool ready_to_run = false;
  53. class StackTraceInit {
  54. public:
  55. StackTraceInit() {
  56. // Extra call to force initialization
  57. _Unwind_Backtrace(nop_backtrace, NULL);
  58. ready_to_run = true;
  59. }
  60. };
  61. static StackTraceInit module_initializer; // Force initialization
  62. static _Unwind_Reason_Code GetOneFrame(struct _Unwind_Context *uc, void *opq) {
  63. trace_arg_t *targ = (trace_arg_t *) opq;
  64. if (targ->skip_count > 0) {
  65. targ->skip_count--;
  66. } else {
  67. targ->result[targ->count++] = (void *) _Unwind_GetIP(uc);
  68. }
  69. if (targ->count == targ->max_depth)
  70. return _URC_END_OF_STACK;
  71. return _URC_NO_REASON;
  72. }
  73. // If you change this function, also change GetStackFrames below.
  74. int GetStackTrace(void** result, int max_depth, int skip_count) {
  75. if (!ready_to_run)
  76. return 0;
  77. trace_arg_t targ;
  78. skip_count += 1; // Do not include the "GetStackTrace" frame
  79. targ.result = result;
  80. targ.max_depth = max_depth;
  81. targ.skip_count = skip_count;
  82. targ.count = 0;
  83. _Unwind_Backtrace(GetOneFrame, &targ);
  84. return targ.count;
  85. }
  86. _END_GOOGLE_NAMESPACE_