Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / virtual-machine

Debugging Android NDK 5 (b) C++ Multithreaded Applications Manually

4.67/5 (3 votes)
6 Feb 2011CPOL2 min read 26K  
Make this ndk-gdb finally work with threads

Well, I am once again confronted with debugging heavily multi threaded C++ applications in Android 2.2. Now it is NDK5(b) and NOT NDK4 as in previous posts. I have seen on the readme of NDK5 that in Android 2.3+, the multi-threaded debugging problem has gone away completely but unfortunately we MUST stick with the 2.2 platform for now.


After move to NDK5, we decided to use the CMake for doing Android builds as it allows us to get one build system for a lot of cross platform (not Android only) components. Unfortunately, this way of building does not give me the possibility to debug application using ndk-gdb. So I had to write my own awkward scripts to ease the debugging process. Now please welcome the complete solution for gdb:



  1. Get the gdbserver from the Android sources as described in my previous post “Yes I Can Debug Multi-threaded C++ JNI Code on Android“.
  2. Upload this gdbserver to the running emulator by typing adb push host/path/to/gdbserver /data/gdbserver in your command prompt.
  3. Get the system libraries from the emulator to path/to/your/project/obj/local/armeabi folder by typing adb pull /system/lib path/to/your/project/obj/local/armeabi.
  4. Get the Dalvik Virtual Machine app_process binary from the emulator to path/to/your/project/obj/local/armeabi folder by typing adb pull /system/bin/app_process path/to/your/project/obj/local/armeabi.
  5. Forward the ports with the help of adb by typing adb forward tcp:1234 tcp:1234 in the command prompt.
  6. Find the PID of the process you wish to attach to (I debug using attaching… never tried to debug starting of the process, fill free to comment if you know how).
  7. Attach the gdbserver to the running process by typing adb shell /data/gdbserver tcp:1234 --attach PID. Leave the command prompt where you typed it running and open another one.
  8. Type /path/to/ndk5/build/prebuilt/linux-x86/arm-linux-androideabi-4.4.3/bin/arm-linux-androideabi-gdb path/to/your/project/obj/local/armeabi/app_process --command=path/to/your/project/obj/local/armeabi/gdb.setup. The gdb starts and breaks waiting for the instructions, type target remote tcp:1234. Press Enter.

And DADA! Yes, you are able to see the threads, switch between them, see there call stacks and do other much more important things that doing this stupid manual setup… brr, please feel free to correct me if I am wrong.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)