blob: 9717cf384962edab252dae2155f89ffad454b0fb [file] [log] [blame]
David Warren1b4feb42009-04-30 17:16:25 -07001page.title=Debugging with GDB
2pdk.version=1.0
3@jd:body
4
David Warren5c40a482009-06-05 15:11:21 -07005<div id="qv-wrapper">
6<div id="qv">
7<h2>In this document</h2>
David Warren1b4feb42009-04-30 17:16:25 -07008<a name="toc"/>
David Warren5c40a482009-06-05 15:11:21 -07009<ul>
10<li><a href="#gdb">Debugging</a></li>
11 <li><a href="#justInTime">Just-In-Time Debug Feature</a></li>
12</ul>
David Warren1b4feb42009-04-30 17:16:25 -070013</div>
David Warren5c40a482009-06-05 15:11:21 -070014</div>
15
16<p>The current version of <code>envsetup.sh</code> has a <code>gdbclient</code> command that handles much of the setup. For example, to attach the
David Warren1b4feb42009-04-30 17:16:25 -070017 already-running <code>globaltime</code> application, execute the following, making sure that: 1) you do this from the same window used to build the software on the device you are debugging and 2) verify that the symbols in the object files in the build tree match up with what is installed on the device or emulator.</p>
18<pre class="prettify">
19gdbclient app_process :5039 globaltime
20</pre>
David Warren5c40a482009-06-05 15:11:21 -070021<a name="gdb"></a><h3>Debugging</h3>
David Warren1b4feb42009-04-30 17:16:25 -070022<a name="gdbShort"></a>
23<h4>Short Instructions</h4>
24<p>Android runs <code>gdbserver</code> on the device and an ARM aware <code>gdb</code>, named <code>arm-eabi-gdb</code>, on the desktop machine.</p>
25<ol>
26 <li>First you need to
27 run <code>gdbserver</code> on the device:<BR>
28 <pre class="prettify">
29 gdbserver :5039 /system/bin/<i>executable</i>
30 </pre>
31 <BR>
32 The <code>:5039</code> tells gdbserver to listen on port 5039 on the localhost, which adb bridges from the host to the device. <code>executable</code> represents the command to debug, a common one being runtime -s which starts the entire system all running in a single process. <br>
33 <br>
34 </li>
35 <li>Launch <code>gdb</code> on the desktop.
36 This can be
37 done easily with the following command in the shell from which you built:
38 <pre class="prettify">
39gdbclient <i>executable</i>
40</pre>
41 </li>
42</ol>
43<p>At this point <code>gdb</code> will connect with your device
44 and you should be
45 able to enter <code>c</code> to have the device start executing inside of the
46 desktop <code>gdb</code> session.</p>
47<a name="gdbDetailed"></a>
48<h4>Detailed Instructions</h4>
49<p>If the short instructions don't work, these detailed instructions should:
50<ol>
51 <li>On the device, launch a new command:
52 <pre class="prettify">gdbserver :5039 /system/bin/<i>executable</i></pre>
53 or attach to an existing process:
54 <pre class="prettify">gdbserver :5039 --attach <i>pid</i></pre>
55 </li>
56 <li>On your workstation, forward port 5039 to the device with adb:
57 <pre class="prettify">adb forward tcp:5039 tcp:5039</pre>
58 </li>
59 <li>Start a special version of <code>gdb</code> that lives in the "prebuilt" area of the source tree: <br>
60 <code>prebuilt/Linux/toolchain-eabi-4.2.1/bin/arm-eabi-gdb</code> (for Linux) <br>
61 <code>prebuilt/darwin-x86/toolchain-eabi-4.2.1/bin/arm-eabi-gdb</code> (for Darwin) </li>
62 <li>If you can't find either special version of <code>gdb</code>, run <code>find prebuilt -name arm-eabi-gdb</code> in your source tree to find and run the latest version:
63 <pre class="prettify">
64prebuilt/Linux/toolchain-eabi-4.2.1/bin/arm-eabi-gdb out/target/product/<i>product-name</i>/symbols/system/bin/<i>executable</i>
65</pre>
66 <BR>
67 Where <i>product-name</i> is the name of the device product that you're building (for example, <code>sooner</code>),
68 and <i>executable</i> is the program to debug (usually <code>app_process</code> for an application).<br>
69 <br>
70 Make sure to use the copy of the executable in the symbols directory, not the
71 primary android directory, because the one in the primary directory has
72 been stripped of its debugging information.</li>
73 <li>In <code>gdb</code>, Tell <code>gdb</code> where to find the shared libraries that will get loaded:
74 <pre class="prettify">
75set solib-absolute-prefix /<i>absolute-source-path</i>/out/target/product/<i>product-name</i>/symbols
76set solib-search-path /<i>absolute-source-path</i>/out/target/product/<i>product-name</i>/symbols/system/lib
77</pre>
78 <BR>
79 <i>absolute-source-path</i> is the path to your source tree; for example, <code>/work/device</code> or <code>/Users/hoser/android/device</code>.<BR>
80 <i>product-name</i> is the same as above; for example, <code>sooner</code>. <BR>
81 <BR>
82 Make sure you specify the correct directories&#151;<code>gdb</code> may not tell you if you make a mistake.</li>
83 <li>Connect to the device by issuing the <code>gdb</code> command:<BR>
84 <pre class="prettify">
85target remote :5039
86</pre>
87 <BR>
88 <BR>
89 The <code>:5039</code> tells <code>gdb</code> to connect to the localhost port 5039, which is bridged to the device by <code>adb</code>.<BR>
90 <BR>
91 You may need to inspire gdb to load some symbols by typing:
92 <pre class="prettify">shared</pre>
93 </li>
94</ol>
95<p>You should be connected and able to debug as you normally would. You can ignore the error about not
96 finding the location for the thread creation breakpoint. It will be found when
97 the linker loads <code>libc</code> into your process before hitting <code>main()</code>. Also note that
98 the <code>gdb</code> remote protocol doesn't have a way for the device to
99 tell the host about
100 newly created threads so you will not always see notifications about newly
101 created threads. Info about other threads will be queried from the
102 device when a
103 breakpoint is hit or you ask for it by running info thread. <a name="justInTime"></a>
104<h3>Just-In-Time Debug Feature</h3>
105If you see the red LED flashing it means a process is in that new
106state (crashed and waiting for GDB connection). If this happens to the
107system process, most likely your device will be frozen at this point. <strong>Do not press the home key</strong>. Bring the device to someone who can
108debug native crashes and ask for advice.
109If you're in the field and just want your device to continue as it
110would have without this feature (like cylonning), press home (a
111tombstone will be recorded as usual).
112
113To enable a process to be debugged this way, you need to set a property:
114<pre class="prettify">
115adb shell setprop debug.db.uid 10000
116</pre>
117and all processes with a <code>uid &lt;= 10000</code> will be trapped in this
118manner. When one of them crashes, the tombstone is processed as usual, an explicit message is printed into the log, and the red LED starts
119flashing waiting for the Home key to be depressed (in which case it
120continues execution as usual).
121<pre class="prettify">
122I/DEBUG ( 27): ********************************************************
123I/DEBUG ( 27): * process 82 crashed. debuggerd waiting for gdbserver
124I/DEBUG ( 27): *
125I/DEBUG ( 27): * adb shell gdbserver :port --attach 82 &
126I/DEBUG ( 27): *
127I/DEBUG ( 27): * and press the HOME key.
128I/DEBUG ( 27): ********************************************************
129</pre>
130<p>When you see the entry above, make sure <code>adb</code> is forwarding port 5039 (you only need to do this once,
131 unless the ADB server dies) and execute:</p>
132<pre class="prettify">% adb forward tcp:5039 tcp:5039</pre>
133Execute the line shown in the debug output, substituting 5039 for the proper <code>port</code>:
134<pre class="prettify">
135% adb shell gdbserver :5039 --attach 82 &
136</pre>
137<p>If the crashing process is based off zygote (that is, system_server and all
138 applications), the default values for the <code>gdbclient</code> command, <code>app_process</code> binary and port <code>5039</code>, are correct, so you can execute:</p>
139<pre class="prettify">
140% cd &lt;top of device source tree&gt;
141% gdbclient
142</pre>
143<p>Otherwise you need to determine the path of the crashing binary and follow the
144 steps as mentioned above (for example, <code>gdbclient hoser :5039</code> if
145 the <code>hoser</code> command has failed).</p>