2014-05-20 18:23:35 +00:00
|
|
|
package proctl
|
|
|
|
|
|
|
|
import (
|
2014-05-27 18:33:49 +00:00
|
|
|
"bytes"
|
2014-05-20 18:23:35 +00:00
|
|
|
"os/exec"
|
2014-06-09 19:56:10 +00:00
|
|
|
"runtime"
|
2014-05-23 19:07:10 +00:00
|
|
|
"syscall"
|
2014-05-20 18:23:35 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
type testfunc func(p *DebuggedProcess)
|
|
|
|
|
2014-05-27 18:33:49 +00:00
|
|
|
func dataAtAddr(pid int, addr uint64) ([]byte, error) {
|
|
|
|
data := make([]byte, 1)
|
|
|
|
_, err := syscall.PtracePeekData(pid, uintptr(addr), data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func getRegisters(p *DebuggedProcess, t *testing.T) *syscall.PtraceRegs {
|
|
|
|
regs, err := p.Registers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Registers():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return regs
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func withTestProcess(name string, t *testing.T, fn testfunc) {
|
2014-06-09 19:56:10 +00:00
|
|
|
runtime.LockOSThread()
|
|
|
|
cmd, err := startTestProcess(name)
|
2014-05-23 19:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Starting test process:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pid := cmd.Process.Pid
|
|
|
|
p, err := NewDebugProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("NewDebugProcess():", err)
|
|
|
|
}
|
2014-05-30 15:12:18 +00:00
|
|
|
defer cmd.Process.Kill()
|
2014-05-23 19:07:10 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
fn(p)
|
2014-05-23 19:07:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 19:56:10 +00:00
|
|
|
func startTestProcess(name string) (*exec.Cmd, error) {
|
2014-06-09 15:55:18 +00:00
|
|
|
cmd := exec.Command("../_fixtures/" + name)
|
2014-05-20 18:23:35 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
err := cmd.Start()
|
2014-05-20 18:23:35 +00:00
|
|
|
if err != nil {
|
2014-05-30 15:12:18 +00:00
|
|
|
return nil, err
|
2014-05-20 18:23:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-05-23 19:07:10 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func TestAttachProcess(t *testing.T) {
|
|
|
|
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
|
|
|
if !p.ProcessState.Sys().(syscall.WaitStatus).Stopped() {
|
|
|
|
t.Errorf("Process was not stopped correctly")
|
|
|
|
}
|
|
|
|
})
|
2014-05-20 18:23:35 +00:00
|
|
|
}
|
2014-05-20 18:23:36 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func TestStep(t *testing.T) {
|
|
|
|
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
2014-06-09 19:56:10 +00:00
|
|
|
if p.ProcessState.Exited() {
|
|
|
|
t.Fatal("Process already exited")
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
regs := getRegisters(p, t)
|
|
|
|
rip := regs.PC()
|
2014-05-20 18:23:36 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
err := p.Step()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Step():", err)
|
|
|
|
}
|
2014-05-20 18:23:36 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
regs = getRegisters(p, t)
|
2014-05-20 18:23:36 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
if rip >= regs.PC() {
|
|
|
|
t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-05-20 18:23:36 +00:00
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func TestContinue(t *testing.T) {
|
|
|
|
withTestProcess("continuetestprog", t, func(p *DebuggedProcess) {
|
|
|
|
if p.ProcessState.Exited() {
|
|
|
|
t.Fatal("Process already exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Continue()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Continue():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ProcessState.Success() {
|
|
|
|
t.Fatal("Process did not exit successfully")
|
|
|
|
}
|
|
|
|
})
|
2014-05-20 18:23:36 +00:00
|
|
|
}
|
2014-05-24 00:44:54 +00:00
|
|
|
|
|
|
|
func TestBreakPoint(t *testing.T) {
|
2014-05-30 15:12:18 +00:00
|
|
|
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
|
|
|
sleepytimefunc := p.GoSymTable.LookupFunc("main.sleepytime")
|
|
|
|
sleepyaddr := sleepytimefunc.Entry
|
|
|
|
|
|
|
|
bp, err := p.Break(uintptr(sleepyaddr))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Break():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
breakpc := bp.Addr + 1
|
|
|
|
err = p.Continue()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Continue():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regs := getRegisters(p, t)
|
|
|
|
|
|
|
|
pc := regs.PC()
|
|
|
|
if pc != breakpc {
|
|
|
|
t.Fatalf("Break not respected:\nPC:%d\nFN:%d\n", pc, breakpc)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Step()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regs = getRegisters(p, t)
|
|
|
|
|
|
|
|
pc = regs.PC()
|
|
|
|
if pc == breakpc {
|
|
|
|
t.Fatalf("Step not respected:\nPC:%d\nFN:%d\n", pc, breakpc)
|
|
|
|
}
|
|
|
|
})
|
2014-05-24 16:22:06 +00:00
|
|
|
}
|
2014-05-24 16:36:18 +00:00
|
|
|
|
|
|
|
func TestBreakPointWithNonExistantFunction(t *testing.T) {
|
2014-05-30 15:12:18 +00:00
|
|
|
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
|
|
|
_, err := p.Break(uintptr(0))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should not be able to break at non existant function")
|
|
|
|
}
|
|
|
|
})
|
2014-05-24 16:36:18 +00:00
|
|
|
}
|
2014-05-27 18:33:49 +00:00
|
|
|
|
|
|
|
func TestClearBreakPoint(t *testing.T) {
|
2014-05-30 15:12:18 +00:00
|
|
|
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
|
|
|
fn := p.GoSymTable.LookupFunc("main.sleepytime")
|
|
|
|
bp, err := p.Break(uintptr(fn.Entry))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Break():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
int3, err := dataAtAddr(p.Pid, bp.Addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bp, err = p.Clear(fn.Entry)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Break():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := dataAtAddr(p.Pid, bp.Addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(data, int3) {
|
|
|
|
t.Fatalf("Breakpoint was not cleared data: %#v, int3: %#v", data, int3)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.BreakPoints) != 0 {
|
|
|
|
t.Fatal("Breakpoint not removed internally")
|
|
|
|
}
|
|
|
|
})
|
2014-05-27 18:33:49 +00:00
|
|
|
}
|