/[slime]/slime/swank-backend.lisp
ViewVC logotype

Contents of /slime/swank-backend.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.186 - (show annotations)
Thu Dec 10 22:21:09 2009 UTC (4 years, 4 months ago) by trittweiler
Branch: MAIN
Changes since 1.185: +20 -4 lines
	* swank-backend.lisp (*debug-swank-backend*): New variable. If
	true, backends should not catch internal errors (e.g. during
	definition finding), and should not perform backtrace magic.
	(make-error-location): New helper.
	(find-definitions [interface]): Default to error location.

	* swank-sbcl.lisp (converting-errors-to-location): New helper
	macro. Regards new *DEBUG-SWANK-BACKEND*.
	(find-definitions [implementation]): Use it.
	(find-source-location [implementation]): Ditto.
	(functiond-spec): Ditto.
	(frame-source-location [implementation]): Ditto.
	(*debug-definition-finding*): Removed.
	(make-source-location-specification): Removed.
	(safe-function-source-location): Removed.
	(safe-source-location-for-emacs): Removed. Not needed anymore.

	(call-with-debugging-environment): Do not perform stack hinting
	depending on *DEBUG-SWANK-BACKEND*.
1 ;;; -*- Mode: lisp; indent-tabs-mode: nil; outline-regexp: ";;;;;*" -*-
2 ;;;
3 ;;; slime-backend.lisp --- SLIME backend interface.
4 ;;;
5 ;;; Created by James Bielman in 2003. Released into the public domain.
6 ;;;
7 ;;;; Frontmatter
8 ;;;
9 ;;; This file defines the functions that must be implemented
10 ;;; separately for each Lisp. Each is declared as a generic function
11 ;;; for which swank-<implementation>.lisp provides methods.
12
13 (defpackage :swank-backend
14 (:use :common-lisp)
15 (:export #:*debug-swank-backend*
16 #:sldb-condition
17 #:compiler-condition
18 #:original-condition
19 #:message
20 #:source-context
21 #:condition
22 #:severity
23 #:with-compilation-hooks
24 #:location
25 #:location-p
26 #:location-buffer
27 #:location-position
28 #:position-p
29 #:position-pos
30 #:print-output-to-string
31 #:quit-lisp
32 #:references
33 #:unbound-slot-filler
34 #:declaration-arglist
35 #:type-specifier-arglist
36 #:with-struct
37 ;; interrupt macro for the backend
38 #:*pending-slime-interrupts*
39 #:check-slime-interrupts
40 #:*interrupt-queued-handler*
41 ;; inspector related symbols
42 #:emacs-inspect
43 #:label-value-line
44 #:label-value-line*
45 ))
46
47 (defpackage :swank-mop
48 (:use)
49 (:export
50 ;; classes
51 #:standard-generic-function
52 #:standard-slot-definition
53 #:standard-method
54 #:standard-class
55 #:eql-specializer
56 #:eql-specializer-object
57 ;; standard-class readers
58 #:class-default-initargs
59 #:class-direct-default-initargs
60 #:class-direct-slots
61 #:class-direct-subclasses
62 #:class-direct-superclasses
63 #:class-finalized-p
64 #:class-name
65 #:class-precedence-list
66 #:class-prototype
67 #:class-slots
68 #:specializer-direct-methods
69 ;; generic function readers
70 #:generic-function-argument-precedence-order
71 #:generic-function-declarations
72 #:generic-function-lambda-list
73 #:generic-function-methods
74 #:generic-function-method-class
75 #:generic-function-method-combination
76 #:generic-function-name
77 ;; method readers
78 #:method-generic-function
79 #:method-function
80 #:method-lambda-list
81 #:method-specializers
82 #:method-qualifiers
83 ;; slot readers
84 #:slot-definition-allocation
85 #:slot-definition-documentation
86 #:slot-definition-initargs
87 #:slot-definition-initform
88 #:slot-definition-initfunction
89 #:slot-definition-name
90 #:slot-definition-type
91 #:slot-definition-readers
92 #:slot-definition-writers
93 #:slot-boundp-using-class
94 #:slot-value-using-class
95 #:slot-makunbound-using-class
96 ;; generic function protocol
97 #:compute-applicable-methods-using-classes
98 #:finalize-inheritance))
99
100 (in-package :swank-backend)
101
102
103 ;;;; Metacode
104
105 (defparameter *debug-swank-backend* nil
106 "If this is true, backends should not catch errors but enter the
107 debugger where appropriate. Also, they should not perform backtrace
108 magic but really show every frame including SWANK related ones.")
109
110 (defparameter *interface-functions* '()
111 "The names of all interface functions.")
112
113 (defparameter *unimplemented-interfaces* '()
114 "List of interface functions that are not implemented.
115 DEFINTERFACE adds to this list and DEFIMPLEMENTATION removes.")
116
117 (defmacro definterface (name args documentation &rest default-body)
118 "Define an interface function for the backend to implement.
119 A function is defined with NAME, ARGS, and DOCUMENTATION. This
120 function first looks for a function to call in NAME's property list
121 that is indicated by 'IMPLEMENTATION; failing that, it looks for a
122 function indicated by 'DEFAULT. If neither is present, an error is
123 signaled.
124
125 If a DEFAULT-BODY is supplied, then a function with the same body and
126 ARGS will be added to NAME's property list as the property indicated
127 by 'DEFAULT.
128
129 Backends implement these functions using DEFIMPLEMENTATION."
130 (check-type documentation string "a documentation string")
131 (assert (every #'symbolp args) ()
132 "Complex lambda-list not supported: ~S ~S" name args)
133 (labels ((gen-default-impl ()
134 `(setf (get ',name 'default) (lambda ,args ,@default-body)))
135 (args-as-list (args)
136 (destructuring-bind (req opt key rest) (parse-lambda-list args)
137 `(,@req ,@opt
138 ,@(loop for k in key append `(,(kw k) ,k))
139 ,@(or rest '(())))))
140 (parse-lambda-list (args)
141 (parse args '(&optional &key &rest)
142 (make-array 4 :initial-element nil)))
143 (parse (args keywords vars)
144 (cond ((null args)
145 (reverse (map 'list #'reverse vars)))
146 ((member (car args) keywords)
147 (parse (cdr args) (cdr (member (car args) keywords)) vars))
148 (t (push (car args) (aref vars (length keywords)))
149 (parse (cdr args) keywords vars))))
150 (kw (s) (intern (string s) :keyword)))
151 `(progn
152 (defun ,name ,args
153 ,documentation
154 (let ((f (or (get ',name 'implementation)
155 (get ',name 'default))))
156 (cond (f (apply f ,@(args-as-list args)))
157 (t (error "~S not implemented" ',name)))))
158 (pushnew ',name *interface-functions*)
159 ,(if (null default-body)
160 `(pushnew ',name *unimplemented-interfaces*)
161 (gen-default-impl))
162 ;; see <http://www.franz.com/support/documentation/6.2/doc/pages/variables/compiler/s_cltl1-compile-file-toplevel-compatibility-p_s.htm>
163 (eval-when (:compile-toplevel :load-toplevel :execute)
164 (export ',name :swank-backend))
165 ',name)))
166
167 (defmacro defimplementation (name args &body body)
168 (assert (every #'symbolp args) ()
169 "Complex lambda-list not supported: ~S ~S" name args)
170 `(progn
171 (setf (get ',name 'implementation) (lambda ,args ,@body))
172 (if (member ',name *interface-functions*)
173 (setq *unimplemented-interfaces*
174 (remove ',name *unimplemented-interfaces*))
175 (warn "DEFIMPLEMENTATION of undefined interface (~S)" ',name))
176 ',name))
177
178 (defun warn-unimplemented-interfaces ()
179 "Warn the user about unimplemented backend features.
180 The portable code calls this function at startup."
181 (let ((*print-pretty* t))
182 (warn "These Swank interfaces are unimplemented:~% ~:<~{~A~^ ~:_~}~:>"
183 (list (sort (copy-list *unimplemented-interfaces*) #'string<)))))
184
185 (defun import-to-swank-mop (symbol-list)
186 (dolist (sym symbol-list)
187 (let* ((swank-mop-sym (find-symbol (symbol-name sym) :swank-mop)))
188 (when swank-mop-sym
189 (unintern swank-mop-sym :swank-mop))
190 (import sym :swank-mop)
191 (export sym :swank-mop))))
192
193 (defun import-swank-mop-symbols (package except)
194 "Import the mop symbols from PACKAGE to SWANK-MOP.
195 EXCEPT is a list of symbol names which should be ignored."
196 (do-symbols (s :swank-mop)
197 (unless (member s except :test #'string=)
198 (let ((real-symbol (find-symbol (string s) package)))
199 (assert real-symbol () "Symbol ~A not found in package ~A" s package)
200 (unintern s :swank-mop)
201 (import real-symbol :swank-mop)
202 (export real-symbol :swank-mop)))))
203
204 (defvar *gray-stream-symbols*
205 '(:fundamental-character-output-stream
206 :stream-write-char
207 :stream-write-string
208 :stream-fresh-line
209 :stream-force-output
210 :stream-finish-output
211 :fundamental-character-input-stream
212 :stream-read-char
213 :stream-peek-char
214 :stream-read-line
215 ;; STREAM-FILE-POSITION is not available on all implementations, or
216 ;; partially under a different name.
217 ; :stream-file-posiion
218 :stream-listen
219 :stream-unread-char
220 :stream-clear-input
221 :stream-line-column
222 :stream-read-char-no-hang
223 ;; STREAM-LINE-LENGTH is an extension to gray streams that's apparently
224 ;; supported by CMUCL, OpenMCL, SBCL and SCL.
225 #+(or cmu openmcl sbcl scl)
226 :stream-line-length))
227
228 (defun import-from (package symbol-names &optional (to-package *package*))
229 "Import the list of SYMBOL-NAMES found in the package PACKAGE."
230 (dolist (name symbol-names)
231 (multiple-value-bind (symbol found) (find-symbol (string name) package)
232 (assert found () "Symbol ~A not found in package ~A" name package)
233 (import symbol to-package))))
234
235
236 ;;;; Utilities
237
238 (defmacro with-struct ((conc-name &rest names) obj &body body)
239 "Like with-slots but works only for structs."
240 (flet ((reader (slot) (intern (concatenate 'string
241 (symbol-name conc-name)
242 (symbol-name slot))
243 (symbol-package conc-name))))
244 (let ((tmp (gensym "OO-")))
245 ` (let ((,tmp ,obj))
246 (symbol-macrolet
247 ,(loop for name in names collect
248 (typecase name
249 (symbol `(,name (,(reader name) ,tmp)))
250 (cons `(,(first name) (,(reader (second name)) ,tmp)))
251 (t (error "Malformed syntax in WITH-STRUCT: ~A" name))))
252 ,@body)))))
253
254 (defun with-symbol (name package)
255 "Generate a form suitable for testing with #+."
256 (if (find-symbol (string name) (string package))
257 '(:and)
258 '(:or)))
259
260
261 ;;;; TCP server
262
263 (definterface create-socket (host port)
264 "Create a listening TCP socket on interface HOST and port PORT .")
265
266 (definterface local-port (socket)
267 "Return the local port number of SOCKET.")
268
269 (definterface close-socket (socket)
270 "Close the socket SOCKET.")
271
272 (definterface accept-connection (socket &key external-format
273 buffering timeout)
274 "Accept a client connection on the listening socket SOCKET.
275 Return a stream for the new connection.")
276
277 (definterface add-sigio-handler (socket fn)
278 "Call FN whenever SOCKET is readable.")
279
280 (definterface remove-sigio-handlers (socket)
281 "Remove all sigio handlers for SOCKET.")
282
283 (definterface add-fd-handler (socket fn)
284 "Call FN when Lisp is waiting for input and SOCKET is readable.")
285
286 (definterface remove-fd-handlers (socket)
287 "Remove all fd-handlers for SOCKET.")
288
289 (definterface preferred-communication-style ()
290 "Return one of the symbols :spawn, :sigio, :fd-handler, or NIL."
291 nil)
292
293 (definterface set-stream-timeout (stream timeout)
294 "Set the 'stream 'timeout. The timeout is either the real number
295 specifying the timeout in seconds or 'nil for no timeout."
296 (declare (ignore stream timeout))
297 nil)
298
299 ;;; Base condition for networking errors.
300 (define-condition network-error (simple-error) ())
301
302 (definterface emacs-connected ()
303 "Hook called when the first connection from Emacs is established.
304 Called from the INIT-FN of the socket server that accepts the
305 connection.
306
307 This is intended for setting up extra context, e.g. to discover
308 that the calling thread is the one that interacts with Emacs."
309 nil)
310
311
312 ;;;; Unix signals
313
314 (defconstant +sigint+ 2)
315
316 (definterface getpid ()
317 "Return the (Unix) process ID of this superior Lisp.")
318
319 (definterface install-sigint-handler (function)
320 "Call FUNCTION on SIGINT (instead of invoking the debugger).
321 Return old signal handler."
322 (declare (ignore function))
323 nil)
324
325 (definterface call-with-user-break-handler (handler function)
326 "Install the break handler HANDLER while executing FUNCTION."
327 (let ((old-handler (install-sigint-handler handler)))
328 (unwind-protect (funcall function)
329 (install-sigint-handler old-handler))))
330
331 (definterface quit-lisp ()
332 "Exit the current lisp image.")
333
334 (definterface lisp-implementation-type-name ()
335 "Return a short name for the Lisp implementation."
336 (lisp-implementation-type))
337
338
339 ;; pathnames are sooo useless
340
341 (definterface filename-to-pathname (filename)
342 "Return a pathname for FILENAME.
343 A filename in Emacs may for example contain asterisks which should not
344 be translated to wildcards."
345 (parse-namestring filename))
346
347 (definterface pathname-to-filename (pathname)
348 "Return the filename for PATHNAME."
349 (namestring pathname))
350
351 (definterface default-directory ()
352 "Return the default directory."
353 (directory-namestring (truename *default-pathname-defaults*)))
354
355 (definterface set-default-directory (directory)
356 "Set the default directory.
357 This is used to resolve filenames without directory component."
358 (setf *default-pathname-defaults* (truename (merge-pathnames directory)))
359 (default-directory))
360
361
362 (definterface call-with-syntax-hooks (fn)
363 "Call FN with hooks to handle special syntax."
364 (funcall fn))
365
366 (definterface default-readtable-alist ()
367 "Return a suitable initial value for SWANK:*READTABLE-ALIST*."
368 '())
369
370
371 ;;;; Compilation
372
373 (definterface call-with-compilation-hooks (func)
374 "Call FUNC with hooks to record compiler conditions.")
375
376 (defmacro with-compilation-hooks ((&rest ignore) &body body)
377 "Execute BODY as in CALL-WITH-COMPILATION-HOOKS."
378 (declare (ignore ignore))
379 `(call-with-compilation-hooks (lambda () (progn ,@body))))
380
381 (definterface swank-compile-string (string &key buffer position filename
382 policy)
383 "Compile source from STRING.
384 During compilation, compiler conditions must be trapped and
385 resignalled as COMPILER-CONDITIONs.
386
387 If supplied, BUFFER and POSITION specify the source location in Emacs.
388
389 Additionally, if POSITION is supplied, it must be added to source
390 positions reported in compiler conditions.
391
392 If FILENAME is specified it may be used by certain implementations to
393 rebind *DEFAULT-PATHNAME-DEFAULTS* which may improve the recording of
394 source information.
395
396 If POLICY is supplied, and non-NIL, it may be used by certain
397 implementations to compile with a debug optimization quality of its
398 value.
399
400 Should return T on successfull compilation, NIL otherwise.
401 ")
402
403 (definterface swank-compile-file (input-file output-file load-p
404 external-format)
405 "Compile INPUT-FILE signalling COMPILE-CONDITIONs.
406 If LOAD-P is true, load the file after compilation.
407 EXTERNAL-FORMAT is a value returned by find-external-format or
408 :default.
409
410 Should return OUTPUT-TRUENAME, WARNINGS-P and FAILURE-p
411 like `compile-file'")
412
413 (deftype severity ()
414 '(member :error :read-error :warning :style-warning :note :redefinition))
415
416 ;; Base condition type for compiler errors, warnings and notes.
417 (define-condition compiler-condition (condition)
418 ((original-condition
419 ;; The original condition thrown by the compiler if appropriate.
420 ;; May be NIL if a compiler does not report using conditions.
421 :type (or null condition)
422 :initarg :original-condition
423 :accessor original-condition)
424
425 (severity :type severity
426 :initarg :severity
427 :accessor severity)
428
429 (message :initarg :message
430 :accessor message)
431
432 ;; Macro expansion history etc. which may be helpful in some cases
433 ;; but is often very verbose.
434 (source-context :initarg :source-context
435 :type (or null string)
436 :initform nil
437 :accessor source-context)
438
439 (references :initarg :references
440 :initform nil
441 :accessor references)
442
443 (location :initarg :location
444 :accessor location)))
445
446 (definterface find-external-format (coding-system)
447 "Return a \"external file format designator\" for CODING-SYSTEM.
448 CODING-SYSTEM is Emacs-style coding system name (a string),
449 e.g. \"latin-1-unix\"."
450 (if (equal coding-system "iso-latin-1-unix")
451 :default
452 nil))
453
454 (definterface guess-external-format (pathname)
455 "Detect the external format for the file with name pathname.
456 Return nil if the file contains no special markers."
457 ;; Look for a Emacs-style -*- coding: ... -*- or Local Variable: section.
458 (with-open-file (s pathname :if-does-not-exist nil
459 :external-format (or (find-external-format "latin-1-unix")
460 :default))
461 (if s
462 (or (let* ((line (read-line s nil))
463 (p (search "-*-" line)))
464 (when p
465 (let* ((start (+ p (length "-*-")))
466 (end (search "-*-" line :start2 start)))
467 (when end
468 (%search-coding line start end)))))
469 (let* ((len (file-length s))
470 (buf (make-string (min len 3000))))
471 (file-position s (- len (length buf)))
472 (read-sequence buf s)
473 (let ((start (search "Local Variables:" buf :from-end t))
474 (end (search "End:" buf :from-end t)))
475 (and start end (< start end)
476 (%search-coding buf start end))))))))
477
478 (defun %search-coding (str start end)
479 (let ((p (search "coding:" str :start2 start :end2 end)))
480 (when p
481 (incf p (length "coding:"))
482 (loop while (and (< p end)
483 (member (aref str p) '(#\space #\tab)))
484 do (incf p))
485 (let ((end (position-if (lambda (c) (find c '(#\space #\tab #\newline)))
486 str :start p)))
487 (find-external-format (subseq str p end))))))
488
489
490 ;;;; Streams
491
492 (definterface make-output-stream (write-string)
493 "Return a new character output stream.
494 The stream calls WRITE-STRING when output is ready.")
495
496 (definterface make-input-stream (read-string)
497 "Return a new character input stream.
498 The stream calls READ-STRING when input is needed.")
499
500
501 ;;;; Documentation
502
503 (definterface arglist (name)
504 "Return the lambda list for the symbol NAME. NAME can also be
505 a lisp function object, on lisps which support this.
506
507 The result can be a list or the :not-available keyword if the
508 arglist cannot be determined."
509 (declare (ignore name))
510 :not-available)
511
512 (defgeneric declaration-arglist (decl-identifier)
513 (:documentation
514 "Return the argument list of the declaration specifier belonging to the
515 declaration identifier DECL-IDENTIFIER. If the arglist cannot be determined,
516 the keyword :NOT-AVAILABLE is returned.
517
518 The different SWANK backends can specialize this generic function to
519 include implementation-dependend declaration specifiers, or to provide
520 additional information on the specifiers defined in ANSI Common Lisp.")
521 (:method (decl-identifier)
522 (case decl-identifier
523 (dynamic-extent '(&rest vars))
524 (ignore '(&rest vars))
525 (ignorable '(&rest vars))
526 (special '(&rest vars))
527 (inline '(&rest function-names))
528 (notinline '(&rest function-names))
529 (declaration '(&rest names))
530 (optimize '(&any compilation-speed debug safety space speed))
531 (type '(type-specifier &rest args))
532 (ftype '(type-specifier &rest function-names))
533 (otherwise
534 (flet ((typespec-p (symbol) (member :type (describe-symbol-for-emacs symbol))))
535 (cond ((and (symbolp decl-identifier) (typespec-p decl-identifier))
536 '(&rest vars))
537 ((and (listp decl-identifier) (typespec-p (first decl-identifier)))
538 '(&rest vars))
539 (t :not-available)))))))
540
541 (defgeneric type-specifier-arglist (typespec-operator)
542 (:documentation
543 "Return the argument list of the type specifier belonging to
544 TYPESPEC-OPERATOR.. If the arglist cannot be determined, the keyword
545 :NOT-AVAILABLE is returned.
546
547 The different SWANK backends can specialize this generic function to
548 include implementation-dependend declaration specifiers, or to provide
549 additional information on the specifiers defined in ANSI Common Lisp.")
550 (:method (typespec-operator)
551 (declare (special *type-specifier-arglists*)) ; defined at end of file.
552 (typecase typespec-operator
553 (symbol (or (cdr (assoc typespec-operator *type-specifier-arglists*))
554 :not-available))
555 (t :not-available))))
556
557 (definterface function-name (function)
558 "Return the name of the function object FUNCTION.
559
560 The result is either a symbol, a list, or NIL if no function name is available."
561 (declare (ignore function))
562 nil)
563
564 (definterface macroexpand-all (form)
565 "Recursively expand all macros in FORM.
566 Return the resulting form.")
567
568 (definterface compiler-macroexpand-1 (form &optional env)
569 "Call the compiler-macro for form.
570 If FORM is a function call for which a compiler-macro has been
571 defined, invoke the expander function using *macroexpand-hook* and
572 return the results and T. Otherwise, return the original form and
573 NIL."
574 (let ((fun (and (consp form) (compiler-macro-function (car form)))))
575 (if fun
576 (let ((result (funcall *macroexpand-hook* fun form env)))
577 (values result (not (eq result form))))
578 (values form nil))))
579
580 (definterface compiler-macroexpand (form &optional env)
581 "Repetitively call `compiler-macroexpand-1'."
582 (labels ((frob (form expanded)
583 (multiple-value-bind (new-form newly-expanded)
584 (compiler-macroexpand-1 form env)
585 (if newly-expanded
586 (frob new-form t)
587 (values new-form expanded)))))
588 (frob form env)))
589
590 (definterface format-string-expand (control-string)
591 "Expand the format string CONTROL-STRING."
592 (macroexpand `(formatter ,control-string)))
593
594 (definterface describe-symbol-for-emacs (symbol)
595 "Return a property list describing SYMBOL.
596
597 The property list has an entry for each interesting aspect of the
598 symbol. The recognised keys are:
599
600 :VARIABLE :FUNCTION :SETF :SPECIAL-OPERATOR :MACRO :COMPILER-MACRO
601 :TYPE :CLASS :ALIEN-TYPE :ALIEN-STRUCT :ALIEN-UNION :ALIEN-ENUM
602
603 The value of each property is the corresponding documentation string,
604 or :NOT-DOCUMENTED. It is legal to include keys not listed here (but
605 slime-print-apropos in Emacs must know about them).
606
607 Properties should be included if and only if they are applicable to
608 the symbol. For example, only (and all) fbound symbols should include
609 the :FUNCTION property.
610
611 Example:
612 \(describe-symbol-for-emacs 'vector)
613 => (:CLASS :NOT-DOCUMENTED
614 :TYPE :NOT-DOCUMENTED
615 :FUNCTION \"Constructs a simple-vector from the given objects.\")")
616
617 (definterface describe-definition (name type)
618 "Describe the definition NAME of TYPE.
619 TYPE can be any value returned by DESCRIBE-SYMBOL-FOR-EMACS.
620
621 Return a documentation string, or NIL if none is available.")
622
623
624 ;;;; Debugging
625
626 (definterface install-debugger-globally (function)
627 "Install FUNCTION as the debugger for all threads/processes. This
628 usually involves setting *DEBUGGER-HOOK* and, if the implementation
629 permits, hooking into BREAK as well."
630 (setq *debugger-hook* function))
631
632 (definterface call-with-debugging-environment (debugger-loop-fn)
633 "Call DEBUGGER-LOOP-FN in a suitable debugging environment.
634
635 This function is called recursively at each debug level to invoke the
636 debugger loop. The purpose is to setup any necessary environment for
637 other debugger callbacks that will be called within the debugger loop.
638
639 For example, this is a reasonable place to compute a backtrace, switch
640 to safe reader/printer settings, and so on.")
641
642 (definterface call-with-debugger-hook (hook fun)
643 "Call FUN and use HOOK as debugger hook. HOOK can be NIL.
644
645 HOOK should be called for both BREAK and INVOKE-DEBUGGER."
646 (let ((*debugger-hook* hook))
647 (funcall fun)))
648
649 (define-condition sldb-condition (condition)
650 ((original-condition
651 :initarg :original-condition
652 :accessor original-condition))
653 (:report (lambda (condition stream)
654 (format stream "Condition in debugger code~@[: ~A~]"
655 (original-condition condition))))
656 (:documentation
657 "Wrapper for conditions that should not be debugged.
658
659 When a condition arises from the internals of the debugger, it is not
660 desirable to debug it -- we'd risk entering an endless loop trying to
661 debug the debugger! Instead, such conditions can be reported to the
662 user without (re)entering the debugger by wrapping them as
663 `sldb-condition's."))
664
665 ;;; The following functions in this section are supposed to be called
666 ;;; within the dynamic contour of CALL-WITH-DEBUGGING-ENVIRONMENT only.
667
668 (definterface compute-backtrace (start end)
669 "Returns a backtrace of the condition currently being debugged,
670 that is an ordered list consisting of frames. ``Ordered list''
671 means that an integer I can be mapped back to the i-th frame of this
672 backtrace.
673
674 START and END are zero-based indices constraining the number of frames
675 returned. Frame zero is defined as the frame which invoked the
676 debugger. If END is nil, return the frames from START to the end of
677 the stack.")
678
679 (definterface print-frame (frame stream)
680 "Print frame to stream.")
681
682 (definterface frame-restartable-p (frame)
683 "Is the frame FRAME restartable?.
684 Return T if `restart-frame' can safely be called on the frame."
685 (declare (ignore frame))
686 nil)
687
688 (definterface frame-source-location (frame-number)
689 "Return the source location for the frame associated to FRAME-NUMBER.")
690
691 (definterface frame-catch-tags (frame-number)
692 "Return a list of catch tags for being printed in a debugger stack
693 frame."
694 (declare (ignore frame-number))
695 '())
696
697 (definterface frame-locals (frame-number)
698 "Return a list of ((&key NAME ID VALUE) ...) where each element of
699 the list represents a local variable in the stack frame associated to
700 FRAME-NUMBER.
701
702 NAME, a symbol; the name of the local variable.
703
704 ID, an integer; used as primary key for the local variable, unique
705 relatively to the frame under operation.
706
707 value, an object; the value of the local variable.")
708
709 (definterface frame-var-value (frame-number var-id)
710 "Return the value of the local variable associated to VAR-ID
711 relatively to the frame associated to FRAME-NUMBER.")
712
713 (definterface disassemble-frame (frame-number)
714 "Disassemble the code for the FRAME-NUMBER.
715 The output should be written to standard output.
716 FRAME-NUMBER is a non-negative integer.")
717
718 (definterface eval-in-frame (form frame-number)
719 "Evaluate a Lisp form in the lexical context of a stack frame
720 in the debugger.
721
722 FRAME-NUMBER must be a positive integer with 0 indicating the
723 frame which invoked the debugger.
724
725 The return value is the result of evaulating FORM in the
726 appropriate context.")
727
728 (definterface return-from-frame (frame-number form)
729 "Unwind the stack to the frame FRAME-NUMBER and return the value(s)
730 produced by evaluating FORM in the frame context to its caller.
731
732 Execute any clean-up code from unwind-protect forms above the frame
733 during unwinding.
734
735 Return a string describing the error if it's not possible to return
736 from the frame.")
737
738 (definterface restart-frame (frame-number)
739 "Restart execution of the frame FRAME-NUMBER with the same arguments
740 as it was called originally.")
741
742 (definterface format-sldb-condition (condition)
743 "Format a condition for display in SLDB."
744 (princ-to-string condition))
745
746 (definterface condition-extras (condition)
747 "Return a list of extra for the debugger.
748 The allowed elements are of the form:
749 (:SHOW-FRAME-SOURCE frame-number)
750 (:REFERENCES &rest refs)
751 "
752 (declare (ignore condition))
753 '())
754
755 (definterface activate-stepping (frame-number)
756 "Prepare the frame FRAME-NUMBER for stepping.")
757
758 (definterface sldb-break-on-return (frame-number)
759 "Set a breakpoint in the frame FRAME-NUMBER.")
760
761 (definterface sldb-break-at-start (symbol)
762 "Set a breakpoint on the beginning of the function for SYMBOL.")
763
764 (definterface sldb-stepper-condition-p (condition)
765 "Return true if SLDB was invoked due to a single-stepping condition,
766 false otherwise. "
767 (declare (ignore condition))
768 nil)
769
770 (definterface sldb-step-into ()
771 "Step into the current single-stepper form.")
772
773 (definterface sldb-step-next ()
774 "Step to the next form in the current function.")
775
776 (definterface sldb-step-out ()
777 "Stop single-stepping temporarily, but resume it once the current function
778 returns.")
779
780
781 ;;;; Definition finding
782
783 (defstruct (:location (:type list) :named
784 (:constructor make-location
785 (buffer position &optional hints)))
786 buffer position
787 ;; Hints is a property list optionally containing:
788 ;; :snippet SOURCE-TEXT
789 ;; This is a snippet of the actual source text at the start of
790 ;; the definition, which could be used in a text search.
791 hints)
792
793 (defstruct (:error (:type list) :named (:constructor)) message)
794 (defstruct (:file (:type list) :named (:constructor)) name)
795 (defstruct (:buffer (:type list) :named (:constructor)) name)
796 (defstruct (:position (:type list) :named (:constructor)) pos)
797
798 (defun make-error-location (datum &rest args)
799 (cond ((typep datum 'condition)
800 `(:error ,(format nil "Error: ~A" datum)))
801 ((symbolp datum)
802 `(:error ,(format nil "Error: ~A" (apply #'make-condition datum args))))
803 (t
804 (assert (stringp datum))
805 `(:error ,(apply #'format nil datum args)))))
806
807 (definterface find-definitions (name)
808 "Return a list ((DSPEC LOCATION) ...) for NAME's definitions.
809
810 NAME is a \"definition specifier\".
811
812 DSPEC is a \"definition specifier\" describing the
813 definition, e.g., FOO or (METHOD FOO (STRING NUMBER)) or
814 \(DEFVAR FOO).
815
816 LOCATION is the source location for the definition.")
817
818 (definterface find-source-location (object)
819 "Returns the source location of OBJECT, or NIL.
820
821 That is the source location of the underlying datastructure of
822 OBJECT. E.g. on a STANDARD-OBJECT, the source location of the
823 respective DEFCLASS definition is returned, on a STRUCTURE-CLASS the
824 respective DEFSTRUCT definition, and so on."
825 ;; This returns one source location and not a list of locations. It's
826 ;; supposed to return the location of the DEFGENERIC definition on
827 ;; #'SOME-GENERIC-FUNCTION.
828 (declare (ignore object))
829 (make-error-location "FIND-DEFINITIONS is not yet implemented on ~
830 this implementation."))
831
832
833 (definterface buffer-first-change (filename)
834 "Called for effect the first time FILENAME's buffer is modified."
835 (declare (ignore filename))
836 nil)
837
838
839
840 ;;;; XREF
841
842 (definterface who-calls (function-name)
843 "Return the call sites of FUNCTION-NAME (a symbol).
844 The results is a list ((DSPEC LOCATION) ...)."
845 (declare (ignore function-name))
846 :not-implemented)
847
848 (definterface calls-who (function-name)
849 "Return the call sites of FUNCTION-NAME (a symbol).
850 The results is a list ((DSPEC LOCATION) ...)."
851 (declare (ignore function-name))
852 :not-implemented)
853
854 (definterface who-references (variable-name)
855 "Return the locations where VARIABLE-NAME (a symbol) is referenced.
856 See WHO-CALLS for a description of the return value."
857 (declare (ignore variable-name))
858 :not-implemented)
859
860 (definterface who-binds (variable-name)
861 "Return the locations where VARIABLE-NAME (a symbol) is bound.
862 See WHO-CALLS for a description of the return value."
863 (declare (ignore variable-name))
864 :not-implemented)
865
866 (definterface who-sets (variable-name)
867 "Return the locations where VARIABLE-NAME (a symbol) is set.
868 See WHO-CALLS for a description of the return value."
869 (declare (ignore variable-name))
870 :not-implemented)
871
872 (definterface who-macroexpands (macro-name)
873 "Return the locations where MACRO-NAME (a symbol) is expanded.
874 See WHO-CALLS for a description of the return value."
875 (declare (ignore macro-name))
876 :not-implemented)
877
878 (definterface who-specializes (class-name)
879 "Return the locations where CLASS-NAME (a symbol) is specialized.
880 See WHO-CALLS for a description of the return value."
881 (declare (ignore class-name))
882 :not-implemented)
883
884 ;;; Simpler variants.
885
886 (definterface list-callers (function-name)
887 "List the callers of FUNCTION-NAME.
888 This function is like WHO-CALLS except that it is expected to use
889 lower-level means. Whereas WHO-CALLS is usually implemented with
890 special compiler support, LIST-CALLERS is usually implemented by
891 groveling for constants in function objects throughout the heap.
892
893 The return value is as for WHO-CALLS.")
894
895 (definterface list-callees (function-name)
896 "List the functions called by FUNCTION-NAME.
897 See LIST-CALLERS for a description of the return value.")
898
899
900 ;;;; Profiling
901
902 ;;; The following functions define a minimal profiling interface.
903
904 (definterface profile (fname)
905 "Marks symbol FNAME for profiling.")
906
907 (definterface profiled-functions ()
908 "Returns a list of profiled functions.")
909
910 (definterface unprofile (fname)
911 "Marks symbol FNAME as not profiled.")
912
913 (definterface unprofile-all ()
914 "Marks all currently profiled functions as not profiled."
915 (dolist (f (profiled-functions))
916 (unprofile f)))
917
918 (definterface profile-report ()
919 "Prints profile report.")
920
921 (definterface profile-reset ()
922 "Resets profile counters.")
923
924 (definterface profile-package (package callers-p methods)
925 "Wrap profiling code around all functions in PACKAGE. If a function
926 is already profiled, then unprofile and reprofile (useful to notice
927 function redefinition.)
928
929 If CALLERS-P is T names have counts of the most common calling
930 functions recorded.
931
932 When called with arguments :METHODS T, profile all methods of all
933 generic functions having names in the given package. Generic functions
934 themselves, that is, their dispatch functions, are left alone.")
935
936
937 ;;;; Inspector
938
939 (defgeneric emacs-inspect (object)
940 (:documentation
941 "Explain to Emacs how to inspect OBJECT.
942
943 Returns a list specifying how to render the object for inspection.
944
945 Every element of the list must be either a string, which will be
946 inserted into the buffer as is, or a list of the form:
947
948 (:value object &optional format) - Render an inspectable
949 object. If format is provided it must be a string and will be
950 rendered in place of the value, otherwise use princ-to-string.
951
952 (:newline) - Render a \\n
953
954 (:action label lambda &key (refresh t)) - Render LABEL (a text
955 string) which when clicked will call LAMBDA. If REFRESH is
956 non-NIL the currently inspected object will be re-inspected
957 after calling the lambda.
958 "))
959
960 (defmethod emacs-inspect ((object t))
961 "Generic method for inspecting any kind of object.
962
963 Since we don't know how to deal with OBJECT we simply dump the
964 output of CL:DESCRIBE."
965 `("Type: " (:value ,(type-of object)) (:newline)
966 "Don't know how to inspect the object, dumping output of CL:DESCRIBE:"
967 (:newline) (:newline)
968 ,(with-output-to-string (desc) (describe object desc))))
969
970
971 ;;; Utilities for inspector methods.
972 ;;;
973
974 (defun label-value-line (label value &key (newline t))
975 "Create a control list which prints \"LABEL: VALUE\" in the inspector.
976 If NEWLINE is non-NIL a `(:newline)' is added to the result."
977
978 (list* (princ-to-string label) ": " `(:value ,value)
979 (if newline '((:newline)) nil)))
980
981 (defmacro label-value-line* (&rest label-values)
982 ` (append ,@(loop for (label value) in label-values
983 collect `(label-value-line ,label ,value))))
984
985 (definterface describe-primitive-type (object)
986 "Return a string describing the primitive type of object."
987 (declare (ignore object))
988 "N/A")
989
990
991 ;;;; Multithreading
992 ;;;
993 ;;; The default implementations are sufficient for non-multiprocessing
994 ;;; implementations.
995
996 (definterface initialize-multiprocessing (continuation)
997 "Initialize multiprocessing, if necessary and then invoke CONTINUATION.
998
999 Depending on the impleimentaion, this function may never return."
1000 (funcall continuation))
1001
1002 (definterface spawn (fn &key name)
1003 "Create a new thread to call FN.")
1004
1005 (definterface thread-id (thread)
1006 "Return an Emacs-parsable object to identify THREAD.
1007
1008 Ids should be comparable with equal, i.e.:
1009 (equal (thread-id <t1>) (thread-id <t2>)) <==> (eq <t1> <t2>)"
1010 thread)
1011
1012 (definterface find-thread (id)
1013 "Return the thread for ID.
1014 ID should be an id previously obtained with THREAD-ID.
1015 Can return nil if the thread no longer exists."
1016 (declare (ignore id))
1017 (current-thread))
1018
1019 (definterface thread-name (thread)
1020 "Return the name of THREAD.
1021 Thread names are short strings meaningful to the user. They do not
1022 have to be unique."
1023 (declare (ignore thread))
1024 "The One True Thread")
1025
1026 (definterface thread-status (thread)
1027 "Return a string describing THREAD's state."
1028 (declare (ignore thread))
1029 "")
1030
1031 (definterface thread-attributes (thread)
1032 "Return a plist of implementation-dependent attributes for THREAD"
1033 (declare (ignore thread))
1034 '())
1035
1036 (definterface make-lock (&key name)
1037 "Make a lock for thread synchronization.
1038 Only one thread may hold the lock (via CALL-WITH-LOCK-HELD) at a time
1039 but that thread may hold it more than once."
1040 (declare (ignore name))
1041 :null-lock)
1042
1043 (definterface call-with-lock-held (lock function)
1044 "Call FUNCTION with LOCK held, queueing if necessary."
1045 (declare (ignore lock)
1046 (type function function))
1047 (funcall function))
1048
1049 (definterface current-thread ()
1050 "Return the currently executing thread."
1051 0)
1052
1053 (definterface all-threads ()
1054 "Return a fresh list of all threads.")
1055
1056 (definterface thread-alive-p (thread)
1057 "Test if THREAD is termintated."
1058 (member thread (all-threads)))
1059
1060 (definterface interrupt-thread (thread fn)
1061 "Cause THREAD to execute FN.")
1062
1063 (definterface kill-thread (thread)
1064 "Terminate THREAD immediately.
1065 Don't execute unwind-protected sections, don't raise conditions.
1066 (Do not pass go, do not collect $200.)"
1067 (declare (ignore thread))
1068 nil)
1069
1070 (definterface send (thread object)
1071 "Send OBJECT to thread THREAD.")
1072
1073 (definterface receive (&optional timeout)
1074 "Return the next message from current thread's mailbox."
1075 (receive-if (constantly t) timeout))
1076
1077 (definterface receive-if (predicate &optional timeout)
1078 "Return the first message satisfiying PREDICATE.")
1079
1080 (definterface set-default-initial-binding (var form)
1081 "Initialize special variable VAR by default with FORM.
1082
1083 Some implementations initialize certain variables in each newly
1084 created thread. This function sets the form which is used to produce
1085 the initial value."
1086 (set var (eval form)))
1087
1088 ;; List of delayed interrupts.
1089 ;; This should only have thread-local bindings, so no init form.
1090 (defvar *pending-slime-interrupts*)
1091
1092 (defun check-slime-interrupts ()
1093 "Execute pending interrupts if any.
1094 This should be called periodically in operations which
1095 can take a long time to complete.
1096 Return a boolean indicating whether any interrupts was processed."
1097 (when (and (boundp '*pending-slime-interrupts*)
1098 *pending-slime-interrupts*)
1099 (funcall (pop *pending-slime-interrupts*))
1100 t))
1101
1102 (defvar *interrupt-queued-handler* nil
1103 "Function to call on queued interrupts.
1104 Interrupts get queued when an interrupt occurs while interrupt
1105 handling is disabled.
1106
1107 Backends can use this function to abort slow operations.")
1108
1109 (definterface wait-for-input (streams &optional timeout)
1110 "Wait for input on a list of streams. Return those that are ready.
1111 STREAMS is a list of streams
1112 TIMEOUT nil, t, or real number. If TIMEOUT is t, return
1113 those streams which are ready immediately, without waiting.
1114 If TIMEOUT is a number and no streams is ready after TIMEOUT seconds,
1115 return nil.
1116
1117 Return :interrupt if an interrupt occurs while waiting."
1118 (assert (member timeout '(nil t)))
1119 (cond #+(or)
1120 ((null (cdr streams))
1121 (wait-for-one-stream (car streams) timeout))
1122 (t
1123 (wait-for-streams streams timeout))))
1124
1125 (defun wait-for-streams (streams timeout)
1126 (loop
1127 (when (check-slime-interrupts) (return :interrupt))
1128 (let ((ready (remove-if-not #'stream-readable-p streams)))
1129 (when ready (return ready)))
1130 (when timeout (return nil))
1131 (sleep 0.1)))
1132
1133 ;; Note: Usually we can't interrupt PEEK-CHAR cleanly.
1134 (defun wait-for-one-stream (stream timeout)
1135 (ecase timeout
1136 ((nil)
1137 (cond ((check-slime-interrupts) :interrupt)
1138 (t (peek-char nil stream nil nil)
1139 (list stream))))
1140 ((t)
1141 (let ((c (read-char-no-hang stream nil nil)))
1142 (cond (c
1143 (unread-char c stream)
1144 (list stream))
1145 (t '()))))))
1146
1147 (defun stream-readable-p (stream)
1148 (let ((c (read-char-no-hang stream nil :eof)))
1149 (cond ((not c) nil)
1150 ((eq c :eof) t)
1151 (t (unread-char c stream) t))))
1152
1153 (definterface toggle-trace (spec)
1154 "Toggle tracing of the function(s) given with SPEC.
1155 SPEC can be:
1156 (setf NAME) ; a setf function
1157 (:defmethod NAME QUALIFIER... (SPECIALIZER...)) ; a specific method
1158 (:defgeneric NAME) ; a generic function with all methods
1159 (:call CALLER CALLEE) ; trace calls from CALLER to CALLEE.
1160 (:labels TOPLEVEL LOCAL)
1161 (:flet TOPLEVEL LOCAL) ")
1162
1163
1164 ;;;; Weak datastructures
1165
1166 (definterface make-weak-key-hash-table (&rest args)
1167 "Like MAKE-HASH-TABLE, but weak w.r.t. the keys."
1168 (apply #'make-hash-table args))
1169
1170 (definterface make-weak-value-hash-table (&rest args)
1171 "Like MAKE-HASH-TABLE, but weak w.r.t. the values."
1172 (apply #'make-hash-table args))
1173
1174 (definterface hash-table-weakness (hashtable)
1175 "Return nil or one of :key :value :key-or-value :key-and-value"
1176 (declare (ignore hashtable))
1177 nil)
1178
1179
1180 ;;;; Character names
1181
1182 (definterface character-completion-set (prefix matchp)
1183 "Return a list of names of characters that match PREFIX."
1184 ;; Handle the standard and semi-standard characters.
1185 (loop for name in '("Newline" "Space" "Tab" "Page" "Rubout"
1186 "Linefeed" "Return" "Backspace")
1187 when (funcall matchp prefix name)
1188 collect name))
1189
1190
1191 (defparameter *type-specifier-arglists*
1192 '((and . (&rest type-specifiers))
1193 (array . (&optional element-type dimension-spec))
1194 (base-string . (&optional size))
1195 (bit-vector . (&optional size))
1196 (complex . (&optional type-specifier))
1197 (cons . (&optional car-typespec cdr-typespec))
1198 (double-float . (&optional lower-limit upper-limit))
1199 (eql . (object))
1200 (float . (&optional lower-limit upper-limit))
1201 (function . (&optional arg-typespec value-typespec))
1202 (integer . (&optional lower-limit upper-limit))
1203 (long-float . (&optional lower-limit upper-limit))
1204 (member . (&rest eql-objects))
1205 (mod . (n))
1206 (not . (type-specifier))
1207 (or . (&rest type-specifiers))
1208 (rational . (&optional lower-limit upper-limit))
1209 (real . (&optional lower-limit upper-limit))
1210 (satisfies . (predicate-symbol))
1211 (short-float . (&optional lower-limit upper-limit))
1212 (signed-byte . (&optional size))
1213 (simple-array . (&optional element-type dimension-spec))
1214 (simple-base-string . (&optional size))
1215 (simple-bit-vector . (&optional size))
1216 (simple-string . (&optional size))
1217 (single-float . (&optional lower-limit upper-limit))
1218 (simple-vector . (&optional size))
1219 (string . (&optional size))
1220 (unsigned-byte . (&optional size))
1221 (values . (&rest typespecs))
1222 (vector . (&optional element-type size))
1223 ))
1224
1225 ;;; Heap dumps
1226
1227 (definterface save-image (filename &optional restart-function)
1228 "Save a heap image to the file FILENAME.
1229 RESTART-FUNCTION, if non-nil, should be called when the image is loaded.")
1230
1231
1232

  ViewVC Help
Powered by ViewVC 1.1.5