Debugging Guide For GDB and Eclipse
Debugging Guide For GDB and Eclipse
by Brian Fraser
Last update: September 20, 2015
Table of Contents
1. Installing gdb-multiarch.........................................................................................................................2
2. GDB.......................................................................................................................................................3
3. Eclipse....................................................................................................................................................5
3.1 Eclipse Installation and Project Setup............................................................................................5
3.2 Debugging with Eclipse.................................................................................................................6
4. Core Dumps...........................................................................................................................................9
5. Stripping a Binary................................................................................................................................10
Note: This guide has not yet been tested in the SFU Surrey Linux Lab (SUR4080). Some changes may
be needed.
Formatting:
1. Commands starting with $ are host Linux console commands:
$ echo "Hello world!"
2. Commands starting with # are target Linux console commands:
# echo "On the target! Hello world!"
3. Commands starting with (gdb) are GDB console commands.
4. Almost all commands are case sensitive in Linux and GDB.
Revision History:
• Sept 20: Initial 2015 version published (revised installation process).
• Note: /etc/apt/sources.list is a protected file, so must be root to edit it. Use sudo to
launch gedit (as shown).
3. Update the packages available through the new repository:
$ sudo apt-get update
• You may need to use the “fix” option first before the above commands will work:
$ sudo apt-get -f install
6. Troubleshooting:
• If you are having problems getting the correct version to install, you can double check that
apt-get is reading the correct repository to find GDB version 7.8 (or better).
View GDB:
$ apt-cache showpkg gdb
View GDB-Multiarchitecture:
$ apt-cache showpkg gdb-multiarch
If the desired version of the package is not shown, double check your sources.list file,
re-run “apt-get update”
Options:
--debug Enable debugging output.
--version Display version information and exit.
--wrapper WRAPPER -- Run WRAPPER to start new programs
• You can ignore any errors about mapping shared library sections. At the moment we do not
need to worry about debugging these.
• If bt does not yield a meaningful stack, it may mean that you are in some library or OS code
that you do not control. Try setting a break-point in a part of your code you know to be
running and then let execution continue. It should hit your breakpoint and show you
meaningful content.
2. Install Eclipse. An easy way to do this is download the “Eclipse Installer” from
https://eclipse.org/downloads/
• When asked, install “Eclipse IDE for C/C++ Developers”.
• Install the latest version (Mars as of Fall 2015). 64-bit version recommended (on 64-bit
systems).
• I suggest not using apt-get; it will likely get an older version of Eclipse.
• When you install, Eclipse just extracts itself into a folder. If you want an icon for it, you'll
have to create the icon yourself.
3. Launch Eclipse. Command is likely:
$ ~/cpp-latest-released/eclipse/eclipse
• You may be asked about a workspace when starting it; it is fine to accept the default one.
4. Create a new project (File → New → Project...).
• Under C/C++, select "Makefile Project with Existing Code"
• Browse to the directory of your existing project with a makefile.
• Select the “Cross GCC” tool chain.
• Name the project and click Finish.
5. Setup the makefile support for your project.
• Display the Make Target view: Window → Show View → Other. Under Make, select Make
Target.
• In the Make Target view, create a new target (green bulls-eye icon) for your desired
makefile target.
• Note, by default Eclipse expects a clean and all target. You can change these by right-
clicking your project, select Properties; under C/C++ Build, select the Behaviour tab.
• Double click on the new make target. The build output should appear in the Console view.
6. Suggested Settings:
• Auto-save all files when compiling:
Window → Preferences, in left expand General → Workspace, check “Save automatically
before build”.
7. Eclipse coding tips:
• Still on the Debugger tab, but on the Connection sub-tab, set the connection information:
Type: TCP
Host name or IP address: 192.168.7.2 (the IP Address of the target on your network)
Port number: 2001
• To do this, you must already have the compiled version of your project on the target (via
NFS works). This file must have been compiled with the -g option if you want symbols to
be available (i.e. function/variable names etc).
• Hint: To pass arguments to your program being debugged, use the command such as the
following where the arguments 10, 42, end, of, world are passed in.:
# gdbserver localhost:2001 helloWorld 10 42 end of world
7. On the Host, click the Debug button. It should connect to the target.
• It may ask you if you want to switch to the debug perspective; say yes.
• Use the integrated debugger to step-through and debug your application. The application is
actually run on the target, so any effects of the program will take effect on the target. For
example, printf() statements output through the console and code to flash an LED will
still flash the target's LED.
8. You can switch back to the normal perspective by clicking C/C++ button in the top-right of
Eclipse.
9. To re-debug your application, you will need to:
• Restart gdbserver on the target:
# gdbserver localhost:2001 helloWorld
• Re-launch the debugger in Eclipse by clicking the drop-down arrow beside the debugger
icon on the toolbar. Then select the GDB launch profile you setup.
• Note that you cannot just click the debug button, as this will launch it locally.
• If you right-click the project and through Debug as... select Local C/C++ Application, it will
not work because you cannot run the project on the local PC (host).
10. Trouble shooting:
• If Eclipse complains that it cannot find the application when you try to debug, you may
need to relaunch the Debug Configuration window, and click "Debug" from there.
• If you are having troubles connecting, ensure that your communication to the target is
correctly configured. Try using ping or telnet.
• Ensure you have network connectivity between the host and target using ping.
• If the panels and tool bars inside Eclipse seem to be out of place or messed up, try:
1) Go to Window → Perspective → Reset Perspective…, and then click Yes to reset all
views to default locations.
2) If missing tool bar buttons: Window → Show Toolbar
warning: Could not load shared library symbols for 2 libraries, e.g.
/lib/arm-linux-gnueabi/libc.so.6.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
Core was generated by `/mnt/remote/myApps/myCrashingProgram'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00008514 in yourFunction (d1=0x11f5a) at myCrashingProgram.c:22
(gdb)
• Or, one can run GDB natively on the target:
# cd /mnt/remote/myApps/
# gdb pathToApplicationThatCrashed core
5. Stripping a Binary
When built with debug information (-g GCC option), the executable can be twice the size. This can
take up too much room on an embedded system, so we may want to strip the version copied to the
target if there is not much room on the target (and don't need it for debugging)..
1. Copy your application (which has debug symbols) to the shared public directory.
2. Check the file size of your application:
$ ls -l helloWorld
3. Change to the public directory, and strip the binary:
$ arm-linux-gnueabi-strip helloWorld
4. Check the file size of your application; it should be (much?) smaller.
$ ls -l helloWorld