2014-06-25 19:06:04 +00:00
|
|
|
package proctl_test
|
2014-05-20 18:23:35 +00:00
|
|
|
|
|
|
|
import (
|
2014-05-27 18:33:49 +00:00
|
|
|
"bytes"
|
2014-06-29 16:52:21 +00:00
|
|
|
"path/filepath"
|
2014-05-23 19:07:10 +00:00
|
|
|
"syscall"
|
2014-05-20 18:23:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2014-10-15 14:21:46 +00:00
|
|
|
"github.com/derekparker/delve/helper"
|
|
|
|
"github.com/derekparker/delve/proctl"
|
2014-06-25 19:06:04 +00:00
|
|
|
)
|
2014-05-30 15:12:18 +00:00
|
|
|
|
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-06-29 16:52:21 +00:00
|
|
|
func assertNoError(err error, t *testing.T, s string) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(s, ":", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func currentPC(p *proctl.DebuggedProcess, t *testing.T) uint64 {
|
|
|
|
pc, err := p.CurrentPC()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pc
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:25:33 +00:00
|
|
|
func currentLineNumber(p *proctl.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
|
|
|
}
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func TestStep(t *testing.T) {
|
2014-06-29 16:52:21 +00:00
|
|
|
helper.WithTestProcess("../_fixtures/testprog", t, func(p *proctl.DebuggedProcess) {
|
2014-11-27 02:35:53 +00:00
|
|
|
helloworldfunc := p.GoSymTable.LookupFunc("main.helloworld")
|
|
|
|
helloworldaddr := helloworldfunc.Entry
|
|
|
|
|
|
|
|
_, err := p.Break(uintptr(helloworldaddr))
|
|
|
|
assertNoError(err, t, "Break()")
|
|
|
|
assertNoError(p.Continue(), t, "Continue()")
|
|
|
|
|
2014-06-25 19:06:04 +00:00
|
|
|
regs := helper.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-06-25 19:06:04 +00:00
|
|
|
regs = helper.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-30 15:12:18 +00:00
|
|
|
func TestContinue(t *testing.T) {
|
2014-06-29 16:52:21 +00:00
|
|
|
helper.WithTestProcess("../_fixtures/continuetestprog", t, func(p *proctl.DebuggedProcess) {
|
2014-05-30 15:12:18 +00:00
|
|
|
err := p.Continue()
|
2014-10-18 01:34:58 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(proctl.ProcessExitedError); !ok {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 15:12:18 +00:00
|
|
|
|
2014-10-18 01:34:58 +00:00
|
|
|
if p.Status().ExitStatus() != 0 {
|
|
|
|
t.Fatal("Process did not exit successfully", p.Status().ExitStatus())
|
2014-05-30 15:12:18 +00:00
|
|
|
}
|
|
|
|
})
|
2014-05-20 18:23:36 +00:00
|
|
|
}
|
2014-05-24 00:44:54 +00:00
|
|
|
|
|
|
|
func TestBreakPoint(t *testing.T) {
|
2014-06-29 16:52:21 +00:00
|
|
|
helper.WithTestProcess("../_fixtures/testprog", t, func(p *proctl.DebuggedProcess) {
|
2014-10-18 01:34:58 +00:00
|
|
|
sleepytimefunc := p.GoSymTable.LookupFunc("main.helloworld")
|
2014-05-30 15:12:18 +00:00
|
|
|
sleepyaddr := sleepytimefunc.Entry
|
|
|
|
|
|
|
|
bp, err := p.Break(uintptr(sleepyaddr))
|
2014-09-06 23:53:22 +00:00
|
|
|
assertNoError(err, t, "Break()")
|
2014-05-30 15:12:18 +00:00
|
|
|
|
|
|
|
breakpc := bp.Addr + 1
|
|
|
|
err = p.Continue()
|
2014-09-06 23:53:22 +00:00
|
|
|
assertNoError(err, 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
|
|
|
|
|
|
|
if pc != breakpc {
|
2014-10-18 01:34:58 +00:00
|
|
|
f, l, _ := p.GoSymTable.PCToLine(pc)
|
|
|
|
t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, breakpc)
|
2014-05-30 15:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Step()
|
2014-11-23 00:57:26 +00:00
|
|
|
if _, ok := err.(proctl.TimeoutError); !ok {
|
|
|
|
assertNoError(err, t, "Step()")
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2014-10-18 01:34:58 +00:00
|
|
|
func TestBreakPointInSeperateGoRoutine(t *testing.T) {
|
|
|
|
helper.WithTestProcess("../_fixtures/testthreads", t, func(p *proctl.DebuggedProcess) {
|
|
|
|
fn := p.GoSymTable.LookupFunc("main.anotherthread")
|
|
|
|
if fn == nil {
|
|
|
|
t.Fatal("No fn exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := p.Break(uintptr(fn.Entry))
|
|
|
|
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-06-29 16:52:21 +00:00
|
|
|
helper.WithTestProcess("../_fixtures/testprog", t, func(p *proctl.DebuggedProcess) {
|
2014-05-30 15:12:18 +00:00
|
|
|
_, 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-06-29 16:52:21 +00:00
|
|
|
helper.WithTestProcess("../_fixtures/testprog", t, func(p *proctl.DebuggedProcess) {
|
2014-05-30 15:12:18 +00:00
|
|
|
fn := p.GoSymTable.LookupFunc("main.sleepytime")
|
|
|
|
bp, err := p.Break(uintptr(fn.Entry))
|
2014-09-06 23:53:22 +00:00
|
|
|
assertNoError(err, t, "Break()")
|
2014-05-30 15:12:18 +00:00
|
|
|
|
|
|
|
int3, err := dataAtAddr(p.Pid, bp.Addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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-07-30 00:00:24 +00:00
|
|
|
helper.WithTestProcess(executablePath, t, func(p *proctl.DebuggedProcess) {
|
2014-06-29 16:52:21 +00:00
|
|
|
pc, _, _ := p.GoSymTable.LineToPC(fp, testcases[0].begin)
|
2014-10-08 03:35:57 +00:00
|
|
|
_, err := p.Break(uintptr(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
|
|
|
|
|
|
|
for _, tc := range testcases {
|
2014-10-07 17:25:33 +00:00
|
|
|
f, ln := currentLineNumber(p, t)
|
2014-06-29 16:52:21 +00:00
|
|
|
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
|
|
|
|
2014-10-25 14:17:05 +00:00
|
|
|
if len(p.BreakPoints) != 1 {
|
2014-11-08 05:44:24 +00:00
|
|
|
t.Fatal("Not all breakpoints were cleaned up", len(p.BreakPoints))
|
2014-09-17 03:37:48 +00:00
|
|
|
}
|
2014-06-29 16:52:21 +00:00
|
|
|
})
|
|
|
|
}
|