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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.193 - (hide annotations)
Fri Sep 12 12:27:38 2008 UTC (5 years, 7 months ago) by trittweiler
Branch: MAIN
Changes since 1.192: +4 -4 lines
	New faces: `sldb-restartable-frame-line-face',
	           `sldb-non-restartable-frame-line-face'.

	The former is the face for frames that are surely restartable, the
	latter for frames that are surely not restartable. If
	restartability of a frame cannot be reliably determined, the face
	`sldb-frame-line-face' is used.

	At the moment, determination of frame restartability is supported
	by the SBCL backend only.

	* slime.el (sldb-frame.string): New.
	(sldb-frame.number): New.
	(sldb-frame.plist): New.
	(sldb-prune-initial-frames): Use them.
	(sldb-insert-frames): Ditto.
	(sldb-compute-frame-face): New.
	(sldb-insert-frame): Use `sldb-compute-frame-face' to insert
	frames with one of the faces described above.

	* swank.lisp (defslimefun backtrace): Changed return value; each
	frame is now accompanied with a PLIST which at the moment can
	contain :RESTARTABLE NIL/T/:UNKNOWN depending on whether the frame
	is restartable, or not.

	* swank-backend.lisp (defstruct swank-frame): New structure.
	(compute-backtrace): Is now supposed to return a list of SWANK-FRAMEs.
	(print-frame): Renamed to PRINT-SWANK-FRAME.

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