forked from gojue/ecapture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe_postgres.go
165 lines (137 loc) · 3.51 KB
/
probe_postgres.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
165
//go:build !androidgki
// +build !androidgki
/*
Copyright © 2022 CFC4N <cfc4n.cs@gmail.com>
*/
package user
import (
"bytes"
"context"
"ecapture/assets"
"ecapture/pkg/event_processor"
"log"
"math"
"os"
"github.com/cilium/ebpf"
manager "github.com/ehids/ebpfmanager"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
type MPostgresProbe struct {
Module
bpfManager *manager.Manager
bpfManagerOptions manager.Options
eventFuncMaps map[*ebpf.Map]event_processor.IEventStruct
eventMaps []*ebpf.Map
}
// init probe
func (this *MPostgresProbe) Init(ctx context.Context, logger *log.Logger, conf IConfig) error {
this.Module.Init(ctx, logger)
this.conf = conf
this.Module.SetChild(this)
this.eventMaps = make([]*ebpf.Map, 0, 2)
this.eventFuncMaps = make(map[*ebpf.Map]event_processor.IEventStruct)
return nil
}
func (this *MPostgresProbe) Start() error {
if err := this.start(); err != nil {
return err
}
return nil
}
func (this *MPostgresProbe) start() error {
// fetch ebpf assets
byteBuf, err := assets.Asset("user/bytecode/postgres_kern.o")
if err != nil {
return errors.Wrap(err, "couldn't find asset")
}
// setup the managers
err = this.setupManagers()
if err != nil {
return errors.Wrap(err, "postgres module couldn't find binPath.")
}
// initialize the bootstrap manager
if err := this.bpfManager.InitWithOptions(bytes.NewReader(byteBuf), this.bpfManagerOptions); err != nil {
return errors.Wrap(err, "couldn't init manager")
}
// start the bootstrap manager
if err := this.bpfManager.Start(); err != nil {
return errors.Wrap(err, "couldn't start bootstrap manager")
}
// 加载map信息,map对应events decode表。
err = this.initDecodeFun()
if err != nil {
return err
}
return nil
}
func (this *MPostgresProbe) Close() error {
if err := this.bpfManager.Stop(manager.CleanAll); err != nil {
return errors.Wrap(err, "couldn't stop manager")
}
return nil
}
func (this *MPostgresProbe) setupManagers() error {
binaryPath := this.conf.(*PostgresConfig).PostgresPath
_, err := os.Stat(binaryPath)
if err != nil {
return err
}
attachFunc := this.conf.(*PostgresConfig).FuncName
probes := []*manager.Probe{
{
Section: "uprobe/exec_simple_qurey",
EbpfFuncName: "postgres_query",
AttachToFuncName: attachFunc,
BinaryPath: binaryPath,
},
}
this.bpfManager = &manager.Manager{
Probes: probes,
Maps: []*manager.Map{
{
Name: "events",
},
},
}
this.logger.Printf("Postgres, binrayPath: %s, FunctionName: %s\n", binaryPath, attachFunc)
this.bpfManagerOptions = manager.Options{
DefaultKProbeMaxActive: 512,
VerifierOptions: ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
LogSize: 2097152,
},
},
RLimit: &unix.Rlimit{
Cur: math.MaxUint64,
Max: math.MaxUint64,
},
}
return nil
}
func (this *MPostgresProbe) DecodeFun(em *ebpf.Map) (event_processor.IEventStruct, bool) {
fun, found := this.eventFuncMaps[em]
return fun, found
}
func (this *MPostgresProbe) initDecodeFun() error {
// postgresEventsMap to hook
postgresEventsMap, found, err := this.bpfManager.GetMap("events")
if err != nil {
return err
}
if !found {
return errors.New("cant found map: events")
}
this.eventMaps = append(this.eventMaps, postgresEventsMap)
this.eventFuncMaps[postgresEventsMap] = &postgresEvent{}
return nil
}
func (this *MPostgresProbe) Events() []*ebpf.Map {
return this.eventMaps
}
func init() {
mod := &MPostgresProbe{}
mod.name = MODULE_NAME_POSTGRES
mod.mType = PROBE_TYPE_UPROBE
Register(mod)
}