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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations)
Thu Aug 23 19:03:37 2007 UTC (6 years, 7 months ago) by heller
Branch: MAIN
Changes since 1.12: +7 -8 lines
Introduce backend-inspector class.

* swank-backend.lisp (backend-inspector): New class. Introduce a named
  class to give as another way to dispatch to backend methods.

* swank-cmucl.lisp: Use backend-inspector class.
* swank-sbcl.lisp: Use backend-inspector class.
* swank-clisp.lisp: Use backend-inspector class.
* swank-lispworks.lisp: Use backend-inspector class.
* swank-allegro.lisp: Use backend-inspector class.
* swank-openmcl.lisp: Use backend-inspector class.
* swank-abcl.lisp: Use backend-inspector class.
* swank-corman.lisp: Use backend-inspector class.
* swank-scl.lisp: Use backend-inspector class.
1 dcrosher 1.1 ;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2     ;;;
3     ;;; Scieneer Common Lisp code for SLIME.
4     ;;;
5     ;;; This code has been placed in the Public Domain. All warranties
6     ;;; are disclaimed.
7     ;;;
8    
9     (in-package :swank-backend)
10    
11    
12    
13     ;;; swank-mop
14    
15 dcrosher 1.7 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
16 dcrosher 1.1
17     (defun swank-mop:slot-definition-documentation (slot)
18 dcrosher 1.7 (documentation slot t))
19 dcrosher 1.1
20    
21     ;;;; TCP server
22     ;;;
23     ;;; SCL only supports the :spawn communication style.
24     ;;;
25    
26     (defimplementation preferred-communication-style ()
27     :spawn)
28    
29     (defimplementation create-socket (host port)
30     (let ((addr (resolve-hostname host)))
31     (ext:create-inet-listener port :stream :host addr :reuse-address t)))
32    
33     (defimplementation local-port (socket)
34     (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
35    
36     (defimplementation close-socket (socket)
37     (ext:close-socket (socket-fd socket)))
38    
39 heller 1.10 (defimplementation accept-connection (socket
40     &key external-format buffering timeout)
41 heller 1.12 (let ((external-format (or external-format :default))
42 heller 1.10 (buffering (or buffering :full))
43 dcrosher 1.6 (fd (socket-fd socket)))
44     (loop
45     (let ((ready (sys:wait-until-fd-usable fd :input timeout)))
46     (unless ready
47     (error "Timeout accepting connection on socket: ~S~%" socket)))
48     (let ((new-fd (ignore-errors (ext:accept-tcp-connection fd))))
49     (when new-fd
50     (return (make-socket-io-stream new-fd external-format buffering)))))))
51    
52     (defimplementation set-stream-timeout (stream timeout)
53     (check-type timeout (or null real))
54     (if (fboundp 'ext::stream-timeout)
55     (setf (ext::stream-timeout stream) timeout)
56     (setf (slot-value (slot-value stream 'cl::stream) 'cl::timeout) timeout)))
57 dcrosher 1.1
58     ;;;;; Sockets
59    
60     (defun socket-fd (socket)
61     "Return the file descriptor for the socket represented by 'socket."
62     (etypecase socket
63     (fixnum socket)
64     (stream (sys:fd-stream-fd socket))))
65    
66     (defun resolve-hostname (hostname)
67     "Return the IP address of 'hostname as an integer (in host byte-order)."
68     (let ((hostent (ext:lookup-host-entry hostname)))
69     (car (ext:host-entry-addr-list hostent))))
70    
71 heller 1.12 (defvar *external-format-to-coding-system*
72     '((:iso-8859-1
73     "latin-1" "latin-1-unix" "iso-latin-1-unix"
74     "iso-8859-1" "iso-8859-1-unix")
75     (:utf-8 "utf-8" "utf-8-unix")
76     (:euc-jp "euc-jp" "euc-jp-unix")))
77    
78     (defimplementation find-external-format (coding-system)
79     (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
80     *external-format-to-coding-system*)))
81 dcrosher 1.1
82 dcrosher 1.3 (defun make-socket-io-stream (fd external-format buffering)
83 dcrosher 1.1 "Create a new input/output fd-stream for 'fd."
84 heller 1.12 (let* ((stream (sys:make-fd-stream fd :input t :output t
85 dcrosher 1.7 :element-type 'base-char
86     :buffering buffering
87     :external-format external-format)))
88     ;; Ignore character conversion errors. Without this the communication
89     ;; channel is prone to lockup if a character conversion error occurs.
90     (setf (cl::stream-character-conversion-error-value stream) #\?)
91     stream))
92 dcrosher 1.1
93    
94     ;;;; Stream handling
95    
96     (defclass slime-input-stream (ext:character-input-stream)
97     ((buffer :initarg :buffer :type string)
98     (index :initarg :index :initform 0 :type fixnum)
99     (position :initarg :position :initform 0 :type integer)
100     (interactive :initarg :interactive :initform nil :type (member nil t))
101     (output-stream :initarg :output-stream :initform nil)
102     (input-fn :initarg :input-fn :type function)
103     ))
104    
105     (defun make-slime-input-stream (input-fn &optional output-stream)
106     (declare (function input-fn))
107     (make-instance 'slime-input-stream
108     :in-buffer (make-string 256)
109     :in-head 0 :in-tail 0
110     :out-buffer ""
111     :buffer "" :index 0
112     :input-fn input-fn
113     :output-stream output-stream))
114    
115     (defmethod print-object ((s slime-input-stream) stream)
116     (print-unreadable-object (s stream :type t)))
117    
118     ;;; input-stream-p inherits from input-stream.
119     ;;; output-stream-p inherits nil.
120    
121     (defmethod ext:stream-listen ((stream slime-input-stream))
122     (let* ((buffer (slot-value stream 'buffer))
123     (index (slot-value stream 'index))
124     (length (length buffer)))
125     (declare (type string buffer)
126     (fixnum index length))
127     (< index length)))
128    
129     (defmethod close ((stream slime-input-stream) &key ((:abort abort) nil))
130     (declare (ignore abort))
131     (when (ext:stream-open-p stream)
132     (setf (ext:stream-open-p stream) nil)
133     (setf (ext:stream-in-buffer stream) " ")
134     t))
135    
136     (defmethod ext:stream-clear-input ((stream slime-input-stream))
137     (let* ((input-buffer (slot-value stream 'buffer))
138     (index (slot-value stream 'index))
139     (input-length (length input-buffer))
140     (available (- input-length index))
141     (position (slot-value stream 'position))
142     (new-position (+ position available)))
143     (declare (type kernel:index index available position new-position))
144     (setf (slot-value stream 'position) new-position))
145     (setf (slot-value stream 'buffer) "")
146     (setf (slot-value stream 'index) 0)
147     nil)
148    
149     ;;; No 'stream-finish-output method.
150     ;;; No 'stream-force-output method.
151     ;;; No 'stream-clear-output method.
152    
153     ;;; stream-element-type inherits from character-stream.
154    
155     ;;; No 'stream-line-length method.
156     ;;; No 'stream-line-column method.
157    
158     ;;; Add the remaining input to the current position.
159     (defmethod file-length ((stream slime-input-stream))
160     (let* ((input-buffer (slot-value stream 'buffer))
161     (index (slot-value stream 'index))
162     (input-length (length input-buffer))
163     (available (- input-length index))
164     (position (slot-value stream 'position))
165     (file-length (+ position available)))
166     (declare (type kernel:index index available position file-length))
167     file-length))
168    
169     (defmethod ext:stream-file-position ((stream slime-input-stream)
170     &optional position)
171     (let ((current-position (slot-value stream 'position)))
172     (declare (type kernel:index current-position))
173     (cond (position
174     ;; Could make an attempt here, but just give up for now.
175     nil)
176     (t
177     current-position))))
178    
179     (defmethod interactive-stream-p ((stream slime-input-stream))
180     (slot-value stream 'interactive))
181    
182     ;;; No 'file-string-length method.
183    
184     (defmethod ext:stream-read-chars ((stream slime-input-stream) buffer
185     start requested waitp)
186     (declare (type simple-string buffer)
187     (type kernel:index start requested))
188     (let* ((input-buffer (slot-value stream 'buffer))
189     (index (slot-value stream 'index))
190     (input-length (length input-buffer))
191     (available (- input-length index))
192     (copy (min available requested)))
193     (declare (string input-buffer)
194     (type kernel:index index available copy))
195     (cond ((plusp copy)
196     (dotimes (i copy)
197     (declare (type kernel:index i))
198     (setf (aref buffer (+ start i)) (aref input-buffer (+ index i))))
199 dcrosher 1.5 (setf (slot-value stream 'index) (+ index copy))
200 dcrosher 1.1 (incf (slot-value stream 'position) copy)
201     copy)
202     (waitp
203     (let ((output-stream (slot-value stream 'output-stream))
204     (input-fn (slot-value stream 'input-fn)))
205     (declare (type function input-fn))
206     (when output-stream
207     (force-output output-stream))
208     (let ((new-input (funcall input-fn)))
209     (cond ((zerop (length new-input))
210     -1)
211     (t
212     (setf (slot-value stream 'buffer) new-input)
213     (setf (slot-value stream 'index) 0)
214 dcrosher 1.5 (ext:stream-read-chars stream buffer
215     start requested waitp))))))
216 dcrosher 1.1 (t
217     0))))
218    
219     ;;; Slime output stream.
220    
221     (defclass slime-output-stream (ext:character-output-stream)
222     ((output-fn :initarg :output-fn :type function)
223     (column :initform 0 :type kernel:index)
224     (interactive :initform nil :type (member nil t))
225     (position :initform 0 :type integer)))
226    
227     (defun make-slime-output-stream (output-fn)
228     (declare (function output-fn))
229     (make-instance 'slime-output-stream
230     :in-buffer ""
231     :out-buffer (make-string 256)
232     :output-fn output-fn))
233    
234     (defmethod print-object ((s slime-output-stream) stream)
235     (print-unreadable-object (s stream :type t)))
236    
237     ;;; Use default 'input-stream-p method for 'output-stream which returns 'nil.
238     ;;; Use default 'output-stream-p method for 'output-stream which returns 't.
239    
240     ;;; No 'stream-listen method.
241    
242     (defmethod close ((stream slime-output-stream) &key ((:abort abort) nil))
243     (when (ext:stream-open-p stream)
244     (unless abort
245     (finish-output stream))
246     (setf (ext:stream-open-p stream) nil)
247     (setf (ext:stream-out-buffer stream) " ")
248     t))
249    
250     ;;; No 'stream-clear-input method.
251    
252     (defmethod ext:stream-finish-output ((stream slime-output-stream))
253     nil)
254    
255     (defmethod ext:stream-force-output ((stream slime-output-stream))
256     nil)
257    
258     (defmethod ext:stream-clear-output ((stream slime-output-stream))
259     nil)
260    
261     ;;; Use default 'stream-element-type method for 'character-stream which
262     ;;; returns 'base-char.
263    
264     (defmethod ext:stream-line-length ((stream slime-output-stream))
265     80)
266    
267     (defmethod ext:stream-line-column ((stream slime-output-stream))
268     (slot-value stream 'column))
269    
270     (defmethod file-length ((stream slime-output-stream))
271     (slot-value stream 'position))
272    
273     (defmethod ext:stream-file-position ((stream slime-output-stream)
274     &optional position)
275     (declare (optimize (speed 3)))
276     (cond (position
277     (let* ((current-position (slot-value stream 'position))
278     (target-position (etypecase position
279     ((member :start) 0)
280     ((member :end) current-position)
281     (kernel:index position))))
282     (declare (type kernel:index current-position target-position))
283     (cond ((= target-position current-position)
284     t)
285     ((> target-position current-position)
286     (let ((output-fn (slot-value stream 'output-fn))
287     (fill-size (- target-position current-position)))
288     (declare (function output-fn))
289     (funcall output-fn (make-string fill-size
290     :initial-element #\space))
291     (setf (slot-value stream 'position) target-position))
292     t)
293     (t
294     nil))))
295     (t
296     (slot-value stream 'position))))
297    
298     (defmethod interactive-stream-p ((stream slime-output-stream))
299     (slot-value stream 'interactive))
300    
301     ;;; Use the default 'character-output-stream 'file-string-length method.
302    
303     ;;; stream-write-chars
304     ;;;
305     ;;; The stream out-buffer is typically large enough that there is little point
306     ;;; growing the stream output 'string large than the total size. For typical
307     ;;; usage this reduces consing. As the string grows larger then grow to
308     ;;; reduce the cost of copying strings around.
309     ;;;
310 dcrosher 1.8 (defmethod ext:stream-write-chars ((stream slime-output-stream)
311     string start end waitp)
312 dcrosher 1.1 (declare (simple-string string)
313 dcrosher 1.8 (type kernel:index start end)
314     (ignore waitp))
315 dcrosher 1.1 (declare (optimize (speed 3)))
316     (unless (ext:stream-open-p stream)
317     (error 'kernel:simple-stream-error
318     :stream stream
319     :format-control "Stream closed."))
320     (let* ((string-length (length string))
321     (start (cond ((< start 0) 0)
322     ((> start string-length) string-length)
323     (t start)))
324     (end (cond ((< end start) start)
325     ((> end string-length) string-length)
326     (t end)))
327     (length (- end start))
328     (output-fn (slot-value stream 'output-fn)))
329     (declare (type kernel:index start end length)
330     (type function output-fn))
331     (unless (zerop length)
332     (funcall output-fn (subseq string start end))
333     (let ((last-newline (position #\newline string :from-end t
334     :start start :end end)))
335     (setf (slot-value stream 'column)
336     (if last-newline
337     (- end last-newline 1)
338     (let ((column (slot-value stream 'column)))
339     (declare (type kernel:index column))
340     (+ column (- end start))))))))
341 dcrosher 1.8 (- end start))
342 dcrosher 1.1
343     ;;;
344    
345     (defimplementation make-fn-streams (input-fn output-fn)
346     (let* ((output (make-slime-output-stream output-fn))
347     (input (make-slime-input-stream input-fn output)))
348     (values input output)))
349    
350     (defimplementation make-stream-interactive (stream)
351     (when (or (typep stream 'slime-input-stream)
352     (typep stream 'slime-output-stream))
353     (setf (slot-value stream 'interactive) t)))
354    
355    
356     ;;;; Compilation Commands
357    
358     (defvar *previous-compiler-condition* nil
359     "Used to detect duplicates.")
360    
361     (defvar *previous-context* nil
362     "Previous compiler error context.")
363    
364     (defvar *buffer-name* nil
365     "The name of the Emacs buffer we are compiling from.
366     Nil if we aren't compiling from a buffer.")
367    
368     (defvar *buffer-start-position* nil)
369     (defvar *buffer-substring* nil)
370    
371     (defimplementation call-with-compilation-hooks (function)
372     (let ((*previous-compiler-condition* nil)
373     (*previous-context* nil)
374     (*print-readably* nil))
375     (handler-bind ((c::compiler-error #'handle-notification-condition)
376     (c::style-warning #'handle-notification-condition)
377     (c::warning #'handle-notification-condition))
378     (funcall function))))
379    
380 heller 1.12 (defimplementation swank-compile-file (filename load-p external-format)
381     (with-compilation-hooks ()
382     (let ((*buffer-name* nil)
383     (ext:*ignore-extra-close-parentheses* nil))
384     (multiple-value-bind (output-file warnings-p failure-p)
385     (compile-file filename :external-format external-format)
386     (unless failure-p
387     ;; Cache the latest source file for definition-finding.
388     (source-cache-get filename (file-write-date filename))
389     (when load-p (load output-file)))
390     (values output-file warnings-p failure-p)))))
391 dcrosher 1.1
392     (defimplementation swank-compile-string (string &key buffer position directory)
393     (declare (ignore directory))
394     (with-compilation-hooks ()
395     (let ((*buffer-name* buffer)
396     (*buffer-start-position* position)
397     (*buffer-substring* string))
398     (with-input-from-string (stream string)
399     (ext:compile-from-stream
400     stream
401     :source-info `(:emacs-buffer ,buffer
402     :emacs-buffer-offset ,position
403     :emacs-buffer-string ,string))))))
404    
405    
406     ;;;;; Trapping notes
407     ;;;
408     ;;; We intercept conditions from the compiler and resignal them as
409     ;;; `swank:compiler-condition's.
410    
411     (defun handle-notification-condition (condition)
412     "Handle a condition caused by a compiler warning."
413     (unless (eq condition *previous-compiler-condition*)
414     (let ((context (c::find-error-context nil)))
415     (setq *previous-compiler-condition* condition)
416     (setq *previous-context* context)
417     (signal-compiler-condition condition context))))
418    
419     (defun signal-compiler-condition (condition context)
420     (signal (make-condition
421     'compiler-condition
422     :original-condition condition
423     :severity (severity-for-emacs condition)
424     :short-message (brief-compiler-message-for-emacs condition)
425     :message (long-compiler-message-for-emacs condition context)
426     :location (if (read-error-p condition)
427     (read-error-location condition)
428     (compiler-note-location context)))))
429    
430     (defun severity-for-emacs (condition)
431     "Return the severity of 'condition."
432     (etypecase condition
433     ((satisfies read-error-p) :read-error)
434     (c::compiler-error :error)
435     (c::style-warning :note)
436     (c::warning :warning)))
437    
438     (defun read-error-p (condition)
439     (eq (type-of condition) 'c::compiler-read-error))
440    
441     (defun brief-compiler-message-for-emacs (condition)
442     "Briefly describe a compiler error for Emacs.
443     When Emacs presents the message it already has the source popped up
444     and the source form highlighted. This makes much of the information in
445     the error-context redundant."
446     (princ-to-string condition))
447    
448     (defun long-compiler-message-for-emacs (condition error-context)
449     "Describe a compiler error for Emacs including context information."
450     (declare (type (or c::compiler-error-context null) error-context))
451     (multiple-value-bind (enclosing source)
452     (if error-context
453     (values (c::compiler-error-context-enclosing-source error-context)
454     (c::compiler-error-context-source error-context)))
455     (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]~A"
456     enclosing source condition)))
457    
458     (defun read-error-location (condition)
459     (let* ((finfo (car (c::source-info-current-file c::*source-info*)))
460     (file (c::file-info-name finfo))
461     (pos (c::compiler-read-error-position condition)))
462     (cond ((and (eq file :stream) *buffer-name*)
463     (make-location (list :buffer *buffer-name*)
464     (list :position (+ *buffer-start-position* pos))))
465     ((and (pathnamep file) (not *buffer-name*))
466     (make-location (list :file (unix-truename file))
467     (list :position (1+ pos))))
468     (t (break)))))
469    
470     (defun compiler-note-location (context)
471     "Derive the location of a complier message from its context.
472     Return a `location' record, or (:error <reason>) on failure."
473     (if (null context)
474     (note-error-location)
475     (let ((file (c::compiler-error-context-file-name context))
476     (source (c::compiler-error-context-original-source context))
477     (path
478     (reverse (c::compiler-error-context-original-source-path context))))
479     (or (locate-compiler-note file source path)
480     (note-error-location)))))
481    
482     (defun note-error-location ()
483     "Pseudo-location for notes that can't be located."
484     (list :error "No error location available."))
485    
486     (defun locate-compiler-note (file source source-path)
487     (cond ((and (eq file :stream) *buffer-name*)
488     ;; Compiling from a buffer
489     (let ((position (+ *buffer-start-position*
490     (source-path-string-position
491     source-path *buffer-substring*))))
492     (make-location (list :buffer *buffer-name*)
493     (list :position position))))
494     ((and (pathnamep file) (null *buffer-name*))
495     ;; Compiling from a file
496     (make-location (list :file (unix-truename file))
497     (list :position
498     (1+ (source-path-file-position
499     source-path file)))))
500     ((and (eq file :lisp) (stringp source))
501     ;; No location known, but we have the source form.
502     ;; XXX How is this case triggered? -luke (16/May/2004)
503     ;; This can happen if the compiler needs to expand a macro
504     ;; but the macro-expander is not yet compiled. Calling the
505     ;; (interpreted) macro-expander triggers IR1 conversion of
506     ;; the lambda expression for the expander and invokes the
507     ;; compiler recursively.
508     (make-location (list :source-form source)
509     (list :position 1)))))
510    
511     (defun unix-truename (pathname)
512     (ext:unix-namestring (truename pathname)))
513    
514    
515    
516     ;;; TODO
517     (defimplementation who-calls (name) nil)
518     (defimplementation who-references (name) nil)
519     (defimplementation who-binds (name) nil)
520     (defimplementation who-sets (name) nil)
521     (defimplementation who-specializes (symbol) nil)
522     (defimplementation who-macroexpands (name) nil)
523    
524    
525     ;;;; Find callers and callees
526     ;;;
527     ;;; Find callers and callees by looking at the constant pool of
528     ;;; compiled code objects. We assume every fdefn object in the
529     ;;; constant pool corresponds to a call to that function. A better
530     ;;; strategy would be to use the disassembler to find actual
531     ;;; call-sites.
532    
533     (declaim (inline map-code-constants))
534     (defun map-code-constants (code fn)
535     "Call 'fn for each constant in 'code's constant pool."
536     (check-type code kernel:code-component)
537     (loop for i from vm:code-constants-offset below (kernel:get-header-data code)
538     do (funcall fn (kernel:code-header-ref code i))))
539    
540     (defun function-callees (function)
541     "Return 'function's callees as a list of functions."
542     (let ((callees '()))
543     (map-code-constants
544     (vm::find-code-object function)
545     (lambda (obj)
546     (when (kernel:fdefn-p obj)
547     (push (kernel:fdefn-function obj) callees))))
548     callees))
549    
550     (declaim (ext:maybe-inline map-allocated-code-components))
551     (defun map-allocated-code-components (spaces fn)
552     "Call FN for each allocated code component in one of 'spaces. FN
553     receives the object as argument. 'spaces should be a list of the
554     symbols :dynamic, :static, or :read-only."
555     (dolist (space spaces)
556     (declare (inline vm::map-allocated-objects)
557     (optimize (ext:inhibit-warnings 3)))
558     (vm::map-allocated-objects
559     (lambda (obj header size)
560     (declare (type fixnum size) (ignore size))
561     (when (= vm:code-header-type header)
562     (funcall fn obj)))
563     space)))
564    
565     (declaim (ext:maybe-inline map-caller-code-components))
566     (defun map-caller-code-components (function spaces fn)
567     "Call 'fn for each code component with a fdefn for 'function in its
568     constant pool."
569     (let ((function (coerce function 'function)))
570     (declare (inline map-allocated-code-components))
571     (map-allocated-code-components
572     spaces
573     (lambda (obj)
574     (map-code-constants
575     obj
576     (lambda (constant)
577     (when (and (kernel:fdefn-p constant)
578     (eq (kernel:fdefn-function constant)
579     function))
580     (funcall fn obj))))))))
581    
582     (defun function-callers (function &optional (spaces '(:read-only :static
583     :dynamic)))
584     "Return 'function's callers. The result is a list of code-objects."
585     (let ((referrers '()))
586     (declare (inline map-caller-code-components))
587     (map-caller-code-components function spaces
588     (lambda (code) (push code referrers)))
589     referrers))
590    
591     (defun debug-info-definitions (debug-info)
592     "Return the defintions for a debug-info. This should only be used
593     for code-object without entry points, i.e., byte compiled
594     code (are theree others?)"
595     ;; This mess has only been tested with #'ext::skip-whitespace, a
596     ;; byte-compiled caller of #'read-char .
597     (check-type debug-info (and (not c::compiled-debug-info) c::debug-info))
598     (let ((name (c::debug-info-name debug-info))
599     (source (c::debug-info-source debug-info)))
600     (destructuring-bind (first) source
601     (ecase (c::debug-source-from first)
602     (:file
603     (list (list name
604     (make-location
605     (list :file (unix-truename (c::debug-source-name first)))
606     (list :function-name (string name))))))))))
607    
608     (defun valid-function-name-p (name)
609     (or (symbolp name) (and (consp name)
610     (eq (car name) 'setf)
611     (symbolp (cadr name))
612     (not (cddr name)))))
613    
614     (defun code-component-entry-points (code)
615     "Return a list ((name location) ...) of function definitons for
616     the code omponent 'code."
617     (let ((names '()))
618     (do ((f (kernel:%code-entry-points code) (kernel::%function-next f)))
619     ((not f))
620     (let ((name (kernel:%function-name f)))
621     (when (valid-function-name-p name)
622     (push (list name (function-location f)) names))))
623     names))
624    
625     (defimplementation list-callers (symbol)
626     "Return a list ((name location) ...) of callers."
627     (let ((components (function-callers symbol))
628     (xrefs '()))
629     (dolist (code components)
630     (let* ((entry (kernel:%code-entry-points code))
631     (defs (if entry
632     (code-component-entry-points code)
633     ;; byte compiled stuff
634     (debug-info-definitions
635     (kernel:%code-debug-info code)))))
636     (setq xrefs (nconc defs xrefs))))
637     xrefs))
638    
639     (defimplementation list-callees (symbol)
640     (let ((fns (function-callees symbol)))
641     (mapcar (lambda (fn)
642     (list (kernel:%function-name fn)
643     (function-location fn)))
644     fns)))
645    
646    
647     ;;;; Resolving source locations
648     ;;;
649     ;;; Our mission here is to "resolve" references to code locations into
650     ;;; actual file/buffer names and character positions. The references
651     ;;; we work from come out of the compiler's statically-generated debug
652     ;;; information, such as `code-location''s and `debug-source''s. For
653     ;;; more details, see the "Debugger Programmer's Interface" section of
654     ;;; the SCL manual.
655     ;;;
656     ;;; The first step is usually to find the corresponding "source-path"
657     ;;; for the location. Once we have the source-path we can pull up the
658     ;;; source file and `READ' our way through to the right position. The
659     ;;; main source-code groveling work is done in
660     ;;; `swank-source-path-parser.lisp'.
661    
662     (defvar *debug-definition-finding* nil
663     "When true don't handle errors while looking for definitions.
664     This is useful when debugging the definition-finding code.")
665    
666     (defvar *source-snippet-size* 256
667     "Maximum number of characters in a snippet of source code.
668     Snippets at the beginning of definitions are used to tell Emacs what
669     the definitions looks like, so that it can accurately find them by
670     text search.")
671    
672     (defmacro safe-definition-finding (&body body)
673     "Execute 'body and return the source-location it returns.
674     If an error occurs and `*debug-definition-finding*' is false, then
675     return an error pseudo-location.
676    
677     The second return value is 'nil if no error occurs, otherwise it is the
678     condition object."
679     `(flet ((body () ,@body))
680     (if *debug-definition-finding*
681     (body)
682     (handler-case (values (progn ,@body) nil)
683     (error (c) (values (list :error (princ-to-string c)) c))))))
684    
685     (defun code-location-source-location (code-location)
686     "Safe wrapper around `code-location-from-source-location'."
687     (safe-definition-finding
688     (source-location-from-code-location code-location)))
689    
690     (defun source-location-from-code-location (code-location)
691     "Return the source location for 'code-location."
692     (let ((debug-fun (di:code-location-debug-function code-location)))
693     (when (di::bogus-debug-function-p debug-fun)
694     ;; Those lousy cheapskates! They've put in a bogus debug source
695     ;; because the code was compiled at a low debug setting.
696     (error "Bogus debug function: ~A" debug-fun)))
697     (let* ((debug-source (di:code-location-debug-source code-location))
698     (from (di:debug-source-from debug-source))
699     (name (di:debug-source-name debug-source)))
700     (ecase from
701     (:file
702     (location-in-file name code-location debug-source))
703     (:stream
704     (location-in-stream code-location debug-source))
705     (:lisp
706     ;; The location comes from a form passed to `compile'.
707     ;; The best we can do is return the form itself for printing.
708     (make-location
709     (list :source-form (with-output-to-string (*standard-output*)
710     (debug::print-code-location-source-form
711     code-location 100 t)))
712     (list :position 1))))))
713    
714     (defun location-in-file (filename code-location debug-source)
715     "Resolve the source location for 'code-location in 'filename."
716     (let* ((code-date (di:debug-source-created debug-source))
717     (source-code (get-source-code filename code-date)))
718     (with-input-from-string (s source-code)
719     (make-location (list :file (unix-truename filename))
720     (list :position (1+ (code-location-stream-position
721     code-location s)))
722     `(:snippet ,(read-snippet s))))))
723    
724     (defun location-in-stream (code-location debug-source)
725     "Resolve the source location for a 'code-location from a stream.
726     This only succeeds if the code was compiled from an Emacs buffer."
727     (unless (debug-source-info-from-emacs-buffer-p debug-source)
728     (error "The code is compiled from a non-SLIME stream."))
729     (let* ((info (c::debug-source-info debug-source))
730     (string (getf info :emacs-buffer-string))
731     (position (code-location-string-offset
732     code-location
733     string)))
734     (make-location
735     (list :buffer (getf info :emacs-buffer))
736     (list :position (+ (getf info :emacs-buffer-offset) position))
737     (list :snippet (with-input-from-string (s string)
738     (file-position s position)
739     (read-snippet s))))))
740    
741     ;;;;; Function-name locations
742     ;;;
743     (defun debug-info-function-name-location (debug-info)
744     "Return a function-name source-location for 'debug-info.
745     Function-name source-locations are a fallback for when precise
746     positions aren't available."
747     (with-struct (c::debug-info- (fname name) source) debug-info
748     (with-struct (c::debug-source- info from name) (car source)
749     (ecase from
750     (:file
751     (make-location (list :file (namestring (truename name)))
752     (list :function-name (string fname))))
753     (:stream
754     (assert (debug-source-info-from-emacs-buffer-p (car source)))
755     (make-location (list :buffer (getf info :emacs-buffer))
756     (list :function-name (string fname))))
757     (:lisp
758     (make-location (list :source-form (princ-to-string (aref name 0)))
759     (list :position 1)))))))
760    
761     (defun debug-source-info-from-emacs-buffer-p (debug-source)
762     "Does the `info' slot of 'debug-source contain an Emacs buffer location?
763     This is true for functions that were compiled directly from buffers."
764     (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
765    
766     (defun info-from-emacs-buffer-p (info)
767     (and info
768     (consp info)
769     (eq :emacs-buffer (car info))))
770    
771    
772     ;;;;; Groveling source-code for positions
773    
774     (defun code-location-stream-position (code-location stream)
775     "Return the byte offset of 'code-location in 'stream. Extract the
776     toplevel-form-number and form-number from 'code-location and use that
777     to find the position of the corresponding form.
778    
779     Finish with 'stream positioned at the start of the code location."
780     (let* ((location (debug::maybe-block-start-location code-location))
781     (tlf-offset (di:code-location-top-level-form-offset location))
782     (form-number (di:code-location-form-number location)))
783     (let ((pos (form-number-stream-position tlf-offset form-number stream)))
784     (file-position stream pos)
785     pos)))
786    
787     (defun form-number-stream-position (tlf-number form-number stream)
788     "Return the starting character position of a form in 'stream.
789     'tlf-number is the top-level-form number.
790     'form-number is an index into a source-path table for the TLF."
791     (multiple-value-bind (tlf position-map) (read-source-form tlf-number stream)
792     (let* ((path-table (di:form-number-translations tlf 0))
793     (source-path
794     (if (<= (length path-table) form-number) ; source out of sync?
795     (list 0) ; should probably signal a condition
796     (reverse (cdr (aref path-table form-number))))))
797     (source-path-source-position source-path tlf position-map))))
798    
799     (defun code-location-string-offset (code-location string)
800     "Return the byte offset of 'code-location in 'string.
801     See 'code-location-stream-position."
802     (with-input-from-string (s string)
803     (code-location-stream-position code-location s)))
804    
805    
806     ;;;; Finding definitions
807    
808     ;;; There are a great many different types of definition for us to
809     ;;; find. We search for definitions of every kind and return them in a
810     ;;; list.
811    
812     (defimplementation find-definitions (name)
813     (append (function-definitions name)
814     (setf-definitions name)
815     (variable-definitions name)
816     (class-definitions name)
817     (type-definitions name)
818     (compiler-macro-definitions name)
819     (source-transform-definitions name)
820     (function-info-definitions name)
821     (ir1-translator-definitions name)))
822    
823     ;;;;; Functions, macros, generic functions, methods
824     ;;;
825     ;;; We make extensive use of the compile-time debug information that
826     ;;; SCL records, in particular "debug functions" and "code
827     ;;; locations." Refer to the "Debugger Programmer's Interface" section
828     ;;; of the SCL manual for more details.
829    
830     (defun function-definitions (name)
831     "Return definitions for 'name in the \"function namespace\", i.e.,
832     regular functions, generic functions, methods and macros.
833     'name can any valid function name (e.g, (setf car))."
834     (let ((macro? (and (symbolp name) (macro-function name)))
835     (special? (and (symbolp name) (special-operator-p name)))
836     (function? (and (valid-function-name-p name)
837     (ext:info :function :definition name)
838     (if (symbolp name) (fboundp name) t))))
839     (cond (macro?
840     (list `((defmacro ,name)
841     ,(function-location (macro-function name)))))
842     (special?
843     (list `((:special-operator ,name)
844     (:error ,(format nil "Special operator: ~S" name)))))
845     (function?
846     (let ((function (fdefinition name)))
847     (if (genericp function)
848     (generic-function-definitions name function)
849     (list (list `(function ,name)
850     (function-location function)))))))))
851    
852     ;;;;;; Ordinary (non-generic/macro/special) functions
853     ;;;
854     ;;; First we test if FUNCTION is a closure created by defstruct, and
855     ;;; if so extract the defstruct-description (`dd') from the closure
856     ;;; and find the constructor for the struct. Defstruct creates a
857     ;;; defun for the default constructor and we use that as an
858     ;;; approximation to the source location of the defstruct.
859     ;;;
860     ;;; For an ordinary function we return the source location of the
861     ;;; first code-location we find.
862     ;;;
863     (defun function-location (function)
864     "Return the source location for FUNCTION."
865     (cond ((struct-closure-p function)
866     (struct-closure-location function))
867     ((c::byte-function-or-closure-p function)
868     (byte-function-location function))
869     (t
870     (compiled-function-location function))))
871    
872     (defun compiled-function-location (function)
873     "Return the location of a regular compiled function."
874     (multiple-value-bind (code-location error)
875     (safe-definition-finding (function-first-code-location function))
876     (cond (error (list :error (princ-to-string error)))
877     (t (code-location-source-location code-location)))))
878    
879     (defun function-first-code-location (function)
880     "Return the first code-location we can find for 'function."
881     (and (function-has-debug-function-p function)
882     (di:debug-function-start-location
883     (di:function-debug-function function))))
884    
885     (defun function-has-debug-function-p (function)
886     (di:function-debug-function function))
887    
888     (defun function-code-object= (closure function)
889     (and (eq (vm::find-code-object closure)
890     (vm::find-code-object function))
891     (not (eq closure function))))
892    
893    
894     (defun byte-function-location (fn)
895     "Return the location of the byte-compiled function 'fn."
896     (etypecase fn
897     ((or c::hairy-byte-function c::simple-byte-function)
898     (let* ((component (c::byte-function-component fn))
899     (debug-info (kernel:%code-debug-info component)))
900     (debug-info-function-name-location debug-info)))
901     (c::byte-closure
902     (byte-function-location (c::byte-closure-function fn)))))
903    
904     ;;; Here we deal with structure accessors. Note that `dd' is a
905     ;;; "defstruct descriptor" structure in SCL. A `dd' describes a
906     ;;; `defstruct''d structure.
907    
908     (defun struct-closure-p (function)
909     "Is 'function a closure created by defstruct?"
910     (or (function-code-object= function #'kernel::structure-slot-accessor)
911     (function-code-object= function #'kernel::structure-slot-setter)
912     (function-code-object= function #'kernel::%defstruct)))
913    
914     (defun struct-closure-location (function)
915     "Return the location of the structure that 'function belongs to."
916     (assert (struct-closure-p function))
917     (safe-definition-finding
918     (dd-location (struct-closure-dd function))))
919    
920     (defun struct-closure-dd (function)
921     "Return the defstruct-definition (dd) of FUNCTION."
922     (assert (= (kernel:get-type function) vm:closure-header-type))
923     (flet ((find-layout (function)
924     (sys:find-if-in-closure
925     (lambda (x)
926     (let ((value (if (di::indirect-value-cell-p x)
927     (c:value-cell-ref x)
928     x)))
929     (when (kernel::layout-p value)
930     (return-from find-layout value))))
931     function)))
932     (kernel:layout-info (find-layout function))))
933    
934     (defun dd-location (dd)
935     "Return the location of a `defstruct'."
936     ;; Find the location in a constructor.
937     (function-location (struct-constructor dd)))
938    
939     (defun struct-constructor (dd)
940     "Return a constructor function from a defstruct definition.
941     Signal an error if no constructor can be found."
942     (let ((constructor (or (kernel:dd-default-constructor dd)
943     (car (kernel::dd-constructors dd)))))
944     (when (or (null constructor)
945     (and (consp constructor) (null (car constructor))))
946     (error "Cannot find structure's constructor: ~S"
947     (kernel::dd-name dd)))
948     (coerce (if (consp constructor) (first constructor) constructor)
949     'function)))
950    
951     ;;;;;; Generic functions and methods
952    
953     (defun generic-function-definitions (name function)
954     "Return the definitions of a generic function and its methods."
955     (cons (list `(defgeneric ,name) (gf-location function))
956     (gf-method-definitions function)))
957    
958     (defun gf-location (gf)
959     "Return the location of the generic function GF."
960     (definition-source-location gf (clos:generic-function-name gf)))
961    
962     (defun gf-method-definitions (gf)
963     "Return the locations of all methods of the generic function GF."
964     (mapcar #'method-definition (clos:generic-function-methods gf)))
965    
966     (defun method-definition (method)
967     (list (method-dspec method)
968     (method-location method)))
969    
970     (defun method-dspec (method)
971     "Return a human-readable \"definition specifier\" for METHOD."
972     (let* ((gf (clos:method-generic-function method))
973     (name (clos:generic-function-name gf))
974     (specializers (clos:method-specializers method))
975     (qualifiers (clos:method-qualifiers method)))
976     `(method ,name ,@qualifiers ,specializers #+nil (clos::unparse-specializers specializers))))
977    
978     ;; XXX maybe special case setters/getters
979     (defun method-location (method)
980     (function-location (clos:method-function method)))
981    
982     (defun genericp (fn)
983     (typep fn 'generic-function))
984    
985     ;;;;;; Types and classes
986    
987     (defun type-definitions (name)
988     "Return `deftype' locations for type NAME."
989     (maybe-make-definition (ext:info :type :expander name) 'deftype name))
990    
991     (defun maybe-make-definition (function kind name)
992     "If FUNCTION is non-nil then return its definition location."
993     (if function
994     (list (list `(,kind ,name) (function-location function)))))
995    
996     (defun class-definitions (name)
997     "Return the definition locations for the class called NAME."
998     (if (symbolp name)
999     (let ((class (find-class name nil)))
1000     (etypecase class
1001     (null '())
1002     (structure-class
1003     (list (list `(defstruct ,name)
1004     (dd-location (find-dd name)))))
1005     (standard-class
1006     (list (list `(defclass ,name)
1007     (class-location (find-class name)))))
1008     ((or built-in-class
1009     kernel:funcallable-structure-class)
1010     (list (list `(kernel::define-type-class ,name)
1011     `(:error
1012     ,(format nil "No source info for ~A" name)))))))))
1013    
1014     (defun class-location (class)
1015     "Return the `defclass' location for CLASS."
1016     (definition-source-location class (class-name class)))
1017    
1018     (defun find-dd (name)
1019     "Find the defstruct-definition by the name of its structure-class."
1020     (let ((layout (ext:info :type :compiler-layout name)))
1021     (if layout
1022     (kernel:layout-info layout))))
1023    
1024     (defun condition-class-location (class)
1025     (let ((name (class-name class)))
1026     `(:error ,(format nil "No location info for condition: ~A" name))))
1027    
1028     (defun make-name-in-file-location (file string)
1029     (multiple-value-bind (filename c)
1030     (ignore-errors
1031     (unix-truename (merge-pathnames (make-pathname :type "lisp")
1032     file)))
1033     (cond (filename (make-location `(:file ,filename)
1034     `(:function-name ,(string string))))
1035     (t (list :error (princ-to-string c))))))
1036    
1037     (defun definition-source-location (object name)
1038     `(:error ,(format nil "No source info for: ~A" object)))
1039    
1040     (defun setf-definitions (name)
1041     (let ((function (or (ext:info :setf :inverse name)
1042     (ext:info :setf :expander name))))
1043     (if function
1044     (list (list `(setf ,name)
1045     (function-location (coerce function 'function)))))))
1046    
1047    
1048     (defun variable-location (symbol)
1049     `(:error ,(format nil "No source info for variable ~S" symbol)))
1050    
1051     (defun variable-definitions (name)
1052     (if (symbolp name)
1053     (multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
1054     (if recorded-p
1055     (list (list `(variable ,kind ,name)
1056     (variable-location name)))))))
1057    
1058     (defun compiler-macro-definitions (symbol)
1059     (maybe-make-definition (compiler-macro-function symbol)
1060     'define-compiler-macro
1061     symbol))
1062    
1063     (defun source-transform-definitions (name)
1064     (maybe-make-definition (ext:info :function :source-transform name)
1065     'c:def-source-transform
1066     name))
1067    
1068     (defun function-info-definitions (name)
1069     (let ((info (ext:info :function :info name)))
1070     (if info
1071     (append (loop for transform in (c::function-info-transforms info)
1072     collect (list `(c:deftransform ,name
1073     ,(c::type-specifier
1074     (c::transform-type transform)))
1075     (function-location (c::transform-function
1076     transform))))
1077     (maybe-make-definition (c::function-info-derive-type info)
1078     'c::derive-type name)
1079     (maybe-make-definition (c::function-info-optimizer info)
1080     'c::optimizer name)
1081     (maybe-make-definition (c::function-info-ltn-annotate info)
1082     'c::ltn-annotate name)
1083     (maybe-make-definition (c::function-info-ir2-convert info)
1084     'c::ir2-convert name)
1085     (loop for template in (c::function-info-templates info)
1086     collect (list `(c::vop ,(c::template-name template))
1087     (function-location
1088     (c::vop-info-generator-function
1089     template))))))))
1090    
1091     (defun ir1-translator-definitions (name)
1092     (maybe-make-definition (ext:info :function :ir1-convert name)
1093     'c:def-ir1-translator name))
1094    
1095    
1096     ;;;; Documentation.
1097    
1098     (defimplementation describe-symbol-for-emacs (symbol)
1099     (let ((result '()))
1100     (flet ((doc (kind)
1101     (or (documentation symbol kind) :not-documented))
1102     (maybe-push (property value)
1103     (when value
1104     (setf result (list* property value result)))))
1105     (maybe-push
1106     :variable (multiple-value-bind (kind recorded-p)
1107     (ext:info variable kind symbol)
1108     (declare (ignore kind))
1109     (if (or (boundp symbol) recorded-p)
1110     (doc 'variable))))
1111     (when (fboundp symbol)
1112     (maybe-push
1113     (cond ((macro-function symbol) :macro)
1114     ((special-operator-p symbol) :special-operator)
1115     ((genericp (fdefinition symbol)) :generic-function)
1116     (t :function))
1117     (doc 'function)))
1118     (maybe-push
1119     :setf (if (or (ext:info setf inverse symbol)
1120     (ext:info setf expander symbol))
1121     (doc 'setf)))
1122     (maybe-push
1123     :type (if (ext:info type kind symbol)
1124     (doc 'type)))
1125     (maybe-push
1126     :class (if (find-class symbol nil)
1127     (doc 'class)))
1128     (maybe-push
1129     :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
1130     (doc 'alien-type)))
1131     (maybe-push
1132     :alien-struct (if (ext:info alien-type struct symbol)
1133     (doc nil)))
1134     (maybe-push
1135     :alien-union (if (ext:info alien-type union symbol)
1136     (doc nil)))
1137     (maybe-push
1138     :alien-enum (if (ext:info alien-type enum symbol)
1139     (doc nil)))
1140     result)))
1141    
1142     (defimplementation describe-definition (symbol namespace)
1143     (describe (ecase namespace
1144     (:variable
1145     symbol)
1146     ((:function :generic-function)
1147     (symbol-function symbol))
1148     (:setf
1149     (or (ext:info setf inverse symbol)
1150     (ext:info setf expander symbol)))
1151     (:type
1152     (kernel:values-specifier-type symbol))
1153     (:class
1154     (find-class symbol))
1155     (:alien-struct
1156     (ext:info :alien-type :struct symbol))
1157     (:alien-union
1158     (ext:info :alien-type :union symbol))
1159     (:alien-enum
1160     (ext:info :alien-type :enum symbol))
1161     (:alien-type
1162     (ecase (ext:info :alien-type :kind symbol)
1163     (:primitive
1164     (let ((alien::*values-type-okay* t))
1165     (funcall (ext:info :alien-type :translator symbol)
1166     (list symbol))))
1167     ((:defined)
1168     (ext:info :alien-type :definition symbol))
1169 heller 1.10 (:unknown :unknown))))))
1170 dcrosher 1.1
1171     ;;;;; Argument lists
1172    
1173 heller 1.10 (defimplementation arglist (fun)
1174 dcrosher 1.11 (etypecase fun
1175     (function (function-arglist fun))
1176     (symbol (function-arglist (or (macro-function fun)
1177     (symbol-function fun))))))
1178 dcrosher 1.1
1179 dcrosher 1.11 (defun function-arglist (fun)
1180 dcrosher 1.1 (flet ((compiled-function-arglist (x)
1181     (let ((args (kernel:%function-arglist x)))
1182     (if args
1183     (read-arglist x)
1184     :not-available))))
1185     (case (kernel:get-type fun)
1186     (#.vm:closure-header-type
1187     (compiled-function-arglist
1188     (kernel:%closure-function fun)))
1189     ((#.vm:function-header-type #.vm:closure-function-header-type)
1190     (compiled-function-arglist fun))
1191     (#.vm:funcallable-instance-header-type
1192     (typecase fun
1193     (kernel:byte-function
1194     :not-available)
1195     (kernel:byte-closure
1196     :not-available)
1197     (eval:interpreted-function
1198     (eval:interpreted-function-arglist fun))
1199     (otherwise
1200     (clos::generic-function-lambda-list fun))))
1201     (t
1202     :non-available))))
1203    
1204     (defimplementation function-name (function)
1205     (cond ((eval:interpreted-function-p function)
1206     (eval:interpreted-function-name function))
1207     ((typep function 'generic-function)
1208     (clos:generic-function-name function))
1209     ((c::byte-function-or-closure-p function)
1210     (c::byte-function-name function))
1211     (t (kernel:%function-name (kernel:%function-self function)))))
1212    
1213     ;;; A simple case: the arglist is available as a string that we can
1214     ;;; `read'.
1215    
1216     (defun read-arglist (fn)
1217     "Parse the arglist-string of the function object FN."
1218     (let ((string (kernel:%function-arglist
1219     (kernel:%function-self fn)))
1220     (package (find-package
1221     (c::compiled-debug-info-package
1222     (kernel:%code-debug-info
1223     (vm::find-code-object fn))))))
1224     (with-standard-io-syntax
1225     (let ((*package* (or package *package*)))
1226     (read-from-string string)))))
1227    
1228     ;;; A harder case: an approximate arglist is derived from available
1229     ;;; debugging information.
1230    
1231     (defun debug-function-arglist (debug-function)
1232     "Derive the argument list of DEBUG-FUNCTION from debug info."
1233     (let ((args (di::debug-function-lambda-list debug-function))
1234     (required '())
1235     (optional '())
1236     (rest '())
1237     (key '()))
1238     ;; collect the names of debug-vars
1239     (dolist (arg args)
1240     (etypecase arg
1241     (di::debug-variable
1242     (push (di::debug-variable-symbol arg) required))
1243     ((member :deleted)
1244     (push ':deleted required))
1245     (cons
1246     (ecase (car arg)
1247     (:keyword
1248     (push (second arg) key))
1249     (:optional
1250     (push (debug-variable-symbol-or-deleted (second arg)) optional))
1251     (:rest
1252     (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1253     ;; intersperse lambda keywords as needed
1254     (append (nreverse required)
1255     (if optional (cons '&optional (nreverse optional)))
1256     (if rest (cons '&rest (nreverse rest)))
1257     (if key (cons '&key (nreverse key))))))
1258    
1259     (defun debug-variable-symbol-or-deleted (var)
1260     (etypecase var
1261     (di:debug-variable
1262     (di::debug-variable-symbol var))
1263     ((member :deleted)
1264     '#:deleted)))
1265    
1266     (defun symbol-debug-function-arglist (fname)
1267     "Return FNAME's debug-function-arglist and %function-arglist.
1268     A utility for debugging DEBUG-FUNCTION-ARGLIST."
1269     (let ((fn (fdefinition fname)))
1270     (values (debug-function-arglist (di::function-debug-function fn))
1271     (kernel:%function-arglist (kernel:%function-self fn)))))
1272    
1273     ;;; Deriving arglists for byte-compiled functions:
1274     ;;;
1275     (defun byte-code-function-arglist (fn)
1276     ;; There doesn't seem to be much arglist information around for
1277     ;; byte-code functions. Use the arg-count and return something like
1278     ;; (arg0 arg1 ...)
1279     (etypecase fn
1280     (c::simple-byte-function
1281     (loop for i from 0 below (c::simple-byte-function-num-args fn)
1282     collect (make-arg-symbol i)))
1283     (c::hairy-byte-function
1284     (hairy-byte-function-arglist fn))
1285     (c::byte-closure
1286     (byte-code-function-arglist (c::byte-closure-function fn)))))
1287    
1288     (defun make-arg-symbol (i)
1289     (make-symbol (format nil "~A~D" (string 'arg) i)))
1290    
1291     ;;; A "hairy" byte-function is one that takes a variable number of
1292     ;;; arguments. `hairy-byte-function' is a type from the bytecode
1293     ;;; interpreter.
1294     ;;;
1295     (defun hairy-byte-function-arglist (fn)
1296     (let ((counter -1))
1297     (flet ((next-arg () (make-arg-symbol (incf counter))))
1298     (with-struct (c::hairy-byte-function- min-args max-args rest-arg-p
1299     keywords-p keywords) fn
1300     (let ((arglist '())
1301     (optional (- max-args min-args)))
1302     ;; XXX isn't there a better way to write this?
1303     ;; (Looks fine to me. -luke)
1304     (dotimes (i min-args)
1305     (push (next-arg) arglist))
1306     (when (plusp optional)
1307     (push '&optional arglist)
1308     (dotimes (i optional)
1309     (push (next-arg) arglist)))
1310     (when rest-arg-p
1311     (push '&rest arglist)
1312     (push (next-arg) arglist))
1313     (when keywords-p
1314     (push '&key arglist)
1315     (loop for (key _ __) in keywords
1316     do (push key arglist))
1317     (when (eq keywords-p :allow-others)
1318     (push '&allow-other-keys arglist)))
1319     (nreverse arglist))))))
1320    
1321    
1322     ;;;; Miscellaneous.
1323    
1324     (defimplementation macroexpand-all (form)
1325     (macroexpand form))
1326    
1327     (defimplementation set-default-directory (directory)
1328     (setf (ext:default-directory) (namestring directory))
1329     ;; Setting *default-pathname-defaults* to an absolute directory
1330     ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1331     (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1332     (default-directory))
1333    
1334     (defimplementation default-directory ()
1335     (namestring (ext:default-directory)))
1336    
1337     (defimplementation call-without-interrupts (fn)
1338     (funcall fn))
1339    
1340     (defimplementation getpid ()
1341     (unix:unix-getpid))
1342    
1343     (defimplementation lisp-implementation-type-name ()
1344     (if (eq ext:*case-mode* :upper) "scl" "scl-lower"))
1345    
1346     (defimplementation quit-lisp ()
1347     (ext:quit))
1348    
1349     ;;; source-path-{stream,file,string,etc}-position moved into
1350     ;;; swank-source-path-parser
1351    
1352    
1353     ;;;; Debugging
1354    
1355     (defvar *sldb-stack-top*)
1356    
1357     (defimplementation call-with-debugging-environment (debugger-loop-fn)
1358     (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1359     (debug:*stack-top-hint* nil)
1360     (kernel:*current-level* 0))
1361     (handler-bind ((di::unhandled-condition
1362     (lambda (condition)
1363     (error (make-condition
1364     'sldb-condition
1365     :original-condition condition)))))
1366     (funcall debugger-loop-fn))))
1367    
1368     (defun frame-down (frame)
1369     (handler-case (di:frame-down frame)
1370     (di:no-debug-info () nil)))
1371    
1372     (defun nth-frame (index)
1373     (do ((frame *sldb-stack-top* (frame-down frame))
1374     (i index (1- i)))
1375     ((zerop i) frame)))
1376    
1377     (defimplementation compute-backtrace (start end)
1378     (let ((end (or end most-positive-fixnum)))
1379     (loop for f = (nth-frame start) then (frame-down f)
1380     for i from start below end
1381     while f
1382     collect f)))
1383    
1384     (defimplementation print-frame (frame stream)
1385     (let ((*standard-output* stream))
1386     (handler-case
1387     (debug::print-frame-call frame :verbosity 1 :number nil)
1388     (error (e)
1389     (ignore-errors (princ e stream))))))
1390    
1391     (defimplementation frame-source-location-for-emacs (index)
1392     (code-location-source-location (di:frame-code-location (nth-frame index))))
1393    
1394     (defimplementation eval-in-frame (form index)
1395     (di:eval-in-frame (nth-frame index) form))
1396    
1397     (defun frame-debug-vars (frame)
1398     "Return a vector of debug-variables in frame."
1399     (di::debug-function-debug-variables (di:frame-debug-function frame)))
1400    
1401     (defun debug-var-value (var frame location)
1402     (let ((validity (di:debug-variable-validity var location)))
1403     (ecase validity
1404     (:valid (di:debug-variable-value var frame))
1405     ((:invalid :unknown) (make-symbol (string validity))))))
1406    
1407     (defimplementation frame-locals (index)
1408     (let* ((frame (nth-frame index))
1409     (loc (di:frame-code-location frame))
1410     (vars (frame-debug-vars frame)))
1411     (loop for v across vars collect
1412     (list :name (di:debug-variable-symbol v)
1413     :id (di:debug-variable-id v)
1414     :value (debug-var-value v frame loc)))))
1415    
1416     (defimplementation frame-var-value (frame var)
1417     (let* ((frame (nth-frame frame))
1418     (dvar (aref (frame-debug-vars frame) var)))
1419     (debug-var-value dvar frame (di:frame-code-location frame))))
1420    
1421     (defimplementation frame-catch-tags (index)
1422     (mapcar #'car (di:frame-catches (nth-frame index))))
1423    
1424     (defimplementation return-from-frame (index form)
1425 dcrosher 1.4 (let ((sym (find-symbol (symbol-name '#:find-debug-tag-for-frame)
1426 dcrosher 1.1 :debug-internals)))
1427     (if sym
1428     (let* ((frame (nth-frame index))
1429     (probe (funcall sym frame)))
1430     (cond (probe (throw (car probe) (eval-in-frame form index)))
1431     (t (format nil "Cannot return from frame: ~S" frame))))
1432     "return-from-frame is not implemented in this version of SCL.")))
1433    
1434     (defimplementation activate-stepping (frame)
1435     (set-step-breakpoints (nth-frame frame)))
1436    
1437     (defimplementation sldb-break-on-return (frame)
1438     (break-on-return (nth-frame frame)))
1439    
1440     ;;; We set the breakpoint in the caller which might be a bit confusing.
1441     ;;;
1442     (defun break-on-return (frame)
1443     (let* ((caller (di:frame-down frame))
1444     (cl (di:frame-code-location caller)))
1445     (flet ((hook (frame bp)
1446     (when (frame-pointer= frame caller)
1447     (di:delete-breakpoint bp)
1448     (signal-breakpoint bp frame))))
1449     (let* ((info (ecase (di:code-location-kind cl)
1450     ((:single-value-return :unknown-return) nil)
1451     (:known-return (debug-function-returns
1452     (di:frame-debug-function frame)))))
1453     (bp (di:make-breakpoint #'hook cl :kind :code-location
1454     :info info)))
1455     (di:activate-breakpoint bp)
1456     `(:ok ,(format nil "Set breakpoint in ~A" caller))))))
1457    
1458     (defun frame-pointer= (frame1 frame2)
1459     "Return true if the frame pointers of FRAME1 and FRAME2 are the same."
1460     (sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
1461    
1462     ;;; The PC in escaped frames at a single-return-value point is
1463     ;;; actually vm:single-value-return-byte-offset bytes after the
1464     ;;; position given in the debug info. Here we try to recognize such
1465     ;;; cases.
1466     ;;;
1467     (defun next-code-locations (frame code-location)
1468     "Like `debug::next-code-locations' but be careful in escaped frames."
1469     (let ((next (debug::next-code-locations code-location)))
1470     (flet ((adjust-pc ()
1471     (let ((cl (di::copy-compiled-code-location code-location)))
1472     (incf (di::compiled-code-location-pc cl)
1473     vm:single-value-return-byte-offset)
1474     cl)))
1475     (cond ((and (di::compiled-frame-escaped frame)
1476     (eq (di:code-location-kind code-location)
1477     :single-value-return)
1478     (= (length next) 1)
1479     (di:code-location= (car next) (adjust-pc)))
1480     (debug::next-code-locations (car next)))
1481     (t
1482     next)))))
1483    
1484     (defun set-step-breakpoints (frame)
1485     (let ((cl (di:frame-code-location frame)))
1486     (when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
1487     (error "Cannot step in elsewhere code"))
1488     (let* ((debug::*bad-code-location-types*
1489     (remove :call-site debug::*bad-code-location-types*))
1490     (next (next-code-locations frame cl)))
1491     (cond (next
1492     (let ((steppoints '()))
1493     (flet ((hook (bp-frame bp)
1494     (signal-breakpoint bp bp-frame)
1495     (mapc #'di:delete-breakpoint steppoints)))
1496     (dolist (code-location next)
1497     (let ((bp (di:make-breakpoint #'hook code-location
1498     :kind :code-location)))
1499     (di:activate-breakpoint bp)
1500     (push bp steppoints))))))
1501     (t
1502     (break-on-return frame))))))
1503    
1504    
1505     ;; XXX the return values at return breakpoints should be passed to the
1506     ;; user hooks. debug-int.lisp should be changed to do this cleanly.
1507    
1508     ;;; The sigcontext and the PC for a breakpoint invocation are not
1509     ;;; passed to user hook functions, but we need them to extract return
1510     ;;; values. So we advice di::handle-breakpoint and bind the values to
1511     ;;; special variables.
1512     ;;;
1513     (defvar *breakpoint-sigcontext*)
1514     (defvar *breakpoint-pc*)
1515    
1516     (defun sigcontext-object (sc index)
1517     "Extract the lisp object in sigcontext SC at offset INDEX."
1518     (kernel:make-lisp-obj (vm:ucontext-register sc index)))
1519    
1520     (defun known-return-point-values (sigcontext sc-offsets)
1521     (let ((fp (system:int-sap (vm:ucontext-register sigcontext
1522     vm::cfp-offset))))
1523     (system:without-gcing
1524     (loop for sc-offset across sc-offsets
1525     collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
1526    
1527     ;;; SCL returns the first few values in registers and the rest on
1528     ;;; the stack. In the multiple value case, the number of values is
1529     ;;; stored in a dedicated register. The values of the registers can be
1530     ;;; accessed in the sigcontext for the breakpoint. There are 3 kinds
1531     ;;; of return conventions: :single-value-return, :unknown-return, and
1532     ;;; :known-return.
1533     ;;;
1534     ;;; The :single-value-return convention returns the value in a
1535     ;;; register without setting the nargs registers.
1536     ;;;
1537     ;;; The :unknown-return variant is used for multiple values. A
1538     ;;; :unknown-return point consists actually of 2 breakpoints: one for
1539     ;;; the single value case and one for the general case. The single
1540     ;;; value breakpoint comes vm:single-value-return-byte-offset after
1541     ;;; the multiple value breakpoint.
1542     ;;;
1543     ;;; The :known-return convention is used by local functions.
1544     ;;; :known-return is currently not supported because we don't know
1545     ;;; where the values are passed.
1546     ;;;
1547     (defun breakpoint-values (breakpoint)
1548     "Return the list of return values for a return point."
1549     (flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
1550     (let ((sc (locally (declare (optimize (ext:inhibit-warnings 3)))
1551     (alien:sap-alien *breakpoint-sigcontext* (* unix:ucontext))))
1552     (cl (di:breakpoint-what breakpoint)))
1553     (ecase (di:code-location-kind cl)
1554     (:single-value-return
1555     (list (1st sc)))
1556     (:known-return
1557     (let ((info (di:breakpoint-info breakpoint)))
1558     (if (vectorp info)
1559     (known-return-point-values sc info)
1560     (progn
1561     ;;(break)
1562     (list "<<known-return convention not supported>>" info)))))
1563     (:unknown-return
1564     (let ((mv-return-pc (di::compiled-code-location-pc cl)))
1565     (if (= mv-return-pc *breakpoint-pc*)
1566     (mv-function-end-breakpoint-values sc)
1567     (list (1st sc)))))))))
1568    
1569     (defun mv-function-end-breakpoint-values (sigcontext)
1570 dcrosher 1.4 (let ((sym (find-symbol (symbol-name '#:function-end-breakpoint-values/standard)
1571     :debug-internals)))
1572 dcrosher 1.1 (cond (sym (funcall sym sigcontext))
1573     (t (di::get-function-end-breakpoint-values sigcontext)))))
1574    
1575     (defun debug-function-returns (debug-fun)
1576     "Return the return style of DEBUG-FUN."
1577     (let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
1578     (c::compiled-debug-function-returns cdfun)))
1579    
1580     (define-condition breakpoint (simple-condition)
1581     ((message :initarg :message :reader breakpoint.message)
1582     (values :initarg :values :reader breakpoint.values))
1583     (:report (lambda (c stream) (princ (breakpoint.message c) stream))))
1584    
1585 heller 1.10 #+nil
1586 dcrosher 1.1 (defimplementation condition-extras ((c breakpoint))
1587     ;; simply pop up the source buffer
1588     `((:short-frame-source 0)))
1589    
1590     (defun signal-breakpoint (breakpoint frame)
1591     "Signal a breakpoint condition for BREAKPOINT in FRAME.
1592     Try to create a informative message."
1593     (flet ((brk (values fstring &rest args)
1594     (let ((msg (apply #'format nil fstring args))
1595     (debug:*stack-top-hint* frame))
1596     (break 'breakpoint :message msg :values values))))
1597     (with-struct (di::breakpoint- kind what) breakpoint
1598     (case kind
1599     (:code-location
1600     (case (di:code-location-kind what)
1601     ((:single-value-return :known-return :unknown-return)
1602     (let ((values (breakpoint-values breakpoint)))
1603     (brk values "Return value: ~{~S ~}" values)))
1604     (t
1605     #+(or)
1606     (when (eq (di:code-location-kind what) :call-site)
1607     (call-site-function breakpoint frame))
1608     (brk nil "Breakpoint: ~S ~S"
1609     (di:code-location-kind what)
1610     (di::compiled-code-location-pc what)))))
1611     (:function-start
1612     (brk nil "Function start breakpoint"))
1613     (t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
1614    
1615     #+nil
1616     (defimplementation sldb-break-at-start (fname)
1617     (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1618     (cond ((not debug-fun)
1619     `(:error ,(format nil "~S has no debug-function" fname)))
1620     (t
1621     (flet ((hook (frame bp &optional args cookie)
1622     (declare (ignore args cookie))
1623     (signal-breakpoint bp frame)))
1624     (let ((bp (di:make-breakpoint #'hook debug-fun
1625     :kind :function-start)))
1626     (di:activate-breakpoint bp)
1627     `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1628    
1629     (defun frame-cfp (frame)
1630     "Return the Control-Stack-Frame-Pointer for FRAME."
1631     (etypecase frame
1632     (di::compiled-frame (di::frame-pointer frame))
1633     ((or di::interpreted-frame null) -1)))
1634    
1635     (defun frame-ip (frame)
1636     "Return the (absolute) instruction pointer and the relative pc of FRAME."
1637     (if (not frame)
1638     -1
1639     (let ((debug-fun (di::frame-debug-function frame)))
1640     (etypecase debug-fun
1641     (di::compiled-debug-function
1642     (let* ((code-loc (di:frame-code-location frame))
1643     (component (di::compiled-debug-function-component debug-fun))
1644     (pc (di::compiled-code-location-pc code-loc))
1645     (ip (sys:without-gcing
1646     (sys:sap-int
1647     (sys:sap+ (kernel:code-instructions component) pc)))))
1648     (values ip pc)))
1649     ((or di::bogus-debug-function di::interpreted-debug-function)
1650     -1)))))
1651    
1652     (defun frame-registers (frame)
1653     "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1654     (let* ((cfp (frame-cfp frame))
1655     (csp (frame-cfp (di::frame-up frame)))
1656     (ip (frame-ip frame))
1657     (ocfp (frame-cfp (di::frame-down frame)))
1658     (lra (frame-ip (di::frame-down frame))))
1659     (values csp cfp ip ocfp lra)))
1660    
1661     (defun print-frame-registers (frame-number)
1662     (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1663     (flet ((fixnum (p) (etypecase p
1664     (integer p)
1665     (sys:system-area-pointer (sys:sap-int p)))))
1666     (apply #'format t "~
1667     CSP = ~X
1668     CFP = ~X
1669     IP = ~X
1670     OCFP = ~X
1671     LRA = ~X~%" (mapcar #'fixnum
1672     (multiple-value-list (frame-registers frame)))))))
1673    
1674    
1675     (defimplementation disassemble-frame (frame-number)
1676     "Return a string with the disassembly of frames code."
1677     (print-frame-registers frame-number)
1678     (terpri)
1679     (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1680     (debug-fun (di::frame-debug-function frame)))
1681     (etypecase debug-fun
1682     (di::compiled-debug-function
1683     (let* ((component (di::compiled-debug-function-component debug-fun))
1684     (fun (di:debug-function-function debug-fun)))
1685     (if fun
1686     (disassemble fun)
1687     (disassem:disassemble-code-component component))))
1688     (di::bogus-debug-function
1689     (format t "~%[Disassembling bogus frames not implemented]")))))
1690    
1691    
1692     ;;;; Inspecting
1693    
1694 heller 1.13 (defclass scl-inspector (backend-inspector) ())
1695 dcrosher 1.1
1696     (defimplementation make-default-inspector ()
1697     (make-instance 'scl-inspector))
1698    
1699     (defconstant +lowtag-symbols+
1700     '(vm:even-fixnum-type
1701     vm:instance-pointer-type
1702     vm:other-immediate-0-type
1703     vm:list-pointer-type
1704     vm:odd-fixnum-type
1705     vm:function-pointer-type
1706     vm:other-immediate-1-type
1707     vm:other-pointer-type)
1708     "Names of the constants that specify type tags.
1709     The `symbol-value' of each element is a type tag.")
1710    
1711     (defconstant +header-type-symbols+
1712     (labels ((suffixp (suffix string)
1713     (and (>= (length string) (length suffix))
1714     (string= string suffix :start1 (- (length string)
1715     (length suffix)))))
1716     (header-type-symbol-p (x)
1717     (and (suffixp (symbol-name '#:-type) (symbol-name x))
1718     (not (member x +lowtag-symbols+))
1719     (boundp x)
1720     (typep (symbol-value x) 'fixnum))))
1721     (remove-if-not #'header-type-symbol-p
1722     (append (apropos-list (symbol-name '#:-type) :vm)
1723     (apropos-list (symbol-name '#:-type) :bignum))))
1724     "A list of names of the type codes in boxed objects.")
1725    
1726     (defimplementation describe-primitive-type (object)
1727     (with-output-to-string (*standard-output*)
1728     (let* ((lowtag (kernel:get-lowtag object))
1729     (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1730     (format t "lowtag: ~A" lowtag-symbol)
1731     (when (member lowtag (list vm:other-pointer-type
1732     vm:function-pointer-type
1733     vm:other-immediate-0-type
1734     vm:other-immediate-1-type
1735     ))
1736     (let* ((type (kernel:get-type object))
1737     (type-symbol (find type +header-type-symbols+
1738     :key #'symbol-value)))
1739     (format t ", type: ~A" type-symbol))))))
1740    
1741 heller 1.13 (defmethod inspect-for-emacs ((o t) (inspector backend-inspector))
1742 dcrosher 1.1 (cond ((di::indirect-value-cell-p o)
1743     (values (format nil "~A is a value cell." o)
1744     `("Value: " (:value ,(c:value-cell-ref o)))))
1745     ((alien::alien-value-p o)
1746     (inspect-alien-value o))
1747     (t
1748     (scl-inspect o))))
1749    
1750     (defun scl-inspect (o)
1751 dcrosher 1.4 (destructuring-bind (text labeledp . parts)
1752     (inspect::describe-parts o)
1753 dcrosher 1.1 (values (format nil "~A~%" text)
1754     (if labeledp
1755     (loop for (label . value) in parts
1756     append (label-value-line label value))
1757     (loop for value in parts for i from 0
1758     append (label-value-line i value))))))
1759    
1760 heller 1.13 (defmethod inspect-for-emacs ((o function) (inspector backend-inspector))
1761 dcrosher 1.1 (declare (ignore inspector))
1762     (let ((header (kernel:get-type o)))
1763     (cond ((= header vm:function-header-type)
1764     (values (format nil "~A is a function." o)
1765     (append (label-value-line*
1766     ("Self" (kernel:%function-self o))
1767     ("Next" (kernel:%function-next o))
1768     ("Name" (kernel:%function-name o))
1769     ("Arglist" (kernel:%function-arglist o))
1770     ("Type" (kernel:%function-type o))
1771     ("Code" (kernel:function-code-header o)))
1772     (list
1773     (with-output-to-string (s)
1774     (disassem:disassemble-function o :stream s))))))
1775     ((= header vm:closure-header-type)
1776     (values (format nil "~A is a closure" o)
1777     (append
1778     (label-value-line "Function" (kernel:%closure-function o))
1779     `("Environment:" (:newline))
1780 dcrosher 1.7 (loop for i from 0 below (- (kernel:get-closure-length o)
1781     (1- vm:closure-info-offset))
1782 dcrosher 1.1 append (label-value-line
1783     i (kernel:%closure-index-ref o i))))))
1784     ((eval::interpreted-function-p o)
1785     (scl-inspect o))
1786     (t
1787     (call-next-method)))))
1788    
1789    
1790 heller 1.13 (defmethod inspect-for-emacs ((o kernel:code-component) (_ backend-inspector))
1791 dcrosher 1.1 (declare (ignore _))
1792     (values (format nil "~A is a code data-block." o)
1793     (append
1794     (label-value-line*
1795     ("code-size" (kernel:%code-code-size o))
1796     ("entry-points" (kernel:%code-entry-points o))
1797     ("debug-info" (kernel:%code-debug-info o))
1798     ("trace-table-offset" (kernel:code-header-ref
1799     o vm:code-trace-table-offset-slot)))
1800     `("Constants:" (:newline))
1801     (loop for i from vm:code-constants-offset
1802     below (kernel:get-header-data o)
1803     append (label-value-line i (kernel:code-header-ref o i)))
1804     `("Code:" (:newline)
1805     , (with-output-to-string (s)
1806     (cond ((kernel:%code-debug-info o)
1807     (disassem:disassemble-code-component o :stream s))
1808     (t
1809     (disassem:disassemble-memory
1810     (disassem::align
1811     (+ (logandc2 (kernel:get-lisp-obj-address o)
1812     vm:lowtag-mask)
1813     (* vm:code-constants-offset vm:word-bytes))
1814     (ash 1 vm:lowtag-bits))
1815     (ash (kernel:%code-code-size o) vm:word-shift)
1816     :stream s))))))))
1817    
1818 heller 1.13 (defmethod inspect-for-emacs ((o kernel:fdefn) (inspector backend-inspector))
1819 dcrosher 1.1 (declare (ignore inspector))
1820     (values (format nil "~A is a fdenf object." o)
1821     (label-value-line*
1822     ("name" (kernel:fdefn-name o))
1823     ("function" (kernel:fdefn-function o))
1824     ("raw-addr" (sys:sap-ref-32
1825     (sys:int-sap (kernel:get-lisp-obj-address o))
1826     (* vm:fdefn-raw-addr-slot vm:word-bytes))))))
1827    
1828 heller 1.13 (defmethod inspect-for-emacs ((o array) (inspector backend-inspector))
1829 dcrosher 1.1 inspector
1830 dcrosher 1.4 (cond ((kernel:array-header-p o)
1831     (values (format nil "~A is an array." o)
1832     (label-value-line*
1833     (:header (describe-primitive-type o))
1834     (:rank (array-rank o))
1835     (:fill-pointer (kernel:%array-fill-pointer o))
1836     (:fill-pointer-p (kernel:%array-fill-pointer-p o))
1837     (:elements (kernel:%array-available-elements o))
1838     (:data (kernel:%array-data-vector o))
1839     (:displacement (kernel:%array-displacement o))
1840     (:displaced-p (kernel:%array-displaced-p o))
1841     (:dimensions (array-dimensions o)))))
1842     (t
1843     (values (format nil "~A is an simple-array." o)
1844     (label-value-line*
1845     (:header (describe-primitive-type o))
1846     (:length (length o)))))))
1847 dcrosher 1.1
1848 heller 1.13 (defmethod inspect-for-emacs ((o simple-vector) (inspector backend-inspector))
1849 dcrosher 1.1 inspector
1850     (values (format nil "~A is a vector." o)
1851     (append
1852     (label-value-line*
1853     (:header (describe-primitive-type o))
1854     (:length (c::vector-length o)))
1855     (unless (eq (array-element-type o) 'nil)
1856     (loop for i below (length o)
1857     append (label-value-line i (aref o i)))))))
1858    
1859     (defun inspect-alien-record (alien)
1860     (values
1861     (format nil "~A is an alien value." alien)
1862     (with-struct (alien::alien-value- sap type) alien
1863     (with-struct (alien::alien-record-type- kind name fields) type
1864     (append
1865     (label-value-line*
1866     (:sap sap)
1867     (:kind kind)
1868     (:name name))
1869     (loop for field in fields
1870     append (let ((slot (alien::alien-record-field-name field)))
1871     (label-value-line slot (alien:slot alien slot)))))))))
1872    
1873     (defun inspect-alien-pointer (alien)
1874     (values
1875     (format nil "~A is an alien value." alien)
1876     (with-struct (alien::alien-value- sap type) alien
1877     (label-value-line*
1878     (:sap sap)
1879     (:type type)
1880     (:to (alien::deref alien))))))
1881    
1882     (defun inspect-alien-value (alien)
1883     (typecase (alien::alien-value-type alien)
1884     (alien::alien-record-type (inspect-alien-record alien))
1885     (alien::alien-pointer-type (inspect-alien-pointer alien))
1886     (t (scl-inspect alien))))
1887    
1888     ;;;; Profiling
1889     (defimplementation profile (fname)
1890     (eval `(profile:profile ,fname)))
1891    
1892     (defimplementation unprofile (fname)
1893     (eval `(profile:unprofile ,fname)))
1894    
1895     (defimplementation unprofile-all ()
1896     (eval `(profile:unprofile))
1897     "All functions unprofiled.")
1898    
1899     (defimplementation profile-report ()
1900     (eval `(profile:report-time)))
1901    
1902     (defimplementation profile-reset ()
1903     (eval `(profile:reset-time))
1904     "Reset profiling counters.")
1905    
1906     (defimplementation profiled-functions ()
1907     profile:*timed-functions*)
1908    
1909     (defimplementation profile-package (package callers methods)
1910     (profile:profile-all :package package
1911     :callers-p callers
1912     #+nil :methods #+nil methods))
1913    
1914    
1915     ;;;; Multiprocessing
1916    
1917 dcrosher 1.11 (defimplementation spawn (fn &key name)
1918     (thread:thread-create fn :name (or name "Anonymous")))
1919 dcrosher 1.1
1920     (defvar *thread-id-counter* 0)
1921     (defvar *thread-id-counter-lock* (thread:make-lock "Thread ID counter"))
1922    
1923     (defimplementation thread-id (thread)
1924     (thread:with-lock-held (*thread-id-counter-lock*)
1925     (or (getf (thread:thread-plist thread) 'id)
1926     (setf (getf (thread:thread-plist thread) 'id)
1927     (incf *thread-id-counter*)))))
1928    
1929     (defimplementation find-thread (id)
1930 heller 1.10 (block find-thread
1931     (thread:map-over-threads
1932     #'(lambda (thread)
1933     (when (eql (getf (thread:thread-plist thread) 'id) id)
1934     (return-from find-thread thread))))))
1935 dcrosher 1.1
1936     (defimplementation thread-name (thread)
1937     (princ-to-string (thread:thread-name thread)))
1938    
1939     (defimplementation thread-status (thread)
1940     (let ((dynamic-values (thread::thread-dynamic-values thread)))
1941     (if (zerop dynamic-values) "Exited" "Running")))
1942    
1943     (defimplementation make-lock (&key name)
1944     (thread:make-lock name))
1945    
1946     (defimplementation call-with-lock-held (lock function)
1947     (declare (type function function))
1948     (thread:with-lock-held (lock) (funcall function)))
1949    
1950     (defimplementation current-thread ()
1951     thread:*thread*)
1952    
1953     (defimplementation all-threads ()
1954     (let ((all-threads nil))
1955     (thread:map-over-threads #'(lambda (thread) (push thread all-threads)))
1956     all-threads))
1957    
1958     (defimplementation interrupt-thread (thread fn)
1959     (thread:thread-interrupt thread #'(lambda ()
1960     (sys:with-interrupts
1961     (funcall fn)))))
1962    
1963     (defimplementation kill-thread (thread)
1964     (thread:destroy-thread thread))
1965    
1966     (defimplementation thread-alive-p (thread)
1967     (not (zerop (thread::thread-dynamic-values thread))))
1968    
1969     (defvar *mailbox-lock* (thread:make-lock "Mailbox lock"))
1970    
1971     (defstruct (mailbox)
1972 dcrosher 1.7 (lock (thread:make-lock "Thread mailbox" :type :error-check
1973     :interruptible nil)
1974 dcrosher 1.1 :type thread:error-check-lock)
1975     (queue '() :type list))
1976    
1977     (defun mailbox (thread)
1978     "Return 'thread's mailbox."
1979     (thread:with-lock-held (*mailbox-lock*)
1980     (or (getf (thread:thread-plist thread) 'mailbox)
1981     (setf (getf (thread:thread-plist thread) 'mailbox) (make-mailbox)))))
1982    
1983     (defimplementation send (thread message)
1984     (let* ((mbox (mailbox thread))
1985 dcrosher 1.7 (lock (mailbox-lock mbox)))
1986     (sys:without-interrupts
1987     (thread:with-lock-held (lock "Mailbox Send")
1988     (setf (mailbox-queue mbox) (nconc (mailbox-queue mbox)
1989     (list message)))))
1990     (mp:process-wakeup thread)
1991 dcrosher 1.1 message))
1992    
1993     (defimplementation receive ()
1994     (let* ((mbox (mailbox thread:*thread*))
1995 dcrosher 1.7 (lock (mailbox-lock mbox)))
1996     (loop
1997     (mp:process-wait-with-timeout "Mailbox read wait" 1
1998     #'(lambda () (mailbox-queue mbox)))
1999     (multiple-value-bind (message winp)
2000     (sys:without-interrupts
2001     (mp:with-lock-held (lock "Mailbox read")
2002     (let ((queue (mailbox-queue mbox)))
2003     (cond (queue
2004     (setf (mailbox-queue mbox) (cdr queue))
2005     (values (car queue) t))
2006     (t
2007     (values nil nil))))))
2008     (when winp
2009     (return message))))))
2010 dcrosher 1.1
2011    
2012    
2013     (defimplementation emacs-connected ())
2014    
2015    
2016     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2017     ;;Trace implementations
2018     ;; In SCL, we have:
2019     ;; (trace <name>)
2020     ;; (trace (method <name> <qualifier>? (<specializer>+)))
2021     ;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
2022     ;; <name> can be a normal name or a (setf name)
2023    
2024     (defun tracedp (spec)
2025     (member spec (eval '(trace)) :test #'equal))
2026    
2027     (defun toggle-trace-aux (spec &rest options)
2028     (cond ((tracedp spec)
2029     (eval `(untrace ,spec))
2030     (format nil "~S is now untraced." spec))
2031     (t
2032     (eval `(trace ,spec ,@options))
2033     (format nil "~S is now traced." spec))))
2034    
2035     (defimplementation toggle-trace (spec)
2036     (ecase (car spec)
2037     ((setf)
2038     (toggle-trace-aux spec))
2039     ((:defgeneric)
2040     (let ((name (second spec)))
2041     (toggle-trace-aux name :methods name)))
2042     ((:defmethod)
2043     nil)
2044     ((:call)
2045     (destructuring-bind (caller callee) (cdr spec)
2046     (toggle-trace-aux (process-fspec callee)
2047     :wherein (list (process-fspec caller)))))))
2048    
2049     (defun process-fspec (fspec)
2050     (cond ((consp fspec)
2051     (ecase (first fspec)
2052     ((:defun :defgeneric) (second fspec))
2053     ((:defmethod)
2054     `(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
2055     ;; this isn't actually supported
2056     ((:labels) `(labels ,(process-fspec (second fspec)) ,(third fspec)))
2057     ((:flet) `(flet ,(process-fspec (second fspec)) ,(third fspec)))))
2058     (t
2059     fspec)))
2060    
2061     ;;; Weak datastructures
2062    
2063     ;;; Not implemented in SCL.
2064     (defimplementation make-weak-key-hash-table (&rest args)
2065     (apply #'make-hash-table :weak-p t args))
2066    
2067     ;; Local Variables:
2068     ;; pbook-heading-regexp: "^;;;\\(;+\\)"
2069     ;; pbook-commentary-regexp: "^;;;\\($\\|[^;]\\)"
2070     ;; End:

  ViewVC Help
Powered by ViewVC 1.1.5