Skip to content

Commit

Permalink
bugtool: allow read or set gops path
Browse files Browse the repository at this point in the history
Signed-off-by: Djalal Harouni <[email protected]>
  • Loading branch information
tixxdz committed Jan 22, 2024
1 parent 506677d commit 08b68e2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cmd/tetra/bugtool/bugtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import (
var (
outFile string
bpfTool string
gops string
)

func New() *cobra.Command {
bugtoolCmd := &cobra.Command{
Use: "bugtool",
Short: "Produce a tar archive with debug information",
Run: func(cmd *cobra.Command, args []string) {
bugtool.Bugtool(outFile, bpfTool)
bugtool.Bugtool(outFile, bpfTool, gops)
},
}

flags := bugtoolCmd.Flags()
flags.StringVarP(&outFile, "out", "o", "tetragon-bugtool.tar.gz", "Output filename")
flags.StringVar(&bpfTool, "bpftool", "", "Path to bpftool binary")
flags.StringVar(&gops, "gops", "", "Path to gops binary")
return bugtoolCmd
}
38 changes: 33 additions & 5 deletions pkg/bugtool/bugtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type InitInfo struct {
GopsAddr string `json:"gops_address"`
MapDir string `json:"map_dir"`
BpfToolPath string `json:"bpftool_path"`
GopsPath string `json:"gops_path"`
}

// LoadInitInfo returns the InitInfo by reading the info file from its default location
Expand Down Expand Up @@ -83,6 +84,14 @@ func doSaveInitInfo(fname string, info *InitInfo) error {
logger.GetLogger().WithField("bpftool", info.BpfToolPath).Info("Successfully detected bpftool path")
}

gops, err := exec.LookPath("gops")
if err != nil {
logger.GetLogger().Warn("failed to locate gops binary, on bugtool debugging ensure you have gops installed")
} else {
info.GopsPath = gops
logger.GetLogger().WithField("gops", info.GopsPath).Info("Successfully detected gops path")
}

// Create DefaultRunDir if it does not already exist
if err := os.MkdirAll(defaults.DefaultRunDir, 0755); err != nil {
logger.GetLogger().WithField("infoFile", fname).Warn("failed to directory exists")
Expand Down Expand Up @@ -182,7 +191,7 @@ func (s *bugtoolInfo) tarAddFile(tarWriter *tar.Writer, fnameSrc string, fnameDs
}

// Bugtool gathers information and writes it as a tar archive in the given filename
func Bugtool(outFname string, bpftool string) error {
func Bugtool(outFname string, bpftool string, gops string) error {
info, err := LoadInitInfo()
if err != nil {
return err
Expand All @@ -192,6 +201,10 @@ func Bugtool(outFname string, bpftool string) error {
info.BpfToolPath = bpftool
}

if gops != "" {
info.GopsPath = gops
}

return doBugtool(info, outFname)
}

Expand Down Expand Up @@ -488,7 +501,7 @@ func (s *bugtoolInfo) addBpftoolInfo(tarWriter *tar.Writer) {

_, err := os.Stat(s.info.BpfToolPath)
if err != nil {
s.multiLog.WithError(err).Warn("Failed to locate bpftool, please install it.")
s.multiLog.WithError(err).Warn("Failed to locate bpftool. Please install it or specify its path, see 'bugtool --help'")
return
}
s.execCmd(tarWriter, "bpftool-maps.json", s.info.BpfToolPath, "map", "show", "-j")
Expand All @@ -498,11 +511,26 @@ func (s *bugtoolInfo) addBpftoolInfo(tarWriter *tar.Writer) {

func (s *bugtoolInfo) addGopsInfo(tarWriter *tar.Writer) {
if s.info.GopsAddr == "" {
s.multiLog.Info("Skipping gops dump info as daemon is running without gops, use --gops-address to enable gops")
return
}
s.execCmd(tarWriter, "gops.stack", "gops", "stack", s.info.GopsAddr)
s.execCmd(tarWriter, "gpos.stats", "gops", "stats", s.info.GopsAddr)
s.execCmd(tarWriter, "gops.memstats", "gops", "memstats", s.info.GopsAddr)

if s.info.GopsPath == "" {
s.multiLog.WithField("gops-address", s.info.GopsAddr).Warn("Failed to locate gops. Please install it or specify its path, see 'bugtool --help'")
return
}

_, err := os.Stat(s.info.GopsPath)
if err != nil {
s.multiLog.WithField("gops-address", s.info.GopsAddr).WithError(err).Warn("Failed to locate gops, please install it.")
return
}

s.multiLog.WithField("gops-address", s.info.GopsAddr).WithField("gops-path", s.info.GopsPath).Info("Dumping gops information")

s.execCmd(tarWriter, "gops.stack", s.info.GopsPath, "stack", s.info.GopsAddr)
s.execCmd(tarWriter, "gpos.stats", s.info.GopsPath, "stats", s.info.GopsAddr)
s.execCmd(tarWriter, "gops.memstats", s.info.GopsPath, "memstats", s.info.GopsAddr)
}

func (s *bugtoolInfo) dumpPolicyFilterMap(tarWriter *tar.Writer) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/observer/observertesthelper/observer_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func WithLib(lib string) TestOption {
func testDone(tb testing.TB, obs *observer.Observer) {
if tb.Failed() {
bugtoolFname := "/tmp/tetragon-bugtool.tar.gz"
if err := bugtool.Bugtool(bugtoolFname, ""); err == nil {
if err := bugtool.Bugtool(bugtoolFname, "", ""); err == nil {
logger.GetLogger().WithField("test", tb.Name()).
WithField("file", bugtoolFname).Info("Dumped bugtool info")
} else {
Expand Down

0 comments on commit 08b68e2

Please sign in to comment.