Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: add timestamp to the output #3358

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/usage/dlv_trace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions cmd/dlv/cmds/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/go-delve/delve/pkg/config"
"github.com/go-delve/delve/pkg/gobuild"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/terminal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strconv"
"strings"
"text/tabwriter"
"time"

"github.com/cosiner/argv"
"github.com/go-delve/delve/pkg/config"
Expand Down Expand Up @@ -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)
Expand Down