Remote cross debugging: Windows to Raspberry-Pi

In the past few days I’ve spent quite a lot of time on my new shining Raspberry-Pi. It’s an amazing little devil, worth every Euro I spent! Since I’ve planned to deploy a software component of my augmented reality system on this board, I needed to figure out how to remotely debug it in order to troubleshoot any issue I would run into. It took me some time, but thanks to the friends on Raspberry Pi Forum and to this awesome tutorial I finally made it: I can deploy my software to the raspberry-pi from Eclipse CDT and debug from my comfortable Windows 7 machine.

Step 1. The first thing you need to do is to setup a working Cygwin environment on your machine. I setup mine by following the advices from IanLinsdell and CopperPhil on the Raspberry Pi forum. Simply copy Cygwin installer to a folder (es. c:\cygwin) and execute it. Be sure to install the python package, the shell and the make tool.
Step 2. The next thing you need to do is to install the required cross-compilation toolchain. That is needed since we are on Windows and we need to compile and debug a software which will run on an ARM machine (our Raspberry Pi). Instead of building your own toolchain, you can download the ones built by a Raspberry Pi Forum user, IanLinsdell, available from his github account here. Since we want to cross-debug, download the hardfp version. Unpack the archive in c:/cygwin/opt/cross/x-tools so that the full path will look like this one c:/cygwin/opt/cross/x-tools/arm-unknown-linux-gnueabi.

Step 3. In order to upload the binaries we will compile to the Raspberry Pi, we need to enable an SSH server on the board. If you installed the Raspbian image, this operation is as easy as running the raspi-config command and selecting the ssh row. Once the server is running, you can use your username and password (default pi/raspberry) to access the Raspberry Pi from your Windows machine, using an SSH terminal.

Step 4. Download and install PUTTY (the full package), which is needed to talk to our Raspberry Pi using an SSH terminal. In order to upload our binaries to the Raspberry Pi and start the debugger, I thought of a little hack: I created a script which run as a post-build step in Eclipse (i.e. gets executed when the compilation successfully produces an artifact/binary) and uploads the file on the board using an SSH connection and executes a few commands after that (chmod and gdbserver). The following is the content of the BAT file, which you can save in a file and path of your choice. In my case I saved it to C:/Users/Dexter/Desktop/RaspberryPi/deploy_debug.bat, but any path will do.

@echo off

rem Check for a valid filename
IF "%1"=="" (
	ECHO You have to provide a valid source file.
	EXIT /b
)

IF "%2"=="" (
	ECHO You have to provide a valid destination path.
	EXIT /b
)

SET PUTTYSCP_BIN="C:\Program Files (x86)\PuTTY\pscp.exe"
SET PUTTY_BIN="C:\Program Files (x86)\PuTTY\putty.exe"
SET RASPBERRYPI_ADDR=192.168.0.123
SET USERNAME=pi
SET PASSWORD=raspberry
SET CMD_FILENAME=commands.sh

rem Upload the file to raspberry pi
%PUTTYSCP_BIN% -pw %PASSWORD% "%1" %USERNAME%@%RASPBERRYPI_ADDR%:"%2"

rem Build a list of actions to do on the pi (chmod, execute GDB server)
if exist %~dp0%CMD_FILENAME% del %~dp0%CMD_FILENAME%
rem echo rm "%2" >> %~dp0%CMD_FILENAME%
echo chmod +x "%2" >> %~dp0%CMD_FILENAME%
echo gdbserver :3785 "%2" >> %~dp0%CMD_FILENAME%

rem Execute the action list on the raspberry pi
%PUTTY_BIN% -pw %PASSWORD% -m %~dp0%CMD_FILENAME% %USERNAME%@%RASPBERRYPI_ADDR%

exit /b %ERRORLEVEL%

After saving the file, the following settings needs to be tweaked:

  • PUTTYSCP_BIN – The full path to pscp.exe, part of the Putty bundle, used to securely copy files over a SSH connection.
  • PUTTY_BIN – The full path to putty.exe, part of the Putty bundle, used to execute commands on a remote host over a SSH connection.
  • RASPBERRYPI_ADDR – The IP address of the Raspberry Pi
  • USERNAME – The username of the user who will authenticate on the Raspberry Pi
  • PASSWORD – The password :-)
  • CMD_FILENAME – The name of the file which will contains all the commands to be executed on the Raspberry Pi. You don’t want to change that, it’s not really nedded.

Step 5. Download and install Eclipse IDE for C/C++ Developers (CDT). Create a sample “Hello World” project, let’s say its name is “RaspHelloWorld”. Open the project properties by right clicking on the project name inside the Project Explorer. Configure the project as follows.

C/C++ Build -> Environment

  • Add a variable named CYGWIN with value nodosfilewarning.
  • Append c:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\bin;c:\cygwin\bin; to the beginning of the PATH variable.

C/C++ Build -> Tool Chain Editor

  • Select Cygwin GCC from Current toolchain dropbox.

C/C++ Build -> Settings -> Tool Settings

  • Set the Command field in Cygwin C++ Compiler to arm-unknown-linux-gnueabi-g++.
  • Add an include path in Includes which points to c:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\sysroot\usr\include.
  • Set the Command field in Cygwin C Compiler to arm-unknown-linux-gnueabi-g++.
  • Add an include path in Includes which points to c:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\sysroot\usr\include.
  • Set the Command field in Cygwin C++ Linker to arm-unknown-linux-gnueabi-g++.
  • Add a library search path in Libraries which points to c:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\sysroot\usr\lib.

C/C++ Build -> Settings -> Build Steps

  • Set the Command field in Post-build steps to C:/Users/Dexter/Desktop/RaspberryPi/deploy_debug.bat $(OutDir)${ProjName}.a “/home/pi/projects/${ProjName}.a”.
  • Add a brief description inside the Description field.

C/C++ Build -> Settings -> Build Artifact

  • Set Artifact Extension to .a

Step 6. Compile your sample project. If everything went smoothly, your Console output in Eclipse should also print something about the binary uploading and command execution. Now it’s time to setup a Debug Configuration. Select Run -> Debug configurations from the top menu. Right click on C/C++ Remote Application and choose New to add a new configuration.

Main tab

  • Set the C/C++ Application field to the name of the produced binary (in my case to Debug/RaspHelloWorld.a).
  • Select Disable auto build.
  • Click on Select other…, near the left of the Apply button. From the newly opened window, choose GDB (DSF) Manual Remote Debugging Launcher and click ok.

Debugger tab

  • Set the GDB debugger field to the path of the gdb which is part of our downloaded tool chain, that is to say c:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\bin\arm-unknown-linux-gnueabi-gdb.exe.
  • In the Connection sub tab, set the Type to TCP, the Host name or IP address field to the IP address of the Rapsberry Pi (the same you specified in the BAT settings) and Port number to 3785.

Click on Apply and then Debug. The debugger should start and Eclipse should switch to the Debug Perspective. We made it ;-) If you have any issue and it doesn’t work as expected, feel free to post a comment below!

Update 01/11/2012: As suggested in the comments (thanks Ivo), disabling the “Automate discovery of paths and symbols” option for the C and/or C++ compiler could fix the problem with missing cout or endl. This option can be found in the project properties “c/c++ Build/Discovery Option”. Once disable, click on the “Clear” button to remove all the unwanted include paths introduced into the project.

  • Quentin Lohri

    Hi,

    When I compile, finished build target is ok!
    But I’ve a problem with post-build.
    Problem:

    /usr/bin/make –no-print-directory post-build
    upload and debug
    C:/Raspi Studio/deploy_debug.bat/helloRaspi.a “/home/pi/projects/helloRaspi.a”
    /bin/sh: C:/Raspi: No such file or directory
    makefile:43: recipe for target ‘post-build’ failed
    make[1]: [post-build] Error 127 (ignored)

    Idea? It’s 2 day I look for this :(

    Thanks

  • http://www.a2p.it/ Alessio Placitelli

    Hi, try renaming the “Raspi Studio” folder to something without a space (i.e. “RaspiStudio”).

  • Quentin Lohri

    Hi, thank you for the response.

    I’ve rename the folder without space and also put C:cygwinbin and C:cygwinoptcrossx-tools….bin in my system PATH… python version 2.6.8-2 and cygwin32… On my raspi, upgrade gdb, create folder /home/pi/projects with all access…

    I’don’t understand… I look again for a solution…

    Here is my deploy_debug.bat file perhaps somebody see somethings:

    @echo off

    rem Check for a valid filename

    IF “%1″==”” (

    ECHO You have to provide a valid source file.

    EXIT /b

    )

    IF “%2″==”” (

    ECHO You have to provide a valid destination path.

    EXIT /b

    )

    SET PUTTYSCP_BIN=”C:Program Files (x86)PuTTYpscp.exe”

    SET PUTTY_BIN=”C:Program Files (x86)PuTTYputty.exe”

    SET RASPBERRYPI_ADDR=192.168.192.57

    SET USERNAME=pi

    SET PASSWORD=xxxxxxxxxxx

    SET CMD_FILENAME=commands.sh

    rem Upload the file to raspberry pi

    %PUTTYSCP_BIN% -pw %PASSWORD% “%1″ %USERNAME%@%RASPBERRYPI_ADDR%:”%2″

    rem Build a list of actions to do on the pi (chmod, execute GDB server)

    if exist %~dp0%CMD_FILENAME% del %~dp0%CMD_FILENAME%

    rem echo rm “%2″ >> %~dp0%CMD_FILENAME%

    echo chmod +x “%2″ >> %~dp0%CMD_FILENAME%

    echo gdbserver :3785 “%2″ >> %~dp0%CMD_FILENAME%

    rem Execute the action list on the raspberry pi

    %PUTTY_BIN% -pw %PASSWORD% -m %~dp0%CMD_FILENAME% %USERNAME%@%RASPBERRYPI_ADDR%

    exit /b %ERRORLEVEL%

    Thank you in advance

  • http://www.a2p.it/ Alessio Placitelli

    Do you get the same error you were getting before?

  • Quentin Lohri

    Hi thank you for your response.

    I’ve rename the folder without space and the problem is the same…
    For information I’ve put also in the PATH system C:cygwinbin and C:cygwinoptcrossx-tools…bin.
    My deploy_debug.bat in the same of this in the tutorial, with correct setting. Python version 2.6.8-2.
    On raspi I’ve upgrate gdb and create a folder /home/pi/projects with all access….

    I’don’t understand why post build failed….

    I look again… but if somebody have an idea…?

    Thank

  • http://www.a2p.it/ Alessio Placitelli

    Would you please paste the complete error?

  • Quentin Lohri

    Here is build console…

    Thank for your help

    08:56:52 **** Incremental Build of configuration Debug for project helloRaspi ****

    make all

    Building file: ../src/helloRaspi.c

    Invoking: Cygwin C Compiler

    arm-unknown-linux-gnueabi-g++ -I”C:cygwinoptcrossx-toolsarm-unknown-linux-gnueabi-hardfp-gdbV1arm-unknown-linux-gnueabiarm-unknown-linux-gnueabisysrootusrinclude” -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF”src/helloRaspi.d” -MT”src/helloRaspi.d” -o “src/helloRaspi.o” “../src/helloRaspi.c”

    Finished building: ../src/helloRaspi.c

    Building target: helloRaspi.a

    Invoking: Cygwin C Linker

    arm-unknown-linux-gnueabi-g++ -L”C:cygwinoptcrossx-toolsarm-unknown-linux-gnueabi-hardfp-gdbV1arm-unknown-linux-gnueabiarm-unknown-linux-gnueabisysrootusrlib” -o “helloRaspi.a” ./src/helloRaspi.o

    Finished building target: helloRaspi.a

    /usr/bin/make –no-print-directory post-build

    upload and debug

    C:/RaspiStudio/deploy_debug.bat/helloRaspi.a “/home/pi/projects/helloRaspi.a”

    /bin/sh: C:/RaspiStudio/deploy_debug.bat/helloRaspi.a: No such file or directory

    makefile:43: recipe for target ‘post-build’ failed

    make[1]: [post-build] Error 127 (ignored)

    08:56:55 Build Finished (took 3s.99ms)

  • http://www.a2p.it/ Alessio Placitelli

    Well, that’s a different error than the one you had. Please, double check what you set the Command field in Post-build steps. Post it here.

  • Quentin Lohri

    Here is my post-build command:
    C:/RaspiStudio/deploy_debug.bat/$(OutDir)${ProjName}.a “/home/pi/projects/${ProjName}.a”

    and I’ve set:
    Artifact extension: a
    Artifact type: executable
    Artifact name: ${ProjName}

  • http://www.a2p.it/ Alessio Placitelli

    Well, your post build is wrong. There’s a “/” more.

    C:/RaspiStudio/deploy_debug.bat $(OutDir)${ProjName}.a “/home/pi/projects/${ProjName}.a”

  • Quentin Lohri

    Oh yes, it’s better. I’ve now another error in console.

    /usr/bin/make –no-print-directory post-build

    upload and debug
    C:/RaspiStudio/deploy_debug.bat helloRaspi.a “/home/pi/projects/helloRaspi.a”
    The system cannot find the path specified.
    The system cannot find the path specified.
    makefile:43: recipe for target ‘post-build’ failed
    make[1]: [post-build] Error 3 (ignored)

    I’ve put also C:RaspiStudio in the system PATH… But nothing change…

    Anew thank you for your help…

  • Quentin Lohri

    Ok!!!

    Very nice… the problem was with the deploy_debug.bat!!

    The specified path for putty was wrong!!!

    Thank you very much for your support and tutorial!

    Quentin

  • Quentin Lohri

    Hi,

    I’ve the same problem with the “h” and putty console close after few seconds…

    here my build console:

    /usr/bin/make –no-print-directory post-build
    upload and debug
    E:/RaspBerry/deploy_debug.bat helloRaspi.a “/home/pi/projects/helloRaspi.a”
    pscp: unable to open ?/home/pi/projects/helloRaspi.a?: no such file or directory

    when i’m connect with putty I can creat a file in my projects folder….

    where can I find the file command.sh?

    Thank you

  • http://www.a2p.it/ Alessio Placitelli

    Just replace the double quotes with valid double quotes.

  • Quentin Lohri

    Hi,
    I can upload and execute my helloRaspi.a on my RPI, no problem…
    Now I’ve configurate my debug and I can launch it.

    But when I want resume it he don’t work. I’ve message:
    No source available for “0xb6deb45c”.

    And in the console:
    The target endianness is set automatically (currently little endian)
    Cannot access memory at address 0x0
    Cannot access memory at address 0x0
    Cannot access memory at address 0x0

    In the debug tag: One thing is strange:
    Thread1: Suspended: signal SIGPIPE: broken pipe

    There’s a procedure to resume the debug?
    or have you an idea about my problem?

    Thanks a lot in advance….

    Quentin

  • MichaW

    Hello,
    I have the same problem. I rechecked my setup. Everything seems to be ok. My Eclipse version is Kepler.

    Any idea?

  • http://www.a2p.it/ Alessio Placitelli

    I’m sorry, unfortunately I don’t know how to solve this problem :( If you do find a solution, please post it here as well!

  • MichaW

    I rechecked my config again. Instead of adding a library search path I added a path for the library. That was wrong – my mistake. After correcting it works fine. Thanx!!!

  • Cristiano

    Your tutorial is great!
    Following your directions I can now build the C++ project with Eclipse and download to the rPI at once.

    The only problem is I can not start the Debugger and
    I get the following error message:
    Could not determine GDB version using command: C:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\bin\arm-unknown-linux-gnueabi-gdb.exe –version

    I wonder if I need to change the GBD command line entry in
    Debug Configurations->Debugger->Main
    The default is .gdbinit but I don’t know if this is correct.

    Thank you in advance for any help

  • shordie

    hey. i got a problem with the installation of the version 2.6 of the python language interpreter. In my setup-x86.exe by cygwin theres just the 2.7.3-1 and the 2.7.8-1 versions available. Does anyone have an idea how i cold install the version 2.6? Thx

  • relliky

    I have the same problem… By reading through cygwin web, https://cygwin.com/faq.html#faq.setup.old-versions 2.6.Can I use Cygwin Setup to get old versions of packages (like gcc-2.95)? And it basically says it is hard to find python 2.6 anymore. I have never find any copy of it by googling it for a couple of hours. Hope anyone can upload a copy of it later.

  • Pingback: how to modify compiler with cmake for cross-compiling [closed] | Zetina Answers()