Skip to content

Commit

Permalink
Merge pull request hengyoush#181 from hengyoush/improve-logs
Browse files Browse the repository at this point in the history
fix: output logs to file during loading
  • Loading branch information
hengyoush authored Dec 15, 2024
2 parents 8b35d45 + 1bf3028 commit 09c0101
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func SetupAgent(options ac.AgentOptions) {
{
bf, err := loader.LoadBPF(&options)
if err != nil {
common.AgentLog.Error("Failed to load BPF programs: ", err)
if bf != nil {
bf.Close()
}
Expand Down
2 changes: 2 additions & 0 deletions agent/render/loader/loader_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"kyanos/agent/common"
c "kyanos/common"
"os"
"strings"
"time"
Expand Down Expand Up @@ -108,6 +109,7 @@ func (m model) View() string {
}

func Start(ctx context.Context, options common.AgentOptions) {
c.SetLogToFile()
const numLastResults = 5
m := model{
spinner: spinner.New(spinner.WithSpinner(spinner.Dot)),
Expand Down
21 changes: 11 additions & 10 deletions bpf/loader/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,18 @@ func generateBTF(fileBytes []byte) (*btf.Spec, error) {
return btfPath, nil
}

func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
func loadBTFSpec(options *ac.AgentOptions) (*btf.Spec, error) {
if bpf.IsKernelSupportHasBTF() {
return nil
return nil, nil
}

options.LoadPorgressChannel <- "starting load BTF file"
var spec *btf.Spec
if options.BTFFilePath != "" {
btfPath, err := btf.LoadSpec(options.BTFFilePath)
if err != nil {
common.AgentLog.Fatalf("can't load btf spec from file %s: %v", options.BTFFilePath, err)
common.AgentLog.Warnf("can't load btf spec from file %s: %v\n", options.BTFFilePath, err)
return nil, err
}
spec = btfPath
options.LoadPorgressChannel <- "starting load BTF file: success!"
Expand All @@ -84,11 +85,11 @@ func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
if needGenerateBTF {
spec, err = generateBTF(fileBytes)
if err != nil {
common.AgentLog.Warnf("failed to generate btf file: %+v", err)
common.AgentLog.Warnf("failed to generate btf file: %+v\n", err)
}
}
} else {
common.AgentLog.Warnf("failed to load embeded btf file: %+v", err)
common.AgentLog.Warnf("failed to load embeded btf file: %+v\n", err)
}
}

Expand All @@ -97,7 +98,7 @@ func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
options.LoadPorgressChannel <- "starting load BTF from network..."
btfSpec, _, err := loadBTFSpecFallback("")
if err != nil {
common.AgentLog.Warnf("failed to get btf file from network: %+v", err)
common.AgentLog.Warnf("failed to get btf file from network: %+v\n", err)
} else {
spec = btfSpec
}
Expand All @@ -110,18 +111,18 @@ func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
if needGenerateBTF {
spec, err = generateBTF(fileBytes)
if err != nil {
common.AgentLog.Warnf("failed to generate btf file (best matched): %+v", err)
common.AgentLog.Warnf("failed to generate btf file (best matched): %+v\n", err)
}
}
} else {
common.AgentLog.Warnf("failed to load embedded btf file (best matched): %+v", err)
common.AgentLog.Warnf("failed to load embedded btf file (best matched): %+v\n", err)
}
}

if spec == nil {
common.AgentLog.Fatalf("can't find btf file to load!")
return nil, fmt.Errorf("can't find btf file to load!")
}
return spec
return spec, nil
}

func loadBTFSpecFallback(path string) (*btf.Spec, string, error) {
Expand Down
9 changes: 7 additions & 2 deletions bpf/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) {
var bf *BPF = &BPF{}

if err := features.HaveProgramType(ebpf.Kprobe); errors.Is(err, ebpf.ErrNotSupported) {
common.AgentLog.Fatalf("Require oldest kernel version is 3.10.0-957, pls check your kernel version by `uname -r`")
common.AgentLog.Errorf("Require oldest kernel version is 3.10.0-957, pls check your kernel version by `uname -r`\n")
return nil, err
}

btfSpec, err := loadBTFSpec(options)
if err != nil {
return nil, err
}
collectionOptions = &ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: loadBTFSpec(options),
KernelTypes: btfSpec,
},
}

Expand Down
5 changes: 5 additions & 0 deletions common/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ var ConntrackLog *Klogger = &Klogger{logrus.New()}
var ProtocolParserLog *Klogger = &Klogger{logrus.New()}

var Loggers []*Klogger = []*Klogger{DefaultLog, AgentLog, BPFLog, BPFEventLog, UprobeLog, ConntrackLog, ProtocolParserLog}
var SetLogToFileFlag = false

func SetLogToFile() {
if SetLogToFileFlag {
return
}
SetLogToFileFlag = true
for _, l := range Loggers {
l.SetOut(io.Discard)
logdir := "/tmp"
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,6 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -343,8 +341,6 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down

0 comments on commit 09c0101

Please sign in to comment.