Skip to content

Commit

Permalink
feat: check pid when attaching uprode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laitr0n committed Feb 8, 2025
1 parent 2c1723a commit dde11f6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (

func SetupAgent(options ac.AgentOptions) {
startGopsServer(options)
err := version.UpgradeDetect()
if err != nil {

if err := version.UpgradeDetect(); err != nil {
if errors.Is(err, version.ErrBehindLatest) {
common.AgentLog.Warn(err)
}
Expand Down
38 changes: 38 additions & 0 deletions agent/uprobe/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uprobe

import (
"debug/elf"
"errors"
"fmt"
ac "kyanos/agent/common"
"kyanos/bpf"
Expand Down Expand Up @@ -34,7 +35,44 @@ func StartHandleSchedExecEvent(ch chan *bpf.AgentProcessExecEvent) {
}()
}

func filterPidMapLength() int {
l := 0
var filterPidMap *ebpf.Map = bpf.GetMapFromObjs(bpf.Objs, "FilterPidMap")
keyOut := uint32(0)
valueOut := int8(0)
iterate := filterPidMap.Iterate()
for iterate.Next(&keyOut, &valueOut) {
l++
}
return l
}

func matchFilter(pid int32) (bool, error) {
if filterPidMapLength() == 0 {
return true, nil
}
var filterPidMap *ebpf.Map = bpf.GetMapFromObjs(bpf.Objs, "FilterPidMap")
one := int8(1)
if err := filterPidMap.Lookup(&pid, &one); err != nil {
if errors.Is(err, ebpf.ErrKeyNotExist) {
return false, nil
} else {
return false, fmt.Errorf("lookup pid %d in filterPidMap failed: %v", pid, err)
}
}
return true, nil
}

func handleSchedExecEvent(event *bpf.AgentProcessExecEvent) {
isPidmatch, err := matchFilter(event.Pid)
if err != nil {
common.UprobeLog.Errorf("matchFilter failed for pid: %d: %v", event.Pid, err)
return
}
if !isPidmatch {
common.UprobeLog.Debugf("pid %d not in filterPidMap, skip attach uprobes", event.Pid)
return
}
links, err := AttachSslUprobe(int(event.Pid))
var procName string
if proc, err := process.NewProcess(event.Pid); err == nil {
Expand Down

0 comments on commit dde11f6

Please sign in to comment.