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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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