2014-12-09 16:51:17 +00:00
|
|
|
package proctl
|
2014-05-20 18:23:35 +00:00
|
|
|
|
|
|
|
import (
|
2014-05-27 18:33:49 +00:00
|
|
|
"bytes"
|
2014-12-09 16:51:17 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-06-29 16:52:21 +00:00
|
|
|
"path/filepath"
|
2014-12-09 16:51:17 +00:00
|
|
|
"runtime"
|
2014-05-20 18:23:35 +00:00
|
|
|
"testing"
|
2014-06-25 19:06:04 +00:00
|
|
|
)
|
2014-05-30 15:12:18 +00:00
|
|
|
|
2014-12-09 16:51:17 +00:00
|
|
|
func withTestProcess(name string, t *testing.T, fn func(p *DebuggedProcess)) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
base := filepath.Base(name)
|
|
|
|
if err := exec.Command("go", "build", "-gcflags=-N -l", "-o", base, name+".go").Run(); err != nil {
|
|
|
|
t.Fatalf("Could not compile %s due to %s", name, err)
|
|
|
|
}
|
|
|
|
defer os.Remove("./" + base)
|
|
|
|
|
|
|
|
p, err := Launch([]string{"./" + base})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Launch():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer p.Process.Kill()
|
|
|
|
|
|
|
|
fn(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRegisters(p *DebuggedProcess, t *testing.T) Registers {
|
2014-12-09 16:35:55 +00:00
|
|
|
regs, err := p.Registers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Registers():", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return regs
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
func dataAtAddr(thread *ThreadContext, addr uint64) ([]byte, error) {
|
2014-05-27 18:33:49 +00:00
|
|
|
data := make([]byte, 1)
|
2015-01-14 02:37:10 +00:00
|
|
|
_, err := readMemory(thread, uintptr(addr), data)
|
2014-05-27 18:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:52:21 +00:00
|
|
|
func assertNoError(err error, t *testing.T, s string) {
|
|
|
|
if err != nil {
|
2015-01-21 21:26:01 +00:00
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
fname := filepath.Base(file)
|
|
|
|
t.Fatalf("failed assertion at %s:%d: %s : %s\n", fname, line, s, err)
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 16:51:17 +00:00
|
|
|
func currentPC(p *DebuggedProcess, t *testing.T) uint64 {
|
2014-06-29 16:52:21 +00:00
|
|
|
pc, err := p.CurrentPC()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pc
|
|
|
|
}
|
|
|
|
|
2014-12-09 16:51:17 +00:00
|
|
|
func currentLineNumber(p *DebuggedProcess, t *testing.T) (string, int) {
|
2014-06-29 16:52:21 +00:00
|
|
|
pc := currentPC(p, t)
|
2014-10-07 17:25:33 +00:00
|
|
|
f, l, _ := p.GoSymTable.PCToLine(pc)
|
2014-06-29 16:52:21 +00:00
|
|
|
|
2014-10-07 17:25:33 +00:00
|
|
|
return f, l
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 22:59:51 +00:00
|
|
|
func TestExit(t *testing.T) {
|
|
|
|
withTestProcess("../_fixtures/continuetestprog", t, func(p *DebuggedProcess) {
|
|
|
|
err := p.Continue()
|
|
|
|
pe, ok := err.(ProcessExitedError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Continue() returned unexpected error type")
|
|
|
|
}
|
|
|
|
if pe.Status != 0 {
|
|
|
|
t.Errorf("Unexpected error status: %d", pe.Status)
|
|
|
|
}
|
|
|
|
if pe.Pid != p.Pid {
|
|
|
|
t.Errorf("Unexpected process id: %d", pe.Pid)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func TestStep(t *testing.T) {
|
2014-12-09 16:51:17 +00:00
|
|
|
withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
|
2014-11-27 02:35:53 +00:00
|
|
|
helloworldfunc := p.GoSymTable.LookupFunc("main.helloworld")
|
|
|
|
helloworldaddr := helloworldfunc.Entry
|
|
|
|
|
2015-01-01 13:23:55 +00:00
|
|
|
_, err := p.Break(helloworldaddr)
|
2014-11-27 02:35:53 +00:00
|
|
|
assertNoError(err, t, "Break()")
|
|
|
|
assertNoError(p.Continue(), t, "Continue()")
|
|
|
|
|
2014-12-09 16:35:55 +00:00
|
|
|
regs := getRegisters(p, t)
|
2014-05-30 15:12:18 +00:00
|
|
|
rip := regs.PC()
|
2014-05-20 18:23:36 +00:00
|
|
|
|
2014-11-27 02:35:53 +00:00
|
|
|
err = p.Step()
|
2014-09-06 23:53:22 +00:00
|
|
|
assertNoError(err, t, "Step()")
|
2014-05-20 18:23:36 +00:00
|
|
|
|
2014-12-09 16:35:55 +00:00
|
|
|
regs = getRegisters(p, t)
|
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-24 00:44:54 +00:00
|
|
|
func TestBreakPoint(t *testing.T) {
|
2014-12-09 16:51:17 +00:00
|
|
|
withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
|
2015-01-14 02:37:10 +00:00
|
|
|
helloworldfunc := p.GoSymTable.LookupFunc("main.helloworld")
|
|
|
|
helloworldaddr := helloworldfunc.Entry
|
2014-05-30 15:12:18 +00:00
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
bp, err := p.Break(helloworldaddr)
|
2014-09-06 23:53:22 +00:00
|
|
|
assertNoError(err, t, "Break()")
|
2015-01-14 02:37:10 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue()")
|
2014-05-30 15:12:18 +00:00
|
|
|
|
2014-10-18 01:34:58 +00:00
|
|
|
pc, err := p.CurrentPC()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-30 15:12:18 +00:00
|
|
|
|
2015-02-28 03:35:26 +00:00
|
|
|
if pc-1 != bp.Addr && pc != bp.Addr {
|
2014-10-18 01:34:58 +00:00
|
|
|
f, l, _ := p.GoSymTable.PCToLine(pc)
|
2015-01-14 02:37:10 +00:00
|
|
|
t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
|
2014-05-30 15:12:18 +00:00
|
|
|
}
|
|
|
|
})
|
2014-05-24 16:22:06 +00:00
|
|
|
}
|
2014-05-24 16:36:18 +00:00
|
|
|
|
2014-10-18 01:34:58 +00:00
|
|
|
func TestBreakPointInSeperateGoRoutine(t *testing.T) {
|
2014-12-09 16:51:17 +00:00
|
|
|
withTestProcess("../_fixtures/testthreads", t, func(p *DebuggedProcess) {
|
2014-10-18 01:34:58 +00:00
|
|
|
fn := p.GoSymTable.LookupFunc("main.anotherthread")
|
|
|
|
if fn == nil {
|
|
|
|
t.Fatal("No fn exists")
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:23:55 +00:00
|
|
|
_, err := p.Break(fn.Entry)
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Continue()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, err := p.CurrentPC()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, l, _ := p.GoSymTable.PCToLine(pc)
|
|
|
|
if f != "testthreads.go" && l != 8 {
|
|
|
|
t.Fatal("Program did not hit breakpoint")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-05-24 16:36:18 +00:00
|
|
|
func TestBreakPointWithNonExistantFunction(t *testing.T) {
|
2014-12-09 16:51:17 +00:00
|
|
|
withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
|
2015-01-01 13:23:55 +00:00
|
|
|
_, err := p.Break(0)
|
2014-05-30 15:12:18 +00:00
|
|
|
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-12-09 16:51:17 +00:00
|
|
|
withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
|
2014-05-30 15:12:18 +00:00
|
|
|
fn := p.GoSymTable.LookupFunc("main.sleepytime")
|
2015-01-01 13:23:55 +00:00
|
|
|
bp, err := p.Break(fn.Entry)
|
2014-09-06 23:53:22 +00:00
|
|
|
assertNoError(err, t, "Break()")
|
2014-05-30 15:12:18 +00:00
|
|
|
|
|
|
|
bp, err = p.Clear(fn.Entry)
|
2014-09-06 23:53:22 +00:00
|
|
|
assertNoError(err, t, "Clear()")
|
2014-05-30 15:12:18 +00:00
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
data, err := dataAtAddr(p.CurrentThread, bp.Addr)
|
2014-05-30 15:12:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-10 23:33:06 +00:00
|
|
|
int3 := []byte{0xcc}
|
2014-05-30 15:12:18 +00:00
|
|
|
if bytes.Equal(data, int3) {
|
|
|
|
t.Fatalf("Breakpoint was not cleared data: %#v, int3: %#v", data, int3)
|
|
|
|
}
|
|
|
|
|
2014-10-25 14:17:05 +00:00
|
|
|
if len(p.BreakPoints) != 0 {
|
2014-05-30 15:12:18 +00:00
|
|
|
t.Fatal("Breakpoint not removed internally")
|
|
|
|
}
|
|
|
|
})
|
2014-05-27 18:33:49 +00:00
|
|
|
}
|
2014-06-29 16:52:21 +00:00
|
|
|
|
|
|
|
func TestNext(t *testing.T) {
|
|
|
|
var (
|
2014-07-30 00:00:24 +00:00
|
|
|
err error
|
|
|
|
executablePath = "../_fixtures/testnextprog"
|
2014-06-29 16:52:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
begin, end int
|
|
|
|
}{
|
2014-10-08 03:35:57 +00:00
|
|
|
{19, 20},
|
|
|
|
{20, 23},
|
|
|
|
{23, 24},
|
|
|
|
{24, 26},
|
|
|
|
{26, 31},
|
|
|
|
{31, 23},
|
|
|
|
{23, 24},
|
|
|
|
{24, 26},
|
|
|
|
{26, 31},
|
|
|
|
{31, 23},
|
|
|
|
{23, 24},
|
|
|
|
{24, 26},
|
|
|
|
{26, 27},
|
|
|
|
{27, 34},
|
|
|
|
{34, 35},
|
2014-10-14 00:04:38 +00:00
|
|
|
{35, 41},
|
2014-10-11 02:00:07 +00:00
|
|
|
{41, 40},
|
2014-10-14 00:04:38 +00:00
|
|
|
{40, 41},
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fp, err := filepath.Abs("../_fixtures/testnextprog.go")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-09 16:51:17 +00:00
|
|
|
withTestProcess(executablePath, t, func(p *DebuggedProcess) {
|
2014-06-29 16:52:21 +00:00
|
|
|
pc, _, _ := p.GoSymTable.LineToPC(fp, testcases[0].begin)
|
2015-01-01 13:23:55 +00:00
|
|
|
_, err := p.Break(pc)
|
2014-09-06 23:56:25 +00:00
|
|
|
assertNoError(err, t, "Break()")
|
|
|
|
assertNoError(p.Continue(), t, "Continue()")
|
2014-06-29 16:52:21 +00:00
|
|
|
|
2014-12-29 02:48:58 +00:00
|
|
|
f, ln := currentLineNumber(p, t)
|
2014-06-29 16:52:21 +00:00
|
|
|
for _, tc := range testcases {
|
|
|
|
if ln != tc.begin {
|
2014-10-07 17:25:33 +00:00
|
|
|
t.Fatalf("Program not stopped at correct spot expected %d was %s:%d", tc.begin, f, ln)
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertNoError(p.Next(), t, "Next() returned an error")
|
|
|
|
|
2014-10-07 17:25:33 +00:00
|
|
|
f, ln = currentLineNumber(p, t)
|
2014-06-29 16:52:21 +00:00
|
|
|
if ln != tc.end {
|
2014-10-07 17:25:33 +00:00
|
|
|
t.Fatalf("Program did not continue to correct next location expected %d was %s:%d", tc.end, f, ln)
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-17 03:37:48 +00:00
|
|
|
|
2015-01-10 23:33:06 +00:00
|
|
|
p.Clear(pc)
|
|
|
|
if len(p.BreakPoints) != 0 {
|
|
|
|
t.Fatal("Not all breakpoints were cleaned up", len(p.HWBreakPoints))
|
|
|
|
}
|
|
|
|
for _, bp := range p.HWBreakPoints {
|
|
|
|
if bp != nil {
|
|
|
|
t.Fatal("Not all breakpoints were cleaned up", bp.Addr)
|
|
|
|
}
|
2014-09-17 03:37:48 +00:00
|
|
|
}
|
2014-06-29 16:52:21 +00:00
|
|
|
})
|
|
|
|
}
|
2014-12-09 16:51:17 +00:00
|
|
|
|
|
|
|
func TestFindReturnAddress(t *testing.T) {
|
|
|
|
var testfile, _ = filepath.Abs("../_fixtures/testnextprog")
|
|
|
|
|
|
|
|
withTestProcess(testfile, t, func(p *DebuggedProcess) {
|
|
|
|
var (
|
|
|
|
fdes = p.FrameEntries
|
|
|
|
gsd = p.GoSymTable
|
|
|
|
)
|
|
|
|
|
|
|
|
testsourcefile := testfile + ".go"
|
|
|
|
start, _, err := gsd.LineToPC(testsourcefile, 24)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:23:55 +00:00
|
|
|
_, err = p.Break(start)
|
2014-12-09 16:51:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Continue()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regs, err := p.Registers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fde, err := fdes.FDEForPC(start)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := fde.ReturnAddressOffset(start)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := uint64(int64(regs.SP()) + ret)
|
|
|
|
data := make([]byte, 8)
|
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
readMemory(p.CurrentThread, uintptr(addr), data)
|
2014-12-09 16:51:17 +00:00
|
|
|
addr = binary.LittleEndian.Uint64(data)
|
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
linuxExpected := uint64(0x400fbc)
|
|
|
|
darwinExpected := uint64(0x23bc)
|
|
|
|
if addr != linuxExpected && addr != darwinExpected {
|
|
|
|
t.Fatalf("return address not found correctly, expected (linux) %#v or (darwin) %#v got %#v", linuxExpected, darwinExpected, addr)
|
2014-12-09 16:51:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-03-26 18:15:35 +00:00
|
|
|
|
|
|
|
func TestSwitchThread(t *testing.T) {
|
|
|
|
var testfile, _ = filepath.Abs("../_fixtures/testnextprog")
|
|
|
|
|
|
|
|
withTestProcess(testfile, t, func(p *DebuggedProcess) {
|
|
|
|
// With invalid thread id
|
|
|
|
err := p.SwitchThread(-1)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error for invalid thread id")
|
|
|
|
}
|
|
|
|
pc, err := p.FindLocation("main.main")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = p.Break(pc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = p.Continue()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var nt int
|
|
|
|
ct := p.CurrentThread.Id
|
|
|
|
for tid, _ := range p.Threads {
|
|
|
|
if tid != ct {
|
|
|
|
nt = tid
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nt == 0 {
|
|
|
|
t.Fatal("could not find thread to switch to")
|
|
|
|
}
|
|
|
|
// With valid thread id
|
|
|
|
err = p.SwitchThread(nt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if p.CurrentThread.Id != nt {
|
|
|
|
t.Fatal("Did not switch threads")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|