debugging_with_gdb.html 17 KB

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