Andrew Gerrand | 7cb21a7 | 2012-01-19 11:24:54 +1100 | [diff] [blame] | 1 | <!--{ |
Andrew Gerrand | a22b0f8 | 2012-03-05 15:30:27 +1100 | [diff] [blame] | 2 | "Title": "Debugging Go Code with GDB", |
Andrew Gerrand | 98155bd | 2012-03-27 11:42:01 +1100 | [diff] [blame] | 3 | "Path": "/doc/gdb" |
Andrew Gerrand | 7cb21a7 | 2012-01-19 11:24:54 +1100 | [diff] [blame] | 4 | }--> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 5 | |
Russ Cox | 6be622c | 2018-01-09 15:32:22 -0500 | [diff] [blame] | 6 | <!-- |
| 7 | NOTE: In this document and others in this directory, the convention is to |
| 8 | set fixed-width phrases with non-fixed-width spaces, as in |
| 9 | <code>hello</code> <code>world</code>. |
| 10 | Do not send CLs removing the interior tags from such phrases. |
| 11 | --> |
| 12 | |
Hana Kim | acc1ec9 | 2017-12-19 15:21:05 -0500 | [diff] [blame] | 13 | <i> |
| 14 | <p> |
| 15 | The following instructions apply to the standard toolchain |
| 16 | (the <code>gc</code> Go compiler and tools). |
| 17 | Gccgo has native gdb support. |
| 18 | </p> |
| 19 | <p> |
| 20 | Note that |
| 21 | <a href="https://github.com/derekparker/delve">Delve</a> is a better |
| 22 | alternative to GDB when debugging Go programs built with the standard |
Russ Cox | b6c871a | 2018-01-09 15:26:21 -0500 | [diff] [blame] | 23 | toolchain. It understands the Go runtime, data structures, and |
Hana Kim | acc1ec9 | 2017-12-19 15:21:05 -0500 | [diff] [blame] | 24 | expressions better than GDB. Delve currently supports Linux, OSX, |
| 25 | and Windows on <code>amd64</code>. |
| 26 | For 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 Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 31 | |
Rob Pike | 2674efb | 2014-04-26 10:18:17 -0600 | [diff] [blame] | 32 | <p> |
| 33 | GDB does not understand Go programs well. |
| 34 | The stack management, threading, and runtime contain aspects that differ |
| 35 | enough from the execution model GDB expects that they can confuse |
Hana Kim | acc1ec9 | 2017-12-19 15:21:05 -0500 | [diff] [blame] | 36 | the debugger and cause incorrect results even when the program is |
| 37 | compiled with gccgo. |
| 38 | As a consequence, although GDB can be useful in some situations (e.g., |
| 39 | debugging Cgo code, or debugging the runtime itself), it is not |
| 40 | a reliable debugger for Go programs, particularly heavily concurrent |
| 41 | ones. Moreover, it is not a priority for the Go project to address |
| 42 | these issues, which are difficult. |
Rob Pike | 2674efb | 2014-04-26 10:18:17 -0600 | [diff] [blame] | 43 | </p> |
| 44 | |
| 45 | <p> |
Hana Kim | acc1ec9 | 2017-12-19 15:21:05 -0500 | [diff] [blame] | 46 | In short, the instructions below should be taken only as a guide to how |
| 47 | to use GDB when it works, not as a guarantee of success. |
| 48 | |
| 49 | Besides 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 Pike | 2674efb | 2014-04-26 10:18:17 -0600 | [diff] [blame] | 54 | </p> |
| 55 | |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 56 | <h2 id="Introduction">Introduction</h2> |
| 57 | |
| 58 | <p> |
Andrew Gerrand | 2a5879d | 2012-03-20 13:50:05 +1100 | [diff] [blame] | 59 | When you compile and link your Go programs with the <code>gc</code> toolchain |
Hana Kim | 0202aa8 | 2017-09-06 16:17:11 -0400 | [diff] [blame] | 60 | on Linux, Mac OS X, FreeBSD or NetBSD, the resulting binaries contain DWARFv4 |
Hana Kim | 01c979d | 2017-11-14 13:17:44 -0500 | [diff] [blame] | 61 | debugging information that recent versions (≥7.5) of the GDB debugger can |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 62 | use to inspect a live process or a core dump. |
| 63 | </p> |
| 64 | |
| 65 | <p> |
Shenghou Ma | c91c564 | 2014-04-16 01:19:26 -0400 | [diff] [blame] | 66 | Pass the <code>'-w'</code> flag to the linker to omit the debug information |
Russ Cox | 6be622c | 2018-01-09 15:32:22 -0500 | [diff] [blame] | 67 | (for example, <code>go</code> <code>build</code> <code>-ldflags=-w</code> <code>prog.go</code>). |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 68 | </p> |
| 69 | |
Shenghou Ma | d9c4cef | 2012-07-03 12:50:03 -0400 | [diff] [blame] | 70 | <p> |
| 71 | The code generated by the <code>gc</code> compiler includes inlining of |
| 72 | function invocations and registerization of variables. These optimizations |
Russ Cox | ee34617 | 2018-01-09 15:31:45 -0500 | [diff] [blame] | 73 | can sometimes make debugging with <code>gdb</code> harder. |
| 74 | If you find that you need to disable these optimizations, |
| 75 | build your program using <code>go</code> <code>build</code> <code>-gcflags=all="-N -l"</code>. |
Shenghou Ma | d9c4cef | 2012-07-03 12:50:03 -0400 | [diff] [blame] | 76 | </p> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 77 | |
Alberto Donizetti | 4601eae | 2017-01-09 19:11:58 +0100 | [diff] [blame] | 78 | <p> |
| 79 | If you want to use gdb to inspect a core dump, you can trigger a dump |
| 80 | on 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 |
| 83 | documentation</a> for more info). |
| 84 | </p> |
| 85 | |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 86 | <h3 id="Common_Operations">Common Operations</h3> |
| 87 | |
| 88 | <ul> |
| 89 | <li> |
Robert Hencke | b0c3429 | 2012-10-07 11:24:14 +1100 | [diff] [blame] | 90 | Show file and line number for code, set breakpoints and disassemble: |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 91 | <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 Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 99 | Show backtraces and unwind stack frames: |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 100 | <pre>(gdb) <b>bt</b> |
| 101 | (gdb) <b>frame <i>n</i></b></pre> |
| 102 | </li> |
| 103 | <li> |
| 104 | Show the name, type and location on the stack frame of local variables, |
| 105 | arguments 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> |
| 112 | Show 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> |
| 121 | A recent extension mechanism to GDB allows it to load extension scripts for a |
Russ Cox | b6c871a | 2018-01-09 15:26:21 -0500 | [diff] [blame] | 122 | given binary. The toolchain uses this to extend GDB with a handful of |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 123 | commands to inspect internals of the runtime code (such as goroutines) and to |
| 124 | pretty print the built-in map, slice and channel types. |
| 125 | </p> |
| 126 | |
| 127 | <ul> |
| 128 | <li> |
| 129 | Pretty printing a string, slice, map, channel or interface: |
| 130 | <pre>(gdb) <b>p <i>var</i></b></pre> |
| 131 | </li> |
| 132 | <li> |
| 133 | A $len() and $cap() function for strings, slices and maps: |
| 134 | <pre>(gdb) <b>p $len(<i>var</i>)</b></pre> |
| 135 | </li> |
| 136 | <li> |
| 137 | A 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 |
| 141 | type 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 |
| 143 | the short type name and a pointer).</p> |
| 144 | </li> |
| 145 | <li> |
| 146 | Inspecting 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> |
| 150 | For example: |
| 151 | <pre>(gdb) <b>goroutine 12 bt</b></pre> |
| 152 | </li> |
| 153 | </ul> |
| 154 | |
| 155 | <p> |
| 156 | If you'd like to see how this works, or want to extend it, take a look at <a |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 157 | href="/src/runtime/runtime-gdb.py">src/runtime/runtime-gdb.py</a> in |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 158 | the Go source distribution. It depends on some special magic types |
| 159 | (<code>hash<T,U></code>) and variables (<code>runtime.m</code> and |
| 160 | <code>runtime.g</code>) that the linker |
Russ Cox | 6e28bf3 | 2016-01-06 15:26:45 -0500 | [diff] [blame] | 161 | (<a href="/src/cmd/link/internal/ld/dwarf.go">src/cmd/link/internal/ld/dwarf.go</a>) ensures are described in |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 162 | the DWARF code. |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 163 | </p> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 164 | |
| 165 | <p> |
| 166 | If you're interested in what the debugging information looks like, run |
Russ Cox | 6be622c | 2018-01-09 15:32:22 -0500 | [diff] [blame] | 167 | <code>objdump</code> <code>-W</code> <code>a.out</code> and browse through the <code>.debug_*</code> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 168 | sections. |
| 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 |
| 176 | from 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> |
| 180 | that needs to be quoted. It objects even more strongly to method names of |
| 181 | the 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> |
| 188 | In 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 Cox | 6be622c | 2018-01-09 15:32:22 -0500 | [diff] [blame] | 190 | change to <code>$GOROOT/src/regexp</code> and run <code>go</code> <code>test</code> <code>-c</code>. |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 191 | This should produce an executable file named <code>regexp.test</code>. |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 192 | </p> |
| 193 | |
| 194 | |
| 195 | <h3 id="Getting_Started">Getting Started</h3> |
| 196 | |
| 197 | <p> |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 198 | Launch GDB, debugging <code>regexp.test</code>: |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 199 | </p> |
| 200 | |
| 201 | <pre> |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 202 | $ <b>gdb regexp.test</b> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 203 | GNU gdb (GDB) 7.2-gg8 |
| 204 | Copyright (C) 2010 Free Software Foundation, Inc. |
| 205 | License GPLv 3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
| 206 | Type "show copying" and "show warranty" for licensing/warranty details. |
| 207 | This GDB was configured as "x86_64-linux". |
| 208 | |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 209 | Reading symbols from /home/user/go/src/regexp/regexp.test... |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 210 | done. |
| 211 | Loading Go Runtime support. |
| 212 | (gdb) |
| 213 | </pre> |
| 214 | |
| 215 | <p> |
Russ Cox | 6be622c | 2018-01-09 15:32:22 -0500 | [diff] [blame] | 216 | The message "Loading Go Runtime support" means that GDB loaded the |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 217 | extension from <code>$GOROOT/src/runtime/runtime-gdb.py</code>. |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 218 | </p> |
| 219 | |
| 220 | <p> |
| 221 | To help GDB find the Go runtime sources and the accompanying support script, |
| 222 | pass your <code>$GOROOT</code> with the <code>'-d'</code> flag: |
| 223 | </p> |
| 224 | |
| 225 | <pre> |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 226 | $ <b>gdb regexp.test -d $GOROOT</b> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 227 | </pre> |
| 228 | |
| 229 | <p> |
| 230 | If for some reason GDB still can't find that directory or that script, you can load |
| 231 | it by hand by telling gdb (assuming you have the go sources in |
| 232 | <code>~/go/</code>): |
Rob Pike | 7ae41e8 | 2013-02-28 13:32:36 -0800 | [diff] [blame] | 233 | </p> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 234 | |
| 235 | <pre> |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 236 | (gdb) <b>source ~/go/src/runtime/runtime-gdb.py</b> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 237 | Loading Go Runtime support. |
| 238 | </pre> |
| 239 | |
| 240 | <h3 id="Inspecting_the_source">Inspecting the source</h3> |
| 241 | |
| 242 | <p> |
| 243 | Use 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> |
| 251 | List a specific part of the source parametrizing <code>"list"</code> with a |
| 252 | function 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> |
| 260 | List 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> |
| 272 | Variable and function names must be qualified with the name of the packages |
| 273 | they belong to. The <code>Compile</code> function from the <code>regexp</code> |
| 274 | package is known to GDB as <code>'regexp.Compile'</code>. |
| 275 | </p> |
| 276 | |
| 277 | <p> |
| 278 | Methods must be qualified with the name of their receiver types. For example, |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 279 | the <code>*Regexp</code> type’s <code>String</code> method is known as |
| 280 | <code>'regexp.(*Regexp).String'</code>. |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 281 | </p> |
| 282 | |
| 283 | <p> |
| 284 | Variables that shadow other variables are magically suffixed with a number in the debug info. |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 285 | Variables referenced by closures will appear as pointers magically prefixed with '&'. |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 286 | </p> |
| 287 | |
| 288 | <h3 id="Setting_breakpoints">Setting breakpoints</h3> |
| 289 | |
| 290 | <p> |
| 291 | Set a breakpoint at the <code>TestFind</code> function: |
| 292 | </p> |
| 293 | |
| 294 | <pre> |
| 295 | (gdb) <b>b 'regexp.TestFind'</b> |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 296 | Breakpoint 1 at 0x424908: file /home/user/go/src/regexp/find_test.go, line 148. |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 297 | </pre> |
| 298 | |
| 299 | <p> |
| 300 | Run the program: |
| 301 | </p> |
| 302 | |
| 303 | <pre> |
| 304 | (gdb) <b>run</b> |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 305 | Starting program: /home/user/go/src/regexp/regexp.test |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 306 | |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 307 | Breakpoint 1, regexp.TestFind (t=0xf8404a89c0) at /home/user/go/src/regexp/find_test.go:148 |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 308 | 148 func TestFind(t *testing.T) { |
| 309 | </pre> |
| 310 | |
| 311 | <p> |
| 312 | Execution has paused at the breakpoint. |
| 313 | See 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> |
| 323 | the 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> |
| 329 | Look 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 Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 334 | #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 Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 337 | #3 0x000000f8404a89c0 in ?? () |
| 338 | #4 0x0000000000573720 in ?? () |
| 339 | #5 0x0000000000000000 in ?? () |
| 340 | </pre> |
| 341 | |
| 342 | <p> |
| 343 | The 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 Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 348 | #0 0x000000000040facb in runtime.gosched () at /home/user/go/src/runtime/proc.c:873 |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 349 | #1 0x00000000004031c9 in runtime.chanrecv (c=void, ep=void, selected=void, received=void) |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 350 | 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 Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 352 | #3 0x000000000043075b in testing.RunTests (matchString={void (struct string, struct string, bool *, error *)} |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 353 | 0x7ffff7f9ef60, tests= []testing.InternalTest = {...}) at /home/user/go/src/testing/testing.go:201 |
Shenghou Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 354 | #4 0x00000000004302b1 in testing.Main (matchString={void (struct string, struct string, bool *, error *)} |
| 355 | 0x7ffff7f9ef80, tests= []testing.InternalTest = {...}, benchmarks= []testing.InternalBenchmark = {...}) |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 356 | at /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 Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 360 | #8 0x0000000000000000 in ?? () |
| 361 | </pre> |
| 362 | |
| 363 | <p> |
| 364 | The 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> |
| 369 | Stack level 0, frame at 0x7ffff7f9ff88: |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 370 | rip = 0x425530 in regexp.TestFind (/home/user/go/src/regexp/find_test.go:148); |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 371 | 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 Cox | 6be622c | 2018-01-09 15:32:22 -0500 | [diff] [blame] | 381 | The command <code>info</code> <code>locals</code> lists all variables local to the function and their values, but is a bit |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 382 | dangerous to use, since it will also try to print uninitialized variables. Uninitialized slices may cause gdb to try |
| 383 | to print arbitrary large arrays. |
| 384 | </p> |
| 385 | |
| 386 | <p> |
| 387 | The function’s arguments: |
| 388 | </p> |
| 389 | |
| 390 | <pre> |
| 391 | (gdb) <b>info args</b> |
| 392 | t = 0xf840688b60 |
| 393 | </pre> |
| 394 | |
| 395 | <p> |
| 396 | When 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> |
| 398 | on 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 Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 409 | (gdb) p *t->ch |
| 410 | $3 = struct hchan<*testing.T> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 411 | </pre> |
| 412 | |
| 413 | <p> |
Russ Cox | 6be622c | 2018-01-09 15:32:22 -0500 | [diff] [blame] | 414 | That <code>struct</code> <code>hchan<*testing.T></code> is the |
Alberto Donizetti | 4601eae | 2017-01-09 19:11:58 +0100 | [diff] [blame] | 415 | runtime-internal representation of a channel. It is currently empty, |
| 416 | or gdb would have pretty-printed its contents. |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 417 | </p> |
| 418 | |
| 419 | <p> |
| 420 | Stepping forward: |
| 421 | </p> |
| 422 | |
| 423 | <pre> |
| 424 | (gdb) <b>n</b> <i># execute next line</i> |
| 425 | 149 for _, test := range findTests { |
| 426 | (gdb) <i># enter is repeat</i> |
| 427 | 150 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> |
| 444 | We can step into the <code>String</code>function call with <code>"s"</code>: |
| 445 | </p> |
| 446 | |
| 447 | <pre> |
| 448 | (gdb) <b>s</b> |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 449 | regexp.(*Regexp).String (re=0xf84068d070, noname=void) at /home/user/go/src/regexp/regexp.go:97 |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 450 | 97 func (re *Regexp) String() string { |
| 451 | </pre> |
| 452 | |
| 453 | <p> |
| 454 | Get a stack trace to see where we are: |
| 455 | </p> |
| 456 | |
| 457 | <pre> |
| 458 | (gdb) <b>bt</b> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 459 | #0 regexp.(*Regexp).String (re=0xf84068d070, noname=void) |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 460 | at /home/user/go/src/regexp/regexp.go:97 |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 461 | #1 0x0000000000425615 in regexp.TestFind (t=0xf840688b60) |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 462 | at /home/user/go/src/regexp/find_test.go:151 |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 463 | #2 0x0000000000430233 in testing.tRunner (t=0xf840688b60, test=0x5747b8) |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 464 | 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 Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 466 | .... |
| 467 | </pre> |
| 468 | |
| 469 | <p> |
| 470 | Look at the source code: |
| 471 | </p> |
| 472 | |
| 473 | <pre> |
| 474 | (gdb) <b>l</b> |
| 475 | 92 mu sync.Mutex |
| 476 | 93 machine []*machine |
| 477 | 94 } |
| 478 | 95 |
| 479 | 96 // String returns the source text used to compile the regular expression. |
| 480 | 97 func (re *Regexp) String() string { |
| 481 | 98 return re.expr |
| 482 | 99 } |
| 483 | 100 |
| 484 | 101 // Compile parses a regular expression and returns, if successful, |
| 485 | </pre> |
| 486 | |
| 487 | <h3 id="Pretty_Printing">Pretty Printing</h3> |
| 488 | |
| 489 | <p> |
| 490 | GDB'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> |
| 499 | Since slices, arrays and strings are not C pointers, GDB can't interpret the subscripting operation for you, but |
| 500 | you 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 Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 506 | (gdb) <b>p slc-></b><i><TAB></i> |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 507 | array 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> |
| 516 | The 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 Ma | 9dbfda5 | 2012-03-21 00:42:53 +0800 | [diff] [blame] | 527 | Channels and maps are 'reference' types, which gdb shows as pointers to C++-like types <code>hash<int,string>*</code>. Dereferencing will trigger prettyprinting |
Luuk van Dijk | 3e26862 | 2011-10-05 10:49:23 -0700 | [diff] [blame] | 528 | </p> |
| 529 | |
| 530 | <p> |
| 531 | 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.) |
| 532 | </p> |
| 533 | |
| 534 | <pre> |
| 535 | (gdb) <b>p i</b> |
| 536 | $4 = {str = "cbb"} |
| 537 | (gdb) <b>whatis i</b> |
| 538 | type = regexp.input |
| 539 | (gdb) <b>p $dtype(i)</b> |
| 540 | $26 = (struct regexp.inputBytes *) 0xf8400b4930 |
| 541 | (gdb) <b>iface i</b> |
| 542 | regexp.input: struct regexp.inputBytes * |
| 543 | </pre> |