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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.126 - (hide annotations)
Thu Oct 28 21:34:36 2004 UTC (9 years, 5 months ago) by heller
Branch: MAIN
Changes since 1.125: +55 -36 lines
(set-step-breakpoints): Handle breakpoints at single-return points in
escaped frames better.  Previously we tried to set a breakpoint at the
current position and consequently was only hit during the next call.

(inspect-for-emacs)[function]: Call the next method only for
funcallable instances.

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

  ViewVC Help
Powered by ViewVC 1.1.5