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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5