/*---------------------------------------------------------------*/
/*                                                               */
/* The GDB Debugger  by Pat Finn. Copyright 2005.                */
/*                                                               */
/*---------------------------------------------------------------*/

	The GDB is an open source program made by the GNU team to 
help programmers with their code. It allows users to find bugs that
are hard to find, can cause the program to act up, cause memory 
leaks, or even result in core dumps. 

This program can be very helpful as one of the few other ways is to
look at your code line by line and try to figure it out by yourself.
That doesn’t seem bad but if your code is over 1000 lines long and
the problem is that you forgot a + somewhere than it could take 
hours to find it. 

The debugger allows you to cut that time down as it can actually:

1. Run the program inside the debugger
2. Use core dumps to find the bugs
3. Be attached to a program that is already running
4. Allow the user to stop the program at specific points and 
         examine variables, functions and code as it runs.
5. Change the code in your program and let you continue to see 
          about other bugs or if the change causes others.

/*--------------------------------------------------------------*/
/*                                                              */
/* Preparing for the GDB                                        */
/*                                                              */
/*--------------------------------------------------------------*/

To be able to use GDB it has to be installed in the OS you are 
working on. (It can only truly be used on Unix systems but there
 is a program for windows called Cygwin that emulates Unix.)

You must first be able to compile your code into an executable
program that by default is called the a.out. You normally do at
the prompt using the g++ command that looks like this:

terra% g++ ProgramName.cc

The problem with just using G++ is that it includes no debugging 
information or symbol table that the GDB needs to operate. So to 
properly use the debugger you need to include a –g when using the
g++ command so it can create a symbols table. 

The command would look like this:

terra% g++ ProgramName.cc –g 

/*--------------------------------------------------------------*/
/*                                                              */
/* Important Note                                               */
/*                                                              */
/*--------------------------------------------------------------*/
	
When you tell the compiler to turn on the debug code with –g the 
a.out is much larger than it normally is. For example without 
using –g the size of one program is normally only 13756 bytes for
its a.out but with the debugging code enabled is increases to
173232. So if you have limited space remember to always delete
them when you are finished.

/*--------------------------------------------------------------*/
/*                                                              */
/* Running GDB                                                  */
/*                                                              */
/*--------------------------------------------------------------*/ 

After compiling GDB it is quite easy to start it up, as you only
need to type gdb. Also if you want you can specify the program 
beforehand by typing in gdb ProgramName.cc. 

So it would look like this at your prompt:

terra% gdb

(or)

terra% gdb a.out

After typing in the command it will come up with the version 
number and  disclaimer that should look like this:

GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, 
and you are welcome to change it and/or 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. This GDB was configured as "sparc-sun-solaris2.8".

(gdb)

If it reads that symbols are not found or can’t read the file type
than you did not compile the code correctly so check above for how
to do it. Also if you are the type that does not want to see all
that come up every time adding a –q when you run gdb will not have
that come up. 

To see other ways of starting gdb just type in gdb – help at the 
terra prompt.

Now you should be at the (gdb) prompt as it has taken place of the
terra% prompt. If you didn’t specify a file when running the gdb
you’ll have to do that before almost anything else by using the
file command. So at the prompt it should look like this:

(gdb) file a.out

At that prompt you can type help to look at help for advanced 
commands and topics that aren’t going to be covered in this guide. 

/*--------------------------------------------------------------*/
/*                                                              */
/* Common Commands                                              */
/*                                                              */
/*--------------------------------------------------------------*/

Here is a list of the most basic and most used commands in gdb 
that will help you get off to a good start.

/*--------------------------------------------------------------*/
/*                                                              */
/* Main Commands                                                */
/*                                                              */
/*--------------------------------------------------------------*/

1. gdb – starts the compiler

         terra% gdb

2. quit – exits gdb, can just type in q.

         (gdb) quit

3. run – Starts the program, just

         (gdb) run

4. where – tells what function you are in.

           (gdb) where

5. kill – stops the execution of the program that is currently 
                running.

          (gdb) kill

/*--------------------------------------------------------------*/
/*                                                              */
/* Break Points                                                 */
/*                                                              */
/*--------------------------------------------------------------*/

Breakpoints are used to control your executable program. 
Breakpoints will stop the program for you to test stuff when 
certain situations happen. If you don’t know where to start 
debugging you can just set a breakpoint at the beginning of your 
code and slowly go through it.

Breakpoints – Can be used to stop at functions, certain line 
numbers or even if certain conditions happen. When you set a 
breakpoint it stays until you remove it or exit the GDB

1. Break function – Sets a break point at the entry of whatever 
                         function is specified.

                   (gdb) break function

2. Break LineNumber – Sets a break point at whatever line is 
                           desired.

                     (gdb) break linenumber

3. tbreak – Sets a breakpoint that will only stop once and will 
                 delete itself afterwards.

            (gdb) tbreak arguement

4. clear – Can be used to remove breakpoints, either linenumbers 
               or functions. 

           (gdb) clear linenumber

           or

           (gdb) clear function

5. Delete – Just like clear but typed by itself can remove all 
                 breakpoints or using the number given in 
                 info break.

            (gdb) delete

            or

            (gdb) delete number

6. info break – Shows information about each break point and 
                      can even be used to look at certain 
                      breakpoints.

                (gdb) info break

                or

                (gdb) info break breaknumber

7. info locals – shows all the variables within the function you
                       are at the moment.

                       (gdb) info locals

/*--------------------------------------------------------------*/
/*                                                              */
/* Displaying Lines of Codes                                    */
/*                                                              */
/*--------------------------------------------------------------*/

The command to see your code like you would in the pico editor is
list. There are many different arguments and settings you can do 
to list.

1. List – If used in the beginning by itself will show the main 
             function along with 5 lines above and 5 lines below.

             (gdb) list

2. list – After that putting in list will show the next 10 lines 
                of codes.

          (gdb) list

3. list - – Will go back 10 lines of code that was just shown.

            (gdb) list –

4. list function – shows lines around the function you specify.

                   (gdb) list main()

5. list LineNumber – Shows 5 lines above and 5 lines below the 
                           line number.

                     (gdb) list 20

6. list x,z – Shows specified range of lines.

              (gdb) list 20,40

7. show listsize – Will show you the amount the basic command 
                        list will show. (default is at 10.) 

                   (gdb) show listsize

8. set listsize number – will set the listsize to whatever you 
                         want. (So if set to 20 list will show 
                         20 lines of code instead of 10.)

                         (gdb) set listsize 20

/*--------------------------------------------------------------*/
/*                                                              */
/* Commands to move through code                                */
/*                                                              */
/*--------------------------------------------------------------*/

After you tell gdb to run it will run the program like normal 
until it meets a breakpoint you have set up. From there you can 
examine your code more closely along with checking variables and 
changing their values.

1. Next – Moves one line ahead in the program.

         (gdb) next

2. Next count – Moves ahead in the code like next but however many
                      number of times you tell the debugger to.

                (gdb) next #

3. Step – Moves one line ahead just like next but will go into a
                function if it meets it.

         (gdb) step

4. Step count – Moves ahead just like next but the number of times
                      you tell the debugger to.

                (gdb) step #

5. Continue – Moves unto the next set break point or continues 
                    execution of program, whichever it meets first
                    in the code.

              (gdb) continue

/*--------------------------------------------------------------*/
/*                                                              */
/* Testing and changing variables and their values              */
/*                                                              */
/*--------------------------------------------------------------*/

There are numerous commands most of them can cause a large amount
of trouble as the debugger is highly complex. So this paper will 
only explain the two basic commands and one other command that 
exemplifies the point.

1. Print – Displays a variable or a function

          (gdb) print variable

2. Set – Changes the value of a variable.

         (gdb) set variable = value 

3. Edit – will take you to whatever editor is default. 
         (Pico editor most of the time)

         (gdb) edit

/*--------------------------------------------------------------*/
/*                                                              */
/* The HELPFILE one more time                                   */
/*                                                              */
/*--------------------------------------------------------------*/

It has already been said but just in case there is a help file 
that goes into more detail if you need it. These are only the 
basics to get you accustomed to the editor.

1. help – Shows a list of topics about the debugger.

          (gdb) help

/*--------------------------------------------------------------*/
/*                                                              */
/* Getting cin from a file while debugging                      */
/*                                                              */
/*--------------------------------------------------------------*/

In case your program uses information from a database you need to 
   pull in or something to that degree.

1. run < datafile – reads data from file for cin

                    (gdb) run < data.cc

/*--------------------------------------------------------------*/
/*                                                              */
/* End                                                          */
/*                                                              */
/*--------------------------------------------------------------*/

Some of these commands can only be run after you run the program 
as others can be before. If you want more help the best thing to 
do is read the help files or find a guide online, as there are 
numerous ones. They even have one at the official website but it 
is very complex. This information was compiled from:

Gdb Developers. Debugging with GDB. 6 Sept. 2005. 
Free Software Foundation. 10 Oct. 2005 
http://sources.redhat.com/gdb/current/onlinedocs/gdb_toc.html#SEC_Contents

Kaufman, Lar, Matthias K. Dalheimer, and Matt Welsh. Running Linux. 
3rd ed. Sebastopol: O'Reilly & Associates, 1999.

The help files within GDB itself that you can view by typing help
after you get to the gdb prompt.