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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5