So I compiled a gdb from source using target=arm-elf-linux and a gdbserver using host=arm-linux-androideabi. Everything works fine with debugging native code on android except that if I type 'info sharedlibrary', it's stuck.
I tried different sets of versions of gdb+gdbserver, including the ones from the latest google NDK, they appears to have the same problem sticking on searching all libraries currently loaded in memory. So I assume this is not the fault of me choosing wrong versions of gdb but the android system on phone.
Is it right? What can I do to make it work?
Related
I want to make DSP calculations by native code. This works well within the emulator (Android 4.2.2). But on several devices (Android 7 and 9) the lib built by the NDK let the app crash directly on executing System.loadLibrary. But: a downloaded lib loads without problems when I put it into my APK.
I confess, for the main app I am using an old Eclipse IDE on Windows, targeting for API 26. My NDK is a small r7, running on Ubuntu. But my lib only makes calculations on byte arrays in standard c, no Android things are accessed here.
The native code compiles free of warnings and errors and is built for armeabi and armeabi-v7a. In the APK the libxyz.so files are in the correct folders. The names of the export functions should be correct. But even if there are no declarations for them in the Java class and nothing native is called, the crash directly occurs on static { System.loadLibrary("libxyz.so"); }
Can my lib binaries be incompatible?
Thanks for any help!
Update: The libs indeed were incompatible, see comments below.
Finally I could read the logcat from a device (by command line adb, in Eclipse this was not working). The problem seems to be caused by "text relocations" in the binaries.
Logcat: "java.lang.UnsatisfiedLinkError ... dlopen failed ... libxyz.so has text relocations"
Updating to a newer NDK (9) fixed the TEXTREL bug. Now System.loadLibrary and calling the native methods works on newer APIs. So I had to install 1,4 GB of NDK for calculating things in standard c.
The NDK has the ability to enable the address sanitizer on anything you build with it by adding the -fsanitize=address flag to both LOCAL_CFLAGS and LOCAL_LDFLAGS, which is nice. Well, it would be nice if it actually worked. If you add that flag and try building your library, you get a bunch of "undefined symbol" errors. You can ignore these errors by passing -Wl,--unresolved-symbols=ignore to the linker and the build completes successfully, but of course the adventure doesn't end here.
The Google's guide then says that you need a rooted device on which you run a script that modifies the runtime executable on the system partition so it loads the ASan library, and then the aforementioned missing symbols link at runtime. I tried this, and the main problem with this approach is that it also profiles something in the JVM itself thus slowing it down so much that the app doesn't start at all or crashes somewhere inside the JVM.
I've also tried the new malloc debug on an emulator but it also debugs the entire JVM thus making it unusable. All these tools seem to be absolutely not intended for the app developers but for those who work on Android itself.
I desperately need a way to fix a memory corruption crash in my app without profiling the whole virtual machine in the process. Will ASan work if I just put its library into my apk and load it before my JNI library? Are there any other ways to debug this? How do people fix this kind of bugs on Android at all?
Making a "minimal wrapper executable" and running it on some other operating system that has proper debugging tools isn't really an option here because the way the JNI library is used depends substantially on the Java part, and even if I make one, I can't be entirely sure that this bug will reproduce.
Update: I tried the asan_device_setup script on an Oreo emulator because it didn't work at all this time on a Nexus 9 with Nougat. The log flooded with linker errors of 32-bit processes trying to load 64-bit libraries and then it froze and the only way to make it usable again was flashing the factory image. Just to be sure I didn't mess anything up, I tried it several times reflashing the factory image, then TWRP, than SuperSU, and then using the "adbd insecure" app to restart the adb daemon as root. So, I started the emulator with -writable-system and ran the script on it, it completed successfully. I then built my app with ASAN and installed it, but it crashes with java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__asan_init_v3" referenced by "/data/...full path to the shared library on launch. I tried it both with and without setprop wrap.com.app.package "asanwrapper", the result is the same.
You should probably file bugs (http://b.android.com) if you're seeing performance issues this serious. We run devices that are fully ASAN instrumented every day and those work better than you'd expect. ASAN is a roughly 2x slowdown, but running your app with ASAN should work fine.
Same goes for debug malloc (I don't remember the performance implications of debug malloc, but I thought they were less severe than ASAN).
I'm trying to debug some native Android code built through the NDK. Unfortunately, when I launch the process under the debugger from Eclipse I get an error
I'm sorry, Dave, I can't do that. Symbol format `elf32-littlearm' unknown.
The code is compiled for ARM thumb so the symbol format makes sense. However, using Google's bundled ADT for Eclipse should work? Do I need to specify a specific version of GDB to use?
To note, the code runs fine without trying to debug.
I'm running on Windows 7 64 bit with NDK release 10.
After some searching it looks like it is a bug with the GDB shipped with the NDK r10 toolchain. See https://code.google.com/p/android/issues/detail?id=74371. The download linked from that bug seems to work for me.
I'm working on a software designed to compile and run on Linux. I was recently asked to cross-compile it for Android using the NDK, which was surprisingly straightforward.
To do this quickly, we just compiled the source, without writing any JNI wrapper. I copied the resulting binaries with adb push to an emulator (rooted) and ran it from the adb shell. Everything worked fine.
Now my question is: How can I embed those binaries somehow in an android application and run them (they act like services and must run in the background) ?
I'd like to avoid changing the existing code to support a JNI interface, but writting a small JNI/C code that just exec the other binaries is fine.
Bonus question: once embedded, is there a way to know the path of the installed binaries ?
http://www.ibm.com/developerworks/opensource/tutorials/os-androidndk/index.html
i found the nice tutorial in IBM developersworks about Android NDK
I'm developping a android's aplication using some shared
libraries written by me and compiled with ndk-r5b. The application works,
y and the calls to the libraries works too, but I detected some errors,
segmentations faults, and I need to debug it, but, I don't know how debug
native code from android and I don't know if I can generate core dumps,
as in linux, for debug my libraries.
Any idea?
The ndk comes with ndk-gdb, which supposedly allows you to debug native applications. Also, if you download the whole andriod open source project, they also have some version of gdb used for debugging. Look in the docs/ folder of the ndk to learn about using it. This tutorial might also prove helpful: http://vilimpoc.org/blog/2010/09/23/hello-gdbserver-a-debuggable-jni-example-for-android/
However, as shown in a recent question I asked: Running ndk-gdb with package not found error on motorola phone I still can't get it to work.
Edit: You said in the comments you were using a Samsung Device: Samsung decide to wreck havoc on some of the crucial internals required for native debugging, but it's easily fixable if you have root access to the device. If you use the --verbose flag, you'll probably find that the error is different than that, a package unknown error. That's because it's looking in /data/system/packages.list, but samsung renamed that file to /dbdata/system/packages.list. So if you make a symlink to that file in /data/system/packages.list, (requires root access) it should work. Or at least so claims the ndk mailing list: http://osdir.com/ml/android-ndk/2011-03/msg00036.html