proc_test.go 16.3 KB
Newer Older
D
Derek Parker 已提交
1
package proc
D
Derek Parker 已提交
2 3

import (
4
	"bytes"
5
	"fmt"
D
Derek Parker 已提交
6 7
	"net"
	"net/http"
D
Derek Parker 已提交
8
	"os"
9
	"path/filepath"
10
	"runtime"
11
	"strings"
D
Derek Parker 已提交
12
	"testing"
D
Derek Parker 已提交
13
	"time"
D
Dan Mace 已提交
14

D
Derek Parker 已提交
15
	protest "github.com/derekparker/delve/proc/test"
D
Derek Parker 已提交
16
)
17

18
func init() {
19 20
	runtime.GOMAXPROCS(4)
	os.Setenv("GOMAXPROCS", "4")
21 22
}

D
Dan Mace 已提交
23
func TestMain(m *testing.M) {
D
Derek Parker 已提交
24
	os.Exit(protest.RunTestsWithFixtures(m))
D
Dan Mace 已提交
25 26
}

D
Derek Parker 已提交
27
func withTestProcess(name string, t *testing.T, fn func(p *Process, fixture protest.Fixture)) {
28
	fixture := protest.BuildFixture(name)
D
Dan Mace 已提交
29
	p, err := Launch([]string{fixture.Path})
30 31 32 33
	if err != nil {
		t.Fatal("Launch():", err)
	}

34 35
	defer func() {
		p.Halt()
36
		p.Kill()
37
	}()
38

D
Dan Mace 已提交
39
	fn(p, fixture)
40 41
}

D
Derek Parker 已提交
42
func getRegisters(p *Process, t *testing.T) Registers {
43 44 45 46 47 48 49 50
	regs, err := p.Registers()
	if err != nil {
		t.Fatal("Registers():", err)
	}

	return regs
}

D
Derek Parker 已提交
51
func dataAtAddr(thread *Thread, addr uint64) ([]byte, error) {
D
Derek Parker 已提交
52
	return thread.readMemory(uintptr(addr), 1)
53 54
}

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

D
Derek Parker 已提交
63
func currentPC(p *Process, t *testing.T) uint64 {
D
Derek Parker 已提交
64
	pc, err := p.PC()
65 66 67 68 69 70 71
	if err != nil {
		t.Fatal(err)
	}

	return pc
}

D
Derek Parker 已提交
72
func currentLineNumber(p *Process, t *testing.T) (string, int) {
73
	pc := currentPC(p, t)
74
	f, l, _ := p.goSymTable.PCToLine(pc)
75

D
Derek Parker 已提交
76
	return f, l
77 78
}

79
func TestExit(t *testing.T) {
D
Derek Parker 已提交
80
	withTestProcess("continuetestprog", t, func(p *Process, fixture protest.Fixture) {
81 82 83
		err := p.Continue()
		pe, ok := err.(ProcessExitedError)
		if !ok {
84
			t.Fatalf("Continue() returned unexpected error type %s", err)
85 86 87 88 89 90 91 92 93 94
		}
		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)
		}
	})
}

95 96 97 98 99 100 101 102
func setFunctionBreakpoint(p *Process, fname string) (*Breakpoint, error) {
	addr, err := p.FindFunctionLocation(fname, true, 0)
	if err != nil {
		return nil, err
	}
	return p.SetBreakpoint(addr)
}

D
Derek Parker 已提交
103
func TestHalt(t *testing.T) {
104 105
	stopChan := make(chan interface{})
	withTestProcess("loopprog", t, func(p *Process, fixture protest.Fixture) {
106
		_, err := setFunctionBreakpoint(p, "main.loop")
107 108 109 110 111 112 113 114 115
		assertNoError(err, t, "SetBreakpoint")
		assertNoError(p.Continue(), t, "Continue")
		for _, th := range p.Threads {
			if th.running != false {
				t.Fatal("expected running = false for thread", th.Id)
			}
			_, err := th.Registers()
			assertNoError(err, t, "Registers")
		}
D
Derek Parker 已提交
116
		go func() {
D
Dan Mace 已提交
117 118
			for {
				if p.Running() {
119
					if err := p.RequestManualStop(); err != nil {
D
Dan Mace 已提交
120 121
						t.Fatal(err)
					}
122
					stopChan <- nil
D
Dan Mace 已提交
123 124
					return
				}
D
Derek Parker 已提交
125 126
			}
		}()
127 128
		assertNoError(p.Continue(), t, "Continue")
		<-stopChan
D
Derek Parker 已提交
129 130 131 132
		// 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 {
133 134
			if th.running != false {
				t.Fatal("expected running = false for thread", th.Id)
D
Derek Parker 已提交
135
			}
136 137
			_, err := th.Registers()
			assertNoError(err, t, "Registers")
D
Derek Parker 已提交
138 139 140 141
		}
	})
}

142
func TestStep(t *testing.T) {
D
Derek Parker 已提交
143
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
144
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
145 146
		helloworldaddr := helloworldfunc.Entry

147 148
		_, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
149 150
		assertNoError(p.Continue(), t, "Continue()")

151
		regs := getRegisters(p, t)
152
		rip := regs.PC()
153

154
		err = p.Step()
D
Derek Parker 已提交
155
		assertNoError(err, t, "Step()")
156

157
		regs = getRegisters(p, t)
158 159 160 161 162
		if rip >= regs.PC() {
			t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
		}
	})
}
163

D
Derek Parker 已提交
164
func TestBreakpoint(t *testing.T) {
D
Derek Parker 已提交
165
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
166
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
D
Derek Parker 已提交
167
		helloworldaddr := helloworldfunc.Entry
168

169 170
		bp, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
D
Derek Parker 已提交
171
		assertNoError(p.Continue(), t, "Continue()")
172

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

D
Derek Parker 已提交
178
		if pc-1 != bp.Addr && pc != bp.Addr {
179
			f, l, _ := p.goSymTable.PCToLine(pc)
D
Derek Parker 已提交
180
			t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
181 182
		}
	})
183
}
184

D
Derek Parker 已提交
185
func TestBreakpointInSeperateGoRoutine(t *testing.T) {
D
Derek Parker 已提交
186
	withTestProcess("testthreads", t, func(p *Process, fixture protest.Fixture) {
187
		fn := p.goSymTable.LookupFunc("main.anotherthread")
188 189 190 191
		if fn == nil {
			t.Fatal("No fn exists")
		}

192
		_, err := p.SetBreakpoint(fn.Entry)
193 194 195 196 197 198 199 200 201
		if err != nil {
			t.Fatal(err)
		}

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

D
Derek Parker 已提交
202
		pc, err := p.PC()
203 204 205 206
		if err != nil {
			t.Fatal(err)
		}

207
		f, l, _ := p.goSymTable.PCToLine(pc)
208 209 210 211 212 213
		if f != "testthreads.go" && l != 8 {
			t.Fatal("Program did not hit breakpoint")
		}
	})
}

D
Derek Parker 已提交
214
func TestBreakpointWithNonExistantFunction(t *testing.T) {
D
Derek Parker 已提交
215
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
216
		_, err := p.SetBreakpoint(0)
217 218 219 220
		if err == nil {
			t.Fatal("Should not be able to break at non existant function")
		}
	})
221
}
222

223
func TestClearBreakpointBreakpoint(t *testing.T) {
D
Derek Parker 已提交
224
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
225
		fn := p.goSymTable.LookupFunc("main.sleepytime")
226 227
		bp, err := p.SetBreakpoint(fn.Entry)
		assertNoError(err, t, "SetBreakpoint()")
228

229 230
		bp, err = p.ClearBreakpoint(fn.Entry)
		assertNoError(err, t, "ClearBreakpoint()")
231

D
Derek Parker 已提交
232
		data, err := dataAtAddr(p.CurrentThread, bp.Addr)
233 234 235 236
		if err != nil {
			t.Fatal(err)
		}

237
		int3 := []byte{0xcc}
238 239 240 241
		if bytes.Equal(data, int3) {
			t.Fatalf("Breakpoint was not cleared data: %#v, int3: %#v", data, int3)
		}

D
Derek Parker 已提交
242
		if len(p.Breakpoints) != 0 {
243 244 245
			t.Fatal("Breakpoint not removed internally")
		}
	})
246
}
247

248 249 250
type nextTest struct {
	begin, end int
}
251

252
func testnext(program string, testcases []nextTest, initialLocation string, t *testing.T) {
D
Derek Parker 已提交
253
	withTestProcess(program, t, func(p *Process, fixture protest.Fixture) {
254
		bp, err := setFunctionBreakpoint(p, initialLocation)
255
		assertNoError(err, t, "SetBreakpoint()")
256
		assertNoError(p.Continue(), t, "Continue()")
257
		p.ClearBreakpoint(bp.Addr)
258
		p.CurrentThread.SetPC(bp.Addr)
259

D
Derek Parker 已提交
260
		f, ln := currentLineNumber(p, t)
261 262
		for _, tc := range testcases {
			if ln != tc.begin {
D
Derek Parker 已提交
263
				t.Fatalf("Program not stopped at correct spot expected %d was %s:%d", tc.begin, filepath.Base(f), ln)
264 265 266 267
			}

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

D
Derek Parker 已提交
268
			f, ln = currentLineNumber(p, t)
269
			if ln != tc.end {
D
Derek Parker 已提交
270
				t.Fatalf("Program did not continue to correct next location expected %d was %s:%d", tc.end, filepath.Base(f), ln)
271 272
			}
		}
273

D
Derek Parker 已提交
274 275
		if len(p.Breakpoints) != 0 {
			t.Fatal("Not all breakpoints were cleaned up", len(p.Breakpoints))
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{
		{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},
	}
297
	testnext("testnextprog", testcases, "main.testnext", t)
298 299 300 301 302 303
}

func TestNextGoroutine(t *testing.T) {
	testcases := []nextTest{
		{47, 42},
	}
304
	testnext("testnextprog", testcases, "main.testgoroutine", t)
305 306 307 308 309 310
}

func TestNextFunctionReturn(t *testing.T) {
	testcases := []nextTest{
		{14, 35},
	}
311 312 313 314 315 316
	testnext("testnextprog", testcases, "main.helloworld", t)
}

func TestNextFunctionReturnDefer(t *testing.T) {
	testcases := []nextTest{
		{9, 6},
317 318
		{6, 7},
		{7, 10},
319 320
	}
	testnext("testnextdefer", testcases, "main.main", t)
321 322
}

D
Derek Parker 已提交
323 324 325 326 327 328 329 330 331 332 333 334
func TestNextNetHTTP(t *testing.T) {
	testcases := []nextTest{
		{11, 12},
		{12, 13},
	}
	withTestProcess("testnextnethttp", t, func(p *Process, fixture protest.Fixture) {
		go func() {
			for !p.Running() {
				time.Sleep(50 * time.Millisecond)
			}
			// Wait for program to start listening.
			for {
D
Derek Parker 已提交
335
				conn, err := net.Dial("tcp", ":9191")
D
Derek Parker 已提交
336 337 338 339 340 341
				if err == nil {
					conn.Close()
					break
				}
				time.Sleep(50 * time.Millisecond)
			}
D
Derek Parker 已提交
342
			http.Get("http://localhost:9191")
D
Derek Parker 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		}()
		if err := p.Continue(); err != nil {
			t.Fatal(err)
		}
		f, ln := currentLineNumber(p, t)
		for _, tc := range testcases {
			if ln != tc.begin {
				t.Fatalf("Program not stopped at correct spot expected %d was %s:%d", tc.begin, filepath.Base(f), ln)
			}

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

			f, ln = currentLineNumber(p, t)
			if ln != tc.end {
				t.Fatalf("Program did not continue to correct next location expected %d was %s:%d", tc.end, filepath.Base(f), ln)
			}
		}
	})
}

D
Derek Parker 已提交
363
func TestRuntimeBreakpoint(t *testing.T) {
D
Derek Parker 已提交
364
	withTestProcess("testruntimebreakpoint", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
		err := p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		pc, err := p.PC()
		if err != nil {
			t.Fatal(err)
		}
		_, l, _ := p.PCToLine(pc)
		if l != 10 {
			t.Fatal("did not respect breakpoint")
		}
	})
}

380
func TestFindReturnAddress(t *testing.T) {
D
Derek Parker 已提交
381
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
382
		start, _, err := p.goSymTable.LineToPC(fixture.Source, 24)
383 384 385
		if err != nil {
			t.Fatal(err)
		}
386
		_, err = p.SetBreakpoint(start)
387 388 389 390 391 392 393
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
394
		addr, err := p.CurrentThread.ReturnAddress()
395 396 397
		if err != nil {
			t.Fatal(err)
		}
398 399 400
		_, l, _ := p.goSymTable.PCToLine(addr)
		if l != 40 {
			t.Fatalf("return address not found correctly, expected line 40")
401
		}
402 403
	})
}
404

405 406 407 408 409 410 411 412
func TestFindReturnAddressTopOfStackFn(t *testing.T) {
	withTestProcess("testreturnaddress", t, func(p *Process, fixture protest.Fixture) {
		fnName := "runtime.rt0_go"
		fn := p.goSymTable.LookupFunc(fnName)
		if fn == nil {
			t.Fatalf("could not find function %s", fnName)
		}
		if _, err := p.SetBreakpoint(fn.Entry); err != nil {
413 414
			t.Fatal(err)
		}
415
		if err := p.Continue(); err != nil {
D
Derek Parker 已提交
416 417
			t.Fatal(err)
		}
418 419
		if _, err := p.CurrentThread.ReturnAddress(); err == nil {
			t.Fatal("expected error to be returned")
420 421 422
		}
	})
}
D
Derek Parker 已提交
423 424

func TestSwitchThread(t *testing.T) {
D
Derek Parker 已提交
425
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
426 427 428 429 430
		// With invalid thread id
		err := p.SwitchThread(-1)
		if err == nil {
			t.Fatal("Expected error for invalid thread id")
		}
431
		pc, err := p.FindFunctionLocation("main.main", true, 0)
D
Derek Parker 已提交
432 433 434
		if err != nil {
			t.Fatal(err)
		}
435
		_, err = p.SetBreakpoint(pc)
D
Derek Parker 已提交
436 437 438 439 440 441 442 443 444
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		var nt int
		ct := p.CurrentThread.Id
D
Dan Mace 已提交
445
		for tid := range p.Threads {
D
Derek Parker 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
			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")
		}
	})
}
A
aarzilli 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

type loc struct {
	line int
	fn   string
}

func (l1 *loc) match(l2 Location) bool {
	if l1.line >= 0 {
		if l1.line != l2.Line-1 {
			return false
		}
	}

	return l1.fn == l2.Fn.Name
}

func TestStacktrace(t *testing.T) {
	stacks := [][]loc{
D
Derek Parker 已提交
482 483
		[]loc{{3, "main.stacktraceme"}, {8, "main.func1"}, {16, "main.main"}},
		[]loc{{3, "main.stacktraceme"}, {8, "main.func1"}, {12, "main.func2"}, {17, "main.main"}},
A
aarzilli 已提交
484
	}
D
Derek Parker 已提交
485
	withTestProcess("stacktraceprog", t, func(p *Process, fixture protest.Fixture) {
486
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
487 488 489 490
		assertNoError(err, t, "BreakByLocation()")

		for i := range stacks {
			assertNoError(p.Continue(), t, "Continue()")
D
Derek Parker 已提交
491
			locations, err := p.CurrentThread.Stacktrace(40)
A
aarzilli 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504
			assertNoError(err, t, "Stacktrace()")

			if len(locations) != len(stacks[i])+2 {
				t.Fatalf("Wrong stack trace size %d %d\n", len(locations), len(stacks[i])+2)
			}

			for j := range stacks[i] {
				if !stacks[i][j].match(locations[j]) {
					t.Fatalf("Wrong stack trace pos %d\n", j)
				}
			}
		}

505
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
		p.Continue()
	})
}

func stackMatch(stack []loc, locations []Location) bool {
	if len(stack) > len(locations) {
		return false
	}
	for i := range stack {
		if !stack[i].match(locations[i]) {
			return false
		}
	}
	return true
}

func TestStacktraceGoroutine(t *testing.T) {
D
Derek Parker 已提交
523 524
	mainStack := []loc{{11, "main.stacktraceme"}, {21, "main.main"}}
	agoroutineStack := []loc{{-1, "runtime.gopark"}, {-1, "runtime.goparkunlock"}, {-1, "runtime.chansend"}, {-1, "runtime.chansend1"}, {8, "main.agoroutine"}}
A
aarzilli 已提交
525

D
Derek Parker 已提交
526
	withTestProcess("goroutinestackprog", t, func(p *Process, fixture protest.Fixture) {
527
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
528 529 530 531 532 533 534 535 536 537
		assertNoError(err, t, "BreakByLocation()")

		assertNoError(p.Continue(), t, "Continue()")

		gs, err := p.GoroutinesInfo()
		assertNoError(err, t, "GoroutinesInfo")

		agoroutineCount := 0
		mainCount := 0

D
Derek Parker 已提交
538 539
		for i, g := range gs {
			locations, err := p.GoroutineStacktrace(g, 40)
A
aarzilli 已提交
540 541 542 543 544 545 546 547 548
			assertNoError(err, t, "GoroutineStacktrace()")

			if stackMatch(mainStack, locations) {
				mainCount++
			}

			if stackMatch(agoroutineStack, locations) {
				agoroutineCount++
			} else {
D
Derek Parker 已提交
549
				t.Logf("Non-goroutine stack: %d (%d)", i, len(locations))
A
aarzilli 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
				for i := range locations {
					name := ""
					if locations[i].Fn != nil {
						name = locations[i].Fn.Name
					}
					t.Logf("\t%s:%d %s\n", locations[i].File, locations[i].Line, name)
				}
			}
		}

		if mainCount != 1 {
			t.Fatalf("Main goroutine stack not found")
		}

		if agoroutineCount != 10 {
			t.Fatalf("Goroutine stacks not found (%d)", agoroutineCount)
		}

568
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
569 570 571
		p.Continue()
	})
}
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

func TestKill(t *testing.T) {
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
		if err := p.Kill(); err != nil {
			t.Fatal(err)
		}
		if p.Exited() != true {
			t.Fatal("expected process to have exited")
		}
		if runtime.GOOS == "linux" {
			_, err := os.Open(fmt.Sprintf("/proc/%d/", p.Pid))
			if err == nil {
				t.Fatal("process has not exited", p.Pid)
			}
		}
	})
}
589 590

func testGSupportFunc(name string, t *testing.T, p *Process, fixture protest.Fixture) {
591
	bp, err := setFunctionBreakpoint(p, "main.main")
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	assertNoError(err, t, name+": BreakByLocation()")

	assertNoError(p.Continue(), t, name+": Continue()")

	g, err := p.CurrentThread.GetG()
	assertNoError(err, t, name+": GetG()")

	if g == nil {
		t.Fatal(name + ": g was nil")
	}

	t.Logf(name+": g is: %v", g)

	p.ClearBreakpoint(bp.Addr)
}

func TestGetG(t *testing.T) {
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
		testGSupportFunc("nocgo", t, p, fixture)
	})

613 614 615 616 617
	// On OSX with Go < 1.5 CGO is not supported due to: https://github.com/golang/go/issues/8973
	if runtime.GOOS == "darwin" && strings.Contains(runtime.Version(), "1.4") {
		return
	}

618 619 620 621
	withTestProcess("cgotest", t, func(p *Process, fixture protest.Fixture) {
		testGSupportFunc("cgo", t, p, fixture)
	})
}
622 623 624

func TestContinueMulti(t *testing.T) {
	withTestProcess("integrationprog", t, func(p *Process, fixture protest.Fixture) {
625
		bp1, err := setFunctionBreakpoint(p, "main.main")
626 627
		assertNoError(err, t, "BreakByLocation()")

628
		bp2, err := setFunctionBreakpoint(p, "main.sayhi")
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
		assertNoError(err, t, "BreakByLocation()")

		mainCount := 0
		sayhiCount := 0
		for {
			err := p.Continue()
			if p.exited {
				break
			}
			assertNoError(err, t, "Continue()")

			if p.CurrentBreakpoint().ID == bp1.ID {
				mainCount++
			}

			if p.CurrentBreakpoint().ID == bp2.ID {
				sayhiCount++
			}
		}

		if mainCount != 1 {
			t.Fatalf("Main breakpoint hit wrong number of times: %d\n", mainCount)
		}

		if sayhiCount != 3 {
			t.Fatalf("Sayhi breakpoint hit wrong number of times: %d\n", sayhiCount)
		}
	})
}
658

659
func versionAfterOrEqual(t *testing.T, verStr string, ver GoVersion) {
660 661 662 663
	pver, ok := parseVersionString(verStr)
	if !ok {
		t.Fatalf("Could not parse version string <%s>", verStr)
	}
664
	if !pver.AfterOrEqual(ver) {
665 666 667 668 669 670
		t.Fatalf("Version <%s> parsed as %v not after %v", verStr, pver, ver)
	}
	t.Logf("version string <%s> → %v", verStr, ver)
}

func TestParseVersionString(t *testing.T) {
671 672 673
	versionAfterOrEqual(t, "go1.5.0", GoVersion{1, 5, 0, 0})
	versionAfterOrEqual(t, "go1.4.2", GoVersion{1, 4, 2, 0})
	versionAfterOrEqual(t, "go1.5beta2", GoVersion{1, 5, -1, 2})
674 675 676 677 678 679 680 681
	ver, ok := parseVersionString("devel +17efbfc Tue Jul 28 17:39:19 2015 +0000 linux/amd64")
	if !ok {
		t.Fatalf("Could not parse devel version string")
	}
	if !ver.IsDevel() {
		t.Fatalf("Devel version string not correctly recognized")
	}
}
682 683 684 685 686 687 688 689 690 691 692 693 694 695

func TestBreakpointOnFunctionEntry(t *testing.T) {
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
		addr, err := p.FindFunctionLocation("main.main", false, 0)
		assertNoError(err, t, "FindFunctionLocation()")
		_, err = p.SetBreakpoint(addr)
		assertNoError(err, t, "SetBreakpoint()")
		assertNoError(p.Continue(), t, "Continue()")
		_, ln := currentLineNumber(p, t)
		if ln != 17 {
			t.Fatalf("Wrong line number: %d (expected: 17)\n", ln)
		}
	})
}