Skip to content

Commit d80e8de

Browse files
thanmmdempsky
authored andcommitted
cmd/compile: avoid truncating fieldname var locations
Don't include package path when creating LSyms for auto and param variables during Prog generation, and update the DWARF emit routine accordingly (remove the code that chops off package path from names in DWARF var location expressions). Implementation suggested by mdempsky@. The intent of this change is to have saner location expressions in cases where the variable corresponds to a structure field. For example, the SSA compiler's "decompose" phase can take a slice value and break it apart into three scalar variables corresponding to the fields (slice "X" gets split into "X.len", "X.cap", "X.ptr"). In such cases we want the name in the location expression to omit the package path but preserve the original variable name (e.g. "X"). Fixes #16338 Change-Id: Ibc444e7f3454b70fc500a33f0397e669d127daa1 Reviewed-on: https://go-review.googlesource.com/31819 Run-TryBot: Than McIntosh <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent c7e0dda commit d80e8de

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/cmd/compile/internal/gc/pgen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ func compile(fn *Node) {
423423
fallthrough
424424
case PPARAM, PPARAMOUT:
425425
p := Gins(obj.ATYPE, n, nil)
426+
p.From.Sym = obj.Linklookup(Ctxt, n.Sym.Name, 0)
426427
p.To.Type = obj.TYPE_MEM
427428
p.To.Name = obj.NAME_EXTERN
428429
p.To.Sym = Linksym(ngotype(n))

src/cmd/internal/dwarf/dwarf.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,6 @@ func PutFunc(ctxt Context, s Sym, name string, external bool, startPC Sym, size
588588
}
589589
names[n] = true
590590

591-
// Drop the package prefix from locals and arguments.
592-
if i := strings.LastIndex(n, "."); i >= 0 {
593-
n = n[i+1:]
594-
}
595-
596591
Uleb128put(ctxt, s, int64(v.Abbrev))
597592
putattr(ctxt, s, v.Abbrev, DW_FORM_string, DW_CLS_STRING, int64(len(n)), n)
598593
loc := append(encbuf[:0], DW_OP_call_frame_cfa)

src/runtime/runtime-gdb_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,18 @@ func checkGdbPython(t *testing.T) {
6868
const helloSource = `
6969
package main
7070
import "fmt"
71+
var gslice []string
7172
func main() {
7273
mapvar := make(map[string]string,5)
7374
mapvar["abc"] = "def"
7475
mapvar["ghi"] = "jkl"
7576
strvar := "abc"
7677
ptrvar := &strvar
77-
fmt.Println("hi") // line 10
78+
slicevar := make([]string, 0, 16)
79+
slicevar = append(slicevar, mapvar["abc"])
80+
fmt.Println("hi") // line 12
7881
_ = ptrvar
82+
gslice = slicevar
7983
}
8084
`
8185

@@ -120,6 +124,9 @@ func TestGdbPython(t *testing.T) {
120124
"-ex", "echo BEGIN print strvar\n",
121125
"-ex", "print strvar",
122126
"-ex", "echo END\n",
127+
"-ex", "echo BEGIN info locals\n",
128+
"-ex", "info locals",
129+
"-ex", "echo END\n",
123130
"-ex", "down", // back to fmt.Println (goroutine 2 below only works at bottom of stack. TODO: fix that)
124131
"-ex", "echo BEGIN goroutine 2 bt\n",
125132
"-ex", "goroutine 2 bt",
@@ -168,6 +175,15 @@ func TestGdbPython(t *testing.T) {
168175
t.Fatalf("print strvar failed: %s", bl)
169176
}
170177

178+
// Issue 16338: ssa decompose phase can split a structure into
179+
// a collection of scalar vars holding the fields. In such cases
180+
// the DWARF variable location expression should be of the
181+
// form "var.field" and not just "field".
182+
infoLocalsRe := regexp.MustCompile(`^slicevar.len = `)
183+
if bl := blocks["info locals"]; !infoLocalsRe.MatchString(bl) {
184+
t.Fatalf("info locals failed: %s", bl)
185+
}
186+
171187
btGoroutineRe := regexp.MustCompile(`^#0\s+runtime.+at`)
172188
if bl := blocks["goroutine 2 bt"]; !btGoroutineRe.MatchString(bl) {
173189
t.Fatalf("goroutine 2 bt failed: %s", bl)

0 commit comments

Comments
 (0)