1page.title=ndk-gdb
2@jd:body
3
4<div id="qv-wrapper">
5    <div id="qv">
6      <h2>On this page</h2>
7
8      <ol>
9        <li><a href="#req">Requirements</a></li>
10        <li><a href="#use">Usage</a></li>
11        <li><a href="#thread">Thread Support</a></li>
12      </ol>
13    </div>
14  </div>
15
16<p>The NDK includes a helper shell script named {@code ndk-gdb} to easily launch a native debugging
17  session for your NDK-generated machine code.</p>
18
19<h2 id="req">Requirements</h2>
20
21<p>For native debugging to work, you must follow these requirements:</p>
22
23<ul>
24<li>Build your app using the {@code ndk-build} script. The {@code ndk-gdb} script
25does not support using the legacy {@code make APP=&lt;name&gt;} method to build.</p></li>
26<li>Enable app debugging in your {@code AndroidManifest.xml} file by including an
27{@code &lt;application&gt;} element that sets the {@code android:debuggable} attribute to {@code
28true}.</li>
29<li>Build your app to run on Android 2.2 (Android API level 8) or higher.</li>
30<li>Debug on a device or emulator running Android 2.2 or higher. For debugging purposes, the target
31API level that you declare in your {@code AndroidManifest.xml} file does not matter.</li>
32<li>Develop your app in a Unix shell. On Windows, use <a href="https://www.cygwin.com/">Cygwin</a>
33or the experimental {@code ndk-gdb-py} <a href="https://www.python.org/">Python</a>
34implementation.</li>
35<li>Use GNU Make 3.81 or higher.</li>
36<li>If you are building your app from
37<a href="{@docRoot}sdk/installing/installing-adt.html">Eclipse</a>, build it
38using version 0.9.7 or higher of the ADT plug-in.</li>
39
40<h2 id="use">Usage</h2>
41  To invoke the {@code ndk-gdb} script, change into the application directory or any directory under
42  it. For example:</p>
43
44<pre class="no-pretty-print">
45cd $PROJECT
46$NDK/ndk-gdb
47</pre>
48
49<p>Here, {@code $PROJECT} points to your project's root directory, and {@code $NDK} points to your
50NDK installation path.</p>
51
52<p>When you invoke {@code ndk-gdb}, it configures the session to look for your source files
53and symbol/debug versions of your generated native libraries. On successfully attaching to your
54application process, {@code ndk-gdb} outputs a long series of error messages, noting that it cannot
55find various system libraries. This is normal, because your host machine does not contain
56symbol/debug versions of these libraries on your target device. You can safely ignore these
57messages.</p>
58
59<p>Next, {@code ndk-gdb} displays a normal GDB prompt.</p>
60
61<p>You interact with {@code ndk-gdb} in the same way as you would with GNU GDB. For example, you can
62use {@code b &lt;location&gt;} to set breakpoints, and {@code c} (for "continue") to
63resume execution. For a comprehensive list of commands, see the
64<a href="http://www.gnu.org/software/gdb/">GDB manual.</a></p>
65
66<p>Note that when you quit the GDB prompt, the application process that you're debugging stops. This
67behavior is a gdb limitation.</p>
68
69<p>{@code ndk-gdb} handles many error conditions, and displays an informative error message if it
70finds a problem. these checks include making sure that the following conditions are satisfied:</p>
71
72<ul>
73<li>Checks that ADB is in your path.</li>
74<li>Checks that your application is declared debuggable in its manifest.</li>
75<li>Checks that, on the device, the installed application with the same package name is also
76debuggable.</li>
77</ul>
78
79<p>By default, {@code ndk-gdb} searches for an already-running application process, and displays an
80error if it doesn't find one. You can, however, use the {@code --start} or
81{@code --launch=&lt;name&gt;} option to automatically start your activity before the debugging
82session. For more information, see <a href="#opt">Options</a>.</p>
83
84
85<h3 id="opt">Options</h3>
86<p>To see a complete list of options, type {@code ndk-gdb --help} on the command line. Table 1
87shows a number of the more commonly used ones, along with brief descriptions.</p>
88
89<p class="table-caption" id="table1">
90  <strong>Table 1.</strong> Common ndk-gdb options and their descriptions.</p>
91
92<table>
93<tr>
94<th>Option</th>
95<th>Description></th>
96<tr>
97
98<tr>
99<td>{@code --verbose}</td>
100<td><p>This option tells the build system to print verbose information about the native-debugging
101session setup. It is necessary only for debugging problems when the debugger can't connect to the
102app, and the error messages that {@code ndk-gdb} displays are not enough.</p></td>
103</tr>
104
105<tr>
106<td>{@code --force}</td>
107<td>By default, {@code ndk-gdb} aborts if it finds that another native debugging session is already
108 running on the same device. This option kills the other session, and replaces it with a new one.
109 Note that this option does not kill the actual app being debugged, which you must kill
110 separately.</td>
111</tr>
112
113<tr>
114<td>{@code --start}</td>
115<td><p>When you start {@code ndk-gdb}, it tries by default to attach to an existing running instance of
116your app on the target device. You can override this default behavior by using {@code --start} to
117explicitly launch the application on the target device before the debugging session.</p></td>
118
119<p>Starting {@code ndk-gdb} with this option specified launches the first launchable activity listed
120in your application manifest. Use {@code --launch=&lt;name&gt;} to start the next launchable
121activity. To dump the list of launchable activities, run {@code --launch-list} from the command
122line.</p>
123</tr>
124
125<tr>
126<td>{@code --launch=&lt;name&gt;}</td>
127<td><p>This option is similar to {@code --start}, except that it allows you to start a specific
128 activity from your application. This feature is only useful if your manifest defines multiple
129 launchable activities.</p></td>
130</tr>
131
132<tr>
133<td>{@code --launch-list}</td>
134<td><p>This convenience option prints the list of all launchable activity names found in your
135 app manifest. {@code --start} uses the first activity name.</p></td>
136</tr>
137
138<tr>
139<td>{@code --project=&lt;path&gt;}</td>
140<td>This option specifies the app project directory. It is useful if you want to launch the
141 script without first having to change to the project directory.</p></td>
142</tr>
143
144<tr>
145<td>{@code --port=&lt;port&gt;}</td>
146<td> <p>By default, {@code ndk-gdb} uses local TCP port 5039 to communicate with the app it
147 is debugging on the target device. Using a different port allows you to natively debug programs
148 running on different devices or emulators connected to the same host machine.</p></td>
149</tr>
150
151<tr>
152<td>{@code --adb=&lt;file&gt;}</td>
153<td><p>This option specifies the <a href="{@docRoot}tools/help/adb.html">adb</a>
154tool executable. It is only necessary if you have not set your path to include that executable.</p>
155</td>
156</tr>
157
158<tr>
159<td>
160<li>{@code -d}</li>
161<li>{@code -e}</li>
162<li>{@code -s &lt;serial&gt;}</li></td>
163<td><p>These flags are similar to the adb commands with the same names. Set these flags if you have
164several devices or emulators connected to your host machine. Their meanings are as follows:</p>
165<dl>
166   <dt>{@code -d}</dt>
167   <dd>Connect to a single physical device.</dd>
168   <dt>{@code -e}</dt>
169   <dd>Connect to a single emulator device.</dd>
170   <dt>{@code -s &lt;serial&gt;}</dt>
171   <dd>Connect to a specific device or emulator. Here, {@code &lt;serial&gt;} is the device's name
172   as listed by the {@code adb devices} command.</dd>
173</dl>
174
175<p>Alternatively, you can define the {@code ADB_SERIAL} environment variable to list a specific
176device, without the need for a specific option.</p></td>
177</tr>
178
179<tr>
180<td>
181<li>{@code --exec=&lt;file&gt;}</li>
182<li>{@code -x &lt;file&gt;}</li>
183</td>
184<td><p>This option tells {@code ndk-gdb} to run the GDB initialization commands found in
185{@code &lt;file&gt;} after connecting to the process it is debugging. This is a useful feature if
186you want to do something repeatedly, such as setting up a list of breakpoints, and then resuming
187execution automatically.</p></td>
188</tr>
189
190<tr>
191<td>{@code --nowait}</td>
192<td><p>Disable pausing the Java code until GDB connects. Passing this option may cause the debugger
193 to miss early breakpoints.</p>
194</tr>
195
196<tr>
197<td>{@code --tui}
198{@code -t}</td>
199<td><p>Enable Text User Interface if it is available.</p></td>
200</tr>
201
202<tr>
203<td>{@code --gnumake-flag=&lt;flag&gt;}</td>
204<td><p>This option is an extra flag (or flags) to pass to the
205{@code ndk-build} system when
206querying it for project information. You can use multiple instances of this option in the
207same command.</p></td>
208</tr>
209
210<tr>
211<td>{@code --stdcxx-py-pr={auto|none|gnustdcxx[-GCCVER]|stlport}}</td>
212<td><p>Use specified Python pretty-printers for displaying types in the Standard C++ Library.
213 {@code auto} mode works by looking at the {@code .so} files for a {@code libstdc++} library,
214 and as such only works for a shared library. When linking statically to a {@code libstdc++} library,
215 you must specify the required printers. The default is {@code none}.</p></td>
216</tr>
217</table>
218
219<p class="note"><strong>Note: </strong>The final three options in this table are only for the
220Python version of {@code ndk-gdb}.</p></td>
221
222<h2 id="thread">Thread Support</h2>
223<p>If your app runs on a platform older than Android 2.3 (API level 9), {@code ndk-gdb}
224cannot debug native threads properly. The debugger can only debug the main thread, abd completely
225ignores the execution of other threads.</p>
226
227<p>Using a version of Android prior to 2.3 causes {@code ndk-gdb} to display the following message
228prior to showing the GDB prompt:</p>
229
230<pre class="no-pretty-print">
231Thread debugging is unsupported on this Android platform!
232</pre>
233
234
235<p>If you place a breakpoint on a function executed on a non-main thread, the program exits, and
236GDB displays the following message:</p>
237
238<pre class="no-pretty-print">
239Program terminated with signal SIGTRAP, Trace/breakpoint trap.
240      The program no longer exists.
241</pre>
242