2014-10-18 01:34:58 +00:00
|
|
|
package proctl
|
|
|
|
|
|
|
|
import (
|
2014-11-08 05:44:24 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2014-10-18 01:34:58 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/derekparker/delve/dwarf/frame"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ThreadContext represents a single thread of execution in the
|
|
|
|
// traced program.
|
|
|
|
type ThreadContext struct {
|
2014-10-25 14:17:05 +00:00
|
|
|
Id int
|
|
|
|
Process *DebuggedProcess
|
|
|
|
Status *syscall.WaitStatus
|
|
|
|
Regs *syscall.PtraceRegs
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains register values from the debugged process.
|
|
|
|
func (thread *ThreadContext) Registers() (*syscall.PtraceRegs, error) {
|
|
|
|
err := syscall.PtraceGetRegs(thread.Id, thread.Regs)
|
|
|
|
if err != nil {
|
2014-11-23 00:57:26 +00:00
|
|
|
return nil, err
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return thread.Regs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (thread *ThreadContext) CurrentPC() (uint64, error) {
|
|
|
|
regs, err := thread.Registers()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return regs.PC(), nil
|
|
|
|
}
|
|
|
|
|
2014-11-08 13:30:22 +00:00
|
|
|
// PrintInfo prints out the thread status
|
|
|
|
// including: PC, tid, file, line, and function.
|
2014-11-08 05:55:25 +00:00
|
|
|
func (thread *ThreadContext) PrintInfo() error {
|
|
|
|
pc, err := thread.CurrentPC()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, l, fn := thread.Process.GoSymTable.PCToLine(pc)
|
2014-11-12 23:12:26 +00:00
|
|
|
if fn != nil {
|
|
|
|
fmt.Printf("Thread %d at %#v %s:%d %s\n", thread.Id, pc, f, l, fn.Name)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Thread %d at %#v\n", thread.Id, pc)
|
|
|
|
}
|
2014-11-08 05:55:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:44:24 +00:00
|
|
|
// Sets a software 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 in order
|
|
|
|
// to initiate any ptrace command. Otherwise, it really doesn't matter
|
|
|
|
// as we're only dealing with threads.
|
|
|
|
func (thread *ThreadContext) Break(addr uintptr) (*BreakPoint, error) {
|
|
|
|
var (
|
|
|
|
int3 = []byte{0xCC}
|
|
|
|
f, l, fn = thread.Process.GoSymTable.PCToLine(uint64(addr))
|
|
|
|
originalData = make([]byte, 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
if fn == nil {
|
|
|
|
return nil, InvalidAddressError{address: addr}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := syscall.PtracePeekData(thread.Id, addr, originalData)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("PEEK ERR")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(originalData, int3) {
|
|
|
|
return nil, BreakPointExistsError{f, l, addr}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = syscall.PtracePokeData(thread.Id, addr, int3)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("POKE ERR")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
breakpoint := &BreakPoint{
|
|
|
|
FunctionName: fn.Name,
|
|
|
|
File: f,
|
|
|
|
Line: l,
|
|
|
|
Addr: uint64(addr),
|
|
|
|
OriginalData: originalData,
|
|
|
|
}
|
|
|
|
|
|
|
|
thread.Process.BreakPoints[uint64(addr)] = breakpoint
|
|
|
|
|
|
|
|
return breakpoint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clears a software breakpoint, and removes it from the process level
|
|
|
|
// break point table.
|
|
|
|
func (thread *ThreadContext) Clear(pc uint64) (*BreakPoint, error) {
|
|
|
|
bp, ok := thread.Process.BreakPoints[pc]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No breakpoint currently set for %#v", pc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := syscall.PtracePokeData(thread.Id, uintptr(bp.Addr), bp.OriginalData); err != nil {
|
2014-11-23 00:57:26 +00:00
|
|
|
return nil, err
|
2014-11-08 05:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(thread.Process.BreakPoints, pc)
|
|
|
|
|
|
|
|
return bp, nil
|
|
|
|
}
|
|
|
|
|
2014-10-18 01:34:58 +00:00
|
|
|
func (thread *ThreadContext) Continue() error {
|
2014-11-08 05:44:24 +00:00
|
|
|
// Check whether we are stopped at a breakpoint, and
|
|
|
|
// if so, single step over it before continuing.
|
|
|
|
regs, err := thread.Registers()
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:44:24 +00:00
|
|
|
if _, ok := thread.Process.BreakPoints[regs.PC()-1]; ok {
|
|
|
|
err := thread.Step()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-18 01:34:58 +00:00
|
|
|
return syscall.PtraceCont(thread.Id, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Steps through thread of execution.
|
|
|
|
func (thread *ThreadContext) Step() (err error) {
|
|
|
|
regs, err := thread.Registers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-25 14:17:05 +00:00
|
|
|
bp, ok := thread.Process.BreakPoints[regs.PC()-1]
|
2014-10-18 01:34:58 +00:00
|
|
|
if ok {
|
|
|
|
// Clear the breakpoint so that we can continue execution.
|
2014-10-25 14:17:05 +00:00
|
|
|
_, err = thread.Process.Clear(bp.Addr)
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset program counter to our restored instruction.
|
|
|
|
regs.SetPC(bp.Addr)
|
|
|
|
err = syscall.PtraceSetRegs(thread.Id, regs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore breakpoint now that we have passed it.
|
|
|
|
defer func() {
|
2014-11-08 05:44:24 +00:00
|
|
|
_, err = thread.Break(uintptr(bp.Addr))
|
2014-10-18 01:34:58 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = syscall.PtraceSingleStep(thread.Id)
|
|
|
|
if err != nil {
|
2014-10-25 17:40:55 +00:00
|
|
|
return fmt.Errorf("step failed: %s", err.Error())
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:44:08 +00:00
|
|
|
_, _, err = timeoutWait(thread, 0)
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
2014-11-23 00:57:26 +00:00
|
|
|
return err
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Steps through thread of execution.
|
|
|
|
func (thread *ThreadContext) Next() (err error) {
|
|
|
|
pc, err := thread.CurrentPC()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-25 14:17:05 +00:00
|
|
|
if _, ok := thread.Process.BreakPoints[pc-1]; ok {
|
2014-11-08 05:44:24 +00:00
|
|
|
pc-- // Decrement PC to account for BreakPoint
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, l, _ := thread.Process.GoSymTable.PCToLine(pc)
|
|
|
|
fde, err := thread.Process.FrameEntries.FDEForPC(pc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
step := func() (uint64, error) {
|
|
|
|
err = thread.Step()
|
|
|
|
if err != nil {
|
2014-11-23 00:57:26 +00:00
|
|
|
return 0, err
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return thread.CurrentPC()
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:44:24 +00:00
|
|
|
ret := thread.ReturnAddressFromOffset(fde.ReturnAddressOffset(pc))
|
2014-10-18 01:34:58 +00:00
|
|
|
for {
|
|
|
|
pc, err = step()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fde.Cover(pc) && pc != ret {
|
2014-11-08 05:44:24 +00:00
|
|
|
err := thread.continueToReturnAddress(pc, fde)
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
2014-11-08 05:44:24 +00:00
|
|
|
if _, ok := err.(InvalidAddressError); !ok {
|
|
|
|
return err
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pc, _ = thread.CurrentPC()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, nl, _ := thread.Process.GoSymTable.PCToLine(pc)
|
|
|
|
if nl != l {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (thread *ThreadContext) continueToReturnAddress(pc uint64, fde *frame.FrameDescriptionEntry) error {
|
|
|
|
for !fde.Cover(pc) {
|
|
|
|
// Our offset here is be 0 because we
|
|
|
|
// have stepped into the first instruction
|
|
|
|
// of this function. Therefore the function
|
|
|
|
// has not had a chance to modify its' stack
|
|
|
|
// and change our offset.
|
2014-11-08 05:44:24 +00:00
|
|
|
addr := thread.ReturnAddressFromOffset(0)
|
|
|
|
bp, err := thread.Break(uintptr(addr))
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(BreakPointExistsError); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 05:44:24 +00:00
|
|
|
bp.temp = true
|
2014-10-18 01:34:58 +00:00
|
|
|
|
|
|
|
err = thread.Continue()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:44:24 +00:00
|
|
|
if _, _, err := trapWait(thread.Process, thread.Id, 0); err != nil {
|
2014-10-18 01:34:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:44:24 +00:00
|
|
|
if err := thread.clearTempBreakpoint(bp.Addr); err != nil {
|
2014-10-18 01:34:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, _ = thread.CurrentPC()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:44:24 +00:00
|
|
|
// Takes an offset from RSP and returns the address of the
|
|
|
|
// instruction the currect function is going to return to.
|
|
|
|
func (thread *ThreadContext) ReturnAddressFromOffset(offset int64) uint64 {
|
|
|
|
regs, err := thread.Registers()
|
|
|
|
if err != nil {
|
|
|
|
panic("Could not obtain register values")
|
|
|
|
}
|
|
|
|
|
|
|
|
retaddr := int64(regs.Rsp) + offset
|
|
|
|
data := make([]byte, 8)
|
|
|
|
syscall.PtracePeekText(thread.Id, uintptr(retaddr), data)
|
|
|
|
return binary.LittleEndian.Uint64(data)
|
|
|
|
}
|
|
|
|
|
2014-10-18 01:34:58 +00:00
|
|
|
func (thread *ThreadContext) clearTempBreakpoint(pc uint64) error {
|
2014-10-25 14:17:05 +00:00
|
|
|
if bp, ok := thread.Process.BreakPoints[pc]; ok {
|
2014-11-08 05:44:24 +00:00
|
|
|
_, err := thread.Clear(bp.Addr)
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset program counter to our restored instruction.
|
2014-11-08 05:44:24 +00:00
|
|
|
regs, err := thread.Registers()
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
regs.SetPC(bp.Addr)
|
|
|
|
return syscall.PtraceSetRegs(thread.Id, regs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func threadIds(pid int) []int {
|
|
|
|
var threads []int
|
|
|
|
dir, err := os.Open(fmt.Sprintf("/proc/%d/task", pid))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
names, err := dir.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, strid := range names {
|
|
|
|
tid, err := strconv.Atoi(strid)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:10:45 +00:00
|
|
|
if tid != pid {
|
|
|
|
threads = append(threads, tid)
|
|
|
|
}
|
2014-10-18 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return threads
|
|
|
|
}
|