From c96d0a5ab262ab11d89d11f56c0c7857fbb57a2a Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Mon, 13 Jul 2015 16:34:51 -0500 Subject: [PATCH] Add pid flag to trace subcommand --- cmd/dlv/main.go | 36 +++++++++++++++++++++--------------- proc/proc.go | 5 +++++ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/cmd/dlv/main.go b/cmd/dlv/main.go index c282d5bc..0d9357e9 100644 --- a/cmd/dlv/main.go +++ b/cmd/dlv/main.go @@ -85,27 +85,31 @@ starts and attaches to it, and enables you to immediately begin debugging your p rootCommand.AddCommand(runCommand) // 'trace' subcommand. + var traceAttachPid int traceCommand := &cobra.Command{ - Use: "trace [regexp]", + Use: "trace [regexp] [flags]", Short: "Compile and begin tracing program.", Long: "Trace program execution. Will set a tracepoint on every function matching [regexp] and output information when tracepoint is hit.", Run: func(cmd *cobra.Command, args []string) { status := func() int { const debugname = "debug" - goBuild := exec.Command("go", "build", "-o", debugname, "-gcflags", "-N -l") - goBuild.Stderr = os.Stderr - err := goBuild.Run() - if err != nil { - return 1 - } - fp, err := filepath.Abs("./" + debugname) - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - return 1 - } - defer os.Remove(fp) + var processArgs []string + if traceAttachPid == 0 { + goBuild := exec.Command("go", "build", "-o", debugname, "-gcflags", "-N -l") + goBuild.Stderr = os.Stderr + err := goBuild.Run() + if err != nil { + return 1 + } + fp, err := filepath.Abs("./" + debugname) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + return 1 + } + defer os.Remove(fp) - processArgs := append([]string{"./" + debugname}, args...) + processArgs = append([]string{"./" + debugname}, args...) + } // Make a TCP listener listener, err := net.Listen("tcp", Addr) if err != nil { @@ -118,6 +122,7 @@ starts and attaches to it, and enables you to immediately begin debugging your p server := rpc.NewServer(&service.Config{ Listener: listener, ProcessArgs: processArgs, + AttachPid: traceAttachPid, }, Log) if err := server.Run(); err != nil { fmt.Fprintln(os.Stderr, err) @@ -158,7 +163,7 @@ starts and attaches to it, and enables you to immediately begin debugging your p } fmt.Printf("%s(%s) %s:%d\n", fname, strings.Join(args, ", "), state.CurrentThread.File, state.CurrentThread.Line) case <-sigChan: - server.Stop(true) + server.Stop(traceAttachPid == 0) return 1 } } @@ -167,6 +172,7 @@ starts and attaches to it, and enables you to immediately begin debugging your p os.Exit(status) }, } + traceCommand.Flags().IntVarP(&traceAttachPid, "pid", "p", 0, "Pid to attach to.") rootCommand.AddCommand(traceCommand) // 'test' subcommand. diff --git a/proc/proc.go b/proc/proc.go index 5d1971cc..6da4c2db 100644 --- a/proc/proc.go +++ b/proc/proc.go @@ -90,6 +90,11 @@ func Attach(pid int) (*Process, error) { // Detach from the process being debugged, optionally killing it. func (dbp *Process) Detach(kill bool) (err error) { + if dbp.Running() { + if err = dbp.Halt(); err != nil { + return + } + } if !kill { // Clean up any breakpoints we've set. for _, bp := range dbp.Breakpoints { -- GitLab