OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / docs / html / guide / developing / tools / adb.jd
1 page.title=Android Debug Bridge
2 @jd:body
3
4 <div id="qv-wrapper">
5 <div id="qv">
6   <h2>ADB quickview</h2>
7   <ul>
8 <li>Manage the state of an emulator or device</li>
9 <li>Run shell commands on a device</li>
10 <li>Manage port forwarding on an emulator or device</li>
11 <li>Copy files to/from an emulator or device</li>
12   </ul>
13
14   <h2>In this document</h2>
15   <ol>
16 <li><a href="#issuingcommands">Issuing ADB Commands</a></li>
17 <li><a href="#devicestatus">Querying for Emulator/Device Instances</a></li>
18 <li><a href="#directingcommands">Directing Commands to a Specific Emulator/Device Instance</a></li>
19 <li><a href="#move">Installing an Application</a></li>
20 <li><a href="#forwardports">Forwarding Ports</a></li>
21 <li><a href="#copyfiles">Copying Files to or from an Emulator/Device Instance</a></li>
22 <li><a href="#commandsummary">Listing of adb Commands </a></li>
23 <li><a href="#shellcommands">Issuing Shell Commands</a></li>
24 <li><a href="#logcat">Enabling logcat Logging</a></li>
25 <li><a href="#stopping">Stopping the adb Server</a></li>
26   </ol>
27
28   <h2>See also</h2>
29   <ol>
30     <li><a href="emulator.html">Emulator</a></li>
31   </ol>
32
33 </div>
34 </div>
35
36 <!-- 
37 <p>Android Debug Bridge (adb) is a versatile tool that </a>. </p>
38
39 <p>Some of ways you can use adb include:</p>
40
41 <ul>
42
43 </ul>
44 <p>The sections below introduce adb and describe many of its common uses. </p>
45
46 <h2>Contents</h2>
47 <dl>
48 <dt><a href="#overview">Overview</a></dt>
49 <dt><a href="#issuingcommands">Issuing adb Commands</a></dt>
50 <dt><a href="#devicestatus">Querying for Emulator/Device Instances</a></dt>
51 <dt><a href="#directingcommands">Directing Commands to a Specific Emulator/Device Instance</a></dt>
52 <dt><a href="#move">Installing an Application</a></dt>
53 <dt><a href="#forwardports">Forwarding Ports</a></dt>
54 <dt><a href="#copyfiles">Copying Files to or from an Emulator/Device Instance</a></dt>
55 <dt><a href="#commandsummary">Listing of adb Commands </a></dt>
56 <dt><a href="#shellcommands">Issuing Shell Commands</a></dt>
57     <dd><a href="#sqlite">Examining sqlite3 Databases from a Remote Shell</a></dd>
58     <dd><a href="#monkey">UI/Application Exerciser Monkey</a></dd>
59         <dd><a href="#othershellcommands">Other Shell Commands</a></dd>
60 <dt><a href="#logcat">Enabling logcat Logging</a> </dt>
61         <dd><a href="#usinglogcat">Using logcat Commands</a></dd>
62         <dd><a href="#filteringoutput">Filtering Log Output</a></dd>
63         <dd><a href="#outputformat">Controlling Log Output Format</a></dd>
64         <dd><a href="#alternativebuffers">Viewing Alternative Log Buffers</a></dd>
65         <dd><a href="#stdout">Viewing stdout and stderr</a></dd>
66         <dd><a href="#logcatoptions">Listing of logcat Command Options</a></dd>
67 <dt><a href="#stopping">Stopping the adb Server</a> </dt>
68 </dl>
69
70 <a name="overview"></a>
71
72 <h2>Overview</h2>
73 -->
74 <p>Android Debug Bridge (adb) is a versatile tool lets you manage the state of an emulator instance or Android-powered device. It is a client-server program that includes three components: </p>
75
76 <ul>
77   <li>A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients. </li>
78   <li>A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device. </li>
79   <li>A daemon, which runs as a background process on each emulator or device instance. </li>
80 </ul>
81
82 <p>You can find the {@code adb} tool in {@code &lt;sdk&gt;/platform-tools/}.</p>
83
84 <p>When you start an adb client, the client first checks whether there is an adb server process already running. If there isn't, it starts the server process. When the server starts, it binds to local TCP port 5037 and listens for commands sent from adb clients&mdash;all adb clients use port 5037 to communicate with the adb server. </p>
85
86 <p>The server then sets up connections to all running emulator/device instances. It locates emulator/device instances by scanning odd-numbered ports in the range 5555 to 5585, the range used by emulators/devices. Where the server finds an adb daemon, it sets up a connection to that port. Note that each emulator/device instance acquires a pair of sequential ports &mdash; an even-numbered port for console connections and an odd-numbered port for adb connections. For example: </p>
87
88 <p style="margin-left:2em">
89 Emulator 1, console: 5554<br/>
90 Emulator 1, adb: 5555<br>
91 Emulator 2, console: 5556<br>
92 Emulator 2, adb: 5557 ...
93 </p>
94
95 <p>As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on port 5554. </p>
96
97 <p>Once the server has set up connections to all emulator instances, you can use adb commands to control and access those instances. Because the server manages connections to emulator/device instances and handles commands from multiple adb clients, you can control any emulator/device instance from any client (or from a script).</p>
98
99 <p>The sections below describe the commands that you can use to access adb capabilities and manage the state of an emulator/device. Note that if you are developing Android applications in Eclipse and have installed the ADT plugin, you do not need to access adb from the command line. The ADT plugin provides a transparent integration of adb into the Eclipse IDE. However, you can still use adb directly as necessary, such as for debugging.</p>
100
101 <a name="issuingcommands"></a>
102
103 <h2>Issuing adb Commands</h2>
104
105 <p>You can issue adb commands from a command line on your development machine or from a script. The usage is: </p>
106
107     <pre>adb [-d|-e|-s &lt;serialNumber&gt;] &lt;command&gt; </pre>
108
109 <p>When you issue a command, the program invokes an adb client. The client is not specifically associated with any emulator instance, so if multiple emulators/devices are running, you need to use the <code>-d</code> option to specify the target instance to which the command should be directed. For more information about using this option, see <a href="#directingcommands">Directing Commands to a Specific Emulator/Device Instance</a>. </p>
110
111 <a name="devicestatus"></a>
112
113 <h2>Querying for Emulator/Device Instances</h2>
114
115 <p>Before issuing adb commands, it is helpful to know what emulator/device instances are connected to the adb server. You can generate a list of attached emulators/devices using the <code>devices</code> command: </p>
116
117         <pre>adb devices</pre>
118
119 <p>In response, adb prints this status information for each instance:</p>
120
121 <ul>
122         <li>Serial number &mdash; A string created by adb to uniquely identify an emulator/device instance by its 
123         console port number. The format of the serial number is <code>&lt;type&gt;-&lt;consolePort&gt;</code>. 
124         Here's an example serial number: <code>emulator-5554</code></li>
125         <li>State &mdash; The connection state of the instance. Three states are supported: 
126                 <ul>
127                 <li><code>offline</code> &mdash; the instance is not connected to adb or is not responding.</li>
128                 <li><code>device</code> &mdash; the instance is now connected to the adb server. Note that this state does not 
129                     imply that the Android system is fully booted and operational, since the instance connects to adb 
130                     while the system is still booting. However, after boot-up, this is the normal operational state of 
131                     an emulator/device instance.</li>
132                 </ul>
133         </li>
134 </ul>
135
136 <p>The output for each instance is formatted like this: </p>
137
138         <pre>[serialNumber] [state]</pre>
139
140 <p>Here's an example showing the <code>devices</code> command and its output:</p>
141
142         <pre>$ adb devices
143 List of devices attached 
144 emulator-5554&nbsp;&nbsp;device
145 emulator-5556&nbsp;&nbsp;device
146 emulator-5558&nbsp;&nbsp;device</pre>
147
148 <p>If there is no emulator/device running, adb returns <code>no device</code>.</p>
149
150
151 <a name="directingcommands"></a>
152
153 <h2>Directing Commands to a Specific Emulator/Device Instance</h2>
154
155 <p>If multiple emulator/device instances are running, you need to specify a target instance when issuing adb commands. To so so, use the <code>-s</code> option in the commands. The usage for the <code>-s</code> option is:</p>
156
157     <pre>adb -s &lt;serialNumber&gt; &lt;command&gt; </pre>
158         
159 <p>As shown, you specify the target instance for a command using its adb-assigned serial number. You can use the <code>devices</code> command to obtain the serial numbers of running emulator/device instances. </p>
160
161 <p>Here is an example: </p>
162
163         <pre>adb -s emulator-5556 install helloWorld.apk</pre>
164
165 <p>Note that, if you issue a command without specifying a target emulator/device instance using <code>-s</code>, adb generates an error. 
166
167 <a name="move"></a>
168
169 <h2>Installing an Application</h2>
170 <p>You can use adb to copy an application from your development computer and install it on an emulator/device instance. To do so, use the <code>install</code> command. With the command, you must specify the path to the .apk file that you want to install:</p>
171
172 <pre>adb install &lt;path_to_apk&gt;</pre>
173
174 <p>For more information about how to create an .apk file that you can install on an emulator/device instance, see <a href="{@docRoot}guide/developing/tools/aapt.html">Android Asset Packaging Tool</a> (aapt). </p>
175
176 <p>Note that, if you are using the Eclipse IDE and have the ADT plugin installed, you do not need to use adb (or aapt) directly to install your application on the emulator/device. Instead, the ADT plugin handles the packaging and installation of the application for you. </p>
177
178
179 <a name="forwardports"></a>
180
181 <h2>Forwarding Ports</h2>
182
183     <p>You can use the <code>forward</code> command to set up arbitrary port forwarding &mdash; forwarding of requests on a specific host port to a different port on an emulator/device instance. Here's how you would set up forwarding of host port 6100 to emulator/device port 7100:</p>
184 <pre>adb forward tcp:6100 tcp:7100</pre>
185     <p>You can also use adb to set up forwarding to named abstract UNIX domain sockets, as illustrated here:</p>
186 <pre>adb forward tcp:6100 local:logd </pre>
187
188 <a name="copyfiles"></a>
189
190 <h2>Copying Files to or from an Emulator/Device Instance</h2>
191
192 <p>You can use the adb commands <code>pull</code> and <code>push</code> to copy files to and from an emulator/device instance's data file. Unlike the <code>install</code> command, which only copies an .apk file to a specific location, the <code>pull</code> and <code>push</code> commands let you copy arbitrary directories and files to any location in an emulator/device instance. </p>
193
194 <p>To copy a file or directory (recursively) <em>from</em> the emulator or device, use</p>
195 <pre>adb pull &lt;remote&gt; &lt;local&gt;</pre> 
196
197 <p>To copy a file or directory (recursively) <em>to</em> the emulator or device, use</p>
198     <pre>adb push &lt;local&gt; &lt;remote&gt;</pre> 
199
200 <p>In the commands, <code>&lt;local&gt;</code> and <code>&lt;remote&gt;</code> refer to the paths to the target files/directory on your development machine (local) and on the emulator/device instance (remote).</p>
201
202 <p>Here's an example: </p>
203 <pre>adb push foo.txt /sdcard/foo.txt</pre>
204
205 <a name="commandsummary"></a>
206
207 <h2>Listing of adb Commands</h2>
208
209 <p>The table below lists all of the supported adb commands and explains their meaning and usage. </p>
210
211
212 <table>
213 <tr>
214         <th>Category</th>
215         <th>Command</th>
216         <th>Description</th>
217         <th>Comments</th>
218 </tr>
219
220 <tr>
221 <td rowspan="3">Options</td>
222 <td><code>-d</code></td>
223 <td>Direct an adb command to the only attached USB device.</td>
224 <td>Returns an error if more than one USB device is attached.</td>
225 </tr>
226
227 <tr>
228 <td><code>-e</code></td>
229 <td>Direct an adb command to the only running emulator instance.</td>
230 <td>Returns an error if more than one emulator instance is running. </td>
231 </tr>
232
233 <tr>
234 <td><code>-s&nbsp;&lt;serialNumber&gt;</code></td>
235 <td>Direct an adb command a specific emulator/device instance, referred to by its adb-assigned serial number (such as "emulator-5556").</td>
236 <td>If not specified, adb generates an error.</td>
237 </tr>
238
239 <tr>
240 <td rowspan="3">General</td>
241 <td><code>devices</code></td>
242 <td>Prints a list of all attached emulator/device instances.</td>
243 <td>See <a href="#devicestatus">Querying for Emulator/Device Instances</a> for more information.</td>
244 </tr>
245
246 <tr>
247 <td><code>help</code></td>
248 <td>Prints a list of supported adb commands.</td>
249 <td>&nbsp;</td>
250 </tr>
251
252 <tr>
253 <td><code>version</code></td>
254 <td>Prints the adb version number. </td>
255 <td>&nbsp;</td>
256 </tr>
257
258 <tr>
259 <td rowspan="3">Debug</td>
260 <td ><code>logcat&nbsp;[&lt;option&gt;] [&lt;filter-specs&gt;]</code></td>
261 <td>Prints log data to the screen. </td>
262 <td>&nbsp;</td>
263 </tr>
264
265 <tr>
266 <td><code>bugreport</code></td>
267 <td>Prints <code>dumpsys</code>, <code>dumpstate</code>, and <code>logcat</code> data to the screen, for the purposes of bug reporting. </td>
268 <td>&nbsp;</td>
269 </tr>
270
271 <tr>
272 <td><code>jdwp</code></td>
273 <td>Prints a list of available JDWP processes on a given device. </td>
274 <td>You can use the <code>forward jdwp:&lt;pid&gt;</code> port-forwarding specification to connect to a specific JDWP process. For example: <br>
275     <code>adb forward tcp:8000 jdwp:472</code><br>
276     <code>jdb -attach localhost:8000</code></p>
277  </td>
278 </tr>
279
280 <tr>
281 <td rowspan=3">Data</td>
282 <td><code>install&nbsp;&lt;path-to-apk&gt;</code></td>
283 <td>Pushes an Android application (specified as a full path to an .apk file) to the data file of an emulator/device. </td>
284 <td>&nbsp;</td>
285 </tr>
286
287 <tr>
288 <td><code>pull&nbsp;&lt;remote&gt;&nbsp;&lt;local&gt;</code></td>
289 <td>Copies a specified file from an emulator/device instance to your development computer. </td>
290 <td>&nbsp;</td>
291 </tr>
292
293 <tr>
294 <td><code>push&nbsp;&lt;local&gt;&nbsp;&lt;remote&gt;</code></td>
295 <td>Copies a specified file from your development computer to an emulator/device instance. </td>
296 <td>&nbsp;</td>
297 </tr>
298
299 <tr>
300 <td rowspan="2">Ports and Networking</td>
301 <td><code>forward&nbsp;&lt;local&gt;&nbsp;&lt;remote&gt;</code></td>
302 <td>Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance. </td>
303 <td>Port specifications can use these schemes: 
304 <ul><li><code>tcp:&lt;portnum&gt;</code></li>
305 <li><code>local:&lt;UNIX domain socket name&gt;</code></li>
306 <li><code>dev:&lt;character device name&gt;</code></li>
307 <li><code>jdwp:&lt;pid&gt;</code></li></ul>
308 </td>
309 </tr>
310
311 <tr>
312 <td><code>ppp&nbsp;&lt;tty&gt;&nbsp;[parm]...</code></td>
313 <td>Run PPP over USB.
314 <ul>
315 <li><code>&lt;tty&gt;</code> &mdash; the tty for PPP stream. For example <code>dev:/dev/omap_csmi_ttyl</code>. </li>
316 <li><code>[parm]... </code> &mdash zero or more PPP/PPPD options, such as <code>defaultroute</code>, <code>local</code>, <code>notty</code>, etc.</li></ul>
317
318 <p>Note that you should not automatically start a PPP connection. </p></td>
319 <td></td>
320 </tr>
321
322 <tr>
323 <td rowspan="3">Scripting</td>
324 <td><code>get-serialno</code></td>
325 <td>Prints the adb instance serial number string.</td>
326 <td rowspan="2">See <a href="#devicestatus">Querying for Emulator/Device Instances</a> for more information. </td>
327 </tr>
328
329 <tr>
330 <td><code>get-state</code></td>
331 <td>Prints the adb state of an emulator/device instance.</td>
332 </td>
333 </tr>
334
335 <tr>
336 <td><code>wait-for-device</code></td>
337 <td>Blocks execution until the device is online &mdash; that is, until the instance state is <code>device</code>.</td>
338 <td>You can prepend this command to other adb commands, in which case adb will wait until the emulator/device instance is connected before issuing the other commands. Here's an example: 
339 <pre>adb wait-for-device shell getprop</pre>
340
341 Note that this command does <em>not</em> cause adb to wait until the entire system is fully booted. For that reason, you should not prepend it to other commands that require a fully booted system. As an example, the <code>install</code> requires the Android package manager, which is available only after the system is fully booted. A command such as 
342
343 <pre>adb wait-for-device install &lt;app&gt;.apk</pre>
344
345 would issue the <code>install</code> command as soon as the emulator or device instance connected to the adb server, but before the Android system was fully booted, so it would result in an error. </td>
346 </tr>
347
348
349
350 <tr>
351 <td rowspan="2">Server</td>
352 <td><code>start-server</code></td>
353 <td>Checks whether the adb server process is running and starts it, if not.</td>
354 <td>&nbsp;</td>
355 </tr>
356
357 <tr>
358 <td><code>kill-server</code></td>
359 <td>Terminates the adb server process.</td>
360 <td>&nbsp;</td>
361 </tr>
362
363
364
365 <tr>
366 <td rowspan="2">Shell</td>
367 <td><code>shell</code></td>
368 <td>Starts a remote shell in the target emulator/device instance.</td>
369 <td rowspan="2">See <a href="#shellcommands">Issuing Shell Commands</a> for more information. </td>
370 </tr>
371
372 <tr>
373 <td><code>shell&nbsp;[&lt;shellCommand&gt;]</code></td>
374 <td>Issues a shell command in the target emulator/device instance and then exits the remote shell.</td>
375 </tr>
376
377 </table>
378
379
380 <a name="shellcommands"></a>
381
382 <h2>Issuing Shell Commands</h2>
383
384 <p>Adb provides an ash shell that you can use to run a variety of commands on an emulator 
385 or device. The command binaries are stored in the file system of the emulator or device, 
386 in this location: </p>
387
388 <pre>/system/bin/...</pre>
389
390 <p>You can use the <code>shell</code> command to issue commands, with or without entering the adb remote shell on the emulator/device. </p>
391
392 <p>To issue a single command without entering a remote shell, use the <code>shell</code> command like this: </p>
393
394         <pre>adb [-d|-e|-s {&lt;serialNumber&gt;}] shell &lt;shellCommand&gt;</pre>
395         
396 <p>To drop into a remote shell on a emulator/device instance, use the <code>shell</code> command like this:</p>
397
398         <pre>adb [-d|-e|-s {&lt;serialNumber&gt;}] shell</pre>
399
400 <p>When you are ready to exit the remote shell, use <code>CTRL+D</code> or <code>exit</code> to end the shell session. </p>
401
402 <p>The sections below provide more information about shell commands that you can use.</p>
403       
404 <a name="sqlite" id="sqlite"></a>
405
406 <h3>Examining sqlite3 Databases from a Remote Shell</h3>
407
408 <p>From an adb remote shell, you can use the 
409 <a href="http://www.sqlite.org/sqlite.html">sqlite3</a> command-line program to 
410 manage SQLite databases created by Android applications. The 
411 <code>sqlite3</code> tool includes many useful commands, such as 
412 <code>.dump</code> to print out the contents of a table and 
413 <code>.schema</code> to print the SQL CREATE statement for an existing table. 
414 The tool also gives you the ability to execute SQLite commands on the fly.</p>
415
416 <p>To use <code>sqlite3</code>, enter a remote shell on the emulator instance, as described above, then invoke the tool using the <code>sqlite3</code> command. Optionally, when invoking <code>sqlite3</code> you can specify the full path to the database you want to explore. Emulator/device instances store SQLite3 databases in the folder <code><span chatdir="1"><span chatindex="259474B4B070F261">/data/data/<em>&lt;package_name&gt;</em>/databases</span></span>/</code>. </p>
417
418 <p>Here's an example: </p>
419
420 <pre>$ adb -s emulator-5554 shell
421 # sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
422 SQLite version 3.3.12
423 Enter &quot;.help&quot; for instructions
424 <em>.... enter commands, then quit...</em>
425 sqlite&gt; .exit </pre>
426
427 <p>Once you've invoked <code>sqlite3</code>, you can issue <code>sqlite3</code> commands in the shell. To exit and return to the adb remote shell, use <code>exit</code> or <code>CTRL+D</code>.
428
429
430 <a name="monkey"></a>
431
432 <h3>UI/Application Exerciser Monkey</h3>
433
434 <p>The Monkey is a program that runs on your emulator or device and generates pseudo-random
435 streams of user events such as clicks, touches, or gestures, as well as a number of system-level 
436 events.  You can use the Monkey to stress-test applications that you are developing,
437 in a random yet repeatable manner.</p>
438
439 <p>The simplest way to use the monkey is with the following command, which will launch your
440 application and send 500 pseudo-random events to it.</p>
441
442 <pre>$ adb shell monkey -v -p your.package.name 500</pre>
443
444 <p>For more information about command options for Monkey, see the complete 
445 <a href="{@docRoot}guide/developing/tools/monkey.html" title="monkey">UI/Application Exerciser Monkey</a> documentation page.</p>
446
447
448 <a name="othershellcommands"></a>
449
450 <h3>Other Shell Commands</h3>
451
452 <p>The table below lists several of the adb shell commands available. For a complete list of commands and programs, start an emulator instance and use the <code>adb -help</code> command. </p>
453
454 <pre>adb shell ls /system/bin</pre>
455
456 <p>Help is available for most of the commands. </p>
457
458 <table>
459 <tr>
460         <th>Shell Command</th>
461         <th>Description</th>
462         <th>Comments</th>
463 </tr>
464
465 <tr>
466 <td><code>dumpsys</code></td>
467 <td>Dumps system data to the screen.</td>
468 <td rowspan=4">The <a href="{@docRoot}guide/developing/tools/ddms.html">Dalvik Debug Monitor Service</a> (DDMS) tool offers integrated debug environment that you may find easier to use.</td>
469 </tr>
470
471 <tr>
472 <td><code>dumpstate</code></td>
473 <td>Dumps state to a file.</td>
474 </tr>
475
476 <tr>
477 <td><code>logcat&nbsp;[&lt;option&gt;]...&nbsp;[&lt;filter-spec&gt;]...</code></td>
478 <td>Enables radio logging and prints output to the screen. </td>
479 </tr>
480
481 <tr>
482 <td><code>dmesg</code></td>
483 <td>Prints kernel debugging messages to the screen. </td>
484 </tr>
485
486 <tr>
487 <td><code>start</code></td>
488 <td>Starts (restarts) an emulator/device instance.</td>
489 <td>&nbsp;</td>
490 </tr>
491
492 <tr>
493 <td><code>stop</code></td>
494 <td>Stops execution of an emulator/device instance.</td>
495 <td>&nbsp;</td>
496 </tr>
497
498 </table>
499
500 <a name="logcat"></a>
501
502 <h2>Enabling logcat Logging</h2>
503
504 <p>The Android logging system provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in a series of circular buffers, which then can be viewed and filtered by the <code>logcat</code> command.</p>
505
506 <a name="usinglogcat"></a>
507
508 <h3>Using logcat Commands</h3>
509
510 <p>You can use the <code>logcat</code> command to view and follow the contents of the system's log buffers. The general usage is:</p>
511
512 <pre>[adb] logcat [&lt;option&gt;] ... [&lt;filter-spec&gt;] ...</pre>
513
514 <p>The sections below explain filter specifications and the command options. See <a href="#logcatoptions">Listing of logcat Command Options</a> for a summary of options. </p>
515
516 <p>You can use the <code>logcat</code> command from your development computer  or from a remote adb shell in an emulator/device instance. To view log output in your development computer, you use</p>
517
518 <pre>$ adb logcat</pre>
519
520 <p>and from a remote adb shell you use</p>
521
522 <pre># logcat</pre>
523
524 <a name="filteringoutput"></a>
525
526 <h3>Filtering Log Output</h3>
527
528 <p>Every Android log message has a <em>tag</em> and a <em>priority</em> associated with it. </p>
529
530 <ul>
531 <li>The tag of a log message is a short string indicating the system component from which the message originates (for example, "View" for the view system). </li>
532
533 <li>The priority is one of the following character values, ordered from lowest to highest priority:</li>
534
535 <ul>
536     <li><code>V</code> &mdash; Verbose (lowest priority)</li>
537         <li><code>D</code> &mdash; Debug</li>
538         <li><code>I</code> &mdash; Info</li>
539         <li><code>W</code> &mdash; Warning</li>
540         <li><code>E</code> &mdash; Error</li>
541         <li><code>F</code> &mdash; Fatal</li>
542         <li><code>S</code> &mdash; Silent (highest priority, on which nothing is ever printed)</li>
543 </ul>
544 </ul>
545
546 <p>You can obtain a list of tags used in the system, together with priorities, by running <code>logcat</code> and observing the first two columns 
547 of each message, given as <code>&lt;priority&gt;/&lt;tag&gt;</code>. </p>
548
549 <p>Here's an example of logcat output that shows that the message relates to priority level "I" and tag "ActivityManager":</p>
550
551 <pre>I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}</pre>
552
553 <p>To reduce the log output to a manageable level, you can restrict log output using <em>filter expressions</em>. Filter expressions let you indicate to the system the tags-priority combinations that you are interested in &mdash; the system suppresses other messages for the specified tags. </p>
554
555 <p>A filter expression follows this format <code>tag:priority ...</code>, where <code>tag</code> indicates the tag of interest and <code>priority</code> indicates the <em>minimum</em> level of priority to report for that tag.  Messages for that tag at or above the specified priority are written to the log. You can supply any number of <code>tag:priority</code> specifications in a single filter expression. The series of specifications is whitespace-delimited. </p>
556
557 <p>Here's an example of a filter expression that suppresses all log messages except those with the tag "ActivityManager", at priority "Info" or above, and all log messages with tag "MyApp", with priority "Debug" or above:</p>
558
559 <pre>adb logcat ActivityManager:I MyApp:D *:S</pre>
560
561 <p>The final element in the above expression, <code>*:S</code>, sets the priority level for all tags to "silent", thus ensuring only log messages with "View" and "MyApp" are displayed. Using <code>*:S</code> is an excellent way to ensure that log output is restricted to the filters that you have explicitly specified &mdash; it lets your filters serve as a "whitelist" for log output.</p>
562
563 <p>The following filter expression displays all log messages with priority level "warning" and higher, on all tags:</p>
564
565 <pre>adb logcat *:W</pre>
566
567 <p>If you're running <code>logcat</code> from your development computer (versus running it on a remote adb shell), you can also set a default filter expression by exporting a value for the environment variable <code>ANDROID_LOG_TAGS</code>:</p>
568
569 <pre>export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"</pre>
570
571 <p>Note that <code>ANDROID_LOG_TAGS</code> filter is not exported to the emulator/device instance, if you are running <code>logcat</code> from a remote shell or using <code>adb shell logcat</code>.</p>
572
573
574 <a name="outputformat"></a>
575
576 <h3>Controlling Log Output Format</h3>
577
578 <p>Log messages contain a number of metadata fields, in addition to the tag and priority. You can modify the output format for messages so that they display a specific metadata field. To do so, you use the <code>-v</code> option and specify one of the supported output formats listed below. </p>
579
580 <ul>
581     <li><code>brief</code> &mdash; Display priority/tag and PID of originating process (the default format).</li>
582         <li><code>process</code> &mdash; Display PID only.</li>
583         <li><code>tag</code> &mdash; Display the priority/tag only. </li>
584         <li><code>thread</code> &mdash; Display process:thread and priority/tag only. </li>
585         <li><code>raw</code> &mdash; Display the raw log message, with no other metadata fields.</li>
586         <li><code>time</code> &mdash; Display the date, invocation time, priority/tag, and PID of the originating process.</li>
587         <li><code>long</code> &mdash; Display all metadata fields and separate messages with a blank lines.</li>
588 </ul>
589
590 <p>When starting <code>logcat</code>, you can specify the output format you want by using the <code>-v</code> option:</p>
591
592 <pre>[adb] logcat [-v &lt;format&gt;]</pre>
593
594 <p>Here's an example that shows how to generate messages in <code>thread</code> output format: </p>
595
596 <pre>adb logcat -v thread</pre>
597
598 <p>Note that you can only specify one output format with the <code>-v</code> option. </p>
599
600 <a name="alternativebuffers"></a>
601
602 <h3>Viewing Alternative Log Buffers </h3>
603
604 <p>The Android logging system keeps multiple circular buffers for log messages, and not all of the log messages are sent to the default circular buffer. To see additional log messages, you can start <code>logcat</code> with the <code>-b</code> option, to request viewing of an alternate circular buffer. You can view any of these alternate buffers: </p>
605
606 <ul>
607 <li><code>radio</code> &mdash; View the buffer that contains radio/telephony related messages.</li>
608 <li><code>events</code> &mdash; View the buffer containing events-related messages.</li>
609 <li><code>main</code> &mdash; View the main log buffer (default)</li>
610 </ul>
611
612 <p>The usage of the <code>-b</code> option is:</p>
613
614 <pre>[adb] logcat [-b &lt;buffer&gt;]</pre>
615
616 <p>Here's an example of how to view a log buffer containing radio and telephony messages: </p>
617
618 <pre>adb logcat -b radio</b></pre>
619
620 <a name="stdout"></a>
621
622 <h3>Viewing stdout and stderr</h3>
623
624 <p>By default, the Android system sends <code>stdout</code> and <code>stderr</code> (<code>System.out</code> and <code>System.err</code>) output to <code>/dev/null</code>. In
625 processes that run the Dalvik VM, you can have the system write a copy of the output to the log file. In this case, the system writes the messages to the log using the log tags <code>stdout</code> and <code>stderr</code>, both with priority <code>I</code>. </p>
626
627 <p>To route the output in this way, you stop a running emulator/device instance and then use the shell command <code>setprop</code> to enable the redirection of output. Here's how you do it: </p>
628
629 <pre>$ adb shell stop
630 $ adb shell setprop log.redirect-stdio true
631 $ adb shell start</pre>
632
633 <p>The system retains this setting until you terminate the emulator/device instance. To use the setting as a default on the emulator/device instance, you can add an entry to <code>/data/local.prop</code>
634 on the device.</p>
635
636 <a name="logcatoptions"></a>
637
638 <h3>Listing of logcat Command Options</h3>
639
640 <table>
641 <tr>
642         <th>Option</th>
643         <th>Description</th>
644 </tr>
645
646 <tr>
647 <td><code>-b&nbsp;&lt;buffer&gt;</code></td>
648 <td>Loads an alternate log buffer for viewing, such as <code>event</code> or <code>radio</code>. The <code>main</code> buffer is used by default. See <a href="#alternativebuffers">Viewing Alternative Log Buffers</a>.</td>
649 </tr>
650
651 <tr>
652 <td><code>-c</code></td>
653 <td>Clears (flushes) the entire log and exits. </td>
654 </tr>
655
656 <tr>
657 <td><code>-d</code></td>
658 <td>Dumps the log to the screen and exits.</td>
659 </tr>
660
661 <tr>
662 <td><code>-f&nbsp;&lt;filename&gt;</code></td>
663 <td>Writes log message output to <code>&lt;filename&gt;</code>. The default is <code>stdout</code>.</td>
664 </tr>
665
666 <tr>
667 <td><code>-g</code></td>
668 <td>Prints the size of the specified log buffer and exits. </td>
669 </tr>
670
671 <tr>
672 <td><code>-n&nbsp;&lt;count&gt;</code></td>
673 <td>Sets the maximum number of rotated logs to <code>&lt;count&gt;</code>. The default value is 4.  Requires the <code>-r</code> option.  </td>
674 </tr>
675
676 <tr>
677 <td><code>-r&nbsp;&lt;kbytes&gt;</code></td>
678 <td>Rotates the log file every <code>&lt;kbytes&gt;</code> of output. The default value is 16.  Requires the <code>-f</code> option.  </td>
679 </tr>
680
681 <tr>
682 <td><code>-s</code></td>
683 <td>Sets the default filter spec to silent. </td>
684 </tr>
685
686 <tr>
687 <td><code>-v&nbsp;&lt;format&gt;</code></td>
688 <td>Sets the output format for log messages. The default is <code>brief</code> format. For a list of supported formats, see <a href="#outputformat">Controlling Log Output Format</a>.</td>
689 </tr>
690
691 </table>
692
693 <a name="stopping"></a>
694
695 <h2>Stopping the adb Server</h2>
696
697 <p>In some cases, you might need to terminate the adb server process and then restart it. For example, if adb does not respond to a command, you can terminate the server and restart it and that may resolve the problem. </p>
698
699 <p>To stop the adb server, use the <code>kill-server</code>. You can then restart the server by issuing any adb command. </p>
700
701