diff --git a/_fixtures/testvariables.go b/_fixtures/testvariables.go index 8057ce2d..52b44cf4 100644 --- a/_fixtures/testvariables.go +++ b/_fixtures/testvariables.go @@ -57,9 +57,11 @@ func foobar(baz string, bar FooBar) { ) barfoo() - fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, b1, b2, baz, neg, i8, u8, u16, u32, u64, up, f32, c64, c128, i32, bar, f, ms, ba) + fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, b1, b2, baz, neg, i8, u8, u16, u32, u64, up, f32, c64, c128, i32, bar, f, ms, ba, p1) } +var p1 = 10 + func main() { foobar("bazburzum", FooBar{Baz: 10, Bur: "lorem"}) } diff --git a/proc/variables.go b/proc/variables.go index c5171997..9045ec53 100644 --- a/proc/variables.go +++ b/proc/variables.go @@ -240,6 +240,20 @@ func (thread *Thread) EvalVariable(name string) (*Variable, error) { } } + // Attempt to evaluate name as a package variable. + if memberName != "" { + return thread.EvalPackageVariable(name) + } else { + loc, err := thread.Location() + if err == nil && loc.Fn != nil { + v, err := thread.EvalPackageVariable(loc.Fn.PackageName() + "." + name) + if err == nil { + v.Name = name + return v, nil + } + } + } + return nil, fmt.Errorf("could not find symbol value for %s", name) } diff --git a/proc/variables_test.go b/proc/variables_test.go index 5736df12..ba82de93 100644 --- a/proc/variables_test.go +++ b/proc/variables_test.go @@ -21,11 +21,11 @@ func assertVariable(t *testing.T, variable *Variable, expected varTest) { } if variable.Type != expected.varType { - t.Fatalf("Expected %s got %s\n", expected.varType, variable.Type) + t.Fatalf("Expected %s got %s (for variable %s)\n", expected.varType, variable.Type, expected.name) } if variable.Value != expected.value { - t.Fatalf("Expected %#v got %#v\n", expected.value, variable.Value) + t.Fatalf("Expected %#v got %#v (for variable %s)\n", expected.value, variable.Value, expected.name) } } @@ -68,6 +68,8 @@ func TestVariableEvaluation(t *testing.T) { {"f", "main.barfoo", "func()", nil}, {"ba", "[]int len: 200, cap: 200, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...+136 more]", "struct []int", nil}, {"ms", "main.Nest {Level: 0, Nest: *main.Nest {Level: 1, Nest: *main.Nest {...}}}", "main.Nest", nil}, + {"main.p1", "10", "int", nil}, + {"p1", "10", "int", nil}, {"NonExistent", "", "", fmt.Errorf("could not find symbol value for NonExistent")}, }