Archive: lab-7.zip
To be able to debug native code using GDB, a special flag must be passed to ndk-build. If you compile manually, you must pass NDK_DEBUG=1 in the command line (or set it as an environment variable).
ndk-build NDK_DEBUG=1 NDK_PROJECT_PATH=/path/to/project
or add
android:debuggable="true"
to the <application> tag in AndroidManifest.xml.
Additionally, you should add -g to LOCAL_CFLAGS in Android.mk and APP_OPTIM = debug in Application.mk.
To start a native debug session, run
ndk-gdb --project=/path/to/project
You can add the –start paramater to make ndk-gdb start the application.
To debug native code directly from eclipse you must add a new Debug Configuration for each project. To do this, select Run > Debug Configurations from the top menu. Select Android Native Application from the left panel, and click the New button. Select your project as the Project target. Make the correct debugger is set in the Debugger tab (it should be ${NdkGdb} or a path to ndk-gdb.
You can add a breakpoint by double clicking on the left bar in the editor area or navigating to the desired line and pressing Ctrl+Shift+B.
Then, start the project with the new debug configuration. When the code is reached, the current perspective should change to debug perspective and Eclipse should be able to help you debug. Check the debug toolbar.
To enable checking of JNI, if it is not already enabled
adb shell setprop debug.checkjni 1
CheckJNI is already enable on emulators. CheckJNI shows detailed information about problems (bad pointers, wrong allocations, incompatible types, JNI calls in a critical section, etc).
Memory issues can be detected with LibC debug mode. To enable this mode:
adb shell setprop libc.debug.malloc <value>
where value:
There is a better alternative to this: Valgrind, but it requires root permission to install.
Android uses gprof for profiling. To enable gprog you have to add -pg to LOCAL_CFLAGS, however, on Android this is not enough, since some symbols will be missing. You have to add https://code.google.com/p/android-ndk-profiler/ to the project.