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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5