Implement break command

This commit is contained in:
Derek Parker
2014-05-27 10:44:29 -05:00
parent 0866de0c86
commit 821313d6cf
5 changed files with 45 additions and 11 deletions

View File

@ -7,7 +7,7 @@ import (
"os" "os"
) )
type cmdfunc func() error type cmdfunc func(args ...string) error
type Commands struct { type Commands struct {
cmds map[string]cmdfunc cmds map[string]cmdfunc
@ -44,15 +44,21 @@ func (c *Commands) Find(cmdstr string) cmdfunc {
return cmd return cmd
} }
func noCmdAvailable() error { func CommandFunc(fn func() error) cmdfunc {
return func(args ...string) error {
return fn()
}
}
func noCmdAvailable(args ...string) error {
return fmt.Errorf("command not available") return fmt.Errorf("command not available")
} }
func exitFunc() error { func exitFunc(args ...string) error {
os.Exit(0) os.Exit(0)
return nil return nil
} }
func nullCommand() error { func nullCommand(args ...string) error {
return nil return nil
} }

View File

@ -24,7 +24,7 @@ func TestCommandDefault(t *testing.T) {
func TestCommandRegister(t *testing.T) { func TestCommandRegister(t *testing.T) {
cmds := Commands{make(map[string]cmdfunc)} cmds := Commands{make(map[string]cmdfunc)}
cmds.Register("foo", func() error { return fmt.Errorf("registered command") }) cmds.Register("foo", func(args ...string) error { return fmt.Errorf("registered command") })
cmd := cmds.Find("foo") cmd := cmds.Find("foo")
@ -40,7 +40,7 @@ func TestCommandRegister(t *testing.T) {
func TestCommandReplay(t *testing.T) { func TestCommandReplay(t *testing.T) {
cmds := DebugCommands() cmds := DebugCommands()
cmds.Register("foo", func() error { return fmt.Errorf("registered command") }) cmds.Register("foo", func(args ...string) error { return fmt.Errorf("registered command") })
cmd := cmds.Find("foo") cmd := cmds.Find("foo")
err := cmd() err := cmd()

Binary file not shown.

View File

@ -6,12 +6,22 @@ import (
"time" "time"
) )
func printPid(pid int) {
fmt.Println(pid)
}
func sayhi() {
fmt.Println("hi")
}
func main() { func main() {
pid := os.Getpid() pid := os.Getpid()
fmt.Println(pid) printPid(pid)
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
for { for {
fmt.Println(pid) printPid(pid)
time.Sleep(3 * time.Millisecond)
sayhi()
} }
} }

24
main.go
View File

@ -47,8 +47,10 @@ func main() {
printStderrAndDie("Prompt for input failed.\n") printStderrAndDie("Prompt for input failed.\n")
} }
cmdstr, args := parseCommand(cmdstr)
cmd := cmds.Find(cmdstr) cmd := cmds.Find(cmdstr)
err = cmd() err = cmd(args...)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Command failed: %s\n", err) fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
} }
@ -61,8 +63,19 @@ func printStderrAndDie(args ...interface{}) {
} }
func registerProcessCommands(cmds *command.Commands, proc *proctl.DebuggedProcess) { func registerProcessCommands(cmds *command.Commands, proc *proctl.DebuggedProcess) {
cmds.Register("step", proc.Step) cmds.Register("step", command.CommandFunc(proc.Step))
cmds.Register("continue", proc.Continue) cmds.Register("continue", command.CommandFunc(proc.Continue))
cmds.Register("break", func(args ...string) error {
fname := args[0]
bp, err := proc.Break(fname)
if err != nil {
return err
}
fmt.Printf("Breakpoint set at %#v for %s %s:%d\n", bp.Addr, bp.FunctionName, bp.File, bp.Line)
return nil
})
} }
func newTerm() *term { func newTerm() *term {
@ -71,6 +84,11 @@ func newTerm() *term {
} }
} }
func parseCommand(cmdstr string) (string, []string) {
vals := strings.Split(cmdstr, " ")
return vals[0], vals[1:]
}
func (t *term) promptForInput() (string, error) { func (t *term) promptForInput() (string, error) {
fmt.Print("dbg> ") fmt.Print("dbg> ")