forked from null-luo/btrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (104 loc) · 3.32 KB
/
main.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
package main
import (
"bytes"
"encoding/binary"
"errors"
"log"
"os"
"os/signal"
"syscall"
"fmt"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"
"github.com/cilium/ebpf/rlimit"
//"github.com/davecgh/go-spew/spew"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -go-package main --target=arm64 -type event bpf binder_transaction.byte.c -- -I./headers
func main() {
// Name of the kernel function to trace.
fn := "binder_transaction"
// Subscribe to signals for terminating the program.
stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)
if err := LoadMethodMappings(methodsJSON); err != nil {
log.Fatal(err)
}
if err := LoadPackageMappings(); err != nil {
log.Fatal(err)
}
// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal(err)
}
// Load pre-compiled programs and maps into the kernel.
objs := bpfObjects{}
if err := loadBpfObjects(&objs, nil); err != nil {
log.Fatalf("loading objects: %v", err)
}
defer objs.Close()
// Open a Kprobe at the entry point of the kernel function and attach the
// pre-compiled program. Each time the kernel function enters, the program
// will emit an event containing pid and command of the execved task.
kp, err := link.Kprobe(fn, objs.KprobeBinderTransaction, nil)
if err != nil {
log.Fatalf("opening kprobe: %s", err)
}
defer kp.Close()
// Open a ringbuf reader from userspace RINGBUF map described in the
// eBPF C program.
rd, err := ringbuf.NewReader(objs.Events)
if err != nil {
log.Fatalf("opening ringbuf reader: %s", err)
}
defer rd.Close()
// Close the reader when the process receives a signal, which will exit
// the read loop.
go func() {
<-stopper
if err := rd.Close(); err != nil {
log.Fatalf("closing ringbuf reader: %s", err)
}
}()
log.Println("Waiting for events..")
// bpfEvent is generated by bpf2go.
var event bpfEvent
for {
record, err := rd.Read()
if err != nil {
if errors.Is(err, ringbuf.ErrClosed) {
log.Println("Received signal, exiting..")
return
}
log.Printf("reading from reader: %s", err)
continue
}
// Parse the ringbuf event entry into a bpfEvent structure.
if err := binary.Read(bytes.NewBuffer(record.RawSample), binary.LittleEndian, &event); err != nil {
log.Printf("parsing ringbuf event: %s", err)
continue
}
//log.Printf("(pid:%d, comm:%s) -> (code:%d, flags:%d, data_size:%d, data:)\n", event.Pid, unix.ByteSliceToString(event.Comm[:]), event.Code, event.Flags, event.DataSize)
if event.DataSize <= 0 {
continue
}
parcelData := event.Data[:event.DataSize-1]
//spew.Dump(parcelData);
interfaceToken, err := ExtractInterfaceName(parcelData)
if err != nil {
fmt.Println("Error parsing parcel:", err)
continue
}
//fmt.Println("Interface Token:", interfaceToken)
methodName, err := GetMethodName(interfaceToken, int(event.Code))
if err != nil {
//fmt.Println("Error:", err)
methodName = fmt.Sprintf("%d", event.Code)
}
packageName, err := GetPackageNameByUid(int(event.Uid))
if err != nil {
//fmt.Println("Error:", err)
packageName = fmt.Sprintf("%d", event.Uid)
}
log.Printf("(pid:%d, uid:%d, package:%s) -> (interface:%s, method:%s)\n", event.Pid, event.Uid, packageName, interfaceToken, methodName)
}
}