proc_test.go 18.7 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
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)
313
		initV, err := evalVariable(p, "n")
314 315
		assertNoError(err, t, "EvalVariable")
		for _, tc := range testcases {
316 317 318 319 320
			g, err := p.CurrentThread.GetG()
			assertNoError(err, t, "GetG()")
			if p.SelectedGoroutine.Id != g.Id {
				t.Fatalf("SelectedGoroutine not CurrentThread's goroutine: %d %d", g.Id, p.SelectedGoroutine.Id)
			}
321 322 323 324 325 326 327 328
			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)
			}
329
			v, err := evalVariable(p, "n")
330 331 332 333 334 335 336 337
			assertNoError(err, t, "EvalVariable")
			if v.Value != initV.Value {
				t.Fatal("Did not end up on same goroutine")
			}
		}
	})
}

338 339 340 341
func TestNextGoroutine(t *testing.T) {
	testcases := []nextTest{
		{47, 42},
	}
342
	testnext("testnextprog", testcases, "main.testgoroutine", t)
343 344 345 346 347 348
}

func TestNextFunctionReturn(t *testing.T) {
	testcases := []nextTest{
		{14, 35},
	}
349 350 351 352 353 354
	testnext("testnextprog", testcases, "main.helloworld", t)
}

func TestNextFunctionReturnDefer(t *testing.T) {
	testcases := []nextTest{
		{9, 6},
355 356
		{6, 7},
		{7, 10},
357 358
	}
	testnext("testnextdefer", testcases, "main.main", t)
359 360
}

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

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

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

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

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
func TestCGONext(t *testing.T) {
	// Test if one can do 'next' in a cgo binary
	// 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
	}

	withTestProcess("cgotest", t, func(p *Process, fixture protest.Fixture) {
		pc, err := p.FindFunctionLocation("main.main", true, 0)
		if err != nil {
			t.Fatal(err)
		}
		_, err = p.SetBreakpoint(pc)
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		err = p.Next()
		if err != nil {
			t.Fatal(err)
		}
	})
}

A
aarzilli 已提交
530 531 532 533 534
type loc struct {
	line int
	fn   string
}

535
func (l1 *loc) match(l2 Stackframe) bool {
A
aarzilli 已提交
536 537 538 539 540 541 542 543 544 545 546
	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 已提交
547 548
		[]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 已提交
549
	}
D
Derek Parker 已提交
550
	withTestProcess("stacktraceprog", t, func(p *Process, fixture protest.Fixture) {
551
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
552 553 554 555
		assertNoError(err, t, "BreakByLocation()")

		for i := range stacks {
			assertNoError(p.Continue(), t, "Continue()")
D
Derek Parker 已提交
556
			locations, err := p.CurrentThread.Stacktrace(40)
A
aarzilli 已提交
557 558 559 560 561 562
			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)
			}

563 564
			t.Logf("Stacktrace %d: %v\n", i, locations)

A
aarzilli 已提交
565 566 567 568 569 570 571
			for j := range stacks[i] {
				if !stacks[i][j].match(locations[j]) {
					t.Fatalf("Wrong stack trace pos %d\n", j)
				}
			}
		}

572
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
573 574 575 576
		p.Continue()
	})
}

577
func stackMatch(stack []loc, locations []Stackframe) bool {
A
aarzilli 已提交
578 579 580 581 582 583 584 585 586 587 588 589
	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 已提交
590 591
	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 已提交
592

D
Derek Parker 已提交
593
	withTestProcess("goroutinestackprog", t, func(p *Process, fixture protest.Fixture) {
594
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
595 596 597 598 599 600 601 602 603 604
		assertNoError(err, t, "BreakByLocation()")

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

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

		agoroutineCount := 0
		mainCount := 0

D
Derek Parker 已提交
605 606
		for i, g := range gs {
			locations, err := p.GoroutineStacktrace(g, 40)
A
aarzilli 已提交
607 608 609 610 611 612 613 614 615
			assertNoError(err, t, "GoroutineStacktrace()")

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

			if stackMatch(agoroutineStack, locations) {
				agoroutineCount++
			} else {
D
Derek Parker 已提交
616
				t.Logf("Non-goroutine stack: %d (%d)", i, len(locations))
A
aarzilli 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
				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)
		}

635
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
636 637 638
		p.Continue()
	})
}
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

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)
			}
		}
	})
}
656 657

func testGSupportFunc(name string, t *testing.T, p *Process, fixture protest.Fixture) {
658
	bp, err := setFunctionBreakpoint(p, "main.main")
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	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)
	})

680 681 682 683 684
	// 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
	}

685 686 687 688
	withTestProcess("cgotest", t, func(p *Process, fixture protest.Fixture) {
		testGSupportFunc("cgo", t, p, fixture)
	})
}
689 690 691

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

695
		bp2, err := setFunctionBreakpoint(p, "main.sayhi")
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
		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)
		}
	})
}
725

726
func versionAfterOrEqual(t *testing.T, verStr string, ver GoVersion) {
727 728 729 730
	pver, ok := parseVersionString(verStr)
	if !ok {
		t.Fatalf("Could not parse version string <%s>", verStr)
	}
731
	if !pver.AfterOrEqual(ver) {
732 733 734 735 736 737
		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 已提交
738
	versionAfterOrEqual(t, "go1.4", GoVersion{1, 4, 0, 0, 0})
D
Derek Parker 已提交
739 740 741 742
	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})
743 744 745 746 747 748 749 750
	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")
	}
}
751 752 753 754 755 756 757 758 759 760 761 762 763 764

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)
		}
	})
}
765 766 767 768 769 770 771 772 773 774

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