From a01fe7384513843b484012497d7b1c547f10318c Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Tue, 24 Jan 2023 06:56:05 -0800 Subject: [PATCH] pkg/proc: do not check decl line for FunctionArguments (#3254) Fixes a bug where we cannot get locals (including arguments and return values) from a given scope because the line number state machine ends up in an invalid state because of this parameter being set to false. --- _fixtures/traceprog.go | 13 +++++++++++++ cmd/dlv/dlv_test.go | 25 +++++++++++++++++++++++++ pkg/proc/eval.go | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 _fixtures/traceprog.go diff --git a/_fixtures/traceprog.go b/_fixtures/traceprog.go new file mode 100644 index 00000000..f8ec72cb --- /dev/null +++ b/_fixtures/traceprog.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func callme(i int) int { + return i * i +} + +func main() { + j := 0 + j += callme(2) + fmt.Println(j) +} diff --git a/cmd/dlv/dlv_test.go b/cmd/dlv/dlv_test.go index b2f35d85..7e134ee2 100644 --- a/cmd/dlv/dlv_test.go +++ b/cmd/dlv/dlv_test.go @@ -1036,6 +1036,31 @@ func TestTrace(t *testing.T) { cmd.Wait() } +func TestTrace2(t *testing.T) { + dlvbin, tmpdir := getDlvBin(t) + defer os.RemoveAll(tmpdir) + + expected := []byte("> goroutine(1): main.callme(2)\n>> goroutine(1): => (4)\n") + + fixtures := protest.FindFixturesDir() + cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "traceprog.go"), "callme") + rdr, err := cmd.StderrPipe() + assertNoError(err, t, "stderr pipe") + defer rdr.Close() + + cmd.Dir = filepath.Join(fixtures, "buildtest") + + assertNoError(cmd.Start(), t, "running trace") + + output, err := ioutil.ReadAll(rdr) + assertNoError(err, t, "ReadAll") + + if !bytes.Contains(output, expected) { + t.Fatalf("expected:\n%s\ngot:\n%s", string(expected), string(output)) + } + cmd.Wait() +} + func TestTraceMultipleGoroutines(t *testing.T) { dlvbin, tmpdir := getDlvBin(t) defer os.RemoveAll(tmpdir) diff --git a/pkg/proc/eval.go b/pkg/proc/eval.go index 1afa2294..d8348fe6 100644 --- a/pkg/proc/eval.go +++ b/pkg/proc/eval.go @@ -474,7 +474,7 @@ func (scope *EvalScope) LocalVariables(cfg LoadConfig) ([]*Variable, error) { // FunctionArguments returns the name, value, and type of all current function arguments. func (scope *EvalScope) FunctionArguments(cfg LoadConfig) ([]*Variable, error) { - vars, err := scope.Locals(0) + vars, err := scope.Locals(localsNoDeclLineCheck) if err != nil { return nil, err }