blob: 19d36f7d97bfa18f38a3030669aad9f8ac5adbcd [file] [log] [blame]
Andrew Gerrand7cb21a72012-01-19 11:24:54 +11001<!--{
Andrew Gerranda22b0f82012-03-05 15:30:27 +11002 "Title": "Debugging Go Code with GDB",
Andrew Gerrand98155bd2012-03-27 11:42:01 +11003 "Path": "/doc/gdb"
Andrew Gerrand7cb21a72012-01-19 11:24:54 +11004}-->
Luuk van Dijk3e268622011-10-05 10:49:23 -07005
Russ Cox6be622c2018-01-09 15:32:22 -05006<!--
7NOTE: In this document and others in this directory, the convention is to
8set fixed-width phrases with non-fixed-width spaces, as in
9<code>hello</code> <code>world</code>.
10Do not send CLs removing the interior tags from such phrases.
11-->
12
Hana Kimacc1ec92017-12-19 15:21:05 -050013<i>
14<p>
15The following instructions apply to the standard toolchain
16(the <code>gc</code> Go compiler and tools).
17Gccgo has native gdb support.
18</p>
19<p>
20Note that
21<a href="https://github.com/derekparker/delve">Delve</a> is a better
22alternative to GDB when debugging Go programs built with the standard
Russ Coxb6c871a2018-01-09 15:26:21 -050023toolchain. It understands the Go runtime, data structures, and
Hana Kimacc1ec92017-12-19 15:21:05 -050024expressions better than GDB. Delve currently supports Linux, OSX,
25and Windows on <code>amd64</code>.
26For 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>
Luuk van Dijk3e268622011-10-05 10:49:23 -070031
Rob Pike2674efb2014-04-26 10:18:17 -060032<p>
33GDB does not understand Go programs well.
34The stack management, threading, and runtime contain aspects that differ
35enough from the execution model GDB expects that they can confuse
Hana Kimacc1ec92017-12-19 15:21:05 -050036the debugger and cause incorrect results even when the program is
37compiled with gccgo.
38As a consequence, although GDB can be useful in some situations (e.g.,
39debugging Cgo code, or debugging the runtime itself), it is not
40a reliable debugger for Go programs, particularly heavily concurrent
41ones. Moreover, it is not a priority for the Go project to address
42these issues, which are difficult.
Rob Pike2674efb2014-04-26 10:18:17 -060043</p>
44
45<p>
Hana Kimacc1ec92017-12-19 15:21:05 -050046In short, the instructions below should be taken only as a guide to how
47to use GDB when it works, not as a guarantee of success.
48
49Besides 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>
Rob Pike2674efb2014-04-26 10:18:17 -060054</p>
55
Luuk van Dijk3e268622011-10-05 10:49:23 -070056<h2 id="Introduction">Introduction</h2>
57
58<p>
Andrew Gerrand2a5879d2012-03-20 13:50:05 +110059When you compile and link your Go programs with the <code>gc</code> toolchain
Hana Kim0202aa82017-09-06 16:17:11 -040060on Linux, Mac OS X, FreeBSD or NetBSD, the resulting binaries contain DWARFv4
Hana Kim01c979d2017-11-14 13:17:44 -050061debugging information that recent versions (&ge;7.5) of the GDB debugger can
Luuk van Dijk3e268622011-10-05 10:49:23 -070062use to inspect a live process or a core dump.
63</p>
64
65<p>
Shenghou Mac91c5642014-04-16 01:19:26 -040066Pass the <code>'-w'</code> flag to the linker to omit the debug information
Russ Cox6be622c2018-01-09 15:32:22 -050067(for example, <code>go</code> <code>build</code> <code>-ldflags=-w</code> <code>prog.go</code>).
Luuk van Dijk3e268622011-10-05 10:49:23 -070068</p>
69
Shenghou Mad9c4cef2012-07-03 12:50:03 -040070<p>
71The code generated by the <code>gc</code> compiler includes inlining of
72function invocations and registerization of variables. These optimizations
Russ Coxee346172018-01-09 15:31:45 -050073can sometimes make debugging with <code>gdb</code> harder.
74If you find that you need to disable these optimizations,
75build your program using <code>go</code> <code>build</code> <code>-gcflags=all="-N -l"</code>.
Shenghou Mad9c4cef2012-07-03 12:50:03 -040076</p>
Luuk van Dijk3e268622011-10-05 10:49:23 -070077
Alberto Donizetti4601eae2017-01-09 19:11:58 +010078<p>
79If you want to use gdb to inspect a core dump, you can trigger a dump
80on 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
83documentation</a> for more info).
84</p>
85
Luuk van Dijk3e268622011-10-05 10:49:23 -070086<h3 id="Common_Operations">Common Operations</h3>
87
88<ul>
89<li>
Robert Henckeb0c34292012-10-07 11:24:14 +110090Show file and line number for code, set breakpoints and disassemble:
Luuk van Dijk3e268622011-10-05 10:49:23 -070091<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>
Shenghou Ma9dbfda52012-03-21 00:42:53 +080099Show backtraces and unwind stack frames:
Luuk van Dijk3e268622011-10-05 10:49:23 -0700100<pre>(gdb) <b>bt</b>
101(gdb) <b>frame <i>n</i></b></pre>
102</li>
103<li>
104Show the name, type and location on the stack frame of local variables,
105arguments 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>
112Show 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>
121A recent extension mechanism to GDB allows it to load extension scripts for a
Russ Coxb6c871a2018-01-09 15:26:21 -0500122given binary. The toolchain uses this to extend GDB with a handful of
Luuk van Dijk3e268622011-10-05 10:49:23 -0700123commands to inspect internals of the runtime code (such as goroutines) and to
124pretty print the built-in map, slice and channel types.
125</p>
126
127<ul>
128<li>
129Pretty printing a string, slice, map, channel or interface:
130<pre>(gdb) <b>p <i>var</i></b></pre>
131</li>
132<li>
133A $len() and $cap() function for strings, slices and maps:
134<pre>(gdb) <b>p $len(<i>var</i>)</b></pre>
135</li>
136<li>
137A 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
141type 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
143the short type name and a pointer).</p>
144</li>
145<li>
146Inspecting 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>
150For example:
151<pre>(gdb) <b>goroutine 12 bt</b></pre>
152</li>
153</ul>
154
155<p>
156If you'd like to see how this works, or want to extend it, take a look at <a
Russ Cox220a6de2014-09-08 00:06:45 -0400157href="/src/runtime/runtime-gdb.py">src/runtime/runtime-gdb.py</a> in
Luuk van Dijk3e268622011-10-05 10:49:23 -0700158the Go source distribution. It depends on some special magic types
159(<code>hash&lt;T,U&gt;</code>) and variables (<code>runtime.m</code> and
160<code>runtime.g</code>) that the linker
Russ Cox6e28bf32016-01-06 15:26:45 -0500161(<a href="/src/cmd/link/internal/ld/dwarf.go">src/cmd/link/internal/ld/dwarf.go</a>) ensures are described in
Luuk van Dijk3e268622011-10-05 10:49:23 -0700162the DWARF code.
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800163</p>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700164
165<p>
166If you're interested in what the debugging information looks like, run
Russ Cox6be622c2018-01-09 15:32:22 -0500167<code>objdump</code> <code>-W</code> <code>a.out</code> and browse through the <code>.debug_*</code>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700168sections.
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
176from 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>
180that needs to be quoted. It objects even more strongly to method names of
181the 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>
188In this tutorial we will inspect the binary of the
189<a href="/pkg/regexp/">regexp</a> package's unit tests. To build the binary,
Russ Cox6be622c2018-01-09 15:32:22 -0500190change to <code>$GOROOT/src/regexp</code> and run <code>go</code> <code>test</code> <code>-c</code>.
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800191This should produce an executable file named <code>regexp.test</code>.
Luuk van Dijk3e268622011-10-05 10:49:23 -0700192</p>
193
194
195<h3 id="Getting_Started">Getting Started</h3>
196
197<p>
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800198Launch GDB, debugging <code>regexp.test</code>:
Luuk van Dijk3e268622011-10-05 10:49:23 -0700199</p>
200
201<pre>
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800202$ <b>gdb regexp.test</b>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700203GNU gdb (GDB) 7.2-gg8
204Copyright (C) 2010 Free Software Foundation, Inc.
205License GPLv 3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
206Type "show copying" and "show warranty" for licensing/warranty details.
207This GDB was configured as "x86_64-linux".
208
Russ Cox220a6de2014-09-08 00:06:45 -0400209Reading symbols from /home/user/go/src/regexp/regexp.test...
Luuk van Dijk3e268622011-10-05 10:49:23 -0700210done.
211Loading Go Runtime support.
212(gdb)
213</pre>
214
215<p>
Russ Cox6be622c2018-01-09 15:32:22 -0500216The message "Loading Go Runtime support" means that GDB loaded the
Russ Cox220a6de2014-09-08 00:06:45 -0400217extension from <code>$GOROOT/src/runtime/runtime-gdb.py</code>.
Luuk van Dijk3e268622011-10-05 10:49:23 -0700218</p>
219
220<p>
221To help GDB find the Go runtime sources and the accompanying support script,
222pass your <code>$GOROOT</code> with the <code>'-d'</code> flag:
223</p>
224
225<pre>
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800226$ <b>gdb regexp.test -d $GOROOT</b>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700227</pre>
228
229<p>
230If for some reason GDB still can't find that directory or that script, you can load
231it by hand by telling gdb (assuming you have the go sources in
232<code>~/go/</code>):
Rob Pike7ae41e82013-02-28 13:32:36 -0800233</p>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700234
235<pre>
Russ Cox220a6de2014-09-08 00:06:45 -0400236(gdb) <b>source ~/go/src/runtime/runtime-gdb.py</b>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700237Loading Go Runtime support.
238</pre>
239
240<h3 id="Inspecting_the_source">Inspecting the source</h3>
241
242<p>
243Use 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>
251List a specific part of the source parametrizing <code>"list"</code> with a
252function 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>
260List 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>
272Variable and function names must be qualified with the name of the packages
273they belong to. The <code>Compile</code> function from the <code>regexp</code>
274package is known to GDB as <code>'regexp.Compile'</code>.
275</p>
276
277<p>
278Methods must be qualified with the name of their receiver types. For example,
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800279the <code>*Regexp</code> type’s <code>String</code> method is known as
280<code>'regexp.(*Regexp).String'</code>.
Luuk van Dijk3e268622011-10-05 10:49:23 -0700281</p>
282
283<p>
284Variables that shadow other variables are magically suffixed with a number in the debug info.
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800285Variables referenced by closures will appear as pointers magically prefixed with '&amp;'.
Luuk van Dijk3e268622011-10-05 10:49:23 -0700286</p>
287
288<h3 id="Setting_breakpoints">Setting breakpoints</h3>
289
290<p>
291Set a breakpoint at the <code>TestFind</code> function:
292</p>
293
294<pre>
295(gdb) <b>b 'regexp.TestFind'</b>
Russ Cox220a6de2014-09-08 00:06:45 -0400296Breakpoint 1 at 0x424908: file /home/user/go/src/regexp/find_test.go, line 148.
Luuk van Dijk3e268622011-10-05 10:49:23 -0700297</pre>
298
299<p>
300Run the program:
301</p>
302
303<pre>
304(gdb) <b>run</b>
Russ Cox220a6de2014-09-08 00:06:45 -0400305Starting program: /home/user/go/src/regexp/regexp.test
Luuk van Dijk3e268622011-10-05 10:49:23 -0700306
Russ Cox220a6de2014-09-08 00:06:45 -0400307Breakpoint 1, regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/regexp/find_test.go:148
Luuk van Dijk3e268622011-10-05 10:49:23 -0700308148 func TestFind(t *testing.T) {
309</pre>
310
311<p>
312Execution has paused at the breakpoint.
313See 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>
323the 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>
329Look at the stack trace for where we’ve paused the program:
330</p>
331
332<pre>
333(gdb) <b>bt</b> <i># backtrace</i>
Russ Cox220a6de2014-09-08 00:06:45 -0400334#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
Luuk van Dijk3e268622011-10-05 10:49:23 -0700337#3 0x000000f8404a89c0 in ?? ()
338#4 0x0000000000573720 in ?? ()
339#5 0x0000000000000000 in ?? ()
340</pre>
341
342<p>
343The 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>
Russ Cox220a6de2014-09-08 00:06:45 -0400348#0 0x000000000040facb in runtime.gosched () at /home/user/go/src/runtime/proc.c:873
Luuk van Dijk3e268622011-10-05 10:49:23 -0700349#1 0x00000000004031c9 in runtime.chanrecv (c=void, ep=void, selected=void, received=void)
Russ Cox220a6de2014-09-08 00:06:45 -0400350 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
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800352#3 0x000000000043075b in testing.RunTests (matchString={void (struct string, struct string, bool *, error *)}
Russ Cox220a6de2014-09-08 00:06:45 -0400353 0x7ffff7f9ef60, tests= []testing.InternalTest = {...}) at /home/user/go/src/testing/testing.go:201
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800354#4 0x00000000004302b1 in testing.Main (matchString={void (struct string, struct string, bool *, error *)}
355 0x7ffff7f9ef80, tests= []testing.InternalTest = {...}, benchmarks= []testing.InternalBenchmark = {...})
Russ Cox220a6de2014-09-08 00:06:45 -0400356at /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
Luuk van Dijk3e268622011-10-05 10:49:23 -0700360#8 0x0000000000000000 in ?? ()
361</pre>
362
363<p>
364The 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>
369Stack level 0, frame at 0x7ffff7f9ff88:
Russ Cox220a6de2014-09-08 00:06:45 -0400370 rip = 0x425530 in regexp.TestFind (/home/user/go/src/regexp/find_test.go:148);
Luuk van Dijk3e268622011-10-05 10:49:23 -0700371 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>
Russ Cox6be622c2018-01-09 15:32:22 -0500381The command <code>info</code> <code>locals</code> lists all variables local to the function and their values, but is a bit
Luuk van Dijk3e268622011-10-05 10:49:23 -0700382dangerous to use, since it will also try to print uninitialized variables. Uninitialized slices may cause gdb to try
383to print arbitrary large arrays.
384</p>
385
386<p>
387The function’s arguments:
388</p>
389
390<pre>
391(gdb) <b>info args</b>
392t = 0xf840688b60
393</pre>
394
395<p>
396When 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>
398on 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}
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800409(gdb) p *t-&gt;ch
410$3 = struct hchan&lt;*testing.T&gt;
Luuk van Dijk3e268622011-10-05 10:49:23 -0700411</pre>
412
413<p>
Russ Cox6be622c2018-01-09 15:32:22 -0500414That <code>struct</code> <code>hchan&lt;*testing.T&gt;</code> is the
Alberto Donizetti4601eae2017-01-09 19:11:58 +0100415runtime-internal representation of a channel. It is currently empty,
416or gdb would have pretty-printed its contents.
Luuk van Dijk3e268622011-10-05 10:49:23 -0700417</p>
418
419<p>
420Stepping forward:
421</p>
422
423<pre>
424(gdb) <b>n</b> <i># execute next line</i>
425149 for _, test := range findTests {
426(gdb) <i># enter is repeat</i>
427150 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>
444We can step into the <code>String</code>function call with <code>"s"</code>:
445</p>
446
447<pre>
448(gdb) <b>s</b>
Russ Cox220a6de2014-09-08 00:06:45 -0400449regexp.(*Regexp).String (re=0xf84068d070, noname=void) at /home/user/go/src/regexp/regexp.go:97
Luuk van Dijk3e268622011-10-05 10:49:23 -070045097 func (re *Regexp) String() string {
451</pre>
452
453<p>
454Get a stack trace to see where we are:
455</p>
456
457<pre>
458(gdb) <b>bt</b>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700459#0 regexp.(*Regexp).String (re=0xf84068d070, noname=void)
Russ Cox220a6de2014-09-08 00:06:45 -0400460 at /home/user/go/src/regexp/regexp.go:97
Luuk van Dijk3e268622011-10-05 10:49:23 -0700461#1 0x0000000000425615 in regexp.TestFind (t=0xf840688b60)
Russ Cox220a6de2014-09-08 00:06:45 -0400462 at /home/user/go/src/regexp/find_test.go:151
Luuk van Dijk3e268622011-10-05 10:49:23 -0700463#2 0x0000000000430233 in testing.tRunner (t=0xf840688b60, test=0x5747b8)
Russ Cox220a6de2014-09-08 00:06:45 -0400464 at /home/user/go/src/testing/testing.go:156
465#3 0x000000000040ea6f in runtime.initdone () at /home/user/go/src/runtime/proc.c:243
Luuk van Dijk3e268622011-10-05 10:49:23 -0700466....
467</pre>
468
469<p>
470Look at the source code:
471</p>
472
473<pre>
474(gdb) <b>l</b>
47592 mu sync.Mutex
47693 machine []*machine
47794 }
47895
47996 // String returns the source text used to compile the regular expression.
48097 func (re *Regexp) String() string {
48198 return re.expr
48299 }
483100
484101 // Compile parses a regular expression and returns, if successful,
485</pre>
486
487<h3 id="Pretty_Printing">Pretty Printing</h3>
488
489<p>
490GDB'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>
499Since slices, arrays and strings are not C pointers, GDB can't interpret the subscripting operation for you, but
500you 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}
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800506(gdb) <b>p slc-&gt;</b><i>&lt;TAB&gt;</i>
Luuk van Dijk3e268622011-10-05 10:49:23 -0700507array 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>
516The 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>
Shenghou Ma9dbfda52012-03-21 00:42:53 +0800527Channels 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
Luuk van Dijk3e268622011-10-05 10:49:23 -0700528</p>
529
530<p>
531Interfaces 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>
538type = regexp.input
539(gdb) <b>p $dtype(i)</b>
540$26 = (struct regexp.inputBytes *) 0xf8400b4930
541(gdb) <b>iface i</b>
542regexp.input: struct regexp.inputBytes *
543</pre>