trace: add timestamp to the output (#3358)

Fix #3356
This commit is contained in:
罗泽轩 2023-05-09 01:41:47 +08:00 committed by GitHub
parent a57c60e43a
commit 801a9109c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 6 deletions

@ -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

@ -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)

@ -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 {

@ -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)