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 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 7 | ## Launching |
| 8 | You can run the app by using one of the wrappers. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 9 | |
| 10 | ```shell |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 11 | # Installs, launches, and enters logcat. |
| 12 | out/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. |
| 14 | 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] | 15 | ``` |
| 16 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 17 | ## Logging |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 18 | [Chromium logging from LOG(INFO)](https://chromium.googlesource.com/chromium/src/+/master/docs/android_logging.md) |
| 19 | etc., is directed to the Android logcat logging facility. You can filter the |
| 20 | messages, e.g. view chromium verbose logging, everything else at warning level |
| 21 | with: |
| 22 | |
| 23 | ```shell |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 24 | # Shows a coloured & filtered logcat. |
| 25 | 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] | 26 | ``` |
| 27 | |
| 28 | ### Warnings for Blink developers |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 29 | * **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 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 37 | ## Take a Screenshot |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 38 | ```shell |
| 39 | build/android/screenshot.py /tmp/screenshot.png |
| 40 | ``` |
| 41 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 42 | ## Inspecting the View Hierarchy |
| 43 | Generate an [Android Studio](android_studio.md) project, and then use |
| 44 | [Layout Inspector](https://developer.android.com/studio/debug/layout-inspector). |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 45 | |
| 46 | ## Debugging Java |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 47 | For both apk and test targets, pass `--wait-for-java-debugger` to the wrapper |
| 48 | scripts. |
| 49 | |
| 50 | Examples: |
| 51 | |
| 52 | ```shell |
| 53 | # Install, launch, and wait: |
| 54 | out/Default/bin/chrome_public_apk run --wait-for-java-debugger |
| 55 | |
| 56 | # Launch, and have GPU process wait rather than Browser process: |
| 57 | out/Default/bin/chrome_public_apk launch --wait-for-java-debugger --debug-process-name privileged_process0 |
| 58 | |
| 59 | # Have Renderers wait: |
| 60 | out/Default/bin/chrome_public_apk launch --args="--renderer-wait-for-java-debugger" |
| 61 | |
| 62 | # Have tests wait: |
| 63 | out/Default/bin/run_chrome_public_test_apk --wait-for-java-debugger |
| 64 | out/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 (陳威尹) | 0f8750b3 | 2017-12-08 21:42:15 | [diff] [blame] | 70 | [here](https://developer.android.com/studio/debug/index.html) for more). |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 71 | * Click "Run"->"Attach to Local Process..." for Robolectric junit tests. |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 72 | |
estevenson | 8c9318ff | 2017-03-10 22:16:35 | [diff] [blame] | 73 | ### Eclipse |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 74 | * 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 Liu | f57cceaf | 2019-03-18 21:31:23 | [diff] [blame] | 83 | third_party/android_sdk/public/tools/monitor |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 84 | ``` |
| 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 | |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 91 | ## Debugging C/C++ |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 92 | While the app is running, use the wrapper script's `gdb` command to enter into a |
| 93 | gdb shell. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 94 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 95 | When running with gdb attached, the app runs **extremely slowly**. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 96 | |
| 97 | ```shell |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 98 | # Attaches to browser process. |
Andrew Grieve | c81af4a | 2017-07-26 18:02:13 | [diff] [blame] | 99 | out/Default/bin/content_shell_apk gdb |
| 100 | out/Default/bin/chrome_public_apk gdb |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 101 | |
| 102 | # Attaches to gpu process. |
| 103 | out/Default/bin/chrome_public_apk gdb --debug-process-name privileged_process0 |
| 104 | |
| 105 | # Attach to other processes ("chrome_public_apk ps" to show pids). |
| 106 | out/Default/bin/chrome_public_apk gdb --pid $PID |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 107 | ``` |
| 108 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 109 | When connecting, gdb will complain of not being able to load a lot of libraries. |
| 110 | This happens because of java code. The following messages are all expected: |
| 111 | ``` |
| 112 | Connecting to :5039... |
| 113 | warning: Could not load shared library symbols for 211 libraries, e.g. /system/framework/arm/boot.oat. |
| 114 | Use the "info sharedlibrary" command to see the complete listing. |
| 115 | Do you need "set solib-search-path" or "set sysroot"? |
| 116 | Failed to read a valid object file image from memory. |
| 117 | ``` |
| 118 | |
Stefan Zager | e2b55cc | 2019-10-04 19:57:54 | [diff] [blame^] | 119 | If you have ever run an ASAN build of chromium on the device, you may get |
| 120 | an error like the following when you start up gdb: |
| 121 | ``` |
| 122 | /tmp/<username>-adb-gdb-tmp-<pid>/gdb.init:11: Error in sourced command file: |
| 123 | "/tmp/<username>-adb-gdb-tmp-<pid>/app_process32": not in executable format: file format not recognized |
| 124 | ``` |
| 125 | If this happens, run the following command and try again: |
| 126 | ```shell |
| 127 | $ src/android/asan/third_party/asan_device_setup.sh --revert |
| 128 | ``` |
| 129 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 130 | ### Using Visual Studio Code |
| 131 | While the app is running, run the `gdb` command with `--ide`: |
| 132 | |
| 133 | ```shell |
| 134 | out/Default/bin/content_shell_apk gdb --ide |
| 135 | ``` |
| 136 | |
| 137 | Once the script has done its thing (generally ~1 second after the initial |
| 138 | time its used), open [vscode.md](vscode.md) and ensure you have the |
| 139 | [Android launch entry](vscode.md#Launch-Commands). |
| 140 | |
| 141 | Connect via the IDE's launch entry. Connecting takes 30-40 seconds. |
| 142 | |
| 143 | When troubleshooting, it's helpful to enable |
| 144 | [engine logging](https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md#enginelogging). |
| 145 | |
| 146 | Known Issues: |
| 147 | * Pretty printers are not working properly. |
| 148 | |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 149 | ### Waiting for Debugger on Early Startup |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 150 | ```shell |
| 151 | # Install, launch, and wait: |
| 152 | out/Default/bin/chrome_public_apk run --args="--wait-for-debugger" |
| 153 | # Launch, and have GPU process wait rather than Browser process: |
| 154 | out/Default/bin/chrome_public_apk launch --args="--wait-for-debugger-children=gpu-process" |
| 155 | # Or for renderers: |
| 156 | out/Default/bin/chrome_public_apk launch --args="--wait-for-debugger-children=renderer" |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 157 | ``` |
| 158 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 159 | #### With an IDE |
| 160 | Once `gdb` attaches, the app will resume execution, so you must set your |
| 161 | breakpoint before attaching. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 162 | |
agrieve | e453b98f | 2018-10-22 14:17:17 | [diff] [blame] | 163 | #### With Command-line GDB |
| 164 | Once attached, gdb will drop into a prompt. Set your breakpoints and run "c" to |
| 165 | continue. |
nyquist | c75738d | 2016-09-13 19:25:01 | [diff] [blame] | 166 | |
| 167 | ## Symbolizing Crash Stacks and Tombstones (C++) |
| 168 | |
| 169 | If a crash has generated a tombstone in your device, use: |
| 170 | |
| 171 | ```shell |
| 172 | build/android/tombstones.py --output-directory out/Default |
| 173 | ``` |
| 174 | |
| 175 | If you have a stack trace (from `adb logcat`) that needs to be symbolized, copy |
| 176 | it into a text file and symbolize with the following command (run from |
| 177 | `${CHROME_SRC}`): |
| 178 | |
| 179 | ```shell |
| 180 | third_party/android_platform/development/scripts/stack --output-directory out/Default [tombstone file | dump file] |
| 181 | ``` |
| 182 | |
| 183 | `stack` can also take its input from `stdin`: |
|
|