blob: d9df558776747514e7fe00771cc610542bc4c492 [file] [log] [blame] [view]
nyquistc75738d2016-09-13 19:25:011# Android Debugging Instructions
nyquistc75738d2016-09-13 19:25:012Chrome on Android has java and c/c++ code. Each "side" have its own set of tools
3for debugging. Here's some tips.
4
5[TOC]
6
agrievee453b98f2018-10-22 14:17:177## Launching
8You can run the app by using one of the wrappers.
nyquistc75738d2016-09-13 19:25:019
10```shell
agrievee453b98f2018-10-22 14:17:1711# Installs, launches, and enters logcat.
12out/Default/bin/content_shell_apk run --args='--disable-fre' 'data:text/html;utf-8,<html>Hello World!</html>'
13# Launches without first installing. Does not show logcat.
14out/Default/bin/chrome_public_apk launch --args='--disable-fre' 'data:text/html;utf-8,<html>Hello World!</html>'
nyquistc75738d2016-09-13 19:25:0115```
16
agrievee453b98f2018-10-22 14:17:1717## Logging
nyquistc75738d2016-09-13 19:25:0118[Chromium logging from LOG(INFO)](https://chromium.googlesource.com/chromium/src/+/master/docs/android_logging.md)
19etc., is directed to the Android logcat logging facility. You can filter the
20messages, e.g. view chromium verbose logging, everything else at warning level
21with:
22
23```shell
agrievee453b98f2018-10-22 14:17:1724# Shows a coloured & filtered logcat.
25out/Default/bin/chrome_public_apk logcat [-v] # Use -v to show logs for other processes
nyquistc75738d2016-09-13 19:25:0126```
27
28### Warnings for Blink developers
nyquistc75738d2016-09-13 19:25:0129* **Do not use fprintf or printf debugging!** This does not
30 redirect to logcat.
31
32* Redirecting stdio to logcat, as documented
33 [here](https://developer.android.com/studio/command-line/logcat.html#viewingStd),
34 has a bad side-effect that it breaks `adb_install.py`. See
35 [here for details](http://stackoverflow.com/questions/28539676/android-adb-fails-to-install-apk-to-nexus-5-on-windows-8-1).
36
agrievee453b98f2018-10-22 14:17:1737## Take a Screenshot
nyquistc75738d2016-09-13 19:25:0138```shell
39build/android/screenshot.py /tmp/screenshot.png
40```
41
agrievee453b98f2018-10-22 14:17:1742## Inspecting the View Hierarchy
43Generate an [Android Studio](android_studio.md) project, and then use
44[Layout Inspector](https://developer.android.com/studio/debug/layout-inspector).
nyquistc75738d2016-09-13 19:25:0145
46## Debugging Java
Andrew Grieve4fe99742017-11-23 19:43:1647For both apk and test targets, pass `--wait-for-java-debugger` to the wrapper
48scripts.
49
50Examples:
51
52```shell
53# Install, launch, and wait:
54out/Default/bin/chrome_public_apk run --wait-for-java-debugger
55
56# Launch, and have GPU process wait rather than Browser process:
57out/Default/bin/chrome_public_apk launch --wait-for-java-debugger --debug-process-name privileged_process0
58
59# Have Renderers wait:
60out/Default/bin/chrome_public_apk launch --args="--renderer-wait-for-java-debugger"
61
62# Have tests wait:
63out/Default/bin/run_chrome_public_test_apk --wait-for-java-debugger
64out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999
65```
66
67### Android Studio
68* Open Android Studio ([instructions](android_studio.md))
69* Click "Run"->"Attach debugger to Android process" (see
Wei-Yin Chen (陳威尹)0f8750b32017-12-08 21:42:1570[here](https://developer.android.com/studio/debug/index.html) for more).
agrievee453b98f2018-10-22 14:17:1771* Click "Run"->"Attach to Local Process..." for Robolectric junit tests.
Andrew Grieve4fe99742017-11-23 19:43:1672
estevenson8c9318ff2017-03-10 22:16:3573### Eclipse
nyquistc75738d2016-09-13 19:25:0174* In Eclipse, make a debug configuration of type "Remote Java Application".
75 Choose a "Name" and set "Port" to `8700`.
76
77* Make sure Eclipse Preferences > Run/Debug > Launching > "Build (if required)
78 before launching" is unchecked.
79
80* Run Android Device Monitor:
81
82 ```shell
Yun Liuf57cceaf2019-03-18 21:31:2383 third_party/android_sdk/public/tools/monitor
nyquistc75738d2016-09-13 19:25:0184 ```
85
86* Now select the process you want to debug in Device Monitor (the port column
87 should now mention 8700 or xxxx/8700).
88
89* Run your debug configuration, and switch to the Debug perspective.
90
nyquistc75738d2016-09-13 19:25:0191## Debugging C/C++
agrievee453b98f2018-10-22 14:17:1792While the app is running, use the wrapper script's `gdb` command to enter into a
93gdb shell.
nyquistc75738d2016-09-13 19:25:0194
agrievee453b98f2018-10-22 14:17:1795When running with gdb attached, the app runs **extremely slowly**.
nyquistc75738d2016-09-13 19:25:0196
97```shell
Andrew Grieve4fe99742017-11-23 19:43:1698# Attaches to browser process.
Andrew Grievec81af4a2017-07-26 18:02:1399out/Default/bin/content_shell_apk gdb
100out/Default/bin/chrome_public_apk gdb
Andrew Grieve4fe99742017-11-23 19:43:16101
102# Attaches to gpu process.
103out/Default/bin/chrome_public_apk gdb --debug-process-name privileged_process0
104
105# Attach to other processes ("chrome_public_apk ps" to show pids).
106out/Default/bin/chrome_public_apk gdb --pid $PID
nyquistc75738d2016-09-13 19:25:01107```
108
agrievee453b98f2018-10-22 14:17:17109When connecting, gdb will complain of not being able to load a lot of libraries.
110This happens because of java code. The following messages are all expected:
111```
112Connecting to :5039...
113warning: Could not load shared library symbols for 211 libraries, e.g. /system/framework/arm/boot.oat.
114Use the "info sharedlibrary" command to see the complete listing.
115Do you need "set solib-search-path" or "set sysroot"?
116Failed to read a valid object file image from memory.
117```
118
119### Using Visual Studio Code
120While the app is running, run the `gdb` command with `--ide`:
121
122```shell
123out/Default/bin/content_shell_apk gdb --ide
124```
125
126Once the script has done its thing (generally ~1 second after the initial
127time its used), open [vscode.md](vscode.md) and ensure you have the
128[Android launch entry](vscode.md#Launch-Commands).
129
130Connect via the IDE's launch entry. Connecting takes 30-40 seconds.
131
132When troubleshooting, it's helpful to enable
133[engine logging](https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md#enginelogging).
134
135Known Issues:
136 * Pretty printers are not working properly.
137
Andrew Grieve4fe99742017-11-23 19:43:16138### Waiting for Debugger on Early Startup
agrievee453b98f2018-10-22 14:17:17139```shell
140# Install, launch, and wait:
141out/Default/bin/chrome_public_apk run --args="--wait-for-debugger"
142# Launch, and have GPU process wait rather than Browser process:
143out/Default/bin/chrome_public_apk launch --args="--wait-for-debugger-children=gpu-process"
144# Or for renderers:
145out/Default/bin/chrome_public_apk launch --args="--wait-for-debugger-children=renderer"
nyquistc75738d2016-09-13 19:25:01146```
147
agrievee453b98f2018-10-22 14:17:17148#### With an IDE
149Once `gdb` attaches, the app will resume execution, so you must set your
150breakpoint before attaching.
nyquistc75738d2016-09-13 19:25:01151
agrievee453b98f2018-10-22 14:17:17152#### With Command-line GDB
153Once attached, gdb will drop into a prompt. Set your breakpoints and run "c" to
154continue.
nyquistc75738d2016-09-13 19:25:01155
156## Symbolizing Crash Stacks and Tombstones (C++)
157
158If a crash has generated a tombstone in your device, use:
159
160```shell
161build/android/tombstones.py --output-directory out/Default
162```
163
164If you have a stack trace (from `adb logcat`) that needs to be symbolized, copy
165it into a text file and symbolize with the following command (run from
166`${CHROME_SRC}`):
167
168```shell
169third_party/android_platform/development/scripts/stack --output-directory out/Default [tombstone file | dump file]
170```
171
172`stack` can also take its input from `stdin`:
173
174```shell
175adb logcat -d | third_party/android_platform/development/scripts/stack --output-directory out/Default
176```
177
178Example:
179
180```shell
181third_party/android_platform/development/scripts/stack --output-directory out/Default ~/crashlogs/tombstone_07-build231.txt
182```
183
184## Deobfuscating Stack Traces (Java)
185
186You will need the ProGuard mapping file that was generated when the application
187that crashed was built. When building locally, these are found in:
188
189```shell
190out/Default/apks/ChromePublic.apk.mapping
agrievea350dbdb2017-07-05 15:27:17191out/Default/apks/ChromeModernPublic.apk.mapping
192etc.
nyquistc75738d2016-09-13 19:25:01193```
194
Sami Kyostila3269af12019-07-02 19:02:45195When debugging a failing test on the build waterfall, you can find the mapping
196file as follows:
197
1981. Open buildbot page for the failing build (e.g.,
199 https://ci.chromium.org/p/chrome/builders/ci/android-go-perf/1234).
2002. Open the swarming page for the failing shard (e.g., shard #3).
2013. Click on "Isolated Inputs" to locate the files the shard used to run the
202 test.
2034. Download the `.mapping` file for the APK used by the test (e.g.,
204 `ChromePublic.apk.mapping`). Note that you may need to use the
205 `tools/swarming_client/isolateserver.py` script to download the mapping
206 file if it's too big. The viewer will provide instructions for this.
207
Andrew Grieveabcac41a2019-08-14 17:16:18208**Googlers Only**: For official build mapping files, see
209[go/chromejavadeobfuscation](https://goto.google.com/chromejavadeobfuscation).
210
211Once you have a .mapping file, build the `java_deobfuscate` tool:
nyquistc75738d2016-09-13 19:25:01212
213```shell
agrievea350dbdb2017-07-05 15:27:17214ninja -C out/Default java_deobfuscate
nyquistc75738d2016-09-13 19:25:01215```
216
agrievea350dbdb2017-07-05 15:27:17217Then run it via:
nyquistc75738d2016-09-13 19:25:01218
219```shell
agrievea350dbdb2017-07-05 15:27:17220# For a file:
221out/Default/bin/java_deobfuscate PROGUARD_MAPPING_FILE.mapping < FILE
222# For logcat:
223adb logcat | out/Default/bin/java_deobfuscate PROGUARD_MAPPING_FILE.mapping
nyquistc75738d2016-09-13 19:25:01224```
225
226## Get WebKit code to output to the adb log
227
228In your build environment:
229
230```shell
231adb root
232adb shell stop
233adb shell setprop log.redirect-stdio true
234adb shell start
235```
236
237In the source itself, use `fprintf(stderr, "message");` whenever you need to
238output a message.
239
240## Debug unit tests with GDB
241
242To run unit tests use the following command:
243
244```shell
jbudorick6a94be32017-05-11 22:38:43245out/Debug/bin/run_test_name -f <test_filter_if_any> --wait-for-debugger -t 6000
nyquistc75738d2016-09-13 19:25:01246```
247
248That command will cause the test process to wait until a debugger is attached.
249
250To attach a debugger:
251
252```shell
253build/android/adb_gdb --output-directory=out/Default --package-name=org.chromium.native_test
254```
255
256After attaching gdb to the process you can use it normally. For example:
257
258```
259(gdb) break main
260Breakpoint 1 at 0x9750793c: main. (2 locations)
261(gdb) continue
262```