gdb
utility is the debugger that comes with the GNU compilers
such as gcc
and g++
. It allows you to step through
a program's execution one line at a time or debug what caused a core dump.
In order to get the most information from gdb
, you must compile
the program with debugging info. This is achieved by using the -g
option. If the program is compiled with debugging info, gdb
can
give you information about the program such as line numbers, function names,
statements being executed and so on.
cp /home/fac/melissa/public_html/cs215/lab15.cpp . g++ -g -o lab15 lab15.cpp ulimit -c unlimitedYou should now have an executable called
lab15
which will core
dump when run. The ulimit command will enable the core dump file (named
core) to be created. Run the program and verify that you have a core dump by looking
for the file core
in the current directory. To invoke the
debugger type:
gdb lab15 coreThe first argument to gdb is the executable name (lab15) and the second argument is the core dump filename (core).
You will now be at the command prompt for gdb. Type the command
btThis will show you all the function calls that lead to the problem code. Sometimes, as with this example, your code may crash while executing a library function. The
bt
command will eventually print out
line numbers from your code that you'll wish to investigate. At the start
of each line, there will be a frame number such as #0, #1, etc.
Find the frame number for the line of code you wish to look further at. In this example, we're interested in looking at frame 1. We can look at frame 1 by typing the command
frame 1
Now we want to see the value for all variables local to the function. Type
the command info locals
. For this example, you should see
something like:
(gdb) info locals i = 0 a = (int *) 0x0 size = 5The value for size will be whatever you entered as the size when you ran
lab15
. Notice the value for a
is 0x0. This means
that the pointer has not been initialized since there was no new
statement after asking for the size. Exit gdb
by typing the
command quit
.
cp /home/fac/melissa/public_html/cs215/lab15_2.cpp . g++ -g -o lab15_2 lab15_2.cpp gdb lab15_2Note that this time we only gave gdb the executable name since we are not debugging a core dump.
In the "Edit my submission" portion of Moodle, copy and paste the following questions. Give the gdb command that would accomplish each task AND the output that gdb produced for lab15_2 when you issued that command. Assume each command is done one after the other in the order given.