Skip to content

Commit 9f242e2

Browse files
committed
tools: reuse codes from common package
1 parent 8e39c09 commit 9f242e2

File tree

46 files changed

+174
-310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+174
-310
lines changed

common/net.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const (
1111

1212
SOCK_STREAM = 1
1313
SOCK_DGRAM = 2
14+
15+
IPPROTO_TCP = 6
16+
IPPROTO_UDP = 17
1417
)
1518

1619
type Uint128 [16]byte

tools/bashreadline/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.18
55
require (
66
github.com/aquasecurity/libbpfgo v0.4.4-libbpf-1.0.1
77
github.com/aquasecurity/libbpfgo/helpers v0.4.4
8+
github.com/mozillazg/libbpfgo-tools/common v0.0.0
89
github.com/spf13/pflag v1.0.5
910
)
1011

tools/bashreadline/main.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ import (
1616

1717
bpf "github.com/aquasecurity/libbpfgo"
1818
"github.com/aquasecurity/libbpfgo/helpers"
19+
"github.com/mozillazg/libbpfgo-tools/common"
1920
flag "github.com/spf13/pflag"
2021
)
2122

23+
const MAX_LINE_SIZE = 80
24+
25+
type StrT struct {
26+
Pid uint32
27+
Str [MAX_LINE_SIZE]byte
28+
}
29+
2230
type Options struct {
2331
bpfObjPath string
2432
verbose bool
@@ -68,20 +76,26 @@ func findReadlineSo() string {
6876
}
6977

7078
func printEvent(data []byte) {
71-
var pid uint32
72-
if err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &pid); err != nil {
79+
var e StrT
80+
if err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &e); err != nil {
7381
log.Fatalf("read data failed: %s\n%v", err, data)
7482
}
75-
str := data[4:]
76-
str = str[:bytes.IndexByte(str, 0)]
77-
7883
ts := time.Now().Format("15:04:05")
79-
fmt.Printf("%-9s %-7d %s\n", ts, pid, str)
84+
fmt.Printf("%-9s %-7d %s\n", ts, e.Pid, common.GoPath(e.Str[:]))
8085
}
8186

8287
func initGlobalVars(bpfModule *bpf.Module) {
8388
}
8489

90+
func loadBPFObj(bpfModule *bpf.Module) {
91+
if err := bpfModule.BPFLoadObject(); err != nil {
92+
log.Fatalln(err)
93+
}
94+
}
95+
96+
func applyFilters(bpfModule *bpf.Module) {
97+
}
98+
8599
func attachPrograms(bpfModule *bpf.Module) {
86100
readlineSoPath := opts.shared
87101
if readlineSoPath == "" {
@@ -111,9 +125,8 @@ func main() {
111125
defer bpfModule.Close()
112126

113127
initGlobalVars(bpfModule)
114-
if err := bpfModule.BPFLoadObject(); err != nil {
115-
log.Fatalln(err)
116-
}
128+
loadBPFObj(bpfModule)
129+
applyFilters(bpfModule)
117130
attachPrograms(bpfModule)
118131

119132
eventsChannel := make(chan []byte)

tools/bindsnoop/go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module github.com/mozillazg/libbpfgo-tools/tools/bindsnoop
33
go 1.18
44

55
require (
6-
github.com/aquasecurity/libbpfgo v0.4.3-libbpf-1.0.1 // indirect
7-
github.com/spf13/pflag v1.0.5 // indirect
6+
github.com/aquasecurity/libbpfgo v0.4.4-libbpf-1.0.1
7+
github.com/mozillazg/libbpfgo-tools/common v0.0.0
8+
github.com/spf13/pflag v1.0.5
89
)
10+
11+
replace github.com/mozillazg/libbpfgo-tools/common v0.0.0 => ../../common

tools/bindsnoop/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/aquasecurity/libbpfgo v0.4.3-libbpf-1.0.1 h1:xDHt+EJM/EJt9BXS2YheZNuynT/QNdraL/LHSSpdOxg=
2-
github.com/aquasecurity/libbpfgo v0.4.3-libbpf-1.0.1/go.mod h1:v+Nk+v6BtHLfdT4kVdsp+fYt4AeUa3cIG2P0y+nBuuY=
1+
github.com/aquasecurity/libbpfgo v0.4.4-libbpf-1.0.1 h1:cjdRq/YhQ5ZVU0jm6H3VXVcHgMzAAslrlexPq8acgSk=
2+
github.com/aquasecurity/libbpfgo v0.4.4-libbpf-1.0.1/go.mod h1:v+Nk+v6BtHLfdT4kVdsp+fYt4AeUa3cIG2P0y+nBuuY=
33
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
44
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

tools/bindsnoop/main.go

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,21 @@ import (
66
"encoding/binary"
77
"fmt"
88
"log"
9-
"net/netip"
109
"os"
1110
"os/signal"
1211
"syscall"
1312
"time"
1413
"unsafe"
1514

1615
bpf "github.com/aquasecurity/libbpfgo"
16+
"github.com/mozillazg/libbpfgo-tools/common"
1717
flag "github.com/spf13/pflag"
1818
)
1919

20-
const (
21-
IPProtoTCP uint16 = 6
22-
IPProtoUDP uint16 = 17
23-
)
24-
25-
type uint128 [16]byte
20+
const TASK_COMM_LEN = 16
2621

2722
type BindEvent struct {
28-
Addr uint128
23+
Addr common.Uint128
2924
TsUs uint64
3025
Pid uint32
3126
BoundDevIf uint32
@@ -34,11 +29,7 @@ type BindEvent struct {
3429
Proto uint16
3530
Opts uint8
3631
Ver uint8
37-
Task [16]byte
38-
}
39-
40-
func (e BindEvent) TaskString() string {
41-
return string(bytes.TrimRight(e.Task[:], "\x00"))
32+
Task [TASK_COMM_LEN]byte
4233
}
4334

4435
type Options struct {
@@ -86,22 +77,10 @@ func initGlobalVariable(bpfModule *bpf.Module, name string, value interface{}) {
8677
}
8778
}
8879

89-
func getCgroupDirFD(cgroupV2DirPath string) (int, error) {
90-
const (
91-
O_DIRECTORY int = 0200000
92-
O_RDONLY int = 00
93-
)
94-
fd, err := syscall.Open(cgroupV2DirPath, O_DIRECTORY|O_RDONLY, 0)
95-
if fd < 0 {
96-
return 0, fmt.Errorf("failed to open cgroupv2 directory path %s: %w", cgroupV2DirPath, err)
97-
}
98-
return fd, nil
99-
}
100-
10180
func initFilters(bpfModule *bpf.Module) {
10281
if opts.cgroup != "" {
10382
idx := 0
104-
cgroupFd, err := getCgroupDirFD(opts.cgroup)
83+
cgroupFd, err := common.GetCgroupDirFD(opts.cgroup)
10584
if err != nil {
10685
log.Fatalln(err)
10786
}
@@ -135,10 +114,10 @@ func formatEvent(event BindEvent) {
135114
fmt.Printf("%8s ", time.Now().Format("15:04:05"))
136115
}
137116
switch event.Proto {
138-
case IPProtoTCP:
117+
case common.IPPROTO_TCP:
139118
proto = "TCP"
140119
break
141-
case IPProtoUDP:
120+
case common.IPPROTO_UDP:
142121
proto = "UDP"
143122
default:
144123
proto = "UNK"
@@ -150,16 +129,12 @@ func formatEvent(event BindEvent) {
150129
}
151130
switch event.Ver {
152131
case 4:
153-
v := [4]byte{}
154-
for i := 0; i < 4; i++ {
155-
v[i] = event.Addr[i]
156-
}
157-
addr = netip.AddrFrom4(v).String()
132+
addr = common.AddrFrom16(common.AF_INET, event.Addr).String()
158133
default:
159-
addr = netip.AddrFrom16(event.Addr).String()
134+
addr = common.AddrFrom16(common.AF_INET6, event.Addr).String()
160135
}
161136
fmt.Printf("%-7d %-16s %-3d %-5s %-5s %-4d %-5d %-48s\n",
162-
event.Pid, event.TaskString(), event.Ret, proto, bindOpts, event.BoundDevIf, event.Port, addr)
137+
event.Pid, common.GoString(event.Task[:]), event.Ret, proto, bindOpts, event.BoundDevIf, event.Port, addr)
163138
}
164139

165140
func main() {

tools/biolatency/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ type Options struct {
4141
times uint64
4242
}
4343

44-
const maxCpuNr = 128
45-
4644
var opts = Options{
4745
bpfObjPath: "biolatency.bpf.o",
4846
verbose: false,

tools/biopattern/main.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ type Counter struct {
2525
Random uint32
2626
}
2727

28-
type Hist struct {
29-
Slots [27]uint32
30-
}
31-
32-
type HistKey struct {
33-
CmdFlags uint32
34-
Dev uint32
35-
}
36-
3728
type Options struct {
3829
bpfObjPath string
3930
verbose bool
@@ -43,8 +34,6 @@ type Options struct {
4334
times uint64
4435
}
4536

46-
const maxCpuNr = 128
47-
4837
var opts = Options{
4938
bpfObjPath: "biopattern.bpf.o",
5039
verbose: false,

tools/biosnoop/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ type Event struct {
3030
Dev uint32
3131
}
3232

33-
func (e Event) CommString() string {
34-
flag.Usage()
35-
return string(bytes.TrimRight(e.Comm[:], "\x00"))
36-
}
37-
3833
type Options struct {
3934
bpfObjPath string
4035
verbose bool
@@ -204,7 +199,7 @@ func printEvent(data []byte, partitions common.Partitions) {
204199
}
205200
fmt.Printf("%-11.6f %-14.14s %-7d %-7s %-4s %-10d %-7d ",
206201
float64(e.Ts-startTs)/1000000000.0,
207-
e.CommString(), e.Pid, name, rwbs, e.Sector, e.Len)
202+
common.GoString(e.Comm[:]), e.Pid, name, rwbs, e.Sector, e.Len)
208203
if opts.queued {
209204
fmt.Printf("%7.3f ", float64(e.Qdelta)/1000000.0)
210205
}

tools/bitesize/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
const (
2222
TASK_COMM_LEN = 16
23-
DISK_NAME_LEN = 32
2423
MAX_SLOTS = 20
2524
)
2625

@@ -32,10 +31,6 @@ type HistKey struct {
3231
Comm [TASK_COMM_LEN]byte
3332
}
3433

35-
func (k HistKey) CommString() string {
36-
return string(bytes.TrimRight(k.Comm[:], "\x00"))
37-
}
38-
3934
type Options struct {
4035
bpfObjPath string
4136
verbose bool
@@ -155,7 +150,7 @@ func printLog2Hists(hists *bpf.BPFMap) {
155150
for _, v := range hist.Slots {
156151
vals = append(vals, int(v))
157152
}
158-
fmt.Printf("\nProcess Name = %s\n", nextKey.CommString())
153+
fmt.Printf("\nProcess Name = %s\n", common.GoString(nextKey.Comm[:]))
159154
common.PrintLog2Hist(vals, "Kbytes")
160155
}
161156

0 commit comments

Comments
 (0)