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