From 436fbf61b1d2bc21bfe222a60508e027d0843351 Mon Sep 17 00:00:00 2001 From: spacewander Date: Fri, 5 May 2023 19:23:24 +0800 Subject: [PATCH] trace: add timestamp to the output Fix #3356 --- Documentation/usage/dlv_trace.md | 1 + cmd/dlv/cmds/commands.go | 23 +++++++++++++++++------ pkg/config/config.go | 4 ++++ pkg/terminal/command.go | 5 +++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Documentation/usage/dlv_trace.md b/Documentation/usage/dlv_trace.md index 176aaca968..018a511429 100644 --- a/Documentation/usage/dlv_trace.md +++ b/Documentation/usage/dlv_trace.md @@ -28,6 +28,7 @@ dlv trace [package] regexp [flags] -p, --pid int Pid to attach to. -s, --stack int Show stack trace with given depth. (Ignored with --ebpf) -t, --test Trace a test binary. + --timestamp Show timestamp in the output ``` ### Options inherited from parent commands diff --git a/cmd/dlv/cmds/commands.go b/cmd/dlv/cmds/commands.go index c98c047b51..54b61ab939 100644 --- a/cmd/dlv/cmds/commands.go +++ b/cmd/dlv/cmds/commands.go @@ -14,6 +14,7 @@ import ( "strconv" "strings" "syscall" + "time" "github.com/go-delve/delve/pkg/config" "github.com/go-delve/delve/pkg/gobuild" @@ -78,11 +79,12 @@ var ( // rootCommand is the root of the command tree. rootCommand *cobra.Command - traceAttachPid int - traceExecFile string - traceTestBinary bool - traceStackDepth int - traceUseEBPF bool + traceAttachPid int + traceExecFile string + traceTestBinary bool + traceStackDepth int + traceUseEBPF bool + traceShowTimestamp bool // redirect specifications for target process redirects []string @@ -307,6 +309,7 @@ only see the output of the trace operations you can redirect stdout.`, traceCommand.Flags().StringVarP(&traceExecFile, "exec", "e", "", "Binary file to exec and trace.") traceCommand.Flags().BoolVarP(&traceTestBinary, "test", "t", false, "Trace a test binary.") traceCommand.Flags().BoolVarP(&traceUseEBPF, "ebpf", "", false, "Trace using eBPF (experimental).") + traceCommand.Flags().BoolVarP(&traceShowTimestamp, "timestamp", "", false, "Show timestamp in the output") traceCommand.Flags().IntVarP(&traceStackDepth, "stack", "s", 0, "Show stack trace with given depth. (Ignored with --ebpf)") traceCommand.Flags().String("output", "debug", "Output path for the binary.") rootCommand.AddCommand(traceCommand) @@ -694,7 +697,10 @@ func traceCmd(cmd *cobra.Command, args []string) { return 1 } cmds := terminal.DebugCommands(client) - t := terminal.New(client, nil) + cfg := &config.Config{ + TraceShowTimestamp: traceShowTimestamp, + } + t := terminal.New(client, cfg) t.SetTraceNonInteractive() t.RedirectTo(os.Stderr) defer t.Close() @@ -723,6 +729,11 @@ func traceCmd(cmd *cobra.Command, args []string) { params.WriteString(p.Value) } } + + if traceShowTimestamp { + fmt.Fprintf(os.Stderr, "%s ", time.Now().Format(time.RFC3339Nano)) + } + if t.IsRet { for _, p := range t.ReturnParams { fmt.Fprintf(os.Stderr, "=> %#v\n", p.Value) diff --git a/pkg/config/config.go b/pkg/config/config.go index b01747a3e6..faf6fa713d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -102,6 +102,10 @@ type Config struct { // This can be used to shorten the tabstop (e.g. " ") or to print a more // visual indent (e.g. ">__ "). Tab string `yaml:"tab"` + + // TraceShowTimestamp controls whether to show timestamp in the trace + // output. + TraceShowTimestamp bool `yaml:"trace-show-timestamp"` } func (c *Config) GetSourceListLineCount() int { diff --git a/pkg/terminal/command.go b/pkg/terminal/command.go index 7afc9ca850..f894703f0d 100644 --- a/pkg/terminal/command.go +++ b/pkg/terminal/command.go @@ -23,6 +23,7 @@ import ( "strconv" "strings" "text/tabwriter" + "time" "github.com/cosiner/argv" "github.com/go-delve/delve/pkg/config" @@ -2756,6 +2757,10 @@ func printBreakpointInfo(t *Term, th *api.Thread, tracepointOnNewline bool) { } func printTracepoint(t *Term, th *api.Thread, bpname string, fn *api.Function, args string, hasReturnValue bool) { + if t.conf.TraceShowTimestamp { + fmt.Fprintf(t.stdout, "%s ", time.Now().Format(time.RFC3339Nano)) + } + if th.Breakpoint.Tracepoint { fmt.Fprintf(t.stdout, "> goroutine(%d): %s%s(%s)\n", th.GoroutineID, bpname, fn.Name(), args) printBreakpointInfo(t, th, !hasReturnValue)