Debugging Guide for GDB and Eclipse by Brian Fraser Last update: Sept 25, 2016

Debugging Guide for GDB and Eclipse by Brian Fraser Last update: Sept 25, 2016 This document guides the user through: 1. Debugging an application using GDB command prompt. 2. Debugging an application using Eclipse. 3. Generating and loading core files. 4. Stripping debug symbols from a binary. Table of Contents 1. Installing gdb-multiarch.........................................................................................................................2 1.1 If Running Ubuntu 16.xx................................................................................................................2 1.2 If Running Ubuntu 14.04................................................................................................................2 2. GDB.......................................................................................................................................................4 3. Eclipse....................................................................................................................................................6 3.1 Eclipse Installation and Project Setup............................................................................................6 3.2 Debugging with Eclipse.................................................................................................................7 4. Core Dumps.........................................................................................................................................10 5. Stripping a Binary................................................................................................................................11 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 18: Initial version for fall 2016 • Sept 26: Added directions for installing gdbserver on target. PDF Created 09/15/14 1/12 1. Installing gdb-multiarch The host needs a cross-debugger to debug an application running on the target. GDB (GNU Debugger) has a version which supports multiple architectures (such as ARM, MIPS, …) named gdb-multiarch. 1.1 If Running Ubuntu 16.xx 1. Install GDB and GDB multi-architecture: $ sudo apt-get install gdb $ sudo apt-get install gdb-multiarch 2. Run gdb-multiarch and check its version. $ gdb-multiarch -v • Should display first line similar to the following: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1 1.2 If Running Ubuntu 14.04 Normally, you should be able to do an apt-get on gdb-multiarch; however, Ubuntu 14.04 is packaged with gdb-multiarch version 7.7 which is unable to correctly process core files from the target. Here is the process to get gdb-multiarch 7.8 which does work. 1. Remove any existing versions of GDB and GDB multi-architecture: $ sudo apt-get remove gdb gdb-multiarch 2. Add the Ubuntu utopic repository to /etc/apt/sources.list: $ sudo gedit /etc/apt/sources.list • At the end of the file, add the following lines: ## Added for GDB 7.8 deb http://old-releases.ubuntu.com/ubuntu utopic main universe • 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 4. Install GDB and GDB multi-architecture: $ sudo apt-get install gdb $ sudo apt-get install gdb-multiarch • You may need to use the “fix” option first before the above commands will work: $ sudo apt-get -f install 5. Run gdb-multiarch and check its version. $ gdb-multiarch -v • Should display first line: GNU gdb (Ubuntu 7.8-1ubuntu4) 7.8.0.20141001-cvs 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). PDF Created 09/15/14 2/12 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” PDF Created 09/15/14 3/12 2. GDB GDB is a text-debugger common to most Linux systems. For remote debugging, we'll run gdbserver on the target, and the cross-debugger (gdb-multiarch) on the host. 1. Build your project using the -g option to ensure the file gets debug symbols. • This likely means adding the -g option to your CFLAGS variable in your Makefile. 2. On the target, install gdbserver (if not already installed): • Ensure you have internet access. If not, see the networking guide. # ping google.ca • Install GDB server on the target: # apt-get update # apt-get install gdbserver 3. On the target, change to the directory where your application is (assumed to be named helloWorld), and launch gdbserver: # gdbserver localhost:2001 helloWorld • It should look like the following (pid likely to be different): # gdbserver localhost:2001 helloWorld Process helloWorld created; pid = 1068 Listening on port 2001 4. On the host, in the directory of your helloWorld executable, launch the cross-debugger: $ gdb-multiarch -q helloWorld 5. At the GDB prompt "(gdb)", type in the following command to connect to the target: (gdb) target remote 192.168.0.102:2001 • Change the IP address to the IP address of the target. • The host should look like this: $ gdb-multiarch -q helloWorld (gdb) target remote 192.168.0.171:2001 Remote debugging using 192.168.0.171:2001 warning: Unable to find dynamic linker breakpoint function. GDB will be unable to debug shared library initializers and track explicitly loaded dynamic code. 0x400007b0 in ?? () (gdb) • The target should now have displayed the additional line (your IP will be different): Remote debugging from host 192.168.0.188 6. You now have a GDB session. You should be familiar with the following GDB commands (parts in italics can be replaced by other values): • list, frame, quit • info breakpoints, break main, break lineNumberHere, delete 1 • continue, print myVar, step, next • bt, info args, info frame, info local, up, down • Control + C (to interrupt program when running). 7. Troubleshooting: • Ensure your host can communicate with the target. Try pinging the board and opening a ssh PDF Created 09/15/14 4/12 prompt to the board. Refer to the quick-start guide and associated trouble shooting steps if this fails. • If you get the wrong version of gdbserver, it may not run correctly on the target. When it is run without arguments, you should see the following: Usage: gdbserver [OPTIONS] COMM PROG [ARGS ...] gdbserver [OPTIONS] --attach COMM PID gdbserver [OPTIONS] --multi COMM COMM may either be a tty device (for serial debugging), or HOST:PORT to listen for a TCP connection. Options: --debug Enable general debugging output. --remote-debug Enable remote protocol debugging output. --version Display version information and exit. --wrapper WRAPPER -- Run WRAPPER to start new programs. --once Exit after the first connection has closed. • 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. PDF Created 09/15/14 5/12 3. Eclipse 3.1 Eclipse Installation and Project Setup 1. Install Java's run time environment: $ sudo apt-get install openjdk-8-jre 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 (Neon as of Fall 2016). 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: $ ~/eclipse/cpp-neon/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. You may need to manually switch to the Console view (bottom). 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: • Ctrl+B to build. View the Console window to see the build messages. PDF Created 09/15/14 6/12 • Eclipse will show you the errors found during your last compile. If you correct the error but don't rebuild yet, Eclipse will still show the error information from the last build. • Eclipse does some code analysis of its own (in addition to the normal build process). Eclipse will show an indication of some problems even without build. However, sometimes it may find things which it thinks are errors but will actually build OK. • What Eclipse rebuilds depends on your makefile. If you setup dependencies correctly, it will rebuild a .c file when it changes. However, often the .h files are missed. So, if you change a .h file you may need to do a make-clean and then a re-build (make uploads/Litterature/ debugging-guide-for-gdb-and-eclipse-this-document-guides-the-user-through.pdf

  • 13
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager