/doc/go_debug_with_gdb_en_zh.html
HTML | 464 lines | 367 code | 87 blank | 10 comment | 0 complexity | 7d9e376ca140dcc57feed6415b3d3bc5 MD5 | raw file
- <!DOCTYPE html>
- <!-- saved from url=(0049)http://tip.golang.org/doc/debugging_with_gdb.html -->
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Debugging Go Code with GDB - The Go Programming Language</title>
- </head>
- <body>
- </div></div>
- <div id="page">
- <div id="content">
- <h1>??GDB??GO??</h1>
- <p>
- <h2>??</h2>
- <p>
- <pre>
- ???<a href='http://tip.golang.org/doc/debugging_with_gdb.html'>http://tip.golang.org/doc/debugging_with_gdb.html</a>
- ???<a href='#'>saber</a> <a href='mailto:wenjie.0617@gmail.com'>(wenjie.0617@gmail.com)</a>
- </pre>
- <p>
- <p><i>
- ???????6g???. Gccgo?gdb????. ??????????
- <a href="http://sourceware.org/gdb/current/onlinedocs/gdb/">GDB ??</a>.
- </i></p>
- <h2 id="Introduction">??</h2>
- <p>
- ???Linux, Mac OSX ? FreeBSD???6g/6l??8g/8l????????Go???,
- ??DWARFv3?????????????????(>7.1)?GDB????????????
- ???????????
- </p>
- <p>
- ?? <code>'-s'</code> ??????????????
- </p>
- <h3 id="Common_Operations">????</h3>
- <ul>
- <li>
- ????,????????
- <pre>(gdb) <b>list</b>
- (gdb) <b>list <i>line</i></b>
- (gdb) <b>list <i>file.go</i>:<i>line</i></b>
- (gdb) <b>break <i>line</i></b>
- (gdb) <b>break <i>file.go</i>:<i>line</i></b>
- (gdb) <b>disas</b></pre>
- </li>
- <li>
- ???????
- <pre>(gdb) <b>bt</b>
- (gdb) <b>frame <i>n</i></b></pre>
- </li>
- <li>
- ??????,?????????????,??????
- <pre>(gdb) <b>info locals</b>
- (gdb) <b>info args</b>
- (gdb) <b>p variable</b>
- (gdb) <b>whatis variable</b></pre>
- </li>
- <li>
- ?????????,?????
- <pre>(gdb) <b>info variables <i>regexp</i></b></pre>
- </li>
- </ul>
- <h3 id="Go_Extensions">Go ??</h3>
- <p>
- GDB????????????????????????. ???????????????????(?? goroutines),
- ?????????map, slice ? channel????.
- </p>
- <ul>
- <li>
- ??string, slice, map, channel?interface:
- <pre>(gdb) <b>p <i>var</i></b></pre>
- </li>
- <li>
- strings, slices?maps?$len(), $cap()??
- <pre>(gdb) <b>p $len(<i>var</i>)</b></pre>
- </li>
- <li>
- ??????????:
- <pre>(gdb) <b>p $dtype(<i>var</i>)</b>
- (gdb) <b>iface <i>var</i></b></pre>
- <p class="detail"><b>????:</b>
- GDB????????????????,????????????????.(?????????,?????????????)
- </p>
- </li>
- <li>
- ?? goroutines:
- <pre>(gdb) <b>info goroutines</b>
- (gdb) <b>goroutine <i>n</i> <i>cmd</i></b>
- (gdb) <b>help goroutine</b></pre>
- For example:
- <pre>(gdb) <b>goroutine 12 bt</b></pre>
- </li>
- </ul>
- <p>
- ????????????????????????Go??????<a href="http://tip.golang.org/src/pkg/runtime/runtime-gdb.py">src/pkg/runtime/runtime-gdb.py</a>.???????????
- (<code>hash<T,U></code>) ??? (<code>runtime.m</code> and
- <code>runtime.g</code>), ???????(<a href="http://tip.golang.org/src/cmd/ld/dwarf.c">src/cmd/ld/dwarf.c</a>)?DWARF???????.
- ?????????????,?? '<code>objdump -W 6.out</code>' ?? <code>.debug_*</code> ??
- </p>
- <h3 id="Known_Issues">????</h3>
- <ol>
- <li>
- ??????????????,??????????.
- </li>
- <li>
- C???????????????.
- </li>
- <li>
- GDB???GO???????, "fmt.Print"??????? <code>"."</code> ??????????. ??????<code>pkg.(*MyType).Meth</code>??????.
- </li><li>
- ??????????? <code>"main"</code> ??.</li>
- </ol>
- <h2 id="Tutorial">????</h2>
- <p>
- ??????, ?????<a href="http://tip.golang.org/pkg/regexp/">regexp</a> ????????????. ????????, ???? <code>$GOROOT/src/pkg/regexp</code>
- ??? <code>gotest</code>. ?????????? <code>6.out</code> ??????.
- </p>
- <h3 id="Getting_Started">??</h3>
- <p>
- ??GDB, ?? <code>6.out</code>:
- </p>
- <pre>$ <b>gdb 6.out</b>
- GNU gdb (GDB) 7.2-gg8
- Copyright (C) 2010 Free Software Foundation, Inc.
- License GPLv 3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
- Type "show copying" and "show warranty" for licensing/warranty details.
- This GDB was configured as "x86_64-linux".
- Reading symbols from /home/user/go/src/pkg/regexp/6.out...
- done.
- Loading Go Runtime support.
- (gdb)
- </pre>
- <p>
- <code>"Loading Go Runtime support"</code> ????GDB? <code>$GOROOT/src/pkg/runtime/runtime-gdb.py</code> ???GO??????.
- </p>
- <p>
- ????GDB????Go???????????, ??<code>'-d'</code> <code>$GOROOT</code>
- </p>
- <pre>$ <b>gdb 6.out -d $GOROOT</b>
- </pre>
- <p>
- ????????GDB????????????, ???????(????go????? <code>~/go/</code> ):
- </p><p>
- </p><pre>(gdb) <b>source ~/go/src/pkg/runtime/runtime-gdb.py</b>
- ??Go?????.
- </pre>
- <h3 id="Inspecting_the_source">????</h3>
- <p>
- ?? <code>"l"</code> ?? <code>"list"</code> ???????
- </p>
- <pre>(gdb) <b>l</b>
- </pre>
- <p>
- ?? <code>"list"</code> ???????????(????????).
- </p>
- <pre>(gdb) <b>l main.main</b>
- </pre>
- <p>
- ????????????:
- </p>
- <pre>(gdb) <b>l regexp.go:1</b>
- (gdb) <i># Hit enter to repeat last command. Here, this lists next 10 lines.</i>
- </pre>
- <h3 id="Naming">??</h3>
- <p>
- ?????????????????. <code>regexp</code> ??? <code>Compile</code> ???GDB??? <code>'regexp.Compile'</code>.
- </p>
- <p>
- ????????????. ??, <code>*Regexp</code> ??? <code>doParse</code> ?????? <code>'regexp.*Regexp.doParse'</code>. (?????????"middot,", ??Go???????.)
- </p>
- <p>
- ??????, ???????????????????. ???????????? '&' ?????.
- </p>
- <h3 id="Setting_breakpoints">????</h3>
- <p>
- ? <code>TestFind</code> ???????
- </p>
- <pre>(gdb) <b>b 'regexp.TestFind'</b>
- Breakpoint 1 at 0x424908: file /home/user/go/src/pkg/regexp/find_test.go, line 148.
- </pre>
- <p>
- Run the program:
- </p>
- <pre>(gdb) <b>run</b>
- Starting program: /home/lvd/g/src/pkg/regexp/6.out
- Breakpoint 1, regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/pkg/regexp/find_test.go:148
- 148 func TestFind(t *testing.T) {
- </pre>
- <p>
- ??????????. ????????goroutines:
- </p>
- <pre>(gdb) <b>info goroutines</b>
- 1 waiting runtime.gosched
- * 13 running runtime.goexit
- </pre>
- <p>
- the one marked with the <code>*</code> is the current goroutine.
- </p>
- <h3 id="Inspecting_the_stack">Inspecting the stack</h3>
- <p>
- ?????????????:
- </p>
- <pre>(gdb) <b>bt</b> <i># backtrace</i>
- #0 regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/pkg/regexp/find_test.go:148
- #1 0x000000000042f60b in testing.tRunner (t=0xf8404a89c0, test=0x573720) at /home/user/go/src/pkg/testing/testing.go:156
- #2 0x000000000040df64 in runtime.initdone () at /home/user/go/src/pkg/runtime/proc.c:242
- #3 0x000000f8404a89c0 in ?? ()
- #4 0x0000000000573720 in ?? ()
- #5 0x0000000000000000 in ?? ()
- </pre>
- <p>
- The other goroutine, number 1, is stuck in <code>runtime.gosched</code>, blocked on a channel receive:
- </p>
- <pre>(gdb) <b>goroutine 1 bt</b>
- #0 0x000000000040facb in runtime.gosched () at /home/lvd/g/src/pkg/runtime/proc.c:873
- #1 0x00000000004031c9 in runtime.chanrecv (c=void, ep=void, selected=void, received=void)
- at /home/lvd/g/src/pkg/runtime/chan.c:342
- #2 0x0000000000403299 in runtime.chanrecv1 (t=void, c=void) at/home/lvd/g/src/pkg/runtime/chan.c:423
- #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
- #4 0x00000000004302b1 in testing.Main (matchString={void (struct string, struct string, bool *, error *)} 0x7ffff7f9ef80, tests= []testing.InternalTest = {...}, benchmarks= []testing.InternalBenchmark = {...})
- at /home/lvd/g/src/pkg/testing/testing.go:168
- #5 0x0000000000400dc1 in main.main () at /home/lvd/g/src/pkg/regexp/_testmain.go:98
- #6 0x00000000004022e7 in runtime.mainstart () at /home/lvd/g/src/pkg/runtime/amd64/asm.s:78
- #7 0x000000000040ea6f in runtime.initdone () at /home/lvd/g/src/pkg/runtime/proc.c:243
- #8 0x0000000000000000 in ?? ()
- </pre>
- <p>
- ?????????????? <code>regexp.TestFind</code> ??, ??????.
- </p>
- <pre>(gdb) <b>info frame</b>
- Stack level 0, frame at 0x7ffff7f9ff88:
- rip = 0x425530 in regexp.TestFind (/home/lvd/g/src/pkg/regexp/find_test.go:148);
- saved rip 0x430233
- called by frame at 0x7ffff7f9ffa8
- source language minimal.
- Arglist at 0x7ffff7f9ff78, args: t=0xf840688b60
- Locals at 0x7ffff7f9ff78, Previous frame's sp is 0x7ffff7f9ff88
- Saved registers:
- rip at 0x7ffff7f9ff80
- </pre>
- <p>
- <code>info locals</code> ??????????????, ???????, ???????????????. ????????????gdb?????????.
- </p>
- <p>
- ????:
- </p>
- <pre>(gdb) <b>info args</b>
- t = 0xf840688b60
- </pre>
- <p>
- ??????, ?? <code>Regexp</code> ???. GDB????? <code>*</code> ????????, ?????C???'struct'???.
- </p>
- <pre>(gdb) <b>p re</b>
- (gdb) p t
- $1 = (struct testing.T *) 0xf840688b60
- (gdb) p t
- $1 = (struct testing.T *) 0xf840688b60
- (gdb) p *t
- $2 = {errors = "", failed = false, ch = 0xf8406f5690}
- (gdb) p *t->ch
- $3 = struct hchan<*testing.T>
- </pre>
- <p>
- <code>struct hchan<*testing.T></code> ?channel?????????. ????, gdb?????????.
- </p>
- <p>
- ????:
- </p>
- <pre>(gdb) <b>n</b> <i># execute next line</i>
- 149 for _, test := range findTests {
- (gdb) <i># enter is repeat</i>
- 150 re := MustCompile(test.pat)
- (gdb) <b>p test.pat</b>
- $4 = ""
- (gdb) <b>p re</b>
- $5 = (struct regexp.Regexp *) 0xf84068d070
- (gdb) <b>p *re</b>
- $6 = {expr = "", prog = 0xf840688b80, prefix = "", prefixBytes = []uint8, prefixComplete = true,
- prefixRune = 0, cond = 0 '\000', numSubexp = 0, longest = false, mu = {state = 0, sema = 0},
- machine = []*regexp.machine}
- (gdb) <b>p *re->prog</b>
- $7 = {Inst = []regexp/syntax.Inst = {{Op = 5 '\005', Out = 0, Arg = 0, Rune = []int}, {Op =
- 6 '\006', Out = 2, Arg = 0, Rune = []int}, {Op = 4 '\004', Out = 0, Arg = 0, Rune = []int}},
- Start = 1, NumCap = 2}
- </pre>
- <p>
- ?????? <code>"s"</code> ??? <code>String</code> ??:
- </p>
- <pre>(gdb) <b>s</b>
- regexp.(*Regexp).String (re=0xf84068d070, noname=void) at /home/lvd/g/src/pkg/regexp/regexp.go:97
- 97 func (re *Regexp) String() string {
- </pre>
- <p>
- ??????,???????:
- </p>
- <pre>(gdb) <b>bt</b>
- (gdb) bt
- #0 regexp.(*Regexp).String (re=0xf84068d070, noname=void)
- at /home/lvd/g/src/pkg/regexp/regexp.go:97
- #1 0x0000000000425615 in regexp.TestFind (t=0xf840688b60)
- at /home/lvd/g/src/pkg/regexp/find_test.go:151
- #2 0x0000000000430233 in testing.tRunner (t=0xf840688b60, test=0x5747b8)
- at /home/lvd/g/src/pkg/testing/testing.go:156
- #3 0x000000000040ea6f in runtime.initdone () at /home/lvd/g/src/pkg/runtime/proc.c:243
- ....
- </pre>
- <p>
- ?????:
- </p>
- <pre>(gdb) <b>l</b>
- 92 mu sync.Mutex
- 93 machine []*machine
- 94 }
- 95
- 96 // String returns the source text used to compile the regular expression.
- 97 func (re *Regexp) String() string {
- 98 return re.expr
- 99 }
- 100
- 101 // Compile parses a regular expression and returns, if successful,
- </pre>
- <h3 id="Pretty_Printing">??</h3>
- <p>
- GDB??????regexp???????. ?slice??:
- </p>
- <pre>(gdb) <b>p utf</b>
- $22 = []uint8 = {0 '\000', 0 '\000', 0 '\000', 0 '\000'}
- </pre>
- <p>
- ??sinces, arrays, strings??C??, GDB????????, ??????????????:
- </p>
- <pre>
- (gdb) <b>p slc</b>
- $11 = []int = {0, 0}
- (gdb) <b>p slc-></b><i><TAB></i>
- array slc len
- (gdb) <b>p slc->array</b>
- $12 = (int *) 0xf84057af00
- (gdb) <b>p slc->array[1]</b>
- $13 = 0</pre>
- <p>
- strings, arrays, slices?$len, #cap???
- </p>
- <pre>(gdb) <b>p $len(utf)</b>
- $23 = 4
- (gdb) <b>p $cap(utf)</b>
- $24 = 4
- </pre>
- <p>
- channels ? maps??????, gdb??????C++ <code>hash<int,string>*</code> ?????. ????????.
- </p>
- <p>
- ??????????????????. ????????Go GDB????????????. <code>$dtype</code> ???????????(<code>regexp.go</code> ? 293??????).
- </p>
- <pre>(gdb) <b>p i</b>
- $4 = {str = "cbb"}
- (gdb) <b>whatis i</b>
- type = regexp.input
- (gdb) <b>p $dtype(i)</b>
- $26 = (struct regexp.inputBytes *) 0xf8400b4930
- (gdb) <b>iface i</b>
- regexp.input: struct regexp.inputBytes *
- </pre>
- </div>
- </div>
- <div id="copyright">
- Build version weekly.2012-02-14 +a9e12ba1c6ea.
- Except as noted, this content is licensed under a
- Creative Commons Attribution 3.0 License.
- </div>
- <!--
- <script type="text/javascript">
- (function() {
- var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
- po.src = 'https://apis.google.com/js/plusone.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
- })();
- </script>
- -->
- </body></html>