glog
src
stacktrace_unwind-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 libgcc
33
34
#include <cstdlib>
// for NULL
35
#include <unwind.h>
// ABI defined unwinder
36
37
#include "
stacktrace.h
"
38
39
_START_GOOGLE_NAMESPACE_
40
41
typedef
struct
{
42
void
**
result
;
43
int
max_depth
;
44
int
skip_count
;
45
int
count
;
46
}
trace_arg_t
;
47
48
49
// Workaround for the malloc() in _Unwind_Backtrace() issue.
50
static
_Unwind_Reason_Code
nop_backtrace
(
struct
_Unwind_Context *
/*uc*/
,
void
*
/*opq*/
) {
51
return
_URC_NO_REASON;
52
}
53
54
55
// This code is not considered ready to run until
56
// static initializers run so that we are guaranteed
57
// that any malloc-related initialization is done.
58
static
bool
ready_to_run
=
false
;
59
class
StackTraceInit
{
60
public
:
61
StackTraceInit
() {
62
// Extra call to force initialization
63
_Unwind_Backtrace(
nop_backtrace
,
NULL
);
64
ready_to_run
=
true
;
65
}
66
};
67
68
static
StackTraceInit
module_initializer
;
// Force initialization
69
70
static
_Unwind_Reason_Code
GetOneFrame
(
struct
_Unwind_Context *uc,
void
*opq) {
71
trace_arg_t
*targ =
static_cast<
trace_arg_t
*
>
(opq);
72
73
if
(targ->
skip_count
> 0) {
74
targ->
skip_count
--;
75
}
else
{
76
targ->
result
[targ->
count
++] = (
void
*) _Unwind_GetIP(uc);
77
}
78
79
if
(targ->
count
== targ->
max_depth
) {
80
return
_URC_END_OF_STACK;
81
}
82
83
return
_URC_NO_REASON;
84
}
85
86
// If you change this function, also change GetStackFrames below.
87
int
GetStackTrace
(
void
** result,
int
max_depth,
int
skip_count) {
88
if
(!
ready_to_run
) {
89
return
0;
90
}
91
92
trace_arg_t
targ;
93
94
skip_count += 1;
// Do not include the "GetStackTrace" frame
95
96
targ.
result
= result;
97
targ.
max_depth
= max_depth;
98
targ.
skip_count
= skip_count;
99
targ.
count
= 0;
100
101
_Unwind_Backtrace(
GetOneFrame
, &targ);
102
103
return
targ.
count
;
104
}
105
106
_END_GOOGLE_NAMESPACE_
GetStackTrace
int GetStackTrace(void **result, int max_depth, int skip_count)
Definition:
stacktrace_unwind-inl.h:87
NULL
NULL
Definition:
test_security_zap.cpp:405
ready_to_run
static bool ready_to_run
Definition:
stacktrace_unwind-inl.h:58
trace_arg_t
Definition:
stacktrace_unwind-inl.h:41
trace_arg_t::result
void ** result
Definition:
stacktrace_unwind-inl.h:42
GetOneFrame
static _Unwind_Reason_Code GetOneFrame(struct _Unwind_Context *uc, void *opq)
Definition:
stacktrace_unwind-inl.h:70
StackTraceInit
Definition:
stacktrace_unwind-inl.h:59
nop_backtrace
static _Unwind_Reason_Code nop_backtrace(struct _Unwind_Context *, void *)
Definition:
stacktrace_unwind-inl.h:50
_START_GOOGLE_NAMESPACE_
Definition:
signalhandler.cc:51
module_initializer
static StackTraceInit module_initializer
Definition:
stacktrace_unwind-inl.h:68
stacktrace.h
StackTraceInit::StackTraceInit
StackTraceInit()
Definition:
stacktrace_unwind-inl.h:61
trace_arg_t::max_depth
int max_depth
Definition:
stacktrace_unwind-inl.h:43
trace_arg_t::skip_count
int skip_count
Definition:
stacktrace_unwind-inl.h:44
trace_arg_t::count
int count
Definition:
stacktrace_unwind-inl.h:45
libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58