07. Debugging and Profiling

  • Description: Debugging tools and techniques on NDK
  • Practical part: Debugging NDK apps

Lecture

Practical

Archive: lab-7.zip

Task 1 - Set up debugging

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.

I had issues with running ndk-gdb outside the project folder, since it tried to use a relative path.

GDB Quick Reference

Task 2 - Integrating debugging with Eclipse

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.

You must set target=android-<version> in project.properties to be the same as android:targetSdkVersion in the AndroidManifest.xml file.

Task 3 - CheckJNI

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).

Task 4 - LibC debug mode

Memory issues can be detected with LibC debug mode. To enable this mode:

adb shell setprop libc.debug.malloc <value>

where value:

  • 1 Leak detection
  • 5 Detect overruns - fill allocated memory
  • 10 Detect overruns - fill memory and add sentinels

There is a better alternative to this: Valgrind, but it requires root permission to install.

Task 5 - Profiling

This works only on ARM for now

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.

ndk/courses/07.txt · Last modified: 2014/04/08 22:38 by petre.eftime
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0