delve/proctl/proctl.go

594 lines
14 KiB
Go
Raw Normal View History

package proctl
import (
2014-11-24 23:27:56 +00:00
"debug/dwarf"
"debug/gosym"
"encoding/binary"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
sys "golang.org/x/sys/unix"
"github.com/derekparker/delve/dwarf/frame"
"github.com/derekparker/delve/dwarf/line"
"github.com/derekparker/delve/dwarf/reader"
"github.com/derekparker/delve/source"
)
2014-12-08 23:15:52 +00:00
// Struct representing a debugged process. Holds onto pid, register values,
// process struct and process state.
type DebuggedProcess struct {
Pid int
Process *os.Process
2015-01-14 23:40:52 +00:00
HWBreakPoints [4]*BreakPoint
BreakPoints map[uint64]*BreakPoint
Threads map[int]*ThreadContext
CurrentThread *ThreadContext
dwarf *dwarf.Data
goSymTable *gosym.Table
frameEntries frame.FrameDescriptionEntries
lineInfo *line.DebugLineInfo
2015-01-14 02:37:10 +00:00
os *OSProcessDetails
ast *source.Searcher
breakpointIDCounter int
running bool
halt bool
exited bool
2014-12-08 23:15:52 +00:00
}
2015-02-02 21:09:56 +00:00
// A ManualStopError happens when the user triggers a
// manual stop via SIGERM.
type ManualStopError struct{}
func (mse ManualStopError) Error() string {
return "Manual stop requested"
}
// ProcessExitedError indicates that the process has exited and contains both
// process id and exit status.
type ProcessExitedError struct {
Pid int
Status int
}
func (pe ProcessExitedError) Error() string {
return fmt.Sprintf("process %d has exited with status %d", pe.Pid, pe.Status)
}
2015-02-02 21:09:56 +00:00
// Attach to an existing process with the given PID.
func Attach(pid int) (*DebuggedProcess, error) {
dbp, err := newDebugProcess(pid, true)
if err != nil {
return nil, err
}
return dbp, nil
}
2015-02-02 21:09:56 +00:00
// Create and begin debugging a new process. First entry in
// `cmd` is the program to run, and then rest are the arguments
// to be supplied to that process.
func Launch(cmd []string) (*DebuggedProcess, error) {
proc := exec.Command(cmd[0])
proc.Args = cmd
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
proc.SysProcAttr = &syscall.SysProcAttr{Ptrace: true}
if err := proc.Start(); err != nil {
return nil, err
}
_, _, err := wait(proc.Process.Pid, 0)
if err != nil {
return nil, fmt.Errorf("waiting for target execve failed: %s", err)
}
return newDebugProcess(proc.Process.Pid, false)
}
// Returns whether or not Delve thinks the debugged
// process has exited.
func (dbp *DebuggedProcess) Exited() bool {
return dbp.exited
}
2015-02-02 21:09:56 +00:00
// Returns whether or not Delve thinks the debugged
// process is currently executing.
func (dbp *DebuggedProcess) Running() bool {
return dbp.running
}
// Finds the executable and then uses it
// to parse the following information:
// * Dwarf .debug_frame section
// * Dwarf .debug_line section
// * Go symbol table.
func (dbp *DebuggedProcess) LoadInformation() error {
var wg sync.WaitGroup
exe, err := dbp.findExecutable()
if err != nil {
return err
}
wg.Add(3)
go dbp.parseDebugFrame(exe, &wg)
go dbp.obtainGoSymbols(exe, &wg)
go dbp.parseDebugLineInfo(exe, &wg)
wg.Wait()
return nil
}
// Find a location by string (file+line, function, breakpoint id, addr)
func (dbp *DebuggedProcess) FindLocation(str string) (uint64, error) {
// File + Line
if strings.ContainsRune(str, ':') {
fl := strings.Split(str, ":")
fileName, err := filepath.Abs(fl[0])
if err != nil {
return 0, err
}
line, err := strconv.Atoi(fl[1])
if err != nil {
return 0, err
}
pc, _, err := dbp.goSymTable.LineToPC(fileName, line)
if err != nil {
return 0, err
}
return pc, nil
2015-03-24 13:31:56 +00:00
}
2015-03-24 13:31:56 +00:00
// Try to lookup by function name
fn := dbp.goSymTable.LookupFunc(str)
2015-03-24 13:31:56 +00:00
if fn != nil {
return fn.Entry, nil
}
// Attempt to parse as number for breakpoint id or raw address
id, err := strconv.ParseUint(str, 0, 64)
if err != nil {
return 0, fmt.Errorf("unable to find location for %s", str)
}
2015-03-24 13:31:56 +00:00
// Use as breakpoint id
for _, bp := range dbp.HWBreakPoints {
if bp == nil {
continue
}
2015-03-24 13:31:56 +00:00
if uint64(bp.ID) == id {
return bp.Addr, nil
}
}
2015-03-24 13:31:56 +00:00
for _, bp := range dbp.BreakPoints {
if uint64(bp.ID) == id {
return bp.Addr, nil
}
}
// Last resort, use as raw address
return id, nil
}
2015-02-02 21:09:56 +00:00
// Sends out a request that the debugged process halt
// execution. Sends SIGSTOP to all threads.
2015-04-13 22:17:06 +00:00
func (dbp *DebuggedProcess) RequestManualStop() error {
dbp.halt = true
2015-04-13 22:17:06 +00:00
err := dbp.requestManualStop()
if err != nil {
return err
}
dbp.running = false
2015-04-13 22:17:06 +00:00
return nil
}
2015-01-14 02:37:10 +00:00
// Sets a breakpoint at addr, and stores it in the process wide
// break point table. Setting a break point must be thread specific due to
// ptrace actions needing the thread to be in a signal-delivery-stop.
//
// Depending on hardware support, Delve will choose to either
// set a hardware or software breakpoint. Essentially, if the
// hardware supports it, and there are free debug registers, Delve
// will set a hardware breakpoint. Otherwise we fall back to software
// breakpoints, which are a bit more work for us.
func (dbp *DebuggedProcess) Break(addr uint64) (*BreakPoint, error) {
return dbp.setBreakpoint(dbp.CurrentThread.Id, addr, false)
}
2015-04-23 01:00:42 +00:00
// Sets a temp breakpoint, for the 'next' command.
func (dbp *DebuggedProcess) TempBreak(addr uint64) (*BreakPoint, error) {
return dbp.setBreakpoint(dbp.CurrentThread.Id, addr, true)
}
// Sets a breakpoint by location string (function, file+line, address)
func (dbp *DebuggedProcess) BreakByLocation(loc string) (*BreakPoint, error) {
addr, err := dbp.FindLocation(loc)
if err != nil {
return nil, err
}
2015-01-14 02:37:10 +00:00
return dbp.Break(addr)
}
// Clears a breakpoint in the current thread.
func (dbp *DebuggedProcess) Clear(addr uint64) (*BreakPoint, error) {
return dbp.clearBreakpoint(dbp.CurrentThread.Id, addr)
}
// Clears a breakpoint by location (function, file+line, address, breakpoint id)
func (dbp *DebuggedProcess) ClearByLocation(loc string) (*BreakPoint, error) {
addr, err := dbp.FindLocation(loc)
if err != nil {
return nil, err
}
2015-01-14 02:37:10 +00:00
return dbp.Clear(addr)
}
// Returns the status of the current main thread context.
func (dbp *DebuggedProcess) Status() *sys.WaitStatus {
return dbp.CurrentThread.Status
}
// Step over function calls.
func (dbp *DebuggedProcess) Next() error {
if err := dbp.setChanRecvBreakpoints(); err != nil {
return err
}
return dbp.run(dbp.next)
}
func (dbp *DebuggedProcess) next() error {
2015-04-23 14:17:14 +00:00
// Make sure we clean up the temp breakpoints created by thread.Next
defer dbp.clearTempBreakpoints()
curg, err := dbp.CurrentThread.curG()
if err != nil {
return err
}
var goroutineExiting bool
for _, th := range dbp.Threads {
if th.blocked() { // Continue threads that aren't running go code.
2015-04-23 15:30:27 +00:00
if err = th.Continue(); err != nil {
return err
}
continue
}
if err = th.Next(); err != nil {
if err, ok := err.(GoroutineExitingError); ok {
if err.goid == curg.Id {
goroutineExiting = true
}
if err := th.Continue(); err != nil {
return err
}
continue
}
return err
}
}
for {
thread, err := dbp.trapWait(-1)
if err != nil {
return err
2015-01-14 02:37:10 +00:00
}
2015-04-03 15:32:43 +00:00
tg, err := thread.curG()
if err != nil {
return err
}
2015-04-23 15:54:26 +00:00
// Make sure we're on the same goroutine, unless it has exited.
if tg.Id == curg.Id || goroutineExiting {
2015-04-03 15:32:43 +00:00
if dbp.CurrentThread != thread {
dbp.SwitchThread(thread.Id)
}
2015-04-23 15:54:26 +00:00
if err = dbp.Halt(); err != nil {
return err
}
break
}
}
2015-04-23 15:54:26 +00:00
return nil
}
2015-04-20 21:58:49 +00:00
func (dbp *DebuggedProcess) setChanRecvBreakpoints() error {
allg, err := dbp.GoroutinesInfo()
if err != nil {
return err
}
for _, g := range allg {
if g.ChanRecvBlocked() {
ret, err := g.chanRecvReturnAddr(dbp)
if err != nil {
return err
}
if _, err = dbp.TempBreak(ret); err != nil {
return err
}
}
}
return nil
}
// Resume process.
func (dbp *DebuggedProcess) Continue() error {
for _, thread := range dbp.Threads {
err := thread.Continue()
if err != nil {
return err
}
}
return dbp.run(dbp.resume)
}
func (dbp *DebuggedProcess) resume() error {
thread, err := dbp.trapWait(-1)
if err != nil {
return err
}
if dbp.CurrentThread != thread {
dbp.SwitchThread(thread.Id)
}
2015-04-23 15:40:33 +00:00
pc, err := thread.PC()
if err != nil {
return err
}
2015-04-13 22:17:06 +00:00
if dbp.CurrentBreakpoint != nil || dbp.halt {
return dbp.Halt()
}
// Check to see if we hit a runtime.breakpoint
fn := dbp.goSymTable.PCToFunc(pc)
if fn != nil && fn.Name == "runtime.breakpoint" {
// step twice to get back to user code
for i := 0; i < 2; i++ {
if err = thread.Step(); err != nil {
return err
2015-03-01 04:03:26 +00:00
}
}
return dbp.Halt()
}
return fmt.Errorf("unrecognized breakpoint %#v", pc)
}
2015-04-23 01:00:42 +00:00
// Single step, will execute a single instruction.
2015-01-14 02:37:10 +00:00
func (dbp *DebuggedProcess) Step() (err error) {
fn := func() error {
for _, th := range dbp.Threads {
if th.blocked() {
continue
}
err := th.Step()
if err != nil {
return err
}
}
return nil
}
return dbp.run(fn)
}
// Change from current thread to the thread specified by `tid`.
func (dbp *DebuggedProcess) SwitchThread(tid int) error {
if th, ok := dbp.Threads[tid]; ok {
dbp.CurrentThread = th
fmt.Printf("thread context changed from %d to %d\n", dbp.CurrentThread.Id, tid)
return nil
}
return fmt.Errorf("thread %d does not exist", tid)
}
2015-04-23 01:00:42 +00:00
// Returns an array of G structures representing the information
// Delve cares about from the internal runtime G structure.
func (dbp *DebuggedProcess) GoroutinesInfo() ([]*G, error) {
var (
allg []*G
reader = dbp.dwarf.Reader()
)
allglen, err := allglenval(dbp, reader)
if err != nil {
return nil, err
}
reader.Seek(0)
allgentryaddr, err := addressFor(dbp, "runtime.allg", reader)
if err != nil {
return nil, err
}
faddr, err := dbp.CurrentThread.readMemory(uintptr(allgentryaddr), ptrsize)
allgptr := binary.LittleEndian.Uint64(faddr)
for i := uint64(0); i < allglen; i++ {
g, err := parseG(dbp.CurrentThread, allgptr+(i*uint64(ptrsize)), reader)
if err != nil {
return nil, err
}
allg = append(allg, g)
}
return allg, nil
}
2015-04-23 01:00:42 +00:00
// Stop all threads.
2015-04-13 22:17:06 +00:00
func (dbp *DebuggedProcess) Halt() (err error) {
for _, th := range dbp.Threads {
if err := th.Halt(); err != nil {
return err
}
}
return nil
}
// Obtains register values from what Delve considers to be the current
// thread of the traced process.
func (dbp *DebuggedProcess) Registers() (Registers, error) {
return dbp.CurrentThread.Registers()
}
// Returns the PC of the current thread.
2015-04-23 15:40:33 +00:00
func (dbp *DebuggedProcess) PC() (uint64, error) {
return dbp.CurrentThread.PC()
}
// Returns the PC of the current thread.
func (dbp *DebuggedProcess) CurrentBreakpoint() *BreakPoint {
return dbp.CurrentThread.CurrentBreakpoint
}
// Returns the value of the named symbol.
func (dbp *DebuggedProcess) EvalSymbol(name string) (*Variable, error) {
return dbp.CurrentThread.EvalSymbol(name)
}
func (dbp *DebuggedProcess) CallFn(name string, fn func() error) error {
return dbp.CurrentThread.CallFn(name, fn)
}
// Returns a reader for the dwarf data
func (dbp *DebuggedProcess) DwarfReader() *reader.Reader {
return reader.New(dbp.dwarf)
}
2015-04-23 01:00:42 +00:00
// Returns list of source files that comprise the debugged binary.
func (dbp *DebuggedProcess) Sources() map[string]*gosym.Obj {
return dbp.goSymTable.Files
}
2015-04-23 01:00:42 +00:00
// Returns list of functions present in the debugged program.
func (dbp *DebuggedProcess) Funcs() []gosym.Func {
return dbp.goSymTable.Funcs
}
2015-04-23 01:00:42 +00:00
// Converts an instruction address to a file/line/function.
func (dbp *DebuggedProcess) PCToLine(pc uint64) (string, int, *gosym.Func) {
return dbp.goSymTable.PCToLine(pc)
}
// Finds the breakpoint for the given pc.
func (dbp *DebuggedProcess) FindBreakpoint(pc uint64) (*BreakPoint, bool) {
for _, bp := range dbp.HWBreakPoints {
if bp != nil && bp.Addr == pc {
return bp, true
}
}
if bp, ok := dbp.BreakPoints[pc]; ok {
return bp, true
}
return nil, false
}
2015-02-27 23:11:13 +00:00
// Returns a new DebuggedProcess struct.
func newDebugProcess(pid int, attach bool) (*DebuggedProcess, error) {
dbp := DebuggedProcess{
Pid: pid,
Threads: make(map[int]*ThreadContext),
BreakPoints: make(map[uint64]*BreakPoint),
os: new(OSProcessDetails),
ast: source.New(),
2015-02-27 23:11:13 +00:00
}
if attach {
err := sys.PtraceAttach(pid)
if err != nil {
return nil, err
}
_, _, err = wait(pid, 0)
if err != nil {
return nil, err
}
}
proc, err := os.FindProcess(pid)
if err != nil {
return nil, err
}
dbp.Process = proc
err = dbp.LoadInformation()
if err != nil {
return nil, err
}
if err := dbp.updateThreadList(); err != nil {
return nil, err
}
return &dbp, nil
}
func (dbp *DebuggedProcess) clearTempBreakpoints() error {
for _, bp := range dbp.HWBreakPoints {
if bp != nil && bp.Temp {
if _, err := dbp.Clear(bp.Addr); err != nil {
return err
}
}
}
for _, bp := range dbp.BreakPoints {
if !bp.Temp {
continue
}
if _, err := dbp.Clear(bp.Addr); err != nil {
return err
}
}
return nil
}
func (dbp *DebuggedProcess) handleBreakpointOnThread(id int) (*ThreadContext, error) {
2015-04-03 15:52:31 +00:00
thread, ok := dbp.Threads[id]
if !ok {
return nil, fmt.Errorf("could not find thread for %d", id)
2015-04-03 15:52:31 +00:00
}
2015-04-23 15:40:33 +00:00
pc, err := thread.PC()
2015-04-03 15:52:31 +00:00
if err != nil {
return nil, err
2015-04-03 15:52:31 +00:00
}
// Check for hardware breakpoint
for _, bp := range dbp.HWBreakPoints {
if bp != nil && bp.Addr == pc {
thread.CurrentBreakpoint = bp
return thread, nil
2015-04-03 15:52:31 +00:00
}
}
// Check to see if we have hit a software breakpoint.
if bp, ok := dbp.BreakPoints[pc-1]; ok {
thread.CurrentBreakpoint = bp
2015-04-23 15:54:26 +00:00
if err = thread.SetPC(bp.Addr); err != nil {
return nil, err
}
return thread, nil
}
if dbp.halt {
return thread, nil
2015-04-03 15:52:31 +00:00
}
2015-04-23 15:54:26 +00:00
return nil, fmt.Errorf("no breakpoint at %#v", pc)
2015-04-03 15:52:31 +00:00
}
2015-02-27 23:11:13 +00:00
func (dbp *DebuggedProcess) run(fn func() error) error {
if dbp.exited {
return fmt.Errorf("process has already exited")
}
dbp.running = true
dbp.halt = false
for _, th := range dbp.Threads {
th.CurrentBreakpoint = nil
}
defer func() { dbp.running = false }()
if err := fn(); err != nil {
if _, ok := err.(ManualStopError); !ok {
return err
}
}
return nil
}