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