How can I debug native Android platform code? - android

I'm interested in learning about the Android runtime (ART) and I'd like to be able to put a debugger on it so I can step through the code. I have an AOSP build that I've been running on an emulator.
What's the best way to attach and run source? Should I just use gdbserver on the emulator? Are there any IDEs that people use for native debugging?

Looks like you figured this out, but just for completeness, here's a link to some official documentation which basically covers what you said: http://source.android.com/devices/tech/debug/index.html

If you want to debug native code, you're going to need the source, which of course means getting the AOSP. After you've setup the AOSP, do the following:
In your AOSP directory, run "choosecombo". Select "debug" and pick an "eng" build of your choice.
Build the AOSP
Start the emulator (ie: on Linux, "emulator &")
adb shell into the emulator and run "setprop debug.db.uid 32767". This prevents debuggerd from attaching to a process that it thinks has halted and causing problems. See system/core/debuggerd/debuggerd.cpp in the AOSP for more info.
In the same shell that you built the AOSP, run "gdbclient [process name]". This will attach and load the debugging information for the process you are interested in. You can also attach to a pid, but you will end up having to load the symbol information yourself.
Enjoy debugging native Android platform code

Related

Why is attaching gdbserver to a native android app failing?

I am developing a fully native application in using C++ and pure CMake as the build system - no Android Studio involved at all (proof of concept here)
The code builds, apk is generated and can be installed and run via ADB without issue but I cannot get gdbserver64 to attach to the process for debugging.
More details:
App is built against SDK/NDK API level 30
Attempting to debug on an Android 11 emulator instance without Google Play
I can run adb root just fine
Image already includes gdbserver and gdbserver64, attempting to use those
Developer options and USB debugging enebled in emulator
App has android:debuggable="true" in manifest
But every time I try gdbserver64 :5039 --attach $(pidof my.app.id)
I get /bin/sh: <app_pid>: inaccessible or not found
What am I missing? And no, I cannot just move to Android Studio - this is a cross platform project that needs to be buildable using only CMake.
According to my observation, this may be a bug of prebuilt gdbserver. It treats the parameter after --attach as a program name and tries to start it.
It's not really an answer, but in similar configuration to yours I got same error message and unable to overcome this. For me, switching to lldb helped, see e.g.

The game crashes after the build for android

I have a problem with the game crashing after the build. If I run the game in Unity, then everything works. And when I build it on android, when I start the first training scenes work fine, but when I reach the main stage, the game crashes. There is a error in Unity:
Google.JarResolver.ResolutionException: Cannot find candidate artifact for com.google.android.gms:play-services-games:8.4+
Maybe that's the problem. If that's not the problem, how do you know what it is?
So this looks like plugin resolve problem. But more information wouldnt be bad and you should learn how to see your device logs on Android. Here is how:
You should check the logs of your mobile device in the moment of the crash. You can do it by using "adb logcat".
First you should download SDK Platform Tools.
Then, enable USB debugging on your phone.
Connect your phone to your PC and give the permission it needs. It will ask you when you first connect it.
Open command line on your computer, go to your SDK Platform Tools folder, copy the directory and paste it after writing "cd " to your command line
Now you can use adb to figure out your Android crashes. To filter the ADB you can use
adb logcat -s Unity ActivityManager PackageManager dalvikvm DEBUG
This way you will only see Unity related logs from your device.

Debugging C++ code of an Android App using lldb from the console

I would like to know how I can debug an Android app by using lldb.
I mean using lldb from console not over Android Studio.
From what I found out, Android Studio is doing this:
Pushing the app to the device
Starting this app
Pushing lldb-server to the device
Starting lldb-server on the remote device
Forwarding the ports
Connecting to the local port which is forwarded to the device
I would like to achieve the same using lldb from the console.
Let's say I have already lldb-server on the remote device (because Android Studio did this already for me), what do I need to do is to connect to an App using lldb from the console?
Never tried, but maybe this could help:
LLDB remote debugging
Debugging C++ native libraries in Android using command line
Additionally:
ensure that APK's native libraries (lib*.so) are having debug info/symbols. I pull the apk locally, locate .so file, and check the output of binutils nm utility. When you get familiar with your .so files only checking the size of the .so file could be enough.
one can debug by attaching to a running process/application (attach) and in some cases you need to debug application on application start (run).
in some debugging cases phone/emulator requires root access (file manipulations)
if you manage to debug C++ libs within Android Studio - check Debug+Console and Debug+LLDB output tabs to find interesting commands/options/steps
the procedure looks quite complicated and fragile - arm yourself with patience, don't ignore warnings and don't skip steps.

Go /etc/resolv.conf missing in android

I cross compiled my Go source code to Arm to run in Android .The program runs in the terminal in the Android device, but I get an error saying that /etc/resolv.conf is missing. My program heavily relies on the net/http library in making http calls.
How can I handle this error in Android?
As of right now you can't use the built in net/http to connect to hostnames, check this issue.
There are few workarounds you could use.
generate /etc/resolv.conf on your program start if you have root access, ugly but easy and simple.
store resolv.conf somewhere and patch http://golang.org/src/pkg/net/dnsclient_unix.go#L169 to use that custom path.
patch $GOROOT/src/pkg/net/dnsconfig_unix.go to get the nameservers by executing getprop net.dns1 and getprop net.dns2.
implement the full patch at https://gist.github.com/ernesto-jimenez/8042366

Install an apk without starting the emulator

When developing on BlackBerry or iOS, you can deploy your application just by dropping the compiled code into a special directory, and when the simulator boots up it will check that directory and install any apps that it finds there. Is this possible for Android?
The reason this feature is desirable is because my build system deploys builds nightly. I want the emulator ready to go when my QA team gets in the next morning. To do that, I have to write a script that boots up the emulator, waits for it to start up, then calls adb install and finally shuts down the emulator. It would be great if I could just drop the .apk in a directory and have it ready to go the next time QA boots up the emulator.
Edit:
Someone asked for the script I described above. It's unfortunately written in Perl, but here it is: http://pastebin.com/6UcNgYRs
Edit 2:
I just found an awesome little command that can help you if you're trying to write a script like the one I mentioned above. You can have your script wait for the emulator to come online with the command $ adb wait-for-device!!
Is this possible for Android?
No, sorry. Nice idea, though.
You can just tell the QA people to use the install script as the emulator itself, just remove the "close the emulator" bit.

Categories

Resources