Debian Codegraph @

ipl/packs/itweak/dbg_run.icn

raw
1 ############################################################################
2 #
3 # File: dbg_run.icn
4 #
5 # Subject: Icon interactive debugging.
6 # Contains an interactive debugging run-time system.
7 #
8 # Author: Hakan Soderstrom
9 #
10 # Revision: $Revision$
11 #
12 ###########################################################################
13 #
14 # Copyright (c) 1994 Hakan Soderstrom and
15 # Soderstrom Programvaruverkstad AB, Sweden
16 #
17 # Permission to use, copy, modify, distribute, and sell this software
18 # and its documentation for any purpose is hereby granted without fee,
19 # provided that the above copyright notice and this permission notice
20 # appear in all copies of the software and related documentation.
21 #
22 # THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
23 # EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
24 # WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
25 #
26 # IN NO EVENT SHALL HAKAN SODERSTROM OR SODERSTROM PROGRAMVARUVERKSTAD
27 # AB BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL
28 # DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
29 # OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY
30 # OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
31 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #
33 ###########################################################################
34 #
35 # General note: all names are prefixed in an elaborate way in order to
36 # avoid name collisions with the debugged program.
37 # The default prefix for all globally visible names is '__dbg_'.
38 #
39 # This is the reason why lists are frequently used instead of records
40 # (whose field names clutter the global name space).
41 #
42 ###########################################################################
43
44 #
45 #-------- Constants --------
46 #
47
48 # Versions (this program and 'itweak').
49 $define PROGRAM_VERSION "$Revision$"
50
51 # Components of a breakpoint descriptor (list).
52 # Breakpoint id (integer).
53 $define BRKP_ID 1
54 # Source file (string).
55 $define BRKP_FILE 2
56 # File index.
57 $define BRKP_FIDX 3
58 # First line number.
59 $define BRKP_LINE1 4
60 # Second line number.
61 $define BRKP_LINE2 5
62 # Ignore counter (integer).
63 $define BRKP_IGNORE 6
64 # Condition for breaking.
65 $define BRKP_COND 7
66 # Commands to perform on break.
67 $define BRKP_DO 8
68
69 # Constants for 'the current breakpoint' and 'the last breakpoint'.
70 $define BRKP_CURRENT -1
71 $define BRKP_LAST -2
72
73 # Keywords for the 'clear' command.
74 # Definitions must match list in compilation procedure.
75 $define CLEAR_BREAKPOINT 1
76 $define CLEAR_COND 2
77 $define CLEAR_DO 3
78 $define CLEAR_ECHO 4
79 $define CLEAR_MACRO 5
80
81 # Keywords for the 'info' command.
82 # Definitions must match list in compilation procedure.
83 $define INFO_BREAKPOINT 1
84 $define INFO_ECHO 2
85 $define INFO_FILES 3
86 $define INFO_GLOBALS 4
87 $define INFO_LOCALS 5
88 $define INFO_MACROS 6
89 $define INFO_TRACE 7
90 $define INFO_VERSION 8
91
92 # Keywords for the 'set' command.
93 # Definitions must match list in compilation procedure.
94 $define SET_ECHO 1
95 $define SET_PRELUDE 2
96 $define SET_POSTLUDE 3
97
98 # Components of a command definition (list).
99 # Used for built-in commands as well as user-defined macros.
100 # Unabbreviated command/macro name (string).
101 $define CMD_NAME 1
102 # Command code (an integer corresponding to the name).
103 $define CMD_CODE 2
104 # Help text (list of string).
105 $define CMD_HELP 3
106 # Compilation procedure; null if macro.
107 $define CMD_COMPILE 4
108 # Macro definition (list of command instances, list of list).
109 # Null if built-in command.
110 $define CMD_MACRO 5
111 # Executing procedure, if built-in. Null otherwise.
112 $define CMD_EXEC 6
113
114 # Command codes.
115 $define BREAK_CMD 1
116 $define CLEAR_CMD 2
117 $define COMMENT_CMD 3
118 $define CONDITION_CMD 4
119 $define DO_CMD 5
120 $define END_CMD 6
121 $define EPRINT_CMD 7
122 $define FAIL_CMD 8
123 $define FPRINT_CMD 9
124 $define FRAME_CMD 10
125 $define GOON_CMD 11
126 $define HELP_CMD 12
127 $define INFO_CMD 13
128 $define IGNORE_CMD 14
129 $define MACRO_CMD 15
130 $define NEXT_CMD 16
131 $define PRINT_CMD 17
132 $define SET_CMD 18
133 $define SOURCE_CMD 19
134 $define STOP_CMD 20
135 $define TRACE_CMD 21
136 $define WHERE_CMD 22
137 $define USERDEF_CMD 23
138
139 # Environment variable for defining the input file (must be a string value).
140 $define DBG_INPUT_ENV "DBG_INPUT"
141
142 # Environment variable for defining the primary output file
143 # (must be a string value).
144 $define DBG_OUTPUT_ENV "DBG_OUTPUT"
145
146 # Prefix for debugging run-time global names.
147 $define DBG_PREFIX "__dbg_"
148
149 # Maximum source nesting levels.
150 $define MAX_SOURCE_NESTING 12
151
152 # File index is obtained by shifting a small integer left a number of
153 # positions.
154 $define FIDX_SHIFT 10
155
156 # Prompt string to use in initialization mode.
157 $define INIT_PROMPT "debug init $ "
158
159 # Execution return status.
160 # Normal return.
161 $define OK_STATUS 0
162 # Break the command loop, resume execution.
163 $define RESUME_STATUS 1
164 # Break the command loop, terminate the session.
165 $define STOP_STATUS 2
166 # Break the command loop, make the current procedure fail.
167 $define FAIL_STATUS 3
168
169 # Index into '__dbg_g_where'.
170 $define WHERE_FILE 1
171 $define WHERE_LINE 2
172 $define WHERE_PROC 3
173 $define WHERE_BRKP 4
174 $define WHERE_PRELUDE 5
175 $define WHERE_POSTLUDE 6
176
177 #
178 #-------- Record types --------
179 #
180
181 #
182 #-------- Globals --------
183 #
184
185 global __dbg_default_prelude, __dbg_default_postlude
186 # The source text for the default pre/postlude (single command assumed).
187
188 global __dbg_g_automacro
189 # The 'prelude' and 'postlude' macros.
190 # List of two components:
191 # (1) prelude commands,
192 # (2) postlude commands.
193 # Both are lists of compiled commands, not complete macros.
194
195 global __dbg_g_brkpcnt
196 # Counter incremented each break.
197 # Used to identify the file written by 'display' which is used by several
198 # commands.
199 # In this way we can check if we have to write the file anew.
200
201 global __dbg_g_brkpdef
202 # Lookup table for breakpoints.
203 # Entry key is a breakpoint id (integer).
204 # Entry value is a breakpoint descriptor (list).
205
206 global __dbg_g_brlookup
207 # Lookup table for breakpoints.
208 # Entry key is a file index or'ed with a line number (integer).
209 # Entry value is a breakpoint descriptor (list).
210
211 global __dbg_g_brkpid
212 # Id of the latest breakpoint created (integer).
213
214 global __dbg_g_cmd
215 # Table of command and macro definitions.
216 # Entry key is an unabbreviated command/macro name.
217 # Entry value is a command descriptor (list).
218
219 global __dbg_g_display
220 # Name of temporary file used by '__dbg_x_opendisplay' and others.
221
222 global __dbg_g_fileidx
223 # Table mapping source file names on (large) integers.
224 # Entry key is a source file name (string).
225 # Entry value is a file index (integer).
226
227 global __dbg_g_in
228 # The file through which debugging input is taken.
229
230 global __dbg_g_level
231 # Value of &level for the interrupted procedure.
232 # Calculated as &level for the breakpoint procedure - 1.
233
234 global __dbg_g_local
235 # Table containing local variables.
236 # Entry key is variable name (string).
237 # Entry value is the value of the variable (any type).
238
239 global __dbg_g_out1
240 # Primary file for debugging output.
241
242 global __dbg_g_out2, __dbg_g_out2name
243 # Secondary file for debugging output; used for 'set echo'.
244 # Null when no echoing is not active.
245 # The name of this file.
246
247 global __dbg_g_src
248 # Stack of input files used by the 'source' command (list of file).
249 # Empty list when no 'source' command is active.
250
251 global __dbg_g_trace
252 # Current trace level (passed to &trace when resuming execution).
253
254 global __dbg_g_where
255 # A list with data about the current breakpoint.
256 # Contents (symbolic names below):
257 # (1) Source file name (string).
258 # (2) Source line number (integer).
259 # (3) Procedure name (string).
260 # (4) The breakpoint causing this break (breakpoint descriptor, a list).
261
262 global __dbg_g_white
263 # This program's definition of white space.
264
265 # A note on the use of global '__dbg_test' (defined in 'dbg_init.icn').
266 # The runtime system assigns this variable one of the following values.
267 # ** Function 'member' for ordinary testing against the breakpoint sets.
268 # ** Function 'integer' (which is guaranteed to always fail, given a
269 # set as its first parameter) in the 'nobreak' mode; execution continues
270 # without break until the program completes.
271 # ** Integer '2' which causes a break at every intercept point.
272 # (Returns the second parameter which is the line number.)
273
274 #
275 #-------- Globals for Icon functions used by the debuggin runtime --------
276 # In an excruciating effort to avoid being hit by bad manners from the
277 # program under test we use our own variables for Icon functions.
278
279 global __dbg_fany, __dbg_fclose, __dbg_fdelete, __dbg_fexit, __dbg_ffind
280 global __dbg_fgetenv, __dbg_fimage, __dbg_finsert, __dbg_finteger, __dbg_fior
281 global __dbg_fishift, __dbg_fkey, __dbg_fmany, __dbg_fmatch
282 global __dbg_fmove, __dbg_fpop, __dbg_fpos, __dbg_fproc, __dbg_fpush
283 global __dbg_fput, __dbg_fread, __dbg_fremove, __dbg_freverse, __dbg_fright
284 global __dbg_fsort, __dbg_fstring, __dbg_ftab, __dbg_ftable, __dbg_ftrim
285 global __dbg_ftype, __dbg_fupto, __dbg_fwrite, __dbg_fwrites
286
287 #
288 #-------------- Expression management globals -----------
289 #
290
291 global __dbg_ge_message
292 # Holds message if there is a conflict in expression compilation or
293 # evaluation
294
295 global __dbg_ge_singular
296 # Value used as default for the local variable table.
297 # Must be initialized to an empty list (or other suitable value).
298
299 #
300 #-------- Main --------
301 #
302
303 procedure __dbg_proc (file, line, proc_name, var_name, var_val[])
304 # This procedure is invoked a first time during initialization with parameters
305 # all null.
306 # Then it is called every time we hit a breakpoint during a debugging session.
307 # The parameters define the breakpoint, as follows,
308 # 'file': source file name (string).
309 # 'line': source line number (integer).
310 # 'proc_name': name of the current procedure (string).
311 # 'var_name': names of variables local to the current procedure
312 # (list of string).
313 # The list is sorted alphabetically.
314 # 'Local' variables include parameters and static variables.
315 # 'var_val': The current values of the local variables (list).
316 # The values occur in the same order as the names in 'var_name'.
317 # NOTE: In order not to affect the logic of the debugged program this
318 # procedure MUST FAIL.
319 # If it returns anything the current procedure will fail immediately.
320 local bdescr, cond, cmd, idx, tfname
321 # Save trace level; turn tracing off.
322 __dbg_g_trace := &trace
323 &trace := 0
324
325 if \file then { # Not the first-time invocation from "dbg_init".
326 # Increment the global breakpoint counter.
327 __dbg_g_brkpcnt +:= 1
328
329 # Compute the procedure nesting level.
330 __dbg_g_level := &level - 1
331
332 # Begin setting up the 'where' structure.
333 __dbg_g_where := [file, line, proc_name, &null]
334
335 # We get here either because of a 'next', or because we hit a
336 # breakpoint.
337 # If we break because of a 'next' we should not treat this as
338 # a breakpoint, even if there is one on this source line.
339 if __dbg_test === member then {
340 # This is a breakpoint; get it.
341 if bdescr := __dbg_g_brlookup[__dbg_fior (__dbg_g_fileidx[file],
342 line)] then {
343 # Check ignore count.
344 ((bdescr[BRKP_IGNORE] -:= 1) = -1) | fail
345 bdescr[BRKP_IGNORE] := 0
346 }
347 else
348 __dbg_io_cfl ("Mysterious break: %1 (%2:%3).",
349 proc_name, file, line)
350 }
351 else { # Break caused by 'next'.
352 # By convention treated as breakpoint number 0.
353 bdescr := __dbg_g_brkpdef[0]
354 # Check ignore count.
355 ((bdescr[BRKP_IGNORE] -:= 1) = -1) | fail
356 bdescr[BRKP_IGNORE] := 0
357 }
358 __dbg_g_where[WHERE_BRKP] := bdescr
359
360 # Create table of locals.
361 __dbg_g_local := __dbg_ftable (__dbg_ge_singular)
362 every idx := 1 to *var_name do
363 __dbg_g_local[var_name[idx]] := var_val[idx]
364
365 # Evaluate the condition of the breakpoint, if any.
366 if cond := \(bdescr)[BRKP_COND] then {
367 idx := 0
368 __dbg_e_eval (cond[1]) & (idx +:= 1)
369 # Check for conflict.
370 # Make sure we don't resume in such case.
371 __dbg_io_cfl ("[%1] condition '%2'\n %3",
372 bdescr[BRKP_ID], cond[2], \__dbg_ge_message) &
373 (idx +:= 1)
374 (idx > 0) | fail
375 }
376
377 # Reset the test procedure (effective if this is a 'next' break).
378 __dbg_test := member
379
380 # The first command to execute is the macro attached to the
381 # breakpoint, if any; otherwise the prelude.
382 cmd := (\(\bdescr)[BRKP_DO] | __dbg_g_automacro[1])
383 }
384 else { # Initialize global variables for Icon functions.
385 __dbg_func_init ()
386 # Initialize breakpoint globals.
387 __dbg_g_brkpcnt := 0
388 __dbg_g_brkpdef := __dbg_ftable ()
389 __dbg_g_brlookup := __dbg_ftable ()
390 __dbg_g_brkpid := 0
391
392 # Compute the procedure nesting level.
393 __dbg_g_level := &level - 2
394
395 # Create breakpoint number 0, used for 'next' breaks.
396 __dbg_g_brkpdef[0] := [0, "*any*", 0, 0, 0, 0, , ]
397
398 # Display file name.
399 __dbg_g_display := "_DBG" || &clock[4:6] || &clock[7:0] || ".tmp"
400
401 # More globals.
402 __dbg_g_src := []
403 __dbg_g_white := ' \t'
404 __dbg_ge_singular := []
405
406 # Create file index table.
407 idx := -1
408 __dbg_g_fileidx := __dbg_ftable ()
409 every __dbg_g_fileidx[key(__dbg_file_map)] :=
410 __dbg_fishift ((idx +:= 1), FIDX_SHIFT)
411
412 # Open input and output files.
413 if tfname := __dbg_fgetenv (DBG_INPUT_ENV) then
414 __dbg_g_in := __dbg_x_openfile (tfname)
415 (/__dbg_g_in := &input) | __dbg_fpush (__dbg_g_src, &input)
416
417 if tfname := __dbg_fgetenv (DBG_OUTPUT_ENV) then
418 __dbg_g_out1 := __dbg_x_openfile (tfname, 1)
419 /__dbg_g_out1 := &errout
420
421 # Initialize command definitions.
422 __dbg_cmd_init ()
423
424 # Set up the breakpoint data structure.
425 # This is not a breakpoint; the following keeps some commands from
426 # crashing.
427 __dbg_g_local := __dbg_ftable ()
428 __dbg_g_where := [&null, 0, "main", &null]
429 __dbg_default_prelude :=
430 "fprint \"[%1] %2 (%3:%4)\\n\";&bp;&proc;&file;&line"
431 __dbg_default_postlude := ""
432 __dbg_g_automacro := [[__dbg_c_compile (__dbg_default_prelude)],
433 []]
434 cmd := []
435 }
436
437 # Command processing.
438 repeat {
439 case __dbg_c_interp (cmd) of {
440 RESUME_STATUS: break
441 STOP_STATUS: {
442 __dbg_fremove (__dbg_g_display)
443 __dbg_io_note ("Debug session terminates.")
444 __dbg_fexit (0)
445 }
446 }
447 # Get input until it compiles OK.
448 repeat {
449 (*__dbg_g_src > 0) | __dbg_fwrites ("$ ")
450 if cmd := [__dbg_c_compile (__dbg_io_getline ())] then
451 break
452 }
453 }
454 # Run the postlude, if any; status discarded.
455 __dbg_c_interp (__dbg_g_automacro[2])
456 &trace := __dbg_g_trace
457 end
458
459 #
460 #-------- Command processing procedures --------
461 #
462
463 procedure __dbg_c_compile (str, macro_def)
464 # Compiles a command.
465 # 'str' must be a command to compile (string).
466 # 'macro_def' must be non-null to indicate a macro is being defined.
467 # RETURNS a command instance (list), or
468 # FAILS on conflict.
469 local cmd, keywd
470 str ? {
471 __dbg_ftab (__dbg_fmany (__dbg_g_white))
472 keywd := __dbg_ftab (__dbg_fupto (__dbg_g_white) | 0)
473 if *keywd = 0 then # empty line treated as comment
474 return [__dbg_cx_NOOP, COMMENT_CMD]
475 __dbg_ftab (__dbg_fmany (__dbg_g_white))
476 (cmd := __dbg_c_findcmd (keywd)) | fail
477 return cmd[CMD_COMPILE] (cmd, macro_def)
478 }
479 end
480
481 procedure __dbg_c_brkpt (not_zero)
482 # Extracts a breakpoint id from a command.
483 # A breakpoint id is either an integer, or one of the special forms
484 # '.' (current), '$' (last defined).
485 # 'not_zero' may be non-null to indicate that breakpoint number zero
486 # is not accepted.
487 # RETURNS a breakpoint identifier (integer) on success;
488 # FAILS with a suitable conflict message otherwise.
489 local id, res
490 __dbg_ftab (__dbg_fmany (__dbg_g_white))
491 (res := (__dbg_finteger (__dbg_ftab (__dbg_fmany (&digits))) |
492 2(id := =".", BRKP_CURRENT) |
493 2(id := ="$", BRKP_LAST))) | {
494 __dbg_io_cfl ("Breakpoint id (integer, '.', '$') expected.")
495 fail
496 }
497 (res > 0) | /not_zero | {
498 __dbg_io_cfl ("Breakpoint number 0 not accepted here.")
499 fail
500 }
501 return res
502 end
503
504 procedure __dbg_c_interp (clist)
505 # Command interpreter.
506 # 'clist' must be a list of command instances.
507 # The interpreter may call itself indirectly through commands.
508 # RETURNS a status code, or
509 # FAILS on conflict, abandoning its command list.
510 local cmd, code
511 every cmd := !clist do {
512 (code := cmd[1]!cmd) | fail
513 (code = OK_STATUS) | return code
514 }
515 return OK_STATUS
516 end
517
518 procedure __dbg_c_findcmd (keywd)
519 # Finds a command descriptor given a keyword.
520 # 'keywd' must be a command keyword candidate, possibly abbreviated (string).
521 # RETURNS a command definition, or
522 # FAILS with a message on conflict.
523 local count, cmd, mstr, sep, try
524 count := 0
525 sep := mstr := ""
526 every __dbg_fmatch (keywd, (try := !__dbg_g_cmd)[CMD_NAME], 1, 0) do {
527 cmd := try
528 count +:= 1
529 mstr ||:= sep || cmd[CMD_NAME]
530 sep := ", "
531 }
532 case count of {
533 0: {
534 __dbg_io_cfl ("%1: unrecognized command.", keywd)
535 fail
536 }
537 1: return cmd
538 default : {
539 __dbg_io_cfl ("'%1': ambiguous (matches %2).", keywd, mstr)
540 fail
541 }
542 }
543 end
544
545 procedure __dbg_c_findkey (keywd, keylist)
546 # Finds a command descriptor given a keyword.
547 # 'keywd' must be a keyword candidate, possibly abbreviated (string).
548 # 'keylist' must be a list of available keywords.
549 # RETURNS an integer index into 'keylist', or
550 # FAILS with a message on conflict.
551 local count, cmd, idx, mstr, sep
552 count := 0
553 sep := mstr := ""
554 every __dbg_fmatch (keywd, keylist[idx := 1 to *keylist], 1, 0) do {
555 count +:= 1
556 mstr ||:= sep || keylist[cmd := idx]
557 sep := ", "
558 }
559 case count of {
560 0: {
561 __dbg_io_cfl ("%1: unrecognized keyword.", keywd)
562 fail
563 }
564 1: return cmd
565 default : {
566 __dbg_io_cfl ("'%1': ambiguous (matches %2).", keywd, mstr)
567 fail
568 }
569 }
570 end
571
572 procedure __dbg_c_mcompile (fname)
573 # Compiles a macro.
574 # 'fname' must contain a file name (string) if the macro definition should
575 # be read from a file; otherwise null.
576 # If 'fname' is defined and can be opened, a null value is pushed on the file
577 # stack before the file, as a mark.
578 # RETURNS a macro, i.e. a list of compiled commands -- on success.
579 # FAILS if a conflict arises during the macro definition.
580 local cfl_count, cmd, f, line, macro
581 cfl_count := 0
582 macro := []
583 if \fname then {
584 if f := __dbg_x_openfile (fname) then {
585 __dbg_fpush (__dbg_g_src, __dbg_g_in)
586 __dbg_fpush (__dbg_g_src, &null)
587 __dbg_g_in := f
588 }
589 else
590 fail
591 }
592 repeat {
593 (*__dbg_g_src > 0) | __dbg_fwrites ("> ")
594 (line := __dbg_io_getline ()) | break
595 if cmd := __dbg_c_compile (line, 1) then {
596 if cmd[CMD_CODE] = END_CMD then
597 break
598 else
599 __dbg_fput (macro, cmd)
600 }
601 else
602 cfl_count +:= 1
603 (cfl_count < 30) | break
604 }
605 /__dbg_g_in := __dbg_fpop (__dbg_g_src)
606 if cfl_count = 0 then
607 return macro
608 else {
609 __dbg_io_note ("The definition did not take effect.")
610 fail
611 }
612 end
613
614 procedure __dbg_c_msource ()
615 # Checks if the source of a macro is a file.
616 # RETURNS a file name if there is a '<' followed by a file name.
617 # RETURNS null if there is nothing but white space.
618 # FAILS with a message on conflict.
619 local fname
620 __dbg_ftab (__dbg_fmany (__dbg_g_white))
621 if ="<" then {
622 __dbg_ftab (__dbg_fmany (__dbg_g_white))
623 if __dbg_fpos (0) then {
624 __dbg_io_cfl ("File name expected.")
625 fail
626 }
627 fname := __dbg_ftrim (__dbg_ftab (0))
628 }
629 return fname
630 end
631
632 procedure __dbg_x_brkpt (id)
633 # RETURNS a breakpoint descriptor, given a breakpoint id ('id', integer).
634 # FAILS with a diagnostic message on conflict.
635 local bdescr
636 bdescr := case id of {
637 BRKP_CURRENT: \__dbg_g_where[WHERE_BRKP] |
638 (__dbg_io_cfl ("No current breakpoint."), &null)
639 BRKP_LAST: \__dbg_g_brkpdef[__dbg_g_brkpid] |
640 (__dbg_io_cfl ("Breakpoint [%1] undefined.", __dbg_g_brkpid),
641 &null)
642 default: \__dbg_g_brkpdef[id] |
643 (__dbg_io_cfl ("Breakpoint [%1] undefined.", id), &null)
644 }
645 return \bdescr
646 end
647
648 procedure __dbg_x_dispglob (f, pat)
649 # Essentially performs the 'info globals' command.
650 # 'f' must be a display file open for input.
651 # 'pat' must be a substring that variable names must contain.
652 local fchanged, line, word
653 static func
654 initial {
655 func := set ()
656 # A set containing all function names.
657 every insert (func, function ())
658 }
659 fchanged := []
660 until __dbg_fread (f) == "global identifiers:"
661 repeat {
662 (line := __dbg_fread (f)) | break
663 word := []
664 line ? repeat {
665 __dbg_ftab (__dbg_fmany (__dbg_g_white))
666 if __dbg_fpos (0) then
667 break
668 __dbg_fput (word, __dbg_ftab (__dbg_fupto (__dbg_g_white) | 0))
669 }
670 __dbg_fmatch (DBG_PREFIX, word[1]) | (word[1] == word[-1]) |
671 if __dbg_ffind (pat, word[1]) then
672 __dbg_io_info ("%1", word[1])
673
674 # Check if function name has been used for other things.
675 if member (func, word[1]) then {
676 (word[-2] == "function" & word[-1] == word[1]) |
677 put (fchanged, word[1])
678 }
679 }
680 if *fchanged > 0 then {
681 __dbg_io_note ("The following global(s) no longer hold their usual Icon functions:")
682 every __dbg_io_wrline (" " || !fchanged)
683 }
684 end
685
686 procedure __dbg_x_dispinit (f)
687 # Reads the display file, skipping over lines caused by the debugger.
688 # 'f' must be the display file, open for input.
689 # RETURNS the first 'significant' line.
690 # NOTE that you must take care of the 'co-expression' line before calling
691 # this procedure.
692 local line
693 until __dbg_fmatch (DBG_PREFIX, line := __dbg_fread (f))
694 while line[1] == " " | __dbg_fmatch (DBG_PREFIX, line) do
695 line := __dbg_fread (f)
696 return line
697 end
698
699 procedure __dbg_x_lbreak (bdescr)
700 # Lists the nominal definition of a breakpoint.
701 # 'bdescr' may be a breakpoint descriptor, or null.
702 # If null all breakpoints are listed.
703 local bd, blist, cond, dodef, tmplist
704 (blist := [\bdescr]) | {
705 tmplist := __dbg_fsort (__dbg_g_brkpdef)
706 blist := []
707 every __dbg_fput (blist, (!tmplist)[2])
708 }
709 every bd := !blist do {
710 dodef := if \bd[BRKP_DO] then " DO defined" else ""
711 __dbg_io_info ("[%1] %2 %3:%4%5", bd[BRKP_ID], bd[BRKP_FILE],
712 bd[BRKP_LINE1], bd[BRKP_LINE2], dodef)
713 if cond := \bd[BRKP_COND] then
714 __dbg_io_info (" CONDITION: %1", cond[2])
715 }
716 end
717
718 procedure __dbg_x_openfile (fname, output, quiet)
719 # Opens a file.
720 # 'fname' must be the name of the file to open.
721 # 'output' must be non-null if the file is to be opened for output.
722 # 'quiet' must be non-null to prevent a conflict from generating a message.
723 # RETURNS an open file on success;
724 # FAILS with a message otherwise, unless 'quiet' is set.
725 # FAILS silently if 'quiet' is set.
726 local f, mode, modestr
727 if \output then {
728 mode := "w"
729 modestr := "output"
730 }
731 else {
732 mode := "r"
733 modestr := "input"
734 }
735 (f := open (fname, mode)) | (\quiet & fail) |
736 __dbg_io_cfl ("Cannot open '%1' for %2.", fname, modestr)
737 return \f
738 end
739
740 procedure __dbg_x_opendisplay ()
741 # Opens the display file for reading; writes it first, if necessary.
742 # RETURNS a file open for input on success.
743 # FAILS with a message on conflict.
744 local f, res
745 if f := __dbg_x_openfile (__dbg_g_display,, 1) then {
746 if __dbg_finteger (__dbg_fread (f)) = __dbg_g_brkpcnt then
747 res := f
748 else
749 __dbg_fclose (f)
750 }
751 \res | {
752 (f := __dbg_x_openfile (__dbg_g_display, 1)) | fail
753 __dbg_fwrite (f, __dbg_g_brkpcnt)
754 display (, f)
755 __dbg_fclose (f)
756 (f := __dbg_x_openfile (__dbg_g_display)) | fail
757 __dbg_fread (f) # Throw away breakpoint counter.
758 res := f
759 }
760 return res
761 end
762
763 #-------- Command compilation procedures --------
764 # 'macro_def' must be non-null to indicate that a macro is being defined.
765 # The command compilation procedures must return a list representing the
766 # compiled command, or fail on conflict.
767 # When they are invoked the keyword and any following white space has been
768 # parsed.
769
770
771 procedure __dbg_cc_break (cmd, macro_def)
772 local fidx, fname, line1, line2
773 __dbg_fany (&digits) | (fname := __dbg_ftab (__dbg_fupto (__dbg_g_white))) | {
774 __dbg_io_cfl ("File name and/or line number expected.")
775 fail
776 }
777
778 # Get file name.
779 if \fname then {
780 (fidx := \__dbg_g_fileidx[fname]) | {
781 __dbg_io_cfl ("File name '%1' not recognized.", fname)
782 fail
783 }
784 }
785 else if fname := \__dbg_g_where[WHERE_FILE] then
786 fidx := __dbg_g_fileidx[fname]
787 else { # init mode
788 __dbg_io_cfl ("File name required.")
789 fail
790 }
791
792 # Get line number(s).
793 __dbg_ftab (__dbg_fmany (__dbg_g_white))
794 (line1 := __dbg_finteger (__dbg_ftab (__dbg_fmany (&digits)))) | {
795 __dbg_io_cfl ("Line number expected.")
796 fail
797 }
798 __dbg_ftab (__dbg_fmany (__dbg_g_white))
799 if =":" then {
800 __dbg_ftab (__dbg_fmany (__dbg_g_white))
801 (line2 := __dbg_finteger (__dbg_ftab (__dbg_fmany (&digits)))) | {
802 __dbg_io_cfl ("Line number expected.")
803 fail
804 }
805 }
806 else
807 line2 := line1
808 (line1 <= line2 < 1000000) | {
809 __dbg_io_cfl ("Weird line number.")
810 fail
811 }
812
813 # Create an almost finished breakpoint descriptor (id is missing).
814 return [cmd[CMD_EXEC], cmd[CMD_CODE], [ , fname, fidx, line1, line2, 0, ,]]
815 end
816
817 procedure __dbg_cc_clear (cmd, macro_def)
818 # A compound command.
819 local keyidx, parm
820 static ckey
821 initial ckey := ["breakpoint", "condition", "do", "echo", "macro"]
822 (keyidx := __dbg_c_findkey (__dbg_ftab (__dbg_fupto (__dbg_g_white) | 0), ckey)) |
823 fail
824 __dbg_ftab (__dbg_fmany (__dbg_g_white))
825 case keyidx of {
826 CLEAR_BREAKPOINT:
827 (parm := __dbg_c_brkpt (1)) | fail
828 (CLEAR_COND | CLEAR_DO):
829 (parm := __dbg_c_brkpt ()) | fail
830 CLEAR_MACRO:
831 (parm := __dbg_e_idf ()) | {
832 __dbg_io_cfl ("Macro name expected.")
833 fail
834 }
835 }
836 return [cmd[CMD_EXEC], cmd[CMD_CODE], ckey, keyidx, parm]
837 end
838
839 procedure __dbg_cc_condition (cmd, macro_def)
840 local brkpt, expr
841 (brkpt := __dbg_c_brkpt ()) | fail
842 # This makes the expression cleaner, but not necessary.
843 __dbg_ftab (__dbg_fmany (__dbg_g_white))
844 (expr := __dbg_e_compile (__dbg_ftab (0))) | {
845 __dbg_io_cfl (__dbg_ge_message)
846 fail
847 }
848 (*expr = 1) | __dbg_io_note ("Last %1 expressions ignored.", *expr - 1)
849 return [cmd[CMD_EXEC], cmd[CMD_CODE], brkpt, expr[1]]
850 end
851
852 procedure __dbg_cc_do (cmd, macro_def)
853 local brkpt, fname
854 /macro_def | {
855 __dbg_io_cfl ("Sorry, nested macros not accepted.")
856 fail
857 }
858 (brkpt := __dbg_c_brkpt ()) | fail
859 (fname := __dbg_c_msource ()) | fail
860 return [cmd[CMD_EXEC], cmd[CMD_CODE], brkpt, fname]
861 end
862
863 procedure __dbg_cc_end (cmd, macro_def)
864 \macro_def | {
865 __dbg_io_cfl ("'end' out of context.")
866 fail
867 }
868 return [cmd[CMD_EXEC], cmd[CMD_CODE]]
869 end
870
871 procedure __dbg_cc_eprint (cmd, macro_def)
872 local expr
873 (expr := __dbg_e_compile (__dbg_ftab (0))) | {
874 __dbg_io_cfl (__dbg_ge_message)
875 fail
876 }
877 (*expr = 1) | __dbg_io_note ("Last %1 expressions ignored.", *expr - 1)
878 return [cmd[CMD_EXEC], cmd[CMD_CODE], expr[1]]
879 end
880
881 procedure __dbg_cc_frame (cmd, macro_def)
882 local frame_no
883 __dbg_fpos (0) | (frame_no := __dbg_finteger (__dbg_ftab (__dbg_fmany (&digits ++ '-')))) | {
884 __dbg_io_cfl ("Frame number expected.")
885 fail
886 }
887 return [cmd[CMD_EXEC], cmd[CMD_CODE], frame_no]
888 end
889
890 procedure __dbg_cc_goon (cmd, macro_def)
891 local opt
892 __dbg_fpos (0) | __dbg_fmatch (opt := __dbg_ftab (__dbg_fmany (&lcase)), "nobreak", 1, 0) | {
893 __dbg_io_cfl ("Expected 'nobreak', found '%1'.", opt)
894 fail
895 }
896 return [cmd[CMD_EXEC], cmd[CMD_CODE], opt]
897 end
898
899 procedure __dbg_cc_help (cmd, macro_def)
900 local keywd
901 __dbg_fpos (0) | (keywd := __dbg_ftab (__dbg_fmany (&lcase))) | {
902 __dbg_io_cfl ("Command keyword expected.")
903 fail
904 }
905 return [cmd[CMD_EXEC], cmd[CMD_CODE], keywd]
906 end
907
908 procedure __dbg_cc_ignore (cmd, macro_def)
909 local brkpt, count
910 (brkpt := __dbg_c_brkpt ()) | fail
911 __dbg_ftab (__dbg_fmany (__dbg_g_white))
912 (count := __dbg_finteger (__dbg_ftab (__dbg_fmany (&digits ++ '+-')))) | {
913 __dbg_io_cfl ("Integer ignore count expected.")
914 fail
915 }
916 return [cmd[CMD_EXEC], cmd[CMD_CODE], brkpt, count]
917 end
918
919 procedure __dbg_cc_info (cmd, macro_def)
920 # A compound command.
921 local keyidx, parm
922 static ckey
923 initial ckey := ["breakpoint", "echo", "files", "globals", "locals", "macros",
924 "trace", "version"]
925 (keyidx := __dbg_c_findkey (__dbg_ftab (__dbg_fupto (__dbg_g_white) | 0), ckey)) |
926 fail
927 __dbg_ftab (__dbg_fmany (__dbg_g_white))
928 if keyidx = INFO_BREAKPOINT then
929 __dbg_fpos (0) | (parm := __dbg_c_brkpt ()) | fail
930 else if keyidx = INFO_GLOBALS then
931 __dbg_fpos (0) | (parm := __dbg_ftab (__dbg_fupto (__dbg_g_white) | 0))
932 return [cmd[CMD_EXEC], cmd[CMD_CODE], ckey, keyidx, parm]
933 end
934
935 procedure __dbg_cc_macro (cmd, macro_def)
936 local fname, idf
937 /macro_def | {
938 __dbg_io_cfl ("Sorry, nested macros not accepted.")
939 fail
940 }
941 (idf := __dbg_ftab (__dbg_fmany (&lcase))) | {
942 __dbg_io_cfl ("Macro name expected.")
943 fail
944 }
945 (fname := __dbg_c_msource ()) | fail
946 return [cmd[CMD_EXEC], cmd[CMD_CODE], idf, fname]
947 end
948
949 procedure __dbg_cc_next (cmd, macro_def)
950 local count
951 __dbg_ftab (__dbg_fmany (__dbg_g_white))
952 __dbg_fpos (0) | (count := __dbg_finteger (__dbg_ftab (__dbg_fmany (&digits ++ '+-')))) | {
953 __dbg_io_cfl ("Integer ignore count expected.")
954 fail
955 }
956 return [cmd[CMD_EXEC], cmd[CMD_CODE], count]
957 end
958
959 procedure __dbg_cc_print (cmd, macro_def)
960 # Used to compile 'fprint' and 'print'.
961 local expr
962 (expr := __dbg_e_compile (__dbg_ftab (0))) | {
963 __dbg_io_cfl (__dbg_ge_message)
964 fail
965 }
966 return [cmd[CMD_EXEC], cmd[CMD_CODE], expr]
967 end
968
969 procedure __dbg_cc_set (cmd, macro_def)
970 # A compound command.
971 local keyidx, parm
972 static ckey
973 initial ckey := ["echo", "prelude", "postlude"]
974 (keyidx := __dbg_c_findkey (__dbg_ftab (__dbg_fupto (__dbg_g_white) | 0), ckey)) |
975 fail
976 __dbg_ftab (__dbg_fmany (__dbg_g_white))
977 case keyidx of {
978 SET_ECHO: {
979 parm := __dbg_ftrim (__dbg_ftab (__dbg_fupto (__dbg_g_white) | 0))
980 (*parm > 0) | {
981 __dbg_io_cfl ("File name expected.")
982 fail
983 }
984 }
985 (SET_PRELUDE | SET_POSTLUDE):
986 (parm := __dbg_c_msource ()) | fail
987 }
988 return [cmd[CMD_EXEC], cmd[CMD_CODE], ckey, keyidx, parm]
989 end
990
991 procedure __dbg_cc_source (cmd, macro_def)
992 # The 'source' command is different from other commands, because it is not
993 # really compiled; it takes effect immediately.
994 # In contrast to macro compilation, no null marker is pushed on the file stack.
995 # RETURNS a dummy 'source' command.
996 local f, fname, res
997 __dbg_ftab (__dbg_fmany (__dbg_g_white))
998 if __dbg_fpos (0) then
999 __dbg_io_cfl ("File name expected.")
1000 else {
1001 fname := __dbg_ftrim (__dbg_ftab (0))
1002 if *__dbg_g_src >= MAX_SOURCE_NESTING then
1003 __dbg_io_cfl ("%1: Too deeply nested 'source' file.", fname)
1004 else if f := __dbg_x_openfile (fname) then {
1005 __dbg_fpush (__dbg_g_src, __dbg_g_in)
1006 __dbg_g_in := f
1007 res := [cmd[CMD_EXEC], cmd[CMD_CODE], fname]
1008 }
1009 }
1010 return \res
1011 end
1012
1013 procedure __dbg_cc_trace (cmd, macro_def)
1014 local tlevel
1015 (tlevel := __dbg_finteger (__dbg_ftab (__dbg_fmany (&digits ++ '+-')))) | {
1016 __dbg_io_cfl ("Integer value expected.")
1017 fail
1018 }
1019 return [cmd[CMD_EXEC], cmd[CMD_CODE], \tlevel]
1020 end
1021
1022 procedure __dbg_cc_SIMPLE (cmd, macro_def)
1023 # Used to compile all keyword-only commands, including macros.
1024 return [cmd[CMD_EXEC], cmd[CMD_CODE], cmd[CMD_MACRO]]
1025 end
1026
1027 #-------- Command executing procedures --------
1028 # The first parameter of these procedures is the procedure itself.
1029 # (Not a very interesting parameter.)
1030 # The command executing procedures must return a return code on success.
1031 # Return codes are defined among the symbolic constants.
1032 # The procedures must fail on conflict.
1033
1034
1035 procedure __dbg_cx_break (proced, ccode, brkp)
1036 local id, bpset, fidx, line1, line2
1037 # Add the breakpoint id to the descriptor.
1038 brkp[BRKP_ID] := id := (__dbg_g_brkpid +:= 1)
1039 __dbg_io_wrline ("[" || id || "]")
1040 # Make sure we can find the breakpint descriptor, given its id.
1041 __dbg_g_brkpdef[id] := brkp
1042 # Install the breakpoint lines in the lookup table.
1043 fidx := brkp[BRKP_FIDX]
1044 line1 := brkp[BRKP_LINE1]
1045 line2 := brkp[BRKP_LINE2]
1046 every __dbg_g_brlookup[__dbg_fior (fidx, line1 to line2)] := brkp
1047 # Add the line numbers to the breakpoint set.
1048 bpset := __dbg_file_map[brkp[BRKP_FILE]]
1049 every __dbg_finsert (bpset, line1 to line2)
1050 return OK_STATUS
1051 end
1052
1053 procedure __dbg_cx_clear (proced, ccode, ckey, keyidx, parm)
1054 # 'ckey' will be a list containing all the possible keywords to 'clear'.
1055 # 'keyidx' is an index into that list, indicating a subcommand.
1056 local bdescr, bpset, cmd, fidx, lcode, line, line1, line2
1057 if keyidx = (CLEAR_BREAKPOINT | CLEAR_COND | CLEAR_DO) then
1058 (bdescr := __dbg_x_brkpt (parm)) | fail
1059 else if keyidx = CLEAR_MACRO then
1060 (cmd := __dbg_c_findcmd (parm)) | fail
1061 case keyidx of {
1062 CLEAR_BREAKPOINT: {
1063 __dbg_fdelete (__dbg_g_brkpdef, bdescr[BRKP_ID])
1064 fidx := bdescr[BRKP_FIDX]
1065 line1 := bdescr[BRKP_LINE1]
1066 line2 := bdescr[BRKP_LINE2]
1067 bpset := __dbg_file_map[bdescr[BRKP_FILE]]
1068 # The range of lines once defined for the breakpoint might
1069 # have been overwritten by later breakpoints.
1070 every lcode := __dbg_fior (fidx, line := line1 to line2) do {
1071 if __dbg_g_brlookup[lcode] === bdescr then {
1072 __dbg_fdelete (__dbg_g_brlookup, lcode)
1073 __dbg_fdelete (bpset, line)
1074 }
1075 }
1076 }
1077 CLEAR_COND: bdescr[BRKP_COND] := &null
1078 CLEAR_DO: bdescr[BRKP_DO] := &null
1079 CLEAR_ECHO: {
1080 __dbg_fclose (\__dbg_g_out2)
1081 __dbg_g_out2 := &null
1082 }
1083 CLEAR_MACRO: {
1084 (cmd := __dbg_c_findcmd (parm)) | fail
1085 __dbg_fdelete (__dbg_g_cmd, cmd[CMD_NAME])
1086 }
1087 }
1088 return OK_STATUS
1089 end
1090
1091 procedure __dbg_cx_condition (proced, ccode, brkpt, expr)
1092 local bdescr
1093 (bdescr := __dbg_x_brkpt (brkpt)) | fail
1094 bdescr[BRKP_COND] := expr
1095 return OK_STATUS
1096 end
1097
1098 procedure __dbg_cx_do (proced, ccode, brkpt, fname)
1099 local bdescr
1100 (bdescr := __dbg_x_brkpt (brkpt)) | fail
1101 (bdescr[BRKP_DO] := __dbg_c_mcompile (fname)) | fail
1102 return OK_STATUS
1103 end
1104
1105 procedure __dbg_cx_eprint (proced, ccode, expr)
1106 local count, val
1107 __dbg_io_wrline ("{" || expr[2] || "}")
1108 count := 0
1109 every val := __dbg_fimage (__dbg_e_eval (expr[1])) do {
1110 if __dbg_io_cfl (\__dbg_ge_message) then
1111 fail
1112 else
1113 __dbg_io_wrline ("" || __dbg_fright ((count +:= 1), 3) ||
1114 ": " || val)
1115 }
1116 return OK_STATUS
1117 end
1118
1119 procedure __dbg_cx_fprint (proced, ccode, elist)
1120 # 'elist' must be a list on the format returned by '__dbg_e_compile'.
1121 local expr, fmt, idx, sval, val
1122 val := []
1123 every expr := !elist do {
1124 __dbg_fput (val, __dbg_e_eval (expr[1]) | "&fail")
1125 if __dbg_io_cfl (\__dbg_ge_message) then
1126 fail
1127 }
1128 (fmt := __dbg_fstring (val[1])) | {
1129 __dbg_io_cfl ("Expected format string; got '%1'.", __dbg_fimage (val[1]))
1130 fail
1131 }
1132 sval := []
1133 every idx := 2 to *val do {
1134 __dbg_fput (sval, __dbg_fstring (val[idx])) | {
1135 __dbg_io_cfl ("Expression not string-convertible: {%1} %2",
1136 elist[idx][2], __dbg_fimage (val[idx]))
1137 fail
1138 }
1139 }
1140 __dbg_io_wrstr (__dbg_x_subst (fmt, sval))
1141 return OK_STATUS
1142 end
1143
1144 procedure __dbg_cx_frame (proced, ccode, frame_spec)
1145 local f, frame_no, idx, line
1146 frame_no := if \frame_spec then {
1147 if frame_spec < 0 then __dbg_g_level + frame_spec else frame_spec
1148 } else __dbg_g_level
1149 (1 <= frame_no <= __dbg_g_level) | {
1150 __dbg_io_cfl ("Invalid frame number.")
1151 fail
1152 }
1153 (f := __dbg_x_opendisplay ()) | fail
1154 line := __dbg_x_dispinit (f)
1155 idx := __dbg_g_level
1156 while idx > frame_no do {
1157 repeat if (line := __dbg_fread (f))[1] ~== " " then
1158 break
1159 idx -:= 1
1160 }
1161 __dbg_io_info ("(%1) %2", frame_no, line)
1162 repeat {
1163 if (line := __dbg_fread (f))[1] ~== " " then
1164 break
1165 line ? {
1166 __dbg_ftab (__dbg_fmany (__dbg_g_white))
1167 =DBG_PREFIX | __dbg_io_info ("%1", line, *line > 0)
1168 }
1169 }
1170 __dbg_fclose (f)
1171 return OK_STATUS
1172 end
1173
1174 procedure __dbg_cx_goon (proced, ccode, nobreak)
1175 if \nobreak then {
1176 __dbg_test := integer
1177 __dbg_fremove (__dbg_g_display)
1178 }
1179 return RESUME_STATUS
1180 end
1181
1182 procedure __dbg_cx_help (proced, ccode, keywd)
1183 # 'keywd' will be an identifier if the command had a keyword.
1184 local cmd, hstr
1185 if cmd := __dbg_c_findcmd (\keywd) then {
1186 if hstr := \cmd[CMD_HELP] then
1187 __dbg_io_wrline (hstr)
1188 else
1189 __dbg_io_note ("No help available for '%1'.", cmd[CMD_NAME])
1190 }
1191 else
1192 __dbg_io_wrline ("Available commands: (all keywords may be abbreviated)\n_
1193 break (set breakpoint)\n_
1194 clear (clear breakpoint or debugger parameter)\n_
1195 condition (attach condition to breakpoint)\n_
1196 do (attach macro to breakpoint)\n_
1197 end (terminate macro definition)\n_
1198 eprint (print every value from expression)\n_
1199 fprint (formatted print)\n_
1200 frame (inspect procedure call chain)\n_
1201 goon (resume execution)\n_
1202 help (print explanatory text)\n_
1203 ignore (set ignore counter on breakpoint)\n_
1204 info (print information about breakpoint or debugger parameter)\n_
1205 macro (define new command)\n_
1206 next (resume execution, break on every line)\n_
1207 print (print expressions)\n_
1208 set (set a debugger parameter)\n_
1209 source (read debugging commands from file)\n_
1210 stop (terminate program and debugging session)\n_
1211 trace (set value of Icon &trace)\n_
1212 where (print procedure call chain)\n\n_
1213 An expression may be formed from a large subset of Icon operators; integer,\n_
1214 string, list literals; locals from the current procedure, and globals.\n_
1215 Procedure/function invocation, subscripting, record field reference is\n_
1216 supported. Several keywords are also included.\n\n_
1217 New/altered keywords,\n_
1218 \ &bp, &breakpoint current breakpoint id (integer)\n_
1219 \ &file current breakpoint source file name (string)\n_
1220 \ &line current breakpoint line number (integer)\n_
1221 \ &proc current breakpoint procedure name (string)")
1222 return OK_STATUS
1223 end
1224
1225 procedure __dbg_cx_ignore (proced, ccode, brkpt, count)
1226 local bdescr
1227 (bdescr := __dbg_x_brkpt (brkpt)) | fail
1228 bdescr[BRKP_IGNORE] := count
1229 return OK_STATUS
1230 end
1231
1232 procedure __dbg_cx_info (proced, ccode, ckey, keyidx, parm)
1233 # 'ckey' will be a list containing all the possible keywords to 'info'.
1234 # 'keyidx' is an index into that list, indicating a subcommand.
1235 local cmd, bdescr, f, nlist, version
1236 case keyidx of {
1237 INFO_BREAKPOINT:
1238 if \parm then {
1239 (bdescr := __dbg_x_brkpt (parm)) | fail
1240 __dbg_x_lbreak (bdescr)
1241 }
1242 else
1243 __dbg_x_lbreak ()
1244 INFO_ECHO:
1245 if \__dbg_g_out2 then
1246 __dbg_io_info ("Echo file: %1.", __dbg_g_out2name)
1247 else
1248 __dbg_io_info ("No echo file.")
1249 INFO_FILES: {
1250 nlist := []
1251 every __dbg_fput (nlist, __dbg_fkey (__dbg_file_map))
1252 nlist := __dbg_fsort (nlist)
1253 __dbg_io_info ("Tweaked source files in this program:")
1254 every __dbg_io_info (" %1", !nlist)
1255 }
1256 INFO_GLOBALS: {
1257 (f := __dbg_x_opendisplay ()) | fail
1258 if \parm then
1259 __dbg_x_dispglob (f, parm)
1260 else
1261 __dbg_x_dispglob (f, "")
1262 __dbg_fclose (f)
1263 }
1264 INFO_LOCALS: {
1265 nlist := []
1266 every __dbg_fput (nlist, __dbg_fkey (__dbg_g_local))
1267 nlist := __dbg_fsort (nlist)
1268 __dbg_io_info ("Local identifiers in the current procedure:",
1269 *nlist > 0)
1270 every __dbg_io_info (" %1", !nlist)
1271 }
1272 INFO_MACROS: {
1273 nlist := []
1274 every \(cmd := !__dbg_g_cmd)[CMD_MACRO] do
1275 __dbg_fput (nlist, cmd[CMD_NAME])
1276 nlist := __dbg_fsort (nlist)
1277 __dbg_io_info ("Currently defined macros:", *nlist > 0)
1278 every __dbg_io_info (" %1", !nlist)
1279 }
1280 INFO_TRACE:
1281 __dbg_io_info ("Current trace level: %1.", __dbg_g_trace)
1282 INFO_VERSION: {
1283 version := (PROGRAM_VERSION ? (__dbg_ftab (__dbg_fupto (&digits)),
1284 __dbg_ftab (__dbg_fmany (&digits++'.'))))
1285 __dbg_io_info ("Program tweaked by itweak version %1.\n_
1286 This is runtime version %2.", __dbg_itweak_ver, version)
1287 }
1288 }
1289 return OK_STATUS
1290 end
1291
1292 procedure __dbg_cx_macro (proced, ccode, idf, fname)
1293 # Executes a 'macro' statement (not the resulting macro).
1294 # 'fname' contains a file name (string) if the macro definition should be
1295 # read from a file; otherwise null.
1296 # SIDE EFFECT: Adds a command definition to '__dbg_g_cmd' on success.
1297 local count, macro, mstr, sep, try
1298 count := 0
1299 mlist := []
1300 # Macro name must not be an abbreviation of an existing command.
1301 every __dbg_fmatch (idf, try := (!__dbg_g_cmd)[CMD_NAME], 1, 0) do {
1302 count +:= 1
1303 __dbg_fput (mlist, try)
1304 }
1305 # Check that no existing command is an abbreviation of macro name.
1306 every __dbg_fmatch (try := (!__dbg_g_cmd)[CMD_NAME], idf, 1, 0) do {
1307 count +:= 1
1308 (try == !mlist) | __dbg_fput (mlist, try)
1309 }
1310 (count = 0) | {
1311 mstr := sep := ""
1312 every mstr ||:= sep || !mlist do
1313 sep := ", "
1314 __dbg_io_cfl ("'%1' clashes with existing command (%2).", idf, mstr)
1315 fail
1316 }
1317 (macro := __dbg_c_mcompile (fname)) | fail
1318 __dbg_g_cmd[idf] := [idf, USERDEF_CMD, , __dbg_cc_SIMPLE, macro, __dbg_cx_userdef]
1319 return OK_STATUS
1320 end
1321
1322 procedure __dbg_cx_next (proced, ccode, count)
1323 # 'count' may be an ignore count.
1324 __dbg_g_brkpdef[0][BRKP_IGNORE] := \count
1325 __dbg_test := 2
1326 return RESUME_STATUS
1327 end
1328
1329 procedure __dbg_cx_print (proced, ccode, elist)
1330 # 'elist' must be a list on the format returned by '__dbg_e_compile'.
1331 local expr, val
1332 every expr := !elist do {
1333 val := (__dbg_fimage (__dbg_e_eval (expr[1])) | "&fail")
1334 if __dbg_io_cfl (\__dbg_ge_message) then
1335 fail
1336 else
1337 __dbg_io_wrline ("{" || expr[2] || "} " || val)
1338 }
1339 return OK_STATUS
1340 end
1341
1342 procedure __dbg_cx_set (proced, ccode, ckey, keyidx, parm)
1343 # 'ckey' will be a list containing all the possible keywords to 'set'.
1344 # 'keyidx' is an index into that list, indicating a subcommand.
1345 case keyidx of {
1346 SET_ECHO: {
1347 (__dbg_g_out2 := __dbg_x_openfile (parm, 1)) | fail
1348 __dbg_g_out2name := parm
1349 }
1350 SET_PRELUDE:
1351 (__dbg_g_automacro[1] := __dbg_c_mcompile (parm)) | fail
1352 SET_POSTLUDE:
1353 (__dbg_g_automacro[2] := __dbg_c_mcompile (parm)) | fail
1354 }
1355 return OK_STATUS
1356 end
1357
1358 procedure __dbg_cx_stop (proced, ccode)
1359 return STOP_STATUS
1360 end
1361
1362 procedure __dbg_cx_trace (proced, ccode, tlevel)
1363 __dbg_g_trace := tlevel
1364 return OK_STATUS
1365 end
1366
1367 procedure __dbg_cx_where (proced, ccode)
1368 local f, idf, idx, line
1369 (f := __dbg_x_opendisplay ()) | fail
1370 __dbg_io_info ("Current call stack in %1:", __dbg_fread (f))
1371 idx := __dbg_g_level
1372 line := __dbg_x_dispinit (f)
1373 repeat {
1374 idf := (line ? __dbg_ftab (__dbg_fupto (__dbg_g_white)))
1375 if idf == "global" then
1376 break
1377 if *idf > 0 then {
1378 __dbg_io_info ("(%1) %2", idx, idf)
1379 idx -:= 1
1380 }
1381 (line := __dbg_fread (f)) | break # Sanity.
1382 }
1383 __dbg_fclose (f)
1384 return OK_STATUS
1385 end
1386
1387 procedure __dbg_cx_userdef (proced, ccode, macro)
1388 return __dbg_c_interp (macro)
1389 end
1390
1391 procedure __dbg_cx_NOOP (proced, ccode)
1392 return OK_STATUS
1393 end
1394
1395 #
1396 #-------- General-purpose procedures --------
1397 #
1398
1399 procedure __dbg_x_fld_adj (str)
1400 # Part of 'subst' format string parsing.
1401 # 'str' must be a parameter string identified by the beginning part of a
1402 # placeholder ('%n').
1403 # This procedure checks if the placeholder contains a fixed field width
1404 # specifier.
1405 # A fixed field specifier begins with '<' or '>' and continues with the field
1406 # width expressed as a decimal literal.
1407 # RETURNS 'str' possibly inserted in a fixed width field.
1408 local just, init_p, res, wid
1409 static fwf
1410 initial fwf := '<>'
1411 init_p := &pos
1412 if (just := if ="<" then left else if =">" then right) &
1413 (wid := __dbg_finteger (__dbg_ftab (__dbg_fmany (&digits)))) then
1414 res := just (str, wid)
1415 else {
1416 res := str
1417 &pos := init_p
1418 }
1419 return res
1420 end
1421
1422 procedure __dbg_x_subst (msg, parm)
1423 # Substitutes parameters in a message template.
1424 # 'msg' must be a message template (string).
1425 # 'parm' must be a list of parameters (list of string-convertible), or null.
1426 # It may also be a string.
1427 local esc, res, sub
1428 static p_digit
1429 initial p_digit := '123456789'
1430 \parm | return msg
1431 parm := [__dbg_fstring (parm)]
1432 res := ""
1433 msg ? until __dbg_fpos (0) do {
1434 res ||:= __dbg_ftab (__dbg_fupto ('%\\') | 0)
1435 if ="%" then res ||:= {
1436 if __dbg_fany (p_digit) then {
1437 sub := (\parm[__dbg_finteger (__dbg_fmove (1))] | "")
1438 __dbg_x_fld_adj (sub)
1439 }
1440 else if __dbg_fany ('%') then
1441 __dbg_fmove (1)
1442 else ""
1443 }
1444 else if ="\\" then res ||:= case esc := __dbg_fmove (1) of {
1445 "n": "\n"
1446 "t": "\t"
1447 default: esc
1448 }
1449 }
1450 return res
1451 end
1452
1453 #
1454 #-------- Input/Output procedures --------
1455 #
1456
1457 procedure __dbg_io_cfl (format, parm[])
1458 # Writes a conflict message to debugging output.
1459 # 'format' must be a format string.
1460 # 'parm' must be string-convertibles to insert into placeholders in the
1461 # format string, if any.
1462 # RETURNS 1 (i.e. always succeeds).
1463 __dbg_io_wrline ("[debug CONFLICT] " || __dbg_x_subst (format, parm))
1464 return 1
1465 end
1466
1467 procedure __dbg_io_getline ()
1468 # RETURNS the next line from debugging input, or
1469 # FAILS on end of file.
1470 local line
1471 (line := __dbg_fread (__dbg_g_in)) | {
1472 __dbg_fclose (__dbg_g_in)
1473 # Check for a macro definition marker.
1474 \(__dbg_g_in := __dbg_fpop (__dbg_g_src)) | fail
1475 if *__dbg_g_src > 0 then
1476 return __dbg_io_getline ()
1477 }
1478 __dbg_fwrite (\__dbg_g_out2, "$ ", \line)
1479 return \line
1480 end
1481
1482 procedure __dbg_io_info (format, parm[])
1483 # Writes an info message to debugging output.
1484 # 'format' must be a format string.
1485 # 'parm' must be string-convertibles to insert into placeholders in the
1486 # format string, if any.
1487 __dbg_io_wrline (__dbg_x_subst (format, parm))
1488 end
1489
1490 procedure __dbg_io_note (format, parm[])
1491 # Writes a note to debugging output.
1492 # 'format' must be a format string.
1493 # 'parm' must be string-convertibles to insert into placeholders in the
1494 # format string, if any.
1495 __dbg_io_wrline ("[debug NOTE] " || __dbg_x_subst (format, parm))
1496 end
1497
1498 procedure __dbg_io_wrline (line)
1499 # Writes a string and a newline to debugging output.
1500 # 'line' must be the string to write.
1501 # It may contains additional newlines.
1502 __dbg_fwrite (__dbg_g_out1, line)
1503 __dbg_fwrite (\__dbg_g_out2, line)
1504 end
1505
1506 procedure __dbg_io_wrstr (line)
1507 # Writes a string without a newline to debugging output.
1508 # 'line' must be the string to write.
1509 # It may contains additional newlines.
1510 __dbg_fwrites (__dbg_g_out1, line)
1511 __dbg_fwrites (\__dbg_g_out2, line)
1512 end
1513
1514 #
1515 #-------- Function initialization ---------
1516 #
1517 procedure __dbg_func_init ()
1518 __dbg_fany := any
1519 __dbg_fclose := close
1520 __dbg_fdelete := delete
1521 __dbg_fexit := exit
1522 __dbg_ffind := find
1523 __dbg_fgetenv := getenv
1524 __dbg_fimage := image
1525 __dbg_finsert := insert
1526 __dbg_finteger := integer
1527 __dbg_fior := ior
1528 __dbg_fishift := ishift
1529 __dbg_fkey := key
1530 __dbg_fmany := many
1531 __dbg_fmatch := match
1532 __dbg_fmove := move
1533 __dbg_fpop := pop
1534 __dbg_fpos := pos
1535 __dbg_fproc := proc
1536 __dbg_fpush := push
1537 __dbg_fput := put
1538 __dbg_fread := read
1539 __dbg_fremove := remove
1540 __dbg_freverse := reverse
1541 __dbg_fright := right
1542 __dbg_fsort := sort
1543 __dbg_fstring := string
1544 __dbg_ftab := tab
1545 __dbg_ftable := table
1546 __dbg_ftrim := trim
1547 __dbg_ftype := type
1548 __dbg_fupto := upto
1549 __dbg_fwrite := write
1550 __dbg_fwrites := writes
1551 end
1552
1553 #
1554 #-------- Command initialization ---------
1555 #
1556
1557 procedure __dbg_cmd_init ()
1558 # Initialize command definitions.
1559 __dbg_g_cmd := __dbg_ftable ()
1560 ### break
1561 __dbg_g_cmd["break"] := ["break", BREAK_CMD,
1562 " break [file] [line [: line]]\n_
1563 Sets a breakpoint on a line or a range of lines. The file name (if present)\n_
1564 must be one of the tweaked files (cf. the 'info files' command). If omitted\n_
1565 the file of the current breakpoint is assumed. The identity of the new\n_
1566 breakpoint (an integer) is displayed. It may be used in other commands.\n_
1567 Besides an integer there are two other ways to identify a breakpoint,\n_
1568 \ . (dot) the current breakpoint,\n_
1569 \ $ (dollar) the last breakpoint defined by a 'break' command.\n_
1570 Breakpoint 0 (zero) is special; see the 'next' command.\n\n_
1571 As a rule a breakpoint takes effect AFTER the breakpointed line has been\n_
1572 executed. If two breakpoints are defined on the same line, only the latest\n_
1573 is in effect.",
1574 __dbg_cc_break, , __dbg_cx_break]
1575 ### clear
1576 __dbg_g_cmd["clear"] := ["clear", CLEAR_CMD,
1577 " clear breakpoint brkpt\n_
1578 Deletes breakpoint identified by 'brkpt'.\n_
1579 \ clear condition brkpt\n_
1580 Removes condition from breakpoint 'brkpt'. The breakpoint becomes\n_
1581 unconditional.\n_
1582 \ clear do brkpt\n_
1583 Removes commands associated with breakpoint 'brkpt'.\n_
1584 \ clear echo\n_
1585 Stops output to echo file.\n_
1586 \ clear macro name\n_
1587 Removes macro identified by 'name'.",
1588 __dbg_cc_clear, , __dbg_cx_clear]
1589 ### comment
1590 __dbg_g_cmd["#"] := ["#", COMMENT_CMD,
1591 " # comment text\n_
1592 A line beginning with '#' is ignored.",
1593 __dbg_cc_SIMPLE, , __dbg_cx_NOOP]
1594 ### condition
1595 __dbg_g_cmd["condition"] := ["condition", CONDITION_CMD,
1596 " condition brkpt expr\n_
1597 Attaches a condition to breakpoint 'brkpt'. The expression 'expr' must\n_
1598 succeed for a break to occur.",
1599 __dbg_cc_condition, , __dbg_cx_condition]
1600 ### do
1601 __dbg_g_cmd["do"] := ["do", DO_CMD,
1602 " do brkpt [<filename]\n_
1603 Attaches commands to the breakpoint identified by 'brkpt'. The commands\n_
1604 are entered interactively (terminate with 'end'), or are read from a file.",
1605 __dbg_cc_do, , __dbg_cx_do]
1606 ### end
1607 __dbg_g_cmd["end"] := ["end", END_CMD,
1608 " end\n_
1609 Terminates a macro definition.",
1610 __dbg_cc_end, , __dbg_cx_NOOP]
1611 ### eprint
1612 __dbg_g_cmd["eprint"] := ["eprint", EPRINT_CMD,
1613 " eprint expr\n_
1614 Prints image of every value generated by expression 'expr'.",
1615 __dbg_cc_eprint, , __dbg_cx_eprint]
1616 ### fprint
1617 __dbg_g_cmd["fprint"] := ["fprint", FPRINT_CMD,
1618 " fprint format-expr {; expr}\n_
1619 Formatted print. The first expression must evaluate to a format string,\n_
1620 possibly containing placeholders (%1, %2, etc). The result of evaluating\n_
1621 remaining expressions will be substituted for the placeholders. You must\n_
1622 make sure their values are string-convertible (the 'image' function is\n_
1623 available). Insert '\\n' in format string to obtain newline.",
1624 __dbg_cc_print, , __dbg_cx_fprint]
1625 ### frame
1626 __dbg_g_cmd["frame"] := ["frame", FRAME_CMD,
1627 " frame [n]\n_
1628 Shows a call frame. 'n' may be an integer frame number (obtained from\n_
1629 the 'where' command), or may be omitted. Omitted frame number = current\n_
1630 procedure. Negative frame number is relative to the current procedure.\n_
1631 The command prints the image of all local variables.",
1632 __dbg_cc_frame, , __dbg_cx_frame]
1633 ### goon
1634 __dbg_g_cmd["goon"] := ["goon", GOON_CMD,
1635 " goon [nobreak]\n_
1636 Resumes execution. With 'nobreak': lets the program run to completion\n_
1637 without breaking.",
1638 __dbg_cc_goon, , __dbg_cx_goon]
1639 ### help
1640 __dbg_g_cmd["help"] := ["help", HELP_CMD,
1641 " help [command]\n_
1642 Displays information. Prints short command description if command keyword\n_
1643 is included. Otherwise prints list of available commands.",
1644 __dbg_cc_help, , __dbg_cx_help]
1645 ### ignore
1646 __dbg_g_cmd["ignore"] := ["ignore", IGNORE_CMD,
1647 " ignore brkpt count\n_
1648 Sets the ignore counter of breakpoint 'brkpt'. 'count' may be a positive\n_
1649 or negative integer. It replaces the previous ignore counter value.\n_
1650 A breakpoint with a non-zero ignore count does not cause a break, but the\n_
1651 ignore count is decremented by 1.",
1652 __dbg_cc_ignore, , __dbg_cx_ignore]
1653 ### info
1654 __dbg_g_cmd["info"] := ["info", INFO_CMD,
1655 " info breakpoint [brkpt]\n_
1656 Prints info about breakpoint identified by 'brkpt', or about all\n_
1657 breakpoints if 'brkpt' is omitted.\n_
1658 \ info echo\n_
1659 Prints the current 'echo' file name, if any.\n_
1660 \ info files\n_
1661 Prints names of source files with tweaked ucode in this program.\n_
1662 \ info globals [substr]\n_
1663 Prints names of global variables. The optional substring limits output\n_
1664 to global names containing this substring.\n_
1665 \ info locals\n_
1666 Prints names of all local variables in current procedure.\n_
1667 \ info macros\n_
1668 Prints names of all currently defined macros.\n_
1669 \ info trace\n_
1670 Prints the current value of &trace.\n_
1671 \ info version\n_
1672 Prints itweak and runtime versions.",
1673 __dbg_cc_info, , __dbg_cx_info]
1674 ### macro
1675 __dbg_g_cmd["macro"] := ["macro", MACRO_CMD,
1676 " macro name\n_
1677 Creates a new command called 'name'. The command will consist of\n_
1678 subsequent lines, up to a line containing 'end'.\n_
1679 \ macro name <filename\n_
1680 As above, but macro definition read from a file. 'end' command optional.",
1681 __dbg_cc_macro, , __dbg_cx_macro]
1682 ### next
1683 __dbg_g_cmd["next"] := ["next", NEXT_CMD,
1684 " next [count]\n_
1685 Resumes execution as if a breakpoint were defined on every line. An\n_
1686 ignore count may be included (see the 'ignore' command). A break\n_
1687 caused by 'next' is considered breakpoint 0 (zero), even if an\n_
1688 ordinary breakpoint is in effect on the same line. The 'condition',\n_
1689 'do', 'info' commands accept 0 as a breakpoint number.",
1690 __dbg_cc_next, , __dbg_cx_next]
1691 ### print
1692 __dbg_g_cmd["print"] := ["print", PRINT_CMD,
1693 " print expr {; expr}\n_
1694 Evaluates and print image of expression(s). Only the first value from\n_
1695 each expression is printed. '&fail' printed if an expression fails.",
1696 __dbg_cc_print, , __dbg_cx_print]
1697 ### set
1698 __dbg_g_cmd["set"] := ["set", SET_CMD,
1699 " set echo filename\n_
1700 Starts echoing output to a file.\n_
1701 \ set prelude [<file]\n_
1702 Defines a macro to be exeucted at breaks. The default prelude is\n_
1703 \ fprint \"[%1] %2 (%3:%4)\\n\";&bp;&proc;&file;&line\n_
1704 It prints breakpoint number, procedure name, source file name, and\n_
1705 line number.\n_
1706 \ set postlude [<file]\n_
1707 Defines a macro to be executed when resuming execution. The default\n_
1708 postlude does nothing.",
1709 __dbg_cc_set, , __dbg_cx_set]
1710 ### source
1711 __dbg_g_cmd["source"] := ["source", SOURCE_CMD,
1712 " source filename\n_
1713 Reads commands from a file. Takes effect immediately when used in a macro\n_
1714 definition.",
1715 __dbg_cc_source, , __dbg_cx_NOOP]
1716 ### stop
1717 __dbg_g_cmd["stop"] := ["stop", STOP_CMD,
1718 " stop\n_
1719 Stops the program and terminates the debugging session.",
1720 __dbg_cc_SIMPLE, , __dbg_cx_stop]
1721 ### trace
1722 __dbg_g_cmd["trace"] := ["trace", TRACE_CMD,
1723 " trace count\n_
1724 Sets the value of the Icon trace counter (&trace) to 'count'.",
1725 __dbg_cc_trace, , __dbg_cx_trace]
1726 ### where
1727 __dbg_g_cmd["where"] := ["where", WHERE_CMD,
1728 " where\n_
1729 Prints the call chain leading up to the current procedure.\n_
1730 Displays frame numbers which may be used by the 'frame' command.",
1731 __dbg_cc_SIMPLE, , __dbg_cx_where]
1732 end
1733
1734 ############### EXPRESSIONS ##############################
1735 #
1736 # Parses a fair subset of Icon expressions.
1737 # Compiles them into a linear post-fix representation.
1738 # Evaluates.
1739 # Somewhat adapted to the debugging environment, but
1740 # generally useful with small modifications.
1741 #
1742 ##########################################################
1743
1744 #
1745 #-------------- Expression management constants ----------
1746 #
1747
1748 $define IDENT_T 1
1749 $define INTEGER_T 2
1750 $define STRING_T 3
1751 $define SPECIAL_T 4
1752 $define FIELD_T 5
1753 $define LIST_T 6
1754 $define EXPR_T 8
1755 $define ELIST_T 9
1756 $define UNOP_T 10
1757 $define BINOP_T 11
1758 $define TEROP_T 12
1759 $define INVOKE_T 13
1760
1761 $define NOTN_OP 901
1762 $define ISN_OP 902
1763 $define SIZ_OP 903
1764 $define BNG_OP 904
1765 $define NEG_OP 905
1766
1767 $define ALT_OP 1501
1768 $define CNJ_OP 1401
1769 # N -- numerical comparison.
1770 $define NEQ_OP 1301
1771 $define NNE_OP 1302
1772 $define NLE_OP 1303
1773 $define NLT_OP 1304
1774 $define NGE_OP 1305
1775 $define NGT_OP 1306
1776 # L -- lexical comparison.
1777 $define LLT_OP 1307
1778 $define LLE_OP 1308
1779 $define LEQ_OP 1309
1780 $define LNE_OP 1310
1781 $define LGE_OP 1311
1782 $define LGT_OP 1312
1783 $define EQ_OP 1313
1784 $define NE_OP 1314
1785 $define ADD_OP 1201
1786 $define SUBTR_OP 1202
1787 $define UNION_OP 1203
1788 $define DIFF_OP 1204
1789 $define CAT_OP 1101
1790 $define LCAT_OP 1102
1791 $define MUL_OP 1001
1792 $define DIV_OP 1002
1793 $define REM_OP 1003
1794 $define ISCT_OP 1004
1795 $define EXP_OP 1001
1796 $define INVOKE_OP 801
1797 $define SSC_OP 802
1798 $define PART_OP 803
1799 $define FLD_OP 804
1800
1801 $define CLOCK_SP 1
1802 $define CURRENT_SP 2
1803 $define DATE_SP 3
1804 $define DATELINE_SP 4
1805 $define POS_SP 5
1806 $define REGIONS_SP 6
1807 $define SOURCE_SP 7
1808 $define STORAGE_SP 8
1809 $define SUBJECT_SP 9
1810 $define VERSION_SP 10
1811
1812 $define BREAK_SP 101
1813 $define FILE_SP 102
1814 $define LEVEL_SP 103
1815 $define LINE_SP 104
1816 $define PROC_SP 105
1817 $define TRACE_SP 106
1818
1819 #
1820 #-------------- Expression parsing ----------------------
1821 #
1822
1823 procedure __dbg_e_compile (str)
1824 # Compiles one or more expressions separated by a semicolon.
1825 # 'str' must be the candidate expression (string).
1826 # RETURNS a list of lists where each sublist has the following components:
1827 # (1) The compiled expression in postfix representation (list).
1828 # This representation can be used with the '__dbg_e_eval' procedure.
1829 # (2) The expression source string.
1830 # FAILS on conflict.
1831 # SIDE EFFECT: Assigns a message (string) to '__dbg_ge_message' on conflict;
1832 # assigns &null otherwise.
1833 local elist, res1, res2, pos1, pos2
1834 elist := []
1835 # Parse the expression(s).
1836 str ? repeat {
1837 pos1 := &pos
1838 (res1 := 1(__dbg_e_expr(), pos2:= &pos, __dbg_e_ws (),
1839 (__dbg_fpos (0) | __dbg_fany (';')))) | {
1840 __dbg_ge_message := "Expression syntax error."
1841 fail
1842 }
1843 # Linearize, convert to postfix.
1844 __dbg_ge_message := &null
1845 res2 := []
1846 __dbg_e_ecode (res1, res2)
1847 # Check for conflict.
1848 /__dbg_ge_message | fail
1849 __dbg_fput (elist, [res2, str[pos1:pos2]])
1850 if __dbg_fpos (0) then
1851 break
1852 else {
1853 __dbg_fmove (1)
1854 __dbg_e_ws ()
1855 }
1856 }
1857 return elist
1858 end
1859
1860 procedure __dbg_e_expr()
1861 __dbg_ftab (__dbg_fmany (' \t'))
1862 suspend [__dbg_e_term()] |
1863 ([__dbg_e_term(), __dbg_e_bin()] ||| __dbg_e_expr())
1864 end
1865
1866 procedure __dbg_e_term()
1867 __dbg_ftab (__dbg_fmany (' \t'))
1868 suspend [__dbg_e_factor()] |
1869 [__dbg_e_factor(), __dbg_e_form()] |
1870 [__dbg_e_un(), __dbg_e_factor()] |
1871 [__dbg_e_un(), __dbg_e_factor(), __dbg_e_form()]
1872 end
1873
1874 procedure __dbg_e_form()
1875 __dbg_ftab (__dbg_fmany (' \t'))
1876 suspend 2(=".", [FLD_OP, [FIELD_T, __dbg_e_idf()]]) |
1877 2(="[", [SSC_OP, __dbg_e_expr()], ="]") |
1878 2(="(", [INVOKE_OP, __dbg_e_elist()], =")") |
1879 2(="[", [PART_OP, __dbg_e_expr(),
1880 3(__dbg_e_ws(), =":", __dbg_e_expr())], ="]") |
1881 (2(=".", [FLD_OP, [FIELD_T, __dbg_e_idf()]]) ||| __dbg_e_form()) |
1882 (2(="[", [SSC_OP, __dbg_e_expr()], ="]") ||| __dbg_e_form()) |
1883 (2(="(", [INVOKE_OP, __dbg_e_elist()], =")") ||| __dbg_e_form()) |
1884 (2(="[", [PART_OP, __dbg_e_expr(),
1885 3(__dbg_e_ws(), =":", __dbg_e_expr())], ="]") |||
1886 __dbg_e_form())
1887 end
1888
1889 procedure __dbg_e_elist()
1890 __dbg_ftab (__dbg_fmany (' \t'))
1891 suspend [] |
1892 [__dbg_e_expr()] |
1893 [__dbg_e_expr()] ||| 3(__dbg_e_ws(), =",", __dbg_e_elist())
1894 end
1895
1896 procedure __dbg_e_factor()
1897 __dbg_ftab (__dbg_fmany (' \t'))
1898 suspend [IDENT_T, __dbg_e_idf()] |
1899 [INTEGER_T, __dbg_e_ilit()] |
1900 [STRING_T, __dbg_e_slit()] |
1901 [SPECIAL_T, (="&", __dbg_e_idf())] |
1902 2(="(", [EXPR_T, __dbg_e_expr()], __dbg_e_ws(), =")") |
1903 2(="[", [LIST_T, __dbg_e_elist()], __dbg_e_ws(), ="]")
1904 end
1905
1906 procedure __dbg_e_idf()
1907 static char1, char2
1908 initial {
1909 char1 := &ucase ++ &lcase ++ '_'
1910 char2 := char1 ++ &digits
1911 }
1912 suspend __dbg_ftab (__dbg_fmany (char1)) || (__dbg_ftab (__dbg_fmany (char2)) | "")
1913 end
1914
1915 procedure __dbg_e_ilit()
1916 suspend __dbg_ftab (__dbg_fmany (&digits))
1917 end
1918
1919 procedure __dbg_e_strend()
1920 static signal, nonsignal
1921 initial {
1922 signal := '\"\\'
1923 nonsignal := ~signal
1924 }
1925 suspend 2(="\"", "") |
1926 1(__dbg_e_stresc(), ="\"") |
1927 (__dbg_e_stresc() || __dbg_ftab (__dbg_fmany (nonsignal)) || __dbg_e_strend()) |
1928 (__dbg_e_stresc() || __dbg_e_strend())
1929 end
1930
1931 procedure __dbg_e_stresc()
1932 suspend (="\\n", "\n") |
1933 (="\\t", "\t") |
1934 (="\\r", "\r") |
1935 (="\\", __dbg_fmove (1))
1936 end
1937
1938 procedure __dbg_e_slit()
1939 static signal, nonsignal
1940 initial {
1941 signal := '\"\\'
1942 nonsignal := ~signal
1943 }
1944 suspend 2(="\"",
1945 (__dbg_ftab (__dbg_fmany (nonsignal)) || __dbg_e_strend()) | __dbg_e_strend())
1946 end
1947
1948 procedure __dbg_e_un()
1949 # Sequence of unary operators.
1950 # Always succeeds.
1951 # NOTE: Assumes no space between operators.
1952 static unop
1953 initial unop := '\\/*!-'
1954 __dbg_ftab (__dbg_fmany (' \t'))
1955 suspend [UNOP_T, __dbg_ftab (__dbg_fmany (unop))]
1956 end
1957
1958 procedure __dbg_e_bin()
1959 # Binary operators.
1960 static optab
1961 initial {
1962 # Table of operators.
1963 # Operators are coded as decimal integers where the hundreds
1964 # digit defines precedence.
1965 optab := table()
1966 optab["|"] := ALT_OP
1967 optab["&"] := CNJ_OP
1968 optab["="] := NEQ_OP
1969 optab["~="] := NNE_OP
1970 optab["<="] := NLE_OP
1971 optab["<"] := NLT_OP
1972 optab[">="] := NGE_OP
1973 optab[">"] := NGT_OP
1974 optab["<<"] := LLT_OP
1975 optab["<<="] := LLE_OP
1976 optab["=="] := LEQ_OP
1977 optab["~=="] := LNE_OP
1978 optab[">>="] := LGE_OP
1979 optab[">>"] := LGT_OP
1980 optab["==="] := EQ_OP
1981 optab["~==="] := NE_OP
1982 optab["+"] := ADD_OP
1983 optab["-"] := SUBTR_OP
1984 optab["++"] := UNION_OP
1985 optab["--"] := DIFF_OP
1986 optab["||"] := CAT_OP
1987 optab["|||"] := LCAT_OP
1988 optab["*"] := MUL_OP
1989 optab["/"] := DIV_OP
1990 optab["%"] := REM_OP
1991 optab["**"] := ISCT_OP
1992 optab["^"] := EXP_OP
1993 }
1994 __dbg_ftab (__dbg_fmany (' \t'))
1995 suspend \optab[__dbg_fmove (3)] |
1996 \optab[__dbg_fmove (2)] |
1997 \optab[__dbg_fmove (1)] |
1998 \optab[=("~===")]
1999 end
2000
2001 procedure __dbg_e_ws()
2002 # Removes optional white space.
2003 # The point is that it always succeeds.
2004 __dbg_ftab (__dbg_fmany (' \t'))
2005 return 1
2006 end
2007
2008 #-------------- Linearization ----------------------
2009
2010 procedure __dbg_e_ecode (ex, res)
2011 # 'Evaluates' the list resulting from pattern matching.
2012 # Produces a single list with everything in postfix order.
2013 # 'ex' must be an expression in the form that '__dbg_e_compile' generates.
2014 # 'res' must be an (empty) list where the expression elements are to
2015 # be inserted.
2016 # Always FAILS.
2017 # SIDE EFFECT: Adds elements to 'res'.
2018 # Assigns a message string to '__dbg_ge_message' on conflict.
2019 local opnd, oprt, op_stack
2020 if *ex = 1 then
2021 __dbg_e_tcode (ex[1], res)
2022 else {
2023 op_stack := []
2024 opnd := create !ex
2025 __dbg_e_tcode (@opnd, res)
2026 while oprt := @opnd do {
2027 while (op_stack[1]/100) <= (oprt/100) do
2028 __dbg_fput (res, __dbg_e_proc ([BINOP_T, __dbg_fpop (op_stack),]))
2029 __dbg_fpush (op_stack, oprt)
2030 __dbg_e_tcode (@opnd, res)
2031 }
2032 while __dbg_fput (res, __dbg_e_proc ([BINOP_T, __dbg_fpop (op_stack),]))
2033 }
2034 end
2035
2036 procedure __dbg_e_tcode (tm, res)
2037 # Disentangles a term.
2038 local comp, unary
2039 static special, unop
2040 initial {
2041 special := __dbg_ftable ()
2042 # The 'normal' keywords.
2043 special["clock"] := CLOCK_SP
2044 special["current"] := CURRENT_SP
2045 special["date"] := DATE_SP
2046 special["dateline"] := DATELINE_SP
2047 special["pos"] := POS_SP
2048 special["regions"] := REGIONS_SP
2049 special["source"] := SOURCE_SP
2050 special["storage"] := STORAGE_SP
2051 special["subject"] := SUBJECT_SP
2052 special["trace"] := TRACE_SP
2053 special["version"] := VERSION_SP
2054
2055 # The special keywords.
2056 special["bp"] :=BREAK_SP
2057 special["breakpoint"] :=BREAK_SP
2058 special["file"] := FILE_SP
2059 special["level"] := LEVEL_SP
2060 special["line"] := LINE_SP
2061 special["proc"] := PROC_SP
2062
2063 unop := __dbg_ftable ()
2064 unop["\\"] := NOTN_OP
2065 unop["/"] := ISN_OP
2066 unop["*"] := SIZ_OP
2067 unop["!"] := BNG_OP
2068 unop["-"] := NEG_OP
2069 }
2070 every comp := !tm do case comp[1] of {
2071 UNOP_T: unary := comp # Save for later.
2072 INTEGER_T: {
2073 comp[2] := __dbg_finteger (comp[2])
2074 __dbg_fput (res, comp)
2075 }
2076 SPECIAL_T: {
2077 if comp[2] := \special[comp[2]] then
2078 __dbg_fput (res, comp)
2079 else
2080 __dbg_ge_message := "'" || comp[2] ||
2081 "': unrecognized special identifier."
2082 }
2083 EXPR_T: __dbg_e_ecode (comp[2], res)
2084 LIST_T: {
2085 every __dbg_e_ecode (!comp[2], res)
2086 __dbg_fput (res, [LIST_T, *comp[2]])
2087 }
2088 (FLD_OP | SSC_OP | INVOKE_OP | PART_OP) :
2089 __dbg_e_fcode (comp, res)
2090 default: __dbg_fput (res, comp)
2091 # This includes: IDENT_T, STRING_T
2092 }
2093 every __dbg_fput (res, __dbg_e_proc ([UNOP_T, unop[!__dbg_freverse ((\unary)[2])],]))
2094 end
2095
2096 procedure __dbg_e_fcode (fm, res)
2097 # Disentangles a form.
2098 # The operators have the same precedence; stack not needed.
2099 local comp, opnd, oprt
2100 comp := create !fm
2101 while oprt := @comp do {
2102 opnd := @comp # There is at least one operand.
2103 case oprt of {
2104 FLD_OP: {
2105 __dbg_fput (res, opnd)
2106 __dbg_fput (res, [BINOP_T, oprt, __dbg_e_field])
2107 }
2108 SSC_OP: {
2109 __dbg_e_ecode (opnd, res)
2110 __dbg_fput (res, [BINOP_T, oprt, __dbg_fproc ("[]", 2)])
2111 }
2112 INVOKE_OP: {
2113 every __dbg_e_ecode (!opnd, res)
2114 __dbg_fput (res, [INVOKE_T, *opnd])
2115 }
2116 PART_OP: {
2117 __dbg_e_ecode (opnd, res)
2118 __dbg_e_ecode (@comp, res)
2119 __dbg_fput (res, [TEROP_T, oprt, __dbg_fproc ("[:]", 3)])
2120 }
2121 default: __dbg_ge_message := __dbg_fimage (oprt) || ": weird operator."
2122 }
2123 }
2124 end
2125
2126 procedure __dbg_e_proc (op_d)
2127 # 'op_d' must be an operator descriptor (list(3)).
2128 # RETURNS the descriptor with the 3rd component filled in by a
2129 # procedure/function.
2130 static opt
2131 initial {
2132 opt := __dbg_ftable ()
2133 opt[NOTN_OP] := __dbg_fproc ("\\", 1)
2134 opt[ISN_OP] := __dbg_fproc ("/", 1)
2135 opt[SIZ_OP] := __dbg_fproc ("*", 1)
2136 opt[BNG_OP] := __dbg_fproc ("!", 1)
2137 opt[NEG_OP] := __dbg_fproc ("-", 1)
2138 opt[ALT_OP] := __dbg_e_alt
2139 opt[CNJ_OP] := __dbg_e_cnj
2140 opt[NEQ_OP] := __dbg_fproc ("=", 2)
2141 opt[NNE_OP] := __dbg_fproc ("~=", 2)
2142 opt[NLE_OP] := __dbg_fproc ("<=", 2)
2143 opt[NLT_OP] := __dbg_fproc ("<", 2)
2144 opt[NGE_OP] := __dbg_fproc (">=", 2)
2145 opt[NGT_OP] := __dbg_fproc (">", 2)
2146 opt[LLT_OP] := __dbg_fproc ("<<", 2)
2147 opt[LLE_OP] := __dbg_fproc ("<<=", 2)
2148 opt[LEQ_OP] := __dbg_fproc ("==", 2)
2149 opt[LNE_OP] := __dbg_fproc ("~==", 2)
2150 opt[LGE_OP] := __dbg_fproc (">>=", 2)
2151 opt[LGT_OP] := __dbg_fproc (">>", 2)
2152 opt[EQ_OP] := __dbg_fproc ("===", 2)
2153 opt[NE_OP] := __dbg_fproc ("~===", 2)
2154 opt[ADD_OP] := __dbg_fproc ("+", 2)
2155 opt[SUBTR_OP] := __dbg_fproc ("-", 2)
2156 opt[UNION_OP] := __dbg_fproc ("++", 2)
2157 opt[DIFF_OP] := __dbg_fproc ("--", 2)
2158 opt[CAT_OP] := __dbg_fproc ("||", 2)
2159 opt[LCAT_OP] := __dbg_fproc ("|||", 2)
2160 opt[MUL_OP] := __dbg_fproc ("*", 2)
2161 opt[DIV_OP] := __dbg_fproc ("/", 2)
2162 opt[REM_OP] := __dbg_fproc ("%", 2)
2163 opt[ISCT_OP] := __dbg_fproc ("**", 2)
2164 opt[EXP_OP] := __dbg_fproc ("^", 2)
2165 opt[SSC_OP] := __dbg_fproc ("[]", 2)
2166 opt[PART_OP] := __dbg_fproc ("[:]", 2)
2167 opt[FLD_OP] := __dbg_e_field
2168 }
2169 op_d[3] := opt[op_d[2]]
2170 return op_d
2171 end
2172
2173 #-------------- Evaluation ----------------------
2174
2175 procedure __dbg_e_eval (expr)
2176 # Evaluates a compiled expression.
2177 # 'expr' must be an expression using the representation created by
2178 # '__dbg_e_compile' (list).
2179 # GENERATES all expression values.
2180 # SIDE EFFECT: Assigns a message (string) to '__dbg_ge_message' on conflict;
2181 # assigns &null otherwise.
2182 local val
2183 __dbg_ge_message := &null
2184 &error := -1
2185 every val := __dbg_e_eval1 (expr, []) do {
2186 &error := 0
2187 suspend val
2188 __dbg_ge_message := &null
2189 &error := -1
2190 }
2191 if &error < -1 then
2192 __dbg_ge_message := "Error number " || &errornumber || ": " ||
2193 &errortext || "." ||
2194 (("\nOffending value: " || __dbg_fimage (\&errorvalue) || ".") | "")
2195 &error := 0
2196 end
2197
2198 procedure __dbg_e_alt (opnd1, opnd2)
2199 # Our version of alternation.
2200 suspend (opnd1 | opnd2)
2201 end
2202
2203 procedure __dbg_e_cnj (opnd1, opnd2)
2204 # Our version of conjunction.
2205 suspend (opnd1 & opnd2)
2206 end
2207
2208 procedure __dbg_e_field (opnd1, opnd2)
2209 # Record field access.
2210 # Any better way to determine if a value is a record of any type?
2211 static builtin
2212 initial {
2213 builtin := __dbg_ftable ()
2214 builtin["co-expression"] := 1
2215 builtin["cset"] := 1
2216 builtin["file"] := 1
2217 builtin["integer"] := 1
2218 builtin["list"] := 1
2219 builtin["null"] := 1
2220 builtin["procedure"] := 1
2221 builtin["real"] := 1
2222 builtin["set"] := 1
2223 builtin["string"] := 1
2224 builtin["table"] := 1
2225 }
2226 if \builtin[__dbg_ftype (opnd1)] then {
2227 __dbg_ge_message := "Record expected; found " || __dbg_fimage (opnd1)
2228 fail
2229 }
2230 suspend opnd1[opnd2]
2231 end
2232
2233 procedure __dbg_e_ident (idf)
2234 # Evaluates an identifier.
2235 local val
2236 (val := ((__dbg_ge_singular ~=== __dbg_g_local[idf]) | variable (idf))) | {
2237 __dbg_ge_message := "Identifier '" || idf || "' not visible."
2238 fail
2239 }
2240 suspend val
2241 end
2242
2243 procedure __dbg_e_special (sp_code)
2244 # Evaluates a special identifier.
2245 suspend case sp_code of {
2246 # Regular Icon keyword variables.
2247 CLOCK_SP: &clock
2248 CURRENT_SP: &current
2249 DATE_SP: &date
2250 DATELINE_SP: &dateline
2251 POS_SP: &pos
2252 REGIONS_SP: &regions
2253 SOURCE_SP: &source
2254 STORAGE_SP: &storage
2255 SUBJECT_SP: &subject
2256 VERSION_SP: &version
2257 # Special keywords.
2258 BREAK_SP: (\__dbg_g_where[WHERE_BRKP])[BRKP_ID]
2259 FILE_SP: __dbg_g_where[WHERE_FILE]
2260 LEVEL_SP: __dbg_g_level
2261 LINE_SP: __dbg_g_where[WHERE_LINE]
2262 PROC_SP: __dbg_g_where[WHERE_PROC]
2263 TRACE_SP: __dbg_g_trace
2264 default: {
2265 __dbg_ge_message := __dbg_fimage (sp_code) ||
2266 ": weird special identifier code."
2267 fail
2268 }
2269 }
2270 end
2271
2272 procedure __dbg_e_eval1 (expr, stack)
2273 # Evaluates an expression.
2274 # 'stack' must be the current evaluation stack (list).
2275 # The procedure is recursive; the initial invocation must supply an
2276 # empty list.
2277 local comp
2278 (comp := expr[1]) | while suspend __dbg_fpop (stack) | fail
2279 suspend __dbg_e_eval1 (expr[2:0], case comp[1] of {
2280 IDENT_T: stack ||| [__dbg_e_ident (comp[2])]
2281 SPECIAL_T: stack ||| [__dbg_e_special (comp[2])]
2282 LIST_T: stack[1:-comp[2]] ||| [stack[-comp[2]:0]]
2283 UNOP_T: stack[1:-1] ||| [comp[3](stack[-1])]
2284 BINOP_T: stack[1:-2] ||| [comp[3]!stack[-2:0]]
2285 TEROP_T: stack[1:-3] ||| [comp[3]!stack[-3:0]]
2286 INVOKE_T: stack[1:-(comp[2]+1)] |||
2287 [stack[-(comp[2]+1)]!stack[-comp[2]:0]]
2288 default: stack ||| [comp[2]]
2289 })
2290 end