github.com/yukk001/go1.10.8@v0.0.0-20190813125351-6df2d3982e20/doc/debugging_with_gdb.html (about) 1 <!--{ 2 "Title": "Debugging Go Code with GDB", 3 "Path": "/doc/gdb" 4 }--> 5 6 <!-- 7 NOTE: In this document and others in this directory, the convention is to 8 set fixed-width phrases with non-fixed-width spaces, as in 9 <code>hello</code> <code>world</code>. 10 Do not send CLs removing the interior tags from such phrases. 11 --> 12 13 <i> 14 <p> 15 The following instructions apply to the standard toolchain 16 (the <code>gc</code> Go compiler and tools). 17 Gccgo has native gdb support. 18 </p> 19 <p> 20 Note that 21 <a href="https://github.com/derekparker/delve">Delve</a> is a better 22 alternative to GDB when debugging Go programs built with the standard 23 toolchain. It understands the Go runtime, data structures, and 24 expressions better than GDB. Delve currently supports Linux, OSX, 25 and Windows on <code>amd64</code>. 26 For the most up-to-date list of supported platforms, please see 27 <a href="https://github.com/derekparker/delve/tree/master/Documentation/installation"> 28 the Delve documentation</a>. 29 </p> 30 </i> 31 32 <p> 33 GDB does not understand Go programs well. 34 The stack management, threading, and runtime contain aspects that differ 35 enough from the execution model GDB expects that they can confuse 36 the debugger and cause incorrect results even when the program is 37 compiled with gccgo. 38 As a consequence, although GDB can be useful in some situations (e.g., 39 debugging Cgo code, or debugging the runtime itself), it is not 40 a reliable debugger for Go programs, particularly heavily concurrent 41 ones. Moreover, it is not a priority for the Go project to address 42 these issues, which are difficult. 43 </p> 44 45 <p> 46 In short, the instructions below should be taken only as a guide to how 47 to use GDB when it works, not as a guarantee of success. 48 49 Besides this overview you might want to consult the 50 <a href="http://sourceware.org/gdb/current/onlinedocs/gdb/">GDB manual</a>. 51 </p> 52 53 <p> 54 </p> 55 56 <h2 id="Introduction">Introduction</h2> 57 58 <p> 59 When you compile and link your Go programs with the <code>gc</code> toolchain 60 on Linux, Mac OS X, FreeBSD or NetBSD, the resulting binaries contain DWARFv4 61 debugging information that recent versions (≥7.5) of the GDB debugger can 62 use to inspect a live process or a core dump. 63 </p> 64 65 <p> 66 Pass the <code>'-w'</code> flag to the linker to omit the debug information 67 (for example, <code>go</code> <code>build</code> <code>-ldflags=-w</code> <code>prog.go</code>). 68 </p> 69 70 <p> 71 The code generated by the <code>gc</code> compiler includes inlining of 72 function invocations and registerization of variables. These optimizations 73 can sometimes make debugging with <code>gdb</code> harder. 74 If you find that you need to disable these optimizations, 75 build your program using <code>go</code> <code>build</code> <code>-gcflags=all="-N -l"</code>. 76 </p> 77 78 <p> 79 If you want to use gdb to inspect a core dump, you can trigger a dump 80 on a program crash, on systems that permit it, by setting 81 <code>GOTRACEBACK=crash</code> in the environment (see the 82 <a href="/pkg/runtime/#hdr-Environment_Variables"> runtime package 83 documentation</a> for more info). 84 </p> 85 86 <h3 id="Common_Operations">Common Operations</h3> 87 88 <ul> 89 <li> 90 Show file and line number for code, set breakpoints and disassemble: 91 <pre>(gdb) <b>list</b> 92 (gdb) <b>list <i>line</i></b> 93 (gdb) <b>list <i>file.go</i>:<i>line</i></b> 94 (gdb) <b>break <i>line</i></b> 95 (gdb) <b>break <i>file.go</i>:<i>line</i></b> 96 (gdb) <b>disas</b></pre> 97 </li> 98 <li> 99 Show backtraces and unwind stack frames: 100 <pre>(gdb) <b>bt</b> 101 (gdb) <b>frame <i>n</i></b></pre> 102 </li> 103 <li> 104 Show the name, type and location on the stack frame of local variables, 105 arguments and return values: 106 <pre>(gdb) <b>info locals</b> 107 (gdb) <b>info args</b> 108 (gdb) <b>p variable</b> 109 (gdb) <b>whatis variable</b></pre> 110 </li> 111 <li> 112 Show the name, type and location of global variables: 113 <pre>(gdb) <b>info variables <i>regexp</i></b></pre> 114 </li> 115 </ul> 116 117 118 <h3 id="Go_Extensions">Go Extensions</h3> 119 120 <p> 121 A recent extension mechanism to GDB allows it to load extension scripts for a 122 given binary. The toolchain uses this to extend GDB with a handful of 123 commands to inspect internals of the runtime code (such as goroutines) and to 124 pretty print the built-in map, slice and channel types. 125 </p> 126 127 <ul> 128 <li> 129 Pretty printing a string, slice, map, channel or interface: 130 <pre>(gdb) <b>p <i>var</i></b></pre> 131 </li> 132 <li> 133 A $len() and $cap() function for strings, slices and maps: 134 <pre>(gdb) <b>p $len(<i>var</i>)</b></pre> 135 </li> 136 <li> 137 A function to cast interfaces to their dynamic types: 138 <pre>(gdb) <b>p $dtype(<i>var</i>)</b> 139 (gdb) <b>iface <i>var</i></b></pre> 140 <p class="detail"><b>Known issue:</b> GDB can’t automatically find the dynamic 141 type of an interface value if its long name differs from its short name 142 (annoying when printing stacktraces, the pretty printer falls back to printing 143 the short type name and a pointer).</p> 144 </li> 145 <li> 146 Inspecting goroutines: 147 <pre>(gdb) <b>info goroutines</b> 148 (gdb) <b>goroutine <i>n</i> <i>cmd</i></b> 149 (gdb) <b>help goroutine</b></pre> 150 For example: 151 <pre>(gdb) <b>goroutine 12 bt</b></pre> 152 </li> 153 </ul> 154 155 <p> 156 If you'd like to see how this works, or want to extend it, take a look at <a 157 href="/src/runtime/runtime-gdb.py">src/runtime/runtime-gdb.py</a> in 158 the Go source distribution. It depends on some special magic types 159 (<code>hash<T,U></code>) and variables (<code>runtime.m</code> and 160 <code>runtime.g</code>) that the linker 161 (<a href="/src/cmd/link/internal/ld/dwarf.go">src/cmd/link/internal/ld/dwarf.go</a>) ensures are described in 162 the DWARF code. 163 </p> 164 165 <p> 166 If you're interested in what the debugging information looks like, run 167 <code>objdump</code> <code>-W</code> <code>a.out</code> and browse through the <code>.debug_*</code> 168 sections. 169 </p> 170 171 172 <h3 id="Known_Issues">Known Issues</h3> 173 174 <ol> 175 <li>String pretty printing only triggers for type string, not for types derived 176 from it.</li> 177 <li>Type information is missing for the C parts of the runtime library.</li> 178 <li>GDB does not understand Go’s name qualifications and treats 179 <code>"fmt.Print"</code> as an unstructured literal with a <code>"."</code> 180 that needs to be quoted. It objects even more strongly to method names of 181 the form <code>pkg.(*MyType).Meth</code>. 182 <li>All global variables are lumped into package <code>"main"</code>.</li> 183 </ol> 184 185 <h2 id="Tutorial">Tutorial</h2> 186 187 <p> 188 In this tutorial we will inspect the binary of the 189 <a href="/pkg/regexp/">regexp</a> package's unit tests. To build the binary, 190 change to <code>$GOROOT/src/regexp</code> and run <code>go</code> <code>test</code> <code>-c</code>. 191 This should produce an executable file named <code>regexp.test</code>. 192 </p> 193 194 195 <h3 id="Getting_Started">Getting Started</h3> 196 197 <p> 198 Launch GDB, debugging <code>regexp.test</code>: 199 </p> 200 201 <pre> 202 $ <b>gdb regexp.test</b> 203 GNU gdb (GDB) 7.2-gg8 204 Copyright (C) 2010 Free Software Foundation, Inc. 205 License GPLv 3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 206 Type "show copying" and "show warranty" for licensing/warranty details. 207 This GDB was configured as "x86_64-linux". 208 209 Reading symbols from /home/user/go/src/regexp/regexp.test... 210 done. 211 Loading Go Runtime support. 212 (gdb) 213 </pre> 214 215 <p> 216 The message "Loading Go Runtime support" means that GDB loaded the 217 extension from <code>$GOROOT/src/runtime/runtime-gdb.py</code>. 218 </p> 219 220 <p> 221 To help GDB find the Go runtime sources and the accompanying support script, 222 pass your <code>$GOROOT</code> with the <code>'-d'</code> flag: 223 </p> 224 225 <pre> 226 $ <b>gdb regexp.test -d $GOROOT</b> 227 </pre> 228 229 <p> 230 If for some reason GDB still can't find that directory or that script, you can load 231 it by hand by telling gdb (assuming you have the go sources in 232 <code>~/go/</code>): 233 </p> 234 235 <pre> 236 (gdb) <b>source ~/go/src/runtime/runtime-gdb.py</b> 237 Loading Go Runtime support. 238 </pre> 239 240 <h3 id="Inspecting_the_source">Inspecting the source</h3> 241 242 <p> 243 Use the <code>"l"</code> or <code>"list"</code> command to inspect source code. 244 </p> 245 246 <pre> 247 (gdb) <b>l</b> 248 </pre> 249 250 <p> 251 List a specific part of the source parametrizing <code>"list"</code> with a 252 function name (it must be qualified with its package name). 253 </p> 254 255 <pre> 256 (gdb) <b>l main.main</b> 257 </pre> 258 259 <p> 260 List a specific file and line number: 261 </p> 262 263 <pre> 264 (gdb) <b>l regexp.go:1</b> 265 (gdb) <i># Hit enter to repeat last command. Here, this lists next 10 lines.</i> 266 </pre> 267 268 269 <h3 id="Naming">Naming</h3> 270 271 <p> 272 Variable and function names must be qualified with the name of the packages 273 they belong to. The <code>Compile</code> function from the <code>regexp</code> 274 package is known to GDB as <code>'regexp.Compile'</code>. 275 </p> 276 277 <p> 278 Methods must be qualified with the name of their receiver types. For example, 279 the <code>*Regexp</code> type’s <code>String</code> method is known as 280 <code>'regexp.(*Regexp).String'</code>. 281 </p> 282 283 <p> 284 Variables that shadow other variables are magically suffixed with a number in the debug info. 285 Variables referenced by closures will appear as pointers magically prefixed with '&'. 286 </p> 287 288 <h3 id="Setting_breakpoints">Setting breakpoints</h3> 289 290 <p> 291 Set a breakpoint at the <code>TestFind</code> function: 292 </p> 293 294 <pre> 295 (gdb) <b>b 'regexp.TestFind'</b> 296 Breakpoint 1 at 0x424908: file /home/user/go/src/regexp/find_test.go, line 148. 297 </pre> 298 299 <p> 300 Run the program: 301 </p> 302 303 <pre> 304 (gdb) <b>run</b> 305 Starting program: /home/user/go/src/regexp/regexp.test 306 307 Breakpoint 1, regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/regexp/find_test.go:148 308 148 func TestFind(t *testing.T) { 309 </pre> 310 311 <p> 312 Execution has paused at the breakpoint. 313 See which goroutines are running, and what they're doing: 314 </p> 315 316 <pre> 317 (gdb) <b>info goroutines</b> 318 1 waiting runtime.gosched 319 * 13 running runtime.goexit 320 </pre> 321 322 <p> 323 the one marked with the <code>*</code> is the current goroutine. 324 </p> 325 326 <h3 id="Inspecting_the_stack">Inspecting the stack</h3> 327 328 <p> 329 Look at the stack trace for where we’ve paused the program: 330 </p> 331 332 <pre> 333 (gdb) <b>bt</b> <i># backtrace</i> 334 #0 regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/regexp/find_test.go:148 335 #1 0x000000000042f60b in testing.tRunner (t=0xf8404a89c0, test=0x573720) at /home/user/go/src/testing/testing.go:156 336 #2 0x000000000040df64 in runtime.initdone () at /home/user/go/src/runtime/proc.c:242 337 #3 0x000000f8404a89c0 in ?? () 338 #4 0x0000000000573720 in ?? () 339 #5 0x0000000000000000 in ?? () 340 </pre> 341 342 <p> 343 The other goroutine, number 1, is stuck in <code>runtime.gosched</code>, blocked on a channel receive: 344 </p> 345 346 <pre> 347 (gdb) <b>goroutine 1 bt</b> 348 #0 0x000000000040facb in runtime.gosched () at /home/user/go/src/runtime/proc.c:873 349 #1 0x00000000004031c9 in runtime.chanrecv (c=void, ep=void, selected=void, received=void) 350 at /home/user/go/src/runtime/chan.c:342 351 #2 0x0000000000403299 in runtime.chanrecv1 (t=void, c=void) at/home/user/go/src/runtime/chan.c:423 352 #3 0x000000000043075b in testing.RunTests (matchString={void (struct string, struct string, bool *, error *)} 353 0x7ffff7f9ef60, tests= []testing.InternalTest = {...}) at /home/user/go/src/testing/testing.go:201 354 #4 0x00000000004302b1 in testing.Main (matchString={void (struct string, struct string, bool *, error *)} 355 0x7ffff7f9ef80, tests= []testing.InternalTest = {...}, benchmarks= []testing.InternalBenchmark = {...}) 356 at /home/user/go/src/testing/testing.go:168 357 #5 0x0000000000400dc1 in main.main () at /home/user/go/src/regexp/_testmain.go:98 358 #6 0x00000000004022e7 in runtime.mainstart () at /home/user/go/src/runtime/amd64/asm.s:78 359 #7 0x000000000040ea6f in runtime.initdone () at /home/user/go/src/runtime/proc.c:243 360 #8 0x0000000000000000 in ?? () 361 </pre> 362 363 <p> 364 The stack frame shows we’re currently executing the <code>regexp.TestFind</code> function, as expected. 365 </p> 366 367 <pre> 368 (gdb) <b>info frame</b> 369 Stack level 0, frame at 0x7ffff7f9ff88: 370 rip = 0x425530 in regexp.TestFind (/home/user/go/src/regexp/find_test.go:148); 371 saved rip 0x430233 372 called by frame at 0x7ffff7f9ffa8 373 source language minimal. 374 Arglist at 0x7ffff7f9ff78, args: t=0xf840688b60 375 Locals at 0x7ffff7f9ff78, Previous frame's sp is 0x7ffff7f9ff88 376 Saved registers: 377 rip at 0x7ffff7f9ff80 378 </pre> 379 380 <p> 381 The command <code>info</code> <code>locals</code> lists all variables local to the function and their values, but is a bit 382 dangerous to use, since it will also try to print uninitialized variables. Uninitialized slices may cause gdb to try 383 to print arbitrary large arrays. 384 </p> 385 386 <p> 387 The function’s arguments: 388 </p> 389 390 <pre> 391 (gdb) <b>info args</b> 392 t = 0xf840688b60 393 </pre> 394 395 <p> 396 When printing the argument, notice that it’s a pointer to a 397 <code>Regexp</code> value. Note that GDB has incorrectly put the <code>*</code> 398 on the right-hand side of the type name and made up a 'struct' keyword, in traditional C style. 399 </p> 400 401 <pre> 402 (gdb) <b>p re</b> 403 (gdb) p t 404 $1 = (struct testing.T *) 0xf840688b60 405 (gdb) p t 406 $1 = (struct testing.T *) 0xf840688b60 407 (gdb) p *t 408 $2 = {errors = "", failed = false, ch = 0xf8406f5690} 409 (gdb) p *t->ch 410 $3 = struct hchan<*testing.T> 411 </pre> 412 413 <p> 414 That <code>struct</code> <code>hchan<*testing.T></code> is the 415 runtime-internal representation of a channel. It is currently empty, 416 or gdb would have pretty-printed its contents. 417 </p> 418 419 <p> 420 Stepping forward: 421 </p> 422 423 <pre> 424 (gdb) <b>n</b> <i># execute next line</i> 425 149 for _, test := range findTests { 426 (gdb) <i># enter is repeat</i> 427 150 re := MustCompile(test.pat) 428 (gdb) <b>p test.pat</b> 429 $4 = "" 430 (gdb) <b>p re</b> 431 $5 = (struct regexp.Regexp *) 0xf84068d070 432 (gdb) <b>p *re</b> 433 $6 = {expr = "", prog = 0xf840688b80, prefix = "", prefixBytes = []uint8, prefixComplete = true, 434 prefixRune = 0, cond = 0 '\000', numSubexp = 0, longest = false, mu = {state = 0, sema = 0}, 435 machine = []*regexp.machine} 436 (gdb) <b>p *re->prog</b> 437 $7 = {Inst = []regexp/syntax.Inst = {{Op = 5 '\005', Out = 0, Arg = 0, Rune = []int}, {Op = 438 6 '\006', Out = 2, Arg = 0, Rune = []int}, {Op = 4 '\004', Out = 0, Arg = 0, Rune = []int}}, 439 Start = 1, NumCap = 2} 440 </pre> 441 442 443 <p> 444 We can step into the <code>String</code>function call with <code>"s"</code>: 445 </p> 446 447 <pre> 448 (gdb) <b>s</b> 449 regexp.(*Regexp).String (re=0xf84068d070, noname=void) at /home/user/go/src/regexp/regexp.go:97 450 97 func (re *Regexp) String() string { 451 </pre> 452 453 <p> 454 Get a stack trace to see where we are: 455 </p> 456 457 <pre> 458 (gdb) <b>bt</b> 459 #0 regexp.(*Regexp).String (re=0xf84068d070, noname=void) 460 at /home/user/go/src/regexp/regexp.go:97 461 #1 0x0000000000425615 in regexp.TestFind (t=0xf840688b60) 462 at /home/user/go/src/regexp/find_test.go:151 463 #2 0x0000000000430233 in testing.tRunner (t=0xf840688b60, test=0x5747b8) 464 at /home/user/go/src/testing/testing.go:156 465 #3 0x000000000040ea6f in runtime.initdone () at /home/user/go/src/runtime/proc.c:243 466 .... 467 </pre> 468 469 <p> 470 Look at the source code: 471 </p> 472 473 <pre> 474 (gdb) <b>l</b> 475 92 mu sync.Mutex 476 93 machine []*machine 477 94 } 478 95 479 96 // String returns the source text used to compile the regular expression. 480 97 func (re *Regexp) String() string { 481 98 return re.expr 482 99 } 483 100 484 101 // Compile parses a regular expression and returns, if successful, 485 </pre> 486 487 <h3 id="Pretty_Printing">Pretty Printing</h3> 488 489 <p> 490 GDB's pretty printing mechanism is triggered by regexp matches on type names. An example for slices: 491 </p> 492 493 <pre> 494 (gdb) <b>p utf</b> 495 $22 = []uint8 = {0 '\000', 0 '\000', 0 '\000', 0 '\000'} 496 </pre> 497 498 <p> 499 Since slices, arrays and strings are not C pointers, GDB can't interpret the subscripting operation for you, but 500 you can look inside the runtime representation to do that (tab completion helps here): 501 </p> 502 <pre> 503 504 (gdb) <b>p slc</b> 505 $11 = []int = {0, 0} 506 (gdb) <b>p slc-></b><i><TAB></i> 507 array slc len 508 (gdb) <b>p slc->array</b> 509 $12 = (int *) 0xf84057af00 510 (gdb) <b>p slc->array[1]</b> 511 $13 = 0</pre> 512 513 514 515 <p> 516 The extension functions $len and $cap work on strings, arrays and slices: 517 </p> 518 519 <pre> 520 (gdb) <b>p $len(utf)</b> 521 $23 = 4 522 (gdb) <b>p $cap(utf)</b> 523 $24 = 4 524 </pre> 525 526 <p> 527 Channels and maps are 'reference' types, which gdb shows as pointers to C++-like types <code>hash<int,string>*</code>. Dereferencing will trigger prettyprinting 528 </p> 529 530 <p> 531 Interfaces are represented in the runtime as a pointer to a type descriptor and a pointer to a value. The Go GDB runtime extension decodes this and automatically triggers pretty printing for the runtime type. The extension function <code>$dtype</code> decodes the dynamic type for you (examples are taken from a breakpoint at <code>regexp.go</code> line 293.) 532 </p> 533 534 <pre> 535 (gdb) <b>p i</b> 536 $4 = {str = "cbb"} 537 (gdb) <b>whatis i</b> 538 type = regexp.input 539 (gdb) <b>p $dtype(i)</b> 540 $26 = (struct regexp.inputBytes *) 0xf8400b4930 541 (gdb) <b>iface i</b> 542 regexp.input: struct regexp.inputBytes * 543 </pre>