Lab6 GDB debugging 1 / 15
Learn how to perform source-level debugging with GDB. 2 / 15
Host Machine OS: Windows Target Machine Raspberry Pi (2 or 3) Build Machine A computer with a SD card slot OS: Ubuntu 15.10 (or above) 64-bit 3 / 15
Host Machine PL2303 Driver PuTTY Target Machine GDB Some buggy programs You may find all software on the CSL Course Software. 4 / 15
Raspberry Pi Power supply Micro SD card and card reader USB-TTL cable Network cable 5 / 15
If you are using RPi 3 and suffer from serial connection problem, please connect to it with ssh. To achieve this, insert the SD card into the build machine, and chroot into the root filesystem to install openssh-server. You may also install GDB for later use. % # chroot into the root filesystem on the SD card % apt-get install openssh-server gdb Configure network for both RPi and the host machine, and plug the network cable between them. Now, you should be able to launch ssh connection from putty to RPi. % vi /etc/network/interfaces 6 / 15
7 / 15
How do you debug without a debugger? Human brain compiler printf printf printf... A debugger allows you to runtime inspect and control the behavior of your program. Step by step execute the code. Examine process state (e.g. CPU registers, memory) at any time. Manually modify process state. The debugger we are going to use is GDB, the GNU Project debugger. 8 / 15
First, install GDB in your RPi. $ sudo apt-get install gdb Suppose you write a source file hello.c, and you want to trace it with GDB. When you compile it with GCC, you need an additional option to add debugging information to the executable hello. $ gcc g o hello hello.c Don t change the relative path between hello.c and hello since GDB require the source file to perform source-level debugging. Now, you can start GDB. $ gdb hello (gdb) (gdb) help (gdb) help command (gdb) (gdb) # Tips: you may type fewer characters if not ambiguous (gdb) h command 9 / 15
break: Set breakpoint to make program stop at certain line or address. (gdb) b line_number (gdb) b main info: Show various kind of information. We use it for breakpoints here. (gdb) i b delete: Delete a breakpoint (gdb) d breakpoint_number run: Start the program (gdb) r (gdb) r arg1 arg2 (gdb) r < input_file > output_file step: Execute a line of source code (gdb) s next: Like step but steps over function calls instead of step into them. (gdb) n 10 / 15
print: Print variables. Both local and global are available. (gdb) p variable (gdb) p &variable (gdb) p *pointer (gdb) p/x variable_in_hex set: Set variables. If variable and subcommand conflicts in names, add the keyword variable. (gdb) set variable=value (gdb) set variable variable=value 11 / 15
12 / 15
layout src: Display source code window and command line window at the same time. The line highlighted in the source code window is the next line to be execute. (gdb) la src focus: Focusing on source code window let you scroll the source code up and down. Focusing on command line window let you search for history command. (gdb) foc src (gdb) foc cmd Ctrl-x Ctrl-a: Turn off TUI mode and back to pure command line mode. 13 / 15
Download the buggy programs here. Extract the archive and you will find 3 exercises. $ tar xf gdb_exercise.tar Exercise 1: Given the dividend and the divisor, the program modulo calculates the remainder by repeatedly subtracting the divisor from the dividend. However, the calculation just don t stop. Use GDB to find the reason if you have no idea. $./modulo 100 3 Exersice 2 & 3: The program sof allows users to modify and print the content of an array. The program fmt receives strings from users and echoes them back. The two programs behave oddly under some special inputs (i.e. payload.txt in each directory). That is, their executions fall into a function called loop(). Show that you can use GDB to find the last statement before executing loop(). $./sof < payload.txt $./fmt < payload.txt 14 / 15
Debugging with GDB Stack buffer overflow - Wikipedia Uncontrolled format string Wikipedia 15 / 15