proctl_test.go 9.5 KB
Newer Older
1
package proctl
D
Derek Parker 已提交
2 3

import (
4
	"bytes"
5 6 7
	"encoding/binary"
	"os"
	"os/exec"
8
	"path/filepath"
9
	"runtime"
D
Derek Parker 已提交
10
	"testing"
D
Derek Parker 已提交
11
	"time"
D
Derek Parker 已提交
12
)
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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 {
33 34 35 36 37 38 39 40
	regs, err := p.Registers()
	if err != nil {
		t.Fatal("Registers():", err)
	}

	return regs
}

D
Derek Parker 已提交
41
func dataAtAddr(thread *ThreadContext, addr uint64) ([]byte, error) {
42
	data := make([]byte, 1)
D
Derek Parker 已提交
43
	_, err := readMemory(thread, uintptr(addr), data)
44 45 46 47 48 49 50
	if err != nil {
		return nil, err
	}

	return data, nil
}

51 52
func assertNoError(err error, t *testing.T, s string) {
	if err != nil {
53 54 55
		_, file, line, _ := runtime.Caller(1)
		fname := filepath.Base(file)
		t.Fatalf("failed assertion at %s:%d: %s : %s\n", fname, line, s, err)
56 57 58
	}
}

59
func currentPC(p *DebuggedProcess, t *testing.T) uint64 {
D
Derek Parker 已提交
60
	pc, err := p.PC()
61 62 63 64 65 66 67
	if err != nil {
		t.Fatal(err)
	}

	return pc
}

68
func currentLineNumber(p *DebuggedProcess, t *testing.T) (string, int) {
69
	pc := currentPC(p, t)
70
	f, l, _ := p.goSymTable.PCToLine(pc)
71

D
Derek Parker 已提交
72
	return f, l
73 74
}

75 76 77 78 79
func TestExit(t *testing.T) {
	withTestProcess("../_fixtures/continuetestprog", t, func(p *DebuggedProcess) {
		err := p.Continue()
		pe, ok := err.(ProcessExitedError)
		if !ok {
80
			t.Fatalf("Continue() returned unexpected error type %s", err)
81 82 83 84 85 86 87 88 89 90
		}
		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)
		}
	})
}

D
Derek Parker 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
func TestHalt(t *testing.T) {
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
		go func() {
			time.Sleep(10 * time.Millisecond)
			err := p.RequestManualStop()
			if err != nil {
				t.Fatal(err)
			}
		}()
		err := p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		// Loop through threads and make sure they are all
		// actually stopped, err will not be nil if the process
		// is still running.
		for _, th := range p.Threads {
			_, err := th.Registers()
			if err != nil {
				t.Error(err)
			}
		}
	})
}

116
func TestStep(t *testing.T) {
117
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
118
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
119 120
		helloworldaddr := helloworldfunc.Entry

121
		_, err := p.Break(helloworldaddr)
122 123 124
		assertNoError(err, t, "Break()")
		assertNoError(p.Continue(), t, "Continue()")

125
		regs := getRegisters(p, t)
126
		rip := regs.PC()
127

128
		err = p.Step()
D
Derek Parker 已提交
129
		assertNoError(err, t, "Step()")
130

131
		regs = getRegisters(p, t)
132 133 134 135 136
		if rip >= regs.PC() {
			t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
		}
	})
}
137

138
func TestBreakPoint(t *testing.T) {
139
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
140
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
D
Derek Parker 已提交
141
		helloworldaddr := helloworldfunc.Entry
142

D
Derek Parker 已提交
143
		bp, err := p.Break(helloworldaddr)
D
Derek Parker 已提交
144
		assertNoError(err, t, "Break()")
D
Derek Parker 已提交
145
		assertNoError(p.Continue(), t, "Continue()")
146

D
Derek Parker 已提交
147
		pc, err := p.PC()
148 149 150
		if err != nil {
			t.Fatal(err)
		}
151

D
Derek Parker 已提交
152
		if pc-1 != bp.Addr && pc != bp.Addr {
153
			f, l, _ := p.goSymTable.PCToLine(pc)
D
Derek Parker 已提交
154
			t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
155 156
		}
	})
157
}
158

159
func TestBreakPointInSeperateGoRoutine(t *testing.T) {
160
	withTestProcess("../_fixtures/testthreads", t, func(p *DebuggedProcess) {
161
		fn := p.goSymTable.LookupFunc("main.anotherthread")
162 163 164 165
		if fn == nil {
			t.Fatal("No fn exists")
		}

166
		_, err := p.Break(fn.Entry)
167 168 169 170 171 172 173 174 175
		if err != nil {
			t.Fatal(err)
		}

		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}

D
Derek Parker 已提交
176
		pc, err := p.PC()
177 178 179 180
		if err != nil {
			t.Fatal(err)
		}

181
		f, l, _ := p.goSymTable.PCToLine(pc)
182 183 184 185 186 187
		if f != "testthreads.go" && l != 8 {
			t.Fatal("Program did not hit breakpoint")
		}
	})
}

188
func TestBreakPointWithNonExistantFunction(t *testing.T) {
189
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
190
		_, err := p.Break(0)
191 192 193 194
		if err == nil {
			t.Fatal("Should not be able to break at non existant function")
		}
	})
195
}
196 197

func TestClearBreakPoint(t *testing.T) {
198
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
199
		fn := p.goSymTable.LookupFunc("main.sleepytime")
200
		bp, err := p.Break(fn.Entry)
D
Derek Parker 已提交
201
		assertNoError(err, t, "Break()")
202 203

		bp, err = p.Clear(fn.Entry)
D
Derek Parker 已提交
204
		assertNoError(err, t, "Clear()")
205

D
Derek Parker 已提交
206
		data, err := dataAtAddr(p.CurrentThread, bp.Addr)
207 208 209 210
		if err != nil {
			t.Fatal(err)
		}

211
		int3 := []byte{0xcc}
212 213 214 215
		if bytes.Equal(data, int3) {
			t.Fatalf("Breakpoint was not cleared data: %#v, int3: %#v", data, int3)
		}

216
		if len(p.BreakPoints) != 0 {
217 218 219
			t.Fatal("Breakpoint not removed internally")
		}
	})
220
}
221

222 223 224
type nextTest struct {
	begin, end int
}
225

226 227
func testnext(testcases []nextTest, initialLocation string, t *testing.T) {
	var executablePath = "../_fixtures/testnextprog"
228
	withTestProcess(executablePath, t, func(p *DebuggedProcess) {
229
		bp, err := p.BreakByLocation(initialLocation)
230 231
		assertNoError(err, t, "Break()")
		assertNoError(p.Continue(), t, "Continue()")
232 233
		p.Clear(bp.Addr)
		p.CurrentThread.SetPC(bp.Addr)
234

D
Derek Parker 已提交
235
		f, ln := currentLineNumber(p, t)
236 237
		for _, tc := range testcases {
			if ln != tc.begin {
D
Derek Parker 已提交
238
				t.Fatalf("Program not stopped at correct spot expected %d was %s:%d", tc.begin, filepath.Base(f), ln)
239 240 241 242
			}

			assertNoError(p.Next(), t, "Next() returned an error")

D
Derek Parker 已提交
243
			f, ln = currentLineNumber(p, t)
244
			if ln != tc.end {
D
Derek Parker 已提交
245
				t.Fatalf("Program did not continue to correct next location expected %d was %s:%d", tc.end, filepath.Base(f), ln)
246 247
			}
		}
248

249
		if len(p.BreakPoints) != 0 {
250
			t.Fatal("Not all breakpoints were cleaned up", len(p.BreakPoints))
251 252 253 254 255
		}
		for _, bp := range p.HWBreakPoints {
			if bp != nil {
				t.Fatal("Not all breakpoints were cleaned up", bp.Addr)
			}
256
		}
257 258
	})
}
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
func TestNextGeneral(t *testing.T) {
	testcases := []nextTest{
		{17, 19},
		{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},
	}
	testnext(testcases, "main.testnext", t)
}

func TestNextGoroutine(t *testing.T) {
	testcases := []nextTest{
		{46, 47},
		{47, 42},
	}
	testnext(testcases, "main.testgoroutine", t)
}

func TestNextFunctionReturn(t *testing.T) {
	testcases := []nextTest{
		{13, 14},
		{14, 35},
	}
	testnext(testcases, "main.helloworld", t)
}

297 298 299 300 301
func TestFindReturnAddress(t *testing.T) {
	var testfile, _ = filepath.Abs("../_fixtures/testnextprog")

	withTestProcess(testfile, t, func(p *DebuggedProcess) {
		var (
302 303
			fdes = p.frameEntries
			gsd  = p.goSymTable
304 305 306 307 308 309 310 311
		)

		testsourcefile := testfile + ".go"
		start, _, err := gsd.LineToPC(testsourcefile, 24)
		if err != nil {
			t.Fatal(err)
		}

312
		_, err = p.Break(start)
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
		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)

D
Derek Parker 已提交
340
		readMemory(p.CurrentThread, uintptr(addr), data)
341 342
		addr = binary.LittleEndian.Uint64(data)

343 344 345
		_, l, _ := p.goSymTable.PCToLine(addr)
		if l != 40 {
			t.Fatalf("return address not found correctly, expected line 40")
346 347 348
		}
	})
}
D
Derek Parker 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

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")
		}
	})
}
D
Derek Parker 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

func TestFunctionCall(t *testing.T) {
	var testfile, _ = filepath.Abs("../_fixtures/testprog")

	withTestProcess(testfile, t, func(p *DebuggedProcess) {
		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)
		}
D
Derek Parker 已提交
409
		pc, err = p.PC()
D
Derek Parker 已提交
410 411 412
		if err != nil {
			t.Fatal(err)
		}
413
		fn := p.goSymTable.PCToFunc(pc)
D
Derek Parker 已提交
414 415 416 417 418 419
		if fn == nil {
			t.Fatalf("Could not find func for PC: %#v", pc)
		}
		if fn.Name != "main.main" {
			t.Fatal("Program stopped at incorrect place")
		}
420 421
		if err = p.CallFn("runtime.getg", func() error {
			th := p.CurrentThread
D
Derek Parker 已提交
422
			pc, err := th.PC()
D
Derek Parker 已提交
423 424 425
			if err != nil {
				t.Fatal(err)
			}
426
			f := th.Process.goSymTable.LookupFunc("runtime.getg")
D
Derek Parker 已提交
427 428 429 430 431 432 433 434 435 436
			if f == nil {
				t.Fatalf("could not find function %s", "runtime.getg")
			}
			if pc-1 != f.End-2 && pc != f.End-2 {
				t.Fatalf("wrong pc expected %#v got %#v", f.End-2, pc-1)
			}
			return nil
		}); err != nil {
			t.Fatal(err)
		}
D
Derek Parker 已提交
437
		pc, err = p.PC()
D
Derek Parker 已提交
438 439 440
		if err != nil {
			t.Fatal(err)
		}
441
		fn = p.goSymTable.PCToFunc(pc)
D
Derek Parker 已提交
442 443 444 445 446 447 448 449
		if fn == nil {
			t.Fatalf("Could not find func for PC: %#v", pc)
		}
		if fn.Name != "main.main" {
			t.Fatal("Program stopped at incorrect place")
		}
	})
}