mirror of
				https://github.com/go-delve/delve.git
				synced 2025-11-01 03:42:59 +08:00 
			
		
		
		
	Implement break command
This commit is contained in:
		| @ -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 | ||||||
| } | } | ||||||
|  | |||||||
| @ -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.
										
									
								
							| @ -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
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								main.go
									
									
									
									
									
								
							| @ -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> ") | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Derek Parker
					Derek Parker