delve/proctl/proctl_linux_amd64.go

259 lines
5.3 KiB
Go
Raw Normal View History

2014-05-21 15:23:14 +00:00
// Package proctl provides functions for attaching to and manipulating
// a process during the debug session.
2014-05-20 18:23:35 +00:00
package proctl
import (
"debug/elf"
"debug/gosym"
2014-05-20 18:23:35 +00:00
"fmt"
"os"
"syscall"
)
2014-05-21 15:23:14 +00:00
// Struct representing a debugged process. Holds onto pid, register values,
// process struct and process state.
2014-05-20 18:23:35 +00:00
type DebuggedProcess struct {
2014-05-20 18:23:36 +00:00
Pid int
Regs *syscall.PtraceRegs
Process *os.Process
ProcessState *os.ProcessState
Executable *elf.File
Symbols []elf.Symbol
GoSymTable *gosym.Table
2014-05-24 16:22:06 +00:00
BreakPoints map[string]*BreakPoint
}
2014-05-27 18:20:10 +00:00
// Represents a single breakpoint. Stores information on the break
// point include the byte of data that originally was stored at that
// address.
2014-05-24 16:22:06 +00:00
type BreakPoint struct {
FunctionName string
File string
2014-05-24 16:22:06 +00:00
Line int
Addr uint64
OriginalData []byte
2014-05-20 18:23:35 +00:00
}
2014-05-21 15:23:14 +00:00
// Returns a new DebuggedProcess struct with sensible defaults.
2014-05-20 18:23:35 +00:00
func NewDebugProcess(pid int) (*DebuggedProcess, error) {
proc, err := os.FindProcess(pid)
2014-05-20 18:23:35 +00:00
if err != nil {
return nil, err
}
err = syscall.PtraceAttach(pid)
2014-05-20 18:23:35 +00:00
if err != nil {
return nil, err
}
2014-05-20 18:23:36 +00:00
ps, err := proc.Wait()
2014-05-20 18:23:35 +00:00
if err != nil {
return nil, err
}
2014-05-20 18:23:36 +00:00
debuggedProc := DebuggedProcess{
Pid: pid,
Regs: &syscall.PtraceRegs{},
Process: proc,
ProcessState: ps,
2014-05-24 16:22:06 +00:00
BreakPoints: make(map[string]*BreakPoint),
2014-05-20 18:23:36 +00:00
}
err = debuggedProc.LoadInformation()
if err != nil {
return nil, err
}
2014-05-20 18:23:35 +00:00
return &debuggedProc, nil
}
2014-05-27 18:20:10 +00:00
// Finds the executable from /proc/<pid>/exe and then
// uses that to parse the Go symbol table.
func (dbp *DebuggedProcess) LoadInformation() error {
err := dbp.findExecutable()
if err != nil {
return err
}
err = dbp.obtainGoSymbols()
if err != nil {
return err
}
return nil
}
2014-05-21 15:23:14 +00:00
// Obtains register values from the debugged process.
2014-05-20 18:23:35 +00:00
func (dbp *DebuggedProcess) Registers() (*syscall.PtraceRegs, error) {
err := syscall.PtraceGetRegs(dbp.Pid, dbp.Regs)
if err != nil {
return nil, fmt.Errorf("Registers():", err)
}
return dbp.Regs, nil
}
// Sets a breakpoint in the running process.
2014-05-27 23:15:18 +00:00
func (dbp *DebuggedProcess) Break(addr uintptr) (*BreakPoint, error) {
var (
2014-05-29 15:19:42 +00:00
int3 = []byte{0xCC}
f, l, fn = dbp.GoSymTable.PCToLine(uint64(addr))
originalData = make([]byte, 1)
)
2014-05-29 15:19:42 +00:00
_, err := syscall.PtracePeekData(dbp.Pid, addr, originalData)
if err != nil {
return nil, err
2014-05-24 16:22:06 +00:00
}
_, err = syscall.PtracePokeData(dbp.Pid, addr, int3)
if err != nil {
2014-05-24 16:22:06 +00:00
return nil, err
}
2014-05-24 16:22:06 +00:00
breakpoint := &BreakPoint{
FunctionName: fn.Name,
File: f,
Line: l,
2014-05-27 23:15:18 +00:00
Addr: uint64(addr),
2014-05-29 15:19:42 +00:00
OriginalData: originalData,
2014-05-24 16:22:06 +00:00
}
2014-05-27 23:15:18 +00:00
fname := fmt.Sprintf("%s:%d", f, l)
2014-05-24 16:22:06 +00:00
dbp.BreakPoints[fname] = breakpoint
return breakpoint, nil
}
// Clears a breakpoint.
2014-05-27 23:15:18 +00:00
func (dbp *DebuggedProcess) Clear(pc uint64) (*BreakPoint, error) {
bp, ok := dbp.PCtoBP(pc)
if !ok {
2014-05-27 23:15:18 +00:00
return nil, fmt.Errorf("No breakpoint currently set for %s", bp.FunctionName)
}
err := dbp.restoreInstruction(bp.Addr, bp.OriginalData)
if err != nil {
2014-05-27 23:15:18 +00:00
return nil, err
}
2014-05-28 22:47:29 +00:00
delete(dbp.BreakPoints, fmt.Sprintf("%s:%d", bp.File, bp.Line))
2014-05-27 23:15:18 +00:00
return bp, nil
}
2014-05-21 15:23:14 +00:00
// Steps through process.
2014-05-20 18:23:35 +00:00
func (dbp *DebuggedProcess) Step() error {
regs, err := dbp.Registers()
if err != nil {
return err
}
2014-05-29 14:34:37 +00:00
bp, ok := dbp.PCtoBP(regs.PC() - 1)
if ok {
2014-05-29 14:34:37 +00:00
_, err = dbp.Clear(bp.Addr)
if err != nil {
return err
}
2014-05-29 14:34:37 +00:00
// Reset instruction pointer to our restored instruction.
regs.Rip -= 1
syscall.PtraceSetRegs(dbp.Pid, regs)
}
err = dbp.handleResult(syscall.PtraceSingleStep(dbp.Pid))
if err != nil {
return fmt.Errorf("step failed: ", err.Error())
}
// Restore breakpoint
if ok {
2014-05-27 23:15:18 +00:00
_, err := dbp.Break(uintptr(bp.Addr))
if err != nil {
return err
}
}
return nil
2014-05-20 18:23:35 +00:00
}
2014-05-20 18:23:36 +00:00
2014-05-21 15:23:14 +00:00
// Continue process until next breakpoint.
2014-05-20 18:23:36 +00:00
func (dbp *DebuggedProcess) Continue() error {
// Stepping first will ensure we are able to continue
// past a breakpoint if that's currently where we are stopped.
err := dbp.Step()
if err != nil {
return err
}
2014-05-21 15:23:45 +00:00
return dbp.handleResult(syscall.PtraceCont(dbp.Pid, 0))
2014-05-20 20:15:52 +00:00
}
2014-05-21 15:23:45 +00:00
func (dbp *DebuggedProcess) handleResult(err error) error {
2014-05-20 18:23:36 +00:00
if err != nil {
return err
}
ps, err := dbp.Process.Wait()
if err != nil {
return err
}
dbp.ProcessState = ps
return nil
}
func (dbp *DebuggedProcess) findExecutable() error {
procpath := fmt.Sprintf("/proc/%d/exe", dbp.Pid)
f, err := os.Open(procpath)
if err != nil {
return err
}
elffile, err := elf.NewFile(f)
if err != nil {
return err
}
dbp.Executable = elffile
return nil
}
func (dbp *DebuggedProcess) obtainGoSymbols() error {
symdat, err := dbp.Executable.Section(".gosymtab").Data()
if err != nil {
return err
}
pclndat, err := dbp.Executable.Section(".gopclntab").Data()
if err != nil {
return err
}
pcln := gosym.NewLineTable(pclndat, dbp.Executable.Section(".text").Addr)
tab, err := gosym.NewTable(symdat, pcln)
if err != nil {
return err
}
dbp.GoSymTable = tab
return nil
}
2014-05-27 18:20:10 +00:00
// Converts a program counter value into a breakpoint, if one was set
// for the function containing pc.
func (dbp *DebuggedProcess) PCtoBP(pc uint64) (*BreakPoint, bool) {
2014-05-27 23:15:18 +00:00
f, l, _ := dbp.GoSymTable.PCToLine(pc)
bp, ok := dbp.BreakPoints[fmt.Sprintf("%s:%d", f, l)]
return bp, ok
}
func (dbp *DebuggedProcess) restoreInstruction(pc uint64, data []byte) error {
_, err := syscall.PtracePokeData(dbp.Pid, uintptr(pc), data)
return err
}