home *** CD-ROM | disk | FTP | other *** search
/ EDUCORP 8 / Educorp2Compilation.sit / educorp2 / Demos / Aztec Source Level Debugger / demo / count.r < prev    next >
Encoding:
Text File  |  1988-06-24  |  15.9 KB  |  486 lines

  1. p @"This session is under the control of a command file"
  2. p @"that is being read in.  At the appropriate time, you will"
  3. p @"be prompted to <Hit Any Key to Continue>."
  4. p @"The command file may be aborted by entering a Cmd-C or Ctrl-C"
  5. p @"at the prompt."
  6. p @" "
  7. p @"The program being executed is count. It's purpose is to read the"
  8. p @"contents of a text file and print some statistics about the file."nn
  9. p @" "
  10. &
  11. p @"To start the session, let's make room for some dialog."
  12. p @"If you were doing this on your own, you would click on"
  13. p @"the rightside of the command window and drag it, or you"
  14. p @"could use the Cmd-Shift-UpArrow combination to move it up."
  15. p @"But during this demo, the command file will do it for you."
  16. &
  17. p @n"As you can see, the screen is divided into three regions."
  18. p @"1) The top window is for the display of your C source code."
  19. p @"The current source line about to be executed is highlighted"
  20. p @"to distinguish it from the others."
  21. p @"2) The middle window is where you enter commands to be executed."
  22. p @"3) The bottom window is where the command output is displayed."
  23. p @"Actual program output is displayed on the screen behind the"
  24. p @"sdb windows.  It can we be seen by either changing the size of"
  25. p @"the window sdb is running in or by bring the screen window"
  26. p @"to the front."
  27. p @" "
  28. p @"Another nice feature is the fact that the source and data windows"
  29. p @"are both buffered.  This allows you to return and view something"
  30. p @"that may already have scrolled off the screen, or to view parts"
  31. p @"of the program other than that currently being executed."
  32. p @"To demonstrate, we'll scroll the source window down five lines."
  33. &
  34. p @n"Since that moved the highlighted line off the screen,"
  35. p @"we'll use the c command to restore the source context."nn
  36. &
  37. oe
  38. c
  39. oe
  40. p @n"We'll now scroll the data window back ten lines."
  41. &
  42. &
  43. p @n"As you probably noticed, the data window automatically"
  44. p @"returns to where you last displayed commands when it's"
  45. p @"time to display addtional information."
  46. p @" "
  47. p @"Moving back and forth in the source or data windows can"
  48. p @"be done using either the mouse, or keyboard equivalents."
  49. p @"Clicking directly on an arrow will scroll the text one line"
  50. p @"at a time.  To scroll source text from the keyboard, hold down"
  51. p @"the shift key and use either the UpArrow or the DownArrow keys."
  52. p @"To move the data window contents hold down the command key and"
  53. p @"use the arrow keys to indicate the direction to scroll it.  Look"
  54. p @"at the documentation file to see the other keyboard equivalents."
  55. p @" "
  56. p @"We will now resize the windows, and begin executing the program"
  57. p @"that's been loaded."
  58. &
  59. p @n"At this point we haven't actually started the function main() even"
  60. p @"though the source for it is displayed.  This being the case we can"
  61. p @"only view the contents of global variables since local variables"
  62. p @"haven't been initialized yet.  To initialize local variables we'll"
  63. p @"do a single step using the s command."
  64. &
  65. s
  66. p @n"sdb?s"nn
  67. p @"As you surely noticed, the highlighting bar moved to the first"
  68. p @"executable line of C source.  Nonexecutable lines such as variable"
  69. p @"declarations, are stepped over since they don't represent actual"
  70. p @"code.  The contents of both global and local variables can now "
  71. p @"be displayed."
  72. &
  73. p @n"To demonstrate this, let's print the contents of inword which"
  74. p @"should have been initialized to FALSE or 0."
  75. p @" "
  76. oe
  77. p inword
  78. oe
  79. p @n"As you may have noticed, the p command knows the type of the"
  80. p @"variable and displays the value according to its type.  This applies"
  81. p @"to aggregate types such as structures as well as the scalar types."
  82. &
  83. p @n"There are several ways to execute the program's code.  The two"
  84. p @"basic ones are s and t.  They differ in that t treats functions"
  85. p @"as a single step while s steps through them.  Either one can be"
  86. p @"prefixed with a number which indicates the number of steps to be"
  87. p @"executed.  The count defaults to one if not specified.  In lower"
  88. p @"case the s and t commands will update the display after each step"
  89. p @"(i.e. scroll source window and move highlighting bar to next"
  90. p @"line), while when entered in uppercase they don't update the"
  91. p @"display until all the steps are executed."
  92. p @n"Let's now execute the next 2 lines."nn
  93. &
  94. oe
  95. 2s
  96. oe
  97. p @n"We've now opened the text file that will be analyzed.  The name"
  98. p @"of the file pointed to by the pointer name can be printed with"
  99. p @"not much more work than was used to diplay the contents of inword."
  100. p @"The only addition is formating information indicating we want"
  101. p @"to view a character string."nn
  102. oe
  103. ps *name
  104. oe
  105. p @n"Another alternative is to view the contents of the memory that"
  106. p @"name is pointing to.  This can be done with the db command."nn
  107. &
  108. oe
  109. db *name
  110. oe
  111. p @n"As you'd expect, the contents are the same.  Other options for the"
  112. p @"diplay command are dw for displaying words and dl for displaying"
  113. p @"long words.  These may optionally be followed by a RANGE."
  114. &
  115. p @n"At this point we're ready to read the contents of the file into"
  116. p @"a buffer and call countwords() to analyse what was read in.  Since"
  117. p @"we're unsure at this time how many times the while loop will"
  118. p @"execute, we'll set a temporary breakpoint following it and let the"
  119. p @"program run until it gets there.  We can do this with the g for"
  120. p @"go command followed by the line to stop on which is count.c.46"nn
  121. &
  122. oe
  123. g count.c.46
  124. oe
  125. p @n"Now the only thing that remains to be done is to actually print"
  126. p @"out the results which should tell us if there are any bugs."
  127. p @"To do this we'll let the program execute to it's end using the"
  128. p @"g command with a temporary breakpoint on line 58."
  129. p @"If we just used a g by itself, the debugger would catch the program"
  130. p @"once it had terminated, but we would have lost the contents of the"
  131. p @"variables we might want to examine."nn
  132. &
  133. oe
  134. g .58
  135. oe
  136. p @n"Since the output was printed on the screen behind the sdb window,"
  137. p @"you'll have to switch windows to view it.  We'll do that at this time"
  138. p @"and let you note the results, in particular the number of characters"
  139. p @"counted.  After viewing the results <Hit Any Key to Continue>."
  140. &
  141. &
  142. p @n"In case you didn't jot down the values, we'll print the contents"
  143. p @"of the characters variable for you to see."nn
  144. oe
  145. p characters
  146. oe
  147. p @n"The file itself contains the correct character count.  To view it"
  148. p @"let's use the df command to display it in place of the C source."nn
  149. &
  150. oe
  151. df count.txt
  152. oe
  153. p @n"As you can see, there are only 630 of them even though the program"
  154. p @"indicated that there were more.  You may have noticed that some"
  155. p @"of the other totals don't match either."
  156. p @" "
  157. p @"Well let's reload the program and check it to see what's wrong."
  158. p @"To do this we'll use the lp command."nn
  159. &
  160. oe
  161. lp
  162. oe
  163. p @n"Now that we know there's a problem with the program, it's"
  164. p @"time to start looking for it.  Since the main() routine doesn't"
  165. p @"do much other than read the text into the buffer, the problem"
  166. p @"must be with one of the other two functions.  Therefore we'll"
  167. p @"set breakpoints at the start of both countwords() and analyze()."
  168. p @" "
  169. p @"To determine the line number to set the breakpoint on, we'll"
  170. p @"repostion down to countwords() in the source window using the"
  171. p @"search command."nn
  172. &
  173. oe
  174. /^countwords
  175. oe
  176. p @n"The line numbers for countwords() are now visible so we can set a"
  177. p @"break point on the first executable line which is .77"nn
  178. &
  179. oe
  180. bs .77
  181. oe
  182. p @n"For the routine analyze() we'll use an alternate method to set"
  183. p @"the breakpoint.  In this case we'll use the label analyze as"
  184. p @"the start of the routine that we want to stop in."nn
  185. &
  186. oe
  187. bs analyze
  188. oe
  189. p @n"To verify where we've actually set the breakpoints we'll use the"
  190. p @"bd command to display all the breakpoints currently set."nn
  191. &
  192. oe
  193. bd
  194. oe
  195. p @n"As you can see by looking at the help screen or documentation"
  196. p @"file, other options such as a COUNT, CONDition, and COMMAND"
  197. p @"can be set.  We'll leave these for you to experiment with"
  198. p @"on your own later."
  199. p @" "
  200. p @"Right now let's go until we hit the first breakpoint."nn
  201. &
  202. oe
  203. g
  204. oe
  205. p @n"We've now entered countwords().  Let's examine the contents"
  206. p @"of inword to see what was passed in."nn
  207. &
  208. oe
  209. p inword
  210. oe
  211. p @n"As we hoped, the contents of inword is 0 for FALSE, the same"
  212. p @"as the inword in main() that was initialized to FALSE."
  213. p @"Since we know the character count was incorrect, let's"
  214. p @"step through the first couple of characters being read in"
  215. p @"to see if they're handled correctly.  We'll first display"
  216. p @"the contents of the buffer using the db command."nn
  217. &
  218. oe
  219. db buffer
  220. oe
  221. p @" "
  222. p @"Since we now know what we're supposed to be reading in,"
  223. p @"(i.e. two spaces followed by the word COUNT) we'll start stepping"
  224. p @"through the code.  At line 87 the program gets a character from"
  225. p @"the buffer and places it in the variable code.  Let's set a breakpoint"
  226. p @"there and print out the values of count, code, and character."
  227. p @"Actually we want the breakpoint one line beyond so the statement"
  228. p @"will already have been executed.  We'll print the three variables"
  229. p @"using a macro definition which will then be executed each time the"
  230. p @"breakpoint is hit.  We'll set up the macro first."nn
  231. &
  232. oe
  233. x p3 p count; p code; p characters
  234. oe
  235. p @n"Now we'll set the breakpoint at line .80 and have it execute"
  236. p @"the macro p3 defined above."nn
  237. &
  238. oe
  239. bs .80; x p3
  240. oe
  241. p @n"With everything set up let's start the program executing with"
  242. p @"a g and see what happens."nn
  243. &
  244. oe
  245. g
  246. oe
  247. p @n"And another g."nn
  248. &
  249. oe
  250. g
  251. oe
  252. p @n"So far we've read in the two leading spaces (i.e. ASCII 32) and the"
  253. p @"value of count has been incremented twice, while the character count"
  254. p @"remains a zero as it should.  Let's now see what happens when we read"
  255. p @"the next character which isn't a space."nn
  256. &
  257. oe
  258. g
  259. oe
  260. p @n"Well we read an ASCII 67 in which corresponds to the 'C' and count"
  261. p @"has been incremented, but characters remains a zero since we haven't"
  262. p @"reached the place where it's incremented.  Let's do another g to see"
  263. p @"if the variable characters will be incremented like it's supposed to."nn
  264. &
  265. oe
  266. g
  267. oe
  268. p @n"Whoops we hit the breakpoint set earlier for analyze().  Since"
  269. p @"we're really intrested in what's happening in countwords() we'll"
  270. p @"enter another g to see what happened to the variable characters."nn
  271. &
  272. oe
  273. g
  274. oe
  275. p @n"Sure enough it's been incremented.  Let's just step through a few"
  276. p @"more times to see if it continues to be incremented correctly."nn
  277. &
  278. oe
  279. g
  280. oe
  281. p @n"We're stopped again in analyze().  Since we're not very interested"
  282. p @"in examining what's happening here, let's clear the breakpoint."nn
  283. &
  284. oe
  285. bc analyze
  286. oe
  287. p @n"An alternative method of removing this breakpoint would have been"
  288. p @" "
  289. p @"bc pc"
  290. p @" "
  291. p @"since the program counter is currently on the label analyze."
  292. p @" "
  293. p @"As long as we're here let's demonstrate a few features that are"
  294. p @"useful when you're nested several layers deep in function calls."
  295. p @"One of these is to display a stack backtrace using ds."nn
  296. &
  297. oe
  298. ds
  299. oe
  300. p @n"Another is the frame up and frame down commands to view the context"
  301. p @"of where you were called from.  Before doing that, let's do a single"
  302. p @"step to initialize all the local variables."nn
  303. &
  304. s
  305. p @"sdb?s"
  306. p @n"Now we can print the contents of inword to see what it is locally."nn
  307. &
  308. oe
  309. p inword
  310. oe
  311. p @n"It's a one as expected since it was assigned a TRUE back in"
  312. p @"'countwords'.  Let's set it to a -1 using the evaluate command e."nn
  313. &
  314. oe
  315. e inword = -1
  316. oe
  317. p @n"To see that it's really been set, we'll print it again."nn
  318. &
  319. oe
  320. p inword
  321. oe
  322. p @n"Now let's do a frame up to view the context of the function countwords()."nn
  323. &
  324. oe
  325. fu
  326. oe
  327. p @n"We'll now print the local variable inword to verify that it's not"
  328. p @"the same as the one in analyze()."nn
  329. &
  330. oe
  331. p inword
  332. oe
  333. p @n"One more frame up will bring us to the function main()."nn
  334. &
  335. oe
  336. fu
  337. oe
  338. p @n"And what should the local inword evaluate to here?"nn
  339. &
  340. oe
  341. p inword
  342. oe
  343. p @n"A zero of course since it hasn't been assigned the results from"
  344. p @"calling countwords() yet.  Let's now do two frame downs to return"
  345. p @"to the analyze() function."nn
  346. &
  347. oe
  348. fd
  349. fd
  350. oe
  351. p @n"We'll print out inword here just to show it still contains the -1 we"
  352. p @"assigned it earlier."nn
  353. &
  354. oe
  355. p inword
  356. oe
  357. p @n"Now let's set it back to it's original value of 1 and move on"
  358. p @"to examining what's happening in countwords()."nn
  359. &
  360. oe
  361. e inword = 1
  362. oe
  363. p @" "
  364. oe
  365. g
  366. oe
  367. p @n"As expected, characters was incremented for the 'O' previously"
  368. p @"read in, and the next character to examine is a 'U'.  Let's go"
  369. p @"four more times and see what happens when we evaluate the next"
  370. p @"space."nn
  371. &
  372. oe
  373. g
  374. g
  375. g
  376. g
  377. oe
  378. p @" "
  379. p @"We've now read in and evaluated the space, and while count was"
  380. p @"incremented the last time, characters was not which is as it's"
  381. p @"supposed to be.  So far, so good."nn
  382. &
  383. p @"Since no problems have occurred we'll clear all the breakpoints"
  384. p @"using bC and let the program run until we get to the other end of"
  385. p @"the buffer."nn
  386. &
  387. oe
  388. bC
  389. oe
  390. p @n"To stop at the other end of the buffer will require either a"
  391. p @"conditional breakpoint or a skip count preceding the breakpoint."
  392. p @"Since a conditonaly breakpoint requires running in trace or"
  393. p @"single step mode, we'll use a regular breakpoint preceded by"
  394. p @"a skip count.  Since the local count is currently equal to 8 and we"
  395. p @"want to stop before it reaches 512, we'll use a skip count of 502"
  396. p @"to stop when count reaches 511."nn
  397. oe
  398. 502bs .80; x p3
  399. oe
  400. &
  401. oe
  402. g
  403. oe
  404. p @n"Let's now display the last several bytes of the 512 byte buffer."nn
  405. &
  406. oe
  407. db buffer+500
  408. oe
  409. p @n"The hex 0x20 following the hex 0x6c is the last character read in"
  410. p @"corresponding to the code = 32 just displayed."nn
  411. &
  412. p @"We'll now clear the breakpoint and stop on line 80 the next time"
  413. p @"it's encountered using a temporary breakpoint."nn
  414. &
  415. oe
  416. bC
  417. g .80
  418. oe
  419. p @n"We've reached the the temporary breakpoint.  Let first display the"
  420. p @"three variables of interest using the macro p3."nn
  421. &
  422. oe
  423. x p3
  424. oe
  425. p @n"Do you see the problem?  The variable count started at 0 and is now"
  426. p @"equal to 512.  That means we've looped 513 times and checked that many"
  427. p @"bytes.  But how many bytes were read into the buffer?  If we look at"
  428. p @"the source file, we see that BUFFSIZE was defined as 512."nn
  429. &
  430. p @"Since BUFFSIZE is 512 you can see we've read past the end of the buffer."
  431. p @"In order to correct the problem we would need to change the condition"
  432. p @"we use to end the for loop from"
  433. p @"     count <= numread"
  434. p @"to"
  435. p @"     count < numread"
  436. p @"on line 78.  But for this session, let's just skip the last iteration"
  437. p @"by setting the pc to the return statement on line 99."nn
  438. &
  439. oe
  440. r pc = .99
  441. oe
  442. p @n"Now we'll go until we reach a temporary breakpoint at line 79."nn
  443. &
  444. oe
  445. g .79
  446. oe
  447. p @n"To correct the execution of countwords() this time we'll start count"
  448. p @"at 1 by incrementing it by one."nn
  449. &
  450. oe
  451. e ++count
  452. oe
  453. p @n"By the way the e for evaluate command may even be used to execute entire"
  454. p @"functions with arguments, but watch out for any possible side effects."
  455. p @" "
  456. p @"At this point we've read in the last of the text file, so we'll let"
  457. p @"the program run until it exits countwords() at which time we should"
  458. p @"have a correct character count if everything else works correctly."
  459. p @" "
  460. p @"We'll use g @ to execute to the end of the current function."nn
  461. &
  462. oe
  463. g @
  464. oe
  465. p @n"Now let's print the contents of characters to see if it contains"
  466. p @"the correct value this time."nn
  467. &
  468. oe
  469. p characters
  470. oe
  471. p @n"Sure enough, it contains the same value we saw in the file count.txt"
  472. p @"when we examined it.  By now we've covered most of the basic commands"
  473. p @"needed to use sdb.  We'll reload the program one more time and let"
  474. p @"you experiment by entering the commands directly.  The escape key or"
  475. p @"back quote (`) may be used to toggle between sdb and the program output."
  476. p @"When you've finished working with the program count you can exit sdb by"
  477. p @"using the q for quit command.  The only other command that you'll need to"
  478. p @"remember is the ? for help.  With that you should be able to figure the"
  479. p @"rest out on your own."nn
  480. &
  481. df count.txt
  482. oe
  483. lp
  484. oe
  485. p @n"The command file has now terminated, and you're in control."
  486.