github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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="https://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, macOS, 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>As of Go 1.11, debug information is compressed by default. 183 Older versions of gdb, such as the one available by default on MacOS, 184 do not understand the compression. 185 You can generate uncompressed debug information by using <code>go 186 build -ldflags=-compressdwarf=false</code>. 187 (For convenience you can put the <code>-ldflags</code> option in 188 the <a href="/cmd/go/#hdr-Environment_variables"><code>GOFLAGS</code> 189 environment variable</a> so that you don't have to specify it each time.) 190 </li> 191 </ol> 192 193 <h2 id="Tutorial">Tutorial</h2> 194 195 <p> 196 In this tutorial we will inspect the binary of the 197 <a href="/pkg/regexp/">regexp</a> package's unit tests. To build the binary, 198 change to <code>$GOROOT/src/regexp</code> and run <code>go</code> <code>test</code> <code>-c</code>. 199 This should produce an executable file named <code>regexp.test</code>. 200 </p> 201 202 203 <h3 id="Getting_Started">Getting Started</h3> 204 205 <p> 206 Launch GDB, debugging <code>regexp.test</code>: 207 </p> 208 209 <pre> 210 $ <b>gdb regexp.test</b> 211 GNU gdb (GDB) 7.2-gg8 212 Copyright (C) 2010 Free Software Foundation, Inc. 213 License GPLv 3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 214 Type "show copying" and "show warranty" for licensing/warranty details. 215 This GDB was configured as "x86_64-linux". 216 217 Reading symbols from /home/user/go/src/regexp/regexp.test... 218 done. 219 Loading Go Runtime support. 220 (gdb) 221 </pre> 222 223 <p> 224 The message "Loading Go Runtime support" means that GDB loaded the 225 extension from <code>$GOROOT/src/runtime/runtime-gdb.py</code>. 226 </p> 227 228 <p> 229 To help GDB find the Go runtime sources and the accompanying support script, 230 pass your <code>$GOROOT</code> with the <code>'-d'</code> flag: 231 </p> 232 233 <pre> 234 $ <b>gdb regexp.test -d $GOROOT</b> 235 </pre> 236 237 <p> 238 If for some reason GDB still can't find that directory or that script, you can load 239 it by hand by telling gdb (assuming you have the go sources in 240 <code>~/go/</code>): 241 </p> 242 243 <pre> 244 (gdb) <b>source ~/go/src/runtime/runtime-gdb.py</b> 245 Loading Go Runtime support. 246 </pre> 247 248 <h3 id="Inspecting_the_source">Inspecting the source</h3> 249 250 <p> 251 Use the <code>"l"</code> or <code>"list"</code> command to inspect source code. 252 </p> 253 254 <pre> 255 (gdb) <b>l</b> 256 </pre> 257 258 <p> 259 List a specific part of the source parametrizing <code>"list"</code> with a 260 function name (it must be qualified with its package name). 261 </p> 262 263 <pre> 264 (gdb) <b>l main.main</b> 265 </pre> 266 267 <p> 268 List a specific file and line number: 269 </p> 270 271 <pre> 272 (gdb) <b>l regexp.go:1</b> 273 (gdb) <i># Hit enter to repeat last command. Here, this lists next 10 lines.</i> 274 </pre> 275 276 277 <h3 id="Naming">Naming</h3> 278 279 <p> 280 Variable and function names must be qualified with the name of the packages 281 they belong to. The <code>Compile</code> function from the <code>regexp</code> 282 package is known to GDB as <code>'regexp.Compile'</code>. 283 </p> 284 285 <p> 286 Methods must be qualified with the name of their receiver types. For example, 287 the <code>*Regexp</code> type’s <code>String</code> method is known as 288 <code>'regexp.(*Regexp).String'</code>. 289 </p> 290 291 <p> 292 Variables that shadow other variables are magically suffixed with a number in the debug info. 293 Variables referenced by closures will appear as pointers magically prefixed with '&'. 294 </p> 295 296 <h3 id="Setting_breakpoints">Setting breakpoints</h3> 297 298 <p> 299 Set a breakpoint at the <code>TestFind</code> function: 300 </p> 301 302 <pre> 303 (gdb) <b>b 'regexp.TestFind'</b> 304 Breakpoint 1 at 0x424908: file /home/user/go/src/regexp/find_test.go, line 148. 305 </pre> 306 307 <p> 308 Run the program: 309 </p> 310 311 <pre> 312 (gdb) <b>run</b> 313 Starting program: /home/user/go/src/regexp/regexp.test 314 315 Breakpoint 1, regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/regexp/find_test.go:148 316 148 func TestFind(t *testing.T) { 317 </pre> 318 319 <p> 320 Execution has paused at the breakpoint. 321 See which goroutines are running, and what they're doing: 322 </p> 323 324 <pre> 325 (gdb) <b>info goroutines</b> 326 1 waiting runtime.gosched 327 * 13 running runtime.goexit 328 </pre> 329 330 <p> 331 the one marked with the <code>*</code> is the current goroutine. 332 </p> 333 334 <h3 id="Inspecting_the_stack">Inspecting the stack</h3> 335 336 <p> 337 Look at the stack trace for where we’ve paused the program: 338 </p> 339 340 <pre> 341 (gdb) <b>bt</b> <i># backtrace</i> 342 #0 regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/regexp/find_test.go:148 343 #1 0x000000000042f60b in testing.tRunner (t=0xf8404a89c0, test=0x573720) at /home/user/go/src/testing/testing.go:156 344 #2 0x000000000040df64 in runtime.initdone () at /home/user/go/src/runtime/proc.c:242 345 #3 0x000000f8404a89c0 in ?? () 346 #4 0x0000000000573720 in ?? () 347 #5 0x0000000000000000 in ?? () 348 </pre> 349 350 <p> 351 The other goroutine, number 1, is stuck in <code>runtime.gosched</code>, blocked on a channel receive: 352 </p> 353 354 <pre> 355 (gdb) <b>goroutine 1 bt</b> 356 #0 0x000000000040facb in runtime.gosched () at /home/user/go/src/runtime/proc.c:873 357 #1 0x00000000004031c9 in runtime.chanrecv (c=void, ep=void, selected=void, received=void) 358 at /home/user/go/src/runtime/chan.c:342 359 #2 0x0000000000403299 in runtime.chanrecv1 (t=void, c=void) at/home/user/go/src/runtime/chan.c:423 360 #3 0x000000000043075b in testing.RunTests (matchString={void (struct string, struct string, bool *, error *)} 361 0x7ffff7f9ef60, tests= []testing.InternalTest = {...}) at /home/user/go/src/testing/testing.go:201 362 #4 0x00000000004302b1 in testing.Main (matchString={void (struct string, struct string, bool *, error *)} 363 0x7ffff7f9ef80, tests= []testing.InternalTest = {...}, benchmarks= []testing.InternalBenchmark = {...}) 364 at /home/user/go/src/testing/testing.go:168 365 #5 0x0000000000400dc1 in main.main () at /home/user/go/src/regexp/_testmain.go:98 366 #6 0x00000000004022e7 in runtime.mainstart () at /home/user/go/src/runtime/amd64/asm.s:78 367 #7 0x000000000040ea6f in runtime.initdone () at /home/user/go/src/runtime/proc.c:243 368 #8 0x0000000000000000 in ?? () 369 </pre> 370 371 <p> 372 The stack frame shows we’re currently executing the <code>regexp.TestFind</code> function, as expected. 373 </p> 374 375 <pre> 376 (gdb) <b>info frame</b> 377 Stack level 0, frame at 0x7ffff7f9ff88: 378 rip = 0x425530 in regexp.TestFind (/home/user/go/src/regexp/find_test.go:148); 379 saved rip 0x430233 380 called by frame at 0x7ffff7f9ffa8 381 source language minimal. 382 Arglist at 0x7ffff7f9ff78, args: t=0xf840688b60 383 Locals at 0x7ffff7f9ff78, Previous frame's sp is 0x7ffff7f9ff88 384 Saved registers: 385 rip at 0x7ffff7f9ff80 386 </pre> 387 388 <p> 389 The command <code>info</code> <code>locals</code> lists all variables local to the function and their values, but is a bit 390 dangerous to use, since it will also try to print uninitialized variables. Uninitialized slices may cause gdb to try 391 to print arbitrary large arrays. 392 </p> 393 394 <p> 395 The function’s arguments: 396 </p> 397 398 <pre> 399 (gdb) <b>info args</b> 400 t = 0xf840688b60 401 </pre> 402 403 <p> 404 When printing the argument, notice that it’s a pointer to a 405 <code>Regexp</code> value. Note that GDB has incorrectly put the <code>*</code> 406 on the right-hand side of the type name and made up a 'struct' keyword, in traditional C style. 407 </p> 408 409 <pre> 410 (gdb) <b>p re</b> 411 (gdb) p t 412 $1 = (struct testing.T *) 0xf840688b60 413 (gdb) p t 414 $1 = (struct testing.T *) 0xf840688b60 415 (gdb) p *t 416 $2 = {errors = "", failed = false, ch = 0xf8406f5690} 417 (gdb) p *t->ch 418 $3 = struct hchan<*testing.T> 419 </pre> 420 421 <p> 422 That <code>struct</code> <code>hchan<*testing.T></code> is the 423 runtime-internal representation of a channel. It is currently empty, 424 or gdb would have pretty-printed its contents. 425 </p> 426 427 <p> 428 Stepping forward: 429 </p> 430 431 <pre> 432 (gdb) <b>n</b> <i># execute next line</i> 433 149 for _, test := range findTests { 434 (gdb) <i># enter is repeat</i> 435 150 re := MustCompile(test.pat) 436 (gdb) <b>p test.pat</b> 437 $4 = "" 438 (gdb) <b>p re</b> 439 $5 = (struct regexp.Regexp *) 0xf84068d070 440 (gdb) <b>p *re</b> 441 $6 = {expr = "", prog = 0xf840688b80, prefix = "", prefixBytes = []uint8, prefixComplete = true, 442 prefixRune = 0, cond = 0 '\000', numSubexp = 0, longest = false, mu = {state = 0, sema = 0}, 443 machine = []*regexp.machine} 444 (gdb) <b>p *re->prog</b> 445 $7 = {Inst = []regexp/syntax.Inst = {{Op = 5 '\005', Out = 0, Arg = 0, Rune = []int}, {Op = 446 6 '\006', Out = 2, Arg = 0, Rune = []int}, {Op = 4 '\004', Out = 0, Arg = 0, Rune = []int}}, 447 Start = 1, NumCap = 2} 448 </pre> 449 450 451 <p> 452 We can step into the <code>String</code>function call with <code>"s"</code>: 453 </p> 454 455 <pre> 456 (gdb) <b>s</b> 457 regexp.(*Regexp).String (re=0xf84068d070, noname=void) at /home/user/go/src/regexp/regexp.go:97 458 97 func (re *Regexp) String() string { 459 </pre> 460 461 <p> 462 Get a stack trace to see where we are: 463 </p> 464 465 <pre> 466 (gdb) <b>bt</b> 467 #0 regexp.(*Regexp).String (re=0xf84068d070, noname=void) 468 at /home/user/go/src/regexp/regexp.go:97 469 #1 0x0000000000425615 in regexp.TestFind (t=0xf840688b60) 470 at /home/user/go/src/regexp/find_test.go:151 471 #2 0x0000000000430233 in testing.tRunner (t=0xf840688b60, test=0x5747b8) 472 at /home/user/go/src/testing/testing.go:156 473 #3 0x000000000040ea6f in runtime.initdone () at /home/user/go/src/runtime/proc.c:243 474 .... 475 </pre> 476 477 <p> 478 Look at the source code: 479 </p> 480 481 <pre> 482 (gdb) <b>l</b> 483 92 mu sync.Mutex 484 93 machine []*machine 485 94 } 486 95 487 96 // String returns the source text used to compile the regular expression. 488 97 func (re *Regexp) String() string { 489 98 return re.expr 490 99 } 491 100 492 101 // Compile parses a regular expression and returns, if successful, 493 </pre> 494 495 <h3 id="Pretty_Printing">Pretty Printing</h3> 496 497 <p> 498 GDB's pretty printing mechanism is triggered by regexp matches on type names. An example for slices: 499 </p> 500 501 <pre> 502 (gdb) <b>p utf</b> 503 $22 = []uint8 = {0 '\000', 0 '\000', 0 '\000', 0 '\000'} 504 </pre> 505 506 <p> 507 Since slices, arrays and strings are not C pointers, GDB can't interpret the subscripting operation for you, but 508 you can look inside the runtime representation to do that (tab completion helps here): 509 </p> 510 <pre> 511 512 (gdb) <b>p slc</b> 513 $11 = []int = {0, 0} 514 (gdb) <b>p slc-></b><i><TAB></i> 515 array slc len 516 (gdb) <b>p slc->array</b> 517 $12 = (int *) 0xf84057af00 518 (gdb) <b>p slc->array[1]</b> 519 $13 = 0</pre> 520 521 522 523 <p> 524 The extension functions $len and $cap work on strings, arrays and slices: 525 </p> 526 527 <pre> 528 (gdb) <b>p $len(utf)</b> 529 $23 = 4 530 (gdb) <b>p $cap(utf)</b> 531 $24 = 4 532 </pre> 533 534 <p> 535 Channels and maps are 'reference' types, which gdb shows as pointers to C++-like types <code>hash<int,string>*</code>. Dereferencing will trigger prettyprinting 536 </p> 537 538 <p> 539 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.) 540 </p> 541 542 <pre> 543 (gdb) <b>p i</b> 544 $4 = {str = "cbb"} 545 (gdb) <b>whatis i</b> 546 type = regexp.input 547 (gdb) <b>p $dtype(i)</b> 548 $26 = (struct regexp.inputBytes *) 0xf8400b4930 549 (gdb) <b>iface i</b> 550 regexp.input: struct regexp.inputBytes * 551 </pre>