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