proc_test.go 17.8 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 135
			if !th.Stopped() {
				t.Fatal("expected thread to be stopped, but was not")
			}
136 137
			if th.running != false {
				t.Fatal("expected running = false for thread", th.Id)
D
Derek Parker 已提交
138
			}
139 140
			_, err := th.Registers()
			assertNoError(err, t, "Registers")
D
Derek Parker 已提交
141 142 143 144
		}
	})
}

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

150 151
		_, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
152 153
		assertNoError(p.Continue(), t, "Continue()")

154
		regs := getRegisters(p, t)
155
		rip := regs.PC()
156

157
		err = p.Step()
D
Derek Parker 已提交
158
		assertNoError(err, t, "Step()")
159

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

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

172 173
		bp, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
D
Derek Parker 已提交
174
		assertNoError(p.Continue(), t, "Continue()")
175

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

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

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

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

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

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

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

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

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

232 233
		bp, err = p.ClearBreakpoint(fn.Entry)
		assertNoError(err, t, "ClearBreakpoint()")
234

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

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

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

251 252 253
type nextTest struct {
	begin, end int
}
254

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

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

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

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

D
Derek Parker 已提交
277 278
		if len(p.Breakpoints) != 0 {
			t.Fatal("Not all breakpoints were cleaned up", len(p.Breakpoints))
279
		}
280 281
	})
}
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
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},
	}
300
	testnext("testnextprog", testcases, "main.testnext", t)
301 302
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
func TestNextConcurrent(t *testing.T) {
	testcases := []nextTest{
		{9, 10},
		{10, 11},
	}
	withTestProcess("parallel_next", t, func(p *Process, fixture protest.Fixture) {
		_, err := setFunctionBreakpoint(p, "main.sayhi")
		assertNoError(err, t, "SetBreakpoint")
		assertNoError(p.Continue(), t, "Continue")
		f, ln := currentLineNumber(p, t)
		initV, err := p.EvalVariable("n")
		assertNoError(err, t, "EvalVariable")
		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)
			}
			v, err := p.EvalVariable("n")
			assertNoError(err, t, "EvalVariable")
			if v.Value != initV.Value {
				t.Fatal("Did not end up on same goroutine")
			}
		}
	})
}

333 334 335 336
func TestNextGoroutine(t *testing.T) {
	testcases := []nextTest{
		{47, 42},
	}
337
	testnext("testnextprog", testcases, "main.testgoroutine", t)
338 339 340 341 342 343
}

func TestNextFunctionReturn(t *testing.T) {
	testcases := []nextTest{
		{14, 35},
	}
344 345 346 347 348 349
	testnext("testnextprog", testcases, "main.helloworld", t)
}

func TestNextFunctionReturnDefer(t *testing.T) {
	testcases := []nextTest{
		{9, 6},
350 351
		{6, 7},
		{7, 10},
352 353
	}
	testnext("testnextdefer", testcases, "main.main", t)
354 355
}

D
Derek Parker 已提交
356 357 358 359 360 361 362 363 364 365 366 367
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 已提交
368
				conn, err := net.Dial("tcp", ":9191")
D
Derek Parker 已提交
369 370 371 372 373 374
				if err == nil {
					conn.Close()
					break
				}
				time.Sleep(50 * time.Millisecond)
			}
D
Derek Parker 已提交
375
			http.Get("http://localhost:9191")
D
Derek Parker 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
		}()
		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 已提交
396
func TestRuntimeBreakpoint(t *testing.T) {
D
Derek Parker 已提交
397
	withTestProcess("testruntimebreakpoint", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
		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")
		}
	})
}

413
func TestFindReturnAddress(t *testing.T) {
D
Derek Parker 已提交
414
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
415
		start, _, err := p.goSymTable.LineToPC(fixture.Source, 24)
416 417 418
		if err != nil {
			t.Fatal(err)
		}
419
		_, err = p.SetBreakpoint(start)
420 421 422 423 424 425 426
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
427
		addr, err := p.CurrentThread.ReturnAddress()
428 429 430
		if err != nil {
			t.Fatal(err)
		}
431 432 433
		_, l, _ := p.goSymTable.PCToLine(addr)
		if l != 40 {
			t.Fatalf("return address not found correctly, expected line 40")
434
		}
435 436
	})
}
437

438 439 440 441 442 443 444 445
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 {
446 447
			t.Fatal(err)
		}
448
		if err := p.Continue(); err != nil {
D
Derek Parker 已提交
449 450
			t.Fatal(err)
		}
451 452
		if _, err := p.CurrentThread.ReturnAddress(); err == nil {
			t.Fatal("expected error to be returned")
453 454 455
		}
	})
}
D
Derek Parker 已提交
456 457

func TestSwitchThread(t *testing.T) {
D
Derek Parker 已提交
458
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
459 460 461 462 463
		// With invalid thread id
		err := p.SwitchThread(-1)
		if err == nil {
			t.Fatal("Expected error for invalid thread id")
		}
464
		pc, err := p.FindFunctionLocation("main.main", true, 0)
D
Derek Parker 已提交
465 466 467
		if err != nil {
			t.Fatal(err)
		}
468
		_, err = p.SetBreakpoint(pc)
D
Derek Parker 已提交
469 470 471 472 473 474 475 476 477
		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 已提交
478
		for tid := range p.Threads {
D
Derek Parker 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
			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 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

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 已提交
515 516
		[]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 已提交
517
	}
D
Derek Parker 已提交
518
	withTestProcess("stacktraceprog", t, func(p *Process, fixture protest.Fixture) {
519
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
520 521 522 523
		assertNoError(err, t, "BreakByLocation()")

		for i := range stacks {
			assertNoError(p.Continue(), t, "Continue()")
D
Derek Parker 已提交
524
			locations, err := p.CurrentThread.Stacktrace(40)
A
aarzilli 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537
			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)
				}
			}
		}

538
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
		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 已提交
556 557
	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 已提交
558

D
Derek Parker 已提交
559
	withTestProcess("goroutinestackprog", t, func(p *Process, fixture protest.Fixture) {
560
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
561 562 563 564 565 566 567 568 569 570
		assertNoError(err, t, "BreakByLocation()")

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

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

		agoroutineCount := 0
		mainCount := 0

D
Derek Parker 已提交
571 572
		for i, g := range gs {
			locations, err := p.GoroutineStacktrace(g, 40)
A
aarzilli 已提交
573 574 575 576 577 578 579 580 581
			assertNoError(err, t, "GoroutineStacktrace()")

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

			if stackMatch(agoroutineStack, locations) {
				agoroutineCount++
			} else {
D
Derek Parker 已提交
582
				t.Logf("Non-goroutine stack: %d (%d)", i, len(locations))
A
aarzilli 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
				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)
		}

601
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
602 603 604
		p.Continue()
	})
}
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

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)
			}
		}
	})
}
622 623

func testGSupportFunc(name string, t *testing.T, p *Process, fixture protest.Fixture) {
624
	bp, err := setFunctionBreakpoint(p, "main.main")
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	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)
	})

646 647 648 649 650
	// 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
	}

651 652 653 654
	withTestProcess("cgotest", t, func(p *Process, fixture protest.Fixture) {
		testGSupportFunc("cgo", t, p, fixture)
	})
}
655 656 657

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

661
		bp2, err := setFunctionBreakpoint(p, "main.sayhi")
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		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)
		}
	})
}
691

692
func versionAfterOrEqual(t *testing.T, verStr string, ver GoVersion) {
693 694 695 696
	pver, ok := parseVersionString(verStr)
	if !ok {
		t.Fatalf("Could not parse version string <%s>", verStr)
	}
697
	if !pver.AfterOrEqual(ver) {
698 699 700 701 702 703
		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) {
A
aarzilli 已提交
704
	versionAfterOrEqual(t, "go1.4", GoVersion{1, 4, 0, 0, 0})
D
Derek Parker 已提交
705 706 707 708
	versionAfterOrEqual(t, "go1.5.0", GoVersion{1, 5, 0, 0, 0})
	versionAfterOrEqual(t, "go1.4.2", GoVersion{1, 4, 2, 0, 0})
	versionAfterOrEqual(t, "go1.5beta2", GoVersion{1, 5, -1, 2, 0})
	versionAfterOrEqual(t, "go1.5rc2", GoVersion{1, 5, -1, 0, 2})
709 710 711 712 713 714 715 716
	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")
	}
}
717 718 719 720 721 722 723 724 725 726 727 728 729 730

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)
		}
	})
}
731 732 733 734 735 736 737 738 739 740

func TestProcessReceivesSIGCHLD(t *testing.T) {
	withTestProcess("sigchldprog", t, func(p *Process, fixture protest.Fixture) {
		err := p.Continue()
		_, ok := err.(ProcessExitedError)
		if !ok {
			t.Fatalf("Continue() returned unexpected error type %s", err)
		}
	})
}