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