nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 1 | # Android Debugging Instructions |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 2 | Chrome on Android has java and c/c++ code. Each "side" have its own set of tools |
| 3 | for debugging. Here's some tips. |
| 4 | |
| 5 | [TOC] |
| 6 | |
Victor Hugo Vianna Silva | 12ddacb | 2020-09-18 01:29:25 | [diff] [blame] | 7 | ## Instructions for Google Employees |
| 8 | |
| 9 | See also |
| 10 | [go/clankium/06-debugging-clank](https://goto.google.com/clankium/06-debugging-clank). |
| 11 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 12 | ## Launching |
| 13 | You can run the app by using one of the wrappers. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 14 | |
| 15 | ```shell |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 16 | # Installs, launches, and enters logcat. |
| 17 | out/Default/bin/content_shell_apk run --args='--disable-fre' 'data:text/html;utf-8,<html>Hello World!</html>' |
| 18 | # Launches without first installing. Does not show logcat. |
| 19 | out/Default/bin/chrome_public_apk launch --args='--disable-fre' 'data:text/html;utf-8,<html>Hello World!</html>' |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 20 | ``` |
| 21 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 22 | ## Logging |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 23 | [Chromium logging from LOG(INFO)](https://chromium.googlesource.com/chromium/src/+/master/docs/android_logging.md) |
| 24 | etc., is directed to the Android logcat logging facility. You can filter the |
| 25 | messages, e.g. view chromium verbose logging, everything else at warning level |
| 26 | with: |
| 27 | |
| 28 | ```shell |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 29 | # Shows a coloured & filtered logcat. |
| 30 | out/Default/bin/chrome_public_apk logcat [-v] # Use -v to show logs for other processes |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 31 | ``` |
| 32 | |
David Van Cleve | e0021d3 | 2020-01-29 16:02:58 | [diff] [blame] | 33 | If this doesn't display the logs you're looking for, try `adb logcat` with your system `adb` |
| 34 | or the one in `//third_party/android_sdk/`. |
| 35 | |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 36 | ### Warnings for Blink developers |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 37 | * **Do not use fprintf or printf debugging!** This does not |
| 38 | redirect to logcat. |
| 39 | |
| 40 | * Redirecting stdio to logcat, as documented |
| 41 | [here](https://developer.android.com/studio/command-line/logcat.html#viewingStd), |
| 42 | has a bad side-effect that it breaks `adb_install.py`. See |
| 43 | [here for details](http://stackoverflow.com/questions/28539676/android-adb-fails-to-install-apk-to-nexus-5-on-windows-8-1). |
| 44 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 45 | ## Take a Screenshot |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 46 | ```shell |
| 47 | build/android/screenshot.py /tmp/screenshot.png |
| 48 | ``` |
| 49 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 50 | ## Inspecting the View Hierarchy |
| 51 | Generate an [Android Studio](android_studio.md) project, and then use |
| 52 | [Layout Inspector](https://developer.android.com/studio/debug/layout-inspector). |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 53 | |
| 54 | ## Debugging Java |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 55 | For both apk and test targets, pass `--wait-for-java-debugger` to the wrapper |
| 56 | scripts. |
| 57 | |
| 58 | Examples: |
| 59 | |
| 60 | ```shell |
| 61 | # Install, launch, and wait: |
| 62 | out/Default/bin/chrome_public_apk run --wait-for-java-debugger |
| 63 | |
| 64 | # Launch, and have GPU process wait rather than Browser process: |
| 65 | out/Default/bin/chrome_public_apk launch --wait-for-java-debugger --debug-process-name privileged_process0 |
| 66 | |
| 67 | # Have Renderers wait: |
| 68 | out/Default/bin/chrome_public_apk launch --args="--renderer-wait-for-java-debugger" |
| 69 | |
| 70 | # Have tests wait: |
| 71 | out/Default/bin/run_chrome_public_test_apk --wait-for-java-debugger |
| 72 | out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999 |
| 73 | ``` |
| 74 | |
| 75 | ### Android Studio |
| 76 | * Open Android Studio ([instructions](android_studio.md)) |
| 77 | * Click "Run"->"Attach debugger to Android process" (see |
Wei-Yin Chen (陳威尹) | 0f8750b3 | 2017-12-08 21:42:15 | [diff] [blame] | 78 | [here](https://developer.android.com/studio/debug/index.html) for more). |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 79 | * Click "Run"->"Attach to Local Process..." for Robolectric junit tests. |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 80 | |
estevenson | 8c9318ff | 2017-03-10 22:16:35 | [diff] [blame] | 81 | ### Eclipse |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 82 | * In Eclipse, make a debug configuration of type "Remote Java Application". |
| 83 | Choose a "Name" and set "Port" to `8700`. |
| 84 | |
| 85 | * Make sure Eclipse Preferences > Run/Debug > Launching > "Build (if required) |
| 86 | before launching" is unchecked. |
| 87 | |
| 88 | * Run Android Device Monitor: |
| 89 | |
| 90 | ```shell |
Yun Liu | f57cceaf | 2019-03-18 21:31:23 | [diff] [blame] | 91 | third_party/android_sdk/public/tools/monitor |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 92 | ``` |
| 93 | |
| 94 | * Now select the process you want to debug in Device Monitor (the port column |
| 95 | should now mention 8700 or xxxx/8700). |
| 96 | |
| 97 | * Run your debug configuration, and switch to the Debug perspective. |
| 98 | |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 99 | ## Debugging C/C++ |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 100 | While the app is running, use the wrapper script's `gdb` command to enter into a |
| 101 | gdb shell. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 102 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 103 | When running with gdb attached, the app runs **extremely slowly**. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 104 | |
| 105 | ```shell |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 106 | # Attaches to browser process. |
Andrew Grieve | c81af4a | 2017-07-26 18:02:13 | [diff] [blame] | 107 | out/Default/bin/content_shell_apk gdb |
| 108 | out/Default/bin/chrome_public_apk gdb |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 109 | |
| 110 | # Attaches to gpu process. |
| 111 | out/Default/bin/chrome_public_apk gdb --debug-process-name privileged_process0 |
| 112 | |
| 113 | # Attach to other processes ("chrome_public_apk ps" to show pids). |
| 114 | out/Default/bin/chrome_public_apk gdb --pid $PID |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 115 | ``` |
| 116 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 117 | When connecting, gdb will complain of not being able to load a lot of libraries. |
| 118 | This happens because of java code. The following messages are all expected: |
| 119 | ``` |
| 120 | Connecting to :5039... |
| 121 | warning: Could not load shared library symbols for 211 libraries, e.g. /system/framework/arm/boot.oat. |
| 122 | Use the "info sharedlibrary" command to see the complete listing. |
| 123 | Do you need "set solib-search-path" or "set sysroot"? |
| 124 | Failed to read a valid object file image from memory. |
| 125 | ``` |
| 126 | |
Stefan Zager | e2b55cc | 2019-10-04 19:57:54 | [diff] [blame] | 127 | If you have ever run an ASAN build of chromium on the device, you may get |
| 128 | an error like the following when you start up gdb: |
| 129 | ``` |
| 130 | /tmp/<username>-adb-gdb-tmp-<pid>/gdb.init:11: Error in sourced command file: |
| 131 | "/tmp/<username>-adb-gdb-tmp-<pid>/app_process32": not in executable format: file format not recognized |
| 132 | ``` |
| 133 | If this happens, run the following command and try again: |
| 134 | ```shell |
| 135 | $ src/android/asan/third_party/asan_device_setup.sh --revert |
| 136 | ``` |
| 137 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 138 | ### Using Visual Studio Code |
| 139 | While the app is running, run the `gdb` command with `--ide`: |
| 140 | |
| 141 | ```shell |
| 142 | out/Default/bin/content_shell_apk gdb --ide |
| 143 | ``` |
| 144 | |
| 145 | Once the script has done its thing (generally ~1 second after the initial |
| 146 | time its used), open [vscode.md](vscode.md) and ensure you have the |
| 147 | [Android launch entry](vscode.md#Launch-Commands). |
| 148 | |
| 149 | Connect via the IDE's launch entry. Connecting takes 30-40 seconds. |
| 150 | |
| 151 | When troubleshooting, it's helpful to enable |
| 152 | [engine logging](https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md#enginelogging). |
| 153 | |
| 154 | Known Issues: |
| 155 | * Pretty printers are not working properly. |
| 156 | |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 157 | ### Waiting for Debugger on Early Startup |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 158 | ```shell |
| 159 | # Install, launch, and wait: |
| 160 | out/Default/bin/chrome_public_apk run --args="--wait-for-debugger" |
| 161 | # Launch, and have GPU process wait rather than Browser process: |
| 162 | out/Default/bin/chrome_public_apk launch --args="--wait-for-debugger-children=gpu-process" |
| 163 | # Or for renderers: |
| 164 | out/Default/bin/chrome_public_apk launch --args="--wait-for-debugger-children=renderer" |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 165 | ``` |
| 166 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 167 | #### With an IDE |
| 168 | Once `gdb` attaches, the app will resume execution, so you must set your |
| 169 | breakpoint before attaching. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 170 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 171 | #### With Command-line GDB |
| 172 | Once attached, gdb will drop into a prompt. Set your breakpoints and run "c" to |
| 173 | continue. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 174 | |
| 175 | ## Symbolizing Crash Stacks and Tombstones (C++) |
| 176 | |
| 177 | If a crash has generated a tombstone in your device, use: |
| 178 | |
| 179 | ```shell |
| 180 | build/android/tombstones.py --output-directory out/Default |
| 181 | ``` |
| 182 | |
| 183 | If you have a stack trace (from `adb logcat`) that needs to be symbolized, copy |
| 184 | it into a text file and symbolize with the following command (run from |
| 185 | `${CHROME_SRC}`): |
| 186 | |
| 187 | ```shell |
| 188 | third_party/android_platform/development/scripts/stack --output-directory out/Default [tombstone file | dump file] |
| 189 | ``` |
| 190 | |
| 191 | `stack` can also take its input from `stdin`: |
| 192 | |
| 193 | ```shell |
| 194 | adb logcat -d | third_party/android_platform/development/scripts/stack --output-directory out/Default |
| 195 | ``` |
| 196 | |
| 197 | Example: |
| 198 | |
| 199 | ```shell |
| 200 | third_party/android_platform/development/scripts/stack --output-directory out/Default ~/crashlogs/tombstone_07-build231.txt |
| 201 | ``` |
| 202 | |
| 203 | ## Deobfuscating Stack Traces (Java) |
| 204 | |
| 205 | You will need the ProGuard mapping file that was generated when the application |
| 206 | that crashed was built. When building locally, these are found in: |
| 207 | |
| 208 | ```shell |
| 209 | out/Default/apks/ChromePublic.apk.mapping |
agrieve | a350dbdb | 2017-07-05 15:27:17 | [diff] [blame] | 210 | etc. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 211 | ``` |
| 212 | |
Sami Kyostila | 3269af1 | 2019-07-02 19:02:45 | [diff] [blame] | 213 | When debugging a failing test on the build waterfall, you can find the mapping |
| 214 | file as follows: |
| 215 | |
| 216 | 1. Open buildbot page for the failing build (e.g., |
| 217 | https://ci.chromium.org/p/chrome/builders/ci/android-go-perf/1234). |
| 218 | 2. Open the swarming page for the failing shard (e.g., shard #3). |
| 219 | 3. Click on "Isolated Inputs" to locate the files the shard used to run the |
| 220 | test. |
| 221 | 4. Download the `.mapping` file for the APK used by the test (e.g., |
| 222 | `ChromePublic.apk.mapping`). Note that you may need to use the |
Takuto Ikuta | 93b8eb80 | 2020-01-30 12:11:28 | [diff] [blame] | 223 | `tools/luci-go/isolated` to download the mapping file if it's too big. The |
| 224 | viewer will provide instructions for this. |
Sami Kyostila | 3269af1 | 2019-07-02 19:02:45 | [diff] [blame] | 225 | |
Andrew Grieve | abcac41a | 2019-08-14 17:16:18 | [diff] [blame] | 226 | **Googlers Only**: For official build mapping files, see |
| 227 | [go/chromejavadeobfuscation](https://goto.google.com/chromejavadeobfuscation). |
| 228 | |
Andrew Grieve | 17a5965 | 2020-03-19 18:14:53 | [diff] [blame] | 229 | Once you have a .mapping file: |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 230 | |
| 231 | ```shell |
agrieve | a350dbdb | 2017-07-05 15:27:17 | [diff] [blame] | 232 | # For a file: |
Andrew Grieve | 17a5965 | 2020-03-19 18:14:53 | [diff] [blame] | 233 | build/android/stacktrace/java_deobfuscate.py PROGUARD_MAPPING_FILE.mapping < FILE |
agrieve | a350dbdb | 2017-07-05 15:27:17 | [diff] [blame] | 234 | # For logcat: |
Andrew Grieve | 17a5965 | 2020-03-19 18:14:53 | [diff] [blame] | 235 | adb logcat | build/android/stacktrace/java_deobfuscate.py PROGUARD_MAPPING_FILE.mapping |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 236 | ``` |
| 237 | |
| 238 | ## Get WebKit code to output to the adb log |
| 239 | |
| 240 | In your build environment: |
| 241 | |
| 242 | ```shell |
| 243 | adb root |
| 244 | adb shell stop |
| 245 | adb shell setprop log.redirect-stdio true |
| 246 | adb shell start |
| 247 | ``` |
| 248 | |
| 249 | In the source itself, use `fprintf(stderr, "message");` whenever you need to |
| 250 | output a message. |
| 251 | |
| 252 | ## Debug unit tests with GDB |
| 253 | |
| 254 | To run unit tests use the following command: |
| 255 | |
| 256 | ```shell |
jbudorick | 6a94be3 | 2017-05-11 22:38:43 | [diff] [blame] | 257 | out/Debug/bin/run_test_name -f <test_filter_if_any> --wait-for-debugger -t 6000 |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 258 | ``` |
| 259 | |
| 260 | That command will cause the test process to wait until a debugger is attached. |
| 261 | |
| 262 | To attach a debugger: |
| 263 | |
| 264 | ```shell |
| 265 | build/android/adb_gdb --output-directory=out/Default --package-name=org.chromium.native_test |
| 266 | ``` |
| 267 | |
| 268 | After attaching gdb to the process you can use it normally. For example: |
| 269 | |
| 270 | ``` |
| 271 | (gdb) break main |
| 272 | Breakpoint 1 at 0x9750793c: main. (2 locations) |
| 273 | (gdb) continue |
| 274 | ``` |