PageRenderTime 519ms CodeModel.GetById 48ms RepoModel.GetById 10ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ Header | 87 lines | 37 code | 10 blank | 40 comment | 6 complexity | 35ae3ad7a6d5db570d9b8c3a2e5fbd3b MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  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 libunwind
  33. #include "utilities.h"
  34. extern "C" {
  35. #define UNW_LOCAL_ONLY
  36. #include <libunwind.h>
  37. }
  38. #include "glog/raw_logging.h"
  39. #include "stacktrace.h"
  40. _START_GOOGLE_NAMESPACE_
  41. // Sometimes, we can try to get a stack trace from within a stack
  42. // trace, because libunwind can call mmap (maybe indirectly via an
  43. // internal mmap based memory allocator), and that mmap gets trapped
  44. // and causes a stack-trace request. If were to try to honor that
  45. // recursive request, we'd end up with infinite recursion or deadlock.
  46. // Luckily, it's safe to ignore those subsequent traces. In such
  47. // cases, we return 0 to indicate the situation.
  48. static bool g_now_entering = false;
  49. // If you change this function, also change GetStackFrames below.
  50. int GetStackTrace(void** result, int max_depth, int skip_count) {
  51. void *ip;
  52. int n = 0;
  53. unw_cursor_t cursor;
  54. unw_context_t uc;
  55. if (sync_val_compare_and_swap(&g_now_entering, false, true)) {
  56. return 0;
  57. }
  58. unw_getcontext(&uc);
  59. RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
  60. skip_count++; // Do not include the "GetStackTrace" frame
  61. while (n < max_depth) {
  62. int ret = unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip);
  63. if (ret < 0)
  64. break;
  65. if (skip_count > 0) {
  66. skip_count--;
  67. } else {
  68. result[n++] = ip;
  69. }
  70. ret = unw_step(&cursor);
  71. if (ret <= 0)
  72. break;
  73. }
  74. g_now_entering = false;
  75. return n;
  76. }
  77. _END_GOOGLE_NAMESPACE_