delve/main.go

148 lines
2.8 KiB
Go
Raw Normal View History

2014-05-20 21:29:01 +00:00
package main
import (
"bufio"
"flag"
2014-05-20 21:29:01 +00:00
"fmt"
"os"
"os/exec"
"runtime"
2014-05-20 21:29:01 +00:00
"strings"
"syscall"
2014-05-20 21:29:01 +00:00
2014-06-09 16:19:49 +00:00
"github.com/derekparker/dbg/command"
"github.com/derekparker/dbg/proctl"
2014-09-19 22:02:06 +00:00
"github.com/rocaltair/goreadline"
2014-05-20 21:29:01 +00:00
)
type term struct {
stdin *bufio.Reader
}
func main() {
// We must ensure here that we are running on the same thread during
// the execution of dbg. This is due to the fact that ptrace(2) expects
2014-05-24 00:01:56 +00:00
// all commands after PTRACE_ATTACH to come from the same thread.
runtime.LockOSThread()
var (
pid int
proc string
t = newTerm()
cmds = command.DebugCommands()
)
2014-05-20 23:11:00 +00:00
flag.IntVar(&pid, "pid", 0, "Pid of running process to attach to.")
flag.StringVar(&proc, "proc", "", "Path to process to run and debug.")
flag.Parse()
2014-05-20 23:11:00 +00:00
if flag.NFlag() == 0 {
flag.Usage()
os.Exit(0)
2014-05-20 23:11:00 +00:00
}
dbgproc := beginTrace(pid, proc)
2014-05-20 23:11:00 +00:00
2014-05-20 21:29:01 +00:00
for {
cmdstr, err := t.promptForInput()
if err != nil {
die(1, "Prompt for input failed.\n")
2014-05-20 21:29:01 +00:00
}
2014-05-27 15:44:29 +00:00
cmdstr, args := parseCommand(cmdstr)
if cmdstr == "exit" {
handleExit(t, dbgproc, 0)
}
2014-05-20 21:29:01 +00:00
cmd := cmds.Find(cmdstr)
err = cmd(dbgproc, args...)
2014-05-20 21:29:01 +00:00
if err != nil {
fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
}
}
}
func beginTrace(pid int, proc string) *proctl.DebuggedProcess {
var (
err error
dbgproc *proctl.DebuggedProcess
)
if pid != 0 {
dbgproc, err = proctl.NewDebugProcess(pid)
if err != nil {
die(1, "Could not start debugging process:", err)
}
}
if proc != "" {
proc := exec.Command(proc)
proc.Stdout = os.Stdout
err = proc.Start()
if err != nil {
die(1, "Could not start process:", err)
}
dbgproc, err = proctl.NewDebugProcess(proc.Process.Pid)
if err != nil {
die(1, "Could not start debugging process:", err)
}
}
return dbgproc
}
func handleExit(t *term, dbp *proctl.DebuggedProcess, status int) {
fmt.Println("Would you like to kill the process? [y/n]")
answer, err := t.stdin.ReadString('\n')
if err != nil {
die(2, err.Error())
}
fmt.Println("Detaching from process...")
err = syscall.PtraceDetach(dbp.Process.Pid)
if err != nil {
die(2, "Could not detach", err)
}
if answer == "y\n" {
fmt.Println("Killing process", dbp.Process.Pid)
err := dbp.Process.Kill()
if err != nil {
fmt.Println("Could not kill process", err)
}
}
die(status, "Hope I was of service hunting your bug!")
}
func die(status int, args ...interface{}) {
2014-05-20 23:11:00 +00:00
fmt.Fprint(os.Stderr, args)
fmt.Fprint(os.Stderr, "\n")
os.Exit(status)
2014-05-20 23:11:00 +00:00
}
2014-05-20 21:29:01 +00:00
func newTerm() *term {
return &term{
stdin: bufio.NewReader(os.Stdin),
}
}
2014-05-27 15:44:29 +00:00
func parseCommand(cmdstr string) (string, []string) {
vals := strings.Split(cmdstr, " ")
return vals[0], vals[1:]
}
2014-05-20 21:29:01 +00:00
func (t *term) promptForInput() (string, error) {
2014-09-19 22:02:06 +00:00
prompt := "dbg> "
line := *goreadline.ReadLine(&prompt)
if line != "" {
goreadline.AddHistory(line)
2014-05-20 21:29:01 +00:00
}
2014-05-20 21:31:25 +00:00
return strings.TrimSuffix(line, "\n"), nil
2014-05-20 21:29:01 +00:00
}