diff --git a/cmd/tetra/bugtool/bugtool.go b/cmd/tetra/bugtool/bugtool.go index 45019608148..ffd15b8dd99 100644 --- a/cmd/tetra/bugtool/bugtool.go +++ b/cmd/tetra/bugtool/bugtool.go @@ -12,6 +12,7 @@ import ( var ( outFile string bpfTool string + gops string ) func New() *cobra.Command { @@ -19,12 +20,13 @@ func New() *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 } diff --git a/pkg/bugtool/bugtool.go b/pkg/bugtool/bugtool.go index 389a7dcd5c7..5864ab9ae11 100644 --- a/pkg/bugtool/bugtool.go +++ b/pkg/bugtool/bugtool.go @@ -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 @@ -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") @@ -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 @@ -192,6 +201,10 @@ func Bugtool(outFname string, bpftool string) error { info.BpfToolPath = bpftool } + if gops != "" { + info.GopsPath = gops + } + return doBugtool(info, outFname) } @@ -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") @@ -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 { diff --git a/pkg/observer/observertesthelper/observer_test_helper.go b/pkg/observer/observertesthelper/observer_test_helper.go index b98428db0a5..1407d5ba80c 100644 --- a/pkg/observer/observertesthelper/observer_test_helper.go +++ b/pkg/observer/observertesthelper/observer_test_helper.go @@ -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 {