From 57c75421f4ac6f290938a0184a7812b065d25346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=83=88=E9=A6=99?= Date: Mon, 13 Jan 2025 01:00:21 +0800 Subject: [PATCH] refactor: optimize logs (#268) --- agent/agent.go | 4 +- agent/agent_test.go | 36 ++++---- agent/agent_utils_test.go | 14 +-- bpf/loader/loader.go | 184 ++++++++++++++++++++++++++++++++------ bpf/prog.go | 137 ++++++++++++---------------- 5 files changed, 244 insertions(+), 131 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 0101fbcd..02c5bbed 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -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" @@ -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() diff --git a/agent/agent_test.go b/agent/agent_test.go index 0db61002..0f4a0eeb 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -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) }, }, @@ -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) }, }, @@ -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, @@ -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, @@ -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) }, }, @@ -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) }, }, @@ -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) }, }, diff --git a/agent/agent_utils_test.go b/agent/agent_utils_test.go index c5cf2dfd..2abc66b0 100644 --- a/agent/agent_utils_test.go +++ b/agent/agent_utils_test.go @@ -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 @@ -777,7 +781,7 @@ 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) { @@ -785,7 +789,7 @@ func ApplyKernelVersionFunctions(t *testing.T, step bpf.AgentStepT) link.Link { if err != nil { t.Fatal(err) } else { - return l + return l, nil } } else { t.FailNow() @@ -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) { diff --git a/bpf/loader/loader.go b/bpf/loader/loader.go index fc8b1537..dbfc1752 100644 --- a/bpf/loader/loader.go +++ b/bpf/loader/loader.go @@ -73,6 +73,7 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) { collectionOptions = &ebpf.CollectionOptions{ Programs: ebpf.ProgramOptions{ KernelTypes: btfSpec, + LogLevel: ebpf.LogLevelInstruction, }, } @@ -86,6 +87,9 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) { } filterFunctions(spec, *options.Kv) err = spec.LoadAndAssign(lagacyobjs, collectionOptions) + if err != nil { + return nil, err + } objs = AgentObjectsFromLagacyKernel310(lagacyobjs) } else { objs = &bpf.AgentObjects{} @@ -95,6 +99,9 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) { } filterFunctions(spec, *options.Kv) err = spec.LoadAndAssign(objs, collectionOptions) + if err != nil { + return nil, err + } } bf.Objs = objs bpf.Objs = objs @@ -447,7 +454,7 @@ func isProcNameMacthed(proc *process.Process, filterComm string) bool { return false } -func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, options *ac.AgentOptions) (l *list.List, err error) { +func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, options *ac.AgentOptions) (links *list.List, err error) { defer func() { if r := recover(); r != nil { common.AgentLog.Errorf("Recovered in attachBpfProgs: %v", r) @@ -511,46 +518,167 @@ func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, opti } } - linkList.PushBack(bpf.AttachSyscallAcceptEntry()) - linkList.PushBack(bpf.AttachSyscallAcceptExit()) + l, err := bpf.AttachSyscallAcceptEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallAcceptExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallSockAllocExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallConnectEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallConnectExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallCloseEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallCloseExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallWriteEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallWriteExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallSendMsgEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallSendMsgExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallSendFile64Entry() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallSockAllocExit()) + l, err = bpf.AttachSyscallSendFile64Exit() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallConnectEntry()) - linkList.PushBack(bpf.AttachSyscallConnectExit()) + l, err = bpf.AttachSyscallRecvMsgEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallCloseEntry()) - linkList.PushBack(bpf.AttachSyscallCloseExit()) + l, err = bpf.AttachSyscallRecvMsgExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallWriteEntry()) - linkList.PushBack(bpf.AttachSyscallWriteExit()) + l, err = bpf.AttachSyscallWritevEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallWritevExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallSendMsgEntry()) - linkList.PushBack(bpf.AttachSyscallSendMsgExit()) + l, err = bpf.AttachSyscallSendtoEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) + + l, err = bpf.AttachSyscallSendtoExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallSendFile64Entry()) - linkList.PushBack(bpf.AttachSyscallSendFile64Exit()) + l, err = bpf.AttachSyscallReadEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallRecvMsgEntry()) - linkList.PushBack(bpf.AttachSyscallRecvMsgExit()) + l, err = bpf.AttachSyscallReadExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallWritevEntry()) - linkList.PushBack(bpf.AttachSyscallWritevExit()) + l, err = bpf.AttachSyscallReadvEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallSendtoEntry()) - linkList.PushBack(bpf.AttachSyscallSendtoExit()) + l, err = bpf.AttachSyscallReadvExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallReadEntry()) - linkList.PushBack(bpf.AttachSyscallReadExit()) + l, err = bpf.AttachSyscallRecvfromEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallReadvEntry()) - linkList.PushBack(bpf.AttachSyscallReadvExit()) + l, err = bpf.AttachSyscallRecvfromExit() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachSyscallRecvfromEntry()) - linkList.PushBack(bpf.AttachSyscallRecvfromExit()) + l, err = bpf.AttachKProbeSecuritySocketRecvmsgEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) - linkList.PushBack(bpf.AttachKProbeSecuritySocketRecvmsgEntry()) - linkList.PushBack(bpf.AttachKProbeSecuritySocketSendmsgEntry()) + l, err = bpf.AttachKProbeSecuritySocketSendmsgEntry() + if err != nil { + return nil, err + } + linkList.PushBack(l) return linkList, nil } diff --git a/bpf/prog.go b/bpf/prog.go index af59d187..467b21a8 100644 --- a/bpf/prog.go +++ b/bpf/prog.go @@ -1,7 +1,7 @@ package bpf import ( - "log" + "kyanos/common" "net" "reflect" @@ -11,7 +11,7 @@ import ( var Objs any -type AttachBpfProgFunction func() link.Link +type AttachBpfProgFunction func() (link.Link, error) func GetProgramFromObjs(objs any, progName string) *ebpf.Program { val := reflect.ValueOf(objs) @@ -32,126 +32,126 @@ func GetProgramFromObjs(objs any, progName string) *ebpf.Program { } /* accept pair */ -func AttachSyscallAcceptEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_accept4", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterAccept4")) +func AttachSyscallAcceptEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_accept4", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterAccept4")) } -func AttachSyscallAcceptExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_accept4", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitAccept4")) +func AttachSyscallAcceptExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_accept4", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitAccept4")) } /* sock_alloc */ -func AttachSyscallSockAllocExit() link.Link { - return KretprobeNoError("sock_alloc", GetProgramFromObjs(Objs, "SockAllocRet")) +func AttachSyscallSockAllocExit() (link.Link, error) { + return Kretprobe("sock_alloc", GetProgramFromObjs(Objs, "SockAllocRet")) } /* connect pair */ -func AttachSyscallConnectEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_connect", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterConnect")) +func AttachSyscallConnectEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_connect", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterConnect")) } -func AttachSyscallConnectExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_connect", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitConnect")) +func AttachSyscallConnectExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_connect", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitConnect")) } /* close pair */ -func AttachSyscallCloseEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_close", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterClose")) +func AttachSyscallCloseEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_close", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterClose")) } -func AttachSyscallCloseExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_close", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitClose")) +func AttachSyscallCloseExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_close", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitClose")) } /* write pair */ -func AttachSyscallWriteEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_write", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterWrite")) +func AttachSyscallWriteEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_write", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterWrite")) } -func AttachSyscallWriteExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_write", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitWrite")) +func AttachSyscallWriteExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_write", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitWrite")) } /* sendmsg pair */ -func AttachSyscallSendMsgEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_sendmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterSendmsg")) +func AttachSyscallSendMsgEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_sendmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterSendmsg")) } -func AttachSyscallSendMsgExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_sendmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitSendmsg")) +func AttachSyscallSendMsgExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_sendmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitSendmsg")) } /* sendfile pair */ -func AttachSyscallSendFile64Entry() link.Link { - return TracepointNoError("syscalls", "sys_enter_sendfile64", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterSendfile64")) +func AttachSyscallSendFile64Entry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_sendfile64", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterSendfile64")) } -func AttachSyscallSendFile64Exit() link.Link { - return TracepointNoError("syscalls", "sys_exit_sendfile64", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitSendfile64")) +func AttachSyscallSendFile64Exit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_sendfile64", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitSendfile64")) } /* recvmsg pair */ -func AttachSyscallRecvMsgEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_recvmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterRecvmsg")) +func AttachSyscallRecvMsgEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_recvmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterRecvmsg")) } -func AttachSyscallRecvMsgExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_recvmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitRecvmsg")) +func AttachSyscallRecvMsgExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_recvmsg", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitRecvmsg")) } /* writev pair */ -func AttachSyscallWritevEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_writev", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterWritev")) +func AttachSyscallWritevEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_writev", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterWritev")) } -func AttachSyscallWritevExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_writev", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitWritev")) +func AttachSyscallWritevExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_writev", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitWritev")) } /* sendto pair */ -func AttachSyscallSendtoEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_sendto", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterSendto")) +func AttachSyscallSendtoEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_sendto", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterSendto")) } -func AttachSyscallSendtoExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_sendto", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitSendto")) +func AttachSyscallSendtoExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_sendto", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitSendto")) } /* read pair */ -func AttachSyscallReadEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_read", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterRead")) +func AttachSyscallReadEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_read", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterRead")) } -func AttachSyscallReadExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_read", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitRead")) +func AttachSyscallReadExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_read", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitRead")) } /* readv pair */ -func AttachSyscallReadvEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_readv", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterReadv")) +func AttachSyscallReadvEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_readv", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterReadv")) } -func AttachSyscallReadvExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_readv", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitReadv")) +func AttachSyscallReadvExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_readv", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitReadv")) } /* recvfrom pair */ -func AttachSyscallRecvfromEntry() link.Link { - return TracepointNoError("syscalls", "sys_enter_recvfrom", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterRecvfrom")) +func AttachSyscallRecvfromEntry() (link.Link, error) { + return Tracepoint("syscalls", "sys_enter_recvfrom", GetProgramFromObjs(Objs, "TracepointSyscallsSysEnterRecvfrom")) } -func AttachSyscallRecvfromExit() link.Link { - return TracepointNoError("syscalls", "sys_exit_recvfrom", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitRecvfrom")) +func AttachSyscallRecvfromExit() (link.Link, error) { + return Tracepoint("syscalls", "sys_exit_recvfrom", GetProgramFromObjs(Objs, "TracepointSyscallsSysExitRecvfrom")) } /* security_socket_recvmsg */ -func AttachKProbeSecuritySocketRecvmsgEntry() link.Link { - return Kprobe2("security_socket_recvmsg", GetProgramFromObjs(Objs, "SecuritySocketRecvmsgEnter")) +func AttachKProbeSecuritySocketRecvmsgEntry() (link.Link, error) { + return Kprobe("security_socket_recvmsg", GetProgramFromObjs(Objs, "SecuritySocketRecvmsgEnter")) } /* security_socket_sendmsg */ -func AttachKProbeSecuritySocketSendmsgEntry() link.Link { - return Kprobe2("security_socket_sendmsg", GetProgramFromObjs(Objs, "SecuritySocketSendmsgEnter")) +func AttachKProbeSecuritySocketSendmsgEntry() (link.Link, error) { + return Kprobe("security_socket_sendmsg", GetProgramFromObjs(Objs, "SecuritySocketSendmsgEnter")) } /* tcp_destroy_sock */ @@ -176,8 +176,8 @@ func AttachKProbeTcpV4DoRcvEntry() (link.Link, error) { return Kprobe("tcp_v4_do_rcv", GetProgramFromObjs(Objs, "TcpV4DoRcv")) } -func AttachTracepointNetifReceiveSkb() link.Link { - return TracepointNoError("net", "netif_receive_skb", GetProgramFromObjs(Objs, "TracepointNetifReceiveSkb")) +func AttachTracepointNetifReceiveSkb() (link.Link, error) { + return Tracepoint("net", "netif_receive_skb", GetProgramFromObjs(Objs, "TracepointNetifReceiveSkb")) } func AttachXdpWithSpecifiedIfName(ifname string) (link.Link, error) { @@ -219,28 +219,9 @@ func Tracepoint(group string, name string, prog *ebpf.Program) (link.Link, error func TracepointNoError(group string, name string, prog *ebpf.Program) link.Link { l, err := link.Tracepoint(group, name, prog, nil) if err != nil { - log.Fatalf("failed to attach tracepoint, group: %s name: %s, err: %v", group, name, err) + common.BPFLog.Warnf("failed to attach tracepoint, group: %s name: %s, err: %v", group, name, err) return nil } else { return l } } - -func KretprobeNoError(func_name string, prog *ebpf.Program) link.Link { - l, err := link.Kretprobe(func_name, prog, nil) - if err != nil { - log.Fatalf("failed to attach kretprobe, func_name: %s , err: %v", func_name, err) - return nil - } else { - return l - } -} - -func Kprobe2(func_name string, prog *ebpf.Program) link.Link { - if link, err := link.Kprobe(func_name, prog, nil); err != nil { - log.Fatalf("kprobe2 failed: %s, %s, fallbacking..", func_name, err) - return nil - } else { - return link - } -}