mirror of
https://github.com/go-delve/delve.git
synced 2025-11-01 20:20:40 +08:00
Implement break command
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
Binary file not shown.
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
24
main.go
24
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> ")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user