Add pid flag to trace subcommand
This commit is contained in:
parent
3cee10d8bc
commit
c96d0a5ab2
@ -85,27 +85,31 @@ starts and attaches to it, and enables you to immediately begin debugging your p
|
|||||||
rootCommand.AddCommand(runCommand)
|
rootCommand.AddCommand(runCommand)
|
||||||
|
|
||||||
// 'trace' subcommand.
|
// 'trace' subcommand.
|
||||||
|
var traceAttachPid int
|
||||||
traceCommand := &cobra.Command{
|
traceCommand := &cobra.Command{
|
||||||
Use: "trace [regexp]",
|
Use: "trace [regexp] [flags]",
|
||||||
Short: "Compile and begin tracing program.",
|
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.",
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
status := func() int {
|
status := func() int {
|
||||||
const debugname = "debug"
|
const debugname = "debug"
|
||||||
goBuild := exec.Command("go", "build", "-o", debugname, "-gcflags", "-N -l")
|
var processArgs []string
|
||||||
goBuild.Stderr = os.Stderr
|
if traceAttachPid == 0 {
|
||||||
err := goBuild.Run()
|
goBuild := exec.Command("go", "build", "-o", debugname, "-gcflags", "-N -l")
|
||||||
if err != nil {
|
goBuild.Stderr = os.Stderr
|
||||||
return 1
|
err := goBuild.Run()
|
||||||
}
|
if err != nil {
|
||||||
fp, err := filepath.Abs("./" + debugname)
|
return 1
|
||||||
if err != nil {
|
}
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fp, err := filepath.Abs("./" + debugname)
|
||||||
return 1
|
if err != nil {
|
||||||
}
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
defer os.Remove(fp)
|
return 1
|
||||||
|
}
|
||||||
|
defer os.Remove(fp)
|
||||||
|
|
||||||
processArgs := append([]string{"./" + debugname}, args...)
|
processArgs = append([]string{"./" + debugname}, args...)
|
||||||
|
}
|
||||||
// Make a TCP listener
|
// Make a TCP listener
|
||||||
listener, err := net.Listen("tcp", Addr)
|
listener, err := net.Listen("tcp", Addr)
|
||||||
if err != nil {
|
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{
|
server := rpc.NewServer(&service.Config{
|
||||||
Listener: listener,
|
Listener: listener,
|
||||||
ProcessArgs: processArgs,
|
ProcessArgs: processArgs,
|
||||||
|
AttachPid: traceAttachPid,
|
||||||
}, Log)
|
}, Log)
|
||||||
if err := server.Run(); err != nil {
|
if err := server.Run(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
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)
|
fmt.Printf("%s(%s) %s:%d\n", fname, strings.Join(args, ", "), state.CurrentThread.File, state.CurrentThread.Line)
|
||||||
case <-sigChan:
|
case <-sigChan:
|
||||||
server.Stop(true)
|
server.Stop(traceAttachPid == 0)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,6 +172,7 @@ starts and attaches to it, and enables you to immediately begin debugging your p
|
|||||||
os.Exit(status)
|
os.Exit(status)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
traceCommand.Flags().IntVarP(&traceAttachPid, "pid", "p", 0, "Pid to attach to.")
|
||||||
rootCommand.AddCommand(traceCommand)
|
rootCommand.AddCommand(traceCommand)
|
||||||
|
|
||||||
// 'test' subcommand.
|
// 'test' subcommand.
|
||||||
|
@ -90,6 +90,11 @@ func Attach(pid int) (*Process, error) {
|
|||||||
|
|
||||||
// Detach from the process being debugged, optionally killing it.
|
// Detach from the process being debugged, optionally killing it.
|
||||||
func (dbp *Process) Detach(kill bool) (err error) {
|
func (dbp *Process) Detach(kill bool) (err error) {
|
||||||
|
if dbp.Running() {
|
||||||
|
if err = dbp.Halt(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
if !kill {
|
if !kill {
|
||||||
// Clean up any breakpoints we've set.
|
// Clean up any breakpoints we've set.
|
||||||
for _, bp := range dbp.Breakpoints {
|
for _, bp := range dbp.Breakpoints {
|
||||||
|
Loading…
Reference in New Issue
Block a user