Removing duplicate paragraph
[mudman.git] / md / debugging.md
1 ---
2 title: 'A Dynamic Debugging System For Muddle'
3 author: Joel Mayer Berez
4 date: January 1978
5 abstract: Program debugging is a time consuming process. Conventional
6   debugging techniques and aids typically give the user a narrow view
7   of the program's operation, making debugging difficult. A debugging
8   system that would present a clear overall picture of a program's
9   behavior and would be both flexible and simple to operate would be a
10   valuable tool. Such a system was designed and implemented in and for
11   Muddle, a high-level applicative programming language. This report
12   discusses the design alternatives considered during the debugging
13   system's design and implementation phases, the reasons for the
14   resulting design choices, and the system attributes. A major
15   attribute of the system (MEND) is that it does not simulate the
16   program being debugged but instead monitors it from another
17   process. This attribute results in a robust and viable debugging
18   system, because MEND need not be modified in order to handle each
19   new extension to Muddle and/or each new user-defined primitive.
20 ---
21
22 MIT Technical Memo 94
23
24 Laboratory for Computer Science\
25 Massachusetts Institute of Technology\
26 545 Technology Square Cambridge, Massachusetts 02139
27
28 Copyright
29 =========
30
31 This research was supported by the Advanced Research Projects Agency
32 of the Department of Defense and was monitored by the Office of Naval
33 Research under Contract No. N00014-75-C-0661.
34
35 This report was originally published in the United States in January
36 1978 without a copyright notice and without subsequent registration
37 with the U.S. Copyright Office within 5 years. Doing at least one of
38 those was a requirement of United States copyright law at that time.
39 This report is therefore in the public domain in the United States for
40 failure to comply with the required formalities. This means you're
41 free to download, modify and redistribute this report. People outside
42 of the United States must check the copyright laws of their country
43 before downloading or redistributing.
44
45 Abstract
46 ========
47
48 Program debugging is a time consuming process. Conventional debugging
49 techniques and aids typically give the user a narrow view of the
50 program's operation, making debugging difficult. A debugging system
51 that would present a clear overall picture of a program's behavior and
52 would be both flexible and simple to operate would be a valuable tool.
53 Such a system was designed and implemented in for Muddle, a high-level
54 applicative programming language. This report discusses: the design
55 alternatives considered during the debugging system's design and
56 implementation phases, the reasons for the resulting design choices,
57 and the system attributes. A major attribute of the system (MEND) is
58 that it does not simulate the program being debugged but instead
59 monitors it from another process. This attribute results in a robust
60 and viable debugging system, because MEND not be modified in order to
61 handle each new extension to Muddle and/or each new user-defined
62 primitive.
63
64 This report reproduces a thesis of the same title submitted to the
65 Department of Electrical Engineering, Massachusetts Institute of
66 Technology, in partial fulfillment of the requirements for the Degree
67 of Bachelor of Science.
68
69 Acknowledgements
70 ================
71
72 I wish to thank Al Vezza, for supervising this work and guiding me
73 along the road to winnage; Stu Galley, for the original idea; Bruce
74 Daniels and Gerald Farrell, for laying some of the ground work I have
75 built upon; Brian Berkowitz and Chris Reeve, for patiently repairing
76 my ailing Muddles; Marc Blank and Tak To, for support work and for
77 providing me with company during all-night console sessions; and all
78 of the other ITS and DynaMod hackers who have built a system well
79 worth using.
80
81 Introduction
82 ============
83
84 Background
85 ----------
86
87 A time-consuming and frustrating aspect of computer programming is the
88 debugging of faulty programs. Current debugging techniques involve
89 tracing through the present operation of the program and mentally
90 comparing its action with one's concept of what should be happening.
91 With few exceptions, an understanding of where the program fails to
92 conform to "correct" operation must be made before the cause of the
93 failure can be determined and corrective action taken. This is where
94 much of the difficulty occurs.
95
96 In conventional debugging, it is rare for the programmer to have
97 available any more than the most basic aids. One usually has to
98 extrapolate from a bare minimum of information (such as machine
99 generated error messages) or one may be buried under a large excess of
100 information, most irrelevant (such as a core dump). Even with the more
101 advanced aids, the programmer typically gets but a small window in the
102 operation of the program through which, sooner or later, she or he
103 will locate the problem. A well-localized fault will be relatively
104 easy to spot compared to a global problem that the programmer may only
105 catch glimpses of through the debugging window.
106
107 In a compiler language, the programmer's best hope is to insert
108 statements to print intermediate results or to try to separate the
109 program into easier-to-handle modules. There are a few more advanced
110 aids available[^1], but their use is limited. One problem is that the
111 program is, in effect, first translated into a lower-level language
112 (generally "machine" language) and then executed interpretively in
113 that language. The original symbols and syntax of the source program
114 are lost, or saved only with great difficulty, making analysis and
115 manipulation of the executing program a very painful process.
116
117 If the programmer is using an interpretive language with facilities
118 for interaction, things are considerably easier. The common technique
119 is to stop execution at strategic points and examine the state of the
120 environment. Since this is done interactively, with the source program
121 still available in more or less its original form, the cause of the
122 problem can often be found in less time than it could otherwise. one
123 of the best example of this approach is the use of DDT (Dynamic
124 Debugging Tool/Technique)[^2][^3].
125
126 DDT basically works with machine-language programs. However, by freely
127 translating between numbers and symbols through the use of a table
128 generated by the assembler, DDT makes programs look to the programmer
129 like symbolic assembly-language programs. The user of DDT can operate
130 in free-run mode or in n-step (statement) mode, switching between them
131 at will. In either case one can set a breakpoint at any statement,
132 which will cause execution to stop just before the statement is
133 executed. Whenever stopped in DDT, the user can examine or change the
134 contents of any location. This can be done in several data modes
135 (e.g., unsigned octal, full ASCII, sixbit, etc.) including the use of
136 symbols to represent addresses. Arbitrary numeric expressions can also
137 be evaluated without affecting the program.
138
139 One main advantage of DDT is that the debugging environment is very
140 similar to the language environment the programmer used to write the
141 program. One has to learn only the DDT commands rather than an
142 entirely new language. Another advantage is the user's ability for
143 viewing the results of the changes. In addition, one can quickly see
144 the results of the changes and act accordingly.
145
146 The main deficiency of DDT is that, although is names include the word
147 "dynamic", its operation is really static. The application program can
148 run freely, but when the programmer wants to see what is taking place,
149 the program must be stopped. Although the real interest may be about
150 changes on a gross scale, perhaps thousands of program statements, if
151 one does not know exactly where the program is misbehaving, one may be
152 required to suspend execution of it every few instructions to examine
153 variable in order to obtain a true picture of the program's behavior.
154 This the programmers see *not what is taking place*, but *what has
155 taken place*, and through small windows at that. This is inefficient,
156 and the programmer can become bogged down in detail that hinders the
157 discovery of the true problem. The situation can be improved with the
158 use of breakpoints that allow the program is execute freely until a
159 breakpoint is reached, at which point the program halts. DDT is a
160 powerful tool but still leaves much to be desired in a debugging tool.
161
162 ESP[^4][^5] (Execution Simulator and Presenter) is one solution to the
163 static problem. It really is dynamic is that large amounts of data are
164 constantly being displayed for the programmer while the program is
165 being executed (actually, simulated). The information is presented in
166 graphic form to improve readability and reduce confusion. A user of
167 ESP may watch areas of the display where data of particular interest
168 are being presented. One also has many options including control over
169 the speed of execution, the type of quantity of data displayed, and
170 special (more flexible than just a breakpoint) conditions for halting
171 execution. In this way one can structure and control the picture
172 presented to more easily understand what the simulated program is
173 doing. And that is one key step in the process of debugging.
174
175 Like DDT, ESP has deficiencies also. These are mainly in the area of
176 editing and DDT-like examination of a faulty program. DDT is a
177 sophisticated language that ESP does not attempt to entirely replace.
178 The flaw in ESP is that it is not compatible with DDT. Ideally both
179 should be simultaneously available to the programmer, who can use
180 features of each as the need dictates.
181
182 DDT and ESP work with a low-level language whose operation can be
183 shown fairly simply. For example, ESP often shows flow of control by
184 just displaying the actual section of program being executed and
185 pointing to the current statement[^6]. It also draws lines to show
186 where branching has occurred and in some cases even indicates looping.
187 The display philosophy can be readily extended to higher level
188 languages that are line oriented, like BASIC, but it fails with
189 applicative ones, like LISP or Muddle. The latter type does not use a
190 linear control flow, but uses a complex depth-first tree structure.
191 Furthermore, quite complicated data structures can be built (or
192 themselves executed) that bear little relation to the appearance of
193 the program.
194
195 A good basic display for Muddle was used in MUMBLE[^7] (Gerald
196 Farrell's monitor and debugging aid). The code being executed is shown
197 in stack form. Each line shows a piece of code being evaluated. As
198 each object in the bottom line is evaluated, it is replaced by a
199 single downward-arrow symbol in this line and then printed on a new
200 line. In this way the evaluation can be followed from the top level
201 down to the current object being evaluated. Furthermore, after the
202 bottom is reached, the value returned by each line replaces its symbol
203 in the previous line. With this mechanism, the programmer can follow
204 execution in a natural and reasonably clear representation.
205
206 MUMBLE had some difficulties arising from the fact that it simulated
207 execution rather than just watching it and letting it proceed
208 naturally. This caused it to run slowly and to be complex yet fragile.
209 At the time MUMBLE was written, the Muddle compiler was not yet
210 perfected and the language itself lacked some of the multiprocessing
211 features that would have made simulation unnecessary. Later Farrell
212 replaced MUMBLE with a debugger utilizing new software related to
213 single-stepping a process, which eliminated the simulation but also
214 eliminated the feature reflecting results of an evaluation back into
215 the original code. Also, a mode was added that allowed the programmer
216 to attach conditions to parts of the program which would stop
217 execution when and if a condition was false. This is as far as MUMBLE
218 ever progressed, and it was not in use as of the time of the proposal
219 for the current work.
220
221 MEND
222 ----
223
224 After the Muddle compiler became operational and many additional new
225 software features became available, it appeared that it would be
226 possible to design and implement a debugging system that would be
227 comprehensive, easy to use, and reasonably fast. It was therefore
228 proposed that a debugging system for Muddle called MEND (Muddle
229 Executor , aNalyzer, and Debugger) be designed and implemented. It has
230 the following characteristics. (A glossary of special terms, those in
231 all capital letters, used here and throughout the rest of this memo
232 may be found in Appendix B.)
233
234 1.  MEND possesses a display similar to that of MUMBLE, including the
235     replacement of arguments in a FORM by their values as they are
236     evaluated.
237 2.  Execution is monitored from another process (as opposed to being
238     simulated as in ESP) using 1STEP and related features[^8].
239 3.  Execution speed is variable by the user including a static single
240     or multi-step mode where desired.
241 4.  MEND allows execution to run freely below a certain depth of
242     evaluation or between certain points in the program and to run
243     controlled elsewhere.
244 5.  Unconditional and conditional breakpoints are available that can
245     be attached to any object to halt execution before evaluation of
246     that object.
247 6.  The system is capable of keeping track of programmer specified
248     conditions and of changing modes or giving some visible indication
249     when the conditions are met (or not met).
250 7.  Information, such as the local value of programmer specified ATOMs
251     and the values of programer specified FORMs, is constantly
252     displayed beneath the main display.
253 8.  Each line in the main display area is &-printed (abbreviated
254     printing, see glossary) and can be viewed in full at any time.
255 9.  At any time, with execution stopped, the user can EVAL objects in
256     the ENVIRONMENT of the program. This means the user can examine
257     the state of the program or change it.
258
259 Possible Additional Characteristics
260 -----------------------------------
261
262 Certain other characteristics were seen as desirable for MEND but
263 possibly beyond the scope of this project. If time permitted these
264 features were to be included in the system:
265
266 1.  The IMLAC multi-screen capability would be used to allow the user
267     to rapidly switch between the debugger display and the program's
268     own output. Other system output could also be put on additional
269     pages.
270 2.  The editor IMED (an editor for Muddle objects analogous to
271     IMEDIT[^9]) would be tied into the system to allow easy editing.
272     PRINTTYPE and READ-TABLEs would be used to allow breakpoints to be
273     easily set and removed in IMED as single symbols. Other control
274     codes and statements could also be inserted using this editor.
275 3.  At the applications programmer's option and within certain limits,
276     execution could be reversed either so that something different
277     could be tried or for purposes of reexamining the process for
278     something that may have been missed the first time. This feature
279     could come in two possible forms, the UNDO package[^10] to
280     actually reverse execution or a simulation displaying information
281     previously stored by the system.
282
283 Design and Implementation
284 -------------------------
285
286 MEND was designed with the intent of providing the application
287 programmer with many options so that debugging could proceed in the
288 most suitable manner for each situation. In the normal running state,
289 MEND displays several kinds of information on the screen. Most
290 important is an area showing the execution of the application program
291 being debugged in stack form. The only other area that is always
292 present is a line or two of status information about the current
293 operation of MEND showing its current speed of execution (user
294 adjustable) and the state of each modifiable mode.
295
296 It was intended that the output of the application program be saved by
297 MEND for later reference. The user of MEND could then elect to have
298 the most recent output constantly displayed in a window on the main
299 screen (see section on future work). If multi-screening were
300 available, the output could be kept on another virtual screen. That
301 screen could be displayed or made invisible at the user's option
302 without stopping MEND.
303
304 Information such as programmer specified values of ATOMs, structured
305 objects, and, in general, the value of any Muddle expression may be
306 constantly displayed. MEND is also capable of displaying such
307 information on an exception basis according to some predescribed
308 condition. Such information is &-printed but is viewable in full when
309 desired.
310
311 It is important for a debugging system like MEND to be compatible with
312 and to take advantage of available software in related areas. One such
313 area is editing. There were two Muddle editors in use when the
314 proposal for this work was made, EDIT[^11] and IMED. The main
315 difference between them is that IMED uses the local editing features
316 of the IMLAC while EDIT does not. EDIT, however, has the advantage of
317 being the only one that possesses breakpoint capabilities. Whichever
318 proved to be most compatible with MEND (possibly both) would be
319 slightly modified to allow the setting and removing of certain MEND
320 codes including, in the case of IMED, breakpoints.
321
322 Muddle itself has many features that greatly aid the debugging
323 process. One of these is FRAMES. This function can be used to print
324 the stack of functional evaluations and applications when execution is
325 halted at any depth below the top level. At this point it is also
326 possible to get the values of objects in the current ENVIRONMENT and
327 to change them. One can even restart execution at a higher level after
328 making such changes. Because the Muddle debugging features are quite
329 powerful, MEND was designed to allow the user to stop execution (of
330 the application program) and to use these aids or any others built in
331 to Muddle with the MEND system itself transparent. Evaluation would
332 take place in the ENVIRONMENT of the application program.
333
334 MEND now includes the main features of all the debuggers that have
335 been mentioned and enough other features that it should prove to be
336 quit useful for the analysis and debugging of Muddle programs. It
337 should also serve as a good example of the type of debugging system
338 that can be built around an applicative type language.
339
340 Terminal Display
341 ================
342
343 IMLAC Console Program
344 ---------------------
345
346 One basic concern throughout the project was the display: how the
347 information made available by MEND would be presented to the user. To
348 a large extent the physical characteristics of Muddle, ITS[^12]
349 (Incompatible Timesharing System, the operating system used on the
350 Dynamic Modeling System computer), and the available terminals
351 dictated what was reasonably possible.
352
353 The terminal most commonly used by users of the Dynamic Modeling
354 System is the IMLAC PDS-1. This is a minicomputer capable of having
355 programs loaded into it from the PDP-10 host computer. One program
356 written for it by Dave Lebling is MSC, a multiple-screen terminal
357 program. Up to four virtual screens (or pages) can be created that
358 will individually operate like the actual screen area of the standard
359 terminal program (SSV\[\^13). Output may be directed to any one of the
360 screens and any screen may be visible or invisible at any instant of
361 time, at the programmer's option. Selection of screen is controllable
362 either by program from the host or locally by the user.
363
364 It was originally Intended that MEND would use MSC for its normal
365 display. One page would constantly show MEND's representation of the
366 control stack of the application program. Output initiated by the
367 program being debugged would go to a second page. A third page would
368 be used for interaction with MEND and would show user typein along
369 with any output from MEND that one was interested in seeing. The
370 latter would include, by user request, full displays of both objects
371 printed in an abbreviated form on the page containing the control
372 stack of the application program and values being monitored or traced.
373 The user could switch back and forth among the pages at will during
374 the execution of the program. The application program would have a
375 full standard screen to write onto, and ample room would be available
376 for the information to be displayed by MEND.
377
378 After a fair amount of testing, this proposal was discarded for the
379 following reasons. First, MSC was supposed to look just like SSV for
380 individual virtual screens. Unfortunately new features added to SSV
381 had not also been added to MSC. A primary reason for this disparity
382 was that the new features encroached upon the IMLAC's character space.
383 An SSV with all current features can only hold about one and a half
384 full pages of text, where a page is the amount that can be visible at
385 one time. The overhead required for additional screens reduces it
386 still further. Therefore, with all features included, four virtual
387 screens could each only average about one-third full. Without many of
388 the current features, people were reluctant to use MSC.
389
390 The second and perhaps most devastating problem with MSC is that it is
391 not properly supported by ITS as is SSV. Ordinarily the operating
392 system will keep track of where on the page the terminal's cursor is
393 and will properly handle such updating as deletions even in the face
394 of random access performed (if done by request to the system). When
395 using MSC, ITS does not realize that information is being written onto
396 more than one screen and will therefore often move the cursor to the
397 wrong position.
398
399 MEND could completely control positioning for typeout and echoing on
400 typein but that would add the large overhead of having to run a
401 non-trivial routine for each character typed out on the display. Also,
402 because MSC is not the standard console program, requiring the user to
403 load it before using MEND might discourage the use of MEND. (It takes
404 between about 30 seconds and a couple of minutes, depending upon
405 system load, to load a new console program into the IMLAC.)
406
407 From the outset it was intended that MEND be used routinely by
408 programmers as debugging problems arose. Therefore it was decided that
409 the proper way for such a system to operate was to use, as far as
410 possible, the common environment so as to keep the overhead for
411 invoking MEND small. This philosophy, which had been seen to affect
412 the success of many earlier projects, decided between the alternatives
413 and in this case led to the final decision to use SSV instead of MSC.
414
415 Terminal Independence
416 ---------------------
417
418 The MEND terminal handling capabilities are actually quite general and
419 MEND does not depend exclusively on SSV. For the purpose of dividing
420 the screen area into several sections, horizontal lines are sometimes
421 drawn (see Appendix A showing sample display). With an IMLAC and SSV
422 these lines could be drawn quite simply using graphics mode. However,
423 for purposes of generality with regard to terminals, these lines are
424 instead formed by using underbar characters (on a line of their own).
425 By using no actual graphics MEND can be used with almost any display
426 terminal having random access. MEND outputs display commands, such as
427 clearing a line, as escape codes to ITS which then translates these
428 into the appropriate commands for the terminal in use. ITS currently
429 knows about several types of display terminals in use at the
430 Laboratory for Computer Science, and other types of terminals located
431 at foreign sites on the ARPA network may be handled by interface
432 software that simulates a known type. Naturally MEND can handle a
433 large range of possible line and screen lengths. (The current version
434 of SSV provides four possible character sizes.)
435
436 Control of Screen Sectioning
437 ----------------------------
438
439 There still remained the question of how to handle the
440 multi-sectioning of the displayed information. Originally three
441 sections corresponding to the three virtual screens in the aborted MSC
442 implementation were planned. The bottom section would hold user typein
443 and application program output. Three possible methods of achieving
444 this involved having ITS, Muddle, or MEND do various amounts of the
445 work with increasing overhead and decreasing speed for those three,
446 respectively.
447
448 The most attractive solution utilized an ITS feature allowing the
449 specification of an echo area at the bottom of the screen where echoed
450 input would always be printed (with the echoing handled by the system,
451 which is the normal case) . After some experimentation this method of
452 handling the typein and application program output was rejected
453 because typeout and deletion are handled by Muddle which ignores the
454 echo area MEND would effectively have had to control all typeout and
455 monitor all typein, which would have made the echo area useless.
456
457 Experimentation with the second solution, an indirect method, involved
458 monitoring of special Muddle memory locations where information
459 concerning horizontal and vertical page positions is stored. It was
460 soon discovered that Muddle becomes confused quickly, contains several
461 bugs with respect to this position information, and generally has a
462 poor idea of where it is actually printing.
463
464 The third solution appeared to be the most painful from an
465 implementation and efficiency point of view. MEND would need to
466 control the printing of every character on output and certain
467 characters on input, constantly checking page positions by querying
468 the system. Not only did this slow output, but also MEND was forced to
469 constantly move the cursor to a safe position in case Muddle managed
470 to sneak some output past it, which it occasionally does.
471
472 Fortuitously the problem was neatly solved when a little known feature
473 of ITS was discovered. It Is possible to open a channel to the
474 terminal in a mode that would cause all output to appear in the echo
475 area. By creating this echo area and reopening Muddle's normal
476 terminal output channel in this mode, it is possible to cause Muddle
477 and the application program to think that the entire screen consists
478 of only the echo area. All output including application program
479 display escape commands is automatically routed by ITS to this echo
480 area. MEND sends its output to a second channel opened in the normal
481 mode, thereby allowing it to use an area of the screen unknown to and
482 left untouched by the application program. The physical cursor stays
483 where the last used logical cursor left it, thereby eliminating most
484 of the unnecessary cursor movement, resulting in a more pleasing
485 visual effect.
486
487 As a result of this solution, the screen is divided into only two main
488 sections. All typein appears in the lower section, whether it is to
489 the application program or to MEND. Application program output also
490 goes to the lower section, as does unstructured output produced by
491 interaction with MEND. The upper section, in general, contains only
492 those items that occupy one line of the display each. As will be
493 explained later, these items include all output automatically
494 displayed by MEND during execution of the application program.
495
496 [^1]: G.A. Mann, "A Survey of Debug Systems", Honeywell Computer
497     Journal 1973, volume 7, number 3, pages 182-198
498
499 [^2]: Digital Equipment Corporation, "DDT-10 Programmer's Reference
500     Manual", Assembly Language Handbook, DEC-10-NRZA-D
501
502 [^3]: B. Bressler, DDT: A Debugging Aid, SYS.06.01, Programming
503     Technology Division Document, Laboratory for Computer Science,
504     M.I.T., November, 1971
505
506 [^4]: S.W. Galley, Debugging with ESP -- Execution Simulator and
507     Presenter, SYS.09.01, Programming Technology Division Document,
508     Laboratory for Computer Science, M.I.T., November, 1971
509
510 [^5]: S.W. Galley and R.P. Goldberg, "Software Debugging: The Virtual
511     Machine Approach", Proceedings of the Association for Computing
512     Machinery Annual Conference, November, 1974, volume 2, pages
513     395-401
514
515 [^6]: M.H. Liu, DETAIL: A Graphical Debugging Tool, S.B. Thesis,
516     Department of Electrical Engineering, M.I.T., February, 1972
517
518 [^7]: G.J. Farrell, A System for Muddle Programming, M.S. Thesis,
519     Department of Electrical Engineering, M.I.T., August, 1973
520
521 [^8]: S.W. Galley and G. Pfister, Muddle Programming Language Primer
522     and Manual, Laboratory for Computer Science, M.I.T., May, 1977
523
524 [^9]: J. Haverty, IMEDIT Editor Program for Use with the Imlac
525     Terminals, SYS.08.01.02, Programming Technology Division Document,
526     Laboratory for Computer Science, M.I.T., August, 1972
527
528 [^10]: B. Berkowitz, UNDO, Undergraduate Research Report, Programming
529     Technology Division, Laboratory for Computer Science, M.I.T.,
530     December, 1974
531
532 [^11]: N. Ryan, EDIT: The Muddle Editor, SYS.11.14, Programming
533     Technology Division Document, Laboratory for Computer Science,
534     M.I.T., August, 1974
535
536 [^12]: D. Eastlake, R. Greenblatt, J. Holloway, T. Knight, and S.
537     Nelson, ITS 1.5 Reference Manual, Memo No. 161A, Artificial
538     Intelligence Laboratory, M.I.T., July 1969