Debian Codegraph @

src/lsp/top.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; top.lsp -- Top-level loop, break loop, and error handlers
6 ;;;;
7 ;;;; Copyright (c) 1990, Giuseppe Attardi.
8 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
9 ;;;;
10 ;;;; See file 'LICENSE' for the copyright details.
11
12 ;;;;
13 ;;;; Revised on July 11, by Carl Hoffman.
14 ;;;; Modified Oct 1986 by Ken Rimey.
15 ;;;; Reworked March 1987, by Edward Wang.
16 ;;;; Merged into new distribution Sept 1987, by Edward Wang.
17 ;;;; Reworked for Threads November 1988, by Giuseppe Attardi.
18 ;;;; Reworked for CLOS November 1988, by Giuseppe Attardi.
19 ;;;; Updated May 2009, by Jean-Claude Beaudoin
20
21 (in-package "SYSTEM")
22
23 (export '(*break-readtable* *break-on-warnings*
24 *tpl-evalhook* *tpl-prompt-hook*))
25
26 (defparameter *quit-tag* (cons nil nil))
27 (defparameter *quit-tags* nil)
28 (defparameter *break-level* 0) ; nesting level of error loops
29 (defparameter *break-env* nil)
30 (defparameter *ihs-base* 0)
31 (defparameter *ihs-top* (ihs-top))
32 (defparameter *ihs-current* 0)
33 (defparameter *frs-base* 0)
34 (defparameter *frs-top* 0)
35 (defparameter *tpl-continuable* t)
36 (defparameter *tpl-prompt-hook* nil)
37 (defparameter *eof* (cons nil nil))
38
39 (defparameter *last-error* nil)
40
41 (defparameter *break-message* nil)
42 (defparameter *break-condition* nil)
43
44 (defparameter *break-readtable* nil)
45 (defparameter *tpl-level* -1) ; nesting level of top-level loops
46 (defparameter *step-level* 0) ; repeated from trace.lsp
47
48 (defparameter *break-hidden-functions* '(error cerror apply funcall invoke-debugger))
49 (defparameter *break-hidden-packages* (list #-ecl-min (find-package 'system)))
50
51 (defconstant tpl-commands
52 '(("Top level commands"
53 ((:cf :compile-file) tpl-compile-command :string
54 ":cf Compile file"
55 ":compile-file &string &rest files [Top level command]~@
56 :cf &string &rest files [Abbreviation]~@
57 ~@
58 Compile files. With no arguments, uses values from latest :cf~@
59 command. File extensions are optional.~%")
60 ((:exit :eof) quit :eval
61 ":exit Exit Lisp"
62 ":exit &eval &optional (status 0) [Top level command]~@
63 ~@
64 Exit Lisp without further confirmation.~%")
65 ((:ld :load) tpl-load-command :string
66 ":ld Load file"
67 ":load &string &rest files [Top level command]~@
68 :ld &string &rest files [Abbreviation]~@
69 ~@
70 Load files. With no arguments, uses values from latest :ld~@
71 or :cf command. File extensions are optional.~%")
72 ((:step) tpl-step-command nil
73 ":step Single step form"
74 ":step form [Top level command]~@
75 ~@
76 Evaluate form in single step mode. While stepping, a new break~@
77 level is invoked before every evaluation. Extra commands are~@
78 available at this time to control stepping and form evaluation.~%")
79 ((:tr :trace) tpl-trace-command nil
80 ":tr(ace) Trace function"
81 ":trace &rest functions [Top level command]~@
82 :tr &rest functions [Abbreviation]~@
83 ~@
84 Trace specified functions. With no arguments, show currently~@
85 traced functions.~@
86 ~@
87 See also: :untrace.~%")
88 ((:untr :untrace) tpl-untrace-command nil
89 ":untr(ace) Untrace function"
90 ":untrace &rest functions [Top level command]~@
91 :untr &rest functions [Abbreviation]~@
92 ~@
93 Untrace specified functions. With no arguments, untrace~@
94 all functions.~@
95 ~@
96 See also: :trace.~%")
97 #+threads
98 ((:s :switch) tpl-switch-command nil
99 ":s(witch) Switch to next process to debug"
100 ":switch process [Break command]~@
101 :s processs [Abbreviation]~@
102 ~@
103 Switch to next process in need to debugger attention. Argument~@
104 process, when provided, must be an integer indicating the rank~@
105 of the process in the debugger waiting list.~%")
106 #+threads
107 ((:br :break) tpl-interrupt-command nil
108 ":br(eak) Stop a given process"
109 ":break process [Break command]~@
110 :br processs [Abbreviation]~@
111 ~@
112 Interrupt a given process. Argument process, must be provided and
113 it must be an integer indicating the rank~@
114 of the process in the debugger waiting list (:waiting).~%")
115 #+threads
116 ((:w :waiting) tpl-waiting-command nil
117 ":w(aiting) Display list of active toplevels"
118 ":waiting [Break command]~@
119 :w [Abbreviation]~@
120 ~@
121 Display list of active toplevels, including open debug sessions.~%")
122 )
123 ("Help commands"
124 ((:apropos) tpl-apropos-command nil
125 ":apropos Apropos"
126 ":apropos string &optional package [Top level command]~@
127 ~@
128 Finds all available symbols whose print names contain string.~@
129 If a non NIL package is specified, only symbols in that package are considered.~@
130 ~%")
131 ((:doc document) tpl-document-command nil
132 ":doc(ument) Document"
133 ":document symbol [Top level command]~@
134 ~@
135 Displays documentation about function, print names contain string.~%")
136 ((? :h :help) tpl-help-command nil
137 ":h(elp) or ? Help. Type \":help help\" for more information"
138 ":help &optional topic [Top level command]~@
139 :h &optional topic [Abbreviation]~@
140 ~@
141 Print information on specified topic. With no arguments, print~@
142 quick summary of top level commands.~@
143 ~@
144 Help information for top level commands follows the documentation~@
145 style found in \"Common Lisp, the Language\"; and, in general, the~@
146 commands themselves follow the conventions of Common Lisp functions,~@
147 with the exception that arguments are normally not evaluated.~@
148 Those commands that do evaluate their arguments are indicated by the~@
149 keyword &eval in their description. A third class of commands~@
150 treat their arguments as whitespace-separated, case-sensitive~@
151 strings, requiring double quotes only when necessary. This style~@
152 of argument processing is indicated by the keyword &string.~@
153 For example, the :load command accepts a list of file names:
154 ~@
155 :load &string &rest files [Top level Command]~@
156 ~@
157 whereas :exit, which requires an optional evaluated argument, is~@
158 ~@
159 :exit &eval &optional status [Top level Command]~%")
160 )))
161
162 (defparameter *tpl-commands* tpl-commands)
163
164 (defconstant break-commands
165 '("Break commands"
166 ((:q :quit) tpl-quit-command nil
167 ":q(uit) Return to some previous break level"
168 ":quit &optional n [Break command]~@
169 :q &optional n [Abbreviation]~@
170 ~@
171 Without argument, return to top level;~@
172 otherwise return to break level n.~%")
173 ((:pop) (tpl-pop-command) :constant
174 ":pop Pop to previous break level"
175 ":pop [Break command]~@
176 ~@
177 Pop to previous break level, or if already in top level,~@
178 exit Lisp after confirmation.~%")
179 ((:c :continue) continue nil
180 ":c(ontinue) Continue execution"
181 ":continue [Break command]~@
182 :c [Abbreviation]~@
183 ~@
184 Continue execution. Return from current break level to the caller.~@
185 This command is only available when the break level is continuable~@
186 (e.g., called from a correctable error or the function break).~%")
187 ((:b :backtrace) tpl-backtrace nil
188 ":b(acktrace) Print backtrace"
189 ":backtrace &optional n [Break command]~@
190 :b &optional n [Abbreviation]~@
191 ~@
192 Show function call history. Only those functions called since~@
193 the previous break level are shown. In addition, functions compiled~@
194 in-line or explicitly hidden are not displayed. Without an argument,~@
195 a concise backtrace is printed with the current function in upper~@
196 case. With integer argument n, the n functions above and including~@
197 the current one are printed in a verbose format.~@
198 ~@
199 See also: :function, :previous, :next.~%")
200 ((:f :function) tpl-print-current nil
201 ":f(unction) Show current function"
202 ":function [Break command]~@
203 :f [Abbreviation]~@
204 ~@
205 Show current function. The current function is the implicit focus~@
206 of attention for several other commands. When it is an interpreted~@
207 function, its lexical environment is available for inspection and~@
208 becomes the environment for evaluating user input forms.~@
209 ~@
210 See also: :backtrace, :next, previous, :disassemble, :variables.~%")
211 ((:p :previous) tpl-previous nil
212 ":p(revious) Go to previous function"
213 ":previous &optional (n 1) [Break command]~@
214 :p &optional (n 1) [Abbreviation]~@
215 ~@
216 Move to the nth previous visible function in the backtrace.~@
217 It becomes the new current function.~@
218 ~@
219 See also: :backtrace, :function, :go, :next.~%")
220 ((:d :down) tpl-previous nil
221 ":d(own) Alias to :previous"
222 ""
223 )
224 ((:n :next) tpl-next nil
225 ":n(ext) Go to next function"
226 ":next &optional (n 1) [Break command]~@
227 :n &optional (n 1) [Abbreviation]~@
228 ~@
229 Move to the nth next visible function in the backtrace. It becomes~@
230 the new current function.~@
231 ~@
232 See also: :backtrace, :function, :go, :previous.~%")
233 ((:u :up) tpl-next nil
234 ":u(p) Alias to :next"
235 ""
236 )
237 ((:g :go) tpl-go nil
238 ":g(o) Go to next function"
239 ":go &optional (n 1) [Break command]~@
240 :g &optional (n 1) [Abbreviation]~@
241 ~@
242 Move to the function at IHS[i].~@
243 See also: :backtrace, :function, :next, :previous.~%")
244 ((:fs :forward-search) tpl-forward-search :string
245 ":fs Search forward for function"
246 ":forward-search &string substring [Break command]~@
247 :fs &string substring [Abbreviation]~@
248 ~@
249 Search forward in the backtrace for function containing substring.~@
250 The match is case insensitive.~@
251 ~@
252 See also: :backtrace, :function, :next.~%")
253 ((:bs :backward-search) tpl-backward-search :string
254 ":bs Search backward for function"
255 ":backward-search &string substring [Break command]~@
256 :bs &string substring [Abbreviation]~@
257 ~@
258 Search backward in the backtrace for function containing substring.~@
259 The match is case insensitive.~@
260 ~@
261 See also: :backtrace, :function, :previous.~%")
262 ((:disassemble) tpl-disassemble-command nil
263 ":disassemble Disassemble current function"
264 ":disassemble [Break command]~@
265 :disassemble [Abbreviation]~@
266 ~@
267 Disassemble the current function. Currently, only interpreted functions~@
268 can be disassembled.~%")
269 ((:le :lambda-expression) tpl-lambda-expression-command nil
270 ":l(ambda-)e(expression) Show lisp code for current function"
271 ":lambda-expression [Break command]~@
272 :le [Abbreviation]~@
273 ~@
274 Show the lisp code of the current function. Only works for interpreted~@
275 functions.~%")
276 ((:v :variables) tpl-variables-command nil
277 ":v(ariables) Show local variables, functions, blocks, and tags"
278 ":variables &optional no-values [Break command]~@
279 :v &optional no-values [Abbreviation]~@
280 ~@
281 Show lexical variables, functions, block names, and tags local~@
282 to the current function. The current function must be interpreted.~@
283 The values of local variables and functions are also shown,~@
284 unless the argument is non-null.~%")
285 #|
286 ((:l :local) tpl-local-command nil
287 ":l(ocal) Return the nth local value on the stack"
288 ":local &optional (n 0) [Break command]~@
289 :l &optional (n 0) [Abbreviation]
290 ~@
291 For compiled functions, return the value of the nth lexical variable.~@
292 As is done normally, the returned value is both printed by the top~@
293 level as well as saved in the variable *.~%")
294 |#
295 ((:hide) tpl-hide nil
296 ":hide Hide function"
297 ":hide function [Break command]~@
298 ~@
299 Hide function. A hidden function is not displayed in a backtrace.~@
300 ~@
301 See also: :backtrace, :unhide, :hide-package.~%")
302 ((:unhide) tpl-unhide nil
303 ":unhide Unhide function"
304 ":unhide function [Break command]~@
305 ~@
306 Unhide function. The specified function will be displayed in future~@
307 backtraces, unless its home package is also hidden.~@
308 ~@
309 See also: :backtrace, :hide, :unhide-package.~%")
310 ((:hp :hide-package) tpl-hide-package nil
311 ":hp Hide package"
312 ":hide-package package [Break command]~@
313 :hp package [Abbreviation]~@
314 ~@
315 Hide package. Functions in a hidden package are not displayed~@
316 in a backtrace.~@
317 ~@
318 See also: :backtrace, :unhide-package.~%")
319 ((:unhp :unhide-package) tpl-unhide-package nil
320 ":unhp Unhide package"
321 ":unhide-package package [Break command]~@
322 :unhp package [Abbreviation]~@
323 ~@
324 Unhide package. Functions in the specified package will be displayed~@
325 in future backtraces, unless they are individually hidden.~@
326 ~@
327 See also: :backtrace, :hide-package, :hide, :unhide.~%")
328 ((:unhide-all) tpl-unhide-all nil
329 ":unhide-all Unhide all variables and packages"
330 ":unhide-all [Break command]~@
331 ~@
332 Unhide all variables and packages. All functions will be displayed~@
333 in future backtraces.~@
334 ~@
335 See also: :hide, :unhide, :hide-package, :unhide-package.~%")
336 #|
337 ((:vs :value-stack) tpl-vs-command nil
338 ":vs Show value stack"
339 ":value-stack &optional n [Break command]~@
340 :vs &optional n [Abbreviation]~@
341 ~@
342 Without an argument, show the entire value stack since the previous~@
343 break level. With an integer argument n, print nothing, but return~@
344 the nth value stack entry.~@
345 ~@
346 See also: :local.~%")
347 |#
348 ((:bds :binding-stack) tpl-bds-command nil
349 ":bds Show binding stack"
350 ":binding-stack &optional variable [Break command]~@
351 :bds &optional variable [Abbreviation]~@
352 ~@
353 Without an argument, show the entire binding stack since the previous~@
354 break level. With a variable name, print nothing, but return the~@
355 value of the given variable on the binding stack.~%")
356 ((:frs :frame-stack) tpl-frs-command nil
357 ":frs Show frame stack"
358 ""
359 )
360 ((:m :message) tpl-print-message nil
361 ":m(essage) Show error message"
362 ":message [Break command]~@
363 :m [Abbreviation]~@
364 ~@
365 Show current error message.~%")
366 ((:hs :help-stack) tpl-help-stack-command nil
367 ":hs Help stack"
368 ":help-stack [Break command]~@
369 :hs [Abbreviation]~@
370 ~@
371 Lists the functions to access the LISP system stacks.~%")
372 ((:i :inspect) tpl-inspect-command nil
373 ":i(nspect) Inspect value of local variable"
374 ":inspect var-name [Break command]~@
375 :i var-name [Abbreviation]~@
376 ~@
377 Inspect value of local variable named by var-name. Argument~@
378 var-name can be a string or a symbol whose name string will~@
379 then be used regardless of of the symbol's package.~@
380 ~@
381 See also: :variables.~%")
382 ((:cb :c-backtrace) ext::dump-c-backtrace nil
383 ":c(-)b(acktrace) Print a raw C backtrace"
384 ":c-backtrace n [Break command]~@
385 :cb n [Abbreviation]~@
386 ~@
387 Show function call history of the n C functions above and~@
388 including the current one.~@
389 ~@
390 See also: :backtrace.~%")
391 ))
392
393 (defparameter *lisp-initialized* nil)
394
395 (defun tpl-format (format-string &rest args)
396 (apply #'format *query-io* format-string args))
397
398 (defun top-level (&optional (process-command-line nil))
399 "Args: ()
400 ECL specific.
401 The top-level loop of ECL. It is called by default when ECL is invoked."
402 (catch *quit-tag*
403 (let* ((*debugger-hook* nil)
404 + ++ +++ - * ** *** / // ///)
405
406 (in-package "CL-USER")
407
408 (unless (or *lisp-initialized* (null process-command-line))
409 (process-command-args)
410 (tpl-format "ECL (Embeddable Common-Lisp) ~A (git:~D)"
411 (lisp-implementation-version)
412 (ext:lisp-implementation-vcs-id))
413 (tpl-format "~%Copyright (C) 1984 Taiichi Yuasa and Masami Hagiya~@
414 Copyright (C) 1993 Giuseppe Attardi~@
415 Copyright (C) 2013 Juan J. Garcia-Ripoll~@
416 Copyright (C) 2018 Daniel Kochmanski~@
417 Copyright (C) 2024 Daniel Kochmanski and Marius Gerbershagen~@
418 ECL is free software, and you are welcome to redistribute it~@
419 under certain conditions; see file 'Copyright' for details.~%")
420 (tpl-format "~&Type :h for Help."))
421 (setq *lisp-initialized* t)
422
423 (let ((*tpl-level* -1))
424 (tpl))
425 0)))
426
427 #+threads
428 (progn
429
430 (defparameter *console-lock* (mp:make-lock :name "Console lock"))
431 (defparameter *console-available* (mp:make-condition-variable))
432 (defparameter *console-owner* nil)
433 (defparameter *console-waiting-list* '())
434
435 (defun candidate-to-get-console-p (process)
436 (or (null *console-owner*)
437 (eq *console-owner* process)
438 (not (mp:process-active-p *console-owner*))))
439
440 (defun register-in-waiting-list (process)
441 (mp:with-lock (*console-lock*)
442 (push process *console-waiting-list*)))
443
444 (defun delete-from-waiting-list (process)
445 (mp:with-lock (*console-lock*)
446 (setf *console-waiting-list* (delete process *console-waiting-list*))))
447
448 (defun grab-console (process)
449 (loop with repeat = t
450 while repeat
451 do (mp:with-lock (*console-lock*)
452 (cond ((candidate-to-get-console-p process)
453 (setf *console-owner* process)
454 (setf repeat nil))
455 (t
456 (mp:condition-variable-wait *console-available* *console-lock*))))))
457
458 (defun release-console (process)
459 (mp:with-lock (*console-lock*)
460 (and (eq process *console-owner*) (setf *console-owner* nil))
461 (mp:condition-variable-signal *console-available*)))
462
463 ) ; #+threads
464
465 (defmacro with-grabbed-console (&rest body)
466 #-threads
467 `(progn ,@body)
468 #+threads
469 `(mp:without-interrupts
470 (unwind-protect
471 (mp:with-restored-interrupts
472 (register-in-waiting-list mp:*current-process*)
473 (grab-console mp:*current-process*)
474 ,@body)
475 (delete-from-waiting-list mp:*current-process*)
476 (release-console mp:*current-process*))))
477
478 (defparameter *allow-recursive-debug* nil)
479 (defparameter *debug-status* nil)
480
481 (defun simple-terminal-interrupt ()
482 (error 'ext:interactive-interrupt))
483
484 #+threads
485 (defun show-process-list (&optional (process-list (mp:all-processes)))
486 (loop with current = mp:*current-process*
487 for rank from 1
488 for process in process-list
489 do (tpl-format (if (eq process current)
490 "~% >~D: ~s"
491 "~% ~D: ~s")
492 rank process)))
493
494 #+threads
495 (defun query-process (&optional (process-list (mp:all-processes)))
496 (tpl-format "~&Choose the integer code of process to interrupt.
497 Use special code 0 to cancel this operation.")
498 (loop for code = (progn
499 (show-process-list process-list)
500 (tpl-prompt)
501 (tpl-read))
502 do (cond ((zerop code)
503 (return nil))
504 ((and (fixnump code) (<= 1 code (length process-list)))
505 (return (list (elt process-list (1- code)))))
506 (t
507 (tpl-format "~&Not a valid process number")))))
508
509 (defparameter *interrupt-lonely-threads-p* t)
510
511 (defun single-threaded-terminal-interrupt ()
512 (restart-case (simple-terminal-interrupt)
513 (continue ())))
514
515 (defun terminal-interrupt (&key (correctablep t))
516 (declare (ignore correctablep))
517 #+threads
518 (mp:without-interrupts
519 (let* ((suspended '())
520 (break-process nil)
521 (all-processes
522 (loop with this = mp:*current-process*
523 for p in (mp:all-processes)
524 unless (or (eq p this)
525 (member (mp:process-name p)
526 '(si:signal-servicing si::handle-signal)))
527 collect p)))
528 (when (and (= (length all-processes) 1) *interrupt-lonely-threads-p*)
529 (mp:interrupt-process (first all-processes)
530 #'single-threaded-terminal-interrupt)
531 (return-from terminal-interrupt))
532 (loop for i in all-processes
533 do (progn (tpl-format "~%;;; Suspending process ~A" i)
534 (push i suspended)
535 (mp:process-suspend i)))
536 (flet ((do-query-process ()
537 (print all-processes)
538 (query-process all-processes)))
539 (mp:with-local-interrupts
540 (restart-case (simple-terminal-interrupt)
541 (continue () (setf break-process nil))
542 (mp:interrupt-process (process)
543 :interactive do-query-process
544 :report (lambda (stream)
545 (princ "Interrupt a certain process." stream))
546 (setf break-process process)))))
547 (loop for process in suspended
548 unless (eq process break-process)
549 do (mp:process-resume process))
550 (when break-process
551 (mp:interrupt-process break-process
552 #'single-threaded-terminal-interrupt))))
553 #-threads
554 (single-threaded-terminal-interrupt))
555
556 (defun tpl (&key ((:commands *tpl-commands*) tpl-commands)
557 ((:prompt-hook *tpl-prompt-hook*) *tpl-prompt-hook*)
558 (broken-at nil)
559 (quiet nil))
560 #-ecl-min
561 (declare (c::policy-debug-ihs-frame))
562 (let* ((*ihs-base* *ihs-top*)
563 (*ihs-top* (if broken-at (ihs-search t broken-at) (ihs-top)))
564 (*ihs-current* (if broken-at (ihs-prev *ihs-top*) *ihs-top*))
565 (*frs-base* (or (sch-frs-base *frs-top* *ihs-base*) (1+ (frs-top))))
566 (*frs-top* (frs-top))
567 (*quit-tags* (cons *quit-tag* *quit-tags*))
568 (*quit-tag* *quit-tags*) ; any unique new value
569 (*tpl-level* (1+ *tpl-level*))
570 (break-level *break-level*)
571 values)
572 (set-break-env)
573 (set-current-ihs)
574 (flet ((rep ()
575 ;; We let warnings pass by this way "warn" does the work. It is
576 ;; conventional not to trap anything that is not a
577 ;; SERIOUS-CONDITION. Otherwise we would be interferring the
578 ;; behavior of code that relies on conditions for communication
579 ;; (for instance our compiler) and which expect nothing to happen
580 ;; by default.
581 (handler-bind
582 ((serious-condition
583 (lambda (condition)
584 (clear-input *query-io*)
585 (cond
586 ;; Toplevel should enter the debugger on any condition.
587 ((< break-level 1))
588 ;; We are told to let the debugger handle this.
589 (*allow-recursive-debug*)
590 ;; go back into the debugger loop.
591 (t
592 (tpl-format "~&Debugger received error of type: ~A~%~A~%"
593 (type-of condition) condition)
594 (tpl-format "Error flushed.~%")
595 (return-from rep t))))))
596 (with-grabbed-console
597 (unless quiet
598 (break-where)
599 (setf quiet t))
600 (setq - (locally (declare (notinline tpl-read))
601 (tpl-prompt)
602 (tpl-read))
603 values (multiple-value-list
604 (eval-with-env - *break-env*))
605 /// // // / / values *** ** ** * * (car /))
606 (tpl-format "~&~{~s~^~%~}~%" values)))))
607 (loop
608 (setq +++ ++ ++ + + -)
609 (when
610 (catch *quit-tag*
611 (if (zerop break-level)
612 (with-simple-restart
613 (restart-toplevel "Go back to Top-Level REPL.")
614 (rep))
615 (with-simple-restart
616 (restart-debugger "Go back to debugger level ~D." break-level)
617 (rep)))
618 nil)
619 (setf quiet nil))))))
620
621 (defun tpl-read ()
622 (let ((*read-suppress* nil))
623 (finish-output *query-io*)
624 (loop
625 (case (peek-char nil *query-io* nil :EOF)
626 ((#\))
627 (warn "Ignoring an unmatched right parenthesis.")
628 (read-char *query-io*))
629 ((#\space #\tab)
630 (read-char *query-io*))
631 ((#\newline #\return)
632 (read-char *query-io*)
633 ;; avoid repeating prompt on successive empty lines:
634 (let ((command (tpl-make-command :newline "")))
635 (when command (return command))))
636 (:EOF
637 (terpri *query-io*)
638 (return (tpl-make-command :EOF "")))
639 (#\:
640 (return (tpl-make-command (read-preserving-whitespace *query-io*)
641 (read-line *query-io*))))
642 (#\?
643 (read-char *query-io*)
644 (case (peek-char nil *query-io* nil :EOF)
645 ((#\space #\tab #\newline #\return :EOF)
646 (return (tpl-make-command :HELP (read-line *query-io*))))
647 (t
648 (unread-char #\? *query-io*)
649 (return (read-preserving-whitespace *query-io*)))))
650 ;; We use READ-PRESERVING-WHITESPACE because with READ, if an
651 ;; error happens within the reader, and we perform a ":C" or
652 ;; (CONTINUE), the reader will wait for an inexistent #\Newline.
653 (t
654 (return (read *query-io*)))))))
655
656 (defun tpl-prompt ()
657 (typecase *tpl-prompt-hook*
658 (function (funcall *tpl-prompt-hook*))
659 (string (tpl-format *tpl-prompt-hook*))
660 (t (tpl-format "~&~A~V,,,'>A "
661 (if (eq *package* (find-package 'cl-user))
662 ""
663 (package-name *package*))
664 (- *tpl-level* *step-level* -1)
665 ""))))
666
667 (defparameter *debug-tpl-commands* nil)
668
669 (defun harden-command (cmd-form)
670 `(if *debug-tpl-commands*
671 ,cmd-form
672 (handler-case ,cmd-form
673 (error (condition)
674 (tpl-format "~&Command aborted.~%Received condition of type: ~A~%~A"
675 (type-of condition) condition)
676 (clear-input *query-io*)))))
677
678 (defun tpl-make-command (name line &aux (c nil))
679 (dolist (commands *tpl-commands*)
680 (when (setq c (assoc name (cdr commands) :test #'member))
681 (return)))
682 (cond ((null c)
683 (if (eq name :newline) ; special handling for Newline.
684 nil
685 `(tpl-unknown-command ',name)))
686 ((eq (third c) :restart)
687 `(progn
688 (invoke-restart-interactively ,(second c))))
689 ((eq (third c) :eval)
690 `(,(second c) . ,(tpl-parse-forms line)))
691 ((eq (third c) :string)
692 (harden-command `(,(second c) . ,(tpl-parse-strings line))))
693 ((eq (third c) :constant)
694 (harden-command (second c)))
695 (t
696 (harden-command `(,(second c) . ,(tpl-parse-forms line t))))))
697
698 (defun tpl-parse-forms (line &optional quote)
699 (with-input-from-string (stream line)
700 (do ((form (read stream nil *eof*) (read stream nil *eof*))
701 (list nil))
702 ((eq form *eof*) (nreverse list))
703 (push (if quote `',form form) list))))
704
705 (defun tpl-parse-strings (line)
706 (do ((i 0 end)
707 (start)
708 (end)
709 (list nil)
710 (space-p #'(lambda (c) (or (eql c #\space) (eql c #\tab))))
711 (length (length line)))
712 ((>= i length) (nreverse list))
713 (cond ((null (setq start (position-if-not space-p line :START i)))
714 (setq end length))
715 ((eql (schar line start) #\")
716 (multiple-value-bind
717 (string n)
718 (read-from-string line t nil :START start)
719 (push string list)
720 (setq end n)))
721 (t
722 (setq end (or (position-if space-p line :START start) length))
723 (push (subseq line start end) list)))))
724
725 (defun tpl-unknown-command (command)
726 (tpl-format "Unknown top level command: ~s~%" command)
727 (values))
728
729 (defun tpl-pop-command (&rest any)
730 (declare (ignore any))
731 (throw (pop *quit-tags*) t))
732
733 (defun tpl-quit-command (&optional (level 0))
734 (when (and (>= level 0) (< level *tpl-level*))
735 (let ((x (nth (- *tpl-level* level 1) *quit-tags*)))
736 (throw x x)))
737 (tpl-print-current))
738
739 (defun tpl-previous (&optional (n 1))
740 (do ((i (si::ihs-prev *ihs-current*) (si::ihs-prev i)))
741 ((or (< i *ihs-base*) (<= n 0)))
742 (when (ihs-visible i)
743 (setq *ihs-current* i)
744 (decf n)))
745 (set-break-env)
746 (tpl-print-current))
747
748 (defun tpl-next (&optional (n 1))
749 (do ((i (si::ihs-next *ihs-current*) (si::ihs-next i)))
750 ((or (> i *ihs-top*) (<= n 0)))
751 (when (ihs-visible i)
752 (setq *ihs-current* i)
753 (decf n)))
754 (set-break-env)
755 (tpl-print-current))
756
757 (defun tpl-go (ihs-index)
758 (setq *ihs-current* (min (max ihs-index *ihs-base*) *ihs-top*))
759 (if (ihs-visible *ihs-current*)
760 (progn (set-break-env) (tpl-print-current))
761 (tpl-previous)))
762
763 (defun tpl-print-message ()
764 (when *break-message*
765 (tpl-format "~a~%" *break-message*))
766 (values))
767
768 (defun tpl-disassemble-command ()
769 (let*((*print-level* 2)
770 (*print-length* 16)
771 (*print-pretty* t)
772 (*print-escape* nil)
773 (*print-readably* nil)
774 (functions) (blocks) (variables))
775 (unless (si::bc-disassemble (ihs-fun *ihs-current*))
776 (tpl-print-current)
777 (tpl-format " Function cannot be disassembled.~%"))
778 (values)))
779
780 (defun tpl-lambda-expression-command ()
781 (let*(;;(*print-level* 2)
782 ;;(*print-length* 4)
783 ;;(*print-pretty* t)
784 ;;(*print-readably* nil)
785 (function (ihs-fun *ihs-current*))
786 (le (function-lambda-expression function)))
787 (if le
788 (pprint le *query-io*)
789 (tpl-format " No source code available for this function.~%"))
790 (values)))
791
792 (defun lambda-list-from-annotations (name)
793 (declare (si::c-local))
794 (let ((args (ext:get-annotation name :lambda-list nil)))
795 (values args (and args t))))
796
797 (defun function-lambda-list (function)
798 (cond
799 ((symbolp function)
800 (cond ((or (special-operator-p function)
801 (macro-function function))
802 (lambda-list-from-annotations function))
803 (t
804 (function-lambda-list (fdefinition function)))))
805 ((typep function 'generic-function)
806 (if (slot-boundp function 'clos::lambda-list)
807 (values (clos:generic-function-lambda-list function) t)
808 (values nil nil)))
809 ;; Use the lambda list from the function definition, if available,
810 ;; but remove &aux arguments.
811 ((let ((f (function-lambda-expression function)))
812 (when f
813 (let* ((list (if (eql (first f) 'LAMBDA)
814 (second f)
815 (third f)))
816 (ndx (position '&aux list)))
817 (return-from function-lambda-list
818 (values (if ndx (subseq list 0 (1- ndx)) list) t))))))
819 ;; If it's a compiled function of ECL itself, reconstruct the
820 ;; lambda-list from the annotation.
821 (t
822 (lambda-list-from-annotations (compiled-function-name function)))))
823
824 #-ecl-min
825 (defun decode-env-elt (env ndx)
826 (ffi:c-inline (env ndx :utf-8) (:object :fixnum :object) :object
827 "
828 cl_object v = #0;
829 cl_index ndx = #1;
830 typedef struct ecl_var_debug_info *pinfo;
831 pinfo d = (pinfo)(v->vector.self.t[1]) + ndx;
832 cl_object name;
833 #ifdef ECL_UNICODE
834 name = ecl_decode_from_cstring(d->name,-1,#2);
835 if (!name)
836 #endif
837 name = ecl_make_constant_base_string(d->name,-1);
838 void *value = (void*)(v->vector.self.t[2+ndx]);
839 cl_object output;
840 switch (d->type) {
841 case _ecl_object_loc:
842 output = *((cl_object*)value);
843 break;
844 case _ecl_fixnum_loc: {
845 cl_fixnum *p = (cl_fixnum*)value;
846 output = ecl_make_integer(*p);
847 break;
848 }
849 case _ecl_float_loc: {
850 float *p = (float*)value;
851 output = ecl_make_single_float(*p);
852 break;
853 }
854 case _ecl_double_loc: {
855 double *p = (double*)value;
856 output = ecl_make_double_float(*p);
857 break;
858 }
859 case _ecl_long_double_loc: {
860 long double *p = (long double*)value;
861 output = ecl_make_long_float(*p);
862 break;
863 }
864 #ifdef ECL_COMPLEX_FLOAT
865 case _ecl_csfloat_loc: {
866 _Complex float *p = (_Complex float*)value;
867 output = ecl_make_csfloat(*p);
868 break;
869 }
870 case _ecl_cdfloat_loc: {
871 _Complex double *p = (_Complex double*)value;
872 output = ecl_make_cdfloat(*p);
873 break;
874 }
875 case _ecl_clfloat_loc: {
876 _Complex long double *p = (_Complex long double*)value;
877 output = ecl_make_clfloat(*p);
878 break;
879 }
880 #endif
881 #ifdef ECL_SSE2
882 case _ecl_int_sse_pack_loc: {
883 __m128i *p = (__m128i*)value;
884 output = ecl_make_int_sse_pack(_mm_loadu_si128(p));
885 break;
886 }
887 case _ecl_float_sse_pack_loc: {
888 __m128 *p = (__m128*)value;
889 output = ecl_make_float_sse_pack(_mm_loadu_ps((float*)p));
890 break;
891 }
892 case _ecl_double_sse_pack_loc: {
893 __m128d *p = (__m128d*)value;
894 output = ecl_make_double_sse_pack(_mm_loadu_pd((double*)p));
895 break;
896 }
897 #endif
898 default: {
899 ecl_base_char *p = (ecl_base_char*)value;
900 output = ECL_CODE_CHAR(*p);
901 break;
902 }
903 }
904 @(return) = CONS(name,output);
905 " :one-liner nil))
906
907 (defun decode-ihs-env (*break-env*)
908 (let ((env *break-env*))
909 (if (vectorp env)
910 #+ecl-min
911 nil
912 #-ecl-min
913 (let* ((next (decode-ihs-env
914 (ffi:c-inline (env) (:object) :object
915 "(#0)->vector.self.t[0]" :one-liner t))))
916 (nreconc (loop with l = (- (length env) 2)
917 for i from 0 below l
918 do (push (decode-env-elt env i) next))
919 next))
920 env)))
921
922 (defun ihs-environment (ihs-index)
923 (labels ((newly-bound-special-variables (bds-min bds-max)
924 (loop for i from bds-min to bds-max
925 for variable = (bds-var i)
926 unless (member variable output :test #'eq)
927 collect variable into output
928 finally (return output)))
929 (special-variables-alist (ihs-index)
930 (let ((top (ihs-top)))
931 (unless (> ihs-index top)
932 (let* ((bds-min (1+ (ihs-bds ihs-index)))
933 (bds-top (bds-top))
934 (bds-max (if (= ihs-index top)
935 bds-top
936 (ihs-bds (1+ ihs-index))))
937 (variables (newly-bound-special-variables bds-min bds-max)))
938 (loop with output = '()
939 for i from (1+ bds-max) to bds-top
940 for var = (bds-var i)
941 when (member var variables :test #'eq)
942 do (setf variables (delete var variables)
943 output (acons var (bds-val i) output))
944 finally (return
945 (append (loop for v in variables
946 collect (cons v (symbol-value v)))
947 output)))))))
948 (extract-restarts (variables-alist)
949 (let ((record (assoc '*restart-clusters* variables-alist)))
950 (if record
951 (let* ((bindings (cdr record))
952 (new-bindings (first bindings)))
953 (values (delete record variables-alist) new-bindings))
954 (values variables-alist nil)))))
955 (let* ((functions '())
956 (blocks '())
957 (local-variables '())
958 (special-variables '())
959 (restarts '())
960 record0 record1)
961 (dolist (record (decode-ihs-env (ihs-env ihs-index)))
962 (cond ((atom record)
963 (push (compiled-function-name record) functions))
964 ((progn
965 (setf record0 (car record) record1 (cdr record))
966 (when (stringp record0)
967 (setf record0
968 (let ((*package* (find-package "KEYWORD")))
969 (with-standard-io-syntax
970 (read-from-string record0)))))
971 (or (symbolp record0) (stringp record0)))
972 (setq local-variables (acons record0 record1 local-variables)))
973 ((symbolp record1)
974 (push record1 blocks))
975 (t
976 )))
977 (multiple-value-bind (special-variables restarts)
978 (extract-restarts (special-variables-alist ihs-index))
979 (values (nreverse local-variables)
980 special-variables
981 functions blocks restarts)))))
982
983 (defun tpl-print-variables (prefix variables no-values)
984 ;; This format is what was in the orignal code.
985 ;; It simply does not work when no-values is t.
986 ;; If you care to debug this kind of conundrum then have fun!
987 ;;(tpl-format "Local variables: ~:[~:[none~;~:*~{~a~1*~:@{, ~a~1*~}~}~]~;~
988 ;; ~:[none~;~:*~{~% ~a: ~s~}~]~]~%"
989 ;; (not no-values) variables)
990 (tpl-format prefix)
991 (if variables
992 (loop for (var . value) in variables
993 do (if no-values
994 (tpl-format "~% ~S" var)
995 (tpl-format "~% ~S: ~S" var value)))
996 (tpl-format "(none)")))
997
998 (defun tpl-variables-command (&optional no-values)
999 (let*((*print-level* 2)
1000 (*print-length* 16)
1001 (*print-pretty* t)
1002 (*print-escape* nil)
1003 (*print-readably* nil))
1004 (multiple-value-bind (local-variables special-variables functions blocks restarts)
1005 (ihs-environment *ihs-current*)
1006 (tpl-format "~:[~;Local functions: ~:*~{~s~^, ~}.~%~]" functions)
1007 (tpl-format "~:[~;Block names: ~:*~{~s~^, ~}.~%~]" blocks)
1008 (when restarts
1009 (tpl-format "New restarts:")
1010 (loop for r in restarts
1011 do (tpl-format "~% ~A: ~A" (restart-name r) r)))
1012 (tpl-print-variables "~%Local variables: " local-variables no-values)
1013 (tpl-print-variables "~%Special variables: " special-variables no-values))
1014 (terpri *query-io*)
1015 (values)))
1016
1017 (defun tpl-inspect-command (var-name)
1018 (when (symbolp var-name)
1019 (setq var-name (symbol-name var-name)))
1020 (let ((val-pair (assoc var-name (decode-ihs-env *break-env*)
1021 :test #'(lambda (s1 s2)
1022 (when (symbolp s2) (setq s2 (symbol-name s2)))
1023 (if (stringp s2)
1024 (string-equal s1 s2)
1025 nil)))))
1026 (when val-pair
1027 ;;(tpl-format "~&In tpl-inspect-command: val-pair = ~S~%" val-pair)
1028 (let ((val (cdr val-pair)))
1029 (inspect val)))))
1030
1031 (defun tpl-bds-command (&optional var)
1032 (if var
1033 (do ((bi (1+ (frs-bds (max 0 (1- *frs-base*)))) (1+ bi))
1034 (last (frs-bds (1+ *frs-top*))))
1035 ((> bi last)
1036 (tpl-format "Variable not found.~%")
1037 (values))
1038 (when (eq (bds-var bi) var)
1039 (return (let ((val (bds-val bi)))
1040 (if (eq val si::unbound) "<unbound value>" val)))))
1041 (do ((bi (1+ (frs-bds (max 0 (1- *frs-base*)))) (1+ bi))
1042 (last (frs-bds (1+ *frs-top*)))
1043 (fi *frs-base*)
1044 (*print-level* 2)
1045 (*print-length* 16)
1046 (*print-pretty* t))
1047 ((> bi last) (values))
1048 (do ()
1049 ((or (> fi *frs-top*) (>= (frs-bds fi) bi)))
1050 (print-frs fi)
1051 (incf fi))
1052 (tpl-format "BDS[~d]: ~s = ~s~%"
1053 bi (bds-var bi)
1054 (let ((val (bds-val bi)))
1055 (if (eq val si::unbound) "<unbound value>" val))))))
1056
1057 (defun tpl-backtrace (&optional n)
1058 (let ((*print-pretty* nil) ;; because CLOS allows (setf foo) as function names
1059 (base *ihs-base*)
1060 (top *ihs-top*))
1061 (tpl-format "~&Backtrace:~%")
1062 (if (null n)
1063 (do ((i top (si::ihs-prev i))
1064 ;;(b nil t)
1065 )
1066 ((< i base))
1067 (when (ihs-visible i)
1068 (let ((*print-case* (if (= i *ihs-current*) :UPCASE :DOWNCASE))
1069 (*print-readably* nil)
1070 (func-name (ihs-fname i)))
1071 ;;(tpl-format "~:[~; >~] ~S" b (ihs-fname i)) ;; JCB
1072 (tpl-format " > ~S" func-name)
1073 (when (eq func-name 'si::bytecodes)
1074 (tpl-format " [Evaluation of: ~S]"
1075 (function-lambda-expression (ihs-fun i))))
1076 (terpri *query-io*))))
1077 (progn
1078 (if (eq t n)
1079 (setq base 0)
1080 (progn
1081 (unless (integerp n)
1082 (error "Argument to command :backtrace must be an integer or t."))
1083 (setq top *ihs-current*)))
1084 (do ((i top (si::ihs-prev i))
1085 ;;(b nil t)
1086 (j 0 (1+ j))
1087 (max (if (eq t n) *ihs-top* n))
1088 )
1089 ((or (< i base) (>= j max))
1090 (when (zerop i)
1091 (tpl-format " > ---end-of-stack---~%")))
1092 (when (or (ihs-visible i) (eq t n))
1093 (let ((*print-case* (if (= i *ihs-current*) :UPCASE :DOWNCASE))
1094 (*print-readably* nil)
1095 (func-name (ihs-fname i)))
1096 ;;(tpl-format "~:[~; >~] ~S" b (ihs-fname i)) ;; JCB
1097 (tpl-format " > ~S" (ihs-fname i))
1098 (when (eq func-name 'si::bytecodes)
1099 (tpl-format " [Evaluation of: ~S]"
1100 (function-lambda-expression (ihs-fun i))))
1101 (terpri *query-io*))))))
1102 (terpri *query-io*))
1103 (values))
1104
1105 (defun tpl-frs-command (&optional n)
1106 (unless n
1107 (setq n *ihs-top*))
1108 (unless (integerp n)
1109 (error "Argument to command :frs must be an integer."))
1110 (do ((i *ihs-top* (si::ihs-prev i))
1111 (k n (1- k)))
1112 ((= k 0) (values))
1113 (let*((j (or (sch-frs-base *frs-base* i) (1+ *frs-top*)))
1114 (*print-level* 2)
1115 (*print-length* 16)
1116 (*print-pretty* t))
1117 (do () ((or (> j *frs-top*) (> (frs-ihs j) i)))
1118 (print-frs j)
1119 (incf j)))))
1120
1121 (defun print-frs (i)
1122 (tpl-format " FRS[~d]: ---> IHS[~d],BDS[~d]~%"
1123 i (frs-ihs i) (frs-bds i)))
1124
1125 (defun break-where ()
1126 (if (<= *tpl-level* 0)
1127 #-threads (tpl-format "~&Top level.~%")
1128 #+threads (tpl-format "~&Top level in: ~A.~%" mp:*current-process*)
1129 (tpl-print-current)))
1130
1131 (defun tpl-print-current ()
1132 (let ((*print-readably* nil)
1133 (name (ihs-fname *ihs-current*)))
1134 (tpl-format "~&Broken at ~:@(~S~)." name)
1135 (when (eq name 'si::bytecodes)
1136 (tpl-format " [Evaluation of: ~S]"
1137 (function-lambda-expression (ihs-fun *ihs-current*)))))
1138 #-threads (terpri *query-io*)
1139 #+threads (tpl-format " In: ~A.~%" mp:*current-process*)
1140 (let ((fun (ihs-fun *ihs-current*)))
1141 (when (and (symbolp fun) (fboundp fun))
1142 (setf fun (fdefinition fun)))
1143 (multiple-value-bind (file position)
1144 (ext:compiled-function-file fun)
1145 (when file
1146 (tpl-format " File: ~S (Position #~D)~%" file position))))
1147 (values))
1148
1149 (defun tpl-hide (fname)
1150 (unless (member fname *break-hidden-functions* :test #'eq)
1151 (push fname *break-hidden-functions*)
1152 (unless (ihs-visible *ihs-current*)
1153 (set-current-ihs)))
1154 (values))
1155
1156 (defun tpl-unhide (fname)
1157 (setq *break-hidden-functions*
1158 (delete fname *break-hidden-functions* :test #'eq))
1159 (values))
1160
1161 (defun tpl-unhide-package (package)
1162 (setq *break-hidden-packages*
1163 (delete (find-package package) *break-hidden-packages* :test #'eq))
1164 (values))
1165
1166 (defun tpl-unhide-all ()
1167 (setq *break-hidden-functions* nil)
1168 (setq *break-hidden-packages* nil)
1169 (values))
1170
1171 (defun tpl-hide-package (package)
1172 (setq package (find-package package))
1173 (unless (member package *break-hidden-packages* :test #'eq)
1174 (push package *break-hidden-packages*)
1175 (unless (ihs-visible *ihs-current*)
1176 (set-current-ihs)))
1177 (values))
1178
1179 (defun ihs-visible (i)
1180 (let ((fname (ihs-fname i)))
1181 #+clos
1182 (when (and (consp fname) (eq 'SETF (car fname)))
1183 (setq fname (second fname)))
1184 (or (eq fname 'EVAL)
1185 (eq fname 'BYTECODES)
1186 (and (not (member (symbol-package fname) *break-hidden-packages*
1187 :TEST #'eq))
1188 (not (null fname))
1189 (not (member fname *break-hidden-functions* :TEST #'eq))))))
1190
1191 (defun ihs-fname (i)
1192 (let ((function (ihs-fun i)))
1193 (cond ((symbolp function) function)
1194 ((compiled-function-p function)
1195 (or (compiled-function-name function) 'lambda))
1196 #+clos
1197 ((si:instancep function) (slot-value function 'name))
1198 (t :zombi))))
1199
1200 (defun set-current-ihs ()
1201 (do ((i *ihs-current* (si::ihs-prev i)))
1202 ((or (and (ihs-visible i) (setq *ihs-current* i))
1203 (<= i *ihs-base*))))
1204 (set-break-env))
1205
1206 (defun set-break-env ()
1207 (setq *break-env* (ihs-env *ihs-current*)))
1208
1209 (defun ihs-search (string unrestricted &optional (start (si::ihs-top)))
1210 (do ((ihs start (si::ihs-prev ihs)))
1211 ((< ihs *ihs-base*)
1212 (return nil))
1213 (when (and (or unrestricted (ihs-visible ihs))
1214 (search (string string) (symbol-name (ihs-fname ihs))
1215 :test #'char-equal))
1216 (return ihs))))
1217
1218 (defun tpl-backward-search (string)
1219 (let ((new-ihs (ihs-search string nil *ihs-current*)))
1220 (cond (new-ihs
1221 (setf *ihs-current* new-ihs)
1222 (set-current-ihs)
1223 (tpl-print-current))
1224 (t
1225 (tpl-format "Search for ~a failed.~%" string)))
1226 (values)))
1227
1228 (defun tpl-forward-search (string)
1229 (do ((ihs (si::ihs-next *ihs-current*) (si::ihs-next ihs)))
1230 ((> ihs *ihs-top*)
1231 (tpl-format "Search for ~a failed.~%" string))
1232 (when (and (ihs-visible ihs)
1233 (search string (symbol-name (ihs-fname ihs))
1234 :test #'char-equal))
1235 (setq *ihs-current* ihs)
1236 (set-current-ihs)
1237 (tpl-print-current)
1238 (return)))
1239 (values))
1240
1241 (defun tpl-apropos-command (&optional string pkg)
1242 (when string (apropos string pkg)))
1243
1244 (defun tpl-document-command (&optional symbol)
1245 (when symbol (help symbol)))
1246
1247 (defun tpl-step-command (&optional form)
1248 (when form (step* form)))
1249
1250 (defun tpl-trace-command (&rest functions)
1251 (trace* functions))
1252
1253 (defun tpl-untrace-command (&rest functions)
1254 (untrace* functions))
1255
1256 (defparameter *tpl-last-load* nil)
1257
1258 (defun tpl-load-command (&rest files)
1259 (when files
1260 (setq *tpl-last-load* files))
1261 (dolist (file *tpl-last-load*) (load file))
1262 *tpl-last-load*)
1263
1264 (defparameter *tpl-last-compile* nil)
1265
1266 (defun tpl-compile-command (&rest files)
1267 (when files
1268 (setq *tpl-last-compile* files))
1269 (dolist (file *tpl-last-compile*) (compile-file file))
1270 (setq *tpl-last-load* *tpl-last-compile*))
1271
1272 (defun tpl-help-command (&optional topic)
1273 (cond ((null topic)
1274 (dolist (commands *tpl-commands*)
1275 (tpl-format "~%~A:~%" (car commands))
1276 (dolist (c (cdr commands))
1277 (when (fourth c)
1278 (tpl-format "~A.~%" (fourth c))))))
1279 ((or (stringp topic) (symbolp topic))
1280 (let (c)
1281 (setq topic (intern (string topic) (find-package 'keyword)))
1282 (dolist (commands *tpl-commands*)
1283 (when (setq c (assoc topic (cdr commands) :test #'member))
1284 (return)))
1285 (cond ((null (fifth c))
1286 (tpl-format "No such help topic: ~s~%"
1287 (string topic)))
1288 (t
1289 (terpri *query-io*)
1290 (tpl-format (fifth c))
1291 (terpri *query-io*)))))
1292 (t
1293 (tpl-format "Not a valid help topic: ~s~%" topic)))
1294 (values))
1295
1296 (defun tpl-help-stack-command ()
1297 (tpl-format "
1298 Use the following functions to directly access ECL stacks.
1299
1300 Invocation History Stack:
1301 (SYS:IHS-TOP) Returns the index of the TOP of the IHS.
1302 (SYS:IHS-FUN i) Returns the function of the i-th entity in IHS.
1303 (SYS:IHS-ENV i)
1304 (SYS:IHS-PREV i)
1305 (SYS:IHS-NEXT i)
1306
1307 Frame (catch, block) Stack:
1308 (SYS:FRS-TOP) Returns the index of the TOP of the FRS.
1309 (SYS:FRS-BDS i) Returns the BDS index of the i-th entity in FRS.
1310 (SYS:FRS-IHS i) Returns the IHS index of the i-th entity in FRS.
1311 (SYS:FRS-TAG i)
1312
1313 Binding Stack:
1314 (SYS:BDS-TOP) Returns the index of the TOP of the BDS.
1315 (SYS:BDS-VAR i) Returns the symbol of the i-th entity in BDS.
1316 (SYS:BDS-VAL i) Returns the value of the i-th entity in BDS.
1317
1318 Note that these functions are named by external symbols in the SYSTEM
1319 package."
1320 ))
1321
1322 (defun compute-restart-commands (condition &key display)
1323 (let ((restarts (compute-restarts condition))
1324 (restart-commands (list "Restart commands")))
1325 (when display
1326 (format display (if restarts
1327 "~&Available restarts:~2%"
1328 "~&No restarts available.~%")))
1329 (loop for restart in restarts
1330 and i from 1
1331 do (let ((user-command (format nil "r~D" i))
1332 (name (format nil "~@[(~A)~]" (restart-name restart)))
1333 (helpstring (princ-to-string restart)))
1334 (push (list
1335 (list (intern (string-upcase user-command) :keyword))
1336 restart :restart
1337 (format nil ":~A~16T~A~24T~A" user-command helpstring name)
1338 (format nil ":~A~48T~A~& ~&~A~A" (string-downcase user-command)
1339 "[Restart command]" name helpstring))
1340 restart-commands)
1341 (when display
1342 (format display "~D. ~A ~A~%" i name restart))))
1343 (when display
1344 (terpri display))
1345 (nreverse restart-commands)))
1346
1347 (defun update-debug-commands (restart-commands)
1348 (let ((commands (copy-list *tpl-commands*)))
1349 (unless (member break-commands commands)
1350 (setq commands (nconc commands (list break-commands))))
1351 (delete-if
1352 #'(lambda (x) (string= "Restart commands" (car x)))
1353 commands)
1354 (nconc commands (list restart-commands))))
1355
1356 (defparameter *default-debugger-maximum-depth* 16)
1357
1358 (defun check-default-debugger-runaway ()
1359 (when (< *default-debugger-maximum-depth* *break-level*)
1360 #+threads
1361 (progn
1362 (tpl-format
1363 "~&Excessive debugger depth! Probable infinite recursion!~%~
1364 Quitting process: ~A.~%" mp:*current-process*)
1365 (when (< (+ *default-debugger-maximum-depth* 3) *break-level*)
1366 ;; we tried to be polite but it does not seem to work.
1367 (quit -1))
1368 (exit-process))
1369 #-threads
1370 (progn
1371 (tpl-format
1372 "~&Excessive debugger depth! Probable infinite recursion!~%~
1373 Quitting.~%")
1374 (quit -1))))
1375
1376 #+threads
1377 (defun tpl-switch-command (&optional rank)
1378 (let (elect)
1379 (cond ((integerp rank)
1380 (let ((max (list-length *console-waiting-list*)))
1381 (unless (and (< 0 rank) (<= rank max))
1382 (error "Debugger switch command: Invalid argument value.")))
1383 (setf elect (elt *console-waiting-list* (1- rank))))
1384 (t
1385 (setf elect (find rank *console-waiting-list* :key #'mp:process-name))))
1386 (when elect
1387 (setq *console-owner* elect)))
1388 (values))
1389
1390 #+threads
1391 (defun tpl-waiting-command ()
1392 (tpl-format "~&~%Debugger's waiting list:~2%")
1393 (loop for process in *console-waiting-list*
1394 for rank from 1
1395 do (tpl-format (if (eq process mp:*current-process*)
1396 " >~D: ~A~%"
1397 " ~D: ~A~%")
1398 rank process))
1399 (terpri *query-io*)
1400 (values))
1401
1402 (defun default-debugger (condition)
1403 (with-standard-io-syntax
1404 (let* ((*query-io* *debug-io*)
1405 ;;(*tpl-prompt-hook* "[dbg] ")
1406 (*print-readably* nil)
1407 (*print-pretty* nil)
1408 (*print-circle* t)
1409 (*print-length* 16)
1410 (*readtable* (or *break-readtable* *readtable*))
1411 (*break-message* (format nil "~&Condition of type: ~A~%~A~%"
1412 (type-of condition) condition))
1413 (*break-condition* condition)
1414 (*break-level* (1+ *break-level*))
1415 (break-level *break-level*)
1416 (*break-env* nil))
1417 (check-default-debugger-runaway)
1418 #+threads
1419 ;; We give our process priority for grabbing the console.
1420 (setq *console-owner* mp:*current-process*)
1421 ;; As of ECL 9.4.1 making a normal function return from the debugger
1422 ;; seems to be a very bad idea! Basically, it dumps core...
1423 (ignore-errors
1424 (when (listen *query-io*)
1425 (clear-input *query-io*)))
1426 ;; All I/O in the debugger (including errors and tpl interactions) are
1427 ;; performed through *QUERY-IO* (that is bound to *DEBUG-IO*).
1428 (ignore-errors (finish-output *query-io*))
1429 ;; We wrap the following in `ignore-errors' because error may be caused by
1430 ;; writing to the `*query-io*', what leads to infinite recursion!
1431 (ignore-errors (tpl-format "~&~%~a" *break-message*))
1432 (loop
1433 ;; Here we show a list of restarts and invoke the toplevel with
1434 ;; an extended set of commands which includes invoking the associated
1435 ;; restarts.
1436 (let* ((restart-commands
1437 (compute-restart-commands condition :display *query-io*))
1438 (debug-commands
1439 ;;(adjoin restart-commands (adjoin break-commands *tpl-commands*))
1440 (update-debug-commands restart-commands)))
1441 (tpl :commands debug-commands))))))
1442
1443 (defun invoke-debugger (condition)
1444 ;; call *INVOKE-DEBUGGER-HOOK* first, so that *DEBUGGER-HOOK* is not
1445 ;; called when the debugger is disabled. We adopt this mechanism
1446 ;; from SBCL.
1447 #-ecl-min
1448 (declare (c::policy-debug-ihs-frame))
1449 (let ((old-hook *invoke-debugger-hook*))
1450 (when old-hook
1451 (let ((*invoke-debugger-hook* nil))
1452 (funcall old-hook condition old-hook))))
1453 (let* ((old-hook *debugger-hook*))
1454 (when old-hook
1455 (let ((*debugger-hook* nil))
1456 (funcall old-hook condition old-hook))))
1457 (locally
1458 (declare (notinline default-debugger))
1459 (if (<= 0 *tpl-level*) ;; Do we have a top-level REPL above us?
1460 (default-debugger condition)
1461 (let* (;; We do not have a si::top-level invocation above us
1462 ;; so we have to provide the environment for interactive use.
1463 (*invoke-debugger-hook* *invoke-debugger-hook*)
1464 (*debugger-hook* *debugger-hook*)
1465 (*ihs-top* *ihs-top*) ;; Or should it be 1?
1466 (*tpl-level* *tpl-level*) ;; Or should we simply say 0.
1467 (*tpl-commands* *tpl-commands*)
1468 + ++ +++ - * ** *** / // ///)
1469 (default-debugger condition))))
1470 (finish-output *query-io*))
1471
1472 (defun safe-eval (form env &optional (err-value nil err-value-p))
1473 "Args: (FORM ENV &optional ERR-VALUE)
1474 Evaluates FORM in the given environment, which may be NIL. If the form
1475 signals an error, or tries to jump to an outer point, the function has two
1476 choices: by default, it will invoke a debugger, but if a third value is
1477 supplied, then SAFE-EVAL will not use a debugger but rather return that
1478 value."
1479 (let ((output nil) (ok nil))
1480 (unwind-protect
1481 (handler-bind ((serious-condition
1482 (if err-value-p
1483 #'(lambda (condition)
1484 (declare (ignore condition))
1485 (return-from safe-eval err-value))
1486 #'invoke-debugger)))
1487 (setf output (si::eval-with-env form env)
1488 ok t))
1489 (return-from safe-eval (if ok output err-value)))))