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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.218 - (show annotations)
Tue Jan 5 09:00:30 2010 UTC (4 years, 3 months ago) by heller
Branch: MAIN
Changes since 1.217: +6 -4 lines
Fix M-. bug related to CMUCL's multi-file compilation units.

* swank-cmucl.lisp (code-location-stream-position): Require the
"root-number" as argument and subtract it from the TLF number.
(location-in-file): Pass the root number along.
(code-location-string-offset): Use 0 as root number.
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 (import-swank-mop-symbols :pcl '(:slot-definition-documentation))
12
13 (defun swank-mop:slot-definition-documentation (slot)
14 (documentation slot t))
15
16 ;;;; "Hot fixes"
17 ;;;
18 ;;; Here are necessary bugfixes to the oldest supported 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 (in-package :lisp)
26
27 ;;; `READ-SEQUENCE' with large sequences has problems in 18e. This new
28 ;;; definition works better.
29
30 #-cmu19
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 (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
59 (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
60 (when s
61 (setf (symbol-value s) t)))
62
63 )
64
65 (in-package :swank-backend)
66
67
68 ;;;; 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
76 (defimplementation preferred-communication-style ()
77 :sigio)
78
79 #-(or darwin mips)
80 (defimplementation create-socket (host port)
81 (let* ((addr (resolve-hostname host))
82 (addr (if (not (find-symbol "SOCKET-ERROR" :ext))
83 (ext:htonl addr)
84 addr)))
85 (ext:create-inet-listener port :stream :reuse-address t :host addr)))
86
87 ;; There seems to be a bug in create-inet-listener on Mac/OSX and Irix.
88 #+(or darwin mips)
89 (defimplementation create-socket (host port)
90 (declare (ignore host))
91 (ext:create-inet-listener port :stream :reuse-address t))
92
93 (defimplementation local-port (socket)
94 (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
95
96 (defimplementation close-socket (socket)
97 (let ((fd (socket-fd socket)))
98 (sys:invalidate-descriptor fd)
99 (ext:close-socket fd)))
100
101 (defimplementation accept-connection (socket &key
102 external-format buffering timeout)
103 (declare (ignore timeout))
104 (make-socket-io-stream (ext:accept-tcp-connection socket)
105 (or buffering :full)
106 (or external-format :iso-8859-1)))
107
108 ;;;;; Sockets
109
110 (defimplementation socket-fd (socket)
111 "Return the filedescriptor for the socket represented by SOCKET."
112 (etypecase socket
113 (fixnum socket)
114 (sys:fd-stream (sys:fd-stream-fd socket))))
115
116 (defun resolve-hostname (hostname)
117 "Return the IP address of HOSTNAME as an integer (in host byte-order)."
118 (let ((hostent (ext:lookup-host-entry hostname)))
119 (car (ext:host-entry-addr-list hostent))))
120
121 (defvar *external-format-to-coding-system*
122 '((:iso-8859-1
123 "latin-1" "latin-1-unix" "iso-latin-1-unix"
124 "iso-8859-1" "iso-8859-1-unix")
125 #+unicode
126 (:utf-8 "utf-8" "utf-8-unix")))
127
128 (defimplementation find-external-format (coding-system)
129 (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
130 *external-format-to-coding-system*)))
131
132 (defun make-socket-io-stream (fd buffering external-format)
133 "Create a new input/output fd-stream for FD."
134 #-unicode(declare (ignore external-format))
135 (sys:make-fd-stream fd :input t :output t :element-type 'base-char
136 :buffering buffering
137 #+unicode :external-format
138 #+unicode external-format))
139
140 (defimplementation make-fd-stream (fd external-format)
141 (make-socket-io-stream fd :full external-format))
142
143 (defimplementation dup (fd)
144 (multiple-value-bind (clone error) (unix:unix-dup fd)
145 (unless clone (error "dup failed: ~a" (unix:get-unix-error-msg error)))
146 clone))
147
148 (defimplementation command-line-args ()
149 ext:*command-line-strings*)
150
151 (defimplementation exec-image (image-file args)
152 (multiple-value-bind (ok error)
153 (unix:unix-execve (car (command-line-args))
154 (list* (car (command-line-args))
155 "-core" image-file
156 "-noinit"
157 args))
158 (error "~a" (unix:get-unix-error-msg error))
159 ok))
160
161 ;;;;; Signal-driven I/O
162
163 (defimplementation install-sigint-handler (function)
164 (sys:enable-interrupt :sigint (lambda (signal code scp)
165 (declare (ignore signal code scp))
166 (funcall function))))
167
168 (defvar *sigio-handlers* '()
169 "List of (key . function) pairs.
170 All functions are called on SIGIO, and the key is used for removing
171 specific functions.")
172
173 (defun reset-sigio-handlers () (setq *sigio-handlers* '()))
174 ;; All file handlers are invalid afer reload.
175 (pushnew 'reset-sigio-handlers ext:*after-save-initializations*)
176
177 (defun set-sigio-handler ()
178 (sys:enable-interrupt :sigio (lambda (signal code scp)
179 (sigio-handler signal code scp))))
180
181 (defun sigio-handler (signal code scp)
182 (declare (ignore signal code scp))
183 (mapc #'funcall (mapcar #'cdr *sigio-handlers*)))
184
185 (defun fcntl (fd command arg)
186 "fcntl(2) - manipulate a file descriptor."
187 (multiple-value-bind (ok error) (unix:unix-fcntl fd command arg)
188 (cond (ok)
189 (t (error "fcntl: ~A" (unix:get-unix-error-msg error))))))
190
191 (defimplementation add-sigio-handler (socket fn)
192 (set-sigio-handler)
193 (let ((fd (socket-fd socket)))
194 (fcntl fd unix:f-setown (unix:unix-getpid))
195 (let ((old-flags (fcntl fd unix:f-getfl 0)))
196 (fcntl fd unix:f-setfl (logior old-flags unix:fasync)))
197 (assert (not (assoc fd *sigio-handlers*)))
198 (push (cons fd fn) *sigio-handlers*)))
199
200 (defimplementation remove-sigio-handlers (socket)
201 (let ((fd (socket-fd socket)))
202 (when (assoc fd *sigio-handlers*)
203 (setf *sigio-handlers* (remove fd *sigio-handlers* :key #'car))
204 (let ((old-flags (fcntl fd unix:f-getfl 0)))
205 (fcntl fd unix:f-setfl (logandc2 old-flags unix:fasync)))
206 (sys:invalidate-descriptor fd))
207 (assert (not (assoc fd *sigio-handlers*)))
208 (when (null *sigio-handlers*)
209 (sys:default-interrupt :sigio))))
210
211 ;;;;; SERVE-EVENT
212
213 (defimplementation add-fd-handler (socket fn)
214 (let ((fd (socket-fd socket)))
215 (sys:add-fd-handler fd :input (lambda (_) _ (funcall fn)))))
216
217 (defimplementation remove-fd-handlers (socket)
218 (sys:invalidate-descriptor (socket-fd socket)))
219
220 (defimplementation wait-for-input (streams &optional timeout)
221 (assert (member timeout '(nil t)))
222 (loop
223 (let ((ready (remove-if-not #'listen streams)))
224 (when ready (return ready)))
225 (when timeout (return nil))
226 (multiple-value-bind (in out) (make-pipe)
227 (let* ((f (constantly t))
228 (handlers (loop for s in (cons in (mapcar #'to-fd-stream streams))
229 collect (add-one-shot-handler s f))))
230 (unwind-protect
231 (let ((*interrupt-queued-handler* (lambda ()
232 (write-char #\! out))))
233 (when (check-slime-interrupts) (return :interrupt))
234 (sys:serve-event))
235 (mapc #'sys:remove-fd-handler handlers)
236 (close in)
237 (close out))))))
238
239 (defun to-fd-stream (stream)
240 (etypecase stream
241 (sys:fd-stream stream)
242 (synonym-stream
243 (to-fd-stream
244 (symbol-value (synonym-stream-symbol stream))))
245 (two-way-stream
246 (to-fd-stream (two-way-stream-input-stream stream)))))
247
248 (defun add-one-shot-handler (stream function)
249 (let (handler)
250 (setq handler (sys:add-fd-handler (sys:fd-stream-fd stream) :input
251 (lambda (fd)
252 (declare (ignore fd))
253 (sys:remove-fd-handler handler)
254 (funcall function stream))))))
255
256 (defun make-pipe ()
257 (multiple-value-bind (in out) (unix:unix-pipe)
258 (values (sys:make-fd-stream in :input t :buffering :none)
259 (sys:make-fd-stream out :output t :buffering :none))))
260
261
262 ;;;; Stream handling
263 ;;; XXX: How come we don't use Gray streams in CMUCL too? -luke (15/May/2004)
264
265 (defimplementation make-output-stream (write-string)
266 (make-slime-output-stream write-string))
267
268 (defimplementation make-input-stream (read-string)
269 (make-slime-input-stream read-string))
270
271 (defstruct (slime-output-stream
272 (:include lisp::lisp-stream
273 (lisp::misc #'sos/misc)
274 (lisp::out #'sos/write-char)
275 (lisp::sout #'sos/write-string))
276 (:conc-name sos.)
277 (:print-function %print-slime-output-stream)
278 (:constructor make-slime-output-stream (output-fn)))
279 (output-fn nil :type function)
280 (buffer (make-string 4000) :type string)
281 (index 0 :type kernel:index)
282 (column 0 :type kernel:index))
283
284 (defun %print-slime-output-stream (s stream d)
285 (declare (ignore d))
286 (print-unreadable-object (s stream :type t :identity t)))
287
288 (defun sos/write-char (stream char)
289 (let ((pending-output nil))
290 (system:without-interrupts
291 (let ((buffer (sos.buffer stream))
292 (index (sos.index stream)))
293 (setf (schar buffer index) char)
294 (setf (sos.index stream) (1+ index))
295 (incf (sos.column stream))
296 (when (char= #\newline char)
297 (setf (sos.column stream) 0)
298 #+(or)(setq pending-output (sos/reset-buffer stream))
299 )
300 (when (= index (1- (length buffer)))
301 (setq pending-output (sos/reset-buffer stream)))))
302 (when pending-output
303 (funcall (sos.output-fn stream) pending-output)))
304 char)
305
306 (defun sos/write-string (stream string start end)
307 (loop for i from start below end
308 do (sos/write-char stream (aref string i))))
309
310 (defun sos/flush (stream)
311 (let ((string (sos/reset-buffer stream)))
312 (when string
313 (funcall (sos.output-fn stream) string))
314 nil))
315
316 (defun sos/reset-buffer (stream)
317 (system:without-interrupts
318 (let ((end (sos.index stream)))
319 (unless (zerop end)
320 (prog1 (subseq (sos.buffer stream) 0 end)
321 (setf (sos.index stream) 0))))))
322
323 (defun sos/misc (stream operation &optional arg1 arg2)
324 (declare (ignore arg1 arg2))
325 (case operation
326 ((:force-output :finish-output) (sos/flush stream))
327 (:charpos (sos.column stream))
328 (:line-length 75)
329 (:file-position nil)
330 (:element-type 'base-char)
331 (:get-command nil)
332 (:close nil)
333 (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
334
335 (defstruct (slime-input-stream
336 (:include string-stream
337 (lisp::in #'sis/in)
338 (lisp::misc #'sis/misc))
339 (:conc-name sis.)
340 (:print-function %print-slime-output-stream)
341 (:constructor make-slime-input-stream (input-fn)))
342 (input-fn nil :type function)
343 (buffer "" :type string)
344 (index 0 :type kernel:index))
345
346 (defun sis/in (stream eof-errorp eof-value)
347 (let ((index (sis.index stream))
348 (buffer (sis.buffer stream)))
349 (when (= index (length buffer))
350 (let ((string (funcall (sis.input-fn stream))))
351 (cond ((zerop (length string))
352 (return-from sis/in
353 (if eof-errorp
354 (error (make-condition 'end-of-file :stream stream))
355 eof-value)))
356 (t
357 (setf buffer string)
358 (setf (sis.buffer stream) buffer)
359 (setf index 0)))))
360 (prog1 (aref buffer index)
361 (setf (sis.index stream) (1+ index)))))
362
363 (defun sis/misc (stream operation &optional arg1 arg2)
364 (declare (ignore arg2))
365 (ecase operation
366 (:file-position nil)
367 (:file-length nil)
368 (:unread (setf (aref (sis.buffer stream)
369 (decf (sis.index stream)))
370 arg1))
371 (:clear-input
372 (setf (sis.index stream) 0
373 (sis.buffer stream) ""))
374 (:listen (< (sis.index stream) (length (sis.buffer stream))))
375 (:charpos nil)
376 (:line-length nil)
377 (:get-command nil)
378 (:element-type 'base-char)
379 (:close nil)
380 (:interactive-p t)))
381
382
383 ;;;; Compilation Commands
384
385 (defvar *previous-compiler-condition* nil
386 "Used to detect duplicates.")
387
388 (defvar *previous-context* nil
389 "Previous compiler error context.")
390
391 (defvar *buffer-name* nil
392 "The name of the Emacs buffer we are compiling from.
393 NIL if we aren't compiling from a buffer.")
394
395 (defvar *buffer-start-position* nil)
396 (defvar *buffer-substring* nil)
397
398 (defimplementation call-with-compilation-hooks (function)
399 (let ((*previous-compiler-condition* nil)
400 (*previous-context* nil)
401 (*print-readably* nil))
402 (handler-bind ((c::compiler-error #'handle-notification-condition)
403 (c::style-warning #'handle-notification-condition)
404 (c::warning #'handle-notification-condition))
405 (funcall function))))
406
407 (defimplementation swank-compile-file (input-file output-file
408 load-p external-format)
409 (declare (ignore external-format))
410 (clear-xref-info input-file)
411 (with-compilation-hooks ()
412 (let ((*buffer-name* nil)
413 (ext:*ignore-extra-close-parentheses* nil))
414 (multiple-value-bind (output-file warnings-p failure-p)
415 (compile-file input-file :output-file output-file)
416 (values output-file warnings-p
417 (or failure-p
418 (when load-p
419 ;; Cache the latest source file for definition-finding.
420 (source-cache-get input-file
421 (file-write-date input-file))
422 (not (load output-file)))))))))
423
424 (defimplementation swank-compile-string (string &key buffer position filename
425 policy)
426 (declare (ignore filename policy))
427 (with-compilation-hooks ()
428 (let ((*buffer-name* buffer)
429 (*buffer-start-position* position)
430 (*buffer-substring* string)
431 (source-info (list :emacs-buffer buffer
432 :emacs-buffer-offset position
433 :emacs-buffer-string string)))
434 (with-input-from-string (stream string)
435 (let ((failurep (ext:compile-from-stream stream :source-info
436 source-info)))
437 (not failurep))))))
438
439
440 ;;;;; Trapping notes
441 ;;;
442 ;;; We intercept conditions from the compiler and resignal them as
443 ;;; `SWANK:COMPILER-CONDITION's.
444
445 (defun handle-notification-condition (condition)
446 "Handle a condition caused by a compiler warning."
447 (unless (eq condition *previous-compiler-condition*)
448 (let ((context (c::find-error-context nil)))
449 (setq *previous-compiler-condition* condition)
450 (setq *previous-context* context)
451 (signal-compiler-condition condition context))))
452
453 (defun signal-compiler-condition (condition context)
454 (signal (make-condition
455 'compiler-condition
456 :original-condition condition
457 :severity (severity-for-emacs condition)
458 :message (compiler-condition-message condition)
459 :source-context (compiler-error-context context)
460 :location (if (read-error-p condition)
461 (read-error-location condition)
462 (compiler-note-location context)))))
463
464 (defun severity-for-emacs (condition)
465 "Return the severity of CONDITION."
466 (etypecase condition
467 ((satisfies read-error-p) :read-error)
468 (c::compiler-error :error)
469 (c::style-warning :note)
470 (c::warning :warning)))
471
472 (defun read-error-p (condition)
473 (eq (type-of condition) 'c::compiler-read-error))
474
475 (defun compiler-condition-message (condition)
476 "Briefly describe a compiler error for Emacs.
477 When Emacs presents the message it already has the source popped up
478 and the source form highlighted. This makes much of the information in
479 the error-context redundant."
480 (princ-to-string condition))
481
482 (defun compiler-error-context (error-context)
483 "Describe context information for Emacs."
484 (declare (type (or c::compiler-error-context null) error-context))
485 (multiple-value-bind (enclosing source)
486 (if error-context
487 (values (c::compiler-error-context-enclosing-source error-context)
488 (c::compiler-error-context-source error-context)))
489 (if (or enclosing source)
490 (format nil "~@[--> ~{~<~%--> ~1:;~A ~>~}~%~]~
491 ~@[==>~{~&~A~}~]"
492 enclosing source))))
493
494 (defun read-error-location (condition)
495 (let* ((finfo (car (c::source-info-current-file c::*source-info*)))
496 (file (c::file-info-name finfo))
497 (pos (c::compiler-read-error-position condition)))
498 (cond ((and (eq file :stream) *buffer-name*)
499 (make-location (list :buffer *buffer-name*)
500 (list :offset *buffer-start-position* pos)))
501 ((and (pathnamep file) (not *buffer-name*))
502 (make-location (list :file (unix-truename file))
503 (list :position (1+ pos))))
504 (t (break)))))
505
506 (defun compiler-note-location (context)
507 "Derive the location of a complier message from its context.
508 Return a `location' record, or (:error REASON) on failure."
509 (if (null context)
510 (note-error-location)
511 (with-struct (c::compiler-error-context- file-name
512 original-source
513 original-source-path) context
514 (or (locate-compiler-note file-name original-source
515 (reverse original-source-path))
516 (note-error-location)))))
517
518 (defun note-error-location ()
519 "Pseudo-location for notes that can't be located."
520 (cond (*compile-file-truename*
521 (make-location (list :file (unix-truename *compile-file-truename*))
522 (list :eof)))
523 (*buffer-name*
524 (make-location (list :buffer *buffer-name*)
525 (list :position *buffer-start-position*)))
526 (t (list :error "No error location available."))))
527
528 (defun locate-compiler-note (file source source-path)
529 (cond ((and (eq file :stream) *buffer-name*)
530 ;; Compiling from a buffer
531 (make-location (list :buffer *buffer-name*)
532 (list :offset *buffer-start-position*
533 (source-path-string-position
534 source-path *buffer-substring*))))
535 ((and (pathnamep file) (null *buffer-name*))
536 ;; Compiling from a file
537 (make-location (list :file (unix-truename file))
538 (list :position (1+ (source-path-file-position
539 source-path file)))))
540 ((and (eq file :lisp) (stringp source))
541 ;; No location known, but we have the source form.
542 ;; XXX How is this case triggered? -luke (16/May/2004)
543 ;; This can happen if the compiler needs to expand a macro
544 ;; but the macro-expander is not yet compiled. Calling the
545 ;; (interpreted) macro-expander triggers IR1 conversion of
546 ;; the lambda expression for the expander and invokes the
547 ;; compiler recursively.
548 (make-location (list :source-form source)
549 (list :position 1)))))
550
551 (defun unix-truename (pathname)
552 (ext:unix-namestring (truename pathname)))
553
554
555 ;;;; XREF
556 ;;;
557 ;;; Cross-reference support is based on the standard CMUCL `XREF'
558 ;;; package. This package has some caveats: XREF information is
559 ;;; recorded during compilation and not preserved in fasl files, and
560 ;;; XREF recording is disabled by default. Redefining functions can
561 ;;; also cause duplicate references to accumulate, but
562 ;;; `swank-compile-file' will automatically clear out any old records
563 ;;; from the same filename.
564 ;;;
565 ;;; To enable XREF recording, set `c:*record-xref-info*' to true. To
566 ;;; clear out the XREF database call `xref:init-xref-database'.
567
568 (defmacro defxref (name function)
569 `(defimplementation ,name (name)
570 (xref-results (,function name))))
571
572 (defxref who-calls xref:who-calls)
573 (defxref who-references xref:who-references)
574 (defxref who-binds xref:who-binds)
575 (defxref who-sets xref:who-sets)
576
577 ;;; More types of XREF information were added since 18e:
578 ;;;
579 #+cmu19
580 (progn
581 (defxref who-macroexpands xref:who-macroexpands)
582 ;; XXX
583 (defimplementation who-specializes (symbol)
584 (let* ((methods (xref::who-specializes (find-class symbol)))
585 (locations (mapcar #'method-location methods)))
586 (mapcar #'list methods locations))))
587
588 (defun xref-results (contexts)
589 (mapcar (lambda (xref)
590 (list (xref:xref-context-name xref)
591 (resolve-xref-location xref)))
592 contexts))
593
594 (defun resolve-xref-location (xref)
595 (let ((name (xref:xref-context-name xref))
596 (file (xref:xref-context-file xref))
597 (source-path (xref:xref-context-source-path xref)))
598 (cond ((and file source-path)
599 (let ((position (source-path-file-position source-path file)))
600 (make-location (list :file (unix-truename file))
601 (list :position (1+ position)))))
602 (file
603 (make-location (list :file (unix-truename file))
604 (list :function-name (string name))))
605 (t
606 `(:error ,(format nil "Unknown source location: ~S ~S ~S "
607 name file source-path))))))
608
609 (defun clear-xref-info (namestring)
610 "Clear XREF notes pertaining to NAMESTRING.
611 This is a workaround for a CMUCL bug: XREF records are cumulative."
612 (when c:*record-xref-info*
613 (let ((filename (truename namestring)))
614 (dolist (db (list xref::*who-calls*
615 #+cmu19 xref::*who-is-called*
616 #+cmu19 xref::*who-macroexpands*
617 xref::*who-references*
618 xref::*who-binds*
619 xref::*who-sets*))
620 (maphash (lambda (target contexts)
621 ;; XXX update during traversal?
622 (setf (gethash target db)
623 (delete filename contexts
624 :key #'xref:xref-context-file
625 :test #'equalp)))
626 db)))))
627
628
629 ;;;; Find callers and callees
630 ;;;
631 ;;; Find callers and callees by looking at the constant pool of
632 ;;; compiled code objects. We assume every fdefn object in the
633 ;;; constant pool corresponds to a call to that function. A better
634 ;;; strategy would be to use the disassembler to find actual
635 ;;; call-sites.
636
637 (labels ((make-stack () (make-array 100 :fill-pointer 0 :adjustable t))
638 (map-cpool (code fun)
639 (declare (type kernel:code-component code) (type function fun))
640 (loop for i from vm:code-constants-offset
641 below (kernel:get-header-data code)
642 do (funcall fun (kernel:code-header-ref code i))))
643
644 (callees (fun)
645 (let ((callees (make-stack)))
646 (map-cpool (vm::find-code-object fun)
647 (lambda (o)
648 (when (kernel:fdefn-p o)
649 (vector-push-extend (kernel:fdefn-function o)
650 callees))))
651 (coerce callees 'list)))
652
653 (callers (fun)
654 (declare (function fun))
655 (let ((callers (make-stack)))
656 (ext:gc :full t)
657 ;; scan :dynamic first to avoid the need for even more gcing
658 (dolist (space '(:dynamic :read-only :static))
659 (vm::map-allocated-objects
660 (lambda (obj header size)
661 (declare (type fixnum header) (ignore size))
662 (when (= vm:code-header-type header)
663 (map-cpool obj
664 (lambda (c)
665 (when (and (kernel:fdefn-p c)
666 (eq (kernel:fdefn-function c) fun))
667 (vector-push-extend obj callers))))))
668 space)
669 (ext:gc))
670 (coerce callers 'list)))
671
672 (entry-points (code)
673 (loop for entry = (kernel:%code-entry-points code)
674 then (kernel::%function-next entry)
675 while entry
676 collect entry))
677
678 (guess-main-entry-point (entry-points)
679 (or (find-if (lambda (fun)
680 (ext:valid-function-name-p
681 (kernel:%function-name fun)))
682 entry-points)
683 (car entry-points)))
684
685 (fun-dspec (fun)
686 (list (kernel:%function-name fun) (function-location fun)))
687
688 (code-dspec (code)
689 (let ((eps (entry-points code))
690 (di (kernel:%code-debug-info code)))
691 (cond (eps (fun-dspec (guess-main-entry-point eps)))
692 (di (list (c::debug-info-name di)
693 (debug-info-function-name-location di)))
694 (t (list (princ-to-string code)
695 `(:error "No src-loc available")))))))
696 (declare (inline map-cpool))
697
698 (defimplementation list-callers (symbol)
699 (mapcar #'code-dspec (callers (coerce symbol 'function) )))
700
701 (defimplementation list-callees (symbol)
702 (mapcar #'fun-dspec (callees symbol))))
703
704 (defun test-list-callers (count)
705 (let ((funsyms '()))
706 (do-all-symbols (s)
707 (when (and (fboundp s)
708 (functionp (symbol-function s))
709 (not (macro-function s))
710 (not (special-operator-p s)))
711 (push s funsyms)))
712 (let ((len (length funsyms)))
713 (dotimes (i count)
714 (let ((sym (nth (random len) funsyms)))
715 (format t "~s -> ~a~%" sym (mapcar #'car (list-callers sym))))))))
716
717 ;; (test-list-callers 100)
718
719
720 ;;;; Resolving source locations
721 ;;;
722 ;;; Our mission here is to "resolve" references to code locations into
723 ;;; actual file/buffer names and character positions. The references
724 ;;; we work from come out of the compiler's statically-generated debug
725 ;;; information, such as `code-location''s and `debug-source''s. For
726 ;;; more details, see the "Debugger Programmer's Interface" section of
727 ;;; the CMUCL manual.
728 ;;;
729 ;;; The first step is usually to find the corresponding "source-path"
730 ;;; for the location. Once we have the source-path we can pull up the
731 ;;; source file and `READ' our way through to the right position. The
732 ;;; main source-code groveling work is done in
733 ;;; `swank-source-path-parser.lisp'.
734
735 (defvar *debug-definition-finding* nil
736 "When true don't handle errors while looking for definitions.
737 This is useful when debugging the definition-finding code.")
738
739 (defvar *source-snippet-size* 256
740 "Maximum number of characters in a snippet of source code.
741 Snippets at the beginning of definitions are used to tell Emacs what
742 the definitions looks like, so that it can accurately find them by
743 text search.")
744
745 (defmacro safe-definition-finding (&body body)
746 "Execute BODY and return the source-location it returns.
747 If an error occurs and `*debug-definition-finding*' is false, then
748 return an error pseudo-location.
749
750 The second return value is NIL if no error occurs, otherwise it is the
751 condition object."
752 `(flet ((body () ,@body))
753 (if *debug-definition-finding*
754 (body)
755 (handler-case (values (progn ,@body) nil)
756 (error (c) (values `(:error ,(trim-whitespace (princ-to-string c)))
757 c))))))
758
759 (defun trim-whitespace (string)
760 (string-trim #(#\newline #\space #\tab) string))
761
762 (defun code-location-source-location (code-location)
763 "Safe wrapper around `code-location-from-source-location'."
764 (safe-definition-finding
765 (source-location-from-code-location code-location)))
766
767 (defun source-location-from-code-location (code-location)
768 "Return the source location for CODE-LOCATION."
769 (let ((debug-fun (di:code-location-debug-function code-location)))
770 (when (di::bogus-debug-function-p debug-fun)
771 ;; Those lousy cheapskates! They've put in a bogus debug source
772 ;; because the code was compiled at a low debug setting.
773 (error "Bogus debug function: ~A" debug-fun)))
774 (let* ((debug-source (di:code-location-debug-source code-location))
775 (from (di:debug-source-from debug-source))
776 (name (di:debug-source-name debug-source)))
777 (ecase from
778 (:file
779 (location-in-file name code-location debug-source))
780 (:stream
781 (location-in-stream code-location debug-source))
782 (:lisp
783 ;; The location comes from a form passed to `compile'.
784 ;; The best we can do is return the form itself for printing.
785 (make-location
786 (list :source-form (with-output-to-string (*standard-output*)
787 (debug::print-code-location-source-form
788 code-location 100 t)))
789 (list :position 1))))))
790
791 (defun location-in-file (filename code-location debug-source)
792 "Resolve the source location for CODE-LOCATION in FILENAME."
793 (let* ((code-date (di:debug-source-created debug-source))
794 (root-number (di:debug-source-root-number debug-source))
795 (source-code (get-source-code filename code-date)))
796 (with-input-from-string (s source-code)
797 (make-location (list :file (unix-truename filename))
798 (list :position (1+ (code-location-stream-position
799 code-location s root-number)))
800 `(:snippet ,(read-snippet s))))))
801
802 (defun location-in-stream (code-location debug-source)
803 "Resolve the source location for a CODE-LOCATION from a stream.
804 This only succeeds if the code was compiled from an Emacs buffer."
805 (unless (debug-source-info-from-emacs-buffer-p debug-source)
806 (error "The code is compiled from a non-SLIME stream."))
807 (let* ((info (c::debug-source-info debug-source))
808 (string (getf info :emacs-buffer-string))
809 (position (code-location-string-offset
810 code-location
811 string)))
812 (make-location
813 (list :buffer (getf info :emacs-buffer))
814 (list :offset (getf info :emacs-buffer-offset) position)
815 (list :snippet (with-input-from-string (s string)
816 (file-position s position)
817 (read-snippet s))))))
818
819 ;;;;; Function-name locations
820 ;;;
821 (defun debug-info-function-name-location (debug-info)
822 "Return a function-name source-location for DEBUG-INFO.
823 Function-name source-locations are a fallback for when precise
824 positions aren't available."
825 (with-struct (c::debug-info- (fname name) source) debug-info
826 (with-struct (c::debug-source- info from name) (car source)
827 (ecase from
828 (:file
829 (make-location (list :file (namestring (truename name)))
830 (list :function-name (string fname))))
831 (:stream
832 (assert (debug-source-info-from-emacs-buffer-p (car source)))
833 (make-location (list :buffer (getf info :emacs-buffer))
834 (list :function-name (string fname))))
835 (:lisp
836 (make-location (list :source-form (princ-to-string (aref name 0)))
837 (list :position 1)))))))
838
839 (defun debug-source-info-from-emacs-buffer-p (debug-source)
840 "Does the `info' slot of DEBUG-SOURCE contain an Emacs buffer location?
841 This is true for functions that were compiled directly from buffers."
842 (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
843
844 (defun info-from-emacs-buffer-p (info)
845 (and info
846 (consp info)
847 (eq :emacs-buffer (car info))))
848
849
850 ;;;;; Groveling source-code for positions
851
852 (defun code-location-stream-position (code-location stream root)
853 "Return the byte offset of CODE-LOCATION in STREAM. Extract the
854 toplevel-form-number and form-number from CODE-LOCATION and use that
855 to find the position of the corresponding form.
856
857 Finish with STREAM positioned at the start of the code location."
858 (let* ((location (debug::maybe-block-start-location code-location))
859 (tlf-offset (- (di:code-location-top-level-form-offset location)
860 root))
861 (form-number (di:code-location-form-number location)))
862 (let ((pos (form-number-stream-position tlf-offset form-number stream)))
863 (file-position stream pos)
864 pos)))
865
866 (defun form-number-stream-position (tlf-number form-number stream)
867 "Return the starting character position of a form in STREAM.
868 TLF-NUMBER is the top-level-form number.
869 FORM-NUMBER is an index into a source-path table for the TLF."
870 (multiple-value-bind (tlf position-map) (read-source-form tlf-number stream)
871 (let* ((path-table (di:form-number-translations tlf 0))
872 (source-path
873 (if (<= (length path-table) form-number) ; source out of sync?
874 (list 0) ; should probably signal a condition
875 (reverse (cdr (aref path-table form-number))))))
876 (source-path-source-position source-path tlf position-map))))
877
878 (defun code-location-string-offset (code-location string)
879 "Return the byte offset of CODE-LOCATION in STRING.
880 See CODE-LOCATION-STREAM-POSITION."
881 (with-input-from-string (s string)
882 (code-location-stream-position code-location s 0)))
883
884
885 ;;;; Finding definitions
886
887 ;;; There are a great many different types of definition for us to
888 ;;; find. We search for definitions of every kind and return them in a
889 ;;; list.
890
891 (defimplementation find-definitions (name)
892 (append (function-definitions name)
893 (setf-definitions name)
894 (variable-definitions name)
895 (class-definitions name)
896 (type-definitions name)
897 (compiler-macro-definitions name)
898 (source-transform-definitions name)
899 (function-info-definitions name)
900 (ir1-translator-definitions name)))
901
902 ;;;;; Functions, macros, generic functions, methods
903 ;;;
904 ;;; We make extensive use of the compile-time debug information that
905 ;;; CMUCL records, in particular "debug functions" and "code
906 ;;; locations." Refer to the "Debugger Programmer's Interface" section
907 ;;; of the CMUCL manual for more details.
908
909 (defun function-definitions (name)
910 "Return definitions for NAME in the \"function namespace\", i.e.,
911 regular functions, generic functions, methods and macros.
912 NAME can any valid function name (e.g, (setf car))."
913 (let ((macro? (and (symbolp name) (macro-function name)))
914 (special? (and (symbolp name) (special-operator-p name)))
915 (function? (and (ext:valid-function-name-p name)
916 (ext:info :function :definition name)
917 (if (symbolp name) (fboundp name) t))))
918 (cond (macro?
919 (list `((defmacro ,name)
920 ,(function-location (macro-function name)))))
921 (special?
922 (list `((:special-operator ,name)
923 (:error ,(format nil "Special operator: ~S" name)))))
924 (function?
925 (let ((function (fdefinition name)))
926 (if (genericp function)
927 (generic-function-definitions name function)
928 (list (list `(function ,name)
929 (function-location function)))))))))
930
931 ;;;;;; Ordinary (non-generic/macro/special) functions
932 ;;;
933 ;;; First we test if FUNCTION is a closure created by defstruct, and
934 ;;; if so extract the defstruct-description (`dd') from the closure
935 ;;; and find the constructor for the struct. Defstruct creates a
936 ;;; defun for the default constructor and we use that as an
937 ;;; approximation to the source location of the defstruct.
938 ;;;
939 ;;; For an ordinary function we return the source location of the
940 ;;; first code-location we find.
941 ;;;
942 (defun function-location (function)
943 "Return the source location for FUNCTION."
944 (cond ((struct-closure-p function)
945 (struct-closure-location function))
946 ((c::byte-function-or-closure-p function)
947 (byte-function-location function))
948 (t
949 (compiled-function-location function))))
950
951 (defun compiled-function-location (function)
952 "Return the location of a regular compiled function."
953 (multiple-value-bind (code-location error)
954 (safe-definition-finding (function-first-code-location function))
955 (cond (error (list :error (princ-to-string error)))
956 (t (code-location-source-location code-location)))))
957
958 (defun function-first-code-location (function)
959 "Return the first code-location we can find for FUNCTION."
960 (and (function-has-debug-function-p function)
961 (di:debug-function-start-location
962 (di:function-debug-function function))))
963
964 (defun function-has-debug-function-p (function)
965 (di:function-debug-function function))
966
967 (defun function-code-object= (closure function)
968 (and (eq (vm::find-code-object closure)
969 (vm::find-code-object function))
970 (not (eq closure function))))
971
972 (defun byte-function-location (fun)
973 "Return the location of the byte-compiled function FUN."
974 (etypecase fun
975 ((or c::hairy-byte-function c::simple-byte-function)
976 (let* ((di (kernel:%code-debug-info (c::byte-function-component fun))))
977 (if di
978 (debug-info-function-name-location di)
979 `(:error
980 ,(format nil "Byte-function without debug-info: ~a" fun)))))
981 (c::byte-closure
982 (byte-function-location (c::byte-closure-function fun)))))
983
984 ;;; Here we deal with structure accessors. Note that `dd' is a
985 ;;; "defstruct descriptor" structure in CMUCL. A `dd' describes a
986 ;;; `defstruct''d structure.
987
988 (defun struct-closure-p (function)
989 "Is FUNCTION a closure created by defstruct?"
990 (or (function-code-object= function #'kernel::structure-slot-accessor)
991 (function-code-object= function #'kernel::structure-slot-setter)
992 (function-code-object= function #'kernel::%defstruct)))
993
994 (defun struct-closure-location (function)
995 "Return the location of the structure that FUNCTION belongs to."
996 (assert (struct-closure-p function))
997 (safe-definition-finding
998 (dd-location (struct-closure-dd function))))
999
1000 (defun struct-closure-dd (function)
1001 "Return the defstruct-definition (dd) of FUNCTION."
1002 (assert (= (kernel:get-type function) vm:closure-header-type))
1003 (flet ((find-layout (function)
1004 (sys:find-if-in-closure
1005 (lambda (x)
1006 (let ((value (if (di::indirect-value-cell-p x)
1007 (c:value-cell-ref x)
1008 x)))
1009 (when (kernel::layout-p value)
1010 (return-from find-layout value))))
1011 function)))
1012 (kernel:layout-info (find-layout function))))
1013
1014 (defun dd-location (dd)
1015 "Return the location of a `defstruct'."
1016 ;; Find the location in a constructor.
1017 (function-location (struct-constructor dd)))
1018
1019 (defun struct-constructor (dd)
1020 "Return a constructor function from a defstruct definition.
1021 Signal an error if no constructor can be found."
1022 (let* ((constructor (or (kernel:dd-default-constructor dd)
1023 (car (kernel::dd-constructors dd))))
1024 (sym (if (consp constructor) (car constructor) constructor)))
1025 (unless sym
1026 (error "Cannot find structure's constructor: ~S" (kernel::dd-name dd)))
1027 (coerce sym 'function)))
1028
1029 ;;;;;; Generic functions and methods
1030
1031 (defun generic-function-definitions (name function)
1032 "Return the definitions of a generic function and its methods."
1033 (cons (list `(defgeneric ,name) (gf-location function))
1034 (gf-method-definitions function)))
1035
1036 (defun gf-location (gf)
1037 "Return the location of the generic function GF."
1038 (definition-source-location gf (pcl::generic-function-name gf)))
1039
1040 (defun gf-method-definitions (gf)
1041 "Return the locations of all methods of the generic function GF."
1042 (mapcar #'method-definition (pcl::generic-function-methods gf)))
1043
1044 (defun method-definition (method)
1045 (list (method-dspec method)
1046 (method-location method)))
1047
1048 (defun method-dspec (method)
1049 "Return a human-readable \"definition specifier\" for METHOD."
1050 (let* ((gf (pcl:method-generic-function method))
1051 (name (pcl:generic-function-name gf))
1052 (specializers (pcl:method-specializers method))
1053 (qualifiers (pcl:method-qualifiers method)))
1054 `(method ,name ,@qualifiers ,(pcl::unparse-specializers specializers))))
1055
1056 ;; XXX maybe special case setters/getters
1057 (defun method-location (method)
1058 (function-location (or (pcl::method-fast-function method)
1059 (pcl:method-function method))))
1060
1061 (defun genericp (fn)
1062 (typep fn 'generic-function))
1063
1064 ;;;;;; Types and classes
1065
1066 (defun type-definitions (name)
1067 "Return `deftype' locations for type NAME."
1068 (maybe-make-definition (ext:info :type :expander name) 'deftype name))
1069
1070 (defun maybe-make-definition (function kind name)
1071 "If FUNCTION is non-nil then return its definition location."
1072 (if function
1073 (list (list `(,kind ,name) (function-location function)))))
1074
1075 (defun class-definitions (name)
1076 "Return the definition locations for the class called NAME."
1077 (if (symbolp name)
1078 (let ((class (kernel::find-class name nil)))
1079 (etypecase class
1080 (null '())
1081 (kernel::structure-class
1082 (list (list `(defstruct ,name) (dd-location (find-dd name)))))
1083 #+(or)
1084 (conditions::condition-class
1085 (list (list `(define-condition ,name)
1086 (condition-class-location class))))
1087 (kernel::standard-class
1088 (list (list `(defclass ,name)
1089 (class-location (find-class name)))))
1090 ((or kernel::built-in-class
1091 conditions::condition-class
1092 kernel:funcallable-structure-class)
1093 (list (list `(kernel::define-type-class ,name)
1094 `(:error
1095 ,(format nil "No source info for ~A" name)))))))))
1096
1097 (defun class-location (class)
1098 "Return the `defclass' location for CLASS."
1099 (definition-source-location class (pcl:class-name class)))
1100
1101 (defun find-dd (name)
1102 "Find the defstruct-definition by the name of its structure-class."
1103 (let ((layout (ext:info :type :compiler-layout name)))
1104 (if layout
1105 (kernel:layout-info layout))))
1106
1107 (defun condition-class-location (class)
1108 (let ((slots (conditions::condition-class-slots class))
1109 (name (conditions::condition-class-name class)))
1110 (cond ((null slots)
1111 `(:error ,(format nil "No location info for condition: ~A" name)))
1112 (t
1113 ;; Find the class via one of its slot-reader methods.
1114 (let* ((slot (first slots))
1115 (gf (fdefinition
1116 (first (conditions::condition-slot-readers slot)))))
1117 (method-location
1118 (first
1119 (pcl:compute-applicable-methods-using-classes
1120 gf (list (find-class name))))))))))
1121
1122 (defun make-name-in-file-location (file string)
1123 (multiple-value-bind (filename c)
1124 (ignore-errors
1125 (unix-truename (merge-pathnames (make-pathname :type "lisp")
1126 file)))
1127 (cond (filename (make-location `(:file ,filename)
1128 `(:function-name ,(string string))))
1129 (t (list :error (princ-to-string c))))))
1130
1131 (defun source-location-form-numbers (location)
1132 (c::decode-form-numbers (c::form-numbers-form-numbers location)))
1133
1134 (defun source-location-tlf-number (location)
1135 (nth-value 0 (source-location-form-numbers location)))
1136
1137 (defun source-location-form-number (location)
1138 (nth-value 1 (source-location-form-numbers location)))
1139
1140 (defun resolve-file-source-location (location)
1141 (let ((filename (c::file-source-location-pathname location))
1142 (tlf-number (source-location-tlf-number location))
1143 (form-number (source-location-form-number location)))
1144 (with-open-file (s filename)
1145 (let ((pos (form-number-stream-position tlf-number form-number s)))
1146 (make-location `(:file ,(unix-truename filename))
1147 `(:position ,(1+ pos)))))))
1148
1149 (defun resolve-stream-source-location (location)
1150 (let ((info (c::stream-source-location-user-info location))
1151 (tlf-number (source-location-tlf-number location))
1152 (form-number (source-location-form-number location)))
1153 ;; XXX duplication in frame-source-location
1154 (assert (info-from-emacs-buffer-p info))
1155 (destructuring-bind (&key emacs-buffer emacs-buffer-string
1156 emacs-buffer-offset) info
1157 (with-input-from-string (s emacs-buffer-string)
1158 (let ((pos (form-number-stream-position tlf-number form-number s)))
1159 (make-location `(:buffer ,emacs-buffer)
1160 `(:offset ,emacs-buffer-offset ,pos)))))))
1161
1162 ;; XXX predicates for 18e backward compatibilty. Remove them when
1163 ;; we're 19a only.
1164 (defun file-source-location-p (object)
1165 (when (fboundp 'c::file-source-location-p)
1166 (c::file-source-location-p object)))
1167
1168 (defun stream-source-location-p (object)
1169 (when (fboundp 'c::stream-source-location-p)
1170 (c::stream-source-location-p object)))
1171
1172 (defun source-location-p (object)
1173 (or (file-source-location-p object)
1174 (stream-source-location-p object)))
1175
1176 (defun resolve-source-location (location)
1177 (etypecase location
1178 ((satisfies file-source-location-p)
1179 (resolve-file-source-location location))
1180 ((satisfies stream-source-location-p)
1181 (resolve-stream-source-location location))))
1182
1183 (defun definition-source-location (object name)
1184 (let ((source (pcl::definition-source object)))
1185 (etypecase source
1186 (null
1187 `(:error ,(format nil "No source info for: ~A" object)))
1188 ((satisfies source-location-p)
1189 (resolve-source-location source))
1190 (pathname
1191 (make-name-in-file-location source name))
1192 (cons
1193 (destructuring-bind ((dg name) pathname) source
1194 (declare (ignore dg))
1195 (etypecase pathname
1196 (pathname (make-name-in-file-location pathname (string name)))
1197 (null `(:error ,(format nil "Cannot resolve: ~S" source)))))))))
1198
1199 (defun setf-definitions (name)
1200 (let ((f (or (ext:info :setf :inverse name)
1201 (ext:info :setf :expander name)
1202 (and (symbolp name)
1203 (fboundp `(setf ,name))
1204 (fdefinition `(setf ,name))))))
1205 (if f
1206 `(((setf ,name) ,(function-location (cond ((functionp f) f)
1207 ((macro-function f))
1208 ((fdefinition f)))))))))
1209
1210 (defun variable-location (symbol)
1211 (multiple-value-bind (location foundp)
1212 ;; XXX for 18e compatibilty. rewrite this when we drop 18e
1213 ;; support.
1214 (ignore-errors (eval `(ext:info :source-location :defvar ',symbol)))
1215 (if (and foundp location)
1216 (resolve-source-location location)
1217 `(:error ,(format nil "No source info for variable ~S" symbol)))))
1218
1219 (defun variable-definitions (name)
1220 (if (symbolp name)
1221 (multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
1222 (if recorded-p
1223 (list (list `(variable ,kind ,name)
1224 (variable-location name)))))))
1225
1226 (defun compiler-macro-definitions (symbol)
1227 (maybe-make-definition (compiler-macro-function symbol)
1228 'define-compiler-macro
1229 symbol))
1230
1231 (defun source-transform-definitions (name)
1232 (maybe-make-definition (ext:info :function :source-transform name)
1233 'c:def-source-transform
1234 name))
1235
1236 (defun function-info-definitions (name)
1237 (let ((info (ext:info :function :info name)))
1238 (if info
1239 (append (loop for transform in (c::function-info-transforms info)
1240 collect (list `(c:deftransform ,name
1241 ,(c::type-specifier
1242 (c::transform-type transform)))
1243 (function-location (c::transform-function
1244 transform))))
1245 (maybe-make-definition (c::function-info-derive-type info)
1246 'c::derive-type name)
1247 (maybe-make-definition (c::function-info-optimizer info)
1248 'c::optimizer name)
1249 (maybe-make-definition (c::function-info-ltn-annotate info)
1250 'c::ltn-annotate name)
1251 (maybe-make-definition (c::function-info-ir2-convert info)
1252 'c::ir2-convert name)
1253 (loop for template in (c::function-info-templates info)
1254 collect (list `(c::vop ,(c::template-name template))
1255 (function-location
1256 (c::vop-info-generator-function
1257 template))))))))
1258
1259 (defun ir1-translator-definitions (name)
1260 (maybe-make-definition (ext:info :function :ir1-convert name)
1261 'c:def-ir1-translator name))
1262
1263
1264 ;;;; Documentation.
1265
1266 (defimplementation describe-symbol-for-emacs (symbol)
1267 (let ((result '()))
1268 (flet ((doc (kind)
1269 (or (documentation symbol kind) :not-documented))
1270 (maybe-push (property value)
1271 (when value
1272 (setf result (list* property value result)))))
1273 (maybe-push
1274 :variable (multiple-value-bind (kind recorded-p)
1275 (ext:info variable kind symbol)
1276 (declare (ignore kind))
1277 (if (or (boundp symbol) recorded-p)
1278 (doc 'variable))))
1279 (when (fboundp symbol)
1280 (maybe-push
1281 (cond ((macro-function symbol) :macro)
1282 ((special-operator-p symbol) :special-operator)
1283 ((genericp (fdefinition symbol)) :generic-function)
1284 (t :function))
1285 (doc 'function)))
1286 (maybe-push
1287 :setf (if (or (ext:info setf inverse symbol)
1288 (ext:info setf expander symbol))
1289 (doc 'setf)))
1290 (maybe-push
1291 :type (if (ext:info type kind symbol)
1292 (doc 'type)))
1293 (maybe-push
1294 :class (if (find-class symbol nil)
1295 (doc 'class)))
1296 (maybe-push
1297 :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
1298 (doc 'alien-type)))
1299 (maybe-push
1300 :alien-struct (if (ext:info alien-type struct symbol)
1301 (doc nil)))
1302 (maybe-push
1303 :alien-union (if (ext:info alien-type union symbol)
1304 (doc nil)))
1305 (maybe-push
1306 :alien-enum (if (ext:info alien-type enum symbol)
1307 (doc nil)))
1308 result)))
1309
1310 (defimplementation describe-definition (symbol namespace)
1311 (describe (ecase namespace
1312 (:variable
1313 symbol)
1314 ((:function :generic-function)
1315 (symbol-function symbol))
1316 (:setf
1317 (or (ext:info setf inverse symbol)
1318 (ext:info setf expander symbol)))
1319 (:type
1320 (kernel:values-specifier-type symbol))
1321 (:class
1322 (find-class symbol))
1323 (:alien-struct
1324 (ext:info :alien-type :struct symbol))
1325 (:alien-union
1326 (ext:info :alien-type :union symbol))
1327 (:alien-enum
1328 (ext:info :alien-type :enum symbol))
1329 (:alien-type
1330 (ecase (ext:info :alien-type :kind symbol)
1331 (:primitive
1332 (let ((alien::*values-type-okay* t))
1333 (funcall (ext:info :alien-type :translator symbol)
1334 (list symbol))))
1335 ((:defined)
1336 (ext:info :alien-type :definition symbol))
1337 (:unknown :unkown))))))
1338
1339 ;;;;; Argument lists
1340
1341 (defimplementation arglist (fun)
1342 (etypecase fun
1343 (function (function-arglist fun))
1344 (symbol (function-arglist (or (macro-function fun)
1345 (symbol-function fun))))))
1346
1347 (defun function-arglist (fun)
1348 (let ((arglist
1349 (cond ((eval:interpreted-function-p fun)
1350 (eval:interpreted-function-arglist fun))
1351 ((pcl::generic-function-p fun)
1352 (pcl:generic-function-lambda-list fun))
1353 ((c::byte-function-or-closure-p fun)
1354 (byte-code-function-arglist fun))
1355 ((kernel:%function-arglist (kernel:%function-self fun))
1356 (handler-case (read-arglist fun)
1357 (error () :not-available)))
1358 ;; this should work both for compiled-debug-function
1359 ;; and for interpreted-debug-function
1360 (t
1361 (handler-case (debug-function-arglist
1362 (di::function-debug-function fun))
1363 (di:unhandled-condition () :not-available))))))
1364 (check-type arglist (or list (member :not-available)))
1365 arglist))
1366
1367 (defimplementation function-name (function)
1368 (cond ((eval:interpreted-function-p function)
1369 (eval:interpreted-function-name function))
1370 ((pcl::generic-function-p function)
1371 (pcl::generic-function-name function))
1372 ((c::byte-function-or-closure-p function)
1373 (c::byte-function-name function))
1374 (t (kernel:%function-name (kernel:%function-self function)))))
1375
1376 ;;; A simple case: the arglist is available as a string that we can
1377 ;;; `read'.
1378
1379 (defun read-arglist (fn)
1380 "Parse the arglist-string of the function object FN."
1381 (let ((string (kernel:%function-arglist
1382 (kernel:%function-self fn)))
1383 (package (find-package
1384 (c::compiled-debug-info-package
1385 (kernel:%code-debug-info
1386 (vm::find-code-object fn))))))
1387 (with-standard-io-syntax
1388 (let ((*package* (or package *package*)))
1389 (read-from-string string)))))
1390
1391 ;;; A harder case: an approximate arglist is derived from available
1392 ;;; debugging information.
1393
1394 (defun debug-function-arglist (debug-function)
1395 "Derive the argument list of DEBUG-FUNCTION from debug info."
1396 (let ((args (di::debug-function-lambda-list debug-function))
1397 (required '())
1398 (optional '())
1399 (rest '())
1400 (key '()))
1401 ;; collect the names of debug-vars
1402 (dolist (arg args)
1403 (etypecase arg
1404 (di::debug-variable
1405 (push (di::debug-variable-symbol arg) required))
1406 ((member :deleted)
1407 (push ':deleted required))
1408 (cons
1409 (ecase (car arg)
1410 (:keyword
1411 (push (second arg) key))
1412 (:optional
1413 (push (debug-variable-symbol-or-deleted (second arg)) optional))
1414 (:rest
1415 (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1416 ;; intersperse lambda keywords as needed
1417 (append (nreverse required)
1418 (if optional (cons '&optional (nreverse optional)))
1419 (if rest (cons '&rest (nreverse rest)))
1420 (if key (cons '&key (nreverse key))))))
1421
1422 (defun debug-variable-symbol-or-deleted (var)
1423 (etypecase var
1424 (di:debug-variable
1425 (di::debug-variable-symbol var))
1426 ((member :deleted)
1427 '#:deleted)))
1428
1429 (defun symbol-debug-function-arglist (fname)
1430 "Return FNAME's debug-function-arglist and %function-arglist.
1431 A utility for debugging DEBUG-FUNCTION-ARGLIST."
1432 (let ((fn (fdefinition fname)))
1433 (values (debug-function-arglist (di::function-debug-function fn))
1434 (kernel:%function-arglist (kernel:%function-self fn)))))
1435
1436 ;;; Deriving arglists for byte-compiled functions:
1437 ;;;
1438 (defun byte-code-function-arglist (fn)
1439 ;; There doesn't seem to be much arglist information around for
1440 ;; byte-code functions. Use the arg-count and return something like
1441 ;; (arg0 arg1 ...)
1442 (etypecase fn
1443 (c::simple-byte-function
1444 (loop for i from 0 below (c::simple-byte-function-num-args fn)
1445 collect (make-arg-symbol i)))
1446 (c::hairy-byte-function
1447 (hairy-byte-function-arglist fn))
1448 (c::byte-closure
1449 (byte-code-function-arglist (c::byte-closure-function fn)))))
1450
1451 (defun make-arg-symbol (i)
1452 (make-symbol (format nil "~A~D" (string 'arg) i)))
1453
1454 ;;; A "hairy" byte-function is one that takes a variable number of
1455 ;;; arguments. `hairy-byte-function' is a type from the bytecode
1456 ;;; interpreter.
1457 ;;;
1458 (defun hairy-byte-function-arglist (fn)
1459 (let ((counter -1))
1460 (flet ((next-arg () (make-arg-symbol (incf counter))))
1461 (with-struct (c::hairy-byte-function- min-args max-args rest-arg-p
1462 keywords-p keywords) fn
1463 (let ((arglist '())
1464 (optional (- max-args min-args)))
1465 ;; XXX isn't there a better way to write this?
1466 ;; (Looks fine to me. -luke)
1467 (dotimes (i min-args)
1468 (push (next-arg) arglist))
1469 (when (plusp optional)
1470 (push '&optional arglist)
1471 (dotimes (i optional)
1472 (push (next-arg) arglist)))
1473 (when rest-arg-p
1474 (push '&rest arglist)
1475 (push (next-arg) arglist))
1476 (when keywords-p
1477 (push '&key arglist)
1478 (loop for (key _ __) in keywords
1479 do (push key arglist))
1480 (when (eq keywords-p :allow-others)
1481 (push '&allow-other-keys arglist)))
1482 (nreverse arglist))))))
1483
1484
1485 ;;;; Miscellaneous.
1486
1487 (defimplementation macroexpand-all (form)
1488 (walker:macroexpand-all form))
1489
1490 (defimplementation compiler-macroexpand-1 (form &optional env)
1491 (ext:compiler-macroexpand-1 form env))
1492
1493 (defimplementation compiler-macroexpand (form &optional env)
1494 (ext:compiler-macroexpand form env))
1495
1496 (defimplementation set-default-directory (directory)
1497 (setf (ext:default-directory) (namestring directory))
1498 ;; Setting *default-pathname-defaults* to an absolute directory
1499 ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1500 (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1501 (default-directory))
1502
1503 (defimplementation default-directory ()
1504 (namestring (ext:default-directory)))
1505
1506 (defimplementation getpid ()
1507 (unix:unix-getpid))
1508
1509 (defimplementation lisp-implementation-type-name ()
1510 "cmucl")
1511
1512 (defimplementation quit-lisp ()
1513 (ext::quit))
1514
1515 ;;; source-path-{stream,file,string,etc}-position moved into
1516 ;;; swank-source-path-parser
1517
1518
1519 ;;;; Debugging
1520
1521 (defvar *sldb-stack-top*)
1522
1523 (defimplementation call-with-debugging-environment (debugger-loop-fn)
1524 (unix:unix-sigsetmask 0)
1525 (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1526 (debug:*stack-top-hint* nil)
1527 (kernel:*current-level* 0))
1528 (handler-bind ((di::unhandled-condition
1529 (lambda (condition)
1530 (error (make-condition
1531 'sldb-condition
1532 :original-condition condition)))))
1533 (unwind-protect
1534 (progn
1535 #+(or)(sys:scrub-control-stack)
1536 (funcall debugger-loop-fn))
1537 #+(or)(sys:scrub-control-stack)
1538 ))))
1539
1540 (defun frame-down (frame)
1541 (handler-case (di:frame-down frame)
1542 (di:no-debug-info () nil)))
1543
1544 (defun nth-frame (index)
1545 (do ((frame *sldb-stack-top* (frame-down frame))
1546 (i index (1- i)))
1547 ((zerop i) frame)))
1548
1549 (defimplementation compute-backtrace (start end)
1550 (let ((end (or end most-positive-fixnum)))
1551 (loop for f = (nth-frame start) then (frame-down f)
1552 for i from start below end
1553 while f collect f)))
1554
1555 (defimplementation print-frame (frame stream)
1556 (let ((*standard-output* stream))
1557 (handler-case
1558 (debug::print-frame-call frame :verbosity 1 :number nil)
1559 (error (e)
1560 (ignore-errors (princ e stream))))))
1561
1562 (defimplementation frame-source-location (index)
1563 (let ((frame (nth-frame index)))
1564 (cond ((foreign-frame-p frame) (foreign-frame-source-location frame))
1565 ((code-location-source-location (di:frame-code-location frame))))))
1566
1567 (defimplementation eval-in-frame (form index)
1568 (di:eval-in-frame (nth-frame index) form))
1569
1570 (defun frame-debug-vars (frame)
1571 "Return a vector of debug-variables in frame."
1572 (let ((loc (di:frame-code-location frame)))
1573 (remove-if
1574 (lambda (v)
1575 (not (eq (di:debug-variable-validity v loc) :valid)))
1576 (di::debug-function-debug-variables (di:frame-debug-function frame)))))
1577
1578 (defun debug-var-value (var frame)
1579 (let* ((loc (di:frame-code-location frame))
1580 (validity (di:debug-variable-validity var loc)))
1581 (ecase validity
1582 (:valid (di:debug-variable-value var frame))
1583 ((:invalid :unknown) (make-symbol (string validity))))))
1584
1585 (defimplementation frame-locals (index)
1586 (let ((frame (nth-frame index)))
1587 (loop for v across (frame-debug-vars frame)
1588 collect (list :name (di:debug-variable-symbol v)
1589 :id (di:debug-variable-id v)
1590 :value (debug-var-value v frame)))))
1591
1592 (defimplementation frame-var-value (frame var)
1593 (let* ((frame (nth-frame frame))
1594 (dvar (aref (frame-debug-vars frame) var)))
1595 (debug-var-value dvar frame)))
1596
1597 (defimplementation frame-catch-tags (index)
1598 (mapcar #'car (di:frame-catches (nth-frame index))))
1599
1600 (defimplementation return-from-frame (index form)
1601 (let ((sym (find-symbol (string 'find-debug-tag-for-frame)
1602 :debug-internals)))
1603 (if sym
1604 (let* ((frame (nth-frame index))
1605 (probe (funcall sym frame)))
1606 (cond (probe (throw (car probe) (eval-in-frame form index)))
1607 (t (format nil "Cannot return from frame: ~S" frame))))
1608 "return-from-frame is not implemented in this version of CMUCL.")))
1609
1610 (defimplementation activate-stepping (frame)
1611 (set-step-breakpoints (nth-frame frame)))
1612
1613 (defimplementation sldb-break-on-return (frame)
1614 (break-on-return (nth-frame frame)))
1615
1616 ;;; We set the breakpoint in the caller which might be a bit confusing.
1617 ;;;
1618 (defun break-on-return (frame)
1619 (let* ((caller (di:frame-down frame))
1620 (cl (di:frame-code-location caller)))
1621 (flet ((hook (frame bp)
1622 (when (frame-pointer= frame caller)
1623 (di:delete-breakpoint bp)
1624 (signal-breakpoint bp frame))))
1625 (let* ((info (ecase (di:code-location-kind cl)
1626 ((:single-value-return :unknown-return) nil)
1627 (:known-return (debug-function-returns
1628 (di:frame-debug-function frame)))))
1629 (bp (di:make-breakpoint #'hook cl :kind :code-location
1630 :info info)))
1631 (di:activate-breakpoint bp)
1632 `(:ok ,(format nil "Set breakpoint in ~A" caller))))))
1633
1634 (defun frame-pointer= (frame1 frame2)
1635 "Return true if the frame pointers of FRAME1 and FRAME2 are the same."
1636 (sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
1637
1638 ;;; The PC in escaped frames at a single-return-value point is
1639 ;;; actually vm:single-value-return-byte-offset bytes after the
1640 ;;; position given in the debug info. Here we try to recognize such
1641 ;;; cases.
1642 ;;;
1643 (defun next-code-locations (frame code-location)
1644 "Like `debug::next-code-locations' but be careful in escaped frames."
1645 (let ((next (debug::next-code-locations code-location)))
1646 (flet ((adjust-pc ()
1647 (let ((cl (di::copy-compiled-code-location code-location)))
1648 (incf (di::compiled-code-location-pc cl)
1649 vm:single-value-return-byte-offset)
1650 cl)))
1651 (cond ((and (di::compiled-frame-escaped frame)
1652 (eq (di:code-location-kind code-location)
1653 :single-value-return)
1654 (= (length next) 1)
1655 (di:code-location= (car next) (adjust-pc)))
1656 (debug::next-code-locations (car next)))
1657 (t
1658 next)))))
1659
1660 (defun set-step-breakpoints (frame)
1661 (let ((cl (di:frame-code-location frame)))
1662 (when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
1663 (error "Cannot step in elsewhere code"))
1664 (let* ((debug::*bad-code-location-types*
1665 (remove :call-site debug::*bad-code-location-types*))
1666 (next (next-code-locations frame cl)))
1667 (cond (next
1668 (let ((steppoints '()))
1669 (flet ((hook (bp-frame bp)
1670 (signal-breakpoint bp bp-frame)
1671 (mapc #'di:delete-breakpoint steppoints)))
1672 (dolist (code-location next)
1673 (let ((bp (di:make-breakpoint #'hook code-location
1674 :kind :code-location)))
1675 (di:activate-breakpoint bp)
1676 (push bp steppoints))))))
1677 (t
1678 (break-on-return frame))))))
1679
1680
1681 ;; XXX the return values at return breakpoints should be passed to the
1682 ;; user hooks. debug-int.lisp should be changed to do this cleanly.
1683
1684 ;;; The sigcontext and the PC for a breakpoint invocation are not
1685 ;;; passed to user hook functions, but we need them to extract return
1686 ;;; values. So we advice di::handle-breakpoint and bind the values to
1687 ;;; special variables.
1688 ;;;
1689 (defvar *breakpoint-sigcontext*)
1690 (defvar *breakpoint-pc*)
1691
1692 ;; XXX don't break old versions without fwrappers. Remove this one day.
1693 #+#.(cl:if (cl:find-package :fwrappers) '(and) '(or))
1694 (progn
1695 (fwrappers:define-fwrapper bind-breakpoint-sigcontext (offset c sigcontext)
1696 (let ((*breakpoint-sigcontext* sigcontext)
1697 (*breakpoint-pc* offset))
1698 (fwrappers:call-next-function)))
1699 (fwrappers:set-fwrappers 'di::handle-breakpoint '())
1700 (fwrappers:fwrap 'di::handle-breakpoint #'bind-breakpoint-sigcontext))
1701
1702 (defun sigcontext-object (sc index)
1703 "Extract the lisp object in sigcontext SC at offset INDEX."
1704 (kernel:make-lisp-obj (vm:sigcontext-register sc index)))
1705
1706 (defun known-return-point-values (sigcontext sc-offsets)
1707 (let ((fp (system:int-sap (vm:sigcontext-register sigcontext
1708 vm::cfp-offset))))
1709 (system:without-gcing
1710 (loop for sc-offset across sc-offsets
1711 collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
1712
1713 ;;; CMUCL returns the first few values in registers and the rest on
1714 ;;; the stack. In the multiple value case, the number of values is
1715 ;;; stored in a dedicated register. The values of the registers can be
1716 ;;; accessed in the sigcontext for the breakpoint. There are 3 kinds
1717 ;;; of return conventions: :single-value-return, :unknown-return, and
1718 ;;; :known-return.
1719 ;;;
1720 ;;; The :single-value-return convention returns the value in a
1721 ;;; register without setting the nargs registers.
1722 ;;;
1723 ;;; The :unknown-return variant is used for multiple values. A
1724 ;;; :unknown-return point consists actually of 2 breakpoints: one for
1725 ;;; the single value case and one for the general case. The single
1726 ;;; value breakpoint comes vm:single-value-return-byte-offset after
1727 ;;; the multiple value breakpoint.
1728 ;;;
1729 ;;; The :known-return convention is used by local functions.
1730 ;;; :known-return is currently not supported because we don't know
1731 ;;; where the values are passed.
1732 ;;;
1733 (defun breakpoint-values (breakpoint)
1734 "Return the list of return values for a return point."
1735 (flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
1736 (let ((sc (locally (declare (optimize (speed 0)))
1737 (alien:sap-alien *breakpoint-sigcontext* (* unix:sigcontext))))
1738 (cl (di:breakpoint-what breakpoint)))
1739 (ecase (di:code-location-kind cl)
1740 (:single-value-return
1741 (list (1st sc)))
1742 (:known-return
1743 (let ((info (di:breakpoint-info breakpoint)))
1744 (if (vectorp info)
1745 (known-return-point-values sc info)
1746 (progn
1747 ;;(break)
1748 (list "<<known-return convention not supported>>" info)))))
1749 (:unknown-return
1750 (let ((mv-return-pc (di::compiled-code-location-pc cl)))
1751 (if (= mv-return-pc *breakpoint-pc*)
1752 (mv-function-end-breakpoint-values sc)
1753 (list (1st sc)))))))))
1754
1755 ;; XXX: di::get-function-end-breakpoint-values takes 2 arguments in
1756 ;; newer versions of CMUCL (after ~March 2005).
1757 (defun mv-function-end-breakpoint-values (sigcontext)
1758 (let ((sym (find-symbol "FUNCTION-END-BREAKPOINT-VALUES/STANDARD" :di)))
1759 (cond (sym (funcall sym sigcontext))
1760 (t (funcall 'di::get-function-end-breakpoint-values sigcontext)))))
1761
1762 (defun debug-function-returns (debug-fun)
1763 "Return the return style of DEBUG-FUN."
1764 (let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
1765 (c::compiled-debug-function-returns cdfun)))
1766
1767 (define-condition breakpoint (simple-condition)
1768 ((message :initarg :message :reader breakpoint.message)
1769 (values :initarg :values :reader breakpoint.values))
1770 (:report (lambda (c stream) (princ (breakpoint.message c) stream))))
1771
1772 (defimplementation condition-extras (condition)
1773 (typecase condition
1774 (breakpoint
1775 ;; pop up the source buffer
1776 `((:show-frame-source 0)))
1777 (t '())))
1778
1779 (defun signal-breakpoint (breakpoint frame)
1780 "Signal a breakpoint condition for BREAKPOINT in FRAME.
1781 Try to create a informative message."
1782 (flet ((brk (values fstring &rest args)
1783 (let ((msg (apply #'format nil fstring args))
1784 (debug:*stack-top-hint* frame))
1785 (break 'breakpoint :message msg :values values))))
1786 (with-struct (di::breakpoint- kind what) breakpoint
1787 (case kind
1788 (:code-location
1789 (case (di:code-location-kind what)
1790 ((:single-value-return :known-return :unknown-return)
1791 (let ((values (breakpoint-values breakpoint)))
1792 (brk values "Return value: ~{~S ~}" values)))
1793 (t
1794 #+(or)
1795 (when (eq (di:code-location-kind what) :call-site)
1796 (call-site-function breakpoint frame))
1797 (brk nil "Breakpoint: ~S ~S"
1798 (di:code-location-kind what)
1799 (di::compiled-code-location-pc what)))))
1800 (:function-start
1801 (brk nil "Function start breakpoint"))
1802 (t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
1803
1804 (defimplementation sldb-break-at-start (fname)
1805 (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1806 (cond ((not debug-fun)
1807 `(:error ,(format nil "~S has no debug-function" fname)))
1808 (t
1809 (flet ((hook (frame bp &optional args cookie)
1810 (declare (ignore args cookie))
1811 (signal-breakpoint bp frame)))
1812 (let ((bp (di:make-breakpoint #'hook debug-fun
1813 :kind :function-start)))
1814 (di:activate-breakpoint bp)
1815 `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1816
1817 (defun frame-cfp (frame)
1818 "Return the Control-Stack-Frame-Pointer for FRAME."
1819 (etypecase frame
1820 (di::compiled-frame (di::frame-pointer frame))
1821 ((or di::interpreted-frame null) -1)))
1822
1823 (defun frame-ip (frame)
1824 "Return the (absolute) instruction pointer and the relative pc of FRAME."
1825 (if (not frame)
1826 -1
1827 (let ((debug-fun (di::frame-debug-function frame)))
1828 (etypecase debug-fun
1829 (di::compiled-debug-function
1830 (let* ((code-loc (di:frame-code-location frame))
1831 (component (di::compiled-debug-function-component debug-fun))
1832 (pc (di::compiled-code-location-pc code-loc))
1833 (ip (sys:without-gcing
1834 (sys:sap-int
1835 (sys:sap+ (kernel:code-instructions component) pc)))))
1836 (values ip pc)))
1837 (di::interpreted-debug-function -1)
1838 (di::bogus-debug-function
1839 #-x86 -1
1840 #+x86
1841 (let ((fp (di::frame-pointer (di:frame-up frame))))
1842 (multiple-value-bind (ra ofp) (di::x86-call-context fp)
1843 (declare (ignore ofp))
1844 (values ra 0))))))))
1845
1846 (defun frame-registers (frame)
1847 "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1848 (let* ((cfp (frame-cfp frame))
1849 (csp (frame-cfp (di::frame-up frame)))
1850 (ip (frame-ip frame))
1851 (ocfp (frame-cfp (di::frame-down frame)))
1852 (lra (frame-ip (di::frame-down frame))))
1853 (values csp cfp ip ocfp lra)))
1854
1855 (defun print-frame-registers (frame-number)
1856 (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1857 (flet ((fixnum (p) (etypecase p
1858 (integer p)
1859 (sys:system-area-pointer (sys:sap-int p)))))
1860 (apply #'format t "~
1861 ~8X Stack Pointer
1862 ~8X Frame Pointer
1863 ~8X Instruction Pointer
1864 ~8X Saved Frame Pointer
1865 ~8X Saved Instruction Pointer~%" (mapcar #'fixnum
1866 (multiple-value-list (frame-registers frame)))))))
1867
1868 (defvar *gdb-program-name* "/usr/bin/gdb")
1869
1870 (defimplementation disassemble-frame (frame-number)
1871 (print-frame-registers frame-number)
1872 (terpri)
1873 (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1874 (debug-fun (di::frame-debug-function frame)))
1875 (etypecase debug-fun
1876 (di::compiled-debug-function
1877 (let* ((component (di::compiled-debug-function-component debug-fun))
1878 (fun (di:debug-function-function debug-fun)))
1879 (if fun
1880 (disassemble fun)
1881 (disassem:disassemble-code-component component))))
1882 (di::bogus-debug-function
1883 (cond ((probe-file *gdb-program-name*)
1884 (let ((ip (sys:sap-int (frame-ip frame))))
1885 (princ (gdb-command "disas 0x~x" ip))))
1886 (t
1887 (format t "~%[Disassembling bogus frames not implemented]")))))))
1888
1889 (defmacro with-temporary-file ((stream filename) &body body)
1890 `(call/temporary-file (lambda (,stream ,filename) . ,body)))
1891
1892 (defun call/temporary-file (fun)
1893 (let ((name (system::pick-temporary-file-name)))
1894 (unwind-protect
1895 (with-open-file (stream name :direction :output :if-exists :supersede)
1896 (funcall fun stream name))
1897 (delete-file name))))
1898
1899 (defun gdb-command (format-string &rest args)
1900 (let ((str (gdb-exec (format nil "attach ~d~%~a~%detach"
1901 (getpid)
1902 (apply #'format nil format-string args)))))
1903 (subseq str (1+ (position #\newline str)))))
1904
1905 (defun gdb-exec (cmd)
1906 (with-temporary-file (file filename)
1907 (write-string cmd file)
1908 (force-output file)
1909 (let* ((output (make-string-output-stream))
1910 (proc (ext:run-program "gdb" `("-batch" "-x" ,filename)
1911 :wait t
1912 :output output)))
1913 (assert (eq (ext:process-status proc) :exited))
1914 (assert (eq (ext:process-exit-code proc) 0))
1915 (get-output-stream-string output))))
1916
1917 (defun foreign-frame-p (frame)
1918 #-x86 nil
1919 #+x86 (let ((ip (frame-ip frame)))
1920 (and (sys:system-area-pointer-p ip)
1921 (multiple-value-bind (pc code)
1922 (di::compute-lra-data-from-pc ip)
1923 (declare (ignore pc))
1924 (not code)))))
1925
1926 (defun foreign-frame-source-location (frame)
1927 (let ((ip (sys:sap-int (frame-ip frame))))
1928 (cond ((probe-file *gdb-program-name*)
1929 (parse-gdb-line-info (gdb-command "info line *0x~x" ip)))
1930 (t `(:error "no srcloc available for ~a" frame)))))
1931
1932 ;; The output of gdb looks like:
1933 ;; Line 215 of "../../src/lisp/x86-assem.S"
1934 ;; starts at address 0x805318c <Ldone+11>
1935 ;; and ends at 0x805318e <Ldone+13>.
1936 ;; The ../../ are fixed up with the "target:" search list which might
1937 ;; be wrong sometimes.
1938 (defun parse-gdb-line-info (string)
1939 (with-input-from-string (*standard-input* string)
1940 (let ((w1 (read-word)))
1941 (cond ((equal w1 "Line")
1942 (let ((line (read-word)))
1943 (assert (equal (read-word) "of"))
1944 (let ((file (read-word)))
1945 (make-location (list :file
1946 (unix-truename
1947 (merge-pathnames
1948 (read-from-string file)
1949 (format nil "~a/lisp/"
1950 (unix-truename "target:")))))
1951 (list :line (parse-integer line))))))
1952 (t `(:error ,string))))))
1953
1954 (defun read-word (&optional (stream *standard-input*))
1955 (peek-char t stream)
1956 (concatenate 'string (loop until (whitespacep (peek-char nil stream))
1957 collect (read-char stream))))
1958
1959 (defun whitespacep (char)
1960 (member char '(#\space #\newline)))
1961
1962
1963 ;;;; Inspecting
1964
1965 (defconstant +lowtag-symbols+
1966 '(vm:even-fixnum-type
1967 vm:function-pointer-type
1968 vm:other-immediate-0-type
1969 vm:list-pointer-type
1970 vm:odd-fixnum-type
1971 vm:instance-pointer-type
1972 vm:other-immediate-1-type
1973 vm:other-pointer-type)
1974 "Names of the constants that specify type tags.
1975 The `symbol-value' of each element is a type tag.")
1976
1977 (defconstant +header-type-symbols+
1978 (labels ((suffixp (suffix string)
1979 (and (>= (length string) (length suffix))
1980 (string= string suffix :start1 (- (length string)
1981 (length suffix)))))
1982 (header-type-symbol-p (x)
1983 (and (suffixp "-TYPE" (symbol-name x))
1984 (not (member x +lowtag-symbols+))
1985 (boundp x)
1986 (typep (symbol-value x) 'fixnum))))
1987 (remove-if-not #'header-type-symbol-p
1988 (append (apropos-list "-TYPE" "VM")
1989 (apropos-list "-TYPE" "BIGNUM"))))
1990 "A list of names of the type codes in boxed objects.")
1991
1992 (defimplementation describe-primitive-type (object)
1993 (with-output-to-string (*standard-output*)
1994 (let* ((lowtag (kernel:get-lowtag object))
1995 (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1996 (format t "lowtag: ~A" lowtag-symbol)
1997 (when (member lowtag (list vm:other-pointer-type
1998 vm:function-pointer-type
1999 vm:other-immediate-0-type
2000 vm:other-immediate-1-type
2001 ))
2002 (let* ((type (kernel:get-type object))
2003 (type-symbol (find type +header-type-symbols+
2004 :key #'symbol-value)))
2005 (format t ", type: ~A" type-symbol))))))
2006
2007 (defmethod emacs-inspect ((o t))
2008 (cond ((di::indirect-value-cell-p o)
2009 `("Value: " (:value ,(c:value-cell-ref o))))
2010 ((alien::alien-value-p o)
2011 (inspect-alien-value o))
2012 (t
2013 (cmucl-inspect o))))
2014
2015 (defun cmucl-inspect (o)
2016 (destructuring-bind (text labeledp . parts) (inspect::describe-parts o)
2017 (list* (format nil "~A~%" text)
2018 (if labeledp
2019 (loop for (label . value) in parts
2020 append (label-value-line label value))
2021 (loop for value in parts for i from 0
2022 append (label-value-line i value))))))
2023
2024 (defmethod emacs-inspect ((o function))
2025 (let ((header (kernel:get-type o)))
2026 (cond ((= header vm:function-header-type)
2027 (append (label-value-line*
2028 ("Self" (kernel:%function-self o))
2029 ("Next" (kernel:%function-next o))
2030 ("Name" (kernel:%function-name o))
2031 ("Arglist" (kernel:%function-arglist o))
2032 ("Type" (kernel:%function-type o))
2033 ("Code" (kernel:function-code-header o)))
2034 (list
2035 (with-output-to-string (s)
2036 (disassem:disassemble-function o :stream s)))))
2037 ((= header vm:closure-header-type)
2038 (list* (format nil "~A is a closure.~%" o)
2039 (append
2040 (label-value-line "Function" (kernel:%closure-function o))
2041 `("Environment:" (:newline))
2042 (loop for i from 0 below (1- (kernel:get-closure-length o))
2043 append (label-value-line
2044 i (kernel:%closure-index-ref o i))))))
2045 ((eval::interpreted-function-p o)
2046 (cmucl-inspect o))
2047 (t
2048 (call-next-method)))))
2049
2050 (defmethod emacs-inspect ((o kernel:funcallable-instance))
2051 (append (label-value-line*
2052 (:function (kernel:%funcallable-instance-function o))
2053 (:lexenv (kernel:%funcallable-instance-lexenv o))
2054 (:layout (kernel:%funcallable-instance-layout o)))
2055 (cmucl-inspect o)))
2056
2057 (defmethod emacs-inspect ((o kernel:code-component))
2058 (append
2059 (label-value-line*
2060 ("code-size" (kernel:%code-code-size o))
2061 ("entry-points" (kernel:%code-entry-points o))
2062 ("debug-info" (kernel:%code-debug-info o))
2063 ("trace-table-offset" (kernel:code-header-ref
2064 o vm:code-trace-table-offset-slot)))
2065 `("Constants:" (:newline))
2066 (loop for i from vm:code-constants-offset
2067 below (kernel:get-header-data o)
2068 append (label-value-line i (kernel:code-header-ref o i)))
2069 `("Code:"
2070 (:newline)
2071 , (with-output-to-string (*standard-output*)
2072 (cond ((c::compiled-debug-info-p (kernel:%code-debug-info o))
2073 (disassem:disassemble-code-component o))
2074 ((or
2075 (c::debug-info-p (kernel:%code-debug-info o))
2076 (consp (kernel:code-header-ref
2077 o vm:code-trace-table-offset-slot)))
2078 (c:disassem-byte-component o))
2079 (t
2080 (disassem:disassemble-memory
2081 (disassem::align
2082 (+ (logandc2 (kernel:get-lisp-obj-address o)
2083 vm:lowtag-mask)
2084 (* vm:code-constants-offset vm:word-bytes))
2085 (ash 1 vm:lowtag-bits))
2086 (ash (kernel:%code-code-size o) vm:word-shift))))))))
2087
2088 (defmethod emacs-inspect ((o kernel:fdefn))
2089 (label-value-line*
2090 ("name" (kernel:fdefn-name o))
2091 ("function" (kernel:fdefn-function o))
2092 ("raw-addr" (sys:sap-ref-32
2093 (sys:int-sap (kernel:get-lisp-obj-address o))
2094 (* vm:fdefn-raw-addr-slot vm:word-bytes)))))
2095
2096 #+(or)
2097 (defmethod emacs-inspect ((o array))
2098 (if (typep o 'simple-array)
2099 (call-next-method)
2100 (label-value-line*
2101 (:header (describe-primitive-type o))
2102 (:rank (array-rank o))
2103 (:fill-pointer (kernel:%array-fill-pointer o))
2104 (:fill-pointer-p (kernel:%array-fill-pointer-p o))
2105 (:elements (kernel:%array-available-elements o))
2106 (:data (kernel:%array-data-vector o))
2107 (:displacement (kernel:%array-displacement o))
2108 (:displaced-p (kernel:%array-displaced-p o))
2109 (:dimensions (array-dimensions o)))))
2110
2111 (defmethod emacs-inspect ((o simple-vector))
2112 (append
2113 (label-value-line*
2114 (:header (describe-primitive-type o))
2115 (:length (c::vector-length o)))
2116 (loop for i below (length o)
2117 append (label-value-line i (aref o i)))))
2118
2119 (defun inspect-alien-record (alien)
2120 (with-struct (alien::alien-value- sap type) alien
2121 (with-struct (alien::alien-record-type- kind name fields) type
2122 (append
2123 (label-value-line*
2124 (:sap sap)
2125 (:kind kind)
2126 (:name name))
2127 (loop for field in fields
2128 append (let ((slot (alien::alien-record-field-name field)))
2129 (declare (optimize (speed 0)))
2130 (label-value-line slot (alien:slot alien slot))))))))
2131
2132 (defun inspect-alien-pointer (alien)
2133 (with-struct (alien::alien-value- sap type) alien
2134 (label-value-line*
2135 (:sap sap)
2136 (:type type)
2137 (:to (alien::deref alien)))))
2138
2139 (defun inspect-alien-value (alien)
2140 (typecase (alien::alien-value-type alien)
2141 (alien::alien-record-type (inspect-alien-record alien))
2142 (alien::alien-pointer-type (inspect-alien-pointer alien))
2143 (t (cmucl-inspect alien))))
2144
2145 ;;;; Profiling
2146 (defimplementation profile (fname)
2147 (eval `(profile:profile ,fname)))
2148
2149 (defimplementation unprofile (fname)
2150 (eval `(profile:unprofile ,fname)))
2151
2152 (defimplementation unprofile-all ()
2153 (eval `(profile:unprofile))
2154 "All functions unprofiled.")
2155
2156 (defimplementation profile-report ()
2157 (eval `(profile:report-time)))
2158
2159 (defimplementation profile-reset ()
2160 (eval `(profile:reset-time))
2161 "Reset profiling counters.")
2162
2163 (defimplementation profiled-functions ()
2164 profile:*timed-functions*)
2165
2166 (defimplementation profile-package (package callers methods)
2167 (profile:profile-all :package package
2168 :callers-p callers
2169 #-cmu18e :methods #-cmu18e methods))
2170
2171
2172 ;;;; Multiprocessing
2173
2174 #+mp
2175 (progn
2176 (defimplementation initialize-multiprocessing (continuation)
2177 (mp::init-multi-processing)
2178 (mp:make-process continuation :name "swank")
2179 ;; Threads magic: this never returns! But top-level becomes
2180 ;; available again.
2181 (unless mp::*idle-process*
2182 (mp::startup-idle-and-top-level-loops)))
2183
2184 (defimplementation spawn (fn &key name)
2185 (mp:make-process fn :name (or name "Anonymous")))
2186
2187 (defvar *thread-id-counter* 0)
2188
2189 (defimplementation thread-id (thread)
2190 (or (getf (mp:process-property-list thread) 'id)
2191 (setf (getf (mp:process-property-list thread) 'id)
2192 (incf *thread-id-counter*))))
2193
2194 (defimplementation find-thread (id)
2195 (find id (all-threads)
2196 :key (lambda (p) (getf (mp:process-property-list p) 'id))))
2197
2198 (defimplementation thread-name (thread)
2199 (mp:process-name thread))
2200
2201 (defimplementation thread-status (thread)
2202 (mp:process-whostate thread))
2203
2204 (defimplementation current-thread ()
2205 mp:*current-process*)
2206
2207 (defimplementation all-threads ()
2208 (copy-list mp:*all-processes*))
2209
2210 (defimplementation interrupt-thread (thread fn)
2211 (mp:process-interrupt thread fn))
2212
2213 (defimplementation kill-thread (thread)
2214 (mp:destroy-process thread))
2215
2216 (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
2217
2218 (defstruct (mailbox (:conc-name mailbox.))
2219 (mutex (mp:make-lock "process mailbox"))
2220 (queue '() :type list))
2221
2222 (defun mailbox (thread)
2223 "Return THREAD's mailbox."
2224 (mp:with-lock-held (*mailbox-lock*)
2225 (or (getf (mp:process-property-list thread) 'mailbox)
2226 (setf (getf (mp:process-property-list thread) 'mailbox)
2227 (make-mailbox)))))
2228
2229 (defimplementation send (thread message)
2230 (check-slime-interrupts)
2231 (let* ((mbox (mailbox thread)))
2232 (mp:with-lock-held ((mailbox.mutex mbox))
2233 (setf (mailbox.queue mbox)
2234 (nconc (mailbox.queue mbox) (list message))))))
2235
2236 (defimplementation receive-if (test &optional timeout)
2237 (let ((mbox (mailbox mp:*current-process*)))
2238 (assert (or (not timeout) (eq timeout t)))
2239 (loop
2240 (check-slime-interrupts)
2241 (mp:with-lock-held ((mailbox.mutex mbox))
2242 (let* ((q (mailbox.queue mbox))
2243 (tail (member-if test q)))
2244 (when tail
2245 (setf (mailbox.queue mbox)
2246 (nconc (ldiff q tail) (cdr tail)))
2247 (return (car tail)))))
2248 (when (eq timeout t) (return (values nil t)))
2249 (mp:process-wait-with-timeout
2250 "receive-if" 0.5
2251 (lambda () (some test (mailbox.queue mbox)))))))
2252
2253
2254 ) ;; #+mp
2255
2256
2257
2258 ;;;; GC hooks
2259 ;;;
2260 ;;; Display GC messages in the echo area to avoid cluttering the
2261 ;;; normal output.
2262 ;;;
2263
2264 ;; this should probably not be here, but where else?
2265 (defun background-message (message)
2266 (funcall (find-symbol (string :background-message) :swank)
2267 message))
2268
2269 (defun print-bytes (nbytes &optional stream)
2270 "Print the number NBYTES to STREAM in KB, MB, or GB units."
2271 (let ((names '((0 bytes) (10 kb) (20 mb) (30 gb) (40 tb) (50 eb))))
2272 (multiple-value-bind (power name)
2273 (loop for ((p1 n1) (p2 n2)) on names
2274 while n2 do
2275 (when (<= (expt 2 p1) nbytes (1- (expt 2 p2)))
2276 (return (values p1 n1))))
2277 (cond (name
2278 (format stream "~,1F ~A" (/ nbytes (expt 2 power)) name))
2279 (t
2280 (format stream "~:D bytes" nbytes))))))
2281
2282 (defconstant gc-generations 6)
2283
2284 #+gencgc
2285 (defun generation-stats ()
2286 "Return a string describing the size distribution among the generations."
2287 (let* ((alloc (loop for i below gc-generations
2288 collect (lisp::gencgc-stats i)))
2289 (sum (coerce (reduce #'+ alloc) 'float)))
2290 (format nil "~{~3F~^/~}"
2291 (mapcar (lambda (size) (/ size sum))
2292 alloc))))
2293
2294 (defvar *gc-start-time* 0)
2295
2296 (defun pre-gc-hook (bytes-in-use)
2297 (setq *gc-start-time* (get-internal-real-time))
2298 (let ((msg (format nil "[Commencing GC with ~A in use.]"
2299 (print-bytes bytes-in-use))))
2300 (background-message msg)))
2301
2302 (defun post-gc-hook (bytes-retained bytes-freed trigger)
2303 (declare (ignore trigger))
2304 (let* ((seconds (/ (- (get-internal-real-time) *gc-start-time*)
2305 internal-time-units-per-second))
2306 (msg (format nil "[GC done. ~A freed ~A retained ~A ~4F sec]"
2307 (print-bytes bytes-freed)
2308 (print-bytes bytes-retained)
2309 #+gencgc(generation-stats)
2310 #-gencgc""
2311 seconds)))
2312 (background-message msg)))
2313
2314 (defun install-gc-hooks ()
2315 (setq ext:*gc-notify-before* #'pre-gc-hook)
2316 (setq ext:*gc-notify-after* #'post-gc-hook))
2317
2318 (defun remove-gc-hooks ()
2319 (setq ext:*gc-notify-before* #'lisp::default-gc-notify-before)
2320 (setq ext:*gc-notify-after* #'lisp::default-gc-notify-after))
2321
2322 (defvar *install-gc-hooks* t
2323 "If non-nil install GC hooks")
2324
2325 (defimplementation emacs-connected ()
2326 (when *install-gc-hooks*
2327 (install-gc-hooks)))
2328
2329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2330 ;;Trace implementations
2331 ;;In CMUCL, we have:
2332 ;; (trace <name>)
2333 ;; (trace (method <name> <qualifier>? (<specializer>+)))
2334 ;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
2335 ;; <name> can be a normal name or a (setf name)
2336
2337 (defun tracedp (spec)
2338 (member spec (eval '(trace)) :test #'equal))
2339
2340 (defun toggle-trace-aux (spec &rest options)
2341 (cond ((tracedp spec)
2342 (eval `(untrace ,spec))
2343 (format nil "~S is now untraced." spec))
2344 (t
2345 (eval `(trace ,spec ,@options))
2346 (format nil "~S is now traced." spec))))
2347
2348 (defimplementation toggle-trace (spec)
2349 (ecase (car spec)
2350 ((setf)
2351 (toggle-trace-aux spec))
2352 ((:defgeneric)
2353 (let ((name (second spec)))
2354 (toggle-trace-aux name :methods name)))
2355 ((:defmethod)
2356 (cond ((fboundp `(method ,@(cdr spec)))
2357 (toggle-trace-aux `(method ,(cdr spec))))
2358 ;; Man, is this ugly
2359 ((fboundp `(pcl::fast-method ,@(cdr spec)))
2360 (toggle-trace-aux `(pcl::fast-method ,@(cdr spec))))
2361 (t
2362 (error 'undefined-function :name (cdr spec)))))
2363 ((:call)
2364 (destructuring-bind (caller callee) (cdr spec)
2365 (toggle-trace-aux (process-fspec callee)
2366 :wherein (list (process-fspec caller)))))
2367 ;; doesn't work properly
2368 ;; ((:labels :flet) (toggle-trace-aux (process-fspec spec)))
2369 ))
2370
2371 (defun process-fspec (fspec)
2372 (cond ((consp fspec)
2373 (ecase (first fspec)
2374 ((:defun :defgeneric) (second fspec))
2375 ((:defmethod)
2376 `(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
2377 ((:labels) `(labels ,(third fspec) ,(process-fspec (second fspec))))
2378 ((:flet) `(flet ,(third fspec) ,(process-fspec (second fspec))))))
2379 (t
2380 fspec)))
2381
2382 ;;; Weak datastructures
2383
2384 (defimplementation make-weak-key-hash-table (&rest args)
2385 (apply #'make-hash-table :weak-p t args))
2386
2387
2388 ;;; Save image
2389
2390 (defimplementation save-image (filename &optional restart-function)
2391 (multiple-value-bind (pid error) (unix:unix-fork)
2392 (when (not pid) (error "fork: ~A" (unix:get-unix-error-msg error)))
2393 (cond ((= pid 0)
2394 (apply #'ext:save-lisp
2395 filename
2396 (if restart-function
2397 `(:init-function ,restart-function))))
2398 (t
2399 (let ((status (waitpid pid)))
2400 (destructuring-bind (&key exited? status &allow-other-keys) status
2401 (assert (and exited? (equal status 0)) ()
2402 "Invalid exit status: ~a" status)))))))
2403
2404 (defun waitpid (pid)
2405 (alien:with-alien ((status c-call:int))
2406 (let ((code (alien:alien-funcall
2407 (alien:extern-alien
2408 waitpid (alien:function c-call:int c-call:int
2409 (* c-call:int) c-call:int))
2410 pid (alien:addr status) 0)))
2411 (cond ((= code -1) (error "waitpid: ~A" (unix:get-unix-error-msg)))
2412 (t (assert (= code pid))
2413 (decode-wait-status status))))))
2414
2415 (defun decode-wait-status (status)
2416 (let ((output (with-output-to-string (s)
2417 (call-program (list (process-status-program)
2418 (format nil "~d" status))
2419 :output s))))
2420 (read-from-string output)))
2421
2422 (defun call-program (args &key output)
2423 (destructuring-bind (program &rest args) args
2424 (let ((process (ext:run-program program args :output output)))
2425 (when (not program) (error "fork failed"))
2426 (unless (and (eq (ext:process-status process) :exited)
2427 (= (ext:process-exit-code process) 0))
2428 (error "Non-zero exit status")))))
2429
2430 (defvar *process-status-program* nil)
2431
2432 (defun process-status-program ()
2433 (or *process-status-program*
2434 (setq *process-status-program*
2435 (compile-process-status-program))))
2436
2437 (defun compile-process-status-program ()
2438 (let ((infile (system::pick-temporary-file-name
2439 "/tmp/process-status~d~c.c")))
2440 (with-open-file (stream infile :direction :output :if-exists :supersede)
2441 (format stream "
2442 #include <stdio.h>
2443 #include <stdlib.h>
2444 #include <sys/types.h>
2445 #include <sys/wait.h>
2446 #include <assert.h>
2447
2448 #define FLAG(value) (value ? \"t\" : \"nil\")
2449
2450 int main (int argc, char** argv) {
2451 assert (argc == 2);
2452 {
2453 char* endptr = NULL;
2454 char* arg = argv[1];
2455 long int status = strtol (arg, &endptr, 10);
2456 assert (endptr != arg && *endptr == '\\0');
2457 printf (\"(:exited? %s :status %d :signal? %s :signal %d :coredump? %s\"
2458 \" :stopped? %s :stopsig %d)\\n\",
2459 FLAG(WIFEXITED(status)), WEXITSTATUS(status),
2460 FLAG(WIFSIGNALED(status)), WTERMSIG(status),
2461 FLAG(WCOREDUMP(status)),
2462 FLAG(WIFSTOPPED(status)), WSTOPSIG(status));
2463 fflush (NULL);
2464 return 0;
2465 }
2466 }
2467 ")
2468 (finish-output stream))
2469 (let* ((outfile (system::pick-temporary-file-name))
2470 (args (list "cc" "-o" outfile infile)))
2471 (warn "Running cc: ~{~a ~}~%" args)
2472 (call-program args :output t)
2473 (delete-file infile)
2474 outfile)))
2475
2476 ;; (save-image "/tmp/x.core")
2477
2478 ;; Local Variables:
2479 ;; pbook-heading-regexp: "^;;;\\(;+\\)"
2480 ;; pbook-commentary-regexp: "^;;;\\($\\|[^;]\\)"
2481 ;; End:

  ViewVC Help
Powered by ViewVC 1.1.5