stacktrace_libunwind-inl.h
Go to the documentation of this 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 libunwind
33 
34 #include "utilities.h"
35 
36 extern "C" {
37 #define UNW_LOCAL_ONLY
38 #include <libunwind.h>
39 }
40 #include <glog/raw_logging.h>
41 #include "stacktrace.h"
42 
44 
45 // Sometimes, we can try to get a stack trace from within a stack
46 // trace, because libunwind can call mmap (maybe indirectly via an
47 // internal mmap based memory allocator), and that mmap gets trapped
48 // and causes a stack-trace request. If were to try to honor that
49 // recursive request, we'd end up with infinite recursion or deadlock.
50 // Luckily, it's safe to ignore those subsequent traces. In such
51 // cases, we return 0 to indicate the situation.
52 // We can use the GCC __thread syntax here since libunwind is not supported on
53 // Windows.
54 static __thread bool g_tl_entered; // Initialized to false.
55 
56 // If you change this function, also change GetStackFrames below.
57 int GetStackTrace(void** result, int max_depth, int skip_count) {
58  void *ip;
59  int n = 0;
60  unw_cursor_t cursor;
61  unw_context_t uc;
62 
63  if (g_tl_entered) {
64  return 0;
65  }
66  g_tl_entered = true;
67 
68  unw_getcontext(&uc);
69  RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
70  skip_count++; // Do not include the "GetStackTrace" frame
71 
72  while (n < max_depth) {
73  int ret =
74  unw_get_reg(&cursor, UNW_REG_IP, reinterpret_cast<unw_word_t *>(&ip));
75  if (ret < 0) {
76  break;
77  }
78  if (skip_count > 0) {
79  skip_count--;
80  } else {
81  result[n++] = ip;
82  }
83  ret = unw_step(&cursor);
84  if (ret <= 0) {
85  break;
86  }
87  }
88 
89  g_tl_entered = false;
90  return n;
91 }
92 
93 _END_GOOGLE_NAMESPACE_
GetStackTrace
int GetStackTrace(void **result, int max_depth, int skip_count)
Definition: stacktrace_libunwind-inl.h:57
utilities.h
n
GLdouble n
Definition: glcorearb.h:4153
_START_GOOGLE_NAMESPACE_
Definition: signalhandler.cc:51
stacktrace.h
g_tl_entered
static _START_GOOGLE_NAMESPACE_ __thread bool g_tl_entered
Definition: stacktrace_libunwind-inl.h:54


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58