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

import (
4
	"bytes"
5
	"encoding/binary"
D
Derek Parker 已提交
6
	"os"
7
	"path/filepath"
8
	"runtime"
D
Derek Parker 已提交
9
	"testing"
D
Dan Mace 已提交
10

D
Derek Parker 已提交
11
	protest "github.com/derekparker/delve/proc/test"
D
Derek Parker 已提交
12
)
13

14 15 16 17
func init() {
	runtime.GOMAXPROCS(2)
}

D
Dan Mace 已提交
18
func TestMain(m *testing.M) {
D
Derek Parker 已提交
19
	os.Exit(protest.RunTestsWithFixtures(m))
D
Dan Mace 已提交
20 21
}

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

29 30 31 32
	defer func() {
		p.Halt()
		p.Detach(true)
	}()
33

D
Dan Mace 已提交
34
	fn(p, fixture)
35 36
}

D
Derek Parker 已提交
37
func getRegisters(p *Process, t *testing.T) Registers {
38 39 40 41 42 43 44 45
	regs, err := p.Registers()
	if err != nil {
		t.Fatal("Registers():", err)
	}

	return regs
}

D
Derek Parker 已提交
46
func dataAtAddr(thread *Thread, addr uint64) ([]byte, error) {
47
	data := make([]byte, 1)
D
Derek Parker 已提交
48
	_, err := readMemory(thread, uintptr(addr), data)
49 50 51 52 53 54 55
	if err != nil {
		return nil, err
	}

	return data, nil
}

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

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

	return pc
}

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

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

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

D
Derek Parker 已提交
96
func TestHalt(t *testing.T) {
97
	runtime.GOMAXPROCS(2)
D
Derek Parker 已提交
98
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
99
		go func() {
D
Dan Mace 已提交
100 101 102 103 104 105 106 107
			for {
				if p.Running() {
					err := p.RequestManualStop()
					if err != nil {
						t.Fatal(err)
					}
					return
				}
D
Derek Parker 已提交
108 109 110 111 112 113 114 115 116 117 118 119
			}
		}()
		err := p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		// 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 {
			_, err := th.Registers()
			if err != nil {
D
Derek Parker 已提交
120
				t.Error(err, th.Id)
D
Derek Parker 已提交
121 122 123 124 125
			}
		}
	})
}

126
func TestStep(t *testing.T) {
D
Derek Parker 已提交
127
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
128
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
129 130
		helloworldaddr := helloworldfunc.Entry

131 132
		_, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
133 134
		assertNoError(p.Continue(), t, "Continue()")

135
		regs := getRegisters(p, t)
136
		rip := regs.PC()
137

138
		err = p.Step()
D
Derek Parker 已提交
139
		assertNoError(err, t, "Step()")
140

141
		regs = getRegisters(p, t)
142 143 144 145 146
		if rip >= regs.PC() {
			t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
		}
	})
}
147

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

153 154
		bp, err := p.SetBreakpoint(helloworldaddr)
		assertNoError(err, t, "SetBreakpoint()")
D
Derek Parker 已提交
155
		assertNoError(p.Continue(), t, "Continue()")
156

D
Derek Parker 已提交
157
		pc, err := p.PC()
158 159 160
		if err != nil {
			t.Fatal(err)
		}
161

D
Derek Parker 已提交
162
		if pc-1 != bp.Addr && pc != bp.Addr {
163
			f, l, _ := p.goSymTable.PCToLine(pc)
D
Derek Parker 已提交
164
			t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
165 166
		}
	})
167
}
168

D
Derek Parker 已提交
169
func TestBreakpointInSeperateGoRoutine(t *testing.T) {
D
Derek Parker 已提交
170
	withTestProcess("testthreads", t, func(p *Process, fixture protest.Fixture) {
171
		fn := p.goSymTable.LookupFunc("main.anotherthread")
172 173 174 175
		if fn == nil {
			t.Fatal("No fn exists")
		}

176
		_, err := p.SetBreakpoint(fn.Entry)
177 178 179 180 181 182 183 184 185
		if err != nil {
			t.Fatal(err)
		}

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

D
Derek Parker 已提交
186
		pc, err := p.PC()
187 188 189 190
		if err != nil {
			t.Fatal(err)
		}

191
		f, l, _ := p.goSymTable.PCToLine(pc)
192 193 194 195 196 197
		if f != "testthreads.go" && l != 8 {
			t.Fatal("Program did not hit breakpoint")
		}
	})
}

D
Derek Parker 已提交
198
func TestBreakpointWithNonExistantFunction(t *testing.T) {
D
Derek Parker 已提交
199
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
200
		_, err := p.SetBreakpoint(0)
201 202 203 204
		if err == nil {
			t.Fatal("Should not be able to break at non existant function")
		}
	})
205
}
206

207
func TestClearBreakpointBreakpoint(t *testing.T) {
D
Derek Parker 已提交
208
	withTestProcess("testprog", t, func(p *Process, fixture protest.Fixture) {
209
		fn := p.goSymTable.LookupFunc("main.sleepytime")
210 211
		bp, err := p.SetBreakpoint(fn.Entry)
		assertNoError(err, t, "SetBreakpoint()")
212

213 214
		bp, err = p.ClearBreakpoint(fn.Entry)
		assertNoError(err, t, "ClearBreakpoint()")
215

D
Derek Parker 已提交
216
		data, err := dataAtAddr(p.CurrentThread, bp.Addr)
217 218 219 220
		if err != nil {
			t.Fatal(err)
		}

221
		int3 := []byte{0xcc}
222 223 224 225
		if bytes.Equal(data, int3) {
			t.Fatalf("Breakpoint was not cleared data: %#v, int3: %#v", data, int3)
		}

D
Derek Parker 已提交
226
		if len(p.Breakpoints) != 0 {
227 228 229
			t.Fatal("Breakpoint not removed internally")
		}
	})
230
}
231

232 233 234
type nextTest struct {
	begin, end int
}
235

236
func testnext(program string, testcases []nextTest, initialLocation string, t *testing.T) {
D
Derek Parker 已提交
237
	withTestProcess(program, t, func(p *Process, fixture protest.Fixture) {
238 239
		bp, err := p.SetBreakpointByLocation(initialLocation)
		assertNoError(err, t, "SetBreakpoint()")
240
		assertNoError(p.Continue(), t, "Continue()")
241
		p.ClearBreakpoint(bp.Addr)
242
		p.CurrentThread.SetPC(bp.Addr)
243

D
Derek Parker 已提交
244
		f, ln := currentLineNumber(p, t)
245 246
		for _, tc := range testcases {
			if ln != tc.begin {
D
Derek Parker 已提交
247
				t.Fatalf("Program not stopped at correct spot expected %d was %s:%d", tc.begin, filepath.Base(f), ln)
248 249 250 251
			}

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

D
Derek Parker 已提交
252
			f, ln = currentLineNumber(p, t)
253
			if ln != tc.end {
D
Derek Parker 已提交
254
				t.Fatalf("Program did not continue to correct next location expected %d was %s:%d", tc.end, filepath.Base(f), ln)
255 256
			}
		}
257

D
Derek Parker 已提交
258 259
		if len(p.Breakpoints) != 0 {
			t.Fatal("Not all breakpoints were cleaned up", len(p.Breakpoints))
260
		}
261 262
	})
}
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
func TestNextGeneral(t *testing.T) {
	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},
	}
282
	testnext("testnextprog", testcases, "main.testnext", t)
283 284 285 286 287 288 289
}

func TestNextGoroutine(t *testing.T) {
	testcases := []nextTest{
		{46, 47},
		{47, 42},
	}
290
	testnext("testnextprog", testcases, "main.testgoroutine", t)
291 292 293 294 295 296 297
}

func TestNextFunctionReturn(t *testing.T) {
	testcases := []nextTest{
		{13, 14},
		{14, 35},
	}
298 299 300 301 302 303 304 305 306
	testnext("testnextprog", testcases, "main.helloworld", t)
}

func TestNextFunctionReturnDefer(t *testing.T) {
	testcases := []nextTest{
		{5, 9},
		{9, 6},
	}
	testnext("testnextdefer", testcases, "main.main", t)
307 308
}

D
Derek Parker 已提交
309
func TestRuntimeBreakpoint(t *testing.T) {
D
Derek Parker 已提交
310
	withTestProcess("testruntimebreakpoint", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
		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")
		}
	})
}

326
func TestFindReturnAddress(t *testing.T) {
D
Derek Parker 已提交
327
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
328
		var (
329 330
			fdes = p.frameEntries
			gsd  = p.goSymTable
331 332
		)

D
Dan Mace 已提交
333
		start, _, err := gsd.LineToPC(fixture.Source, 24)
334 335 336 337
		if err != nil {
			t.Fatal(err)
		}

338
		_, err = p.SetBreakpoint(start)
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
		if err != nil {
			t.Fatal(err)
		}

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

		regs, err := p.Registers()
		if err != nil {
			t.Fatal(err)
		}

		fde, err := fdes.FDEForPC(start)
		if err != nil {
			t.Fatal(err)
		}

		ret := fde.ReturnAddressOffset(start)
		if err != nil {
			t.Fatal(err)
		}

		addr := uint64(int64(regs.SP()) + ret)
		data := make([]byte, 8)

D
Derek Parker 已提交
366
		readMemory(p.CurrentThread, uintptr(addr), data)
367 368
		addr = binary.LittleEndian.Uint64(data)

369 370 371
		_, l, _ := p.goSymTable.PCToLine(addr)
		if l != 40 {
			t.Fatalf("return address not found correctly, expected line 40")
372 373 374
		}
	})
}
D
Derek Parker 已提交
375 376

func TestSwitchThread(t *testing.T) {
D
Derek Parker 已提交
377
	withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
D
Derek Parker 已提交
378 379 380 381 382 383 384 385 386
		// With invalid thread id
		err := p.SwitchThread(-1)
		if err == nil {
			t.Fatal("Expected error for invalid thread id")
		}
		pc, err := p.FindLocation("main.main")
		if err != nil {
			t.Fatal(err)
		}
387
		_, err = p.SetBreakpoint(pc)
D
Derek Parker 已提交
388 389 390 391 392 393 394 395 396
		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 已提交
397
		for tid := range p.Threads {
D
Derek Parker 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
			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 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

type loc struct {
	line int
	fn   string
}

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

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

func TestStacktrace(t *testing.T) {
	stacks := [][]loc{
		[]loc{{8, "main.func1"}, {16, "main.main"}},
		[]loc{{8, "main.func1"}, {12, "main.func2"}, {17, "main.main"}},
	}
D
Derek Parker 已提交
437
	withTestProcess("stacktraceprog", t, func(p *Process, fixture protest.Fixture) {
438
		bp, err := p.SetBreakpointByLocation("main.stacktraceme")
A
aarzilli 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
		assertNoError(err, t, "BreakByLocation()")

		for i := range stacks {
			assertNoError(p.Continue(), t, "Continue()")
			locations, err := p.CurrentThread.Stacktrace(40)
			assertNoError(err, t, "Stacktrace()")

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

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

457
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
		p.Continue()
	})
}

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

func TestStacktraceGoroutine(t *testing.T) {
	mainStack := []loc{{21, "main.main"}}
	agoroutineStack := []loc{{-1, "runtime.goparkunlock"}, {-1, "runtime.chansend"}, {-1, "runtime.chansend1"}, {8, "main.agoroutine"}}

D
Derek Parker 已提交
478
	withTestProcess("goroutinestackprog", t, func(p *Process, fixture protest.Fixture) {
479
		bp, err := p.SetBreakpointByLocation("main.stacktraceme")
A
aarzilli 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
		assertNoError(err, t, "BreakByLocation()")

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

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

		agoroutineCount := 0
		mainCount := 0

		for _, g := range gs {
			locations, _ := p.GoroutineStacktrace(g, 40)
			assertNoError(err, t, "GoroutineStacktrace()")

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

			if stackMatch(agoroutineStack, locations) {
				agoroutineCount++
			} else {
				t.Logf("Non-goroutine stack: (%d)", len(locations))
				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)
		}

522
		p.ClearBreakpoint(bp.Addr)
A
aarzilli 已提交
523 524 525
		p.Continue()
	})
}