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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.167 - (hide annotations)
Sun Nov 19 21:33:03 2006 UTC (7 years, 5 months ago) by heller
Branch: MAIN
Changes since 1.166: +3 -9 lines
(find-external-format, guess-external-format): New.
(swank-compile-file): The external-format argument is now a
backend specific value returned by find-external-format.

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