Hertaville.com
  • Home
  • Categories
  • Tags
  • Archives

Remote Debugging of Raspberry Pi Applications From Eclipse

UPDATED JULY 15 2013

In this entry I will outline how one can utilize remote debugging tools available in Eclipse to debug applications from a Linux Desktop PC while running (in debug mode) on the Raspberry Pi. This tutorial assumes that you have already setup your Raspberry Pi cross compiling toolchain and Eclipse and that you are able to  build your applications in Eclipse (using the cross-compiling toolchain) as well as transfer the binary executables to the Raspberry Pi via the RSE plugin. If you have not done so, I urge you to look at the "Development Environment for the Raspberry Pi using a Cross Compiling Toolchain and Eclipse" since this tutorial is really a continuation of the tutorial in that blog entry.

To make things easy, I will use the same project created in that previous entry and modify the code slightly to the following:

#include <iostream>
#include <unistd.h>

using namespace std;

int main (void)
{
    int i = 0;
    cout << "Hello RPi Development World !"<< endl;

    cout << "Hello RPI Development World Again !" << endl;

    while(1)
    {
        cout << "I'm in the While loop and the value of variable i is: " << i << endl;
        i++;
        usleep(1000000); //wait for 1 seconds
    }

    return 0;

}

This gives us a slightly longer program that makes demoing the debug process easier. Before moving onto the debugging, make sure you clean the project and rebuild it. Then transfer the binary file to the Raspberry Pi via the RSE plugin  as instructed in the previous blog entry. In the Eclipse SSH terminal (on the Raspberry Pi) modify the binary "HelloWorld" file permissions to include "execute" with the command "chmod +x HelloWorld" then run the binary with "./HelloWorld" as shown in Figure 1.

Figure 1. Make sure Binary executable file runs properly
Figure 1. Make sure Binary executable file runs properly

If you are able to get to this point then you are ready to debug!!! There are two remote debug approaches that can be  utilised in Eclipse; automatic remote debugging and manual remote debugging. Automatic remote debugging allows Eclipse to initiate the GDB server on the Raspberry Pi remotely and then connect to it using the arm-linux-gnueabihf-gdb client running on the Linux Desktop PC. The advantage of Automatic remote debugging is that the entire setup on both the Raspberry Pi and Linux Desktop PC is taken care of by Eclipse running on the Linux Desktop PC. Having said that,  I do not recommend Automatic remote debugging because in some instances (if your application needs root access) Eclipse will require root access on the Raspberry Pi. This is a Linux security faux pas.  Instead I recommend manual remote debugging which is convenient to setup and is more secure.

Before moving onto setting up manual remote debugging in eclipse however let us make sure that our GDB client on our Linux PC is executable.

  • In a terminal, type the following command: "arm-linux-gnueabihf-gdb -v". If the cross compiling toolchain's binary directory is not included in the PATH variable (which it should be) then type "/home/halherta/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gdb -v". If this command runs successfully as shown in Figure 2, then we are ready to proceed.
Figure 2. GDB client binary (arm-linux-gnueabihf-gdb) executes successfully
Figure 2. GDB client binary (arm-linux-gnueabihf-gdb) executes successfully

When I first ran this command for the first time, I got the following error message:"error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory". This basically means that the GDB client "arm-linux-gnueabihf-gdb" requires the libncurses.so.5 shared library to run. So we will need to install it on our Linux Desktop PC. since the "arm-linux-gnueabihf-gdb"is a 32-bit binary, we must make sure that we include the 32-bit version of the libncurses library. If we're running a 64-bit Linux OS on our desktop we have to be wary of this. To install the 32-bit version of the libncurses library while running a  64-bit Linux OS we type the following command: "sudo apt-get install libncurses5:i386". The libncurses library is a pretty important library so I highly encourage you to have both the 64-bit & 32-bit versions of it available on your 64-bit Linux machine. This can be accomplished with the following command: "sudo apt-get install libncurses5:i386 libncurses5". Remember that on Debian & Crunchbang linux, we have to enable the 32-bit package repository before being able to complete this step. This is done by the following commands: “dpkg –add-architecture i386” and ” "apt-get update“; both of which require root access.

Manual Remote Debugging

Manual remote debugging requires that the user manually transfers the binary file onto the Raspberry Pi (via the RSE plugin) and that the user manually starts the GDB server on the Raspberry Pi.

  • First transfer the "HelloRPiWorld" binary file onto the Raspberry Pi via the RSE plugin.
  • Then make the "HelloRPiWorld" binary file executable with the "chmod +x HelloRPiWorld" command.
  • Then start the GDB server on the Raspberry Pi with the following command: "*sudo gdbserver 10.42.0.1:12345 HelloRPiWorld*".
    • We need to start the GDB server with root privileges using sudo. This is required in case the program requires user-restricted resources such as GPIO.
    • We then specify the IP address of the Linux Desktop PC (in my case 10.42.0.1);  where Eclipse and the cross-compiling toolchain is running from, along with the port number that the GDB server will be listening to. This number can be changed from "12345". But make sure that you do not choose a port number assigned to another application such as "80 (HTTP)" or "22 (SSH)".
    • Finally you need to specify the name of the binary file that will be debugged.  The GDB server needs to be started on the Raspberry Pi. This can done via the SSH terminal in Eclipse (RSE Plugin) as shown in Figure 3.
Figure 3. Starting the GDB server on the Raspberry Pi from Eclipse
Figure 3. Starting the GDB server on the Raspberry Pi from Eclipse
  • Go To "Debug Configurations" window by clickig on the "Run->Debug Configurations" menu items, and create a new remote debug configuration by double clicking on the "C/C++ Remote Application" option in the left frame.
  • Create a distinctive name for this debug configuration. I chose to call it "Manual Debug".

Figure 4. Debug Configuration Window. Manual Debug Figure 4. Debug Configuration Window. Manual Debug

  • Make sure that on the bottom of the Debug configuration Window it says "Using GDB (DSF) Manual Remote Debugging Launcher". If it does not, then click on the "Select Other" link (Figure 4). This will open the "Select Preferred Launcher Window". Then click on the change workplace settings" link and this will open the "Preferences (Filtered)" window. In this window highlight the word "[Debug]" under "C++ Remote Application" in the left frame(Launch Type/Mode). Finally in the right frame (Preferred Launcher), select the "GDB (DSF) Manual Remote Debugging Launcher" option as shown in Figure 5. Click the "Apply" button in the "Preferences (Filtered)" window and "OK" in the "Select Preferred Launcher" window. You should be back at the "Debug configurations" window with "GDB (DSF) Manual Remote Debugging Launcher" shown at the bottom of the window.
Figure 5. Selecting GDB (DSF) Manual Remote Debugging Launcher
Figure 5. Selecting GDB (DSF) Manual Remote Debugging Launcher
  • In the "Debug configurations" window, under the "Main" tab, make sure that all entries look identical to those in Figure 4.
Figure 6. Specify ARM specific GDB client from cross compiling toolchain
Figure 6. Specify ARM specific GDB client from cross compiling toolchain
  • In the "Debug configurations" window, under the "Debugger" tab and then "Main" sub-tab (Figure 6) , select the ARM specific GDB client "arm-linux-gnueabihf-gdb" from the cross compiling toolchain i.e. type "arm-linux-gnueabihf-gdb" in the "GDB debugger" field .
Figure 7. Specify RaspberryPi IP address and the port number that the GDB server is listening to.
Figure 7. Specify RaspberryPi IP address and the port number that the GDB server is listening to.
  • In the "Debug configurations" window, under the "Debug" tab and then "connection" sub-tab (Figure 7) , specify the Raspberry Pi's IP address  in the "Host name or IP address" field and the port number that the GDB server is listening to in the "Port number" field. In my case, the IP address of the Raspberry Pi is 10.42.0.73  and the GDB server is listening to port 12345 as specified earlier (see Figure 3).
Figure 8. "Add Manual Debug" configuration to toolbar.
Figure 8. "Add Manual Debug" configuration to toolbar.
  • In the "Debug configurations" window, under the "common" tab (Figure 8), check the "Debug" check-box under "Display in Favorites menu". This will make the "Manual Debug" Debug configuration accessible from the "green bug" icon in the Eclipse toolbar.
  • At this point you can click on the "Apply" button to apply all changes to the "Manual Debug" debug configuration.
  • To start this debug configuration, you can either click the "Debug" button in the "Debugger Configurations" window. You can also click the "Close" button in the "Debugger Configurations" window. Then click on the arrow sign to the right of the green bug icon in the Eclipse toolbar (Figure 9). This will open a small menu containing the "Manual Debug" debug configuration. You can then click on that to start the Debug session.
  • If everything goes OK, just before  the debug session starts, you will get a "Confirm Perspective Switch" window. This basically informs the user that the Eclipse perspective will be switched from the C/C++ perspective to the Debug Perspective. Click on the "Yes" button in this pop-up window and the Debug session will commence!
Figure 9 Accessing the "Manual Debug" debug configuration from the Eclipse toolbar
Figure 9 Accessing the "Manual Debug" debug configuration from the Eclipse toolbar

A Very Brief Introduction to Debugging in Eclipse

The debug session in Eclipse always commences in the debug perspective. In the top right, you’ll find several tabbed windows showing you program variables, breakpoints, registers and modules.  You can also add other windows displaying disassembly and/or memory windows by  clicking on the “show view” option under the “Window” menu and then selecting an item under the "Debug folder".

Additionally, there is a "Debug" toolbar that contains useful icons. The icons are numbered in Figure 10.

Figure 10. Debug Perspective
Figure 10. Debug Perspective
  1. This is the “step over” icon allowing you to step over functions or to the next instruction
  2. This is the “step into” icon allowing you to step into functions or to the next instruction
  3. This is the continue or Resume icon and will cause code to run.
  4. This icon halts execution and is selectable only while the program is running
  5. The red square button terminates debug mode and kills the GDB server and client applications.
  6. This icon disconnects the GDB client from the GDB Server.
  • To add a breakpoint just right click in the grey margin of the source code window and choose the “toggle breakpoint” option. Repeat the process to remove break points. So let’s add a break point at each "cout"  statement. Now click continue (icon 3).
  • After hitting the continue icon once, code execution will halt at the first breakpoint. Hitting the continue icon again repeatedly will cause code to halt at the next breakpoint an so forth. Congratulations! you just remotely debugged your first Raspberry Pi application!
  • Once you are done debugging, you can always stop debugging by pressing on the red square termination icon (icon 5) or icon 6 and switching the perspective back to C/C++  via the far top right icons in Figure 10 .

  • « Introduction to accessing the Raspberry Pi's GPIO in C++ (sysfs)
  • Interfacing an I2C GPIO expander (MCP23017) to the Raspberry Pi using C++ (i2cdev) »

Published

Jan 11, 2013

Category

Raspberry Pi

Tags

  • C++ 10
  • Cross compilation 4
  • Eclipse 4
  • GCC 11
  • GDB 4
  • Raspberry Pi 13
  • Powered by Pelican. Theme: Elegant by Talha Mansoor