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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.186 - (hide annotations)
Mon Aug 11 17:41:55 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.185: +18 -4 lines
Improve interrupt safety for single-threaded lisps.

* slime.el (slime-interrupt): Send a :emacs-interrupt message
together with SIGINT.  SIGINT now means "check for new events"
instead of "invoke the debugger".

* swank-backend.lisp (install-sigint-handler)
(call-with-user-break-handler): New functions.

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