Emacs, g++ and gdb

Contents

What you need

  1. Your password and username for Troi

Goals

When programs get so complicated that you cannot see exactly what it is doing at any particular time, there is a program called gdb that will show you the way the program is running. The program gdb is one of a class of programs called debuggers. Using it you can trace through your program line by line and examine the values of the variables.

By the end of lab class today, you should have loaded a file into gdb and traced through it.

Compiling the program so you can used gdb

You need to use the -g flag to compile programs so that the debugger will run on them. For example, if you compile your checkers program like this g+ *.cc+, you will need to compile it like this g+ -g *.cc+.

Debugging the Program

To start gdb, used the command M-x gdb. The minibuffer will show the line Run gdb (like this): gdb Type in the name of the program you want to write. If you type in a.outThe screen will look this:

Current directory is /u2/csc171/csc171/checkers
  GDB 4.0.1, Copyright (C) 1991 Free Software Foundation, Inc.
  There is ABSOLUTELY NO WARRANTY for GDB; type "info warranty" for details.
  GDB is free software and you are welcome to distribute copies of it
   under certain conditions; type "info copying" to see the conditions.
  Type "help" for a list of commands.
  
  Reading symbols from /u2/csc171/csc171/checkers/a.out
  (gdb)
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  --**-Emacs: *gud-checkers*       (Debugger: run)--All-------------------------
  

The prompt for gdb is (gdb). At this prompt type in break main. This command tells gdb to halt execution when it gets to the main() function. Since this is the first function executed, it will stop almost as soon as it starts the program.

Now run the program by typing the command run at the gdb prompt. The window will split and you will see an arrow next to the first line in your main() function. This is the line that gdb is about to execute.

There are two ways to move forward in the program: step and next. The step command will cause gdb to enter a function and show the code associated with that function if it is about to execute one. The next command causes gdb to continuing executing until it gets to the next line in the current file, regardless of whether it is a function.

Type in the step command. Gdb will change to different files as you step throught he program.

Finishing up the Programs

When you feel comfortable with the way the functions are called, type in the rest of the program from the lab manual. Type it in bit by bit making sure that each part works before going on to the next part.

When you are done, make sure you exit the shell by typing exit at the prompt in the *shell* buffer, and exit from gdb by typing quit at the gdb prompt in the *gud-timer* buffer.

More about Emacs

Emacs has extensive documentation associated with it. To get to this documentation, use the command M-x info. You will see a list of topics, each of which has a * in front of it. Move down to the topic you are interested in and hit return. A new menu of options will appear. To go back to the previous menu, hit u.