Skip to content

Commit

Permalink
refactor: remove fatal log (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyoush authored Dec 19, 2024
1 parent f384ad8 commit 4be0227
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 165 deletions.
23 changes: 14 additions & 9 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,23 @@ func SetupAgent(options ac.AgentOptions) {
kernelVersion := compatible.GetCurrentKernelVersion()
options.Kv = &kernelVersion
var err error
{
bf, err := loader.LoadBPF(&options)
defer func() {
if err != nil {
common.AgentLog.Error("Failed to load BPF programs: ", err)
if bf != nil {
bf.Close()
}
_bf.Err = err
options.LoadPorgressChannel <- "❌ Kyanos start failed"
options.LoadPorgressChannel <- "quit"
return
}
_bf.Links = bf.Links
_bf.Objs = bf.Objs
}()
bf, err := loader.LoadBPF(&options)
if err != nil {
if bf != nil {
bf.Close()
}
return
}
_bf.Links = bf.Links
_bf.Objs = bf.Objs

err = bpf.PullSyscallDataEvents(ctx, pm.GetSyscallEventsChannels(), 2048, options.CustomSyscallEventHook)
if err != nil {
Expand All @@ -123,7 +125,10 @@ func SetupAgent(options ac.AgentOptions) {
if err != nil {
return
}
_bf.AttachProgs(&options)
err = _bf.AttachProgs(&options)
if err != nil {
return
}
if !options.WatchOptions.DebugOutput {
options.LoadPorgressChannel <- "🍹 All programs attached"
options.LoadPorgressChannel <- "🍭 Waiting for events.."
Expand Down
139 changes: 0 additions & 139 deletions agent/container.go

This file was deleted.

2 changes: 0 additions & 2 deletions bpf/loader/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
ac "kyanos/agent/common"
"kyanos/bpf"
"kyanos/common"
"log"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -438,6 +437,5 @@ func getBestMatchedBTFFile(findExactly bool) ([]uint8, error) {
return file, nil
}
}
log.Fatalln("can't start kyanos because no available btf file, please refer this url: https://hengyoush.github.io/kyanos/quickstart.html for more info.")
return nil, errors.New("no btf file found to load")
}
20 changes: 11 additions & 9 deletions bpf/loader/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package loader

import (
"context"
"fmt"
ac "kyanos/agent/common"
"kyanos/agent/metadata"
"kyanos/agent/metadata/types"
"kyanos/bpf"
"kyanos/common"
"log"

"github.com/cilium/ebpf"
)
Expand All @@ -22,15 +22,17 @@ func applyContainerFilter(ctx context.Context, options *ac.AgentOptions) (*metad
cc, err, k8sErr := metadata.NewContainerCache(ctx, options.DockerEndpoint, options.ContainerdEndpoint, options.CriRuntimeEndpoint)
if err != nil {
if options.FilterByContainer() {
common.DefaultLog.Fatalf("find container failed: %s", err)
common.DefaultLog.Errorf("find container failed: %s", err)
return nil, nil, err
} else {
common.DefaultLog.Warnf("find container failed: %s", err)
return nil, nil, nil
}
}
if k8sErr != nil {
if options.FilterByK8s() {
common.DefaultLog.Fatalf("find pod failed: %s", k8sErr)
common.DefaultLog.Errorf("find pod failed: %s", k8sErr)
return nil, nil, k8sErr
} else {
common.DefaultLog.Infof("find pod failed: %s", k8sErr)
}
Expand All @@ -45,25 +47,25 @@ func applyContainerFilter(ctx context.Context, options *ac.AgentOptions) (*metad
case options.ContainerId != "":
container := cc.GetById(options.ContainerId)
if container.EmptyNS() {
log.Fatalf("can not find any running container by id %s", options.ContainerId)
return nil, nil, fmt.Errorf("can not find any running container by id %s", options.ContainerId)
}
containers = append(containers, container)
case options.ContainerName != "":
cs := cc.GetByName(options.ContainerName)
cs = removeNonFilterAbleContainers(cs)
if len(cs) > 1 {
log.Fatalf("found more than one containers by name %s", options.ContainerName)
return nil, nil, fmt.Errorf("found more than one containers by name %s", options.ContainerName)
}
if len(cs) == 0 {
log.Fatalf("can not find any running container by name %s", options.ContainerName)
return nil, nil, fmt.Errorf("can not find any running container by name %s", options.ContainerName)
}
container := cs[0]
containers = append(containers, container)
case options.PodName != "":
cs := cc.GetByPodName(options.PodName, options.PodNameSpace)
cs = removeNonFilterAbleContainers(cs)
if len(cs) == 0 {
log.Fatalf("can not find any running pod by name %s in namespace %s", options.PodName, options.PodNameSpace)
return nil, nil, fmt.Errorf("can not find any running pod by name %s in namespace %s", options.PodName, options.PodNameSpace)
}
containers = append(containers, cs...)
}
Expand All @@ -74,10 +76,10 @@ func applyContainerFilter(ctx context.Context, options *ac.AgentOptions) (*metad
}
for _, container := range containers {
if container.IsSandbox() {
common.DefaultLog.Infof("skip sandbox container: %#v", container)
common.AgentLog.Infof("skip sandbox container: %#v", container)
continue
}
common.DefaultLog.Infof("filter by container %#v", container)
common.AgentLog.Infof("filter by container %#v", container)
if container.PidNamespace > 0 && container.PidNamespace != metadata.HostPidNs {
result.pidnsIds = append(result.pidnsIds, uint32(container.PidNamespace))
}
Expand Down
26 changes: 20 additions & 6 deletions bpf/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) {
lagacyobjs := &bpf.AgentLagacyKernel310Objects{}
spec, err = bpf.LoadAgentLagacyKernel310()
if err != nil {
common.AgentLog.Fatal("load Agent error:", err)
return nil, err
}
filterFunctions(spec, *options.Kv)
err = spec.LoadAndAssign(lagacyobjs, collectionOptions)
Expand All @@ -90,7 +90,7 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) {
objs = &bpf.AgentObjects{}
spec, err = bpf.LoadAgent()
if err != nil {
common.AgentLog.Fatal("load Agent error:", err)
return nil, err
}
filterFunctions(spec, *options.Kv)
err = spec.LoadAndAssign(objs, collectionOptions)
Expand Down Expand Up @@ -123,10 +123,14 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) {

func (bf *BPF) AttachProgs(options *ac.AgentOptions) error {
var links *list.List
var err error
if options.LoadBpfProgramFunction != nil {
links = options.LoadBpfProgramFunction()
} else {
links = attachBpfProgs(options.IfName, options.Kv, options)
links, err = attachBpfProgs(options.IfName, options.Kv, options)
if err != nil {
return err
}
}

options.LoadPorgressChannel <- "🍆 Attached base eBPF programs."
Expand Down Expand Up @@ -314,6 +318,9 @@ func setAndValidateParameters(ctx context.Context, options *ac.AgentOptions) boo
writeFilterNsIdsToMap(filterResult, bpf.Objs)
one := int64(1)
controlValues.Update(bpf.AgentControlValueIndexTKEnableFilterByPid, one, ebpf.UpdateAny)
} else {
common.AgentLog.Errorf("applyContainerFilter failed: %v", err)
return false
}
}

Expand Down Expand Up @@ -398,7 +405,14 @@ func isProcNameMacthed(proc *process.Process, filterComm string) bool {
return false
}

func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, options *ac.AgentOptions) *list.List {
func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, options *ac.AgentOptions) (l *list.List, err error) {
defer func() {
if r := recover(); r != nil {
common.AgentLog.Errorf("Recovered in attachBpfProgs: %v", r)
err = fmt.Errorf("attachBpfProgs panic: %v", r)
}
}()

linkList := list.New()

if kernelVersion.SupportCapability(compatible.SupportXDP) {
Expand Down Expand Up @@ -443,7 +457,7 @@ func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, opti
if isNonCriticalStep {
common.AgentLog.Debugf("Attach failed: %v, functions: %v skip it because it's a non-criticalstep", err, functions)
} else {
common.AgentLog.Fatalf("Attach failed: %v, functions: %v", err, functions)
return nil, fmt.Errorf("Attach failed: %v, functions: %v", err, functions)
}
} else {
common.AgentLog.Debugf("Attach failed but has fallback: %v, functions: %v", err, functions)
Expand Down Expand Up @@ -493,7 +507,7 @@ func attachBpfProgs(ifName string, kernelVersion *compatible.KernelVersion, opti
linkList.PushBack(bpf.AttachKProbeSecuritySocketRecvmsgEntry())
linkList.PushBack(bpf.AttachKProbeSecuritySocketSendmsgEntry())

return linkList
return linkList, nil
}

func attachOpenSslUprobes(links *list.List, options *ac.AgentOptions, kernelVersion *compatible.KernelVersion, objs any) {
Expand Down

0 comments on commit 4be0227

Please sign in to comment.