Debugging an Android NDK application with CGDB

CGDB is a terminal-based lightweight interface to the GNU debugger gdb. It provides a split screen view, which displays the source code along with the debug information. This recipe discusses how to debug Android application with cgdb.

Getting ready

The following instructions install cgdb on different operating systems:

  • If you're using Ubuntu, you can use the following command to install cgdb:
    $ sudo apt-get install cgdb
    

    Alternatively, you can download the source code from http://cgdb.github.com/, and perform the following instructions to install cgdb:

    $ ./configure --prefix=/usr/local
    $ make
    $ sudo make install
    

    Note that cgdb requires libreadline and ncurses development libraries.

  • If you're using Windows, a Windows binary is available at http://cgdb.sourceforge.net/download.php.
  • If you're using MacOS, you can use the MacPorts installation command as follows:
    $ sudo port install cgdb
    

Please read the Debugging Android NDK Application with NDK GDB recipe before going through this one.

How to do it...

The following steps enable cgdb for Android NDK application debugging:

  1. Make a copy of the ndk-gdb script under the Android NDK root directory. This can be done with the following command:
    $ cp $ANDROID_NDK/ndk-gdb $ANDROID_NDK/ndk-cgdb
    

    Here, $ANDROID_NDK refers to the Android NDK root directory.

  2. Change the following line in the ndk-cgdb script from:
    GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
    

    To the following:

    GDBCLIENT="cgdb -d ${TOOLCHAIN_PREFIX}gdb --"
    
  3. We'll use the project created in the Debugging Android NDK application with NDK GDB recipe. If you don't have the project open in your Eclipse IDE, click on File | Import. Select Existing Projects into Workspace under General, then click on Next. In the import window, check Select root directory, and browse to the HelloNDKGDB project. Click on Finish to import the project:
    How to do it...
  4. Run the application on an Android device. Then, start a termina, and enter the following command:
    ndk-cgdb
    

    The following is a screenshot of the cgdb interface:

    How to do it...
  5. We can issue gdb commands. Note that the upper-half of the window will mark the current execution line with an arrow and all the breakpoints with red.

How it works...

As shown in the preceding screenshot, cgdb provides a more intuitive interface for debugging the native code in Android. We can view the source code as we enter gdb commands. This recipe demonstrates the basic setup of cgdb for debugging the native code. The details of how to use cgdb can be found at its documentation available at http://cgdb.github.com/docs/cgdb.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset