delve/main.go

168 lines
3.1 KiB
Go
Raw Normal View History

2014-05-20 21:29:01 +00:00
package main
import (
"bufio"
"debug/gosym"
2014-05-20 21:29:01 +00:00
"fmt"
"os"
"path/filepath"
"runtime"
2014-05-20 23:11:00 +00:00
"strconv"
2014-05-20 21:29:01 +00:00
"strings"
2014-06-09 16:19:49 +00:00
"github.com/derekparker/dbg/command"
"github.com/derekparker/dbg/proctl"
2014-05-20 21:29:01 +00:00
)
type term struct {
stdin *bufio.Reader
}
func main() {
// We must ensure here that we are running on the same thread during
// the execution of dbg. This is due to the fact that ptrace(2) expects
2014-05-24 00:01:56 +00:00
// all commands after PTRACE_ATTACH to come from the same thread.
runtime.LockOSThread()
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
}
2014-05-27 15:44:29 +00:00
cmdstr, args := parseCommand(cmdstr)
2014-05-20 21:29:01 +00:00
cmd := cmds.Find(cmdstr)
2014-05-27 15:44:29 +00:00
err = cmd(args...)
2014-05-20 21:29:01 +00:00
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) {
2014-05-27 15:44:29 +00:00
cmds.Register("continue", command.CommandFunc(proc.Continue))
cmds.Register("step", func(args ...string) error {
err := proc.Step()
if err != nil {
return err
}
regs, err := proc.Registers()
if err != nil {
return err
}
f, l, _ := proc.GoSymTable.PCToLine(regs.PC())
fmt.Printf("Stopped at: %s:%d\n", f, l)
return nil
})
2014-05-27 19:32:40 +00:00
cmds.Register("clear", func(args ...string) error {
fname := args[0]
2014-05-27 23:15:18 +00:00
fn := proc.GoSymTable.LookupFunc(fname)
if fn == nil {
return fmt.Errorf("No function named %s", fname)
}
bp, err := proc.Clear(fn.Entry)
2014-05-27 19:32:40 +00:00
if err != nil {
return err
}
fmt.Printf("Breakpoint cleared at %#v for %s %s:%d\n", bp.Addr, bp.FunctionName, bp.File, bp.Line)
return nil
})
2014-05-27 15:44:29 +00:00
cmds.Register("break", func(args ...string) error {
var (
fn *gosym.Func
pc uint64
fname = args[0]
)
if strings.ContainsRune(fname, ':') {
fl := strings.Split(fname, ":")
f, err := filepath.Abs(fl[0])
if err != nil {
return err
}
l, err := strconv.Atoi(fl[1])
if err != nil {
return err
}
pc, fn, err = proc.GoSymTable.LineToPC(f, l)
if err != nil {
return err
}
} else {
fn = proc.GoSymTable.LookupFunc(fname)
pc = fn.Entry
}
2014-05-27 23:15:18 +00:00
if fn == nil {
return fmt.Errorf("No function named %s", fname)
}
bp, err := proc.Break(uintptr(pc))
2014-05-27 15:44:29 +00:00
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
})
2014-05-20 23:11:00 +00:00
}
2014-05-20 21:29:01 +00:00
func newTerm() *term {
return &term{
stdin: bufio.NewReader(os.Stdin),
}
}
2014-05-27 15:44:29 +00:00
func parseCommand(cmdstr string) (string, []string) {
vals := strings.Split(cmdstr, " ")
return vals[0], vals[1:]
}
2014-05-20 21:29:01 +00:00
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
}