[honeydew]/vallino/csc173>g++ -g -o gdbprog gdbprog.cc
[honeydew]/vallino/csc173>gdb gdbprog GDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions. There is absolutely no warranty for GDB; type "show warranty" for details. GDB 4.12 (sparc-sun-sunos4.1.3C), Copyright 1994 Free Software Foundation, Inc...
(gdb) run Starting program: /home/emerald/u4/vallino/csc173/gdbprog Program received signal SIGSEGV, Segmentation fault. 0x2350 in DoOperation (ptrs=0x18bc0) at gdbprog.cc:24 24 sum += *ptrs[i];
(gdb) where #0 0x2350 in DoOperation (ptrs=0x18bc0) at gdbprog.cc:24 #1 0x24b0 in main () at gdbprog.cc:45
(gdb) list
19 {
20 int sum = 0;
21 int i;
22
23 for(i = 0;i <10;i++) 24 sum +="*ptrs[i];" 25 } 26 27 28 void PrintArray()
(gdb) print i
$1 = 1
(gdb) print ptrs[i]
$2 = (int *) 0x0
(gdb) print (int* [10])*ptrs
$3 = {0x18be8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
(gdb) break InitArrays Breakpoint 1 at 0x2298: file gdbprog.cc, line 10.
(gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /home/emerald/u4/vallino/csc173/gdbprog Breakpoint 1, InitArrays (array=0x18be8) at gdbprog.cc:10 10 for(i = 0;i <1;i++)
(gdb) step 12 ptrArray[i] = array + i; (gdb) print i $4 = 0 (gdb) step 13 iArray[i] = i; (gdb) step 14 } (gdb) step 15 } (gdb) step main () at gdbprog.cc:45 45 cout << "operation result is " << DoOperation(ptrArray) << '\n';
(gdb) file gdbprog
A program is being debugged already. Kill it? (y or n) y
Load new symbol table from "gdbprog"? (y or n) y
Reading symbols from gdbprog...
done.
Breakpoint 1 at 0x2298: file /home/emerald/u4/vallino/csc173/gdbprog.cc, line 10.
(gdb) run
Starting program: /home/emerald/u4/vallino/csc173/gdbprog
Breakpoint 1, InitArrays (array=0x18be8)
at /home/emerald/u4/vallino/csc173/gdbprog.cc:10
10 for(i = 0;i <10;i++)
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x00002298 in InitArrays(int *)
at
/home/emerald/u4/vallino/csc173/gdbprog.cc:10
(gdb) disable 1
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep n 0x00002298 in InitArrays(int *)
at
/home/emerald/u4/vallino/csc173/gdbprog.cc:10
(gdb) cont Continuing. operation result is 101312 0 1 2 3 4 5 This is an error 7 8 9 Program exited normally.
(gdb) break gdbprog.cc:24 Breakpoint 2 at 0x2334: file /home/emerald/u4/vallino/csc173/gdbprog.cc, line 24.
(gdb) define mydump Type commands for definition of "mydump". End with a line saying just "end". print i print ptrs[i] print *ptrs[i] end
(gdb) command 2 Type commands for when breakpoint 2 is hit, one per line. End with a line saying just "end". silent mydump cont end (gdb) run Starting program: /home/emerald/u4/vallino/csc173/gdbprog $1 = 0 $2 = (int *) 0x18be8 $3 = 0 $4 = 1 $5 = (int *) 0x18bec $6 = 1 $7 = 2 $8 = (int *) 0x18bf0 $9 = 2 ... $25 = 8 $26 = (int *) 0x18c08 $27 = 8 $28 = 9 $29 = (int *) 0x18c0c $30 = 9 operation result is 101312 0 1 2 3 4 5 This is an error 7 8 9 Program exited normally.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep n 0x00002298 in InitArrays(int *)
at
/home/emerald/u4/vallino/csc173/gdbprog.cc:10
2 breakpoint keep y 0x00002334 in DoOperation(int **)
at
/home/emerald/u4/vallino/csc173/gdbprog.cc:24
silent
mydump
cont
(gdb) delete 2
(gdb) break gdbprog.cc:47
Breakpoint 3 at 0x2484: file /home/emerald/u4/vallino/csc173/gdbprog.cc, line
47.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep n 0x00002298 in InitArrays(int *)
at
/home/emerald/u4/vallino/csc173/gdbprog.cc:10
3 breakpoint keep y 0x00002484 in main
at
/home/emerald/u4/vallino/csc173/gdbprog.cc:47
(gdb) run
Starting program: /home/emerald/u4/vallino/csc173/gdbprog
Breakpoint 3, main () at /home/emerald/u4/vallino/csc173/gdbprog.cc:47
47 arraySum = DoOperation(ptrArray);
(gdb) step
DoOperation (ptrs=0x18bc0) at /home/emerald/u4/vallino/csc173/gdbprog.cc:20
20 int sum = 0;
(gdb) finish
Run till exit from #0 DoOperation (ptrs=0x18bc0)
at /home/emerald/u4/vallino/csc173/gdbprog.cc:20
0x2494 in main () at /home/emerald/u4/vallino/csc173/gdbprog.cc:47
47 arraySum = DoOperation(ptrArray);
Value returned is $1 = 101312
(gdb) file gdbprog
A program is being debugged already. Kill it? (y or n) y
Load new symbol table from "gdbprog"? (y or n) y
Reading symbols from gdbprog...
done.
(gdb) run
Starting program: /home/emerald/u4/vallino/csc173/gdbprog
Breakpoint 3, main () at /home/emerald/u4/vallino/csc173/gdbprog.cc:48 48 InitArrays(iArray);
(gdb) next 49 arraySum = DoOperation(ptrArray); (gdb) next 50 cout << "operation result is " << arraySum << '\n'; (gdb) print arraySum $1="45" (gdb) cont Continuing. operation result is 45 0 1 2 3 4 5 This is an error 7 8 9 Program exited normally.
(gdb) delete Delete all breakpoints? (y or n) y
(gdb) break gdbprog.cc:35 Breakpoint 4 at 0x23c0: file /home/emerald/u4/vallino/csc173/gdbprog.cc, line 35. (gdb) cond 4 (i == 6) (gdb) run Starting program: /home/emerald/u4/vallino/csc173/gdbprog operation result is 45 0 1 2 3 4 5 Breakpoint 4, PrintArray () at /home/emerald/u4/vallino/csc173/gdbprog.cc:36 36 if (iArray[i] == 6) (gdb) step 37 cout << "This is an error\n"; (gdb) next This is an error 40 }
If you have eight hours to cut down a tree, it is best to spend six hours sharpening your axe and then two hours cutting down the tree.
(gdb) cont Continuing. 7 8 9 Program exited normally. (gdb) quit [honeydew]/vallino/csc173>
For more information on GDB you can find the full manual here.