blob: 705b5388cd41a1caf6dd41c22c788fd73ba321bb [file] [log] [blame] [view]
nyquistc75738d2016-09-13 19:25:011# Android Debugging Instructions
2
3Chrome on Android has java and c/c++ code. Each "side" have its own set of tools
4for debugging. Here's some tips.
5
6[TOC]
7
nyquistc75738d2016-09-13 19:25:018## Launching the app
9
Andrew Grievec81af4a2017-07-26 18:02:1310You can launch the app by using one of the wrappers.
nyquistc75738d2016-09-13 19:25:0111
12```shell
Andrew Grievec81af4a2017-07-26 18:02:1313out/Default/bin/content_shell_apk launch [--args='--foo --bar'] 'data:text/html;utf-8,Hello World!'
14out/Default/bin/chrome_public_apk launch [--args='--foo --bar'] 'data:text/html;utf-8,Hello World!'
nyquistc75738d2016-09-13 19:25:0115```
16
17## Log output
18
19[Chromium logging from LOG(INFO)](https://chromium.googlesource.com/chromium/src/+/master/docs/android_logging.md)
20etc., is directed to the Android logcat logging facility. You can filter the
21messages, e.g. view chromium verbose logging, everything else at warning level
22with:
23
24```shell
25adb logcat chromium:V cr.SomeComponent:V *:W
Andrew Grievec81af4a2017-07-26 18:02:1326# or:
27out/Default/bin/chrome_public_apk logcat
nyquistc75738d2016-09-13 19:25:0128```
29
30### Warnings for Blink developers
31
32* **Do not use fprintf or printf debugging!** This does not
33 redirect to logcat.
34
35* Redirecting stdio to logcat, as documented
36 [here](https://developer.android.com/studio/command-line/logcat.html#viewingStd),
37 has a bad side-effect that it breaks `adb_install.py`. See
38 [here for details](http://stackoverflow.com/questions/28539676/android-adb-fails-to-install-apk-to-nexus-5-on-windows-8-1).
39
40## Take a screenshot
41
42While your phone is plugged into USB, use the `screenshot.py` tool in
43`build/android`. `envsetup.sh` should have put it in your path.
44
45```shell
46build/android/screenshot.py /tmp/screenshot.png
47```
48
49## Inspecting the view hierarchy
50
51You can use either
52[hierarchy viewer](https://developer.android.com/studio/profile/hierarchy-viewer-setup.html)
53or [monitor](https://developer.android.com/studio/profile/monitor.html) to see
54the Android view hierarchy and see the layout and drawing properties associated
55with it.
56
57While your phone is plugged into USB, you can inspect the Android view hierarchy
58using the following command:
59
60```shell
61ANDROID_HVPROTO=ddm monitor
62```
63
64Setting `ANDROID_HVPROTO` allows you to inspect debuggable apps on non-rooted
65devices. When building a local version of Chromium, the build tools
66automatically add `android:debuggable=true` to the `AndroidManifest.xml`, which
67will allow you to inspect them on rooted devices.
68
69Want to add some additional information to your Views? You can do that by
70adding the
71[@ViewDebug.ExportedProperty](https://developer.android.com/reference/android/view/ViewDebug.ExportedProperty.html)
72annotation.
73
74Example:
75
76```java
77@ViewDebug.ExportedProperty(category="chrome")
78private int mSuperNiftyDrawingProperty;
79```
80
81## Debugging Java
82
estevenson8c9318ff2017-03-10 22:16:3583### Eclipse
nyquistc75738d2016-09-13 19:25:0184* In Eclipse, make a debug configuration of type "Remote Java Application".
85 Choose a "Name" and set "Port" to `8700`.
86
87* Make sure Eclipse Preferences > Run/Debug > Launching > "Build (if required)
88 before launching" is unchecked.
89
90* Run Android Device Monitor:
91
92 ```shell
93 third_party/android_tools/sdk/tools/monitor
94 ```
95
96* Now select the process you want to debug in Device Monitor (the port column
97 should now mention 8700 or xxxx/8700).
98
99* Run your debug configuration, and switch to the Debug perspective.
100
estevenson8c9318ff2017-03-10 22:16:35101### Android Studio
102* Build and install the desired target
103
104* Click the "Attach debugger to Android process" (see
105[here](https://developer.android.com/studio/debug/index.html) for more)
106
nyquistc75738d2016-09-13 19:25:01107## Waiting for Java Debugger on Early Startup
108
109* To debug early startup, pass `--wait-for-java-debugger` as a command line
110 flag.
111
112## Debugging C/C++
113
114Under `build/android`, there are a few scripts:
115
116```shell
Andrew Grievec81af4a2017-07-26 18:02:13117out/Default/bin/content_shell_apk gdb
118out/Default/bin/chrome_public_apk gdb
nyquistc75738d2016-09-13 19:25:01119```
120
121By default, these wrappers will attach to the browser process.
122
Andrew Grievec81af4a2017-07-26 18:02:13123You can also attach to the renderer process by using `--args='--sandboxed'`.
124You might need to be root on the phone for that. Run `adb root` if needed)
nyquistc75738d2016-09-13 19:25:01125
126## Waiting for Debugger on Early Startup
127
128Set the target command line flag with `--wait-for-debugger`.
129
Andrew Grievec81af4a2017-07-26 18:02:13130Launch the debugger using one of the scripts from above.
nyquistc75738d2016-09-13 19:25:01131
132Type `info threads` and look for a line like:
133
134```
13511 Thread 2564 clock_gettime () at bionic/libc/arch-arm/syscalls/clock_gettime.S:11
136```
137
138or perhaps:
139
140```
1411 Thread 10870 0x40127050 in nanosleep () from /tmp/user-adb-gdb-libs/system/lib/libc.so
142```
143
144We need to jump out of its sleep routine:
145
146```
147(gdb) thread 11
148(gdb) up
149(gdb) up
150(gdb) return
151Make base::debug::BreakDebugger() return now? (y or n) y
152(gdb) continue
153```
154
155## Symbolizing Crash Stacks and Tombstones (C++)
156
157If a crash has generated a tombstone in your device, use:
158
159```shell
160build/android/tombstones.py --output-directory out/Default
161```
162
163If you have a stack trace (from `adb logcat`) that needs to be symbolized, copy
164it into a text file and symbolize with the following command (run from
165`${CHROME_SRC}`):
166
167```shell
168third_party/android_platform/development/scripts/stack --output-directory out/Default [tombstone file | dump file]
169```
170
171`stack` can also take its input from `stdin`:
172
173```shell
174adb logcat -d | third_party/android_platform/development/scripts/stack --output-directory out/Default
175```
176
177Example:
178
179```shell
180third_party/android_platform/development/scripts/stack --output-directory out/Default ~/crashlogs/tombstone_07-build231.txt
181```
182
183## Deobfuscating Stack Traces (Java)
184
185You will need the ProGuard mapping file that was generated when the application
186that crashed was built. When building locally, these are found in:
187
188```shell
189out/Default/apks/ChromePublic.apk.mapping
agrievea350dbdb2017-07-05 15:27:17190out/Default/apks/ChromeModernPublic.apk.mapping
191etc.
nyquistc75738d2016-09-13 19:25:01192```
193
agrievea350dbdb2017-07-05 15:27:17194Build the `java_deobfuscate` tool:
nyquistc75738d2016-09-13 19:25:01195
196```shell
agrievea350dbdb2017-07-05 15:27:17197ninja -C out/Default java_deobfuscate
nyquistc75738d2016-09-13 19:25:01198```
199
agrievea350dbdb2017-07-05 15:27:17200Then run it via:
nyquistc75738d2016-09-13 19:25:01201
202```shell
agrievea350dbdb2017-07-05 15:27:17203# For a file:
204out/Default/bin/java_deobfuscate PROGUARD_MAPPING_FILE.mapping < FILE
205# For logcat:
206adb logcat | out/Default/bin/java_deobfuscate PROGUARD_MAPPING_FILE.mapping
nyquistc75738d2016-09-13 19:25:01207```
208
209## Get WebKit code to output to the adb log
210
211In your build environment:
212
213```shell
214adb root
215adb shell stop
216adb shell setprop log.redirect-stdio true
217adb shell start
218```
219
220In the source itself, use `fprintf(stderr, "message");` whenever you need to
221output a message.
222
223## Debug unit tests with GDB
224
225To run unit tests use the following command:
226
227```shell
jbudorick6a94be32017-05-11 22:38:43228out/Debug/bin/run_test_name -f --wait-for-debugger -t 6000
nyquistc75738d2016-09-13 19:25:01229```
230
231That command will cause the test process to wait until a debugger is attached.
232
233To attach a debugger:
234
235```shell
236build/android/adb_gdb --output-directory=out/Default --package-name=org.chromium.native_test
237```
238
239After attaching gdb to the process you can use it normally. For example:
240
241```
242(gdb) break main
243Breakpoint 1 at 0x9750793c: main. (2 locations)
244(gdb) continue
245```