Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize logs #268

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ func SetupAgent(options ac.AgentOptions) {

var _bf loader.BPF
go func(_bf *loader.BPF) {
defer wg.Done()
options.LoadPorgressChannel <- "🍩 Kyanos starting..."
kernelVersion := compatible.GetCurrentKernelVersion()
options.Kv = &kernelVersion
var err error
defer func() {
if err != nil {
common.AgentLog.Error("Failed to load BPF programs: ", err)
common.AgentLog.Errorf("Failed to load BPF programs: %+v", errors.Unwrap(errors.Unwrap(err)))
_bf.Err = err
options.LoadPorgressChannel <- "❌ Kyanos start failed"
options.LoadPorgressChannel <- "quit"
Expand Down Expand Up @@ -151,7 +152,6 @@ func SetupAgent(options ac.AgentOptions) {
time.Sleep(500 * time.Millisecond)
options.LoadPorgressChannel <- "quit"
}
defer wg.Done()
}(&_bf)
defer func() {
_bf.Close()
Expand Down
36 changes: 18 additions & 18 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,25 +586,25 @@ func TestSslEventsCanRelatedToKernEvents(t *testing.T) {
bpf.AttachSyscallWriteExit,
bpf.AttachKProbeSecuritySocketSendmsgEntry,
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTUSER_COPY)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_IN)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTTCP_IN)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_OUT)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTQDISC_OUT)
},
},
Expand Down Expand Up @@ -964,7 +964,7 @@ func TestIpXmit(t *testing.T) {
bpf.AttachSyscallWriteEntry,
bpf.AttachSyscallWriteExit,
bpf.AttachKProbeSecuritySocketSendmsgEntry,
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
},
},
Expand Down Expand Up @@ -994,10 +994,10 @@ func TestDevQueueXmit(t *testing.T) {
bpf.AttachSyscallWriteEntry,
bpf.AttachSyscallWriteExit,
bpf.AttachKProbeSecuritySocketSendmsgEntry,
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTQDISC_OUT)
},
}, "GET DevQueueXmit\n", Write, Read,
Expand Down Expand Up @@ -1029,10 +1029,10 @@ func TestDevHardStartXmit(t *testing.T) {
bpf.AttachSyscallWriteEntry,
bpf.AttachSyscallWriteExit,
bpf.AttachKProbeSecuritySocketSendmsgEntry,
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_OUT)
},
}, "GET DevHardStartXmit\n", Write, Read,
Expand Down Expand Up @@ -1110,10 +1110,10 @@ func TestIpRcvCore(t *testing.T) {
bpf.AttachSyscallWriteExit,
bpf.AttachKProbeSecuritySocketSendmsgEntry,
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_IN)
},
},
Expand Down Expand Up @@ -1150,10 +1150,10 @@ func TestTcpV4DoRcv(t *testing.T) {
bpf.AttachSyscallWriteExit,
bpf.AttachKProbeSecuritySocketSendmsgEntry,
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTTCP_IN)
},
},
Expand Down Expand Up @@ -1190,10 +1190,10 @@ func TestSkbCopyDatagramIter(t *testing.T) {
bpf.AttachSyscallWriteExit,
bpf.AttachKProbeSecuritySocketSendmsgEntry,
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
},
func() link.Link {
func() (link.Link, error) {
return ApplyKernelVersionFunctions(t, bpf.AgentStepTUSER_COPY)
},
},
Expand Down
14 changes: 9 additions & 5 deletions agent/agent_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func StartAgent0(bpfAttachFunctions []bpf.AttachBpfProgFunction,
progs := list.New()
for _, each := range bpfAttachFunctions {
if each != nil {
progs.PushBack(each())
l, err := each()
if err != nil {
log.Fatalf("Attach failed: %v", err)
}
progs.PushBack(l)
}
}
return progs
Expand Down Expand Up @@ -777,15 +781,15 @@ func min(a, b int) int {
// compatilbeMode = b
// }

func ApplyKernelVersionFunctions(t *testing.T, step bpf.AgentStepT) link.Link {
func ApplyKernelVersionFunctions(t *testing.T, step bpf.AgentStepT) (link.Link, error) {
v := compatible.GetCurrentKernelVersion()
if step == bpf.AgentStepTNIC_IN {
if v.SupportCapability(compatible.SupportXDP) {
l, err := bpf.AttachXdp()
if err != nil {
t.Fatal(err)
} else {
return l
return l, nil
}
} else {
t.FailNow()
Expand Down Expand Up @@ -813,11 +817,11 @@ func ApplyKernelVersionFunctions(t *testing.T, step bpf.AgentStepT) link.Link {
log.Fatalf("Attach failed: %v, functions: %v", err, functions)
}
} else {
return l
return l, nil
}
}
t.FailNow()
return nil
return nil, nil
}

func KernRcvTestWithHTTP(t *testing.T, progs []bpf.AttachBpfProgFunction, kernEvtFilter FindInterestedKernEventOptions, kernEvtAsserts KernDataEventAssertConditions) {
Expand Down
Loading
Loading