intellij icon indicating copy to clipboard operation
intellij copied to clipboard

Debugging C++ in Android Studio

Open sheldonneuberger opened this issue 6 years ago • 22 comments

The example/android project in bazel has C++ and Java, but when using this plugin only the Java debugger is connected in Android Studio. Android Studio has support for running dual C++/Java debuggers, so it would be nice if projects built with bazel could use that.

sheldonneuberger avatar Apr 19 '18 18:04 sheldonneuberger

Do you see a checkbox for "Enable native debugging" if you edit the run configuration?

chaoren avatar Apr 19 '18 18:04 chaoren

I just tried that and it connects the lldb debugger, but it doesn't stop at breakpoints.

It prints "Attention! No symbol directories found - please check your native debug configuration" on attach too.

AS log output:

2018-04-19 12:26:25,905 [ thread 15]   INFO - n.lldb.ShellSessionStarterImpl - Copying to app folder: /data/local/tmp/lldb-server => /data/data/bazel.android/lldb/bin/lldb-server 
2018-04-19 12:26:25,905 [ thread 15]   INFO - n.lldb.ShellSessionStarterImpl - Command: cat /data/local/tmp/lldb-server | run-as bazel.android sh -c 'cat > /data/data/bazel.android/lldb/bin/lldb-server && chmod 700 /data/data/bazel.android/lldb/bin/lldb-server' 
2018-04-19 12:26:25,998 [ thread 15]   INFO - n.lldb.ShellSessionStarterImpl - Copying to app folder: /data/local/tmp/start_lldb_server.sh => /data/data/bazel.android/lldb/bin/start_lldb_server.sh 
2018-04-19 12:26:25,998 [ thread 15]   INFO - n.lldb.ShellSessionStarterImpl - Command: cat /data/local/tmp/start_lldb_server.sh | run-as bazel.android sh -c 'cat > /data/data/bazel.android/lldb/bin/start_lldb_server.sh && chmod 700 /data/data/bazel.android/lldb/bin/start_lldb_server.sh' 
2018-04-19 12:26:26,064 [ thread 15]   INFO - n.lldb.ShellSessionStarterImpl - Starting LLDB server : /data/data/bazel.android/lldb/bin/start_lldb_server.sh /data/data/bazel.android/lldb unix-abstract /bazel.android-0 platform-1524165985576.sock "lldb process:gdb-remote packets" 
2018-04-19 12:26:26,064 [ thread 15]   INFO - ndk.run.lldb.AndroidLLDBDriver - Connecting to LLDB server: unix-abstract-connect://[FA7911A08037]/bazel.android-0/platform-1524165985576.sock 
2018-04-19 12:26:26,072 [ thread 15]   WARN - ndk.run.lldb.AndroidLLDBDriver - failed to connect platform - retrying 
2018-04-19 12:26:26,572 [ thread 15]   INFO - ndk.run.lldb.AndroidLLDBDriver - Connecting to LLDB server: unix-abstract-connect://[FA7911A08037]/bazel.android-0/platform-1524165985576.sock 
2018-04-19 12:26:26,596 [ thread 15]   INFO - ndk.run.lldb.AndroidLLDBDriver - create empty target 
2018-04-19 12:26:26,597 [ thread 15]   INFO - n.AndroidNativeAppDebugProcess - Attaching to inferior: pid=28899, ABI=32-bit (arm), native debuggable=false 
2018-04-19 12:26:28,671 [ thread 15]   INFO - ndk.run.lldb.AndroidLLDBDriver - Post attach command: "settings set target.process.thread.step-avoid-regexp ''" 
2018-04-19 12:26:28,671 [ thread 15]   INFO - ndk.run.lldb.AndroidLLDBDriver - Post attach command: "type format add --format boolean jboolean" 
2018-04-19 12:26:28,672 [ thread 15]   INFO - n.AndroidNativeAppDebugProcess - Running in ART VM 
2018-04-19 12:26:28,699 [ thread 24]   INFO - n.AndroidNativeAppDebugProcess - Resuming paused inferior 
2018-04-19 12:26:28,738 [ thread 24]   INFO - .run.AndroidNativeDebugProcess - Launch has been completed 

sheldonneuberger avatar Apr 19 '18 19:04 sheldonneuberger

Attaching to inferior: pid=28899, ABI=32-bit (arm), native debuggable=false

Make sure the app is declared debuggable in the manifest: https://developer.android.com/guide/topics/manifest/application-element.html#debug

chaoren avatar Apr 19 '18 19:04 chaoren

Added that to the manifest but it seems to have no effect. Note that java debugging works (including breakpoints), and the lldb debugger attaches, but it's not hitting breakpoints. More debug output below, with a couple suspect lines bolded:

2018-04-19 13:07:08,812 [cManager-0]   INFO - base.scope.scopes.IdeaLogScope - BlazeInvocation: 11.3s 
2018-04-19 13:07:08,812 [cManager-0]   INFO - base.scope.scopes.IdeaLogScope - Other: 82ms 
2018-04-19 13:07:08,884 [se-915-b08]   WARN - .workspace.NdkWorkspaceManager - NDK support for project 'bazel' is disabled because it's not a valid Android project
2018-04-19 13:07:08,885 [ thread 71]   INFO -       #com.jetbrains.cidr.lang - Clearing symbols finished in 0 s. 
2018-04-19 13:07:08,885 [ thread 71]   INFO -       #com.jetbrains.cidr.lang - Loading symbols finished in 0 s. 
2018-04-19 13:07:08,886 [ thread 71]   INFO -       #com.jetbrains.cidr.lang - Building symbols finished in 0 s. 
2018-04-19 13:07:08,886 [ thread 71]   INFO -       #com.jetbrains.cidr.lang - Saving symbols finished in 0 s. 
2018-04-19 13:07:09,365 [ thread 73]   INFO -       #com.jetbrains.cidr.lang - Clearing symbols finished in 0 s. 
2018-04-19 13:07:09,366 [ thread 73]   INFO -       #com.jetbrains.cidr.lang - Loading symbols finished in 0 s. 
2018-04-19 13:07:09,366 [ thread 73]   INFO -       #com.jetbrains.cidr.lang - Building symbols finished in 0 s. 
2018-04-19 13:07:09,367 [ thread 73]   INFO -       #com.jetbrains.cidr.lang - Saving symbols finished in 0 s. 
2018-04-19 13:07:17,468 [utorImpl-9]   INFO - base.scope.scopes.IdeaLogScope - Waiting for mobile-install device target... 
2018-04-19 13:07:17,612 [utorImpl-9]   INFO - base.scope.scopes.IdeaLogScope - Command: /usr/local/bin/bazel mobile-install --tool_tag=ijwb:AndroidStudio --device FA7911A08037 --adb_arg=-s  --adb_arg=FA7911A08037 --adb /Users/sheldon.neuberger/Library/Android/sdk/platform-tools/adb --curses=no --color=yes --experimental_ui=no --progress_in_terminal_title=no --fission=no -c dbg --experimental_build_event_binary_file=/var/folders/15/12js8qjx7sv6wmzlqwdbfh7r0000gq/T/intellij-bep-24100f48-1978-408e-866c-e551e401d744 --noexperimental_build_event_binary_file_path_conversion --output_groups=android_deploy_info -- //examples/android/java/bazel:hello_world

 
2018-04-19 13:07:32,920 [utorImpl-6]   INFO - base.scope.scopes.IdeaLogScope - Command: /usr/local/bin/bazel info --tool_tag=ijwb:AndroidStudio execution_root --curses=no --color=yes --experimental_ui=no --progress_in_terminal_title=no --fission=no -c dbg --

 
2018-04-19 13:07:33,442 [nt Monitor]   INFO -            #com.android.ddmlib - Opening a debugger listener at port 8604 for client with pid 2431 
2018-04-19 13:07:34,459 [nt Monitor]   INFO -            #com.android.ddmlib - Opening a debugger listener at port 8604 for client with pid 2431 
2018-04-19 13:07:35,411 [se-915-b08]   INFO - s.ndk.run.lldb.ConnectLLDBTask - Launching Bazel:BlazeCommandRunConfigurationType:Hybrid native debug session on device: manufacturer=Google, model=Pixel 2, API=27, codename=REL, ABIs=[arm64-v8a, armeabi-v7a, armeabi] 
2018-04-19 13:07:35,583 [se-915-b08]   WARN - s.ndk.run.lldb.ConnectLLDBTask - Using run-as to start debug session 
2018-04-19 13:07:35,584 [se-915-b08]   INFO - s.ndk.run.lldb.ConnectLLDBTask - ABIs supported by app: [armeabi-v7a, armeabi] 
2018-04-19 13:07:35,584 [se-915-b08]   INFO - s.ndk.run.lldb.ConnectLLDBTask - Found LLDB server: "/Users/sheldon.neuberger/Library/Android/sdk/lldb/3.0/android/armeabi/lldb-server" 
2018-04-19 13:07:35,588 [se-915-b08]   INFO - AndroidLLDBDriverConfiguration - LLDB framework file: /Users/sheldon.neuberger/Library/Android/sdk/lldb/3.0/lib/liblldb.6.0.0.dylib 
2018-04-19 13:07:35,588 [se-915-b08]   WARN - ndk.run.lldb.AndroidLLDBDriver - No symbol directories found 
2018-04-19 13:07:35,602 [ thread 75]   INFO - ls.ndk.run.lldb.SessionStarter - Pushing files to device 
2018-04-19 13:07:35,619 [se-915-b08]   INFO - idea.run.AndroidProcessHandler - stopAll() 
2018-04-19 13:07:35,620 [ thread 75]   INFO - ls.ndk.run.lldb.SessionStarter - Remote file /data/local/tmp/lldb-server is up-to-date. 
2018-04-19 13:07:35,627 [ thread 75]   INFO - ls.ndk.run.lldb.SessionStarter - Remote file /data/local/tmp/start_lldb_server.sh is up-to-date. 
2018-04-19 13:07:35,627 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Loading driver 
2018-04-19 13:07:35,627 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Loading startup script: /Applications/Android Studio.app/Contents/bin/lldb/shared/stl_printers/load_script 
2018-04-19 13:07:35,956 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Loading startup script: /Applications/Android Studio.app/Contents/bin/lldb/shared/jobject_printers/jstring_reader.py 
2018-04-19 13:07:35,961 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Startup command: "settings set auto-confirm true" 
2018-04-19 13:07:35,961 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Startup command: "settings set plugin.symbol-file.dwarf.comp-dir-symlink-paths /proc/self/cwd" 
2018-04-19 13:07:35,962 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Startup command: "settings set plugin.jit-loader.gdb.enable-jit-breakpoint false" 
2018-04-19 13:07:36,092 [ thread 75]   WARN - n.lldb.ShellSessionStarterImpl - mkdir: '/data/data/bazel.android/lldb': File exists
mkdir: '/data/data/bazel.android/lldb/bin': File exists

 
2018-04-19 13:07:36,092 [ thread 75]   INFO - n.lldb.ShellSessionStarterImpl - Copying to app folder: /data/local/tmp/lldb-server => /data/data/bazel.android/lldb/bin/lldb-server 
2018-04-19 13:07:36,092 [ thread 75]   INFO - n.lldb.ShellSessionStarterImpl - Command: cat /data/local/tmp/lldb-server | run-as bazel.android sh -c 'cat > /data/data/bazel.android/lldb/bin/lldb-server && chmod 700 /data/data/bazel.android/lldb/bin/lldb-server' 
2018-04-19 13:07:36,277 [ thread 75]   INFO - n.lldb.ShellSessionStarterImpl - Copying to app folder: /data/local/tmp/start_lldb_server.sh => /data/data/bazel.android/lldb/bin/start_lldb_server.sh 
2018-04-19 13:07:36,278 [ thread 75]   INFO - n.lldb.ShellSessionStarterImpl - Command: cat /data/local/tmp/start_lldb_server.sh | run-as bazel.android sh -c 'cat > /data/data/bazel.android/lldb/bin/start_lldb_server.sh && chmod 700 /data/data/bazel.android/lldb/bin/start_lldb_server.sh' 
2018-04-19 13:07:36,373 [ thread 75]   INFO - n.lldb.ShellSessionStarterImpl - Starting LLDB server : /data/data/bazel.android/lldb/bin/start_lldb_server.sh /data/data/bazel.android/lldb unix-abstract /bazel.android-0 platform-1524168455584.sock "lldb process:gdb-remote packets" 
2018-04-19 13:07:36,373 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Connecting to LLDB server: unix-abstract-connect://[FA7911A08037]/bazel.android-0/platform-1524168455584.sock 
2018-04-19 13:07:36,381 [ thread 75]   WARN - ndk.run.lldb.AndroidLLDBDriver - failed to connect platform - retrying 
2018-04-19 13:07:36,883 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Connecting to LLDB server: unix-abstract-connect://[FA7911A08037]/bazel.android-0/platform-1524168455584.sock 
2018-04-19 13:07:36,910 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - create empty target 
2018-04-19 13:07:36,911 [ thread 75]   INFO - n.AndroidNativeAppDebugProcess - Attaching to inferior: pid=2431, ABI=32-bit (arm), native debuggable=false 
2018-04-19 13:07:39,077 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Post attach command: "settings set target.process.thread.step-avoid-regexp ''" 
2018-04-19 13:07:39,077 [ thread 75]   INFO - ndk.run.lldb.AndroidLLDBDriver - Post attach command: "type format add --format boolean jboolean" 
2018-04-19 13:07:39,078 [ thread 75]   INFO - n.AndroidNativeAppDebugProcess - Running in ART VM 
2018-04-19 13:07:39,125 [ thread 71]   INFO - n.AndroidNativeAppDebugProcess - Resuming paused inferior 
2018-04-19 13:07:39,154 [ thread 71]   INFO - .run.AndroidNativeDebugProcess - Launch has been completed 

sheldonneuberger avatar Apr 19 '18 20:04 sheldonneuberger

Oh, you need this in your .bazelproject file.

additional_languages:
  c

chaoren avatar Apr 19 '18 21:04 chaoren

Already had that when I imported the project. Could it be because I'm running the latest plugin from this repo on AS 3.0.1?

Just in case someone wants to try to repo:

  1. Clone the bazel project
  2. Build the latest bazel AS plugin out of this repo and install it to AS 3.0
  3. Add "android:debuggable="true"" to the AndroidManifest.xml in examples/android. Import bazel/WORKSPACE and point the wizard to //examples/android/java/bazel:hello_world. Uncomment "c" for add'l languages.
  4. Edit run config to enable native debugging
  5. Set C++ breakpoint and run debug.

sheldonneuberger avatar Apr 19 '18 21:04 sheldonneuberger

Here's my project view:

directories:
  examples/android/java/bazel

targets:
  //examples/android/java/bazel:hello_world

additional_languages:
  c
  
android_sdk_platform: android-stable

build_flags:
  --fat_apk_cpu=x86

My run configuration:

Target expression (andorid_binary handled by Android Binary Handler):
//examples/android/java/bazel:hello_world
Bazel flags:
Executable flags (mobile-install only):
[x] Enable native debugging
Run with bazel mobile-install

Using an x86 emulator, set a breakpoint at jni.cc:9, and it stops just fine when i hit debug.

chaoren avatar Apr 20 '18 16:04 chaoren

I keep getting the "No symbol directories found" error. Should this work with AS 3.0.1? I've tried with the latest master for the plugin and bazel and still no luck. I haven't tried with AS 3.2 (bazel build //aswb:aswb_bazel_zip --define=ij_product=android-studio-canary doesn't work for me).

sheldonneuberger avatar Apr 25 '18 22:04 sheldonneuberger

Yeah, I used AS 3.0.1. Do you have the latest LLDB package for your AS version? Can you try it with an emulator to see if it's a device problem?

chaoren avatar Apr 25 '18 23:04 chaoren

I've tried on an x86 emulator with that fat_apk_cpu build flag but still no luck. Also this is on a mac; are you using linux? Is there anyway to get more debugging logs from the bazel plugin and bazel?

LLDB is v3.0. There's a v3.1 available but I can't get AS3.0 to use it. I think LLDB v3.1 is only to be used with AS 3.1?

sheldonneuberger avatar Apr 25 '18 23:04 sheldonneuberger

Also this is on a mac; are you using linux?

Yes, I'm only using linux.

Is there anyway to get more debugging logs from the bazel plugin and bazel?

Logs on mac should be located at ~/Library/Logs/AndroidStudio3.0/

I think LLDB v3.1 is only to be used with AS 3.1?

That is correct. LLDB 3.0 should be the correct version for you.

Can you check if the resulting .so file in the APK contains debug symbols?

$ unzip bazel-bin/examples/android/java/bazel/hello_world.apk lib/armeabi-v7a/libhello_world.so
$ $ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/a
rm-linux-androideabi-readelf --debug-dump lib/armeabi-v7a/libhello_world.so

chaoren avatar Apr 26 '18 18:04 chaoren

Debug symbols appear to be there: https://gist.github.com/sheldonneuberger/b58126ca85f1d9e709513f40335eb2e3

I believe this is a mac issue, because I just got a fresh new mac and did a fresh install of everything and this same error still happens. I used AS 3.0.1, bazel 0.13, and bazel plugin v2018.04.09.0.2.

Detailed repro instructions:

  • fresh mac with macos 10.13.4
  • installed bazel 0.13 with the official .sh installer from the github releases page
  • installed android studio 3.0.1 from the official archives, chose "standard setup" in wizard, also installed lldb/cmake/ndk (you must specify lldb 3.0, it will try to install 3.1 by default)
  • installed bazel plugin v2018.04.09.0.2 from Studio's plugin installer
  • uncommented the androidsdk/androidndk lines in bazel/WORKSPACE
  • imported bazel with same project view as you posted, except I used android-27 instead of stable.
  • open AS with 'open /Applications/Android\ Studio.app' so it gets env vars
  • tick "enable native debugging" in run config ("run with bazel mobile-install" is already set)
  • debug, see no symbol dir error

sheldonneuberger avatar May 01 '18 01:05 sheldonneuberger

Hi, any update about that issue? We're still experiencing the issue with the latest Android Studio 3.3 and latest Bazel plugin. Thanks ;)

dolphineye avatar Feb 07 '19 13:02 dolphineye

I have the same problem for lldb ndk debugger, but I am a windows environment.Sometimes I suspect that it is caused by the virtual machine environment and the mobile environment, but vm test enviroment same question with me! Android Studio version 3.3.2 iuess: Connecting to com.ljt.jni Now Launching Native Debug Session $ adb shell cat /data/local/tmp/lldb-server | run-as com.ljt.jni sh -c 'cat > /data/data/com.ljt.jni/lldb/bin/lldb-server && chmod 700 /data/data/com.ljt.jni/lldb/bin/lldb-server' $ adb shell cat /data/local/tmp/start_lldb_server.sh | run-as com.ljt.jni sh -c 'cat > /data/data/com.ljt.jni/lldb/bin/start_lldb_server.sh && chmod 700 /data/data/com.ljt.jni/lldb/bin/start_lldb_server.sh' Starting LLDB server: /data/data/com.ljt.jni/lldb/bin/start_lldb_server.sh /data/data/com.ljt.jni/lldb unix-abstract /com.ljt.jni-0 platform-1552381955552.sock "lldb process:gdb-remote packets" Debugger attached to process 16611 warning: failed to set breakpoint site at 0x7c785bb688 for breakpoint 3.1: error: 0 sending the breakpoint request plz help me check out this question .thanks

LeeJiTing avatar Mar 12 '19 09:03 LeeJiTing

Hi, any update about that issue?

superlcr avatar Mar 05 '20 06:03 superlcr

android-studio 3.6.2 / eclipse luna sr2 / ndk v19c, v20b, debug c/c++ test ok https://fatalfeel.blogspot.com/2013/10/android-studio-with-eclipse-debug-jni.html

fatalfeel avatar Apr 04 '20 07:04 fatalfeel

experiencing the same issue, android studio 3.6.2, macOS Catalina platform.

use the example code: https://github.com/bazelbuild/examples.git android/ndk

.bazelproject:

directories:
  app/src/main

# Automatically includes all relevant targets under the 'directories' above
derive_targets_from_directories: true

targets:
  //app/src/main:app

additional_languages:
  c

android_sdk_platform: android-28

build_flags:
  --spawn_strategy=local
  --fat_apk_cpu=arm64-v8a

android studio build command:

/usr/local/bin/bazel build --tool_tag=ijwb:AndroidStudio --output_groups=+android_deploy_info --curses=no --color=yes --progress_in_terminal_title=no --spawn_strategy=local --fat_apk_cpu=arm64-v8a --fission=no -c dbg --build_event_binary_file=/var/folders/zj/8qbk7bqx2pbghrl6fc2j6f980000gn/T/intellij-bep-ab5517dd-4ab5-4ea0-8fac-3f29e014e8d6 --nobuild_event_binary_file_path_conversion -- //app/src/main:app

dolmens avatar Apr 11 '20 01:04 dolmens

@dolmens i meet the same issues, did you resolve?

weinixuehao avatar Apr 25 '20 03:04 weinixuehao

I can hit a breakpoint by execute a lldb cmd b cppFile:lineNum image image

weinixuehao avatar Jul 03 '20 06:07 weinixuehao

Having been struggling with this for a while, I found a workaround that others might find useful: if you use ndk>=21 you can add build --copt="-fdebug-compilation-dir" --copt="/proc/self/cwd" to your .bazelrc. For ndk<21 the equivalent line is build --copt="-Xclang" --copt="-fdebug-compilation-dir" --copt="-Xclang" --copt="/proc/self/cwd". Alternatively, you can add those --copt flags to your .bazelproject file under build_flags one flag per line.


Here's my understanding of why this works. When you put a breakpoint in a file in android studio, it relays that information to the lldb client, but it does so using the file's absolute path. The lldb client needs a way to match those full paths with the debug info from the compiled .so file. Unfortunately, android .so files built on macos have full sandbox paths embedded into them by default (specifically in DW_AT_comp_dir), which incidentally makes them not reproducible. As a result, lldb is unable to figure out where it should put the breakpoint. On linux, however, the compilation path that gets embedded is /proc/self/cwd and android studio already has support for that by issuing settings set plugin.symbol-file.dwarf.comp-dir-symlink-paths /proc/self/cwd to lldb, but it doesn't work on macos. This plugin added support for debugging linux built binaries on macos some time ago in commit https://github.com/bazelbuild/intellij/commit/71ed9914a937254d9ed295e7b749d237b6c17882 by mapping /proc/self/cwd to the workspace root, and the workaround leverages that by mimicking the compilation directory that bazel on linux would generate.

kitterion avatar Nov 18 '20 12:11 kitterion

Are there any thoughts or updates on this issue?

hicksjoseph avatar Oct 28 '21 16:10 hicksjoseph

kitterion's solution works for us.

kkpattern avatar Aug 31 '22 07:08 kkpattern

Thank you for contributing to the IntelliJ repository! This issue has been marked as stale since it has not had any activity in the last 6 months. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-maintainer". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.

github-actions[bot] avatar Jun 07 '23 02:06 github-actions[bot]

This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please reach out to the triage team (@bazelbuild/triage). Thanks!

github-actions[bot] avatar Jun 22 '23 02:06 github-actions[bot]

Any updates on this issue?

nkoroste avatar Oct 13 '23 13:10 nkoroste