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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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