PageRenderTime 88ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/go_debug_with_gdb_en_zh.html

http://golang-china.googlecode.com/
HTML | 464 lines | 367 code | 87 blank | 10 comment | 0 complexity | 7d9e376ca140dcc57feed6415b3d3bc5 MD5 | raw file
  1. <!DOCTYPE html>
  2. <!-- saved from url=(0049)http://tip.golang.org/doc/debugging_with_gdb.html -->
  3. <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>Debugging Go Code with GDB - The Go Programming Language</title>
  5. </head>
  6. <body>
  7. </div></div>
  8. <div id="page">
  9. <div id="content">
  10. <h1>??GDB??GO??</h1>
  11. <p>
  12. <h2>??</h2>
  13. <p>
  14. <pre>
  15. ???<a href='http://tip.golang.org/doc/debugging_with_gdb.html'>http://tip.golang.org/doc/debugging_with_gdb.html</a>
  16. ???<a href='#'>saber</a> <a href='mailto:wenjie.0617@gmail.com'>(wenjie.0617@gmail.com)</a>
  17. </pre>
  18. <p>
  19. <p><i>
  20. ???????6g???. Gccgo?gdb????. ??????????
  21. <a href="http://sourceware.org/gdb/current/onlinedocs/gdb/">GDB ??</a>.
  22. </i></p>
  23. <h2 id="Introduction">??</h2>
  24. <p>
  25. ???Linux, Mac OSX ? FreeBSD???6g/6l??8g/8l????????Go???,
  26. ??DWARFv3?????????????????(&gt;7.1)?GDB????????????
  27. ???????????
  28. </p>
  29. <p>
  30. ?? <code>'-s'</code> ??????????????
  31. </p>
  32. <h3 id="Common_Operations">????</h3>
  33. <ul>
  34. <li>
  35. ????,????????
  36. <pre>(gdb) <b>list</b>
  37. (gdb) <b>list <i>line</i></b>
  38. (gdb) <b>list <i>file.go</i>:<i>line</i></b>
  39. (gdb) <b>break <i>line</i></b>
  40. (gdb) <b>break <i>file.go</i>:<i>line</i></b>
  41. (gdb) <b>disas</b></pre>
  42. </li>
  43. <li>
  44. ???????
  45. <pre>(gdb) <b>bt</b>
  46. (gdb) <b>frame <i>n</i></b></pre>
  47. </li>
  48. <li>
  49. ??????,?????????????,??????
  50. <pre>(gdb) <b>info locals</b>
  51. (gdb) <b>info args</b>
  52. (gdb) <b>p variable</b>
  53. (gdb) <b>whatis variable</b></pre>
  54. </li>
  55. <li>
  56. ?????????,?????
  57. <pre>(gdb) <b>info variables <i>regexp</i></b></pre>
  58. </li>
  59. </ul>
  60. <h3 id="Go_Extensions">Go ??</h3>
  61. <p>
  62. GDB????????????????????????. ???????????????????(?? goroutines),
  63. ?????????map, slice ? channel????.
  64. </p>
  65. <ul>
  66. <li>
  67. ??string, slice, map, channel?interface:
  68. <pre>(gdb) <b>p <i>var</i></b></pre>
  69. </li>
  70. <li>
  71. strings, slices?maps?$len(), $cap()??
  72. <pre>(gdb) <b>p $len(<i>var</i>)</b></pre>
  73. </li>
  74. <li>
  75. ??????????:
  76. <pre>(gdb) <b>p $dtype(<i>var</i>)</b>
  77. (gdb) <b>iface <i>var</i></b></pre>
  78. <p class="detail"><b>????:</b>
  79. GDB????????????????,????????????????.(?????????,?????????????)
  80. </p>
  81. </li>
  82. <li>
  83. ?? goroutines:
  84. <pre>(gdb) <b>info goroutines</b>
  85. (gdb) <b>goroutine <i>n</i> <i>cmd</i></b>
  86. (gdb) <b>help goroutine</b></pre>
  87. For example:
  88. <pre>(gdb) <b>goroutine 12 bt</b></pre>
  89. </li>
  90. </ul>
  91. <p>
  92. ????????????????????????Go??????<a href="http://tip.golang.org/src/pkg/runtime/runtime-gdb.py">src/pkg/runtime/runtime-gdb.py</a>.???????????
  93. (<code>hash&lt;T,U&gt;</code>) ??? (<code>runtime.m</code> and
  94. <code>runtime.g</code>), ???????(<a href="http://tip.golang.org/src/cmd/ld/dwarf.c">src/cmd/ld/dwarf.c</a>)?DWARF???????.
  95. ?????????????,?? '<code>objdump -W 6.out</code>' ?? <code>.debug_*</code> ??
  96. </p>
  97. <h3 id="Known_Issues">????</h3>
  98. <ol>
  99. <li>
  100. ??????????????,??????????.
  101. </li>
  102. <li>
  103. C???????????????.
  104. </li>
  105. <li>
  106. GDB???GO???????, "fmt.Print"??????? <code>"."</code> ??????????. ??????<code>pkg.(*MyType).Meth</code>??????.
  107. </li><li>
  108. ??????????? <code>"main"</code> ??.</li>
  109. </ol>
  110. <h2 id="Tutorial">????</h2>
  111. <p>
  112. ??????, ?????<a href="http://tip.golang.org/pkg/regexp/">regexp</a> ????????????. ????????, ???? <code>$GOROOT/src/pkg/regexp</code>
  113. ??? <code>gotest</code>. ?????????? <code>6.out</code> ??????.
  114. </p>
  115. <h3 id="Getting_Started">??</h3>
  116. <p>
  117. ??GDB, ?? <code>6.out</code>:
  118. </p>
  119. <pre>$ <b>gdb 6.out</b>
  120. GNU gdb (GDB) 7.2-gg8
  121. Copyright (C) 2010 Free Software Foundation, Inc.
  122. License GPLv 3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  123. Type "show copying" and "show warranty" for licensing/warranty details.
  124. This GDB was configured as "x86_64-linux".
  125. Reading symbols from /home/user/go/src/pkg/regexp/6.out...
  126. done.
  127. Loading Go Runtime support.
  128. (gdb)
  129. </pre>
  130. <p>
  131. <code>"Loading Go Runtime support"</code> ????GDB? <code>$GOROOT/src/pkg/runtime/runtime-gdb.py</code> ???GO??????.
  132. </p>
  133. <p>
  134. ????GDB????Go???????????, ??<code>'-d'</code> <code>$GOROOT</code>
  135. </p>
  136. <pre>$ <b>gdb 6.out -d $GOROOT</b>
  137. </pre>
  138. <p>
  139. ????????GDB????????????, ???????(????go????? <code>~/go/</code> ):
  140. </p><p>
  141. </p><pre>(gdb) <b>source ~/go/src/pkg/runtime/runtime-gdb.py</b>
  142. ??Go?????.
  143. </pre>
  144. <h3 id="Inspecting_the_source">????</h3>
  145. <p>
  146. ?? <code>"l"</code> ?? <code>"list"</code> ???????
  147. </p>
  148. <pre>(gdb) <b>l</b>
  149. </pre>
  150. <p>
  151. ?? <code>"list"</code> ???????????(????????).
  152. </p>
  153. <pre>(gdb) <b>l main.main</b>
  154. </pre>
  155. <p>
  156. ????????????:
  157. </p>
  158. <pre>(gdb) <b>l regexp.go:1</b>
  159. (gdb) <i># Hit enter to repeat last command. Here, this lists next 10 lines.</i>
  160. </pre>
  161. <h3 id="Naming">??</h3>
  162. <p>
  163. ?????????????????. <code>regexp</code> ??? <code>Compile</code> ???GDB??? <code>'regexp.Compile'</code>.
  164. </p>
  165. <p>
  166. ????????????. ??, <code>*Regexp</code> ??? <code>doParse</code> ?????? <code>'regexp.*Regexp.doParse'</code>. (?????????"middot,", ??Go???????.)
  167. </p>
  168. <p>
  169. ??????, ???????????????????. ???????????? '&amp;' ?????.
  170. </p>
  171. <h3 id="Setting_breakpoints">????</h3>
  172. <p>
  173. ? <code>TestFind</code> ???????
  174. </p>
  175. <pre>(gdb) <b>b 'regexp.TestFind'</b>
  176. Breakpoint 1 at 0x424908: file /home/user/go/src/pkg/regexp/find_test.go, line 148.
  177. </pre>
  178. <p>
  179. Run the program:
  180. </p>
  181. <pre>(gdb) <b>run</b>
  182. Starting program: /home/lvd/g/src/pkg/regexp/6.out
  183. Breakpoint 1, regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/pkg/regexp/find_test.go:148
  184. 148 func TestFind(t *testing.T) {
  185. </pre>
  186. <p>
  187. ??????????. ????????goroutines:
  188. </p>
  189. <pre>(gdb) <b>info goroutines</b>
  190. 1 waiting runtime.gosched
  191. * 13 running runtime.goexit
  192. </pre>
  193. <p>
  194. the one marked with the <code>*</code> is the current goroutine.
  195. </p>
  196. <h3 id="Inspecting_the_stack">Inspecting the stack</h3>
  197. <p>
  198. ?????????????:
  199. </p>
  200. <pre>(gdb) <b>bt</b> <i># backtrace</i>
  201. #0 regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/pkg/regexp/find_test.go:148
  202. #1 0x000000000042f60b in testing.tRunner (t=0xf8404a89c0, test=0x573720) at /home/user/go/src/pkg/testing/testing.go:156
  203. #2 0x000000000040df64 in runtime.initdone () at /home/user/go/src/pkg/runtime/proc.c:242
  204. #3 0x000000f8404a89c0 in ?? ()
  205. #4 0x0000000000573720 in ?? ()
  206. #5 0x0000000000000000 in ?? ()
  207. </pre>
  208. <p>
  209. The other goroutine, number 1, is stuck in <code>runtime.gosched</code>, blocked on a channel receive:
  210. </p>
  211. <pre>(gdb) <b>goroutine 1 bt</b>
  212. #0 0x000000000040facb in runtime.gosched () at /home/lvd/g/src/pkg/runtime/proc.c:873
  213. #1 0x00000000004031c9 in runtime.chanrecv (c=void, ep=void, selected=void, received=void)
  214. at /home/lvd/g/src/pkg/runtime/chan.c:342
  215. #2 0x0000000000403299 in runtime.chanrecv1 (t=void, c=void) at/home/lvd/g/src/pkg/runtime/chan.c:423
  216. #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
  217. #4 0x00000000004302b1 in testing.Main (matchString={void (struct string, struct string, bool *, error *)} 0x7ffff7f9ef80, tests= []testing.InternalTest = {...}, benchmarks= []testing.InternalBenchmark = {...})
  218. at /home/lvd/g/src/pkg/testing/testing.go:168
  219. #5 0x0000000000400dc1 in main.main () at /home/lvd/g/src/pkg/regexp/_testmain.go:98
  220. #6 0x00000000004022e7 in runtime.mainstart () at /home/lvd/g/src/pkg/runtime/amd64/asm.s:78
  221. #7 0x000000000040ea6f in runtime.initdone () at /home/lvd/g/src/pkg/runtime/proc.c:243
  222. #8 0x0000000000000000 in ?? ()
  223. </pre>
  224. <p>
  225. ?????????????? <code>regexp.TestFind</code> ??, ??????.
  226. </p>
  227. <pre>(gdb) <b>info frame</b>
  228. Stack level 0, frame at 0x7ffff7f9ff88:
  229. rip = 0x425530 in regexp.TestFind (/home/lvd/g/src/pkg/regexp/find_test.go:148);
  230. saved rip 0x430233
  231. called by frame at 0x7ffff7f9ffa8
  232. source language minimal.
  233. Arglist at 0x7ffff7f9ff78, args: t=0xf840688b60
  234. Locals at 0x7ffff7f9ff78, Previous frame's sp is 0x7ffff7f9ff88
  235. Saved registers:
  236. rip at 0x7ffff7f9ff80
  237. </pre>
  238. <p>
  239. <code>info locals</code> ??????????????, ???????, ???????????????. ????????????gdb?????????.
  240. </p>
  241. <p>
  242. ????:
  243. </p>
  244. <pre>(gdb) <b>info args</b>
  245. t = 0xf840688b60
  246. </pre>
  247. <p>
  248. ??????, ?? <code>Regexp</code> ???. GDB????? <code>*</code> ????????, ?????C???'struct'???.
  249. </p>
  250. <pre>(gdb) <b>p re</b>
  251. (gdb) p t
  252. $1 = (struct testing.T *) 0xf840688b60
  253. (gdb) p t
  254. $1 = (struct testing.T *) 0xf840688b60
  255. (gdb) p *t
  256. $2 = {errors = "", failed = false, ch = 0xf8406f5690}
  257. (gdb) p *t-&gt;ch
  258. $3 = struct hchan&lt;*testing.T&gt;
  259. </pre>
  260. <p>
  261. <code>struct hchan&lt;*testing.T&gt;</code> ?channel?????????. ????, gdb?????????.
  262. </p>
  263. <p>
  264. ????:
  265. </p>
  266. <pre>(gdb) <b>n</b> <i># execute next line</i>
  267. 149 for _, test := range findTests {
  268. (gdb) <i># enter is repeat</i>
  269. 150 re := MustCompile(test.pat)
  270. (gdb) <b>p test.pat</b>
  271. $4 = ""
  272. (gdb) <b>p re</b>
  273. $5 = (struct regexp.Regexp *) 0xf84068d070
  274. (gdb) <b>p *re</b>
  275. $6 = {expr = "", prog = 0xf840688b80, prefix = "", prefixBytes = []uint8, prefixComplete = true,
  276. prefixRune = 0, cond = 0 '\000', numSubexp = 0, longest = false, mu = {state = 0, sema = 0},
  277. machine = []*regexp.machine}
  278. (gdb) <b>p *re-&gt;prog</b>
  279. $7 = {Inst = []regexp/syntax.Inst = {{Op = 5 '\005', Out = 0, Arg = 0, Rune = []int}, {Op =
  280. 6 '\006', Out = 2, Arg = 0, Rune = []int}, {Op = 4 '\004', Out = 0, Arg = 0, Rune = []int}},
  281. Start = 1, NumCap = 2}
  282. </pre>
  283. <p>
  284. ?????? <code>"s"</code> ??? <code>String</code> ??:
  285. </p>
  286. <pre>(gdb) <b>s</b>
  287. regexp.(*Regexp).String (re=0xf84068d070, noname=void) at /home/lvd/g/src/pkg/regexp/regexp.go:97
  288. 97 func (re *Regexp) String() string {
  289. </pre>
  290. <p>
  291. ??????,???????:
  292. </p>
  293. <pre>(gdb) <b>bt</b>
  294. (gdb) bt
  295. #0 regexp.(*Regexp).String (re=0xf84068d070, noname=void)
  296. at /home/lvd/g/src/pkg/regexp/regexp.go:97
  297. #1 0x0000000000425615 in regexp.TestFind (t=0xf840688b60)
  298. at /home/lvd/g/src/pkg/regexp/find_test.go:151
  299. #2 0x0000000000430233 in testing.tRunner (t=0xf840688b60, test=0x5747b8)
  300. at /home/lvd/g/src/pkg/testing/testing.go:156
  301. #3 0x000000000040ea6f in runtime.initdone () at /home/lvd/g/src/pkg/runtime/proc.c:243
  302. ....
  303. </pre>
  304. <p>
  305. ?????:
  306. </p>
  307. <pre>(gdb) <b>l</b>
  308. 92 mu sync.Mutex
  309. 93 machine []*machine
  310. 94 }
  311. 95
  312. 96 // String returns the source text used to compile the regular expression.
  313. 97 func (re *Regexp) String() string {
  314. 98 return re.expr
  315. 99 }
  316. 100
  317. 101 // Compile parses a regular expression and returns, if successful,
  318. </pre>
  319. <h3 id="Pretty_Printing">??</h3>
  320. <p>
  321. GDB??????regexp???????. ?slice??:
  322. </p>
  323. <pre>(gdb) <b>p utf</b>
  324. $22 = []uint8 = {0 '\000', 0 '\000', 0 '\000', 0 '\000'}
  325. </pre>
  326. <p>
  327. ??sinces, arrays, strings??C??, GDB????????, ??????????????:
  328. </p>
  329. <pre>
  330. (gdb) <b>p slc</b>
  331. $11 = []int = {0, 0}
  332. (gdb) <b>p slc-&gt;</b><i>&lt;TAB&gt;</i>
  333. array slc len
  334. (gdb) <b>p slc-&gt;array</b>
  335. $12 = (int *) 0xf84057af00
  336. (gdb) <b>p slc-&gt;array[1]</b>
  337. $13 = 0</pre>
  338. <p>
  339. strings, arrays, slices?$len, #cap???
  340. </p>
  341. <pre>(gdb) <b>p $len(utf)</b>
  342. $23 = 4
  343. (gdb) <b>p $cap(utf)</b>
  344. $24 = 4
  345. </pre>
  346. <p>
  347. channels ? maps??????, gdb??????C++ <code>hash&lt;int,string&gt;*</code> ?????. ????????.
  348. </p>
  349. <p>
  350. ??????????????????. ????????Go GDB????????????. <code>$dtype</code> ???????????(<code>regexp.go</code> ? 293??????).
  351. </p>
  352. <pre>(gdb) <b>p i</b>
  353. $4 = {str = "cbb"}
  354. (gdb) <b>whatis i</b>
  355. type = regexp.input
  356. (gdb) <b>p $dtype(i)</b>
  357. $26 = (struct regexp.inputBytes *) 0xf8400b4930
  358. (gdb) <b>iface i</b>
  359. regexp.input: struct regexp.inputBytes *
  360. </pre>
  361. </div>
  362. </div>
  363. <div id="copyright">
  364. Build version weekly.2012-02-14 +a9e12ba1c6ea.
  365. Except as noted, this content is licensed under a
  366. Creative Commons Attribution 3.0 License.
  367. </div>
  368. <!--
  369. <script type="text/javascript">
  370. (function() {
  371. var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  372. po.src = 'https://apis.google.com/js/plusone.js';
  373. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  374. })();
  375. </script>
  376. -->
  377. </body></html>