Skip to content

Commit

Permalink
fix(test): fix filter-by-comm.test occasionally failed (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyoush authored Jan 2, 2025
1 parent 6f6c515 commit a60ee5e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
4 changes: 1 addition & 3 deletions agent/uprobe/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import (
var attachedLibPaths map[string]bool = make(map[string]bool)
var uprobeLinks []link.Link = make([]link.Link, 0)

func StartHandleSchedExecEvent() chan *bpf.AgentProcessExecEvent {
ch := make(chan *bpf.AgentProcessExecEvent)
func StartHandleSchedExecEvent(ch chan *bpf.AgentProcessExecEvent) {
go func() {
for event := range ch {
go func(e *bpf.AgentProcessExecEvent) {
Expand All @@ -33,7 +32,6 @@ func StartHandleSchedExecEvent() chan *bpf.AgentProcessExecEvent {
}(event)
}
}()
return ch
}

func handleSchedExecEvent(event *bpf.AgentProcessExecEvent) {
Expand Down
4 changes: 2 additions & 2 deletions bpf/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func parseExitEvent(rawSample []byte) (*AgentProcessExitEvent, error) {
return &event, nil
}

func PullProcessExecEvents(ctx context.Context, channels []chan *AgentProcessExecEvent) error {
func PullProcessExecEvents(ctx context.Context, channels *[]chan *AgentProcessExecEvent) error {
pageSize := os.Getpagesize()
perCPUBuffer := pageSize * 4
eventSize := int(unsafe.Sizeof(AgentProcessExecEvent{}))
Expand Down Expand Up @@ -96,7 +96,7 @@ func PullProcessExecEvents(ctx context.Context, channels []chan *AgentProcessExe
common.AgentLog.Errorf("[dataReader] handleKernEvt err: %s\n", err)
continue
} else {
for _, ch := range channels {
for _, ch := range *channels {
ch <- evt
}
}
Expand Down
82 changes: 36 additions & 46 deletions bpf/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,18 @@ func (bf *BPF) AttachProgs(options *ac.AgentOptions) error {
options.LoadPorgressChannel <- "🍆 Attached base eBPF programs."

if !options.DisableOpensslUprobe {
uprobeSchedEventChannel := make(chan *bpf.AgentProcessExecEvent, 10)
uprobe.StartHandleSchedExecEvent(uprobeSchedEventChannel)
execEventChannels := []chan *bpf.AgentProcessExecEvent{uprobeSchedEventChannel}
if options.ProcessExecEventChannel != nil {
execEventChannels = append(execEventChannels, options.ProcessExecEventChannel)
}
bpf.PullProcessExecEvents(options.Ctx, &execEventChannels)

attachOpenSslUprobes(links, options, options.Kv, bf.Objs)
options.LoadPorgressChannel <- "🍕 Attached ssl eBPF programs."
}
attachSchedProgs(links)
attachNfFunctions(links)
options.LoadPorgressChannel <- "🥪 Attached conntrack eBPF programs."
bf.Links = links
Expand Down Expand Up @@ -514,60 +523,41 @@ func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, opti
}

func attachOpenSslUprobes(links *list.List, options *ac.AgentOptions, kernelVersion *compatible.KernelVersion, objs any) {
if attachOpensslToSpecificProcess() {
sslUprobeLinks, err := uprobe.AttachSslUprobe(int(viper.GetInt64(common.FilterPidVarName)))
if err == nil {
for _, l := range sslUprobeLinks {
links.PushBack(l)
pids, err := common.GetAllPids()
loadGoTlsErr := uprobe.LoadGoTlsUprobe()
if loadGoTlsErr != nil {
common.UprobeLog.Debugf("Load GoTls Probe failed: %+v", loadGoTlsErr)
}
if err == nil {
for _, pid := range pids {
uprobeLinks, err := uprobe.AttachSslUprobe(int(pid))
if err == nil && len(uprobeLinks) > 0 {
for _, l := range uprobeLinks {
links.PushBack(l)
}
common.UprobeLog.Infof("Attach OpenSsl uprobes success for pid: %d", pid)
} else if err != nil {
common.UprobeLog.Infof("Attach OpenSsl uprobes failed: %+v for pid: %d", err, pid)
} else if len(uprobeLinks) == 0 {
common.UprobeLog.Infof("Attach OpenSsl uprobes success for pid: %d use previous libssl path", pid)
}
} else {
common.AgentLog.Infof("Attach OpenSsl uprobes failed: %+v for pid: %d", err, viper.GetInt64(common.FilterPidVarName))
}
if loadGoTlsErr == nil {
gotlsUprobeLinks, err := uprobe.AttachGoTlsProbes(int(pid))

} else {
pids, err := common.GetAllPids()
loadGoTlsErr := uprobe.LoadGoTlsUprobe()
if loadGoTlsErr != nil {
common.UprobeLog.Debugf("Load GoTls Probe failed: %+v", loadGoTlsErr)
}
if err == nil {
for _, pid := range pids {
uprobeLinks, err := uprobe.AttachSslUprobe(int(pid))
if err == nil && len(uprobeLinks) > 0 {
for _, l := range uprobeLinks {
if err == nil && len(gotlsUprobeLinks) > 0 {
for _, l := range gotlsUprobeLinks {
links.PushBack(l)
}
common.UprobeLog.Infof("Attach OpenSsl uprobes success for pid: %d", pid)
common.UprobeLog.Infof("Attach GoTls uprobes success for pid: %d", pid)
} else if err != nil {
common.UprobeLog.Infof("Attach OpenSsl uprobes failed: %+v for pid: %d", err, pid)
} else if len(uprobeLinks) == 0 {
common.UprobeLog.Infof("Attach OpenSsl uprobes success for pid: %d use previous libssl path", pid)
}
if loadGoTlsErr == nil {
gotlsUprobeLinks, err := uprobe.AttachGoTlsProbes(int(pid))

if err == nil && len(gotlsUprobeLinks) > 0 {
for _, l := range gotlsUprobeLinks {
links.PushBack(l)
}
common.UprobeLog.Infof("Attach GoTls uprobes success for pid: %d", pid)
} else if err != nil {
common.UprobeLog.Infof("Attach GoTls uprobes failed: %+v for pid: %d", err, pid)
} else {
common.UprobeLog.Infof("Attach GoTls uprobes failed: %+v for pid: %d links is empty %v", err, pid, gotlsUprobeLinks)
}
common.UprobeLog.Infof("Attach GoTls uprobes failed: %+v for pid: %d", err, pid)
} else {
common.UprobeLog.Infof("Attach GoTls uprobes failed: %+v for pid: %d links is empty %v", err, pid, gotlsUprobeLinks)
}
}
} else {
common.UprobeLog.Warnf("get all pid failed: %v", err)
}
attachSchedProgs(links)
uprobeSchedExecEvent := uprobe.StartHandleSchedExecEvent()
execEventChannels := []chan *bpf.AgentProcessExecEvent{uprobeSchedExecEvent}
if options.ProcessExecEventChannel != nil {
execEventChannels = append(execEventChannels, options.ProcessExecEventChannel)
}
bpf.PullProcessExecEvents(options.Ctx, execEventChannels)
} else {
common.UprobeLog.Warnf("get all pid failed: %v", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions testdata/test_filter_by_comm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function test_filter_by_server_comm() {
timeout 40 python3 ./testdata/start_http_server.py &
timeout 30 ${CMD} watch --debug-output http --comm python3 2>&1 | tee "${BEFORE_LNAME}" &
sleep 2
timeout 25 ./testdata/https-request/https-request 'http://127.0.0.1:8080' 40 &
./testdata/https-request/https-request 'http://127.0.0.1:8080' 40 &>/dev/null || true
sleep 10
wait

Expand All @@ -25,7 +25,7 @@ function test_filter_by_client_comm() {
# client start after kyanos
timeout 40 ${CMD} watch --debug-output http --comm https-request 2>&1 | tee "${AFTER_LNAME}" &
sleep 20
timeout 30 ./testdata/https-request/https-request 'http://ipinfo.io' 40 &
./testdata/https-request/https-request 'http://ipinfo.io' 40 &>/dev/null || true
wait

cat "${AFTER_LNAME}"
Expand Down

0 comments on commit a60ee5e

Please sign in to comment.