home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PASTUT34 / DEBUG.TXT < prev    next >
Text File  |  1993-06-12  |  7KB  |  146 lines

  1.                         PROGRAM ERRORS & DEBUGGING.
  2.                         ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4. Programming errors inevitably occur and it is fortunate that a debugger
  5. is built into the Turbo Pascal Integrated Development Environment (IDE).
  6.  
  7. There are three basic types of program bugs:
  8.  
  9.  1.   Syntax errors, which are found at compile time, such as the
  10.       omission of an end-of-statement semi-colon or assigning a real
  11.       value to an integer variable.
  12.  
  13.  2.   Semantic errors are found at run-time, and include dividing by
  14.       zero or opening a non-existant file for input.
  15.  
  16.  3.   Logical errors, which are difficult to find. Perhaps a variable
  17.       has not been initialized and hence assumes an unexpected value,
  18.       or the algorithm for the solution of the problem is incorrectly
  19.       chosen.
  20.  
  21.  
  22. 1. Syntax errors.
  23. ═════════════════
  24. Pascal has strict rules, compared with many other programming languages
  25. and Turbo Pascal will not compile the program until all the syntax errors
  26. are removed. Instead it stops compiling, searches the source code and
  27. finds the error, positions the cursor there and displays an error message
  28. in the EDIT window. When corrected, compiling can be restarted and the
  29. next error found, until all errors are removed and compilation is completed.
  30. If the command-line version (TPC.EXE) of Turbo Pascal is used instead of
  31. the IDE version (TURBO.EXE), then the offending line is printed with a line
  32. number and error message so that any alternative editor can be used to
  33. correct the program.
  34.  
  35. 2. Semantic errors.
  36. ═══════════════════
  37. When a compiled program is run under the IDE, any errors which then occur
  38. are automatically located by referring to the source file and an appropriate
  39. error message is displayed at the top of the Edit window, for example:
  40.  
  41.     Error 200: Division by zero.
  42.  
  43. and the program halts, with the cursor at the offending line.
  44. Further details of each numerical error message (###) can be found in the
  45. list of error codes and messages in Appendix A of the Programmer's Guide.
  46.  
  47. If the command-line compiler is used to create an executable file, with the
  48. extension name .EXE and the program is run by typing the filename,
  49. then the message   'Run-time error ### at seg:ofs'  is displayed. The
  50. address should be noted and then the command-line compiler re-used with the
  51. /F option and the address after the filename, for example:
  52.          C:\>TPC TEST /F0000:001C
  53. This then recompiles and finds the offending line in the source code.
  54. See pages 232-3 of the User's Guide for details.
  55.  
  56.  
  57. 3. Logical errors.
  58. ══════════════════
  59. These are the most difficult to find, and would normally require a manual
  60. calculation through the algorithm or the insertion of additional lines in
  61. the program (temporarily) to show values as the program is executed.
  62. Fortunately the Turbo Pascal integrated debugger is included in the IDE,
  63. with two main menus (Run and Debug) with suitable hot keys for some of the
  64. options.
  65. Pages 189-192 of the User's Guide give details of the Run menu's commands,
  66. whilst pages 194-8 give details of the Debug menu's commands.
  67.  
  68. Debug Options.
  69. ══════════════
  70. The debugger of the Integrated Development Environment (IDE) has the
  71. following options:
  72.  
  73. 1. Tracing through the program as it is executed, one line at a time, is
  74.    achieved by selecting the Run Menu (Alt-R) and then T for Trace into
  75.                         ▄▄▄▄▄▄
  76.    or using the hot key ▌ F7 ▐
  77.                         ▀▀▀▀▀▀
  78.    By entering variable names in the Watch Window, the user can see how any
  79.    selected variables change as the program is executed and thus quickly
  80.    indicate any logical errors.
  81.    Variable names are entered in the Watch Window by the key-presses:
  82.    Alt-D for Debug Menu, W for Watches and then A for Add watch or by the
  83.                        ▄▄▄▄▄▄▄▄▄▄▄▄
  84.    hot key combination ▌ CTRL-F7) ▐
  85.                        ▀▀▀▀▀▀▀▀▀▀▀▀
  86.    After these alternative key-presses, a dialog box appears on the screen,
  87.    with a default entry taken as the word currently indicated by the cursor.
  88.    This can be accepted directly or overwritten as desired.
  89.  
  90.    Borland suggest that 'Tracing' is a good way to learn about programming.
  91.  
  92. 2. The option 'Go to Cursor' (Alt-R for Run Menu and then G) causes the
  93.    program to run up to a preselected line in the program where the cursor
  94.    has been located.
  95.                                   ▄▄▄▄▄▄
  96.    The hot key for this option is ▌ F4 ▐
  97.                                   ▀▀▀▀▀▀
  98.  
  99. 3. Lines in the program can be set as 'breakpoints' so that the program
  100.    will stop at each breakpoint, displaying the source code and allowing
  101.    examination of the variables selected in the Watch Window.
  102.    A breakpoint is set or cleared by moving the cursor to the required
  103.    line and then selecting the Debug Menu (Alt-D) followed by T to toggle
  104.    the breakpoint on or off.
  105.                                                      ▄▄▄▄▄▄▄▄▄▄▄
  106.    The hot key combination for toggle breakpoints is ▌ Ctrl-F8 ▐
  107.                                                      ▀▀▀▀▀▀▀▀▀▀▀
  108.  
  109. There are other features of the debugger as listed in the Chapter 5 of the
  110. User's Guide (version 6.0), but for most initial usage the above options
  111. should be sufficient.
  112.  
  113. Selection of the Integrated Debugger.
  114. ═════════════════════════════════════
  115. In order to use the debugger it is necessary to instruct the compiler to
  116. generate the necessary symbol table and line number information for that
  117. program. The symbol table is a database of all the identifiers used.
  118. This requires the use of the compiler directives {$D+} and {$L+}.
  119.  
  120. {$D+} generates the general debug information for global identifiers.
  121. This can also be preset by selecting Options/Compiler/Debugging from the
  122. IDE and then toggling the Debug information box by pressing the spacebar.
  123.  
  124. {$L+} generates local debug information. (i.e. it creates a list of the
  125. identifiers local to each procedure or function). This can also be preset
  126. by the Options/Compiler/Debugging/Local Symbols selection in the IDE.
  127.  
  128. These compiler directives must also be declared in any Unit used by the
  129. program.
  130.  
  131. It is also necessary to ensure that the IDE's debugging switch is on
  132. (Options/Debugger/Debugging/Integrated).
  133.  
  134. It should be noted that the Trace execution is for a single line, which
  135. might include more than one statement. However, if a single statement is
  136. spread over more than one line then the complete statement is executed.
  137.  
  138. Finally it should be noted that the debug procedure does not increase the
  139. size of the .EXE file and there is no need to recompile once debugging is
  140. finished.
  141.  
  142.  
  143. DEBUG.TXT
  144. Revised 18.1.93
  145.  
  146.