The Fibonacci numbers are defined by F[0]=0
, F[1]=1
, and F[n]=F[n-1]+F[n-2]
The first few are as follows
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 |
Your task is to debug a program to compute factorials.
You can either cut and paste from the browser, or copy from
/fcs/courses/cs2023/tutorial3/fib.c
. There are at least 3 bugs in this program.
Instructions.
In the following, "HAL9000> " indicates the bash command line, and "(gdb) " the gdb prompt. Follow along my script, filling in details where I left them out.
Initial run
HAL9000> gcc fib.c
HAL9000> ./a.out
Enter a positive integer 7
What is the output?
First attempt at using the debugger
HAL9000> gdb a.out
GNU gdb (GDB) 6.8.50.20090628-cvs-debian
[snip]
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) run
Starting program: /home/bremner/teaching/cs2023/tutorial/t3/a.out
Enter a positive integer
7
Program received signal SIGSEGV, Segmentation fault.
0xb7ebb8f9 in _IO_vfscanf () from /lib/i686/cmov/libc.so.6
(gdb) bt
#0 0xb7ebb8f9 in _IO_vfscanf () from /lib/i686/cmov/libc.so.6
#1 0xb7ec0388 in scanf () from /lib/i686/cmov/libc.so.6
#2 0x08048472 in main ()
(gdb) list
(gdb) quit
The program is running. Quit anyway (and kill it)? (y or n) y
Compiling with debugging information.
Ok, that wasn't too helpful. Let us recompile with debugging information.
HAL9000> gcc -g fib.c
HAL9000> gdb ./a.out
GNU gdb (GDB) 6.8.50.20090628-cvs-debian
[snip]
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) run
Starting program: /home/bremner/teaching/cs2023/tutorial/t3/a.out
Enter a positive integer
7
Program received signal SIGSEGV, Segmentation fault.
0xb7ebb8f9 in _IO_vfscanf () from /lib/i686/cmov/libc.so.6
(gdb) bt
#0 0xb7ebb8f9 in _IO_vfscanf () from /lib/i686/cmov/libc.so.6
#1 0xb7ec0388 in scanf () from /lib/i686/cmov/libc.so.6
#2 0x08048472 in main () at fib.c:10
(gdb) list
1 #include <stdio.h>
2
3 int main(void){
4
5 int n;
6 int fib,last=1,before_last=0;
7 int i;
8
9 printf("Enter a positive integer\n");
10 scanf("%d",n);
(gdb) quit
The program is running. Quit anyway (and kill it)? (y or n) y
OK, so the debugger showed us the source line with the error. Fix it, and put the new version in fib1.c
Next bug
HAL9000> gcc -g fib1.c
HAL9000> ./a.out
Enter a positive integer
3
Hmm, the output doesn't look right.
HAL9000> gdb ./a.out
GNU gdb (GDB) 6.8.50.20090628-cvs-debian
[snip]
(gdb) list
(gdb) list
(gdb) break 12
Breakpoint 1 at 0x8048472: file fib1.c, line 12.
(gdb) run
Starting program: /home/bremner/teaching/cs2023/tutorial/t3/a.out
Enter a positive integer
3
Breakpoint 1, main () at fib1.c:12
12 for (i=2; i<n ; i++){
(gdb) n
13 fib=last+before_last;
(gdb) display fib
1: fib = 134518436
(gdb) n
14 before_last=last;
1: fib = 1
(gdb) n
15 last=fib;
1: fib = 1
(gdb) n
12 for (i=2; i<n ; i++){
1: fib = 1
(gdb) n
18 printf("F[%d]=%d\n",n,fib);
1: fib = 1
(gdb) n
F[3]=1
19 }
1: fib = 1
(gdb) n
0xb7e867a5 in __libc_start_main () from /lib/i686/cmov/libc.so.6
(gdb) q
The program is running. Quit anyway (and kill it)? (y or n) y
There seems to be a problem with the loop bounds. Fix it, and put the version in fib2.c.
last bug?
HAL9000> gcc -g fib2.c
HAL9000> ./a.out
Enter a positive integer
3
F[3]=2
HAL9000> # yay
HAL9000> ./a.out
Enter a positive integer
1
F[1]=134518436
HAL9000> # boo!
HAL9000> gdb ./a.out
GNU gdb (GDB) 6.8.50.20090628-cvs-debian
[snip]
(gdb) list
1 #include <stdio.h>
2
3 int main(void){
4
5 int n;
6 int fib,last=1,before_last=0;
7 int i;
8
9 printf("Enter a positive integer\n");
10 scanf("%d",&n);
(gdb) list
11
12 for (i=1; i<n ; i++){
13 fib=last+before_last;
14 before_last=last;
15 last=fib;
16 }
17
18 printf("F[%d]=%d\n",n,fib);
19 }
(gdb) break 12
Breakpoint 1 at 0x8048472: file fib2.c, line 12.
(gdb) run
Starting program: /home/bremner/teaching/cs2023/tutorial/t3/a.out
Enter a positive integer
1
Breakpoint 1, main () at fib2.c:12
12 for (i=1; i<n ; i++){
(gdb) n
18 printf("F[%d]=%d\n",n,fib);
(gdb) n
F[1]=134518436
19 }
(gdb) quit
The program is running. Quit anyway (and kill it)? (y or n) y
Hmm. We didn't go through the loop at all. Actually, that is OK, but there is
another problem to fix. Find it, and put the output in fib3.c
HAL9000> gcc -g fib3.c
HAL9000> ./a.out
Enter a positive integer
1
HAL9000> ./a.out
Enter a positive integer
10