forked from ianlancetaylor/cgosymbolizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
traceback.c
90 lines (76 loc) · 1.86 KB
/
traceback.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build cgo
// +build linux
#include "config.h"
#include <stddef.h>
#include <stdint.h>
#include <unwind.h>
struct unwind_data {
uintptr_t* addrs;
int idx;
int max;
uintptr_t cfa;
};
static _Unwind_Reason_Code
unwind(struct _Unwind_Context* context, void* data)
{
struct unwind_data* ud = (struct unwind_data*)data;
uintptr_t cfa;
uintptr_t pc;
int ip_before_insn;
if (ud->idx >= ud->max) {
return _URC_END_OF_STACK;
}
if (ud->idx < 0) {
ud->idx++;
return _URC_NO_REASON;
}
cfa = _Unwind_GetCFA(context);
if (ud->cfa == 0) {
ud->cfa = (uintptr_t)(cfa);
} else {
// If we are no longer in a plausible stack range, give up.
// Default thread stack size on GNU/Linux is 2MB, allow 1MB.
// Remember we are on the system stack here.
if (cfa < ud->cfa || cfa >= ud->cfa + (1 << 10)) {
return _URC_END_OF_STACK;
}
}
pc = _Unwind_GetIPInfo(context, &ip_before_insn);
if (!ip_before_insn) {
--pc;
}
ud->addrs[ud->idx] = pc;
ud->idx++;
return _URC_NO_REASON;
}
struct cgoTracebackArg {
uintptr_t context;
uintptr_t sigContext;
uintptr_t* buf;
uintptr_t max;
};
// Gather addresses from the call stack.
void cgoTraceback(void* parg) {
struct cgoTracebackArg* arg = (struct cgoTracebackArg*)(parg);
struct unwind_data ud;
// We can only unwind the current stack.
if (arg->context != 0) {
arg->buf[0] = 0;
return;
}
// Skip this frame, the runtime/cgo function, and the sigreturn frame.
ud.idx = -3;
ud.addrs = arg->buf;
ud.max = arg->max;
ud.cfa = 0;
_Unwind_Backtrace(unwind, (void*)&ud);
// The list of addresses terminates at a 0, so make sure there is one.
if (ud.idx < 0) {
arg->buf[0] = 0;
} else if (ud.idx < arg->max) {
arg->buf[ud.idx] = 0;
}
}