-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt.go
130 lines (115 loc) · 2.64 KB
/
bt.go
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
package randtest
import (
"reflect"
"runtime"
"unsafe"
)
type (
intArgs uintptr
int31Args uintptr
int31nArgs struct {
_ uintptr
n int32
}
int63Args uintptr
int63nArgs struct {
_ uintptr
n int64
}
intnArgs struct {
_ uintptr
n int
}
uint32Args uintptr
uint64Args uintptr
float32Args uintptr
float64Args uintptr
shuffleArgs struct {
_ uintptr
n int
swap func(int, int)
}
permArgs struct {
_ uintptr
n int
}
)
type argtrace struct {
Int *intArgs
Int31 *int31Args
Int31n *int31nArgs
Int63 *int63Args
Int63n *int63nArgs
Intn *intnArgs
Uint32 *uint32Args
Uint64 *uint64Args
Float32 *float32Args
Float64 *float64Args
Shuffle *shuffleArgs
Perm *permArgs
}
func getbp() uint64
func (s *Source) l() *argtrace {
bp := getbp()
pc := make([]uintptr, 10)
npc := runtime.Callers(1, pc)
pc = pc[:npc]
frameIter := runtime.CallersFrames(pc)
var frames []runtime.Frame
for more := true; more; {
var frame runtime.Frame
frame, more = frameIter.Next()
frames = append(frames, frame)
}
a := new(argtrace)
a.backtrace(uintptr(bp), frames)
return a
}
func (a *argtrace) backtrace(lbp uintptr, frames []runtime.Frame) {
if len(frames) == 0 {
return
}
funcname := frames[0].Function
argp := lbp + 16
switch funcname {
case "math/rand.(*Rand).Int":
a.Int = (*intArgs)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Intn":
a.Intn = (*intnArgs)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Int31":
a.Int31 = (*int31Args)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Int31n":
a.Int31n = (*int31nArgs)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Int63":
a.Int63 = (*int63Args)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Int63n":
a.Int63n = (*int63nArgs)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Uint32":
a.Uint32 = (*uint32Args)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Uint64":
a.Uint64 = (*uint64Args)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Float32":
a.Float32 = (*float32Args)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Float64":
a.Float64 = (*float64Args)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Perm":
a.Perm = (*permArgs)(unsafe.Pointer(argp))
case "math/rand.(*Rand).Shuffle":
a.Shuffle = (*shuffleArgs)(unsafe.Pointer(argp))
}
bp, ok := deref(lbp)
if !ok {
return
}
a.backtrace(bp, frames[1:])
}
func deref(p uintptr) (uintptr, bool) {
// `*(*unsafe.Pointer)(unsafe.Pointer(p))` will fault on darwin environment.
defer func() { recover() }()
v := reflect.NewAt(reflect.TypeOf((*unsafe.Pointer)(nil)), unsafe.Pointer(p))
vv := reflect.Indirect(v)
if v == vv {
return 0, false
}
return uintptr(vv.Pointer()), true
}