-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcallback.go
More file actions
209 lines (169 loc) · 5.57 KB
/
callback.go
File metadata and controls
209 lines (169 loc) · 5.57 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package callback
import (
"fmt"
"runtime"
"runtime/debug"
"sync"
"unsafe"
"github.com/Rookout/GoSDK/pkg/augs"
"github.com/Rookout/GoSDK/pkg/locations_set"
"github.com/Rookout/GoSDK/pkg/logger"
"github.com/Rookout/GoSDK/pkg/rookoutErrors"
"github.com/Rookout/GoSDK/pkg/services/collection"
"github.com/Rookout/GoSDK/pkg/services/collection/go_id"
"github.com/Rookout/GoSDK/pkg/services/collection/registers"
"github.com/Rookout/GoSDK/pkg/services/go_runtime"
"github.com/Rookout/GoSDK/pkg/services/instrumentation/binary_info"
"github.com/Rookout/GoSDK/pkg/utils"
)
func getContext() uintptr
var BinaryInfo *binary_info.BinaryInfo
var locationsSet *locations_set.LocationsSet
var triggerChan chan bool
func SetBinaryInfo(binaryInfoIn *binary_info.BinaryInfo) {
BinaryInfo = binaryInfoIn
}
func SetLocationsSet(locationsSetIn *locations_set.LocationsSet) {
locationsSet = locationsSetIn
}
func SetTriggerChan(triggerChanIn chan bool) {
triggerChan = triggerChanIn
}
type BreakpointInfo struct {
Stacktrace []collection.Stackframe
regs registers.Registers
}
func collectStacktrace(regs *registers.OnStackRegisters, g go_runtime.GPtr, maxStacktrace int) []collection.Stackframe {
if maxStacktrace == 0 {
maxStacktrace = 1
}
pcs := make([]uintptr, maxStacktrace)
frameCount := go_runtime.Callers(uintptr(regs.PC()), uintptr(regs.SP()), g, pcs)
pcs = pcs[:frameCount]
stacktrace := make([]collection.Stackframe, 0, frameCount)
frames := runtime.CallersFrames(pcs)
more := true
frame := runtime.Frame{}
for i := 0; i < frameCount; i++ {
if !more {
logger.Logger().Warningf("Expected more frames but more is false: %d/%d\n", i, frameCount)
break
}
frame, more = frames.Next()
stacktrace = append(stacktrace,
collection.Stackframe{
File: frame.File,
Line: frame.Line,
Function: frame.Func.Name(),
})
}
return stacktrace
}
func printBytesAt(sp, count uint64, prefixes []string) uint64 {
for i := uint64(0); i < count; i++ {
//goland:noinspection GoVetUnsafePointer
stackValue := *(*uint64)(unsafe.Pointer(uintptr(sp)))
fmt.Printf("0x%016x:\t0x%016x (%s) \n", sp, stackValue, prefixes[i])
sp = sp - 8
}
return sp
}
func printStack(stackRegs registers.OnStackRegisters) {
stackPtr := stackRegs.SP()
fmt.Printf("BP: 0x%016x\n", stackRegs.BP())
fmt.Printf("SP: 0x%016x\n", stackRegs.SP())
fmt.Println("Native:")
stackPtr = printBytesAt(stackPtr, 4, []string{"idk", "flags", "rdi", "rdx"})
stackPtr -= 240
stackPtr = printBytesAt(stackPtr, 11, []string{"rbx", "rax", "rcx", "rsi", "r8", "r9", "r10", "r11", "rbp", "rdi", "retval"})
fmt.Println("Golang:")
if runtime.Version() == "go1.16" {
stackPtr = printBytesAt(stackPtr, 1, []string{"idk", "idk"})
} else {
stackPtr = printBytesAt(stackPtr, 2, []string{"idk", "idk"})
}
stackPtr = printBytesAt(stackPtr, 12, []string{"rsp", "rbp", "tls", "rip", "r11", "r10", "r9", "r8", "rsi", "rcx", "rax", "rbx"})
stackPtr = printBytesAt(stackPtr, 2, []string{"rdx", "rdi"})
}
//go:linkname systemstack runtime.systemstack
func systemstack(func())
//go:nosplit
func Callback() {
context := getContext()
g := go_runtime.Getg()
goCollect(context, g)
}
//go:nosplit
func goCollect(context uintptr, g go_runtime.GPtr) {
var waitChan chan struct{}
triggerChan <- true
systemstack(func() {
waitChan = make(chan struct{})
go func() {
defer func() {
waitChan <- struct{}{}
triggerChan <- false
if v := recover(); v != nil {
if utils.OnPanicFunc != nil {
utils.OnPanicFunc(rookoutErrors.NewUnknownError(v))
}
return
}
}()
debug.SetPanicOnFault(true)
collectBreakpoint(context, g)
}()
})
<-waitChan
}
func collectBreakpoint(context uintptr, g go_runtime.GPtr) {
regs := registers.NewOnStackRegisters(context)
bpInstance, ok := locationsSet.FindBreakpointByAddr(regs.PC())
if !ok {
file, line, function := BinaryInfo.PCToLine(regs.PC())
var functionName string
if function != nil {
functionName = function.Name
}
logger.Logger().Errorf("Breakpoint triggered on unknown address, 0x%x (%s:%d - %s)", regs.PC(), file, line, functionName)
return
}
goid := go_id.GetGoID(g)
stacktrace := collectStacktrace(regs, g, bpInstance.Breakpoint.Stacktrace)
stacktrace[0].Line = bpInstance.Breakpoint.Line
bpInfo := &BreakpointInfo{
Stacktrace: stacktrace,
regs: regs,
}
reportBreakpoint(bpInstance, bpInfo, goid)
}
func reportBreakpoint(bpInstance *augs.BreakpointInstance, bpInfo *BreakpointInfo, goid int) {
bp := bpInstance.Breakpoint
locations, exists := locationsSet.FindLocationsByBreakpointName(bp.Name)
if !exists {
logger.Logger().Errorf("Breakpoint %s (on %s:%d - 0x%x) triggered but the breakpoint doesn't exist.", bp.Name, bp.File, bp.Line, bpInfo.regs.PC())
return
}
logger.Logger().Debugf("Triggered breakpoint %s:%d (0x%x)", bp.File, bp.Line, bpInstance.Addr)
wg := sync.WaitGroup{}
wg.Add(len(locations))
for i := range locations {
utils.CreateGoroutine(func(i int) func() {
return func() {
defer func() {
logger.Logger().Debugf("Finished collecting breakpoint %s:%d (0x%x), audID: %d", bp.File, bp.Line, bpInstance.Addr, locations[i].GetAugID())
if r := recover(); r != nil {
locations[i].SetError(rookoutErrors.NewUnknownError(r))
}
}()
defer wg.Done()
collectionService, err := collection.NewCollectionService(bpInfo.regs, BinaryInfo.PointerSize, bpInfo.Stacktrace, bpInstance.VariableLocators, goid)
if err != nil {
logger.Logger().WithError(err).Errorf("failed to report breakpoint info")
}
locations[i].GetAug().Execute(collectionService)
}
}(i))
}
wg.Wait()
}