-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracer.go
164 lines (145 loc) · 3.61 KB
/
tracer.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
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
package main
import (
"bufio"
"context"
"fmt"
"os"
"os/signal"
"regexp"
"strings"
"github.com/jschwinger233/gofuncgraph/elf"
"github.com/jschwinger233/gofuncgraph/internal/bpf"
"github.com/jschwinger233/gofuncgraph/internal/eventmanager"
"github.com/jschwinger233/gofuncgraph/internal/uprobe"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var (
OffsetPattern *regexp.Regexp
)
func init() {
OffsetPattern = regexp.MustCompile(`\+\d+$`)
}
type Tracer struct {
bin string
elf *elf.ELF
excludeVendor bool
uprobeWildcards []string
args []string
bpf *bpf.BPF
}
func NewTracer(bin string, excludeVendor bool, uprobeWildcards, args []string) (_ *Tracer, err error) {
elf, err := elf.New(bin)
if err != nil {
return
}
return &Tracer{
bin: bin,
elf: elf,
excludeVendor: excludeVendor,
uprobeWildcards: uprobeWildcards,
args: args,
bpf: bpf.New(),
}, nil
}
func (t *Tracer) ParseArgs(inputs []string) (in []string, fetch map[string]map[string]string, err error) {
fetch = map[string]map[string]string{}
for _, input := range inputs {
if input[len(input)-1] == ')' {
stack := []byte{')'}
for i := len(input) - 2; i >= 0; i-- {
if input[i] == ')' {
stack = append(stack, ')')
} else if input[i] == '(' {
if len(stack) > 0 && stack[len(stack)-1] == ')' {
stack = stack[:len(stack)-1]
} else {
err = fmt.Errorf("imbalanced parenthese: %s", input)
return
}
}
if len(stack) == 0 {
funcname := input[:i]
fetch[funcname] = map[string]string{}
for _, part := range strings.Split(input[i+1:len(input)-1], ",") {
varState := strings.Split(part, "=")
if len(varState) != 2 {
err = fmt.Errorf("invalid variable statement: %s", varState)
return
}
fetch[funcname][strings.TrimSpace(varState[0])] = strings.TrimSpace(varState[1])
}
input = input[:i]
break
}
}
if len(stack) > 0 {
err = fmt.Errorf("imbalanced parenthese: %s", input)
return
}
}
in = append(in, input)
}
return
}
func (t *Tracer) Start() (err error) {
in, fetch, err := t.ParseArgs(t.args)
if err != nil {
return
}
uprobes, err := uprobe.Parse(t.elf, &uprobe.ParseOptions{
ExcludeVendor: t.excludeVendor,
UprobeWildcards: t.uprobeWildcards,
OutputWildcards: in,
Fetch: fetch,
})
if err != nil {
return
}
requireConfirm:
fmt.Fprintf(os.Stdout, "found %d uprobes, large number of uprobes (>1000) need long time for attaching and detaching, continue? [Y/n]\n", len(uprobes))
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return errors.WithStack(err)
}
switch strings.TrimSpace(input) {
case "n", "N":
return
case "y", "Y":
break
default:
goto requireConfirm
}
goidOffset, err := t.elf.FindGoidOffset()
if err != nil {
return
}
gOffset, err := t.elf.FindGOffset()
if err != nil {
return
}
log.Debugf("offset of goid from g is %d, offset of g from fs is -0x%x\n", goidOffset, -gOffset)
if err = t.bpf.Load(uprobes, bpf.LoadOptions{
GoidOffset: goidOffset,
GOffset: gOffset,
}); err != nil {
return
}
if err = t.bpf.Attach(t.bin, uprobes); err != nil {
return
}
defer t.bpf.Detach()
log.Info("start tracing\n")
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
eventManager, err := eventmanager.New(uprobes, t.elf, t.bpf.PollArg(ctx))
if err != nil {
return
}
for event := range t.bpf.PollEvents(ctx) {
if err = eventManager.Handle(event); err != nil {
return
}
}
return eventManager.PrintRemaining()
}