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