Skip to content

Commit

Permalink
[Improvement] Add btf option to specify btf file path
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyoush committed Sep 10, 2024
1 parent f9add7c commit da49a46
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bpf/kheaders.h: FORCE

FORCE:

kyanos: $(LIBBPF_OBJ) $(GO_FILES) $(wildcard bpf/*.[ch]) bpf/kheaders.h | $(OUTPUT)
kyanos: $(LIBBPF_OBJ) $(GO_FILES) $(wildcard bpf/*.[ch]) | $(OUTPUT)
$(call msg,BINARY,$@)
./build.sh "$(CFLAGS)" "$(VMLINUX)"
rm -f bpf/kheaders.h
Expand Down
20 changes: 18 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"unsafe"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/ringbuf"
Expand Down Expand Up @@ -54,6 +55,7 @@ type AgentOptions struct {
LatencyFilter protocol.LatencyFilter
TraceSide common.SideEnum
IfName string
BTFFilePath string
protocol.SizeFilter
AnalysisEnable bool
analysis.AnalysisOptions
Expand Down Expand Up @@ -116,20 +118,34 @@ func SetupAgent(options AgentOptions) {
var objs any
var spec *ebpf.CollectionSpec
var err error
var collectionOptions *ebpf.CollectionOptions
if options.BTFFilePath != "" {
btfPath, err := btf.LoadSpec(options.BTFFilePath)
if err != nil {
log.Fatalf("can't load btf spec: %v", err)
}
collectionOptions = &ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfPath,
},
}
} else {
collectionOptions = nil
}
if common.NeedsRunningInCompatibleMode() {
objs = &bpf.AgentOldObjects{}
spec, err = bpf.LoadAgentOld()
if err != nil {
log.Fatal("load Agent error:", err)
}
err = spec.LoadAndAssign(objs, nil)
err = spec.LoadAndAssign(objs, collectionOptions)
} else {
objs = &bpf.AgentObjects{}
spec, err = bpf.LoadAgent()
if err != nil {
log.Fatal("load Agent error:", err)
}
err = spec.LoadAndAssign(objs, nil)
err = spec.LoadAndAssign(objs, collectionOptions)
}
bpf.Objs = objs

Expand Down
1 change: 0 additions & 1 deletion bpf/kheaders.h

This file was deleted.

2 changes: 1 addition & 1 deletion bpf/pktlatency.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/* Copyright (c) 2021 Sartura */
#include "kheaders.h"
#include "../vmlinux/vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#ifndef COMPAT_MODE
Expand Down
1 change: 1 addition & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func startAgent(options agent.AgentOptions) {
return
}
options.IfName = IfName
options.BTFFilePath = BTFFilePath

initLog()
logger.Infoln("Kyanos starting...")
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var LocalPorts []string
var RemoteIps []string
var LocalIps []string
var IfName string
var BTFFilePath string

func init() {
// rootCmd.PersistentFlags().StringVar(&LogDir, "log-dir", "", "log file dir")
Expand All @@ -50,6 +51,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "print verbose message")
rootCmd.PersistentFlags().StringVar(&IfName, "ifname", "eth0", "--ifname eth0")
rootCmd.PersistentFlags().MarkHidden("compatible")
rootCmd.PersistentFlags().StringVar(&BTFFilePath, "btf", "", "btf file path")
rootCmd.Flags().SortFlags = false
rootCmd.PersistentFlags().SortFlags = false
viper.BindPFlags(rootCmd.Flags())
Expand Down

0 comments on commit da49a46

Please sign in to comment.