Emacs is a very powerful editor with many commands. Because we are running it from a terminal, we cannot use the mouse to interact with emacs; therefore we will give commands using keystrokes. Several of these commands take several keystrokes, and may even require you to type something in. Pressing Ctrl-g tells emacs to cancel whatever command you are currently entering and go back to the default mode.
Note: If emacs isn't working like you expect it to, it's possible that you are stuck in the middle of a command—you entered the first part of the command and now emacs is waiting for you to finish the command. Try pressing Ctrl-g to see if it helps.
Move the scroll bar back down so that emacs fills the screen again.
Although we can no longer see our file, it's still there. Emacs keeps track of several different "buffers", each with its own name and containing its own text. The name of this buffer is *shell* (including the asterisks). You can see the name in the status bar (the second to last line on the screen).
Any buffer (including this one) can be edited like a text file—the only difference is that when we press Enter in this buffer emacs takes what we typed and passes it to the shell. Similarly, we can scroll around this buffer using the cursor. (Remember that this shell is still inside of emacs, so you cannot use the scroll bar.)
Use the Ctrl-x, b command to switch back to the *shell* buffer.
Note: it's very easy to copy and paste using PuTTY. To copy something on the PuTTY window to the clipboard, simply highlight it. It will automatically be copied to the clipboard. To paste into PuTTY, click the right mouse button anywhere on the screen. The text will be pasted at the current cursor position (as if you just typed those characters).
The -g option is important: it tells g++ to compile in debug mode. If we leave it out, we won't be able to debug our program.
This program takes a string and a delimeter. It breaks the string into pieces every time it finds the delimeter and then prints out the pieces. For example, if you enter the string "taz.harding.edu" and the delimeter "." the program should print out "taz", "harding", and "edu".
Hmmm… that didn't seem to work. Let's debug!
Look at the command line (at the bottom of the screen). What does this line say?
Emacs is telling us how it will run gdb. By default, emacs will simply run the program gdb. (gdb is actually a program we could run outside of emacs if we wanted to.) We want to run the gdb program, but we need to give it a command line argument: the name of the program to debug. Emacs will let us enter any command line arguments here on the command line.
Emacs creates yet another buffer to hold the output of the debugger. This one is named *gud-split*. The buffer contains some introductory text telling us about gdb, and a (gdb) prompt where we can type commands into the debugger.
Enter the command: b main What is the debugger's response?
The b (or break) tells the debugger we want to set a breakpoint, and the main tells the debugger where to set it—the main subroutine. We could use this command to set a breakpoint on any subroutine.
Emacs splits the screen into two windows: the top one contains the buffer for the debugger, the bottom one contains the buffer for our source code. (You may want to enlarge your PuTTY window so that you can see more.) This is why we are using emacs; we could run gdb just from a shell, but then we wouldn't be able to see the code at the same time.
Notice that emacs puts an "arrow" (consisting of an equal sign and a greater than sign) at the beginning of the current line. If you look at the status bar for the split.cpp buffer, you can see the characters "L24". This means the current line is line 24.
Woah—what a mess! The problem is that a string is actually a rather complex object with several fields; the debugger is trying to show us all of them. If you look closely, you can in fact see the string "taz.harding.edu" amidst all the other output.
The problem is that the debugger doesn't know what a string is—it
doesn't know that all we really care about are the characters. However,
it does know what a C-style string is. One trick we can use, then, is to
call the string's c_str()
function to get a C-style string.
When at a command prompt in either the shell buffer or the debugger buffer, you can use Alt-p to recall commands you typed previously. In this way you can avoid typing the same command repeatedly.
print_split( input, delim );
. When asked for a delimeter,
enter ".". (Verify that the variable was set correctly.)
Now we've jumped to the first line of the print_split
subroutine. What line number are we on?
int
variables print out much more nicely than strings.
The p command doesn't only work for variables; we can give it any expression to print out. In particular, we can give it an assignment statement, actually changing the value of a variable.
find
function. Let's check to make sure it worked. Enter: p next
Looks good, let's move ahead. It would be nice to jump down to the line
that reads: cout << piece << endl;
We could do that if we
knew the line number.
Now we can move the cursor down to the line we want to jump to. The status bar tells us the line number for that line.
peice
variable. Write the value below.
start
and next
.
Aha—next
is the position of the next delimeter. But
the second argument to substr
should be how many characters
to include, not where to stop.
You might want to view this substring as a C-style string to make it
easier to read. Unfortunately, the c_str()
function does not
work on temporary variables.
Every time you print a value using the p command, it is assigned to a debugger variable which looks like a dollar sign and a number. We can use that variable to reference the value again if we wish.
substr
command. Use the p command to print the
call the .c_str()
function on the convenience variable (for
example: p $8.c_str() Wow, it didn't like that. Write just the first line of its output after that command.
piece= input.substr( start, next - start );
Save the file by pressing: Ctrl-x, Ctrl-s
If you would like to use the emacs editor to make more than trivial edits to a file you should check out my emacs reference page for more tips.