home *** CD-ROM | disk | FTP | other *** search
- p @"This session is under the control of a command file"
- p @"that is being read in. At the appropriate time, you will"
- p @"be prompted to <Hit Any Key to Continue>."
- p @"The command file may be aborted by entering a Cmd-C or Ctrl-C"
- p @"at the prompt."
- p @" "
- p @"The program being executed is count. It's purpose is to read the"
- p @"contents of a text file and print some statistics about the file."nn
- p @" "
- &
- p @"To start the session, let's make room for some dialog."
- p @"If you were doing this on your own, you would click on"
- p @"the rightside of the command window and drag it, or you"
- p @"could use the Cmd-Shift-UpArrow combination to move it up."
- p @"But during this demo, the command file will do it for you."
- &
- p @n"As you can see, the screen is divided into three regions."
- p @"1) The top window is for the display of your C source code."
- p @"The current source line about to be executed is highlighted"
- p @"to distinguish it from the others."
- p @"2) The middle window is where you enter commands to be executed."
- p @"3) The bottom window is where the command output is displayed."
- p @"Actual program output is displayed on the screen behind the"
- p @"sdb windows. It can we be seen by either changing the size of"
- p @"the window sdb is running in or by bring the screen window"
- p @"to the front."
- p @" "
- p @"Another nice feature is the fact that the source and data windows"
- p @"are both buffered. This allows you to return and view something"
- p @"that may already have scrolled off the screen, or to view parts"
- p @"of the program other than that currently being executed."
- p @"To demonstrate, we'll scroll the source window down five lines."
- &
- p @n"Since that moved the highlighted line off the screen,"
- p @"we'll use the c command to restore the source context."nn
- &
- oe
- c
- oe
- p @n"We'll now scroll the data window back ten lines."
- &
- &
- p @n"As you probably noticed, the data window automatically"
- p @"returns to where you last displayed commands when it's"
- p @"time to display addtional information."
- p @" "
- p @"Moving back and forth in the source or data windows can"
- p @"be done using either the mouse, or keyboard equivalents."
- p @"Clicking directly on an arrow will scroll the text one line"
- p @"at a time. To scroll source text from the keyboard, hold down"
- p @"the shift key and use either the UpArrow or the DownArrow keys."
- p @"To move the data window contents hold down the command key and"
- p @"use the arrow keys to indicate the direction to scroll it. Look"
- p @"at the documentation file to see the other keyboard equivalents."
- p @" "
- p @"We will now resize the windows, and begin executing the program"
- p @"that's been loaded."
- &
- p @n"At this point we haven't actually started the function main() even"
- p @"though the source for it is displayed. This being the case we can"
- p @"only view the contents of global variables since local variables"
- p @"haven't been initialized yet. To initialize local variables we'll"
- p @"do a single step using the s command."
- &
- s
- p @n"sdb?s"nn
- p @"As you surely noticed, the highlighting bar moved to the first"
- p @"executable line of C source. Nonexecutable lines such as variable"
- p @"declarations, are stepped over since they don't represent actual"
- p @"code. The contents of both global and local variables can now "
- p @"be displayed."
- &
- p @n"To demonstrate this, let's print the contents of inword which"
- p @"should have been initialized to FALSE or 0."
- p @" "
- oe
- p inword
- oe
- p @n"As you may have noticed, the p command knows the type of the"
- p @"variable and displays the value according to its type. This applies"
- p @"to aggregate types such as structures as well as the scalar types."
- &
- p @n"There are several ways to execute the program's code. The two"
- p @"basic ones are s and t. They differ in that t treats functions"
- p @"as a single step while s steps through them. Either one can be"
- p @"prefixed with a number which indicates the number of steps to be"
- p @"executed. The count defaults to one if not specified. In lower"
- p @"case the s and t commands will update the display after each step"
- p @"(i.e. scroll source window and move highlighting bar to next"
- p @"line), while when entered in uppercase they don't update the"
- p @"display until all the steps are executed."
- p @n"Let's now execute the next 2 lines."nn
- &
- oe
- 2s
- oe
- p @n"We've now opened the text file that will be analyzed. The name"
- p @"of the file pointed to by the pointer name can be printed with"
- p @"not much more work than was used to diplay the contents of inword."
- p @"The only addition is formating information indicating we want"
- p @"to view a character string."nn
- oe
- ps *name
- oe
- p @n"Another alternative is to view the contents of the memory that"
- p @"name is pointing to. This can be done with the db command."nn
- &
- oe
- db *name
- oe
- p @n"As you'd expect, the contents are the same. Other options for the"
- p @"diplay command are dw for displaying words and dl for displaying"
- p @"long words. These may optionally be followed by a RANGE."
- &
- p @n"At this point we're ready to read the contents of the file into"
- p @"a buffer and call countwords() to analyse what was read in. Since"
- p @"we're unsure at this time how many times the while loop will"
- p @"execute, we'll set a temporary breakpoint following it and let the"
- p @"program run until it gets there. We can do this with the g for"
- p @"go command followed by the line to stop on which is count.c.46"nn
- &
- oe
- g count.c.46
- oe
- p @n"Now the only thing that remains to be done is to actually print"
- p @"out the results which should tell us if there are any bugs."
- p @"To do this we'll let the program execute to it's end using the"
- p @"g command with a temporary breakpoint on line 58."
- p @"If we just used a g by itself, the debugger would catch the program"
- p @"once it had terminated, but we would have lost the contents of the"
- p @"variables we might want to examine."nn
- &
- oe
- g .58
- oe
- p @n"Since the output was printed on the screen behind the sdb window,"
- p @"you'll have to switch windows to view it. We'll do that at this time"
- p @"and let you note the results, in particular the number of characters"
- p @"counted. After viewing the results <Hit Any Key to Continue>."
- &
- &
- p @n"In case you didn't jot down the values, we'll print the contents"
- p @"of the characters variable for you to see."nn
- oe
- p characters
- oe
- p @n"The file itself contains the correct character count. To view it"
- p @"let's use the df command to display it in place of the C source."nn
- &
- oe
- df count.txt
- oe
- p @n"As you can see, there are only 630 of them even though the program"
- p @"indicated that there were more. You may have noticed that some"
- p @"of the other totals don't match either."
- p @" "
- p @"Well let's reload the program and check it to see what's wrong."
- p @"To do this we'll use the lp command."nn
- &
- oe
- lp
- oe
- p @n"Now that we know there's a problem with the program, it's"
- p @"time to start looking for it. Since the main() routine doesn't"
- p @"do much other than read the text into the buffer, the problem"
- p @"must be with one of the other two functions. Therefore we'll"
- p @"set breakpoints at the start of both countwords() and analyze()."
- p @" "
- p @"To determine the line number to set the breakpoint on, we'll"
- p @"repostion down to countwords() in the source window using the"
- p @"search command."nn
- &
- oe
- /^countwords
- oe
- p @n"The line numbers for countwords() are now visible so we can set a"
- p @"break point on the first executable line which is .77"nn
- &
- oe
- bs .77
- oe
- p @n"For the routine analyze() we'll use an alternate method to set"
- p @"the breakpoint. In this case we'll use the label analyze as"
- p @"the start of the routine that we want to stop in."nn
- &
- oe
- bs analyze
- oe
- p @n"To verify where we've actually set the breakpoints we'll use the"
- p @"bd command to display all the breakpoints currently set."nn
- &
- oe
- bd
- oe
- p @n"As you can see by looking at the help screen or documentation"
- p @"file, other options such as a COUNT, CONDition, and COMMAND"
- p @"can be set. We'll leave these for you to experiment with"
- p @"on your own later."
- p @" "
- p @"Right now let's go until we hit the first breakpoint."nn
- &
- oe
- g
- oe
- p @n"We've now entered countwords(). Let's examine the contents"
- p @"of inword to see what was passed in."nn
- &
- oe
- p inword
- oe
- p @n"As we hoped, the contents of inword is 0 for FALSE, the same"
- p @"as the inword in main() that was initialized to FALSE."
- p @"Since we know the character count was incorrect, let's"
- p @"step through the first couple of characters being read in"
- p @"to see if they're handled correctly. We'll first display"
- p @"the contents of the buffer using the db command."nn
- &
- oe
- db buffer
- oe
- p @" "
- p @"Since we now know what we're supposed to be reading in,"
- p @"(i.e. two spaces followed by the word COUNT) we'll start stepping"
- p @"through the code. At line 87 the program gets a character from"
- p @"the buffer and places it in the variable code. Let's set a breakpoint"
- p @"there and print out the values of count, code, and character."
- p @"Actually we want the breakpoint one line beyond so the statement"
- p @"will already have been executed. We'll print the three variables"
- p @"using a macro definition which will then be executed each time the"
- p @"breakpoint is hit. We'll set up the macro first."nn
- &
- oe
- x p3 p count; p code; p characters
- oe
- p @n"Now we'll set the breakpoint at line .80 and have it execute"
- p @"the macro p3 defined above."nn
- &
- oe
- bs .80; x p3
- oe
- p @n"With everything set up let's start the program executing with"
- p @"a g and see what happens."nn
- &
- oe
- g
- oe
- p @n"And another g."nn
- &
- oe
- g
- oe
- p @n"So far we've read in the two leading spaces (i.e. ASCII 32) and the"
- p @"value of count has been incremented twice, while the character count"
- p @"remains a zero as it should. Let's now see what happens when we read"
- p @"the next character which isn't a space."nn
- &
- oe
- g
- oe
- p @n"Well we read an ASCII 67 in which corresponds to the 'C' and count"
- p @"has been incremented, but characters remains a zero since we haven't"
- p @"reached the place where it's incremented. Let's do another g to see"
- p @"if the variable characters will be incremented like it's supposed to."nn
- &
- oe
- g
- oe
- p @n"Whoops we hit the breakpoint set earlier for analyze(). Since"
- p @"we're really intrested in what's happening in countwords() we'll"
- p @"enter another g to see what happened to the variable characters."nn
- &
- oe
- g
- oe
- p @n"Sure enough it's been incremented. Let's just step through a few"
- p @"more times to see if it continues to be incremented correctly."nn
- &
- oe
- g
- oe
- p @n"We're stopped again in analyze(). Since we're not very interested"
- p @"in examining what's happening here, let's clear the breakpoint."nn
- &
- oe
- bc analyze
- oe
- p @n"An alternative method of removing this breakpoint would have been"
- p @" "
- p @"bc pc"
- p @" "
- p @"since the program counter is currently on the label analyze."
- p @" "
- p @"As long as we're here let's demonstrate a few features that are"
- p @"useful when you're nested several layers deep in function calls."
- p @"One of these is to display a stack backtrace using ds."nn
- &
- oe
- ds
- oe
- p @n"Another is the frame up and frame down commands to view the context"
- p @"of where you were called from. Before doing that, let's do a single"
- p @"step to initialize all the local variables."nn
- &
- s
- p @"sdb?s"
- p @n"Now we can print the contents of inword to see what it is locally."nn
- &
- oe
- p inword
- oe
- p @n"It's a one as expected since it was assigned a TRUE back in"
- p @"'countwords'. Let's set it to a -1 using the evaluate command e."nn
- &
- oe
- e inword = -1
- oe
- p @n"To see that it's really been set, we'll print it again."nn
- &
- oe
- p inword
- oe
- p @n"Now let's do a frame up to view the context of the function countwords()."nn
- &
- oe
- fu
- oe
- p @n"We'll now print the local variable inword to verify that it's not"
- p @"the same as the one in analyze()."nn
- &
- oe
- p inword
- oe
- p @n"One more frame up will bring us to the function main()."nn
- &
- oe
- fu
- oe
- p @n"And what should the local inword evaluate to here?"nn
- &
- oe
- p inword
- oe
- p @n"A zero of course since it hasn't been assigned the results from"
- p @"calling countwords() yet. Let's now do two frame downs to return"
- p @"to the analyze() function."nn
- &
- oe
- fd
- fd
- oe
- p @n"We'll print out inword here just to show it still contains the -1 we"
- p @"assigned it earlier."nn
- &
- oe
- p inword
- oe
- p @n"Now let's set it back to it's original value of 1 and move on"
- p @"to examining what's happening in countwords()."nn
- &
- oe
- e inword = 1
- oe
- p @" "
- oe
- g
- oe
- p @n"As expected, characters was incremented for the 'O' previously"
- p @"read in, and the next character to examine is a 'U'. Let's go"
- p @"four more times and see what happens when we evaluate the next"
- p @"space."nn
- &
- oe
- g
- g
- g
- g
- oe
- p @" "
- p @"We've now read in and evaluated the space, and while count was"
- p @"incremented the last time, characters was not which is as it's"
- p @"supposed to be. So far, so good."nn
- &
- p @"Since no problems have occurred we'll clear all the breakpoints"
- p @"using bC and let the program run until we get to the other end of"
- p @"the buffer."nn
- &
- oe
- bC
- oe
- p @n"To stop at the other end of the buffer will require either a"
- p @"conditional breakpoint or a skip count preceding the breakpoint."
- p @"Since a conditonaly breakpoint requires running in trace or"
- p @"single step mode, we'll use a regular breakpoint preceded by"
- p @"a skip count. Since the local count is currently equal to 8 and we"
- p @"want to stop before it reaches 512, we'll use a skip count of 502"
- p @"to stop when count reaches 511."nn
- oe
- 502bs .80; x p3
- oe
- &
- oe
- g
- oe
- p @n"Let's now display the last several bytes of the 512 byte buffer."nn
- &
- oe
- db buffer+500
- oe
- p @n"The hex 0x20 following the hex 0x6c is the last character read in"
- p @"corresponding to the code = 32 just displayed."nn
- &
- p @"We'll now clear the breakpoint and stop on line 80 the next time"
- p @"it's encountered using a temporary breakpoint."nn
- &
- oe
- bC
- g .80
- oe
- p @n"We've reached the the temporary breakpoint. Let first display the"
- p @"three variables of interest using the macro p3."nn
- &
- oe
- x p3
- oe
- p @n"Do you see the problem? The variable count started at 0 and is now"
- p @"equal to 512. That means we've looped 513 times and checked that many"
- p @"bytes. But how many bytes were read into the buffer? If we look at"
- p @"the source file, we see that BUFFSIZE was defined as 512."nn
- &
- p @"Since BUFFSIZE is 512 you can see we've read past the end of the buffer."
- p @"In order to correct the problem we would need to change the condition"
- p @"we use to end the for loop from"
- p @" count <= numread"
- p @"to"
- p @" count < numread"
- p @"on line 78. But for this session, let's just skip the last iteration"
- p @"by setting the pc to the return statement on line 99."nn
- &
- oe
- r pc = .99
- oe
- p @n"Now we'll go until we reach a temporary breakpoint at line 79."nn
- &
- oe
- g .79
- oe
- p @n"To correct the execution of countwords() this time we'll start count"
- p @"at 1 by incrementing it by one."nn
- &
- oe
- e ++count
- oe
- p @n"By the way the e for evaluate command may even be used to execute entire"
- p @"functions with arguments, but watch out for any possible side effects."
- p @" "
- p @"At this point we've read in the last of the text file, so we'll let"
- p @"the program run until it exits countwords() at which time we should"
- p @"have a correct character count if everything else works correctly."
- p @" "
- p @"We'll use g @ to execute to the end of the current function."nn
- &
- oe
- g @
- oe
- p @n"Now let's print the contents of characters to see if it contains"
- p @"the correct value this time."nn
- &
- oe
- p characters
- oe
- p @n"Sure enough, it contains the same value we saw in the file count.txt"
- p @"when we examined it. By now we've covered most of the basic commands"
- p @"needed to use sdb. We'll reload the program one more time and let"
- p @"you experiment by entering the commands directly. The escape key or"
- p @"back quote (`) may be used to toggle between sdb and the program output."
- p @"When you've finished working with the program count you can exit sdb by"
- p @"using the q for quit command. The only other command that you'll need to"
- p @"remember is the ? for help. With that you should be able to figure the"
- p @"rest out on your own."nn
- &
- df count.txt
- oe
- lp
- oe
- p @n"The command file has now terminated, and you're in control."
-