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