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

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

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

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

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

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

36 37
	defer func() {
		p.Halt()
38
		p.Kill()
39
	}()
40

D
Dan Mace 已提交
41
	fn(p, fixture)
42 43
}

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

	return regs
}

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

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

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

	return pc
}

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

D
Derek Parker 已提交
78
	return f, l
79 80
}

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

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
func TestExitAfterContinue(t *testing.T) {
	withTestProcess("continuetestprog", t, func(p *Process, fixture protest.Fixture) {
		_, err := setFunctionBreakpoint(p, "main.sayhi")
		assertNoError(err, t, "setFunctionBreakpoint()")
		assertNoError(p.Continue(), t, "First Continue()")
		err = p.Continue()
		pe, ok := err.(ProcessExitedError)
		if !ok {
			t.Fatalf("Continue() returned unexpected error type %s", err)
		}
		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)
		}
	})
}

116 117 118 119 120 121 122 123
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 已提交
124
func TestHalt(t *testing.T) {
125 126
	stopChan := make(chan interface{})
	withTestProcess("loopprog", t, func(p *Process, fixture protest.Fixture) {
127
		_, err := setFunctionBreakpoint(p, "main.loop")
128 129 130 131
		assertNoError(err, t, "SetBreakpoint")
		assertNoError(p.Continue(), t, "Continue")
		for _, th := range p.Threads {
			if th.running != false {
D
Derek Parker 已提交
132
				t.Fatal("expected running = false for thread", th.ID)
133 134 135 136
			}
			_, err := th.Registers()
			assertNoError(err, t, "Registers")
		}
D
Derek Parker 已提交
137
		go func() {
D
Dan Mace 已提交
138 139
			for {
				if p.Running() {
140
					if err := p.RequestManualStop(); err != nil {
D
Dan Mace 已提交
141 142
						t.Fatal(err)
					}
143
					stopChan <- nil
D
Dan Mace 已提交
144 145
					return
				}
D
Derek Parker 已提交
146 147
			}
		}()
148 149
		assertNoError(p.Continue(), t, "Continue")
		<-stopChan
D
Derek Parker 已提交
150 151 152 153
		// 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 {
154 155 156
			if !th.Stopped() {
				t.Fatal("expected thread to be stopped, but was not")
			}
157
			if th.running != false {
D
Derek Parker 已提交
158
				t.Fatal("expected running = false for thread", th.ID)
D
Derek Parker 已提交
159
			}
160 161
			_, err := th.Registers()
			assertNoError(err, t, "Registers")
D
Derek Parker 已提交
162 163 164 165
		}
	})
}

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

171 172
		_, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
173 174
		assertNoError(p.Continue(), t, "Continue()")

175
		regs := getRegisters(p, t)
176
		rip := regs.PC()
177

178
		err = p.Step()
D
Derek Parker 已提交
179
		assertNoError(err, t, "Step()")
180

181
		regs = getRegisters(p, t)
182 183 184 185 186
		if rip >= regs.PC() {
			t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
		}
	})
}
187

D
Derek Parker 已提交
188
func TestBreakpoint(t *testing.T) {
D
Derek Parker 已提交
189
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
190
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
D
Derek Parker 已提交
191
		helloworldaddr := helloworldfunc.Entry
192

193 194
		bp, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
D
Derek Parker 已提交
195
		assertNoError(p.Continue(), t, "Continue()")
196

D
Derek Parker 已提交
197
		pc, err := p.PC()
198 199 200
		if err != nil {
			t.Fatal(err)
		}
201

202 203 204 205
		if bp.TotalHitCount != 1 {
			t.Fatalf("Breakpoint should be hit once, got %d\n", bp.TotalHitCount)
		}

D
Derek Parker 已提交
206
		if pc-1 != bp.Addr && pc != bp.Addr {
207
			f, l, _ := p.goSymTable.PCToLine(pc)
D
Derek Parker 已提交
208
			t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
209 210
		}
	})
211
}
212

D
Derek Parker 已提交
213
func TestBreakpointInSeperateGoRoutine(t *testing.T) {
D
Derek Parker 已提交
214
	withTestProcess("testthreads", t, func(p *Process, fixture protest.Fixture) {
215
		fn := p.goSymTable.LookupFunc("main.anotherthread")
216 217 218 219
		if fn == nil {
			t.Fatal("No fn exists")
		}

220
		_, err := p.SetBreakpoint(fn.Entry)
221 222 223 224 225 226 227 228 229
		if err != nil {
			t.Fatal(err)
		}

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

D
Derek Parker 已提交
230
		pc, err := p.PC()
231 232 233 234
		if err != nil {
			t.Fatal(err)
		}

235
		f, l, _ := p.goSymTable.PCToLine(pc)
236 237 238 239 240 241
		if f != "testthreads.go" && l != 8 {
			t.Fatal("Program did not hit breakpoint")
		}
	})
}

D
Derek Parker 已提交
242
func TestBreakpointWithNonExistantFunction(t *testing.T) {
D
Derek Parker 已提交
243
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
244
		_, err := p.SetBreakpoint(0)
245 246 247 248
		if err == nil {
			t.Fatal("Should not be able to break at non existant function")
		}
	})
249
}
250

251
func TestClearBreakpointBreakpoint(t *testing.T) {
D
Derek Parker 已提交
252
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
253
		fn := p.goSymTable.LookupFunc("main.sleepytime")
254 255
		bp, err := p.SetBreakpoint(fn.Entry)
		assertNoError(err, t, "SetBreakpoint()")
256

257 258
		bp, err = p.ClearBreakpoint(fn.Entry)
		assertNoError(err, t, "ClearBreakpoint()")
259

D
Derek Parker 已提交
260
		data, err := dataAtAddr(p.CurrentThread, bp.Addr)
261 262 263 264
		if err != nil {
			t.Fatal(err)
		}

265
		int3 := []byte{0xcc}
266 267 268 269
		if bytes.Equal(data, int3) {
			t.Fatalf("Breakpoint was not cleared data: %#v, int3: %#v", data, int3)
		}

D
Derek Parker 已提交
270
		if len(p.Breakpoints) != 0 {
271 272 273
			t.Fatal("Breakpoint not removed internally")
		}
	})
274
}
275

276 277 278
type nextTest struct {
	begin, end int
}
279

280
func testnext(program string, testcases []nextTest, initialLocation string, t *testing.T) {
D
Derek Parker 已提交
281
	withTestProcess(program, t, func(p *Process, fixture protest.Fixture) {
282
		bp, err := setFunctionBreakpoint(p, initialLocation)
283
		assertNoError(err, t, "SetBreakpoint()")
284
		assertNoError(p.Continue(), t, "Continue()")
285
		p.ClearBreakpoint(bp.Addr)
286
		p.CurrentThread.SetPC(bp.Addr)
287

D
Derek Parker 已提交
288
		f, ln := currentLineNumber(p, t)
289 290
		for _, tc := range testcases {
			if ln != tc.begin {
D
Derek Parker 已提交
291
				t.Fatalf("Program not stopped at correct spot expected %d was %s:%d", tc.begin, filepath.Base(f), ln)
292 293 294 295
			}

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

D
Derek Parker 已提交
296
			f, ln = currentLineNumber(p, t)
297
			if ln != tc.end {
D
Derek Parker 已提交
298
				t.Fatalf("Program did not continue to correct next location expected %d was %s:%d", tc.end, filepath.Base(f), ln)
299 300
			}
		}
301

D
Derek Parker 已提交
302 303
		if len(p.Breakpoints) != 0 {
			t.Fatal("Not all breakpoints were cleaned up", len(p.Breakpoints))
304
		}
305 306
	})
}
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
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},
	}
325
	testnext("testnextprog", testcases, "main.testnext", t)
326 327
}

328 329 330 331 332 333
func TestNextConcurrent(t *testing.T) {
	testcases := []nextTest{
		{9, 10},
		{10, 11},
	}
	withTestProcess("parallel_next", t, func(p *Process, fixture protest.Fixture) {
334
		bp, err := setFunctionBreakpoint(p, "main.sayhi")
335 336 337
		assertNoError(err, t, "SetBreakpoint")
		assertNoError(p.Continue(), t, "Continue")
		f, ln := currentLineNumber(p, t)
338
		initV, err := evalVariable(p, "n")
339
		initVval, _ := constant.Int64Val(initV.Value)
340
		assertNoError(err, t, "EvalVariable")
341 342
		_, err = p.ClearBreakpoint(bp.Addr)
		assertNoError(err, t, "ClearBreakpoint()")
343
		for _, tc := range testcases {
344 345
			g, err := p.CurrentThread.GetG()
			assertNoError(err, t, "GetG()")
D
Derek Parker 已提交
346 347
			if p.SelectedGoroutine.ID != g.ID {
				t.Fatalf("SelectedGoroutine not CurrentThread's goroutine: %d %d", g.ID, p.SelectedGoroutine.ID)
348
			}
349 350 351 352 353 354 355 356
			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)
			}
357
			v, err := evalVariable(p, "n")
358
			assertNoError(err, t, "EvalVariable")
359 360
			vval, _ := constant.Int64Val(v.Value)
			if vval != initVval {
361 362 363 364 365 366
				t.Fatal("Did not end up on same goroutine")
			}
		}
	})
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
func TestNextConcurrentVariant2(t *testing.T) {
	// Just like TestNextConcurrent but instead of removing the initial breakpoint we check that when it happens is for other goroutines
	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 := evalVariable(p, "n")
		initVval, _ := constant.Int64Val(initV.Value)
		assertNoError(err, t, "EvalVariable")
		for _, tc := range testcases {
			g, err := p.CurrentThread.GetG()
			assertNoError(err, t, "GetG()")
D
Derek Parker 已提交
384 385
			if p.SelectedGoroutine.ID != g.ID {
				t.Fatalf("SelectedGoroutine not CurrentThread's goroutine: %d %d", g.ID, p.SelectedGoroutine.ID)
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
			}
			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")
			var vval int64
			for {
				v, err := evalVariable(p, "n")
				assertNoError(err, t, "EvalVariable")
				vval, _ = constant.Int64Val(v.Value)
				if p.CurrentThread.CurrentBreakpoint == nil {
					if vval != initVval {
						t.Fatal("Did not end up on same goroutine")
					}
					break
				} else {
					if vval == initVval {
						t.Fatal("Initial breakpoint triggered twice for the same goroutine")
					}
					assertNoError(p.Continue(), t, "Continue 2")
				}
			}
			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)
			}
		}
	})
}

416 417
func TestNextFunctionReturn(t *testing.T) {
	testcases := []nextTest{
D
Derek Parker 已提交
418 419
		{14, 15},
		{15, 35},
420
	}
421 422 423 424 425
	testnext("testnextprog", testcases, "main.helloworld", t)
}

func TestNextFunctionReturnDefer(t *testing.T) {
	testcases := []nextTest{
D
Derek Parker 已提交
426 427 428 429
		{8, 9},
		{9, 10},
		{10, 7},
		{7, 8},
430 431
	}
	testnext("testnextdefer", testcases, "main.main", t)
432 433
}

D
Derek Parker 已提交
434 435 436 437 438 439 440 441 442 443 444 445
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 已提交
446
				conn, err := net.Dial("tcp", ":9191")
D
Derek Parker 已提交
447 448 449 450 451 452
				if err == nil {
					conn.Close()
					break
				}
				time.Sleep(50 * time.Millisecond)
			}
D
Derek Parker 已提交
453
			http.Get("http://localhost:9191")
D
Derek Parker 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		}()
		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 已提交
474
func TestRuntimeBreakpoint(t *testing.T) {
D
Derek Parker 已提交
475
	withTestProcess("testruntimebreakpoint", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
		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")
		}
	})
}

491
func TestFindReturnAddress(t *testing.T) {
D
Derek Parker 已提交
492
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
493
		start, _, err := p.goSymTable.LineToPC(fixture.Source, 24)
494 495 496
		if err != nil {
			t.Fatal(err)
		}
497
		_, err = p.SetBreakpoint(start)
498 499 500 501 502 503 504
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
505
		addr, err := p.CurrentThread.ReturnAddress()
506 507 508
		if err != nil {
			t.Fatal(err)
		}
509 510 511
		_, l, _ := p.goSymTable.PCToLine(addr)
		if l != 40 {
			t.Fatalf("return address not found correctly, expected line 40")
512
		}
513 514
	})
}
515

516 517 518 519 520 521 522 523
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 {
524 525
			t.Fatal(err)
		}
526
		if err := p.Continue(); err != nil {
D
Derek Parker 已提交
527 528
			t.Fatal(err)
		}
529 530
		if _, err := p.CurrentThread.ReturnAddress(); err == nil {
			t.Fatal("expected error to be returned")
531 532 533
		}
	})
}
D
Derek Parker 已提交
534 535

func TestSwitchThread(t *testing.T) {
D
Derek Parker 已提交
536
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
537 538 539 540 541
		// With invalid thread id
		err := p.SwitchThread(-1)
		if err == nil {
			t.Fatal("Expected error for invalid thread id")
		}
542
		pc, err := p.FindFunctionLocation("main.main", true, 0)
D
Derek Parker 已提交
543 544 545
		if err != nil {
			t.Fatal(err)
		}
546
		_, err = p.SetBreakpoint(pc)
D
Derek Parker 已提交
547 548 549 550 551 552 553 554
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		var nt int
D
Derek Parker 已提交
555
		ct := p.CurrentThread.ID
D
Dan Mace 已提交
556
		for tid := range p.Threads {
D
Derek Parker 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569
			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)
		}
D
Derek Parker 已提交
570
		if p.CurrentThread.ID != nt {
D
Derek Parker 已提交
571 572 573 574
			t.Fatal("Did not switch threads")
		}
	})
}
A
aarzilli 已提交
575

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
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 已提交
603 604 605 606 607
type loc struct {
	line int
	fn   string
}

608
func (l1 *loc) match(l2 Stackframe) bool {
A
aarzilli 已提交
609
	if l1.line >= 0 {
610
		if l1.line != l2.Call.Line {
A
aarzilli 已提交
611 612 613
			return false
		}
	}
614
	return l1.fn == l2.Call.Fn.Name
A
aarzilli 已提交
615 616 617 618
}

func TestStacktrace(t *testing.T) {
	stacks := [][]loc{
D
Derek Parker 已提交
619 620
		{{4, "main.stacktraceme"}, {8, "main.func1"}, {16, "main.main"}},
		{{4, "main.stacktraceme"}, {8, "main.func1"}, {12, "main.func2"}, {17, "main.main"}},
A
aarzilli 已提交
621
	}
D
Derek Parker 已提交
622
	withTestProcess("stacktraceprog", t, func(p *Process, fixture protest.Fixture) {
623
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
624 625 626 627
		assertNoError(err, t, "BreakByLocation()")

		for i := range stacks {
			assertNoError(p.Continue(), t, "Continue()")
D
Derek Parker 已提交
628
			locations, err := p.CurrentThread.Stacktrace(40)
A
aarzilli 已提交
629 630 631 632 633 634
			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)
			}

635 636 637 638
			t.Logf("Stacktrace %d:\n", i)
			for i := range locations {
				t.Logf("\t%s:%d\n", locations[i].Call.File, locations[i].Call.Line)
			}
639

A
aarzilli 已提交
640 641 642 643 644 645 646
			for j := range stacks[i] {
				if !stacks[i][j].match(locations[j]) {
					t.Fatalf("Wrong stack trace pos %d\n", j)
				}
			}
		}

647
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
648 649 650 651
		p.Continue()
	})
}

652 653 654 655 656 657
func TestStacktrace2(t *testing.T) {
	withTestProcess("retstack", t, func(p *Process, fixture protest.Fixture) {
		assertNoError(p.Continue(), t, "Continue()")

		locations, err := p.CurrentThread.Stacktrace(40)
		assertNoError(err, t, "Stacktrace()")
658
		if !stackMatch([]loc{{-1, "main.f"}, {16, "main.main"}}, locations, false) {
659 660 661
			for i := range locations {
				t.Logf("\t%s:%d [%s]\n", locations[i].Call.File, locations[i].Call.Line, locations[i].Call.Fn.Name)
			}
D
Derek Parker 已提交
662
			t.Fatalf("Stack error at main.f()\n%v\n", locations)
663 664 665 666 667
		}

		assertNoError(p.Continue(), t, "Continue()")
		locations, err = p.CurrentThread.Stacktrace(40)
		assertNoError(err, t, "Stacktrace()")
668
		if !stackMatch([]loc{{-1, "main.g"}, {17, "main.main"}}, locations, false) {
669 670 671
			for i := range locations {
				t.Logf("\t%s:%d [%s]\n", locations[i].Call.File, locations[i].Call.Line, locations[i].Call.Fn.Name)
			}
D
Derek Parker 已提交
672
			t.Fatalf("Stack error at main.g()\n%v\n", locations)
673 674 675 676 677
		}
	})

}

678
func stackMatch(stack []loc, locations []Stackframe, skipRuntime bool) bool {
A
aarzilli 已提交
679 680 681
	if len(stack) > len(locations) {
		return false
	}
682 683 684 685 686 687 688 689 690 691 692
	i := 0
	for j := range locations {
		if i >= len(stack) {
			break
		}
		if skipRuntime {
			if locations[j].Call.Fn == nil || strings.HasPrefix(locations[j].Call.Fn.Name, "runtime.") {
				continue
			}
		}
		if !stack[i].match(locations[j]) {
A
aarzilli 已提交
693 694
			return false
		}
695
		i++
A
aarzilli 已提交
696
	}
697
	return i >= len(stack)
A
aarzilli 已提交
698 699 700
}

func TestStacktraceGoroutine(t *testing.T) {
701 702 703
	mainStack := []loc{{13, "main.stacktraceme"}, {26, "main.main"}}
	agoroutineStackA := []loc{{9, "main.agoroutine"}}
	agoroutineStackB := []loc{{10, "main.agoroutine"}}
A
aarzilli 已提交
704

D
Derek Parker 已提交
705
	withTestProcess("goroutinestackprog", t, func(p *Process, fixture protest.Fixture) {
706
		bp, err := setFunctionBreakpoint(p, "main.stacktraceme")
A
aarzilli 已提交
707 708 709 710 711 712 713 714 715 716
		assertNoError(err, t, "BreakByLocation()")

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

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

		agoroutineCount := 0
		mainCount := 0

D
Derek Parker 已提交
717 718
		for i, g := range gs {
			locations, err := p.GoroutineStacktrace(g, 40)
A
aarzilli 已提交
719 720
			assertNoError(err, t, "GoroutineStacktrace()")

721
			if stackMatch(mainStack, locations, false) {
A
aarzilli 已提交
722 723 724
				mainCount++
			}

725 726 727
			if stackMatch(agoroutineStackA, locations, true) {
				agoroutineCount++
			} else if stackMatch(agoroutineStackB, locations, true) {
A
aarzilli 已提交
728 729
				agoroutineCount++
			} else {
D
Derek Parker 已提交
730
				t.Logf("Non-goroutine stack: %d (%d)", i, len(locations))
A
aarzilli 已提交
731 732
				for i := range locations {
					name := ""
733 734
					if locations[i].Call.Fn != nil {
						name = locations[i].Call.Fn.Name
A
aarzilli 已提交
735
					}
736
					t.Logf("\t%s:%d %s\n", locations[i].Call.File, locations[i].Call.Line, name)
A
aarzilli 已提交
737 738 739 740 741
				}
			}
		}

		if mainCount != 1 {
742
			t.Fatalf("Main goroutine stack not found %d", mainCount)
A
aarzilli 已提交
743 744 745 746 747 748
		}

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

749
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
750 751 752
		p.Continue()
	})
}
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

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

func testGSupportFunc(name string, t *testing.T, p *Process, fixture protest.Fixture) {
772
	bp, err := setFunctionBreakpoint(p, "main.main")
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	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)
	})

794 795 796 797 798
	// 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
	}

799 800 801 802
	withTestProcess("cgotest", t, func(p *Process, fixture protest.Fixture) {
		testGSupportFunc("cgo", t, p, fixture)
	})
}
803 804 805

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

809
		bp2, err := setFunctionBreakpoint(p, "main.sayhi")
810 811 812 813 814 815
		assertNoError(err, t, "BreakByLocation()")

		mainCount := 0
		sayhiCount := 0
		for {
			err := p.Continue()
816
			if p.Exited() {
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
				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)
		}
	})
}
839

840
func versionAfterOrEqual(t *testing.T, verStr string, ver GoVersion) {
841 842 843 844
	pver, ok := parseVersionString(verStr)
	if !ok {
		t.Fatalf("Could not parse version string <%s>", verStr)
	}
845
	if !pver.AfterOrEqual(ver) {
846 847 848 849 850 851
		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 已提交
852
	versionAfterOrEqual(t, "go1.4", GoVersion{1, 4, 0, 0, 0})
D
Derek Parker 已提交
853 854 855 856
	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})
857 858 859 860 861 862 863 864
	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")
	}
}
865 866 867 868 869 870 871 872 873 874 875 876 877 878

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)
		}
	})
}
879 880 881 882 883 884 885 886 887 888

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)
		}
	})
}
889 890 891 892 893 894 895 896 897 898

func TestIssue239(t *testing.T) {
	withTestProcess("is sue239", t, func(p *Process, fixture protest.Fixture) {
		pos, _, err := p.goSymTable.LineToPC(fixture.Source, 17)
		assertNoError(err, t, "LineToPC()")
		_, err = p.SetBreakpoint(pos)
		assertNoError(err, t, fmt.Sprintf("SetBreakpoint(%d)", pos))
		assertNoError(p.Continue(), t, fmt.Sprintf("Continue()"))
	})
}
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

func evalVariable(p *Process, symbol string) (*Variable, error) {
	scope, err := p.CurrentThread.Scope()
	if err != nil {
		return nil, err
	}
	return scope.EvalVariable(symbol)
}

func setVariable(p *Process, symbol, value string) error {
	scope, err := p.CurrentThread.Scope()
	if err != nil {
		return err
	}
	return scope.SetVariable(symbol, value)
}

func TestVariableEvaluation(t *testing.T) {
	testcases := []struct {
		name        string
		st          reflect.Kind
		value       interface{}
		length, cap int64
		childrenlen int
	}{
		{"a1", reflect.String, "foofoofoofoofoofoo", 18, 0, 0},
		{"a11", reflect.Array, nil, 3, -1, 3},
		{"a12", reflect.Slice, nil, 2, 2, 2},
		{"a13", reflect.Slice, nil, 3, 3, 3},
		{"a2", reflect.Int, int64(6), 0, 0, 0},
		{"a3", reflect.Float64, float64(7.23), 0, 0, 0},
		{"a4", reflect.Array, nil, 2, -1, 2},
		{"a5", reflect.Slice, nil, 5, 5, 5},
		{"a6", reflect.Struct, nil, 2, 0, 2},
		{"a7", reflect.Ptr, nil, 1, 0, 1},
		{"a8", reflect.Struct, nil, 2, 0, 2},
		{"a9", reflect.Ptr, nil, 1, 0, 1},
		{"baz", reflect.String, "bazburzum", 9, 0, 0},
		{"neg", reflect.Int, int64(-1), 0, 0, 0},
		{"f32", reflect.Float32, float64(float32(1.2)), 0, 0, 0},
A
aarzilli 已提交
939 940
		{"c64", reflect.Complex64, complex128(complex64(1 + 2i)), 0, 0, 0},
		{"c128", reflect.Complex128, complex128(2 + 3i), 0, 0, 0},
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
		{"a6.Baz", reflect.Int, int64(8), 0, 0, 0},
		{"a7.Baz", reflect.Int, int64(5), 0, 0, 0},
		{"a8.Baz", reflect.String, "feh", 3, 0, 0},
		{"a8", reflect.Struct, nil, 2, 0, 2},
		{"i32", reflect.Array, nil, 2, -1, 2},
		{"b1", reflect.Bool, true, 0, 0, 0},
		{"b2", reflect.Bool, false, 0, 0, 0},
		{"f", reflect.Func, "main.barfoo", 0, 0, 0},
		{"ba", reflect.Slice, nil, 200, 200, 64},
	}

	withTestProcess("testvariables", t, func(p *Process, fixture protest.Fixture) {
		assertNoError(p.Continue(), t, "Continue() returned an error")

		for _, tc := range testcases {
			v, err := evalVariable(p, tc.name)
			assertNoError(err, t, fmt.Sprintf("EvalVariable(%s)", tc.name))

			if v.Kind != tc.st {
				t.Fatalf("%s simple type: expected: %s got: %s", tc.name, tc.st, v.Kind.String())
			}
			if v.Value == nil && tc.value != nil {
				t.Fatalf("%s value: expected: %v got: %v", tc.name, tc.value, v.Value)
			} else {
965 966 967
				switch v.Kind {
				case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
					x, _ := constant.Int64Val(v.Value)
968 969 970
					if y, ok := tc.value.(int64); !ok || x != y {
						t.Fatalf("%s value: expected: %v got: %v", tc.name, tc.value, v.Value)
					}
971 972
				case reflect.Float32, reflect.Float64:
					x, _ := constant.Float64Val(v.Value)
973 974 975
					if y, ok := tc.value.(float64); !ok || x != y {
						t.Fatalf("%s value: expected: %v got: %v", tc.name, tc.value, v.Value)
					}
A
aarzilli 已提交
976 977 978 979 980 981
				case reflect.Complex64, reflect.Complex128:
					xr, _ := constant.Float64Val(constant.Real(v.Value))
					xi, _ := constant.Float64Val(constant.Imag(v.Value))
					if y, ok := tc.value.(complex128); !ok || complex(xr, xi) != y {
						t.Fatalf("%s value: expected: %v got: %v", tc.name, tc.value, v.Value)
					}
982 983
				case reflect.String:
					if y, ok := tc.value.(string); !ok || constant.StringVal(v.Value) != y {
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
						t.Fatalf("%s value: expected: %v got: %v", tc.name, tc.value, v.Value)
					}
				}
			}
			if v.Len != tc.length {
				t.Fatalf("%s len: expected: %d got: %d", tc.name, tc.length, v.Len)
			}
			if v.Cap != tc.cap {
				t.Fatalf("%s cap: expected: %d got: %d", tc.name, tc.cap, v.Cap)
			}
			if len(v.Children) != tc.childrenlen {
				t.Fatalf("%s children len: expected %d got: %d", tc.name, tc.childrenlen, len(v.Children))
			}
		}
	})
}

func TestFrameEvaluation(t *testing.T) {
	withTestProcess("goroutinestackprog", t, func(p *Process, fixture protest.Fixture) {
		_, err := setFunctionBreakpoint(p, "main.stacktraceme")
		assertNoError(err, t, "setFunctionBreakpoint")
		assertNoError(p.Continue(), t, "Continue()")

1007
		// Testing evaluation on goroutines
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
		gs, err := p.GoroutinesInfo()
		assertNoError(err, t, "GoroutinesInfo")
		found := make([]bool, 10)
		for _, g := range gs {
			frame := -1
			frames, err := p.GoroutineStacktrace(g, 10)
			assertNoError(err, t, "GoroutineStacktrace()")
			for i := range frames {
				if frames[i].Call.Fn != nil && frames[i].Call.Fn.Name == "main.agoroutine" {
					frame = i
					break
				}
			}

			if frame < 0 {
D
Derek Parker 已提交
1023
				t.Logf("Goroutine %d: could not find correct frame", g.ID)
1024 1025 1026
				continue
			}

D
Derek Parker 已提交
1027
			scope, err := p.ConvertEvalScope(g.ID, frame)
1028 1029 1030 1031 1032
			assertNoError(err, t, "ConvertEvalScope()")
			t.Logf("scope = %v", scope)
			v, err := scope.EvalVariable("i")
			t.Logf("v = %v", v)
			if err != nil {
D
Derek Parker 已提交
1033
				t.Logf("Goroutine %d: %v\n", g.ID, err)
1034 1035
				continue
			}
1036 1037
			vval, _ := constant.Int64Val(v.Value)
			found[vval] = true
1038 1039 1040 1041 1042 1043 1044 1045
		}

		for i := range found {
			if !found[i] {
				t.Fatalf("Goroutine %d not found\n", i)
			}
		}

1046
		// Testing evaluation on frames
1047 1048 1049 1050 1051
		assertNoError(p.Continue(), t, "Continue() 2")
		g, err := p.CurrentThread.GetG()
		assertNoError(err, t, "GetG()")

		for i := 0; i <= 3; i++ {
D
Derek Parker 已提交
1052
			scope, err := p.ConvertEvalScope(g.ID, i+1)
1053 1054 1055
			assertNoError(err, t, fmt.Sprintf("ConvertEvalScope() on frame %d", i+1))
			v, err := scope.EvalVariable("n")
			assertNoError(err, t, fmt.Sprintf("EvalVariable() on frame %d", i+1))
1056
			n, _ := constant.Int64Val(v.Value)
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
			t.Logf("frame %d n %d\n", i+1, n)
			if n != int64(3-i) {
				t.Fatalf("On frame %d value of n is %d (not %d)", i+1, n, 3-i)
			}
		}
	})
}

func TestPointerSetting(t *testing.T) {
	withTestProcess("testvariables3", t, func(p *Process, fixture protest.Fixture) {
		assertNoError(p.Continue(), t, "Continue() returned an error")

		pval := func(n int64) {
			variable, err := evalVariable(p, "p1")
			assertNoError(err, t, "EvalVariable()")
1072 1073 1074
			c0val, _ := constant.Int64Val(variable.Children[0].Value)
			if c0val != n {
				t.Fatalf("Wrong value of p1, *%d expected *%d", c0val, n)
1075 1076 1077 1078 1079 1080 1081 1082
			}
		}

		pval(1)

		// change p1 to point to i2
		scope, err := p.CurrentThread.Scope()
		assertNoError(err, t, "Scope()")
A
aarzilli 已提交
1083 1084 1085
		i2addr, err := scope.EvalExpression("i2")
		assertNoError(err, t, "EvalExpression()")
		assertNoError(setVariable(p, "p1", fmt.Sprintf("(*int)(0x%x)", i2addr.Addr)), t, "SetVariable()")
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
		pval(2)

		// change the value of i2 check that p1 also changes
		assertNoError(setVariable(p, "i2", "5"), t, "SetVariable()")
		pval(5)
	})
}

func TestVariableFunctionScoping(t *testing.T) {
	withTestProcess("testvariables", t, func(p *Process, fixture protest.Fixture) {
		err := p.Continue()
		assertNoError(err, t, "Continue() returned an error")

		_, err = evalVariable(p, "a1")
		assertNoError(err, t, "Unable to find variable a1")

		_, err = evalVariable(p, "a2")
		assertNoError(err, t, "Unable to find variable a1")

		// Move scopes, a1 exists here by a2 does not
		err = p.Continue()
		assertNoError(err, t, "Continue() returned an error")

		_, err = evalVariable(p, "a1")
		assertNoError(err, t, "Unable to find variable a1")

		_, err = evalVariable(p, "a2")
		if err == nil {
			t.Fatalf("Can eval out of scope variable a2")
		}
	})
}

func TestRecursiveStructure(t *testing.T) {
	withTestProcess("testvariables2", t, func(p *Process, fixture protest.Fixture) {
		assertNoError(p.Continue(), t, "Continue()")
		v, err := evalVariable(p, "aas")
		assertNoError(err, t, "EvalVariable()")
		t.Logf("v: %v\n", v)
	})
}
1127 1128 1129 1130 1131 1132 1133 1134 1135

func TestIssue316(t *testing.T) {
	// A pointer loop that includes one interface should not send dlv into an infinite loop
	withTestProcess("testvariables3", t, func(p *Process, fixture protest.Fixture) {
		assertNoError(p.Continue(), t, "Continue()")
		_, err := evalVariable(p, "iface5")
		assertNoError(err, t, "EvalVariable()")
	})
}
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149

func TestIssue325(t *testing.T) {
	// nil pointer dereference when evaluating interfaces to function pointers
	withTestProcess("testvariables3", t, func(p *Process, fixture protest.Fixture) {
		assertNoError(p.Continue(), t, "Continue()")
		iface2fn1v, err := evalVariable(p, "iface2fn1")
		assertNoError(err, t, "EvalVariable()")
		t.Logf("iface2fn1: %v\n", iface2fn1v)

		iface2fn2v, err := evalVariable(p, "iface2fn2")
		assertNoError(err, t, "EvalVariable()")
		t.Logf("iface2fn2: %v\n", iface2fn2v)
	})
}
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

func TestBreakpointCounts(t *testing.T) {
	withTestProcess("bpcountstest", t, func(p *Process, fixture protest.Fixture) {
		addr, _, err := p.goSymTable.LineToPC(fixture.Source, 12)
		assertNoError(err, t, "LineToPC")
		bp, err := p.SetBreakpoint(addr)
		assertNoError(err, t, "SetBreakpoint()")

		for {
			if err := p.Continue(); err != nil {
				if _, exited := err.(ProcessExitedError); exited {
					break
				}
				assertNoError(err, t, "Continue()")
			}
		}

		t.Logf("TotalHitCount: %d", bp.TotalHitCount)
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
		if bp.TotalHitCount != 200 {
			t.Fatalf("Wrong TotalHitCount for the breakpoint (%d)", bp.TotalHitCount)
		}

		if len(bp.HitCount) != 2 {
			t.Fatalf("Wrong number of goroutines for breakpoint (%d)", len(bp.HitCount))
		}

		for _, v := range bp.HitCount {
			if v != 100 {
				t.Fatalf("Wrong HitCount for breakpoint (%v)", bp.HitCount)
			}
		}
	})
}

const doTestBreakpointCountsWithDetection = false

func TestBreakpointCountsWithDetection(t *testing.T) {
	if !doTestBreakpointCountsWithDetection {
		return
	}
	m := map[int64]int64{}
	withTestProcess("bpcountstest", t, func(p *Process, fixture protest.Fixture) {
		addr, _, err := p.goSymTable.LineToPC(fixture.Source, 12)
		assertNoError(err, t, "LineToPC")
		bp, err := p.SetBreakpoint(addr)
		assertNoError(err, t, "SetBreakpoint()")

		for {
			if err := p.Continue(); err != nil {
				if _, exited := err.(ProcessExitedError); exited {
					break
				}
				assertNoError(err, t, "Continue()")
			}
			fmt.Printf("Continue returned %d\n", bp.TotalHitCount)
			for _, th := range p.Threads {
				if th.CurrentBreakpoint == nil {
					continue
				}
				scope, err := th.Scope()
				assertNoError(err, t, "Scope()")
				v, err := scope.EvalVariable("i")
				assertNoError(err, t, "evalVariable")
				i, _ := constant.Int64Val(v.Value)
				v, err = scope.EvalVariable("id")
				assertNoError(err, t, "evalVariable")
				id, _ := constant.Int64Val(v.Value)
				m[id] = i
D
Derek Parker 已提交
1218
				fmt.Printf("\tgoroutine (%d) %d: %d\n", th.ID, id, i)
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
			}

			total := int64(0)
			for i := range m {
				total += m[i] + 1
			}

			if uint64(total) != bp.TotalHitCount {
				t.Fatalf("Mismatched total count %d %d\n", total, bp.TotalHitCount)
			}
		}

		t.Logf("TotalHitCount: %d", bp.TotalHitCount)
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
		if bp.TotalHitCount != 200 {
			t.Fatalf("Wrong TotalHitCount for the breakpoint (%d)", bp.TotalHitCount)
		}

		if len(bp.HitCount) != 2 {
			t.Fatalf("Wrong number of goroutines for breakpoint (%d)", len(bp.HitCount))
		}

		for _, v := range bp.HitCount {
			if v != 100 {
				t.Fatalf("Wrong HitCount for breakpoint (%v)", bp.HitCount)
			}
		}
	})
}
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

func TestIssue262(t *testing.T) {
	// Continue does not work when the current breakpoint is set on a NOP instruction
	withTestProcess("issue262", t, func(p *Process, fixture protest.Fixture) {
		addr, _, err := p.goSymTable.LineToPC(fixture.Source, 11)
		assertNoError(err, t, "LineToPC")
		_, err = p.SetBreakpoint(addr)
		assertNoError(err, t, "SetBreakpoint()")

		assertNoError(p.Continue(), t, "Continue()")
		err = p.Continue()
		if err == nil {
			t.Fatalf("No error on second continue")
		}
		_, exited := err.(ProcessExitedError)
		if !exited {
			t.Fatalf("Process did not exit after second continue: %v", err)
		}
	})
}
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277

func TestIssue341(t *testing.T) {
	// pointer loop through map entries
	withTestProcess("testvariables3", t, func(p *Process, fixture protest.Fixture) {
		assertNoError(p.Continue(), t, "Continue()")
		t.Logf("requesting mapinf")
		mapinf, err := evalVariable(p, "mapinf")
		assertNoError(err, t, "EvalVariable()")
		t.Logf("mapinf: %v\n", mapinf)
	})
}