debugging_with_gdb.html 15 KB

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