This demonstrates basic gdb commands. See the other sample for an actual debugging session with comments explaining the actions.

Green text is from gdb or Linux; red text is from swap.cpp; black text is user input


Script started on Mon 07 Sep 2009 01:37:12 PM EDT

[hermes.cs.kent.edu]{1}-> g++ -g -o swap swap.cpp

[hermes.cs.kent.edu]{2}-> gdb swap

GNU gdb Fedora (6.8-27.el5)

Copyright (C) 2008 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type "show copying"

and "show warranty" for details.

This GDB was configured as "i386-redhat-linux-gnu"...

(gdb) l

2 // allowing for repeats

3 // Example for demo of gdb

4 //JWB

5 #include <iostream>

6 using namespace std;

7 int main () {

8 int i;

9 char a,b, more;

10 do {

11 cout << "Input character values for a and b : ";

(gdb) l

12 cin >> a;

13 cout << "\nOriginally a is " << a << " and b is " << b << endl;

14

15 //do swap

16 a = b;

17 b = a;

18

19 //output results

20 cout << "\nAfter the swap a is now " << a << " and b is now " << b << endl;

21

(gdb) r

Starting program: /users/cs/faculty/jbaker/cs1-alt/swap

Input character values for a and b : AB

Originally a is A and b is ¿

After the swap a is now ¿ and b is now ¿

Input c to continue; s to stop :

Input character values for a and b : AB

Originally a is A and b is ¿

After the swap a is now ¿ and b is now ¿

Input c to continue; s to stop :

Input character values for a and b :

Program received signal SIGINT, Interrupt.

0x003aa410 in __kernel_vsyscall ()

(gdb) b 1 [K [K [Kl 13

8 int i;

9 char a,b, more;

10 do {

11 cout << "Input character values for a and b : ";

12 cin >> a;

13 cout << "\nOriginally a is " << a << " and b is " << b << endl;

14

15 //do swap

16 a = b;

17 b = a;

(gdb) b 13

Breakpoint 1 at 0x80487f0: file swap.cpp, line 13.

(gdb) r

The program being debugged has been started already.

Start it from the beginning? (y or n) y

Starting program: /users/cs/faculty/jbaker/cs1-alt/swap

Input character values for a and b : AB

Breakpoint 1, main () at swap.cpp:13

13 cout << "\nOriginally a is " << a << " and b is " << b << endl;

(gdb) p a

$1 = 65 'A'

(gdb) pb [K b Note: The [K is a backspace where the user changed the entry

from pb to p b

$2 = -65 '¿'

(gdb) p b='B" [K'

$3 = 66 'B'

(gdb) c

Continuing.

Originally a is A and b is B

After the swap a is now B and b is now B

Input c to continue; s to stop :

Input character values for a and b :

Program received signal SIGINT, Interrupt.

0x001f3410 in __kernel_vsyscall ()

(gdb) r

The program being debugged has been started already.

Start it from the beginning? (y or n) y

Starting program: /users/cs/faculty/jbaker/cs1-alt/swap

Input character values for a and b : AB

Breakpoint 1, main () at swap.cpp:13

13 cout << "\nOriginally a is " << a << " and b is " << b << endl;

(gdb) n

Originally a is A and b is ¿

16 a = b;

(gdb) n

17 b = a;

(gdb) n

20 cout << "\nAfter the swap a is now " << a << " and b is now " << b << endl;

(gdb) n

After the swap a is now ¿ and b is now ¿

22 cout << "Input c to continue; s to stop : ";

(gdb) n

23 cin >> more;

(gdb) p more

$4 = 18 '\022'

(gdb) quit

The program is running. Exit anyway? (y or n) y

[hermes.cs.kent.edu]{3}-> exit

Script done on Mon 07 Sep 2009 01:41:21 PM EDT