delve/main.go

78 lines
1.3 KiB
Go
Raw Normal View History

2014-05-20 21:29:01 +00:00
package main
import (
"bufio"
"fmt"
"os"
2014-05-20 23:11:00 +00:00
"strconv"
2014-05-20 21:29:01 +00:00
"strings"
"github.com/Dparker1990/dbg/command"
2014-05-20 23:11:00 +00:00
"github.com/Dparker1990/dbg/proctl"
2014-05-20 21:29:01 +00:00
)
type term struct {
stdin *bufio.Reader
}
func main() {
2014-05-20 23:11:00 +00:00
t := newTerm()
if len(os.Args) == 1 {
printStderrAndDie("You must provide a pid\n")
}
pid, err := strconv.Atoi(os.Args[1])
if err != nil {
printStderrAndDie(err)
}
dbgproc, err := proctl.NewDebugProcess(pid)
if err != nil {
printStderrAndDie("Could not start debugging process:", err)
}
cmds := command.DebugCommands()
registerProcessCommands(cmds, dbgproc)
2014-05-20 21:29:01 +00:00
for {
cmdstr, err := t.promptForInput()
if err != nil {
2014-05-20 23:11:00 +00:00
printStderrAndDie("Prompt for input failed.\n")
2014-05-20 21:29:01 +00:00
}
cmd := cmds.Find(cmdstr)
err = cmd()
if err != nil {
fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
}
}
}
2014-05-20 23:11:00 +00:00
func printStderrAndDie(args ...interface{}) {
fmt.Fprint(os.Stderr, args)
os.Exit(1)
}
func registerProcessCommands(cmds *command.Commands, proc *proctl.DebuggedProcess) {
cmds.Register("step", proc.Step)
cmds.Register("continue", proc.Continue)
}
2014-05-20 21:29:01 +00:00
func newTerm() *term {
return &term{
stdin: bufio.NewReader(os.Stdin),
}
}
func (t *term) promptForInput() (string, error) {
fmt.Print("dbg> ")
line, err := t.stdin.ReadString('\n')
if err != nil {
return "", err
}
2014-05-20 21:31:25 +00:00
return strings.TrimSuffix(line, "\n"), nil
2014-05-20 21:29:01 +00:00
}