blob: 96103a5cd7a9137971cc10e7bddb54fbc735eff9 [file] [log] [blame]
David Warren94a41a52009-07-15 16:43:07 -07001page.title=Debugging with tcpdump and other tools
2pdk.version=1.0
3@jd:body
4
5
6<a name="toc"/>
7<div style="padding:10px">
8 <a href="#installing">Installing tcpdump</a><BR>
9 <a href="#running">Running tcpdump</a><br/>
10 <a href="#other">Other network debugging commands</a><br/>
11</div>
12
13<a name="installing"></a>
14<h3>Installing tcpdump</h3>
15<h4>Pushing the binary to an existing device</h4>
16<p>Download tcpdump from <a href="http://www.tcpdump.org/">http://www.tcpdump.org/</a>, then execute:</p>
17<pre>
18adb root
19adb remount
20adb push /wherever/you/put/tcpdump /system/xbin/tcpdump
21adb shell chmod 6755 /data/local/tmp/tcpdump
22</pre>
23<h4>Including tcpdump in the build image</h4>
24<p>If you are running your own build, execute:</p>
25<pre>
26mmm external/tcpdump # install the binary in out/.../system/xbin
27make snod # build a new system.img that includes it
28</pre>
29<p>Flash the device as usual, for example, <code>fastboot flashball</code>.</p>
30<p>If you want to build tcpdump by default, add <code>CUSTOM_TARGETS += tcpdump</code> to your <code>buildspec.mk</code>.</p>
31<h3><a name="running"></a>Running tcpdump</h3>
32<p>You need to have root access on your device. </p>
33<h4>Batch mode capture</h4>
34<p>The typical procedure is to capture packets to a file and then examine the file on the desktop, as illustrated below:</p>
35<pre>
36adb shell tcpdump -i any -p -s 0 -w /sdcard/capture.pcap
37# "-i any": listen on any network interface
38# "-p": disable promiscuous mode (doesn't work anyway)
39# "-s 0": capture the entire packet
40# "-w": write packets to a file (rather than printing to stdout)
41
42 ... do whatever you want to capture, then ^C to stop it ...
43
44adb pull /sdcard/capture.pcap .
45sudo apt-get install wireshark # or ethereal, if you're still on dapper
46wireshark capture.pcap # or ethereal
47
48 ... look at your packets and be wise ...
49</pre>
50<p>You can run <code>tcpdump</code> in the background from an interactive shell or from Terminal. By default, <code>tcpdump</code> captures all traffic without filtering. If you prefer, add an expression like port 80 to the <code>tcpdump</code> command line.</p>
51<h4>Real time packet monitoring</h4>
52<p>Execute the following if you would like to watch packets go by rather than capturing them to a file (<code>-n</code> skips DNS lookups. <code>-s 0</code> captures the entire packet rather than just the header):</p>
53<pre>
54adb shell tcpdump -n -s 0
55</pre>
56<p>Typical <code>tcpdump</code> options apply. For example, if you want to see HTTP traffic:</p>
57<pre>
58adb shell tcpdump -X -n -s 0 port 80
59</pre>
60<p>You can also monitor packets with <code>wireshark</code> or <code>ethereal</code>, as shown below:</p>
61<pre>
62# In one shell, start tcpdump.
63adb shell "tcpdump -n -s 0 -w - | nc -l -p 11233"
64
65# In a separate shell, forward data and run ethereal.
66adb forward tcp:11233 tcp:11233 && nc 127.0.0.1 11233 | ethereal -k -S -i -
67</pre>
68<p>Note that you can't restart capture via <code>ethereal</code>. If anything goes wrong, you will need to rerun both commands.</p>
69<p>For more immediate output, add <code>-l</code> to the <code>tcpdump</code> command line, but this can cause <code>adb</code> to choke (it helps to use a nonzero argument for <code>-s</code> to limit the amount of data captured per packet; <code>-s 100</code> is sufficient if you just want to see headers).</p>
70<h4>Disabling encryption</h4>
71<p>If your service runs over <code>https</code>, <code>tcpdump</code> is of limited use. In this case, you can rewrite some service URLs to use <code>http</code>, for example:</p>
72<pre>
73vendor/google/tools/override-gservices url:calendar_sync_https_proxy \
74 https://www.google.com/calendar rewrite http://android.clients.google.com/proxy/calendar
75</pre>
76<h3><a name="other"></a>Other network debugging commands</h3>
77<h4>On the device:</h4>
78<ul>
79 <li><code>ifconfig interface</code>: note that unlike Linux, you need to give <code>ifconfig</code> an argument</li>
80 <li><code>netcfg</code>: lists interfaces and IP addresses</li>
81 <li><code>iftop</code>: like top for network</li>
82 <li><code>route</code>: examine the routing table</li>
83 <li><code>netstat</code>: see active network connections</li>
84 <li><code>nc</code>: <code>netcat</code> connection utility</li>
85</ul>
86<h4>On the desktop:</h4>
87<ul>
88 <li> <code>curl</code>: fetch URLs directly to emulate device requests</li>
89</ul>