Professional Documents
Culture Documents
57:017, Computers in
Engineering
Using gdb and ddd
z
z
z
z
z
z
z
systematic debugging
thorough testing
use of toolse.g. gdb & ddd
avoidance of evolutionary programming
extra
semicolon
/* Addition Program */
#include <stdio.h>;
int main()
{
int integer1, integer2, sum;
missing
semicolon
/*declaration*/
Hard to Read. Format your code to make it easier to read and then find errors.
}
What is a Debugger?
z
z
z
z
Example:
gcc -g -o datagram datagram.c
gdb datagram
You will see the prompt (gdb) which means the debugger
is waiting for your commands
run, list, break, continue, step, next,
where, print
z
z
z
z
stops
z
int main() {
int a;
setint(&a,
ti t(&
10)
10);
printf("%d\n", a);
int* b;
setint(b, 10);
printf("%d\n", *b);
return 0;
}
void setint(int* ip, int i) {
*ip = i;
}
(gdb) continue
Continuing.
next string is: Oasis
Breakpoint 1, main (argc=2, argv=0x7ffff3f8) at eofTest.c:37
37
printf("next string is: %s\n", buff);
(gdb) continue
Continuing.
next string is: Be Here Now
Breakpoint 1, main (argc=2, argv=0x7ffff3f8) at eofTest.c:37
37
printf("next string is: %s\n", buff);
(gdb) continue
Continuing.
next string is: 1997
Breakpoint 1, main (argc=2, argv=0x7ffff3f8) at eofTest.c:37
37
printf("next string is: %s\n", buff);
(gdb) continue
Continuing.
next string is: 4
Breakpoint 1, main (argc=2, argv=0x7ffff3f8) at eofTest.c:37
37
printf("next string is: %s\n", buff);
(gdb) next
Note: end of file is not detected
next string is: Stand By Me
28 while(!feof(fp)) {
by outer loop after last character
(gdb) print c
is processed. c = \n
$1 = 10 '\n'
(gdb) next
29
(gdb) next
30
(gdb) next
31
(gdb) print c
$2 = -1 '\377'
(gdb) next
32
(gdb) next
33
(gdb) next
34
(gdb) next
31
(gdb) next
32
(gdb) print c
$3 = -1 '\377'
(gdb)
buff[i] = c;
i++;
c = fgetc(fp);
*argv[]) {
if (argc<2) {
printf("data file must be specified as command line argument\n");
}
else {
if ((fp = fopen(argv[1], "r"))==NULL) {
printf("error opeining file \n");
}
else {
c = fgetc(fp);
while(!feof(fp)) {
i = 0;
while((c != ';')&&(c != '\n')) {
buff[i] = c;
i++;
c=fgetc(fp);
}
buff[i] = '\0';
printf("next string is: %s\n", buff);
c =fgetc(fp);
}
}
}
return 0;