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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5