diff --git a/command/command.go b/command/command.go index db744efc..edf6ecdf 100644 --- a/command/command.go +++ b/command/command.go @@ -7,7 +7,7 @@ import ( "os" ) -type cmdfunc func() error +type cmdfunc func(args ...string) error type Commands struct { cmds map[string]cmdfunc @@ -44,15 +44,21 @@ func (c *Commands) Find(cmdstr string) cmdfunc { 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") } -func exitFunc() error { +func exitFunc(args ...string) error { os.Exit(0) return nil } -func nullCommand() error { +func nullCommand(args ...string) error { return nil } diff --git a/command/command_test.go b/command/command_test.go index ee06085b..b40ee6eb 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -24,7 +24,7 @@ func TestCommandDefault(t *testing.T) { func TestCommandRegister(t *testing.T) { 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") @@ -40,7 +40,7 @@ func TestCommandRegister(t *testing.T) { func TestCommandReplay(t *testing.T) { 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") err := cmd() diff --git a/fixtures/livetestprog b/fixtures/livetestprog index 30abc59f..364b46db 100755 Binary files a/fixtures/livetestprog and b/fixtures/livetestprog differ diff --git a/fixtures/livetestprog.go b/fixtures/livetestprog.go index 5489557c..be4b4a6d 100644 --- a/fixtures/livetestprog.go +++ b/fixtures/livetestprog.go @@ -6,12 +6,22 @@ import ( "time" ) +func printPid(pid int) { + fmt.Println(pid) +} + +func sayhi() { + fmt.Println("hi") +} + func main() { pid := os.Getpid() - fmt.Println(pid) + printPid(pid) time.Sleep(3 * time.Second) for { - fmt.Println(pid) + printPid(pid) + time.Sleep(3 * time.Millisecond) + sayhi() } } diff --git a/main.go b/main.go index 85bd0cdf..47922d9c 100644 --- a/main.go +++ b/main.go @@ -47,8 +47,10 @@ func main() { printStderrAndDie("Prompt for input failed.\n") } + cmdstr, args := parseCommand(cmdstr) + cmd := cmds.Find(cmdstr) - err = cmd() + err = cmd(args...) if err != nil { 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) { - cmds.Register("step", proc.Step) - cmds.Register("continue", proc.Continue) + cmds.Register("step", command.CommandFunc(proc.Step)) + 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 { @@ -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) { fmt.Print("dbg> ")