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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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