integration1_test.go 32.2 KB
Newer Older
1
package service_test
D
Dan Mace 已提交
2 3

import (
A
aarzilli 已提交
4
	"fmt"
5
	"math/rand"
D
Dan Mace 已提交
6
	"net"
7
	"path/filepath"
8
	"runtime"
A
aarzilli 已提交
9
	"strconv"
L
Luke Hoban 已提交
10
	"strings"
D
Dan Mace 已提交
11
	"testing"
12
	"time"
D
Dan Mace 已提交
13

D
Derek Parker 已提交
14
	protest "github.com/derekparker/delve/pkg/proc/test"
D
Derek Parker 已提交
15

16
	"github.com/derekparker/delve/pkg/goversion"
D
Dan Mace 已提交
17 18
	"github.com/derekparker/delve/service"
	"github.com/derekparker/delve/service/api"
19
	"github.com/derekparker/delve/service/rpc1"
A
aarzilli 已提交
20
	"github.com/derekparker/delve/service/rpccommon"
D
Dan Mace 已提交
21 22
)

23
func withTestClient1(name string, t *testing.T, fn func(c *rpc1.RPCClient)) {
24 25 26
	if testBackend == "rr" {
		protest.MustHaveRecordingAllowed(t)
	}
D
Dan Mace 已提交
27 28 29 30
	listener, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		t.Fatalf("couldn't start listener: %s\n", err)
	}
D
Derek Parker 已提交
31
	defer listener.Close()
A
aarzilli 已提交
32
	server := rpccommon.NewServer(&service.Config{
33
		Listener:    listener,
34
		ProcessArgs: []string{protest.BuildFixture(name, 0).Path},
35
		Backend:     testBackend,
D
Derek Parker 已提交
36
	})
37 38 39
	if err := server.Run(); err != nil {
		t.Fatal(err)
	}
40
	client := rpc1.NewClient(listener.Addr().String())
41 42 43 44
	defer func() {
		client.Detach(true)
	}()

45
	fn(client)
D
Dan Mace 已提交
46 47
}

48
func Test1RunWithInvalidPath(t *testing.T) {
49 50 51 52 53 54
	if testBackend == "rr" {
		// This test won't work because rr returns an error, after recording, when
		// the recording failed but also when the recording succeeded but the
		// inferior returned an error. Therefore we have to ignore errors from rr.
		return
	}
55 56 57 58 59
	listener, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		t.Fatalf("couldn't start listener: %s\n", err)
	}
	defer listener.Close()
A
aarzilli 已提交
60
	server := rpccommon.NewServer(&service.Config{
61 62
		Listener:    listener,
		ProcessArgs: []string{"invalid_path"},
63
		Backend:     testBackend,
D
Derek Parker 已提交
64
	})
65 66 67 68 69
	if err := server.Run(); err == nil {
		t.Fatal("Expected Run to return error for invalid program path")
	}
}

70
func Test1Restart_afterExit(t *testing.T) {
71
	withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {
D
Derek Parker 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
		origPid := c.ProcessPid()
		state := <-c.Continue()
		if !state.Exited {
			t.Fatal("expected initial process to have exited")
		}
		if err := c.Restart(); err != nil {
			t.Fatal(err)
		}
		if c.ProcessPid() == origPid {
			t.Fatal("did not spawn new process, has same PID")
		}
		state = <-c.Continue()
		if !state.Exited {
85
			t.Fatalf("expected restarted process to have exited %v", state)
D
Derek Parker 已提交
86 87 88 89
		}
	})
}

90
func Test1Restart_breakpointPreservation(t *testing.T) {
91
	withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {
92 93 94
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Name: "firstbreakpoint", Tracepoint: true})
		assertNoError(err, t, "CreateBreakpoint()")
		stateCh := c.Continue()
A
aarzilli 已提交
95 96

		state := <-stateCh
97 98 99
		if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {
			t.Fatalf("Wrong breakpoint: %#v\n", state.CurrentThread.Breakpoint)
		}
A
aarzilli 已提交
100
		state = <-stateCh
101 102 103
		if !state.Exited {
			t.Fatal("Did not exit after first tracepoint")
		}
A
aarzilli 已提交
104

105 106 107
		t.Log("Restart")
		c.Restart()
		stateCh = c.Continue()
A
aarzilli 已提交
108
		state = <-stateCh
109 110 111
		if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {
			t.Fatalf("Wrong breakpoint (after restart): %#v\n", state.CurrentThread.Breakpoint)
		}
A
aarzilli 已提交
112
		state = <-stateCh
113 114 115 116 117 118
		if !state.Exited {
			t.Fatal("Did not exit after first tracepoint (after restart)")
		}
	})
}

119
func Test1Restart_duringStop(t *testing.T) {
120
	withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {
D
Derek Parker 已提交
121
		origPid := c.ProcessPid()
122
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})
D
Derek Parker 已提交
123 124 125 126
		if err != nil {
			t.Fatal(err)
		}
		state := <-c.Continue()
127
		if state.CurrentThread.Breakpoint == nil {
D
Derek Parker 已提交
128 129 130 131 132 133 134 135 136 137 138 139
			t.Fatal("did not hit breakpoint")
		}
		if err := c.Restart(); err != nil {
			t.Fatal(err)
		}
		if c.ProcessPid() == origPid {
			t.Fatal("did not spawn new process, has same PID")
		}
		bps, err := c.ListBreakpoints()
		if err != nil {
			t.Fatal(err)
		}
140 141
		if len(bps) == 0 {
			t.Fatal("breakpoints not preserved")
D
Derek Parker 已提交
142 143 144 145
		}
	})
}

146
func Test1Restart_attachPid(t *testing.T) {
D
Derek Parker 已提交
147 148
	// Assert it does not work and returns error.
	// We cannot restart a process we did not spawn.
A
aarzilli 已提交
149
	server := rpccommon.NewServer(&service.Config{
D
Derek Parker 已提交
150 151
		Listener:  nil,
		AttachPid: 999,
152
		Backend:   testBackend,
D
Derek Parker 已提交
153
	})
154
	if err := server.Restart(); err == nil {
D
Derek Parker 已提交
155 156 157 158
		t.Fatal("expected error on restart after attaching to pid but got none")
	}
}

159
func Test1ClientServer_exit(t *testing.T) {
160
	withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {
D
Dan Mace 已提交
161 162 163 164 165 166 167
		state, err := c.GetState()
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
		if e, a := false, state.Exited; e != a {
			t.Fatalf("Expected exited %v, got %v", e, a)
		}
A
aarzilli 已提交
168
		state = <-c.Continue()
169 170
		if state.Err == nil {
			t.Fatalf("Error expected after continue")
D
Derek Parker 已提交
171
		}
A
aarzilli 已提交
172 173
		if !state.Exited {
			t.Fatalf("Expected exit after continue: %v", state)
D
Derek Parker 已提交
174
		}
175
		_, err = c.GetState()
176 177
		if err == nil {
			t.Fatal("Expected error on querying state from exited process")
D
Dan Mace 已提交
178 179 180 181
		}
	})
}

182
func Test1ClientServer_step(t *testing.T) {
183
	withTestClient1("testprog", t, func(c *rpc1.RPCClient) {
184
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: -1})
D
Dan Mace 已提交
185 186 187 188
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

D
Derek Parker 已提交
189
		stateBefore := <-c.Continue()
A
aarzilli 已提交
190 191
		if stateBefore.Err != nil {
			t.Fatalf("Unexpected error: %v", stateBefore.Err)
D
Dan Mace 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205
		}

		stateAfter, err := c.Step()
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		if before, after := stateBefore.CurrentThread.PC, stateAfter.CurrentThread.PC; before >= after {
			t.Errorf("Expected %#v to be greater than %#v", before, after)
		}
	})
}

func testnext(testcases []nextTest, initialLocation string, t *testing.T) {
206
	withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {
207
		bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: initialLocation, Line: -1})
D
Dan Mace 已提交
208 209 210 211
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

A
aarzilli 已提交
212 213 214
		state := <-c.Continue()
		if state.Err != nil {
			t.Fatalf("Unexpected error: %v", state.Err)
D
Dan Mace 已提交
215 216
		}

D
Derek Parker 已提交
217
		_, err = c.ClearBreakpoint(bp.ID)
D
Dan Mace 已提交
218 219 220 221 222 223
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		for _, tc := range testcases {
			if state.CurrentThread.Line != tc.begin {
D
Dan Mace 已提交
224
				t.Fatalf("Program not stopped at correct spot expected %d was %d", tc.begin, state.CurrentThread.Line)
D
Dan Mace 已提交
225 226 227 228 229 230 231 232 233
			}

			t.Logf("Next for scenario %#v", tc)
			state, err = c.Next()
			if err != nil {
				t.Fatalf("Unexpected error: %v", err)
			}

			if state.CurrentThread.Line != tc.end {
D
Dan Mace 已提交
234
				t.Fatalf("Program did not continue to correct next location expected %d was %d", tc.end, state.CurrentThread.Line)
D
Dan Mace 已提交
235 236 237 238 239
			}
		}
	})
}

240
func Test1NextGeneral(t *testing.T) {
241 242
	var testcases []nextTest

243
	ver, _ := goversion.Parse(runtime.Version())
244

245
	if ver.Major < 0 || ver.AfterOrEqual(goversion.GoVersion{1, 7, -1, 0, 0, ""}) {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		testcases = []nextTest{
			{17, 19},
			{19, 20},
			{20, 23},
			{23, 24},
			{24, 26},
			{26, 31},
			{31, 23},
			{23, 24},
			{24, 26},
			{26, 31},
			{31, 23},
			{23, 24},
			{24, 26},
			{26, 27},
			{27, 28},
			{28, 34},
		}
	} else {
		testcases = []nextTest{
			{17, 19},
			{19, 20},
			{20, 23},
			{23, 24},
			{24, 26},
			{26, 31},
			{31, 23},
			{23, 24},
			{24, 26},
			{26, 31},
			{31, 23},
			{23, 24},
			{24, 26},
			{26, 27},
			{27, 34},
		}
D
Dan Mace 已提交
282
	}
283

D
Dan Mace 已提交
284 285 286
	testnext(testcases, "main.testnext", t)
}

287
func Test1NextFunctionReturn(t *testing.T) {
D
Dan Mace 已提交
288
	testcases := []nextTest{
289
		{13, 14},
D
Derek Parker 已提交
290 291
		{14, 15},
		{15, 35},
D
Dan Mace 已提交
292 293 294 295
	}
	testnext(testcases, "main.helloworld", t)
}

296
func Test1ClientServer_breakpointInMainThread(t *testing.T) {
297
	withTestClient1("testprog", t, func(c *rpc1.RPCClient) {
298
		bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: 1})
D
Dan Mace 已提交
299 300 301 302
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

A
aarzilli 已提交
303
		state := <-c.Continue()
D
Dan Mace 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316
		if err != nil {
			t.Fatalf("Unexpected error: %v, state: %#v", err, state)
		}

		pc := state.CurrentThread.PC

		if pc-1 != bp.Addr && pc != bp.Addr {
			f, l := state.CurrentThread.File, state.CurrentThread.Line
			t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
		}
	})
}

317
func Test1ClientServer_breakpointInSeparateGoroutine(t *testing.T) {
318
	withTestClient1("testthreads", t, func(c *rpc1.RPCClient) {
319
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.anotherthread", Line: 1})
D
Dan Mace 已提交
320 321 322 323
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

A
aarzilli 已提交
324 325 326
		state := <-c.Continue()
		if state.Err != nil {
			t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
D
Dan Mace 已提交
327 328 329
		}

		f, l := state.CurrentThread.File, state.CurrentThread.Line
330
		if f != "testthreads.go" && l != 9 {
D
Dan Mace 已提交
331 332 333 334 335
			t.Fatal("Program did not hit breakpoint")
		}
	})
}

336
func Test1ClientServer_breakAtNonexistentPoint(t *testing.T) {
337
	withTestClient1("testprog", t, func(c *rpc1.RPCClient) {
338
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "nowhere", Line: 1})
D
Dan Mace 已提交
339 340 341 342 343 344
		if err == nil {
			t.Fatal("Should not be able to break at non existent function")
		}
	})
}

345
func Test1ClientServer_clearBreakpoint(t *testing.T) {
346
	withTestClient1("testprog", t, func(c *rpc1.RPCClient) {
347
		bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sleepytime", Line: 1})
D
Dan Mace 已提交
348 349 350 351
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

352
		if e, a := 1, countBreakpoints(t, c); e != a {
D
Dan Mace 已提交
353 354 355
			t.Fatalf("Expected breakpoint count %d, got %d", e, a)
		}

D
Derek Parker 已提交
356
		deleted, err := c.ClearBreakpoint(bp.ID)
D
Dan Mace 已提交
357 358 359 360 361 362 363 364
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		if deleted.ID != bp.ID {
			t.Fatalf("Expected deleted breakpoint ID %v, got %v", bp.ID, deleted.ID)
		}

365
		if e, a := 0, countBreakpoints(t, c); e != a {
D
Dan Mace 已提交
366 367 368 369 370
			t.Fatalf("Expected breakpoint count %d, got %d", e, a)
		}
	})
}

371
func Test1ClientServer_switchThread(t *testing.T) {
372
	withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {
D
Dan Mace 已提交
373 374 375 376 377 378
		// With invalid thread id
		_, err := c.SwitchThread(-1)
		if err == nil {
			t.Fatal("Expected error for invalid thread id")
		}

379
		_, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})
D
Dan Mace 已提交
380 381 382
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
A
aarzilli 已提交
383 384 385
		state := <-c.Continue()
		if state.Err != nil {
			t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
D
Dan Mace 已提交
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
		}

		var nt int
		ct := state.CurrentThread.ID
		threads, err := c.ListThreads()
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
		for _, th := range threads {
			if th.ID != ct {
				nt = th.ID
				break
			}
		}
		if nt == 0 {
			t.Fatal("could not find thread to switch to")
		}
		// With valid thread id
		state, err = c.SwitchThread(nt)
		if err != nil {
			t.Fatal(err)
		}
		if state.CurrentThread.ID != nt {
			t.Fatal("Did not switch threads")
		}
	})
}
413

414
func Test1ClientServer_infoLocals(t *testing.T) {
415
	withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {
A
aarzilli 已提交
416 417
		fp := testProgPath(t, "testnextprog")
		_, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 23})
418 419 420
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
A
aarzilli 已提交
421 422 423
		state := <-c.Continue()
		if state.Err != nil {
			t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
424
		}
425
		locals, err := c.ListLocalVariables(api.EvalScope{-1, 0})
426 427 428 429 430 431 432 433 434
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
		if len(locals) != 3 {
			t.Fatalf("Expected 3 locals, got %d %#v", len(locals), locals)
		}
	})
}

435
func Test1ClientServer_infoArgs(t *testing.T) {
436
	withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {
A
aarzilli 已提交
437 438
		fp := testProgPath(t, "testnextprog")
		_, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 47})
439 440 441
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
A
aarzilli 已提交
442 443 444
		state := <-c.Continue()
		if state.Err != nil {
			t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
445
		}
446 447 448 449 450 451 452
		regs, err := c.ListRegisters()
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
		if regs == "" {
			t.Fatal("Expected string showing registers values, got empty string")
		}
453
		locals, err := c.ListFunctionArgs(api.EvalScope{-1, 0})
454 455 456 457 458 459 460 461
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}
		if len(locals) != 2 {
			t.Fatalf("Expected 2 function args, got %d %#v", len(locals), locals)
		}
	})
}
A
aarzilli 已提交
462

463
func Test1ClientServer_traceContinue(t *testing.T) {
464
	withTestClient1("integrationprog", t, func(c *rpc1.RPCClient) {
A
aarzilli 已提交
465
		fp := testProgPath(t, "integrationprog")
D
Derek Parker 已提交
466
		_, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 15, Tracepoint: true, Goroutine: true, Stacktrace: 5, Variables: []string{"i"}})
A
aarzilli 已提交
467 468 469 470
		if err != nil {
			t.Fatalf("Unexpected error: %v\n", err)
		}
		count := 0
D
Derek Parker 已提交
471 472
		contChan := c.Continue()
		for state := range contChan {
473
			if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {
A
aarzilli 已提交
474 475 476 477
				count++

				t.Logf("%v", state)

478
				bpi := state.CurrentThread.BreakpointInfo
A
aarzilli 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

				if bpi.Goroutine == nil {
					t.Fatalf("No goroutine information")
				}

				if len(bpi.Stacktrace) <= 0 {
					t.Fatalf("No stacktrace\n")
				}

				if len(bpi.Variables) != 1 {
					t.Fatalf("Wrong number of variables returned: %d", len(bpi.Variables))
				}

				if bpi.Variables[0].Name != "i" {
					t.Fatalf("Wrong variable returned %s", bpi.Variables[0].Name)
				}

496 497 498 499 500 501
				t.Logf("Variable i is %v", bpi.Variables[0])

				n, err := strconv.Atoi(bpi.Variables[0].Value)

				if err != nil || n != count-1 {
					t.Fatalf("Wrong variable value %q (%v %d)", bpi.Variables[0].Value, err, count)
A
aarzilli 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
				}
			}
			if state.Exited {
				continue
			}
			t.Logf("%v", state)
			if state.Err != nil {
				t.Fatalf("Unexpected error during continue: %v\n", state.Err)
			}

		}

		if count != 3 {
			t.Fatalf("Wrong number of continues hit: %d\n", count)
		}
	})
}
519

520
func Test1ClientServer_traceContinue2(t *testing.T) {
521
	withTestClient1("integrationprog", t, func(c *rpc1.RPCClient) {
522
		bp1, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Tracepoint: true})
523 524 525
		if err != nil {
			t.Fatalf("Unexpected error: %v\n", err)
		}
526
		bp2, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: 1, Tracepoint: true})
527 528 529 530 531 532 533
		if err != nil {
			t.Fatalf("Unexpected error: %v\n", err)
		}
		countMain := 0
		countSayhi := 0
		contChan := c.Continue()
		for state := range contChan {
534 535
			if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {
				switch state.CurrentThread.Breakpoint.ID {
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
				case bp1.ID:
					countMain++
				case bp2.ID:
					countSayhi++
				}

				t.Logf("%v", state)
			}
			if state.Exited {
				continue
			}
			if state.Err != nil {
				t.Fatalf("Unexpected error during continue: %v\n", state.Err)
			}

		}

		if countMain != 1 {
			t.Fatalf("Wrong number of continues (main.main) hit: %d\n", countMain)
		}

		if countSayhi != 3 {
			t.Fatalf("Wrong number of continues (main.sayhi) hit: %d\n", countSayhi)
		}
	})
}
562

563
func Test1ClientServer_FindLocations(t *testing.T) {
564
	withTestClient1("locationsprog", t, func(c *rpc1.RPCClient) {
565 566 567 568
		someFunctionCallAddr := findLocationHelper(t, c, "locationsprog.go:26", false, 1, 0)[0]
		someFunctionLine1 := findLocationHelper(t, c, "locationsprog.go:27", false, 1, 0)[0]
		findLocationHelper(t, c, "anotherFunction:1", false, 1, someFunctionLine1)
		findLocationHelper(t, c, "main.anotherFunction:1", false, 1, someFunctionLine1)
569 570 571 572 573 574 575 576
		findLocationHelper(t, c, "anotherFunction", false, 1, someFunctionCallAddr)
		findLocationHelper(t, c, "main.anotherFunction", false, 1, someFunctionCallAddr)
		findLocationHelper(t, c, fmt.Sprintf("*0x%x", someFunctionCallAddr), false, 1, someFunctionCallAddr)
		findLocationHelper(t, c, "sprog.go:26", true, 0, 0)

		findLocationHelper(t, c, "String", true, 0, 0)
		findLocationHelper(t, c, "main.String", true, 0, 0)

577 578
		someTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:14", false, 1, 0)[0]
		otherTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:18", false, 1, 0)[0]
579 580 581 582 583
		findLocationHelper(t, c, "SomeType.String", false, 1, someTypeStringFuncAddr)
		findLocationHelper(t, c, "(*SomeType).String", false, 1, someTypeStringFuncAddr)
		findLocationHelper(t, c, "main.SomeType.String", false, 1, someTypeStringFuncAddr)
		findLocationHelper(t, c, "main.(*SomeType).String", false, 1, someTypeStringFuncAddr)

584
		// Issue #275
585 586 587 588 589
		readfile := findLocationHelper(t, c, "io/ioutil.ReadFile", false, 1, 0)[0]

		// Issue #296
		findLocationHelper(t, c, "/io/ioutil.ReadFile", false, 1, readfile)
		findLocationHelper(t, c, "ioutil.ReadFile", false, 1, readfile)
590

591 592 593 594 595 596
		stringAddrs := findLocationHelper(t, c, "/^main.*Type.*String$/", false, 2, 0)

		if otherTypeStringFuncAddr != stringAddrs[0] && otherTypeStringFuncAddr != stringAddrs[1] {
			t.Fatalf("Wrong locations returned for \"/.*Type.*String/\", got: %v expected: %v and %v\n", stringAddrs, someTypeStringFuncAddr, otherTypeStringFuncAddr)
		}

A
Alessandro Arzilli 已提交
597
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 4, Tracepoint: false})
598 599 600 601 602 603
		if err != nil {
			t.Fatalf("CreateBreakpoint(): %v\n", err)
		}

		<-c.Continue()

A
Alessandro Arzilli 已提交
604 605 606 607 608
		locationsprog35Addr := findLocationHelper(t, c, "locationsprog.go:35", false, 1, 0)[0]
		findLocationHelper(t, c, fmt.Sprintf("%s:35", testProgPath(t, "locationsprog")), false, 1, locationsprog35Addr)
		findLocationHelper(t, c, "+1", false, 1, locationsprog35Addr)
		findLocationHelper(t, c, "35", false, 1, locationsprog35Addr)
		findLocationHelper(t, c, "-1", false, 1, findLocationHelper(t, c, "locationsprog.go:33", false, 1, 0)[0])
609 610
	})

611
	withTestClient1("testnextdefer", t, func(c *rpc1.RPCClient) {
612
		firstMainLine := findLocationHelper(t, c, "testnextdefer.go:5", false, 1, 0)[0]
613 614 615
		findLocationHelper(t, c, "main.main", false, 1, firstMainLine)
	})

616
	withTestClient1("stacktraceprog", t, func(c *rpc1.RPCClient) {
617 618 619
		stacktracemeAddr := findLocationHelper(t, c, "stacktraceprog.go:4", false, 1, 0)[0]
		findLocationHelper(t, c, "main.stacktraceme", false, 1, stacktracemeAddr)
	})
L
Luke Hoban 已提交
620

621
	withTestClient1("locationsUpperCase", t, func(c *rpc1.RPCClient) {
L
Luke Hoban 已提交
622 623 624 625
		// Upper case
		findLocationHelper(t, c, "locationsUpperCase.go:6", false, 1, 0)

		// Fully qualified path
626
		path := protest.Fixtures[protest.FixtureKey{"locationsUpperCase", 0}].Source
L
Luke Hoban 已提交
627 628 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
		findLocationHelper(t, c, path+":6", false, 1, 0)
		bp, err := c.CreateBreakpoint(&api.Breakpoint{File: path, Line: 6})
		if err != nil {
			t.Fatalf("Could not set breakpoint in %s: %v\n", path, err)
		}
		c.ClearBreakpoint(bp.ID)

		//  Allow `/` or `\` on Windows
		if runtime.GOOS == "windows" {
			findLocationHelper(t, c, filepath.FromSlash(path)+":6", false, 1, 0)
			bp, err = c.CreateBreakpoint(&api.Breakpoint{File: filepath.FromSlash(path), Line: 6})
			if err != nil {
				t.Fatalf("Could not set breakpoint in %s: %v\n", filepath.FromSlash(path), err)
			}
			c.ClearBreakpoint(bp.ID)
		}

		// Case-insensitive on Windows, case-sensitive otherwise
		shouldWrongCaseBeError := true
		numExpectedMatches := 0
		if runtime.GOOS == "windows" {
			shouldWrongCaseBeError = false
			numExpectedMatches = 1
		}
		findLocationHelper(t, c, strings.ToLower(path)+":6", shouldWrongCaseBeError, numExpectedMatches, 0)
		bp, err = c.CreateBreakpoint(&api.Breakpoint{File: strings.ToLower(path), Line: 6})
		if (err == nil) == shouldWrongCaseBeError {
			t.Fatalf("Could not set breakpoint in %s: %v\n", strings.ToLower(path), err)
		}
		c.ClearBreakpoint(bp.ID)
	})
658
}
A
aarzilli 已提交
659

660
func Test1ClientServer_FindLocationsAddr(t *testing.T) {
661
	withTestClient1("locationsprog2", t, func(c *rpc1.RPCClient) {
662 663 664
		<-c.Continue()

		afunction := findLocationHelper(t, c, "main.afunction", false, 1, 0)[0]
665
		anonfunc := findLocationHelper(t, c, "main.main.func1", false, 1, 0)[0]
666 667 668 669 670 671

		findLocationHelper(t, c, "*fn1", false, 1, afunction)
		findLocationHelper(t, c, "*fn3", false, 1, anonfunc)
	})
}

672
func Test1ClientServer_EvalVariable(t *testing.T) {
673
	withTestClient1("testvariables", t, func(c *rpc1.RPCClient) {
A
aarzilli 已提交
674 675 676 677 678 679
		state := <-c.Continue()

		if state.Err != nil {
			t.Fatalf("Continue(): %v\n", state.Err)
		}

680
		var1, err := c.EvalVariable(api.EvalScope{-1, 0}, "a1")
681
		assertNoError(err, t, "EvalVariable")
A
aarzilli 已提交
682

683
		t.Logf("var1: %s", var1.SinglelineString())
A
aarzilli 已提交
684 685

		if var1.Value != "foofoofoofoofoofoo" {
686
			t.Fatalf("Wrong variable value: %s", var1.Value)
687 688 689 690
		}
	})
}

691
func Test1ClientServer_SetVariable(t *testing.T) {
692
	withTestClient1("testvariables", t, func(c *rpc1.RPCClient) {
693 694 695 696 697 698
		state := <-c.Continue()

		if state.Err != nil {
			t.Fatalf("Continue(): %v\n", state.Err)
		}

D
Derek Parker 已提交
699
		assertNoError(c.SetVariable(api.EvalScope{-1, 0}, "a2", "8"), t, "SetVariable()")
700

D
Derek Parker 已提交
701
		a2, err := c.EvalVariable(api.EvalScope{-1, 0}, "a2")
702 703 704
		if err != nil {
			t.Fatalf("Could not evaluate variable: %v", err)
		}
705

706
		t.Logf("a2: %v", a2)
707

708 709 710 711
		n, err := strconv.Atoi(a2.Value)

		if err != nil && n != 8 {
			t.Fatalf("Wrong variable value: %v", a2)
A
aarzilli 已提交
712 713 714
		}
	})
}
715

716
func Test1ClientServer_FullStacktrace(t *testing.T) {
717
	withTestClient1("goroutinestackprog", t, func(c *rpc1.RPCClient) {
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.stacktraceme", Line: -1})
		assertNoError(err, t, "CreateBreakpoint()")
		state := <-c.Continue()
		if state.Err != nil {
			t.Fatalf("Continue(): %v\n", state.Err)
		}

		gs, err := c.ListGoroutines()
		assertNoError(err, t, "GoroutinesInfo()")
		found := make([]bool, 10)
		for _, g := range gs {
			frames, err := c.Stacktrace(g.ID, 10, true)
			assertNoError(err, t, fmt.Sprintf("Stacktrace(%d)", g.ID))
			for i, frame := range frames {
				if frame.Function == nil {
					continue
				}
735
				if frame.Function.Name() != "main.agoroutine" {
736 737 738 739 740 741 742
					continue
				}
				t.Logf("frame %d: %v", i, frame)
				for _, arg := range frame.Arguments {
					if arg.Name != "i" {
						continue
					}
743
					t.Logf("frame %d, variable i is %v\n", i, arg)
744 745 746 747
					argn, err := strconv.Atoi(arg.Value)
					if err == nil {
						found[argn] = true
					}
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
				}
			}
		}

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

		state = <-c.Continue()
		if state.Err != nil {
			t.Fatalf("Continue(): %v\n", state.Err)
		}

		frames, err := c.Stacktrace(-1, 10, true)
		assertNoError(err, t, "Stacktrace")

		cur := 3
		for i, frame := range frames {
			if i == 0 {
				continue
			}
			t.Logf("frame %d: %v", i, frame)
			v := frame.Var("n")
			if v == nil {
				t.Fatalf("Could not find value of variable n in frame %d", i)
			}
776 777 778
			vn, err := strconv.Atoi(v.Value)
			if err != nil || vn != cur {
				t.Fatalf("Expected value %d got %d (error: %v)", cur, vn, err)
779 780 781 782 783 784 785 786
			}
			cur--
			if cur < 0 {
				break
			}
		}
	})
}
787

788
func Test1Issue355(t *testing.T) {
789
	// After the target process has terminated should return an error but not crash
790
	withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
		bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: -1})
		assertNoError(err, t, "CreateBreakpoint()")
		ch := c.Continue()
		state := <-ch
		tid := state.CurrentThread.ID
		gid := state.SelectedGoroutine.ID
		assertNoError(state.Err, t, "First Continue()")
		ch = c.Continue()
		state = <-ch
		if !state.Exited {
			t.Fatalf("Target did not terminate after second continue")
		}

		ch = c.Continue()
		state = <-ch
		assertError(state.Err, t, "Continue()")

		_, err = c.Next()
		assertError(err, t, "Next()")
		_, err = c.Step()
		assertError(err, t, "Step()")
		_, err = c.StepInstruction()
		assertError(err, t, "StepInstruction()")
		_, err = c.SwitchThread(tid)
		assertError(err, t, "SwitchThread()")
		_, err = c.SwitchGoroutine(gid)
		assertError(err, t, "SwitchGoroutine()")
		_, err = c.Halt()
		assertError(err, t, "Halt()")
		_, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: -1})
		assertError(err, t, "CreateBreakpoint()")
		_, err = c.ClearBreakpoint(bp.ID)
		assertError(err, t, "ClearBreakpoint()")
		_, err = c.ListThreads()
		assertError(err, t, "ListThreads()")
		_, err = c.GetThread(tid)
		assertError(err, t, "GetThread()")
		assertError(c.SetVariable(api.EvalScope{gid, 0}, "a", "10"), t, "SetVariable()")
		_, err = c.ListLocalVariables(api.EvalScope{gid, 0})
		assertError(err, t, "ListLocalVariables()")
		_, err = c.ListFunctionArgs(api.EvalScope{gid, 0})
		assertError(err, t, "ListFunctionArgs()")
		_, err = c.ListRegisters()
		assertError(err, t, "ListRegisters()")
		_, err = c.ListGoroutines()
		assertError(err, t, "ListGoroutines()")
		_, err = c.Stacktrace(gid, 10, false)
		assertError(err, t, "Stacktrace()")
		_, err = c.FindLocation(api.EvalScope{gid, 0}, "+1")
		assertError(err, t, "FindLocation()")
A
aarzilli 已提交
841 842 843 844 845
		_, err = c.DisassemblePC(api.EvalScope{-1, 0}, 0x40100, api.IntelFlavour)
		assertError(err, t, "DisassemblePC()")
	})
}

846
func Test1Disasm(t *testing.T) {
A
aarzilli 已提交
847 848 849 850
	// Tests that disassembling by PC, range, and current PC all yeld similar results
	// Tests that disassembly by current PC will return a disassembly containing the instruction at PC
	// Tests that stepping on a calculated CALL instruction will yield a disassembly that contains the
	// effective destination of the CALL instruction
851
	withTestClient1("locationsprog2", t, func(c *rpc1.RPCClient) {
A
aarzilli 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
		ch := c.Continue()
		state := <-ch
		assertNoError(state.Err, t, "Continue()")

		locs, err := c.FindLocation(api.EvalScope{-1, 0}, "main.main")
		assertNoError(err, t, "FindLocation()")
		if len(locs) != 1 {
			t.Fatalf("wrong number of locations for main.main: %d", len(locs))
		}
		d1, err := c.DisassemblePC(api.EvalScope{-1, 0}, locs[0].PC, api.IntelFlavour)
		assertNoError(err, t, "DisassemblePC()")
		if len(d1) < 2 {
			t.Fatalf("wrong size of disassembly: %d", len(d1))
		}

		pcstart := d1[0].Loc.PC
		pcend := d1[len(d1)-1].Loc.PC + uint64(len(d1[len(d1)-1].Bytes))
		d2, err := c.DisassembleRange(api.EvalScope{-1, 0}, pcstart, pcend, api.IntelFlavour)
		assertNoError(err, t, "DisassembleRange()")

		if len(d1) != len(d2) {
			t.Logf("d1: %v", d1)
			t.Logf("d2: %v", d2)
			t.Fatal("mismatched length between disassemble pc and disassemble range")
		}

		d3, err := c.DisassemblePC(api.EvalScope{-1, 0}, state.CurrentThread.PC, api.IntelFlavour)
		assertNoError(err, t, "DisassemblePC() - second call")

		if len(d1) != len(d3) {
			t.Logf("d1: %v", d1)
			t.Logf("d3: %v", d3)
			t.Fatal("mismatched length between the two calls of disassemble pc")
		}

		// look for static call to afunction() on line 29
		found := false
		for i := range d3 {
890
			if d3[i].Loc.Line == 29 && strings.HasPrefix(d3[i].Text, "call") && d3[i].DestLoc != nil && d3[i].DestLoc.Function != nil && d3[i].DestLoc.Function.Name() == "main.afunction" {
A
aarzilli 已提交
891 892 893 894 895 896 897 898 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 939
				found = true
				break
			}
		}
		if !found {
			t.Fatal("Could not find call to main.afunction on line 29")
		}

		haspc := false
		for i := range d3 {
			if d3[i].AtPC {
				haspc = true
				break
			}
		}

		if !haspc {
			t.Logf("d3: %v", d3)
			t.Fatal("PC instruction not found")
		}

		startinstr := getCurinstr(d3)

		count := 0
		for {
			if count > 20 {
				t.Fatal("too many step instructions executed without finding a call instruction")
			}
			state, err := c.StepInstruction()
			assertNoError(err, t, fmt.Sprintf("StepInstruction() %d", count))

			d3, err = c.DisassemblePC(api.EvalScope{-1, 0}, state.CurrentThread.PC, api.IntelFlavour)
			assertNoError(err, t, fmt.Sprintf("StepInstruction() %d", count))

			curinstr := getCurinstr(d3)

			if curinstr == nil {
				t.Fatalf("Could not find current instruction %d", count)
			}

			if curinstr.Loc.Line != startinstr.Loc.Line {
				t.Fatal("Calling StepInstruction() repeatedly did not find the call instruction")
			}

			if strings.HasPrefix(curinstr.Text, "call") {
				t.Logf("call: %v", curinstr)
				if curinstr.DestLoc == nil || curinstr.DestLoc.Function == nil {
					t.Fatalf("Call instruction does not have destination: %v", curinstr)
				}
940
				if curinstr.DestLoc.Function.Name() != "main.afunction" {
A
aarzilli 已提交
941 942 943 944 945 946 947
					t.Fatalf("Call instruction destination not main.afunction: %v", curinstr)
				}
				break
			}

			count++
		}
948 949
	})
}
950

951
func Test1NegativeStackDepthBug(t *testing.T) {
952
	// After the target process has terminated should return an error but not crash
953
	withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {
954 955 956 957 958 959 960 961 962
		_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: -1})
		assertNoError(err, t, "CreateBreakpoint()")
		ch := c.Continue()
		state := <-ch
		assertNoError(state.Err, t, "Continue()")
		_, err = c.Stacktrace(-1, -2, true)
		assertError(err, t, "Stacktrace()")
	})
}
963

964
func Test1ClientServer_CondBreakpoint(t *testing.T) {
965
	withTestClient1("parallel_next", t, func(c *rpc1.RPCClient) {
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
		bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: 1})
		assertNoError(err, t, "CreateBreakpoint()")
		bp.Cond = "n == 7"
		assertNoError(c.AmendBreakpoint(bp), t, "AmendBreakpoint() 1")
		bp, err = c.GetBreakpoint(bp.ID)
		assertNoError(err, t, "GetBreakpoint() 1")
		bp.Variables = append(bp.Variables, "n")
		assertNoError(c.AmendBreakpoint(bp), t, "AmendBreakpoint() 2")
		bp, err = c.GetBreakpoint(bp.ID)
		assertNoError(err, t, "GetBreakpoint() 2")
		if bp.Cond == "" {
			t.Fatalf("No condition set on breakpoint %#v", bp)
		}
		if len(bp.Variables) != 1 {
			t.Fatalf("Wrong number of expressions to evaluate on breakpoint %#v", bp)
		}
		state := <-c.Continue()
		assertNoError(state.Err, t, "Continue()")

		nvar, err := c.EvalVariable(api.EvalScope{-1, 0}, "n")
		assertNoError(err, t, "EvalVariable()")

		if nvar.SinglelineString() != "7" {
			t.Fatalf("Stopped on wrong goroutine %s\n", nvar.Value)
		}
	})
}
993

994
func Test1SkipPrologue(t *testing.T) {
995
	withTestClient1("locationsprog2", t, func(c *rpc1.RPCClient) {
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
		<-c.Continue()

		afunction := findLocationHelper(t, c, "main.afunction", false, 1, 0)[0]
		findLocationHelper(t, c, "*fn1", false, 1, afunction)
		findLocationHelper(t, c, "locationsprog2.go:8", false, 1, afunction)

		afunction0 := findLocationHelper(t, c, "main.afunction:0", false, 1, 0)[0]

		if afunction == afunction0 {
			t.Fatal("Skip prologue failed")
		}
	})
}

1010
func Test1SkipPrologue2(t *testing.T) {
1011
	withTestClient1("callme", t, func(c *rpc1.RPCClient) {
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		callme := findLocationHelper(t, c, "main.callme", false, 1, 0)[0]
		callmeZ := findLocationHelper(t, c, "main.callme:0", false, 1, 0)[0]
		findLocationHelper(t, c, "callme.go:5", false, 1, callme)
		if callme == callmeZ {
			t.Fatal("Skip prologue failed")
		}

		callme2 := findLocationHelper(t, c, "main.callme2", false, 1, 0)[0]
		callme2Z := findLocationHelper(t, c, "main.callme2:0", false, 1, 0)[0]
		findLocationHelper(t, c, "callme.go:12", false, 1, callme2)
		if callme2 == callme2Z {
			t.Fatal("Skip prologue failed")
		}

		callme3 := findLocationHelper(t, c, "main.callme3", false, 1, 0)[0]
		callme3Z := findLocationHelper(t, c, "main.callme3:0", false, 1, 0)[0]
1028 1029
		ver, _ := goversion.Parse(runtime.Version())
		if ver.Major < 0 || ver.AfterOrEqual(goversion.GoVer18Beta) {
1030 1031 1032 1033 1034 1035 1036 1037
			findLocationHelper(t, c, "callme.go:19", false, 1, callme3)
		} else {
			// callme3 does not have local variables therefore the first line of the
			// function is immediately after the prologue
			// This is only true before 1.8 where frame pointer chaining introduced a
			// bit of prologue even for functions without local variables
			findLocationHelper(t, c, "callme.go:19", false, 1, callme3Z)
		}
1038 1039 1040 1041 1042
		if callme3 == callme3Z {
			t.Fatal("Skip prologue failed")
		}
	})
}
1043

1044
func Test1Issue419(t *testing.T) {
1045 1046
	// Calling service/rpc.(*Client).Halt could cause a crash because both Halt and Continue simultaneously
	// try to read 'runtime.g' and debug/dwarf.Data.Type is not thread safe
1047
	withTestClient1("issue419", t, func(c *rpc1.RPCClient) {
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
		go func() {
			rand.Seed(time.Now().Unix())
			d := time.Duration(rand.Intn(4) + 1)
			time.Sleep(d * time.Second)
			_, err := c.Halt()
			assertNoError(err, t, "RequestManualStop()")
		}()
		statech := c.Continue()
		state := <-statech
		assertNoError(state.Err, t, "Continue()")
	})
}
A
aarzilli 已提交
1060

1061
func Test1TypesCommand(t *testing.T) {
1062
	withTestClient1("testvariables2", t, func(c *rpc1.RPCClient) {
A
aarzilli 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
		state := <-c.Continue()
		assertNoError(state.Err, t, "Continue()")
		types, err := c.ListTypes("")
		assertNoError(err, t, "ListTypes()")

		found := false
		for i := range types {
			if types[i] == "main.astruct" {
				found = true
				break
			}
		}
		if !found {
			t.Fatal("Type astruct not found in ListTypes output")
		}

1079
		types, err = c.ListTypes("^main.astruct$")
A
aarzilli 已提交
1080
		assertNoError(err, t, "ListTypes(\"main.astruct\")")
1081 1082
		if len(types) != 1 {
			t.Fatalf("ListTypes(\"^main.astruct$\") did not filter properly, expected 1 got %d: %v", len(types), types)
A
aarzilli 已提交
1083 1084 1085
		}
	})
}
1086 1087

func Test1Issue406(t *testing.T) {
1088
	withTestClient1("issue406", t, func(c *rpc1.RPCClient) {
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		locs, err := c.FindLocation(api.EvalScope{-1, 0}, "issue406.go:146")
		assertNoError(err, t, "FindLocation()")
		_, err = c.CreateBreakpoint(&api.Breakpoint{Addr: locs[0].PC})
		assertNoError(err, t, "CreateBreakpoint()")
		ch := c.Continue()
		state := <-ch
		assertNoError(state.Err, t, "Continue()")
		v, err := c.EvalVariable(api.EvalScope{-1, 0}, "cfgtree")
		assertNoError(err, t, "EvalVariable()")
		vs := v.MultilineString("")
		t.Logf("cfgtree formats to: %s\n", vs)
	})
}