proctl_test.go 9.4 KB
Newer Older
1
package proctl
D
Derek Parker 已提交
2 3

import (
4
	"bytes"
5 6 7
	"encoding/binary"
	"os"
	"os/exec"
8
	"path/filepath"
9
	"runtime"
D
Derek Parker 已提交
10
	"testing"
D
Derek Parker 已提交
11
	"time"
D
Derek Parker 已提交
12
)
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
func withTestProcess(name string, t *testing.T, fn func(p *DebuggedProcess)) {
	runtime.LockOSThread()
	base := filepath.Base(name)
	if err := exec.Command("go", "build", "-gcflags=-N -l", "-o", base, name+".go").Run(); err != nil {
		t.Fatalf("Could not compile %s due to %s", name, err)
	}
	defer os.Remove("./" + base)

	p, err := Launch([]string{"./" + base})
	if err != nil {
		t.Fatal("Launch():", err)
	}

	defer p.Process.Kill()

	fn(p)
}

func getRegisters(p *DebuggedProcess, t *testing.T) Registers {
33 34 35 36 37 38 39 40
	regs, err := p.Registers()
	if err != nil {
		t.Fatal("Registers():", err)
	}

	return regs
}

D
Derek Parker 已提交
41
func dataAtAddr(thread *ThreadContext, addr uint64) ([]byte, error) {
42
	data := make([]byte, 1)
D
Derek Parker 已提交
43
	_, err := readMemory(thread, uintptr(addr), data)
44 45 46 47 48 49 50
	if err != nil {
		return nil, err
	}

	return data, nil
}

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

59
func currentPC(p *DebuggedProcess, t *testing.T) uint64 {
60 61 62 63 64 65 66 67
	pc, err := p.CurrentPC()
	if err != nil {
		t.Fatal(err)
	}

	return pc
}

68
func currentLineNumber(p *DebuggedProcess, t *testing.T) (string, int) {
69
	pc := currentPC(p, t)
70
	f, l, _ := p.goSymTable.PCToLine(pc)
71

D
Derek Parker 已提交
72
	return f, l
73 74
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
func TestExit(t *testing.T) {
	withTestProcess("../_fixtures/continuetestprog", t, func(p *DebuggedProcess) {
		err := p.Continue()
		pe, ok := err.(ProcessExitedError)
		if !ok {
			t.Fatalf("Continue() returned unexpected error type")
		}
		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 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
func TestHalt(t *testing.T) {
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
		go func() {
			time.Sleep(10 * time.Millisecond)
			err := p.RequestManualStop()
			if err != nil {
				t.Fatal(err)
			}
		}()
		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 {
				t.Error(err)
			}
		}
	})
}

116
func TestStep(t *testing.T) {
117
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
118
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
119 120
		helloworldaddr := helloworldfunc.Entry

121
		_, err := p.Break(helloworldaddr)
122 123 124
		assertNoError(err, t, "Break()")
		assertNoError(p.Continue(), t, "Continue()")

125
		regs := getRegisters(p, t)
126
		rip := regs.PC()
127

128
		err = p.Step()
D
Derek Parker 已提交
129
		assertNoError(err, t, "Step()")
130

131
		regs = getRegisters(p, t)
132 133 134 135 136
		if rip >= regs.PC() {
			t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
		}
	})
}
137

138
func TestBreakPoint(t *testing.T) {
139
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
140
		helloworldfunc := p.goSymTable.LookupFunc("main.helloworld")
D
Derek Parker 已提交
141
		helloworldaddr := helloworldfunc.Entry
142

D
Derek Parker 已提交
143
		bp, err := p.Break(helloworldaddr)
D
Derek Parker 已提交
144
		assertNoError(err, t, "Break()")
D
Derek Parker 已提交
145
		assertNoError(p.Continue(), t, "Continue()")
146

147 148 149 150
		pc, err := p.CurrentPC()
		if err != nil {
			t.Fatal(err)
		}
151

D
Derek Parker 已提交
152
		if pc-1 != bp.Addr && pc != bp.Addr {
153
			f, l, _ := p.goSymTable.PCToLine(pc)
D
Derek Parker 已提交
154
			t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
155 156
		}
	})
157
}
158

159
func TestBreakPointInSeperateGoRoutine(t *testing.T) {
160
	withTestProcess("../_fixtures/testthreads", t, func(p *DebuggedProcess) {
161
		fn := p.goSymTable.LookupFunc("main.anotherthread")
162 163 164 165
		if fn == nil {
			t.Fatal("No fn exists")
		}

166
		_, err := p.Break(fn.Entry)
167 168 169 170 171 172 173 174 175 176 177 178 179 180
		if err != nil {
			t.Fatal(err)
		}

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

		pc, err := p.CurrentPC()
		if err != nil {
			t.Fatal(err)
		}

181
		f, l, _ := p.goSymTable.PCToLine(pc)
182 183 184 185 186 187
		if f != "testthreads.go" && l != 8 {
			t.Fatal("Program did not hit breakpoint")
		}
	})
}

188
func TestBreakPointWithNonExistantFunction(t *testing.T) {
189
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
190
		_, err := p.Break(0)
191 192 193 194
		if err == nil {
			t.Fatal("Should not be able to break at non existant function")
		}
	})
195
}
196 197

func TestClearBreakPoint(t *testing.T) {
198
	withTestProcess("../_fixtures/testprog", t, func(p *DebuggedProcess) {
199
		fn := p.goSymTable.LookupFunc("main.sleepytime")
200
		bp, err := p.Break(fn.Entry)
D
Derek Parker 已提交
201
		assertNoError(err, t, "Break()")
202 203

		bp, err = p.Clear(fn.Entry)
D
Derek Parker 已提交
204
		assertNoError(err, t, "Clear()")
205

D
Derek Parker 已提交
206
		data, err := dataAtAddr(p.CurrentThread, bp.Addr)
207 208 209 210
		if err != nil {
			t.Fatal(err)
		}

211
		int3 := []byte{0xcc}
212 213 214 215
		if bytes.Equal(data, int3) {
			t.Fatalf("Breakpoint was not cleared data: %#v, int3: %#v", data, int3)
		}

216
		if len(p.BreakPoints) != 0 {
217 218 219
			t.Fatal("Breakpoint not removed internally")
		}
	})
220
}
221 222 223

func TestNext(t *testing.T) {
	var (
224 225
		err            error
		executablePath = "../_fixtures/testnextprog"
226 227 228 229 230
	)

	testcases := []struct {
		begin, end int
	}{
D
Derek Parker 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244
		{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
Derek Parker 已提交
245
		{34, 41},
246
		{41, 40},
D
Derek Parker 已提交
247
		{40, 41},
248 249 250 251 252 253 254
	}

	fp, err := filepath.Abs("../_fixtures/testnextprog.go")
	if err != nil {
		t.Fatal(err)
	}

255
	withTestProcess(executablePath, t, func(p *DebuggedProcess) {
256
		pc, _, _ := p.goSymTable.LineToPC(fp, testcases[0].begin)
257
		_, err := p.Break(pc)
258 259
		assertNoError(err, t, "Break()")
		assertNoError(p.Continue(), t, "Continue()")
D
Derek Parker 已提交
260
		p.Clear(pc)
261

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

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

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

276 277 278 279 280 281 282 283
		p.Clear(pc)
		if len(p.BreakPoints) != 0 {
			t.Fatal("Not all breakpoints were cleaned up", len(p.HWBreakPoints))
		}
		for _, bp := range p.HWBreakPoints {
			if bp != nil {
				t.Fatal("Not all breakpoints were cleaned up", bp.Addr)
			}
284
		}
285 286
	})
}
287 288 289 290 291 292

func TestFindReturnAddress(t *testing.T) {
	var testfile, _ = filepath.Abs("../_fixtures/testnextprog")

	withTestProcess(testfile, t, func(p *DebuggedProcess) {
		var (
293 294
			fdes = p.frameEntries
			gsd  = p.goSymTable
295 296 297 298 299 300 301 302
		)

		testsourcefile := testfile + ".go"
		start, _, err := gsd.LineToPC(testsourcefile, 24)
		if err != nil {
			t.Fatal(err)
		}

303
		_, err = p.Break(start)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		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 已提交
331
		readMemory(p.CurrentThread, uintptr(addr), data)
332 333
		addr = binary.LittleEndian.Uint64(data)

D
Derek Parker 已提交
334 335 336 337
		linuxExpected := uint64(0x400fbc)
		darwinExpected := uint64(0x23bc)
		if addr != linuxExpected && addr != darwinExpected {
			t.Fatalf("return address not found correctly, expected (linux) %#v or (darwin) %#v got %#v", linuxExpected, darwinExpected, addr)
338 339 340
		}
	})
}
D
Derek Parker 已提交
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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

func TestSwitchThread(t *testing.T) {
	var testfile, _ = filepath.Abs("../_fixtures/testnextprog")

	withTestProcess(testfile, t, func(p *DebuggedProcess) {
		// 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)
		}
		_, err = p.Break(pc)
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		var nt int
		ct := p.CurrentThread.Id
		for tid, _ := range p.Threads {
			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")
		}
	})
}
D
Derek Parker 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

func TestFunctionCall(t *testing.T) {
	var testfile, _ = filepath.Abs("../_fixtures/testprog")

	withTestProcess(testfile, t, func(p *DebuggedProcess) {
		pc, err := p.FindLocation("main.main")
		if err != nil {
			t.Fatal(err)
		}
		_, err = p.Break(pc)
		if err != nil {
			t.Fatal(err)
		}
		err = p.Continue()
		if err != nil {
			t.Fatal(err)
		}
		pc, err = p.CurrentPC()
		if err != nil {
			t.Fatal(err)
		}
405
		fn := p.goSymTable.PCToFunc(pc)
D
Derek Parker 已提交
406 407 408 409 410 411 412 413 414 415 416
		if fn == nil {
			t.Fatalf("Could not find func for PC: %#v", pc)
		}
		if fn.Name != "main.main" {
			t.Fatal("Program stopped at incorrect place")
		}
		if err = p.CallFn("runtime.getg", func(th *ThreadContext) error {
			pc, err := th.CurrentPC()
			if err != nil {
				t.Fatal(err)
			}
417
			f := th.Process.goSymTable.LookupFunc("runtime.getg")
D
Derek Parker 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431
			if f == nil {
				t.Fatalf("could not find function %s", "runtime.getg")
			}
			if pc-1 != f.End-2 && pc != f.End-2 {
				t.Fatalf("wrong pc expected %#v got %#v", f.End-2, pc-1)
			}
			return nil
		}); err != nil {
			t.Fatal(err)
		}
		pc, err = p.CurrentPC()
		if err != nil {
			t.Fatal(err)
		}
432
		fn = p.goSymTable.PCToFunc(pc)
D
Derek Parker 已提交
433 434 435 436 437 438 439 440
		if fn == nil {
			t.Fatalf("Could not find func for PC: %#v", pc)
		}
		if fn.Name != "main.main" {
			t.Fatal("Program stopped at incorrect place")
		}
	})
}