0% found this document useful (0 votes)
6 views

Debugging Programs With GDB

Uploaded by

Fernando Lagos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Debugging Programs With GDB

Uploaded by

Fernando Lagos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Debugging Programs with GDB

Debugging Programs with GDB


● This is a description of several of the gdb features that are useful for debugging your programs.
The gdb session shown below was generated while debugging the program gdbprog.cc

● Must compile and link with -g option

[honeydew]/vallino/csc173>g++ -g -o gdbprog gdbprog.cc

● Load executable into gdb

[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...

● Use help to get information about available commands.

● Run program with run command

(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];

● Program will run until termination, a break point is reached, or an error occurs

● You can find out where the program is, i.e. where the segmentation fault occurred, using the
where command. This gives a function call trace of how you got to this point and shows line
numbers inside files.

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (1 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

(gdb) where
#0 0x2350 in DoOperation (ptrs=0x18bc0) at gdbprog.cc:24
#1 0x24b0 in main () at gdbprog.cc:45

● list shows surrounding code

(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()

● print allows you to display variables or any language expression. You may need a cast to display
value correctly.

(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}

● break sets breakpoints at places where you want the debugger to stop.
break function-name will set a breakpoint at the start of the function. You can set multiple
breakpoints.

(gdb) break InitArrays


Breakpoint 1 at 0x2298: file gdbprog.cc, line 10.

● run will restart the program when stopped in mid-execution

(gdb) run
The program being debugged has been started already.

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (2 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

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++)

● The program stops before executing the statement where the break is set.

● step executes one instruction. It will step into functions.

(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';

● After changing program, reload executable with file command

(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)

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (3 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

at /home/emerald/u4/vallino/csc173/gdbprog.cc:10
10 for(i = 0;i < 10;i++)

● info break tells you where the current breakpoints are set. disable can turn them off.

(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

● continue will continue execution from the current location.

(gdb) cont
Continuing.
operation result is 101312
0
1
2
3
4
5
This is an error
7
8
9

Program exited normally.

● break file-name:line-number will set a breakpoint when that line of code is reached by the
program.

(gdb) break gdbprog.cc:24


Breakpoint 2 at 0x2334: file /home/emerald/u4/vallino/csc173/

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (4 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

gdbprog.cc, line 24.

● For frequently used operations, especially prints, define your own commands with define. source
can read a file of gdb commands, defines or others, into gdb.

(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

● command breakpoint-number specifies commands to run whenever the breakpoint is reached.


display specifies a value to display whenever the program is stopped.

(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

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (5 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

$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.

● delete breakpoint-number removes breakpoint.

(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

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (6 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

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);

● step will enter functions. finish will run to the end of the current function and display return value.

(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

● After reloading modified program breakpoints may no longer be valid.

Breakpoint 3, main () at /home/emerald/u4/vallino/csc173/


gdbprog.cc:48
48 InitArrays(iArray);

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (7 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

● next will run until the next source line is reached. Use it to step to the next line without needing
to step through a function. Needed whenever a system function is called.

(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.

● delete can be used to remove all breakpoints.

(gdb) delete
Delete all breakpoints? (y or n) y

● cond break exp causes a program break at the break location only if exp evaluates true

(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

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (8 of 9)25/09/2004 07:22:16 p.m.


Debugging Programs with GDB

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 }

● Learn how to effectively use a debugger. It sure beats print statements!! Once you learn one,
moving to another is usually just new syntax. It is worth the learning curve!

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.

● With a working knowledge of a debugger like gdb, finding bugs in your programs will go much
faster. You will be able to use quit to exit gdb with a working program before you know it.

(gdb) cont
Continuing.
7
8
9

Program exited normally.


(gdb) quit
[honeydew]/vallino/csc173>

http://www.cs.rochester.edu/u/srini/csc172/gdb.html (9 of 9)25/09/2004 07:22:16 p.m.

You might also like