You are about to dive into the arcane, but necessary world of remote debugging/cross-compiling. In this chapter, we will specifically take a look at a recipe for setting up remote debugging using the GDB server.
We will set up a specific kind of environment on your client desktop, another kind of environment on your BBB, and use an IDE as a way to ease the potential collision between these two worlds.
Wait a minute! Didn't we just do a debug recipe directly on BeagleBone Black using GDB? And can't we just develop and compile right on BeagleBone Black? Yes and yes.
But your BeagleBone Black is not quite powerful enough to serve as a serious development box. Ideally, what we want to do is hand off that work to a fancier, faster, desktop box. Otherwise, you will spend many unnecessary hours testing, compiling, debugging, and watching reruns of Doctor Who while waiting for the BBB to complete its tasks.
Why don't we just make something on one box and move it over to another box? Hmm. Not so fast. If you want to create or debug an application on a nonBBB board and output a binary or chunk of code to run it on an ARM Linux board like the BBB's, then you have to have the right toolset. This toolset will juggle between differing board architectures and operating system requirements.
Unfortunately, getting this toolset set up properly can be quite unpleasant. Not only does it take a fair bit of time, the process is also fraught with many opportunities for error. So, be patient and persistent in following this recipe.
Before going into the minutiae, here are the high-level steps to get remote debugging going on BeagleBone Black:
Nothing to it, right? Not quite...
Part I: Set up Virtual Machine—Mac OSX
On your client box, we will set up a virtual machine (VM) with a Linux distribution on board. There are a number of sources for this, but we will use Oracle's free virtual box (once again, if you are already using this book and its recipes with a Linux box as your client machine, installing a VM is not necessary).
Download and install the VM from https://www.virtualbox.org/. When finished, open the application. Perform the following steps
.iso
you downloadedFollow these settings:
System | Storage | Controller IDE—Click on the (+) sign (Add CD/DVD device) to open a browse directory window. Click on Choose Disk.
Navigate to the folder where you downloaded the .iso
file of your preferred Linux distro. The filename will be something similar to debian-x.y.z-i386-netinst.iso
or some variant on the architecture that you chose. Then, click on Open.
Your OS should now be loaded. But now, we need to have the OS actually installed in its normal fashion on the virtual drive that you set up. Perform the following steps:
You will then be taken through quite a few setup windows, including choosing language, region and keyboard preferences, network options, password and user setup, and other configuration options that I will not elaborate here. Configure as you see fit, while clicking on the Continue button until you end up with the hard disk partitioning sequence. Make your choices there according to your preference. In most cases, choosing the default option should be fine.
$ ping 192.168.7.2
You should get consistent pings.
Adding cut and paste to VM
We now need to implement an additional functionality to the virtual box that will drive you mad if you do not add it: the cut-and-paste function. When you use Mac OS X as your client machine, Virtual Box, like most virtual machine software, does not include cut-and-paste by default. Instead, they have given us a series of 15 steps (yes, 15!). Oracle/Virtual Box refers to this function as Guest Additions. Here we go:
$ sudo -i ~# apt-get install dkms
.iso
file that contains the code to add the guest additions with its cut and paste feature. In Finder, navigate to Applications | Virtualbox.app | Show Package Contents.This will open all the hidden directories in the package, including our target directory and file.
Then, navigate to Contents | MacOS | VBoxGuestAdditions.iso.
VBoxGuestAdditions.iso
file to a place where you can find it on your machine.Another way to perform the same thing is from the drop-down menus at the top of the screen (not in the VM/Debian window, but the virtual machine menu options at the very top of your desktop machine. There, you can choose Devices | CD/DVD devices | IDE (IDE Primary Master) | Choose a Virtual/CD/DVD disk file.
VBoxGuestAdditions.iso
file and select it. The .iso
file should now appear in the CD/DVD drive options. Click on OK and restart the VM.~# apt-get update ~# apt-get upgrade
make
and GNU C compiler installed, which should already be on the distribution that you installed. Check it with the following code:~# gcc --version
command not found
error, you will need to install it as follows:~# apt-get install make gcc
~# uname -a
linux-headers
package. Replace the following version numbers with the version number shown in the prior step:~# apt-get install linux-headers-2.6.26-2-68
This may take some time; just wait for the installation to finish and return to your terminal prompt.
# reboot
The cut-and-paste functionality between your client host box (in our case, Mac OS X) and your virtual machine environment should now be enabled.
Ground Control
and then select Edit | Copy. Then, navigate to your client desktop (Mac/Windows machine, and so on), open a simple text editor, and select Paste. Hopefully, Ground Control
will be pasted to the window.Major Tom
in (Mac/Windows) text editor on the desktop and press Ctrl + c (copy). Now, switch back to the VM editor window and then select Edit | Paste. Major Tom
should be pasted successfully to the gedit
window.# sudo -i
sudo
group as follows:# adduser username sudo
fdisk
command to check whether sudo
is working properly. The -v
option will check the version of fdisk
installed on your VM. You should get a prompt for a user password, as shown in the following code:# sudo fdisk -v
The screen output should show the version of fdisk
that is installed, and there should be no errors about permissions.
ssh
. We already use it on the BBB. Now, we need to add it to our VM. SSH gives Eclipse the ability to connect and transfer files to BeagleBone Black using scp
, a secure network protocol. Install it with the following code:# sudo apt-get install ssh
Part II: Installing gdbserver on BeagleBone Black
We need this package on board the BBB so that we can run gdb
from afar or from another box. Perform the following steps:
gdbserver
for armhf
(the BBB-native architecture) on your VM as follows:# wget http://http.us.debian.org/debian/pool/main/g/gdb/gdbserver_7.4.1+dfsg-0.1_armhf.deb
scp
to transfer the package to the BBB, as shown in the following code:# scp gdbserver_7.4.1+dfsg-0.1_armhf.deb [email protected]:~
# ssh [email protected] root@arm# dpkg -i gdbserver_7.4.1+dfsg-0.1_armhf.deb
root@arm# exit
Part III: Installing and setting up the toolchain
This part and the next one are the hairiest bits of the process; many online tutorials on this subject purport to deliver a working toolchain lashed to an IDE, though few of them actually deliver.
Fortunately for us, engineer and blogger Aaron Clarke's very clear steps at http://bit.ly/1x7TGUY provide superb guidelines for a successful toolchain and the Eclipse IDE setup. The vast majority of the credit for the next two parts of the recipe go to Mr. Clarke.
We chose not to include screenshots of the process because doing so would have made the recipe considerably longer. However, you can find a more graphical version of this recipe online at Aaron Clarke's blog, which includes a version for Debian Jessie at http://blog.embeddedcoding.com/2015/05/beaglebone-black-development-part-6.html.
Here we go.
Perform the following steps:
$ sudo -i
root@debian:/home/user# mkdir bbb-tools root@debian:/home/user# cd bbb-tools
wget
, download the toolchain from the following link to the directory that we just created:# wget -c https://launchpad.net/linaro-toolchain-binaries/trunk/2013.10/+download/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux.tar.xz
# tar xf gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux.tar.xz
echo
command to create the bbb-vars
file; this will be used to test that our environment is set up properly, as shown in the following code:# echo "export CC=`pwd`/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf-" > bbb-vars
bbb-vars
file should appear in the directory as follows:root@debian:/home/user/bbb-tools# ls bbb-vars
bbb-vars
file. Take a look at the command preceding the (.
) is the file location:root@debian:~# ./home/user/bbb-tools/bbb-vars
root@debian:~# ${CC}gcc –-version arm-linux-gnueabihf-gcc (crosstool-NG linaro-1.13.1-4.8-2013.10 - Linaro GCC 2013.10) 4.8.2 20131014 (prerelease)Copyright (C) 2013 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
Part IV: Installing and setting up the Eclipse IDE
Perform the following steps:
root@debian:~# sudo apt-get install eclipse eclipse-cdt
Then, open Eclipse and set up a workspace by closing the welcome screen and navigating to Window | Show view | C/C++ Projects.
bbb_ground_control
.
Prefix: arm-linux-gnueabihf-
Path: /home/user/bbb-tools/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin
Prefix: arm-linux-gnueabihf-
Path: /home/cah/bbb-tools/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin
Symbol 'EXIT_SUCCESS' could not be resolved
. Aaargh!The error says that some C code symbols cannot be resolved by the compiler. Not to worry though. This error can be ignored because according to the Eclipse documentation, it is inaccurate and does not affect the successful operation of the application.
gcc
compiler as follows:00:24:52 **** Build of configuration Debug for project bbb_ground_control ****make all Building file: ../src/bbb_ground_control.cInvoking: Cross GCC Compilerarm-linux-gnueabihf-gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/ bbb_ground_control.d" -MT"src/ bbb_ground_control.d" -o "src/ bbb_ground_control.o" "../src/ bbb_ground_control.c"Finished building: ../src/ bbb_ground_control.c Building target: bbb_ground_control Invoking: Cross GCC Linkerarm-linux-gnueabihf-gcc -o " bbb_ground_control " ./src/ bbb_ground_control.o Finished building target: bbb_ground_control 00:24:52 Build Finished (took 172ms)
BeagleBone Black
:192.168.2.7
Beaglebone Black
Beaglebone Black
select.ssh
files and then click on Next.processes.shel.linux
. Then, click on Next.ssh.shells
. Then, click on Next.ssh.terminals
and click on Finish./root
.In the following steps, we will perform two key things: configure our remote paths on the BBB and give our application permission to execute.
remote absolute file path for C/C++ application: /root/test_project/bbb_ground_control
commands to execute before application: chmod +x /root/test_project/bbb_ground_control
Choose a path on BeagleBone Black where the bbb_ground_control
executable file will be placed. In our example, it is /root/test_project/bbb_ground_control
, where bbb_ground_control
is the name of the binary file that we will compile. The purpose of the chmod +x
command is to give our program permission to execute.
# sudo ssh 192.168.7.2
yes
when prompted are you sure
.root
. You should now be logged in to the BBB.root@beaglebone:~# mkdir test_project root@beaglebone:~# cd test_project root@beaglebone:~# chmod a+w .
root@beaglebone:~# exit
root
. Click on them and agree to the next few prompts from the BBB.The last few steps are focused on how to set up a remote debug session.
Debug
path with the following code:# cd ~/bbb-eclipse-workspace/bbb_ground_control/Debug
.gdbinit
. This is an initialization file that resides in the debug directory and automatically loads and parses when you run GDB.Typically, you would populate this file with any required commands. However, for our current recipe, the file remains empty, although it needs to remain present for GDB's proper operation.
nano
to create the file because we used this editor in the earlier chapters. However, there is another command that actually generates the file on the fly, saves it, and places it in whatever directory you have entered, as shown in the following code:# touch .gdbinit
# ls
Hmm. It isn't there!
To figure out what is happening, this is a case where using different options with a command is vital. With the ls
command, we want to append the command with the -a
option. So, try again using the following code:
# ls -a
The gdbinit
file should now appear in the directory's lineup.
Hold on tight. Almost done.
GDB Debugger: /home/user/bbb-tools/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf-gdb
GDB command file: /home/user/bbb-eclipse-workspace/bbb_ground_control/Debug/.gdbinit
So, at the bottom of the still open window, click on Debug. Hold your breath! This will start the remote session with the BBB and finish the cycle just after the main()
line in your .c
file.
If all went as planned and the gremlins stay home, you should see the following output on your Eclipse console screen:
Last login: DAY MO DATE TIME YEAR from 192.168.7.1 echo $PWD'>' chmod +x /root/test_project/bbb_ground_control;gdbserver :2345 /root/test_project/bbb_ground_control;exit root@beaglebone:~# echo $PWD'>' /root> root@beaglebone:~# chmod +x /root/test_project/bbb_ground_control;gdbserver :2345 /root/test_project/bbb_ground_control;exit Process /root/test_project/bbb_ground_control created; pid = 2910 Listening on port 2345 Remote debugging from host 192.168.7.1
bbb_ground_control.c
window is selected. Then, at the top, choose Run.# Go to actual cabinet on actual shelf # Take out bottle of single malt scotch # Pour dram, perhaps two # Lean back head and drink # Congratulate Major Tom on a good test flight
A thorough review of how to debug is beyond the scope of this book. Fortunately, there are ample resources to learn more. Here are a few:
.gdbinit
file, refer to http://www.dirac.org/linux/gdb/03-Initialization,_Listing,_And_Running.php3.145.69.255