分类: LINUX
2007-05-08 16:04:40
Date:
2007-4-27 Chpt.2 Code Coverage
Date: 2007-5-8
Chpt.3 GNU debugger (gdb)
Chpt.2
Code Coverage
l
Why
do We Need Code Coverage Analysis?
In theory, every line of codes should be
tested, which means, no dead code. Code coverage analysis can help us to find
the dead code or improve the test suites.
l
Useful
Tools—gcov
Usage:
$ gcc –fprofile-arcs
–ftest-coverage –g eg.c –o eg
$ ./eg
$ gcvo
eg.c
Disadvantage:
gcov can’t
find logic errors.
Chpt.3
GNU debugger (gdb)
l
Some
GDB Commands
attach (at) Attaches to a running
process.
backtrace (bt) Prints a stack trace.
break (b) Sets a break point.
clear Clears a break point.
continue (c) Allows the program to
continue executing.
delete Clears a break point by
number.
detach Detaches from the currently
attached process.
display Displays the value of an
expression every time execution stops.
finish Runs to the end of the
function and displays return values of that function.
jump Jumps to an address and
continues the execution there.
list (l) Lists the next 10 lines.
next (n) Steps to the next machine
language instruction.
print (p) Prints the value of an
expression.
run (r) Runs the current program
from the start.
set Changes the value of a
variable.
step (s) Steps the program
instruction by instruction.
where (w) Prints a stack trace.
quit exit gdb.
l
Compile
files with debug information
$ gcc/g++
-g eg.c –o eg
l
GDB
core file
First, Enable system generate core
file like this: ulimit –c 500000. (man ulimit to get more information) In
redhat systems, no core file generated when some programs crashed by default.
Then run gdb like this:
(gdb) file eg-program
(gdb) core-file core
By the way, if you want a running program
core dump, please try this command “kill -SEGV
l
Graphic
GDB Interface (Skipped)
ddd(Data
Display Debugger)
Insight
l
Debugging Techniques
To check
the values stored in the data at the end of a function:
1. Do a
list funct_name, and find the number of the line (line#) in that function where
the function end-block-delimiter (}) is.
2. Set a
break point at each of these line numbers using the following: "break
To find
which line of the source code makes your program crashed:
1. Compile
your program using the -g compiler option if this hasn't already been done.
2. Enter
gdb executable_file from the Linux command line.
3. Enter
the run command. The program begins executing and stops at the first line of
code that has an error (the program may have more than one runtime error).
4. Enter
the gdb command list. In most cases, gdb lists the line of code that caused the
program to crash.
5. Edit
the source code, and go to the line of code just specified. Fix the logical
error and then recompile and run the program to see if things are now working
correctly.
Do the
following to step through the entire program:
1. Start
the executable code by using gdb.
2. Enter
break main from the gdb command line.
3. Enter
run from the gdb command line. The program begins executing. Execution stops on
line 1 of main.
4. Repeatedly
enter step from the gdb command line. At each step, you can execute a print
command to see the values of variables as the program executes.