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-06-25 19:06:04 +00:00
|
|
|
"github.com/derekparker/dbg/_helper"
|
|
|
|
"github.com/derekparker/dbg/proctl"
|
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func currentLineNumber(p *proctl.DebuggedProcess, t *testing.T) int {
|
|
|
|
pc := currentPC(p, t)
|
|
|
|
_, l, _ := p.GoSymTable.PCToLine(pc)
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:12:18 +00:00
|
|
|
func TestAttachProcess(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
|
|
|
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) {
|
2014-06-29 16:52:21 +00:00
|
|
|
helper.WithTestProcess("../_fixtures/testprog", t, func(p *proctl.DebuggedProcess) {
|
2014-06-09 19:56:10 +00:00
|
|
|
if p.ProcessState.Exited() {
|
|
|
|
t.Fatal("Process already exited")
|
|
|
|
}
|
|
|
|
|
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-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-06-25 19:06:04 +00:00
|
|
|
regs = helper.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) {
|
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
|
|
|
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-06-29 16:52:21 +00:00
|
|
|
helper.WithTestProcess("../_fixtures/testprog", t, func(p *proctl.DebuggedProcess) {
|
2014-05-30 15:12:18 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-06-25 19:06:04 +00:00
|
|
|
regs := helper.GetRegisters(p, t)
|
2014-05-30 15:12:18 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-06-25 19:06:04 +00:00
|
|
|
regs = helper.GetRegisters(p, t)
|
2014-05-30 15:12:18 +00:00
|
|
|
|
|
|
|
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-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))
|
|
|
|
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
|
|
|
}
|
2014-06-29 16:52:21 +00:00
|
|
|
|
|
|
|
func TestNext(t *testing.T) {
|
|
|
|
var (
|
2014-07-30 00:00:24 +00:00
|
|
|
ln int
|
|
|
|
err error
|
|
|
|
executablePath = "../_fixtures/testnextprog"
|
2014-06-29 16:52:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
begin, end int
|
|
|
|
}{
|
2014-07-10 23:07:39 +00:00
|
|
|
{17, 19},
|
|
|
|
{19, 20},
|
2014-06-29 16:52:21 +00:00
|
|
|
{20, 22},
|
2014-07-21 23:20:16 +00:00
|
|
|
{22, 19},
|
|
|
|
{19, 20},
|
2014-07-10 23:07:39 +00:00
|
|
|
{20, 22},
|
2014-07-21 23:20:16 +00:00
|
|
|
{22, 19},
|
|
|
|
{19, 25},
|
2014-07-10 23:07:39 +00:00
|
|
|
{25, 26},
|
|
|
|
{26, 30},
|
2014-07-11 19:52:55 +00:00
|
|
|
{30, 31},
|
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)
|
|
|
|
_, err := p.Break(uintptr(pc))
|
|
|
|
assertNoError(err, t, "Break() returned an error")
|
|
|
|
assertNoError(p.Continue(), t, "Continue() returned an error")
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
ln = currentLineNumber(p, t)
|
|
|
|
if ln != tc.begin {
|
|
|
|
t.Fatalf("Program not stopped at correct spot expected %d was %d", tc.begin, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNoError(p.Next(), t, "Next() returned an error")
|
|
|
|
|
|
|
|
ln = currentLineNumber(p, t)
|
|
|
|
if ln != tc.end {
|
|
|
|
t.Fatalf("Program did not continue to correct next location expected %d was %d", tc.end, ln)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-07-30 00:00:24 +00:00
|
|
|
|
|
|
|
func TestVariableEvaluation(t *testing.T) {
|
|
|
|
executablePath := "../_fixtures/testvariables"
|
|
|
|
|
|
|
|
fp, err := filepath.Abs(executablePath + ".go")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
varType string
|
|
|
|
}{
|
2014-08-04 18:53:15 +00:00
|
|
|
{"a1", "foo", "struct string"},
|
2014-08-04 18:20:20 +00:00
|
|
|
{"a2", "6", "int"},
|
|
|
|
{"a3", "7.23", "float64"},
|
2014-08-04 20:21:35 +00:00
|
|
|
{"a4", "[2]int [1 2]", "[2]int"},
|
2014-08-04 20:41:09 +00:00
|
|
|
{"a5", "len: 5 cap: 5 [1 2 3 4 5]", "struct []int"},
|
2014-07-30 00:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
helper.WithTestProcess(executablePath, t, func(p *proctl.DebuggedProcess) {
|
2014-08-04 18:20:20 +00:00
|
|
|
pc, _, _ := p.GoSymTable.LineToPC(fp, 21)
|
2014-07-30 00:00:24 +00:00
|
|
|
|
|
|
|
_, err := p.Break(uintptr(pc))
|
|
|
|
assertNoError(err, t, "Break() returned an error")
|
|
|
|
|
|
|
|
err = p.Continue()
|
2014-08-04 18:20:20 +00:00
|
|
|
assertNoError(err, t, "Continue() returned an error")
|
2014-07-30 00:00:24 +00:00
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
variable, err := p.EvalSymbol(tc.name)
|
|
|
|
assertNoError(err, t, "Variable() returned an error")
|
|
|
|
|
|
|
|
if variable.Name != tc.name {
|
|
|
|
t.Fatalf("Expected %s got %s\n", tc.name, variable.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if variable.Type != tc.varType {
|
|
|
|
t.Fatalf("Expected %s got %s\n", tc.varType, variable.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if variable.Value != tc.value {
|
2014-08-04 18:53:15 +00:00
|
|
|
t.Fatalf("Expected %#v got %#v\n", tc.value, variable.Value)
|
2014-07-30 00:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|