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