Lab04_IntroToCodeDevelopmentUsingYocto
Lab04_IntroToCodeDevelopmentUsingYocto
GOAL
The goal of this Lab is to allow you to set up the development environment using Yocto to write and cross-compile
applications for the embedded Linux running on the Raspberry Pi 3 and to perform remote debugging operations.
PREREQUISITES
WORKPLACE SETUP
We assume that:
Please refer to the first lab for instructions on how to obtain the above configuration.
To debug application running on the Raspberry Pi 3, a new package shall be added to the Yocto configuration so
that debug tools are installed in the embedded Linux distribution. For this purpose, edit the file
raspberryPi3 /build/conf/local.conf to have the following content:
MACHINE ??= 'raspberrypi3'
DISTRO ?= 'poky'
PACKAGE_CLASSES ?= "package_rpm"
EXTRA_IMAGE_FEATURES = "debug-tweaks tools-debug"
USER_CLASSES ?= "buildstats image-mklibs"
PATCHRESOLVE = "noop"
BB_DISKMON_DIRS = "\
STOPTASKS,${TMPDIR},1G,100K \
STOPTASKS,${DL_DIR},1G,100K \
STOPTASKS,${SSTATE_DIR},1G,100K \
STOPTASKS,/tmp,100M,100K \
ABORT,${TMPDIR},100M,1K \
ABORT,${DL_DIR},100M,1K \
ABORT,${SSTATE_DIR},100M,1K \
ABORT,/tmp,10M,1K"
PACKAGECONFIG_append_pn-qemu-native = " sdl"
PACKAGECONFIG_append_pn-nativesdk-qemu = " sdl"
CONF_VERSION = "1"
ENABLE_UART = "1"
DL_DIR ?= "${BSPDIR}/downloads/"
The new configuration adds the tools-debug package that provides the target with debug support.
cd ~/raspberryPi3
After a while, a new Micro SD card image would be available, which you can deploy in the Micro SD as follows
(assuming the Micro SD is available to the PC as /dev/sdN). Alternatively, use a program of your preference to
flash the image.
sudo fdisk -l
command to determine which device to flash to (plug in and unplug the SD card to determine which device it is).
For this example, the SD card is under the name “sdc” (this may be different in your environment). Next, ensure
that the device is unmounted. This can be done using the command:
Once this is done, the following command can be used to copy the image across to the SD card (substitute any
folder names and device names to ensure they are relevant to your specific environment).
Also note that, this time, the most recently built image should be in the “tmp” folder, not “tmp-glibc”.
You are now ready to compile the cross-compiler environment that allows building programs compatible with the
just compiled embedded Linux system.
cd ~/raspberryPi3
cd ~/raspberryPi3/rpi-build/tmp/deploy/sdk/
./poky-glibc-x86_64-rpi-basic-image-cortexa7hf-neon-vfpv4-toolchain-2.1.3.sh
The cross-compiler environment is installed into /opt/poky/2.1.3/, and a shell script is provided to set up the
variables needed to use it.
Before using the cross-compiler environment, the needed variables shall be set up by running the following
command:
sudo chmod +x /opt/poky/2.1.3/environment-setup-cortexa7hf-neon-vfpv4-poky-linux-
gnueabi
. /opt/poky/2.1.3/environment-setup-cortexa7hf-neon-vfpv4-poky-linux-gnueabi
Create a new directory and populate it with a simple “Hello World” application stored in the file hello.c, having
the following content:
#include <stdio.h>
return 0;
}
Now, prepare the file Makefile with the following content (as before, ensure the lines are indented with tabs, not
spaces):
C=arm-poky-linux-gnueabi-gcc --sysroot=/opt/poky/2.1.3/sysroots/cortexa9hf-neon-vfpv4-
poky-linux-gnueabi/ -mfloat-abi=hard
LD=arm-poky-linux-gnueabi-ld --sysroot=/opt/poky/2.1.3/sysroots/cortexa9hf-neon-vfpv4-
poky-linux-gnueabi/
CFLAGS=-O2 -pipe -g -feliminate-unused-debug-types
CXXFLAGS=-O2 -pipe -g -feliminate-unused-debug-types
all: hello
hello.o: hello.c
hello: hello.o
$(CC) $(CFLAGS) -o hello hello.o
clean:
rm hello hello.o
The file instructs how to run the cross-compiler (arm-poky-linux-gnueacbi-gcc) to generate the executable
file to be executed in the Raspberry Pi 3.
make
In case the cross-compilation is executed correctly, the file hello will be created.
The executable program can be transferred on the Raspberry Pi 3 using the scp command as follows:
If the operation succeeds, you will find the hello executable in the /home/root directory on the Raspberry Pi
3.
It might throw a warning: if this is the case, you can add the key host as suggested in the message.
We are now ready to remotely debug the application using the command-line debugger that comes with the cross-
compilation environment.
The server for the gnu debugger (gdbserver) is executed to debug the hello program, awaiting connection on
the localhost at port 2000.
This will load the gnu debugger cross-compiled for the embedded Linux running on the Raspberry Pi 3 (arm-poky-
linux-gnueabi-gdb).
Once the debugger is running, we need to connect to gdbserver for performing a remote debugging session. For
this purpose, the command target remote 192.168.1.2:2000 is used.
You can now debug the program using the gdb commands. For example, type the following commands:
(gdb) list
1 #include <stdio.h>
2
3 int main( void )
4 {
5 printf( "Hello, World!\n" );
6
7 return 0;
8 }
9
10
The list command shows you the source code of the program.
We can then set up a break point with the break command as follows:
(gdb) break 5
Breakpoint 1 at 0x102f4: file hello.c, line 5.
We are not ready to run the program till the break point is reached:
(gdb) continue
Continuing.
warning: Could not load shared library symbols for 2 libraries, e.g. /lib/libc.so.6.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
To advance the execution step by step, you can use the next command. When the printf() instruction is
executed, you can see the “Hello, World!” message on the Raspberry Pi 3 terminal.
To terminate the debug session, you can use the quit command.
In case your program makes use of variables, you can inspect them with the print command (please refer to the
online help for details on the syntax, using the command help print).