github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/runtime/runtime-gdb_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime_test 6 7 import ( 8 "bytes" 9 "fmt" 10 "go/build" 11 "internal/testenv" 12 "io/ioutil" 13 "os" 14 "os/exec" 15 "path/filepath" 16 "regexp" 17 "runtime" 18 "strconv" 19 "strings" 20 "testing" 21 ) 22 23 func checkGdbEnvironment(t *testing.T) { 24 testenv.MustHaveGoBuild(t) 25 if runtime.GOOS == "darwin" { 26 t.Skip("gdb does not work on darwin") 27 } 28 if runtime.GOOS == "linux" && runtime.GOARCH == "ppc64" { 29 t.Skip("skipping gdb tests on linux/ppc64; see golang.org/issue/17366") 30 } 31 if final := os.Getenv("GOROOT_FINAL"); final != "" && runtime.GOROOT() != final { 32 t.Skip("gdb test can fail with GOROOT_FINAL pending") 33 } 34 } 35 36 func checkGdbVersion(t *testing.T) { 37 // Issue 11214 reports various failures with older versions of gdb. 38 out, err := exec.Command("gdb", "--version").CombinedOutput() 39 if err != nil { 40 t.Skipf("skipping: error executing gdb: %v", err) 41 } 42 re := regexp.MustCompile(`([0-9]+)\.([0-9]+)`) 43 matches := re.FindSubmatch(out) 44 if len(matches) < 3 { 45 t.Skipf("skipping: can't determine gdb version from\n%s\n", out) 46 } 47 major, err1 := strconv.Atoi(string(matches[1])) 48 minor, err2 := strconv.Atoi(string(matches[2])) 49 if err1 != nil || err2 != nil { 50 t.Skipf("skipping: can't determine gdb version: %v, %v", err1, err2) 51 } 52 if major < 7 || (major == 7 && minor < 7) { 53 t.Skipf("skipping: gdb version %d.%d too old", major, minor) 54 } 55 t.Logf("gdb version %d.%d", major, minor) 56 } 57 58 func checkGdbPython(t *testing.T) { 59 cmd := exec.Command("gdb", "-nx", "-q", "--batch", "-iex", "python import sys; print('go gdb python support')") 60 out, err := cmd.CombinedOutput() 61 62 if err != nil { 63 t.Skipf("skipping due to issue running gdb: %v", err) 64 } 65 if string(out) != "go gdb python support\n" { 66 t.Skipf("skipping due to lack of python gdb support: %s", out) 67 } 68 } 69 70 const helloSource = ` 71 import "fmt" 72 var gslice []string 73 func main() { 74 mapvar := make(map[string]string,5) 75 mapvar["abc"] = "def" 76 mapvar["ghi"] = "jkl" 77 strvar := "abc" 78 ptrvar := &strvar 79 slicevar := make([]string, 0, 16) 80 slicevar = append(slicevar, mapvar["abc"]) 81 fmt.Println("hi") // line 12 82 _ = ptrvar 83 gslice = slicevar 84 } 85 ` 86 87 func TestGdbPython(t *testing.T) { 88 testGdbPython(t, false) 89 } 90 91 func TestGdbPythonCgo(t *testing.T) { 92 if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" { 93 testenv.SkipFlaky(t, 18784) 94 } 95 testGdbPython(t, true) 96 } 97 98 func testGdbPython(t *testing.T, cgo bool) { 99 if runtime.GOARCH == "mips64" { 100 testenv.SkipFlaky(t, 18173) 101 } 102 if cgo && !build.Default.CgoEnabled { 103 t.Skip("skipping because cgo is not enabled") 104 } 105 106 t.Parallel() 107 checkGdbEnvironment(t) 108 checkGdbVersion(t) 109 checkGdbPython(t) 110 111 dir, err := ioutil.TempDir("", "go-build") 112 if err != nil { 113 t.Fatalf("failed to create temp directory: %v", err) 114 } 115 defer os.RemoveAll(dir) 116 117 var buf bytes.Buffer 118 buf.WriteString("package main\n") 119 if cgo { 120 buf.WriteString(`import "C"` + "\n") 121 } 122 buf.WriteString(helloSource) 123 124 src := filepath.Join(dir, "main.go") 125 err = ioutil.WriteFile(src, buf.Bytes(), 0644) 126 if err != nil { 127 t.Fatalf("failed to create file: %v", err) 128 } 129 130 cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe") 131 cmd.Dir = dir 132 out, err := testEnv(cmd).CombinedOutput() 133 if err != nil { 134 t.Fatalf("building source %v\n%s", err, out) 135 } 136 137 args := []string{"-nx", "-q", "--batch", "-iex", 138 fmt.Sprintf("add-auto-load-safe-path %s/src/runtime", runtime.GOROOT()), 139 "-ex", "set startup-with-shell off", 140 "-ex", "info auto-load python-scripts", 141 "-ex", "set python print-stack full", 142 "-ex", "br fmt.Println", 143 "-ex", "run", 144 "-ex", "echo BEGIN info goroutines\n", 145 "-ex", "info goroutines", 146 "-ex", "echo END\n", 147 "-ex", "up", // up from fmt.Println to main 148 "-ex", "echo BEGIN print mapvar\n", 149 "-ex", "print mapvar", 150 "-ex", "echo END\n", 151 "-ex", "echo BEGIN print strvar\n", 152 "-ex", "print strvar", 153 "-ex", "echo END\n", 154 "-ex", "echo BEGIN info locals\n", 155 "-ex", "info locals", 156 "-ex", "echo END\n", 157 "-ex", "down", // back to fmt.Println (goroutine 2 below only works at bottom of stack. TODO: fix that) 158 "-ex", "echo BEGIN goroutine 2 bt\n", 159 "-ex", "goroutine 2 bt", 160 "-ex", "echo END\n", 161 filepath.Join(dir, "a.exe"), 162 } 163 got, _ := exec.Command("gdb", args...).CombinedOutput() 164 165 firstLine := bytes.SplitN(got, []byte("\n"), 2)[0] 166 if string(firstLine) != "Loading Go Runtime support." { 167 // This can happen when using all.bash with 168 // GOROOT_FINAL set, because the tests are run before 169 // the final installation of the files. 170 cmd := exec.Command(testenv.GoToolPath(t), "env", "GOROOT") 171 cmd.Env = []string{} 172 out, err := cmd.CombinedOutput() 173 if err != nil && bytes.Contains(out, []byte("cannot find GOROOT")) { 174 t.Skipf("skipping because GOROOT=%s does not exist", runtime.GOROOT()) 175 } 176 177 _, file, _, _ := runtime.Caller(1) 178 179 t.Logf("package testing source file: %s", file) 180 t.Fatalf("failed to load Go runtime support: %s\n%s", firstLine, got) 181 } 182 183 // Extract named BEGIN...END blocks from output 184 partRe := regexp.MustCompile(`(?ms)^BEGIN ([^\n]*)\n(.*?)\nEND`) 185 blocks := map[string]string{} 186 for _, subs := range partRe.FindAllSubmatch(got, -1) { 187 blocks[string(subs[1])] = string(subs[2]) 188 } 189 190 infoGoroutinesRe := regexp.MustCompile(`\*\s+\d+\s+running\s+`) 191 if bl := blocks["info goroutines"]; !infoGoroutinesRe.MatchString(bl) { 192 t.Fatalf("info goroutines failed: %s", bl) 193 } 194 195 printMapvarRe := regexp.MustCompile(`\Q = map[string]string = {["abc"] = "def", ["ghi"] = "jkl"}\E$`) 196 if bl := blocks["print mapvar"]; !printMapvarRe.MatchString(bl) { 197 t.Fatalf("print mapvar failed: %s", bl) 198 } 199 200 strVarRe := regexp.MustCompile(`\Q = "abc"\E$`) 201 if bl := blocks["print strvar"]; !strVarRe.MatchString(bl) { 202 t.Fatalf("print strvar failed: %s", bl) 203 } 204 205 // Issue 16338: ssa decompose phase can split a structure into 206 // a collection of scalar vars holding the fields. In such cases 207 // the DWARF variable location expression should be of the 208 // form "var.field" and not just "field". 209 infoLocalsRe := regexp.MustCompile(`^slicevar.len = `) 210 if bl := blocks["info locals"]; !infoLocalsRe.MatchString(bl) { 211 t.Fatalf("info locals failed: %s", bl) 212 } 213 214 btGoroutineRe := regexp.MustCompile(`^#0\s+runtime.+at`) 215 if bl := blocks["goroutine 2 bt"]; !btGoroutineRe.MatchString(bl) { 216 t.Fatalf("goroutine 2 bt failed: %s", bl) 217 } 218 } 219 220 const backtraceSource = ` 221 package main 222 223 //go:noinline 224 func aaa() bool { return bbb() } 225 226 //go:noinline 227 func bbb() bool { return ccc() } 228 229 //go:noinline 230 func ccc() bool { return ddd() } 231 232 //go:noinline 233 func ddd() bool { return f() } 234 235 //go:noinline 236 func eee() bool { return true } 237 238 var f = eee 239 240 func main() { 241 _ = aaa() 242 } 243 ` 244 245 // TestGdbBacktrace tests that gdb can unwind the stack correctly 246 // using only the DWARF debug info. 247 func TestGdbBacktrace(t *testing.T) { 248 if runtime.GOOS == "netbsd" { 249 testenv.SkipFlaky(t, 15603) 250 } 251 if runtime.GOARCH == "mips64" { 252 testenv.SkipFlaky(t, 18173) 253 } 254 255 t.Parallel() 256 checkGdbEnvironment(t) 257 checkGdbVersion(t) 258 259 dir, err := ioutil.TempDir("", "go-build") 260 if err != nil { 261 t.Fatalf("failed to create temp directory: %v", err) 262 } 263 defer os.RemoveAll(dir) 264 265 // Build the source code. 266 src := filepath.Join(dir, "main.go") 267 err = ioutil.WriteFile(src, []byte(backtraceSource), 0644) 268 if err != nil { 269 t.Fatalf("failed to create file: %v", err) 270 } 271 cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe") 272 cmd.Dir = dir 273 out, err := testEnv(cmd).CombinedOutput() 274 if err != nil { 275 t.Fatalf("building source %v\n%s", err, out) 276 } 277 278 // Execute gdb commands. 279 args := []string{"-nx", "-batch", 280 "-ex", "set startup-with-shell off", 281 "-ex", "break main.eee", 282 "-ex", "run", 283 "-ex", "backtrace", 284 "-ex", "continue", 285 filepath.Join(dir, "a.exe"), 286 } 287 got, _ := exec.Command("gdb", args...).CombinedOutput() 288 289 // Check that the backtrace matches the source code. 290 bt := []string{ 291 "eee", 292 "ddd", 293 "ccc", 294 "bbb", 295 "aaa", 296 "main", 297 } 298 for i, name := range bt { 299 s := fmt.Sprintf("#%v.*main\\.%v", i, name) 300 re := regexp.MustCompile(s) 301 if found := re.Find(got) != nil; !found { 302 t.Errorf("could not find '%v' in backtrace", s) 303 t.Fatalf("gdb output:\n%v", string(got)) 304 } 305 } 306 } 307 308 const autotmpTypeSource = ` 309 package main 310 311 type astruct struct { 312 a, b int 313 } 314 315 func main() { 316 var iface interface{} = map[string]astruct{} 317 var iface2 interface{} = []astruct{} 318 println(iface, iface2) 319 } 320 ` 321 322 // TestGdbAutotmpTypes ensures that types of autotmp variables appear in .debug_info 323 // See bug #17830. 324 func TestGdbAutotmpTypes(t *testing.T) { 325 if runtime.GOARCH == "mips64" { 326 testenv.SkipFlaky(t, 18173) 327 } 328 329 t.Parallel() 330 checkGdbEnvironment(t) 331 checkGdbVersion(t) 332 333 dir, err := ioutil.TempDir("", "go-build") 334 if err != nil { 335 t.Fatalf("failed to create temp directory: %v", err) 336 } 337 defer os.RemoveAll(dir) 338 339 // Build the source code. 340 src := filepath.Join(dir, "main.go") 341 err = ioutil.WriteFile(src, []byte(autotmpTypeSource), 0644) 342 if err != nil { 343 t.Fatalf("failed to create file: %v", err) 344 } 345 cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=-N -l", "-o", "a.exe") 346 cmd.Dir = dir 347 out, err := testEnv(cmd).CombinedOutput() 348 if err != nil { 349 t.Fatalf("building source %v\n%s", err, out) 350 } 351 352 // Execute gdb commands. 353 args := []string{"-nx", "-batch", 354 "-ex", "set startup-with-shell off", 355 "-ex", "break main.main", 356 "-ex", "run", 357 "-ex", "step", 358 "-ex", "info types astruct", 359 filepath.Join(dir, "a.exe"), 360 } 361 got, _ := exec.Command("gdb", args...).CombinedOutput() 362 363 sgot := string(got) 364 365 // Check that the backtrace matches the source code. 366 types := []string{ 367 "struct []main.astruct;", 368 "struct bucket<string,main.astruct>;", 369 "struct hash<string,main.astruct>;", 370 "struct main.astruct;", 371 "typedef struct hash<string,main.astruct> * map[string]main.astruct;", 372 } 373 for _, name := range types { 374 if !strings.Contains(sgot, name) { 375 t.Errorf("could not find %s in 'info typrs astruct' output", name) 376 t.Fatalf("gdb output:\n%v", sgot) 377 } 378 } 379 }