Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Debugging NativeAOT #9855

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dellis1972
Copy link
Contributor

NativeAOT Sample

This is the sample for using NativeAOT with .net Android

Debugging

In order to debug we need to use lldb. The following commands will install the lldb-server for the application.

adb push $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/darwin-x86_64/lib/clang/18/lib/linux/aarch64/lldb-server /data/local/tmp/lldb-server
adb shell run-as net.dot.hellonativeaot cp /data/local/tmp/lldb-server .
adb forward tcp:5039 tcp:5039
adb shell run-as net.dot.hellonativeaot killall -9 lldb-server
adb shell run-as net.dot.hellonativeaot ./lldb-server platform --listen "*:5039" --server

The above commands are setup in a MSBuild Target which will run after the Install target has run. So there
is no need to do this manually if you are using NativeAOT.csproj.

Once lldb-server is up and running, you will want to run up the app using the following

adb shell am start -S --user "0" -a "android.intent.action.MAIN" -c "android.intent.category.LAUNCHER" -n "net.dot.hellonativeaot/my.MainActivity" -D

The -D is important as it stops the app from running until the java debugger is attached.
Now that the app is running we need to get the process id.

adb shell ps | grep net.dot.hellonativeaot

Grab the process id, then start VSCode Debug NativeAOT Task. And drop in the process id.

Set your breakpoints as usual in the VSCode.

if you are using lldb from the command line use the following

lldb
> platform select remote-android
> platform connect connect://localhost:5039
> settings set target.process.thread.step-in-avoid-nodebug true
> settings set target.process.thread.step-out-avoid-nodebug true
> target create samples/NativeAOT/bin/Debug/net10.0-android/android-arm64/native/NativeAOT.so
> target symbols add samples/NativeAOT/bin/Debug/net10.0-android/android-arm64/native/NativeAOT.so.dbg
> target select 0
> attach --pid <processid>

Next you need to attach the java debugger to clear the dialog which is currently blocking the
application execution.

adb forward --remove tcp:8700
adb forward tcp:8700 jdwp:<pid>
jdb -attach localhost:8700 

You will want to type quit to exit the jdb terminal once it has connected.

  • Figure out why breakpoints do not work we currently get
Breakpoint at /Users/xxx/Documents/Sandbox/Xamarin/android/samples/NativeAOT/MainActivity.cs:93 could not be resolved, but a valid location was found at /Users/xxx/Documents/Sandbox/Xamarin/android/samples/NativeAOT/MainActivity.cs:93

Comment on lines +22 to +24
```dotnetcli
adb shell am start -S --user "0" -a "android.intent.action.MAIN" -c "android.intent.category.LAUNCHER" -n "net.dot.hellonativeaot/my.MainActivity" -D
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the -D, do you need the jdb example below? Or is it needed for other reasons?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The -D is there primarily to make sure the app stops on startup so you have time to attach the lldb debugger before the app starts.

If you don't mind attaching after startup you can remove it.

@dellis1972 dellis1972 force-pushed the dev/dellis1972/debugnativeaot branch from 6c543ac to 0bcd739 Compare March 4, 2025 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants