Skip to content

Commit c45751e

Browse files
leepaianlancetaylor
authored andcommitted
runtime: allow pointers to strings to be printed
Being able to printer pointers to strings means one will able to output the result of things like the flag library and other components that use string pointers. While here, adjusted the tests for gdb to test original string pretty printing as well as pointers to them. It was doing it via the map before but for completeness this ensures it's tested as a unit. Change-Id: I4926547ae4fa6c85ef74301e7d96d49ba4a7b0c6 Reviewed-on: https://go-review.googlesource.com/8217 TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 6ca91d2 commit c45751e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/runtime/runtime-gdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __getitem__(self, i):
6060
class StringTypePrinter:
6161
"Pretty print Go strings."
6262

63-
pattern = re.compile(r'^struct string$')
63+
pattern = re.compile(r'^struct string( \*)?$')
6464

6565
def __init__(self, val):
6666
self.val = val

src/runtime/runtime-gdb_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func main() {
3131
mapvar := make(map[string]string,5)
3232
mapvar["abc"] = "def"
3333
mapvar["ghi"] = "jkl"
34-
fmt.Println("hi") // line 8
34+
strvar := "abc"
35+
ptrvar := &strvar
36+
fmt.Println("hi") // line 10
37+
_ = ptrvar
3538
}
3639
`
3740

@@ -63,14 +66,20 @@ func TestGdbPython(t *testing.T) {
6366

6467
got, _ := exec.Command("gdb", "-nx", "-q", "--batch", "-iex",
6568
fmt.Sprintf("add-auto-load-safe-path %s/src/runtime", runtime.GOROOT()),
66-
"-ex", "br main.go:8",
69+
"-ex", "br main.go:10",
6770
"-ex", "run",
6871
"-ex", "echo BEGIN info goroutines\n",
6972
"-ex", "info goroutines",
7073
"-ex", "echo END\n",
7174
"-ex", "echo BEGIN print mapvar\n",
7275
"-ex", "print mapvar",
7376
"-ex", "echo END\n",
77+
"-ex", "echo BEGIN print strvar\n",
78+
"-ex", "print strvar",
79+
"-ex", "echo END\n",
80+
"-ex", "echo BEGIN print ptrvar\n",
81+
"-ex", "print ptrvar",
82+
"-ex", "echo END\n",
7483
filepath.Join(dir, "a.exe")).CombinedOutput()
7584

7685
firstLine := bytes.SplitN(got, []byte("\n"), 2)[0]
@@ -94,4 +103,13 @@ func TestGdbPython(t *testing.T) {
94103
if bl := blocks["print mapvar"]; !printMapvarRe.MatchString(bl) {
95104
t.Fatalf("print mapvar failed: %s", bl)
96105
}
106+
107+
strVarRe := regexp.MustCompile(`\Q = "abc"\E$`)
108+
if bl := blocks["print strvar"]; !strVarRe.MatchString(bl) {
109+
t.Fatalf("print strvar failed: %s", bl)
110+
}
111+
112+
if bl := blocks["print ptrvar"]; !strVarRe.MatchString(bl) {
113+
t.Fatalf("print ptrvar failed: %s", bl)
114+
}
97115
}

0 commit comments

Comments
 (0)