2017-05-05 22:17:52 +00:00
|
|
|
package gdbserial_test
|
|
|
|
|
|
|
|
import (
|
2018-10-16 08:34:18 +00:00
|
|
|
"flag"
|
2017-05-05 22:17:52 +00:00
|
|
|
"fmt"
|
2018-06-06 15:01:47 +00:00
|
|
|
"os"
|
2017-05-05 22:17:52 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-04 18:39:25 +00:00
|
|
|
"github.com/go-delve/delve/pkg/logflags"
|
|
|
|
"github.com/go-delve/delve/pkg/proc"
|
|
|
|
"github.com/go-delve/delve/pkg/proc/gdbserial"
|
|
|
|
protest "github.com/go-delve/delve/pkg/proc/test"
|
2017-05-05 22:17:52 +00:00
|
|
|
)
|
|
|
|
|
2018-06-06 15:01:47 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2018-10-16 08:34:18 +00:00
|
|
|
var logConf string
|
|
|
|
flag.StringVar(&logConf, "log", "", "configures logging")
|
|
|
|
flag.Parse()
|
2019-03-27 21:58:36 +00:00
|
|
|
logflags.Setup(logConf != "", logConf, "")
|
2018-06-06 15:01:47 +00:00
|
|
|
os.Exit(protest.RunTestsWithFixtures(m))
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:41:24 +00:00
|
|
|
func withTestRecording(name string, t testing.TB, fn func(t *proc.Target, fixture protest.Fixture)) {
|
2017-08-15 06:21:24 +00:00
|
|
|
fixture := protest.BuildFixture(name, 0)
|
2017-05-05 22:17:52 +00:00
|
|
|
protest.MustHaveRecordingAllowed(t)
|
|
|
|
if path, _ := exec.LookPath("rr"); path == "" {
|
|
|
|
t.Skip("test skipped, rr not found")
|
|
|
|
}
|
|
|
|
t.Log("recording")
|
2018-11-07 22:21:35 +00:00
|
|
|
p, tracedir, err := gdbserial.RecordAndReplay([]string{fixture.Path}, ".", true, []string{})
|
2017-05-05 22:17:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Launch():", err)
|
|
|
|
}
|
|
|
|
t.Logf("replaying %q", tracedir)
|
|
|
|
|
2020-01-21 20:41:24 +00:00
|
|
|
defer p.Detach(true)
|
2017-05-05 22:17:52 +00:00
|
|
|
|
|
|
|
fn(p, fixture)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNoError(err error, t testing.TB, s string) {
|
|
|
|
if err != nil {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
fname := filepath.Base(file)
|
|
|
|
t.Fatalf("failed assertion at %s:%d: %s - %s\n", fname, line, s, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:14:14 +00:00
|
|
|
func setFunctionBreakpoint(p *proc.Target, t *testing.T, fname string) *proc.Breakpoint {
|
2019-11-01 19:41:06 +00:00
|
|
|
_, f, l, _ := runtime.Caller(1)
|
|
|
|
f = filepath.Base(f)
|
|
|
|
|
|
|
|
addrs, err := proc.FindFunctionLocation(p, fname, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s:%d: FindFunctionLocation(%s): %v", f, l, fname, err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 1 {
|
|
|
|
t.Fatalf("%s:%d: setFunctionBreakpoint(%s): too many results %v", f, l, fname, addrs)
|
|
|
|
}
|
|
|
|
bp, err := p.SetBreakpoint(addrs[0], proc.UserBreakpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s:%d: FindFunctionLocation(%s): %v", f, l, fname, err)
|
|
|
|
}
|
2017-05-05 22:17:52 +00:00
|
|
|
return bp
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestartAfterExit(t *testing.T) {
|
|
|
|
protest.AllowRecording(t)
|
2020-01-21 20:41:24 +00:00
|
|
|
withTestRecording("testnextprog", t, func(p *proc.Target, fixture protest.Fixture) {
|
2017-05-05 22:17:52 +00:00
|
|
|
setFunctionBreakpoint(p, t, "main.main")
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue")
|
2017-05-05 22:17:52 +00:00
|
|
|
loc, err := p.CurrentThread().Location()
|
|
|
|
assertNoError(err, t, "CurrentThread().Location()")
|
2020-03-24 16:16:58 +00:00
|
|
|
err = p.Continue()
|
2018-08-31 18:08:18 +00:00
|
|
|
if _, isexited := err.(proc.ErrProcessExited); err == nil || !isexited {
|
2017-05-05 22:17:52 +00:00
|
|
|
t.Fatalf("program did not exit: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNoError(p.Restart(""), t, "Restart")
|
|
|
|
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue (after restart)")
|
2017-05-05 22:17:52 +00:00
|
|
|
loc2, err := p.CurrentThread().Location()
|
|
|
|
assertNoError(err, t, "CurrentThread().Location() (after restart)")
|
|
|
|
if loc2.Line != loc.Line {
|
|
|
|
t.Fatalf("stopped at %d (expected %d)", loc2.Line, loc.Line)
|
|
|
|
}
|
2020-03-24 16:16:58 +00:00
|
|
|
err = p.Continue()
|
2018-08-31 18:08:18 +00:00
|
|
|
if _, isexited := err.(proc.ErrProcessExited); err == nil || !isexited {
|
2017-05-05 22:17:52 +00:00
|
|
|
t.Fatalf("program did not exit (after exit): %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestartDuringStop(t *testing.T) {
|
|
|
|
protest.AllowRecording(t)
|
2020-01-21 20:41:24 +00:00
|
|
|
withTestRecording("testnextprog", t, func(p *proc.Target, fixture protest.Fixture) {
|
2017-05-05 22:17:52 +00:00
|
|
|
setFunctionBreakpoint(p, t, "main.main")
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue")
|
2017-05-05 22:17:52 +00:00
|
|
|
loc, err := p.CurrentThread().Location()
|
|
|
|
assertNoError(err, t, "CurrentThread().Location()")
|
|
|
|
|
|
|
|
assertNoError(p.Restart(""), t, "Restart")
|
|
|
|
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue (after restart)")
|
2017-05-05 22:17:52 +00:00
|
|
|
loc2, err := p.CurrentThread().Location()
|
|
|
|
assertNoError(err, t, "CurrentThread().Location() (after restart)")
|
|
|
|
if loc2.Line != loc.Line {
|
|
|
|
t.Fatalf("stopped at %d (expected %d)", loc2.Line, loc.Line)
|
|
|
|
}
|
2020-03-24 16:16:58 +00:00
|
|
|
err = p.Continue()
|
2018-08-31 18:08:18 +00:00
|
|
|
if _, isexited := err.(proc.ErrProcessExited); err == nil || !isexited {
|
2017-05-05 22:17:52 +00:00
|
|
|
t.Fatalf("program did not exit (after exit): %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:14:14 +00:00
|
|
|
func setFileBreakpoint(p *proc.Target, t *testing.T, fixture protest.Fixture, lineno int) *proc.Breakpoint {
|
2019-11-01 19:41:06 +00:00
|
|
|
_, f, l, _ := runtime.Caller(1)
|
|
|
|
f = filepath.Base(f)
|
|
|
|
|
|
|
|
addrs, err := proc.FindFileLocation(p, fixture.Source, lineno)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s:%d: FindFileLocation(%s, %d): %v", f, l, fixture.Source, lineno, err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 1 {
|
|
|
|
t.Fatalf("%s:%d: setFileLineBreakpoint(%s, %d): too many results %v", f, l, fixture.Source, lineno, addrs)
|
|
|
|
}
|
|
|
|
bp, err := p.SetBreakpoint(addrs[0], proc.UserBreakpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s:%d: SetBreakpoint: %v", f, l, err)
|
|
|
|
}
|
2017-05-05 22:17:52 +00:00
|
|
|
return bp
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReverseBreakpointCounts(t *testing.T) {
|
|
|
|
protest.AllowRecording(t)
|
2020-01-21 20:41:24 +00:00
|
|
|
withTestRecording("bpcountstest", t, func(p *proc.Target, fixture protest.Fixture) {
|
2019-11-01 19:41:06 +00:00
|
|
|
endbp := setFileBreakpoint(p, t, fixture, 28)
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue()")
|
2017-05-05 22:17:52 +00:00
|
|
|
loc, _ := p.CurrentThread().Location()
|
|
|
|
if loc.PC != endbp.Addr {
|
|
|
|
t.Fatalf("did not reach end of main.main function: %s:%d (%#x)", loc.File, loc.Line, loc.PC)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ClearBreakpoint(endbp.Addr)
|
proc,terminal: Implement reverse step, next and stepout (#1785)
* proc: move defer breakpoint code into a function
Moves the code that sets a breakpoint on the first deferred function,
used by both next and StepOut, to its function.
* proc: implement reverse step/next/stepout
When the direction of execution is reversed (on a recording) Step, Next and
StepOut will behave similarly to their forward version. However there are
some subtle interactions between their behavior, prologue skipping, deferred
calls and normal calls. Specifically:
- when stepping backwards we need to set a breakpoint on the first
instruction after each CALL instruction, once this breakpoint is reached we
need to execute a single StepInstruction operation to reverse step into the
CALL.
- to insure that the prologue is skipped reverse next needs to check if it
is on the first instruction after the prologue, and if it is behave like
reverse stepout.
- there is no reason to set breakpoints on deferred calls when reverse
nexting or reverse stepping out, they will never be hit.
- reverse step out should generally place its breakpoint on the CALL
instruction that created the current stack frame (which will be the CALL
instruction immediately preceding the instruction at the return address).
- reverse step out needs to treat panic calls and deferreturn calls
specially.
* service,terminal: implement reverse step, next, stepout
2020-03-11 22:40:41 +00:00
|
|
|
assertNoError(p.ChangeDirection(proc.Backward), t, "Switching to backward direction")
|
2019-11-01 19:41:06 +00:00
|
|
|
bp := setFileBreakpoint(p, t, fixture, 12)
|
|
|
|
startbp := setFileBreakpoint(p, t, fixture, 20)
|
2017-05-05 22:17:52 +00:00
|
|
|
|
|
|
|
countLoop:
|
|
|
|
for {
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue()")
|
2017-05-05 22:17:52 +00:00
|
|
|
loc, _ := p.CurrentThread().Location()
|
|
|
|
switch loc.PC {
|
|
|
|
case startbp.Addr:
|
|
|
|
break countLoop
|
|
|
|
case bp.Addr:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected stop location %s:%d %#x", loc.File, loc.Line, loc.PC)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("TotalHitCount: %d", bp.TotalHitCount)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:41:24 +00:00
|
|
|
func getPosition(p *proc.Target, t *testing.T) (when string, loc *proc.Location) {
|
2017-05-05 22:17:52 +00:00
|
|
|
var err error
|
|
|
|
when, err = p.When()
|
|
|
|
assertNoError(err, t, "When")
|
|
|
|
loc, err = p.CurrentThread().Location()
|
|
|
|
assertNoError(err, t, "Location")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckpoints(t *testing.T) {
|
|
|
|
protest.AllowRecording(t)
|
2020-01-21 20:41:24 +00:00
|
|
|
withTestRecording("continuetestprog", t, func(p *proc.Target, fixture protest.Fixture) {
|
2017-05-05 22:17:52 +00:00
|
|
|
// Continues until start of main.main, record output of 'when'
|
|
|
|
bp := setFunctionBreakpoint(p, t, "main.main")
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue")
|
2017-05-05 22:17:52 +00:00
|
|
|
when0, loc0 := getPosition(p, t)
|
2020-02-11 01:31:54 +00:00
|
|
|
t.Logf("when0: %q (%#x) %x", when0, loc0.PC, p.CurrentThread().ThreadID())
|
2017-05-05 22:17:52 +00:00
|
|
|
|
|
|
|
// Create a checkpoint and check that the list of checkpoints reflects this
|
|
|
|
cpid, err := p.Checkpoint("checkpoint1")
|
|
|
|
if cpid != 1 {
|
|
|
|
t.Errorf("unexpected checkpoint id %d", cpid)
|
|
|
|
}
|
|
|
|
assertNoError(err, t, "Checkpoint")
|
|
|
|
checkpoints, err := p.Checkpoints()
|
|
|
|
assertNoError(err, t, "Checkpoints")
|
|
|
|
if len(checkpoints) != 1 {
|
|
|
|
t.Fatalf("wrong number of checkpoints %v (one expected)", checkpoints)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move forward with next, check that the output of 'when' changes
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Next(), t, "First Next")
|
|
|
|
assertNoError(p.Next(), t, "Second Next")
|
2017-05-05 22:17:52 +00:00
|
|
|
when1, loc1 := getPosition(p, t)
|
2020-02-11 01:31:54 +00:00
|
|
|
t.Logf("when1: %q (%#x) %x", when1, loc1.PC, p.CurrentThread().ThreadID())
|
2017-05-05 22:17:52 +00:00
|
|
|
if loc0.PC == loc1.PC {
|
|
|
|
t.Fatalf("next did not move process %#x", loc0.PC)
|
|
|
|
}
|
|
|
|
if when0 == when1 {
|
|
|
|
t.Fatalf("output of when did not change after next: %q", when0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move back to checkpoint, check that the output of 'when' is the same as
|
|
|
|
// what it was when we set the breakpoint
|
|
|
|
p.Restart(fmt.Sprintf("c%d", cpid))
|
2020-02-11 01:31:54 +00:00
|
|
|
g, _ := proc.FindGoroutine(p, 1)
|
|
|
|
p.SwitchGoroutine(g)
|
2017-05-05 22:17:52 +00:00
|
|
|
when2, loc2 := getPosition(p, t)
|
2020-02-11 01:31:54 +00:00
|
|
|
t.Logf("when2: %q (%#x) %x", when2, loc2.PC, p.CurrentThread().ThreadID())
|
2017-05-05 22:17:52 +00:00
|
|
|
if loc2.PC != loc0.PC {
|
|
|
|
t.Fatalf("PC address mismatch %#x != %#x", loc0.PC, loc2.PC)
|
|
|
|
}
|
|
|
|
if when0 != when2 {
|
|
|
|
t.Fatalf("output of when mismatched %q != %q", when0, when2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move forward with next again, check that the output of 'when' matches
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Next(), t, "First Next")
|
|
|
|
assertNoError(p.Next(), t, "Second Next")
|
2017-05-05 22:17:52 +00:00
|
|
|
when3, loc3 := getPosition(p, t)
|
|
|
|
t.Logf("when3: %q (%#x)", when3, loc3.PC)
|
|
|
|
if loc3.PC != loc1.PC {
|
|
|
|
t.Fatalf("PC address mismatch %#x != %#x", loc1.PC, loc3.PC)
|
|
|
|
}
|
|
|
|
if when3 != when1 {
|
|
|
|
t.Fatalf("when output mismatch %q != %q", when1, when3)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete breakpoint, move back to checkpoint then next twice and check
|
|
|
|
// output of 'when' again
|
|
|
|
_, err = p.ClearBreakpoint(bp.Addr)
|
|
|
|
assertNoError(err, t, "ClearBreakpoint")
|
|
|
|
p.Restart(fmt.Sprintf("c%d", cpid))
|
2020-02-11 01:31:54 +00:00
|
|
|
g, _ = proc.FindGoroutine(p, 1)
|
|
|
|
p.SwitchGoroutine(g)
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Next(), t, "First Next")
|
|
|
|
assertNoError(p.Next(), t, "Second Next")
|
2017-05-05 22:17:52 +00:00
|
|
|
when4, loc4 := getPosition(p, t)
|
|
|
|
t.Logf("when4: %q (%#x)", when4, loc4.PC)
|
|
|
|
if loc4.PC != loc1.PC {
|
|
|
|
t.Fatalf("PC address mismatch %#x != %#x", loc1.PC, loc4.PC)
|
|
|
|
}
|
|
|
|
if when4 != when1 {
|
|
|
|
t.Fatalf("when output mismatch %q != %q", when1, when4)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete checkpoint, check that the list of checkpoints is updated
|
|
|
|
assertNoError(p.ClearCheckpoint(cpid), t, "ClearCheckpoint")
|
|
|
|
checkpoints, err = p.Checkpoints()
|
|
|
|
assertNoError(err, t, "Checkpoints")
|
|
|
|
if len(checkpoints) != 0 {
|
|
|
|
t.Fatalf("wrong number of checkpoints %v (zero expected)", checkpoints)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-10-16 08:34:18 +00:00
|
|
|
|
|
|
|
func TestIssue1376(t *testing.T) {
|
|
|
|
// Backward Continue should terminate when it encounters the start of the process.
|
|
|
|
protest.AllowRecording(t)
|
2020-01-21 20:41:24 +00:00
|
|
|
withTestRecording("continuetestprog", t, func(p *proc.Target, fixture protest.Fixture) {
|
2018-10-16 08:34:18 +00:00
|
|
|
bp := setFunctionBreakpoint(p, t, "main.main")
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue (forward)")
|
2018-10-16 08:34:18 +00:00
|
|
|
_, err := p.ClearBreakpoint(bp.Addr)
|
|
|
|
assertNoError(err, t, "ClearBreakpoint")
|
proc,terminal: Implement reverse step, next and stepout (#1785)
* proc: move defer breakpoint code into a function
Moves the code that sets a breakpoint on the first deferred function,
used by both next and StepOut, to its function.
* proc: implement reverse step/next/stepout
When the direction of execution is reversed (on a recording) Step, Next and
StepOut will behave similarly to their forward version. However there are
some subtle interactions between their behavior, prologue skipping, deferred
calls and normal calls. Specifically:
- when stepping backwards we need to set a breakpoint on the first
instruction after each CALL instruction, once this breakpoint is reached we
need to execute a single StepInstruction operation to reverse step into the
CALL.
- to insure that the prologue is skipped reverse next needs to check if it
is on the first instruction after the prologue, and if it is behave like
reverse stepout.
- there is no reason to set breakpoints on deferred calls when reverse
nexting or reverse stepping out, they will never be hit.
- reverse step out should generally place its breakpoint on the CALL
instruction that created the current stack frame (which will be the CALL
instruction immediately preceding the instruction at the return address).
- reverse step out needs to treat panic calls and deferreturn calls
specially.
* service,terminal: implement reverse step, next, stepout
2020-03-11 22:40:41 +00:00
|
|
|
assertNoError(p.ChangeDirection(proc.Backward), t, "Switching to backward direction")
|
2020-03-24 16:16:58 +00:00
|
|
|
assertNoError(p.Continue(), t, "Continue (backward)")
|
2018-10-16 08:34:18 +00:00
|
|
|
})
|
|
|
|
}
|