Lab 01: GDB Tutorial: 1. Overview
Lab 01: GDB Tutorial: 1. Overview
1. Overview
gdb is a debugger for C (and C++). It allows you to do things like run the program up to a certain point
then stop and print out the values of certain variables at that point, or step through the program one line
at a time and print out the values of each variable after executing each line. gdb uses a command line
interface.
2. GDB tutorial
This is a brief description of some of the most commonly used features of gdb.
2.1. Compiling
To prepare your program for debugging with gdb, you must compile it with the -g flag. So, if your
program is in a source file called test.c and you want to put the executable in the file test.out, then you
would compile with the following command:
Notes:
-m32 option tells the compiler to generate code in 32-bit mode if you are compiling on a 64-bit system.
You can skip this option if you are working on a 32 system.
To start gdb, just type gdb at the Linux prompt. gdb will give you a prompt that looks like this: (gdb).
From that prompt you can run your program, look at variables, etc., using the commands listed below
Or, you can start gdb and give it the name of the program executable you want to debug by typing
To exit the program just type quit at the (gdb) prompt (actually just typing q is good enough).
Once the debugging symbols from the executable were loaded, you can start executing your program
using the run or r command
(gdb) run
2. Objectives
This lab aims to provide students with ability:
3. Lab Environment
a) Install VMWare workstation/VirtualBox on your Windows Computer.
b) Download the virtual machine image of a Linux machine for this lab from here.
c) Extract the SEEDUbuntu archive then open it in VMWare, the linux machine will start
automatically (login with username: SEED, password:dees).
4. Tasks
4.1. Compose s simple C++ program in nano text editor
gdb test.out
By default, the assembly code follows the AT&T syntax like this:
0x08048414 <+0>: push %ebp
0x08048415 <+1>: mov %esp,%ebp
0x08048417 <+3>: sub $0x18,%esp
0x0804841a <+6>: mov 0xc(%ebp),%eax
0x0804841d <+9>: add %eax,0x8(%ebp)
0x08048420 <+12>: mov $0x8048550,%eax
0x08048425 <+17>: mov 0x8(%ebp),%edx
0x08048428 <+20>: mov %edx,0x4(%esp)
0x0804842c <+24>: mov %eax,(%esp)
0x0804842f <+27>: call 0x8048320 <printf@plt>
0x08048434 <+32>: leave
0x08048435 <+33>: ret
Insert breakpoints
To examine registers, the program needed to be executed and stop at a particular point. We will insert
breakpoints to do this. Set breakpoints at the beginning of main and func functions:
(gdb) break main
Breakpoint 1 at 0x804843f: file test.c, line 10.
(gdb) break func
Breakpoint 2 at 0x804841a: file test.c, line 4.
a) Execute the program step by step then examine the values of variables a, b in main()
b) Try to set a, b to values other than 5,7 while executing program then check the final result.