Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/slime/swank-cmucl.lisp
990 views
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
&key policy)
410
(declare (ignore external-format policy))
411
(clear-xref-info input-file)
412
(with-compilation-hooks ()
413
(let ((*buffer-name* nil)
414
(ext:*ignore-extra-close-parentheses* nil))
415
(multiple-value-bind (output-file warnings-p failure-p)
416
(compile-file input-file :output-file output-file)
417
(values output-file warnings-p
418
(or failure-p
419
(when load-p
420
;; Cache the latest source file for definition-finding.
421
(source-cache-get input-file
422
(file-write-date input-file))
423
(not (load output-file)))))))))
424
425
(defimplementation swank-compile-string (string &key buffer position filename
426
policy)
427
(declare (ignore filename policy))
428
(with-compilation-hooks ()
429
(let ((*buffer-name* buffer)
430
(*buffer-start-position* position)
431
(*buffer-substring* string)
432
(source-info (list :emacs-buffer buffer
433
:emacs-buffer-offset position
434
:emacs-buffer-string string)))
435
(with-input-from-string (stream string)
436
(let ((failurep (ext:compile-from-stream stream :source-info
437
source-info)))
438
(not failurep))))))
439
440
441
;;;;; Trapping notes
442
;;;
443
;;; We intercept conditions from the compiler and resignal them as
444
;;; `SWANK:COMPILER-CONDITION's.
445
446
(defun handle-notification-condition (condition)
447
"Handle a condition caused by a compiler warning."
448
(unless (eq condition *previous-compiler-condition*)
449
(let ((context (c::find-error-context nil)))
450
(setq *previous-compiler-condition* condition)
451
(setq *previous-context* context)
452
(signal-compiler-condition condition context))))
453
454
(defun signal-compiler-condition (condition context)
455
(signal (make-condition
456
'compiler-condition
457
:original-condition condition
458
:severity (severity-for-emacs condition)
459
:message (compiler-condition-message condition)
460
:source-context (compiler-error-context context)
461
:location (if (read-error-p condition)
462
(read-error-location condition)
463
(compiler-note-location context)))))
464
465
(defun severity-for-emacs (condition)
466
"Return the severity of CONDITION."
467
(etypecase condition
468
((satisfies read-error-p) :read-error)
469
(c::compiler-error :error)
470
(c::style-warning :note)
471
(c::warning :warning)))
472
473
(defun read-error-p (condition)
474
(eq (type-of condition) 'c::compiler-read-error))
475
476
(defun compiler-condition-message (condition)
477
"Briefly describe a compiler error for Emacs.
478
When Emacs presents the message it already has the source popped up
479
and the source form highlighted. This makes much of the information in
480
the error-context redundant."
481
(princ-to-string condition))
482
483
(defun compiler-error-context (error-context)
484
"Describe context information for Emacs."
485
(declare (type (or c::compiler-error-context null) error-context))
486
(multiple-value-bind (enclosing source)
487
(if error-context
488
(values (c::compiler-error-context-enclosing-source error-context)
489
(c::compiler-error-context-source error-context)))
490
(if (or enclosing source)
491
(format nil "~@[--> ~{~<~%--> ~1:;~A ~>~}~%~]~
492
~@[==>~{~&~A~}~]"
493
enclosing source))))
494
495
(defun read-error-location (condition)
496
(let* ((finfo (car (c::source-info-current-file c::*source-info*)))
497
(file (c::file-info-name finfo))
498
(pos (c::compiler-read-error-position condition)))
499
(cond ((and (eq file :stream) *buffer-name*)
500
(make-location (list :buffer *buffer-name*)
501
(list :offset *buffer-start-position* pos)))
502
((and (pathnamep file) (not *buffer-name*))
503
(make-location (list :file (unix-truename file))
504
(list :position (1+ pos))))
505
(t (break)))))
506
507
(defun compiler-note-location (context)
508
"Derive the location of a complier message from its context.
509
Return a `location' record, or (:error REASON) on failure."
510
(if (null context)
511
(note-error-location)
512
(with-struct (c::compiler-error-context- file-name
513
original-source
514
original-source-path) context
515
(or (locate-compiler-note file-name original-source
516
(reverse original-source-path))
517
(note-error-location)))))
518
519
(defun note-error-location ()
520
"Pseudo-location for notes that can't be located."
521
(cond (*compile-file-truename*
522
(make-location (list :file (unix-truename *compile-file-truename*))
523
(list :eof)))
524
(*buffer-name*
525
(make-location (list :buffer *buffer-name*)
526
(list :position *buffer-start-position*)))
527
(t (list :error "No error location available."))))
528
529
(defun locate-compiler-note (file source source-path)
530
(cond ((and (eq file :stream) *buffer-name*)
531
;; Compiling from a buffer
532
(make-location (list :buffer *buffer-name*)
533
(list :offset *buffer-start-position*
534
(source-path-string-position
535
source-path *buffer-substring*))))
536
((and (pathnamep file) (null *buffer-name*))
537
;; Compiling from a file
538
(make-location (list :file (unix-truename file))
539
(list :position (1+ (source-path-file-position
540
source-path file)))))
541
((and (eq file :lisp) (stringp source))
542
;; No location known, but we have the source form.
543
;; XXX How is this case triggered? -luke (16/May/2004)
544
;; This can happen if the compiler needs to expand a macro
545
;; but the macro-expander is not yet compiled. Calling the
546
;; (interpreted) macro-expander triggers IR1 conversion of
547
;; the lambda expression for the expander and invokes the
548
;; compiler recursively.
549
(make-location (list :source-form source)
550
(list :position 1)))))
551
552
(defun unix-truename (pathname)
553
(ext:unix-namestring (truename pathname)))
554
555
556
;;;; XREF
557
;;;
558
;;; Cross-reference support is based on the standard CMUCL `XREF'
559
;;; package. This package has some caveats: XREF information is
560
;;; recorded during compilation and not preserved in fasl files, and
561
;;; XREF recording is disabled by default. Redefining functions can
562
;;; also cause duplicate references to accumulate, but
563
;;; `swank-compile-file' will automatically clear out any old records
564
;;; from the same filename.
565
;;;
566
;;; To enable XREF recording, set `c:*record-xref-info*' to true. To
567
;;; clear out the XREF database call `xref:init-xref-database'.
568
569
(defmacro defxref (name function)
570
`(defimplementation ,name (name)
571
(xref-results (,function name))))
572
573
(defxref who-calls xref:who-calls)
574
(defxref who-references xref:who-references)
575
(defxref who-binds xref:who-binds)
576
(defxref who-sets xref:who-sets)
577
578
;;; More types of XREF information were added since 18e:
579
;;;
580
#+cmu19
581
(progn
582
(defxref who-macroexpands xref:who-macroexpands)
583
;; XXX
584
(defimplementation who-specializes (symbol)
585
(let* ((methods (xref::who-specializes (find-class symbol)))
586
(locations (mapcar #'method-location methods)))
587
(mapcar #'list methods locations))))
588
589
(defun xref-results (contexts)
590
(mapcar (lambda (xref)
591
(list (xref:xref-context-name xref)
592
(resolve-xref-location xref)))
593
contexts))
594
595
(defun resolve-xref-location (xref)
596
(let ((name (xref:xref-context-name xref))
597
(file (xref:xref-context-file xref))
598
(source-path (xref:xref-context-source-path xref)))
599
(cond ((and file source-path)
600
(let ((position (source-path-file-position source-path file)))
601
(make-location (list :file (unix-truename file))
602
(list :position (1+ position)))))
603
(file
604
(make-location (list :file (unix-truename file))
605
(list :function-name (string name))))
606
(t
607
`(:error ,(format nil "Unknown source location: ~S ~S ~S "
608
name file source-path))))))
609
610
(defun clear-xref-info (namestring)
611
"Clear XREF notes pertaining to NAMESTRING.
612
This is a workaround for a CMUCL bug: XREF records are cumulative."
613
(when c:*record-xref-info*
614
(let ((filename (truename namestring)))
615
(dolist (db (list xref::*who-calls*
616
#+cmu19 xref::*who-is-called*
617
#+cmu19 xref::*who-macroexpands*
618
xref::*who-references*
619
xref::*who-binds*
620
xref::*who-sets*))
621
(maphash (lambda (target contexts)
622
;; XXX update during traversal?
623
(setf (gethash target db)
624
(delete filename contexts
625
:key #'xref:xref-context-file
626
:test #'equalp)))
627
db)))))
628
629
630
;;;; Find callers and callees
631
;;;
632
;;; Find callers and callees by looking at the constant pool of
633
;;; compiled code objects. We assume every fdefn object in the
634
;;; constant pool corresponds to a call to that function. A better
635
;;; strategy would be to use the disassembler to find actual
636
;;; call-sites.
637
638
(labels ((make-stack () (make-array 100 :fill-pointer 0 :adjustable t))
639
(map-cpool (code fun)
640
(declare (type kernel:code-component code) (type function fun))
641
(loop for i from vm:code-constants-offset
642
below (kernel:get-header-data code)
643
do (funcall fun (kernel:code-header-ref code i))))
644
645
(callees (fun)
646
(let ((callees (make-stack)))
647
(map-cpool (vm::find-code-object fun)
648
(lambda (o)
649
(when (kernel:fdefn-p o)
650
(vector-push-extend (kernel:fdefn-function o)
651
callees))))
652
(coerce callees 'list)))
653
654
(callers (fun)
655
(declare (function fun))
656
(let ((callers (make-stack)))
657
(ext:gc :full t)
658
;; scan :dynamic first to avoid the need for even more gcing
659
(dolist (space '(:dynamic :read-only :static))
660
(vm::map-allocated-objects
661
(lambda (obj header size)
662
(declare (type fixnum header) (ignore size))
663
(when (= vm:code-header-type header)
664
(map-cpool obj
665
(lambda (c)
666
(when (and (kernel:fdefn-p c)
667
(eq (kernel:fdefn-function c) fun))
668
(vector-push-extend obj callers))))))
669
space)
670
(ext:gc))
671
(coerce callers 'list)))
672
673
(entry-points (code)
674
(loop for entry = (kernel:%code-entry-points code)
675
then (kernel::%function-next entry)
676
while entry
677
collect entry))
678
679
(guess-main-entry-point (entry-points)
680
(or (find-if (lambda (fun)
681
(ext:valid-function-name-p
682
(kernel:%function-name fun)))
683
entry-points)
684
(car entry-points)))
685
686
(fun-dspec (fun)
687
(list (kernel:%function-name fun) (function-location fun)))
688
689
(code-dspec (code)
690
(let ((eps (entry-points code))
691
(di (kernel:%code-debug-info code)))
692
(cond (eps (fun-dspec (guess-main-entry-point eps)))
693
(di (list (c::debug-info-name di)
694
(debug-info-function-name-location di)))
695
(t (list (princ-to-string code)
696
`(:error "No src-loc available")))))))
697
(declare (inline map-cpool))
698
699
(defimplementation list-callers (symbol)
700
(mapcar #'code-dspec (callers (coerce symbol 'function) )))
701
702
(defimplementation list-callees (symbol)
703
(mapcar #'fun-dspec (callees symbol))))
704
705
(defun test-list-callers (count)
706
(let ((funsyms '()))
707
(do-all-symbols (s)
708
(when (and (fboundp s)
709
(functionp (symbol-function s))
710
(not (macro-function s))
711
(not (special-operator-p s)))
712
(push s funsyms)))
713
(let ((len (length funsyms)))
714
(dotimes (i count)
715
(let ((sym (nth (random len) funsyms)))
716
(format t "~s -> ~a~%" sym (mapcar #'car (list-callers sym))))))))
717
718
;; (test-list-callers 100)
719
720
721
;;;; Resolving source locations
722
;;;
723
;;; Our mission here is to "resolve" references to code locations into
724
;;; actual file/buffer names and character positions. The references
725
;;; we work from come out of the compiler's statically-generated debug
726
;;; information, such as `code-location''s and `debug-source''s. For
727
;;; more details, see the "Debugger Programmer's Interface" section of
728
;;; the CMUCL manual.
729
;;;
730
;;; The first step is usually to find the corresponding "source-path"
731
;;; for the location. Once we have the source-path we can pull up the
732
;;; source file and `READ' our way through to the right position. The
733
;;; main source-code groveling work is done in
734
;;; `swank-source-path-parser.lisp'.
735
736
(defvar *debug-definition-finding* nil
737
"When true don't handle errors while looking for definitions.
738
This is useful when debugging the definition-finding code.")
739
740
(defvar *source-snippet-size* 256
741
"Maximum number of characters in a snippet of source code.
742
Snippets at the beginning of definitions are used to tell Emacs what
743
the definitions looks like, so that it can accurately find them by
744
text search.")
745
746
(defmacro safe-definition-finding (&body body)
747
"Execute BODY and return the source-location it returns.
748
If an error occurs and `*debug-definition-finding*' is false, then
749
return an error pseudo-location.
750
751
The second return value is NIL if no error occurs, otherwise it is the
752
condition object."
753
`(flet ((body () ,@body))
754
(if *debug-definition-finding*
755
(body)
756
(handler-case (values (progn ,@body) nil)
757
(error (c) (values `(:error ,(trim-whitespace (princ-to-string c)))
758
c))))))
759
760
(defun trim-whitespace (string)
761
(string-trim #(#\newline #\space #\tab) string))
762
763
(defun code-location-source-location (code-location)
764
"Safe wrapper around `code-location-from-source-location'."
765
(safe-definition-finding
766
(source-location-from-code-location code-location)))
767
768
(defun source-location-from-code-location (code-location)
769
"Return the source location for CODE-LOCATION."
770
(let ((debug-fun (di:code-location-debug-function code-location)))
771
(when (di::bogus-debug-function-p debug-fun)
772
;; Those lousy cheapskates! They've put in a bogus debug source
773
;; because the code was compiled at a low debug setting.
774
(error "Bogus debug function: ~A" debug-fun)))
775
(let* ((debug-source (di:code-location-debug-source code-location))
776
(from (di:debug-source-from debug-source))
777
(name (di:debug-source-name debug-source)))
778
(ecase from
779
(:file
780
(location-in-file name code-location debug-source))
781
(:stream
782
(location-in-stream code-location debug-source))
783
(:lisp
784
;; The location comes from a form passed to `compile'.
785
;; The best we can do is return the form itself for printing.
786
(make-location
787
(list :source-form (with-output-to-string (*standard-output*)
788
(debug::print-code-location-source-form
789
code-location 100 t)))
790
(list :position 1))))))
791
792
(defun location-in-file (filename code-location debug-source)
793
"Resolve the source location for CODE-LOCATION in FILENAME."
794
(let* ((code-date (di:debug-source-created debug-source))
795
(root-number (di:debug-source-root-number debug-source))
796
(source-code (get-source-code filename code-date)))
797
(with-input-from-string (s source-code)
798
(make-location (list :file (unix-truename filename))
799
(list :position (1+ (code-location-stream-position
800
code-location s root-number)))
801
`(:snippet ,(read-snippet s))))))
802
803
(defun location-in-stream (code-location debug-source)
804
"Resolve the source location for a CODE-LOCATION from a stream.
805
This only succeeds if the code was compiled from an Emacs buffer."
806
(unless (debug-source-info-from-emacs-buffer-p debug-source)
807
(error "The code is compiled from a non-SLIME stream."))
808
(let* ((info (c::debug-source-info debug-source))
809
(string (getf info :emacs-buffer-string))
810
(position (code-location-string-offset
811
code-location
812
string)))
813
(make-location
814
(list :buffer (getf info :emacs-buffer))
815
(list :offset (getf info :emacs-buffer-offset) position)
816
(list :snippet (with-input-from-string (s string)
817
(file-position s position)
818
(read-snippet s))))))
819
820
;;;;; Function-name locations
821
;;;
822
(defun debug-info-function-name-location (debug-info)
823
"Return a function-name source-location for DEBUG-INFO.
824
Function-name source-locations are a fallback for when precise
825
positions aren't available."
826
(with-struct (c::debug-info- (fname name) source) debug-info
827
(with-struct (c::debug-source- info from name) (car source)
828
(ecase from
829
(:file
830
(make-location (list :file (namestring (truename name)))
831
(list :function-name (string fname))))
832
(:stream
833
(assert (debug-source-info-from-emacs-buffer-p (car source)))
834
(make-location (list :buffer (getf info :emacs-buffer))
835
(list :function-name (string fname))))
836
(:lisp
837
(make-location (list :source-form (princ-to-string (aref name 0)))
838
(list :position 1)))))))
839
840
(defun debug-source-info-from-emacs-buffer-p (debug-source)
841
"Does the `info' slot of DEBUG-SOURCE contain an Emacs buffer location?
842
This is true for functions that were compiled directly from buffers."
843
(info-from-emacs-buffer-p (c::debug-source-info debug-source)))
844
845
(defun info-from-emacs-buffer-p (info)
846
(and info
847
(consp info)
848
(eq :emacs-buffer (car info))))
849
850
851
;;;;; Groveling source-code for positions
852
853
(defun code-location-stream-position (code-location stream root)
854
"Return the byte offset of CODE-LOCATION in STREAM. Extract the
855
toplevel-form-number and form-number from CODE-LOCATION and use that
856
to find the position of the corresponding form.
857
858
Finish with STREAM positioned at the start of the code location."
859
(let* ((location (debug::maybe-block-start-location code-location))
860
(tlf-offset (- (di:code-location-top-level-form-offset location)
861
root))
862
(form-number (di:code-location-form-number location)))
863
(let ((pos (form-number-stream-position tlf-offset form-number stream)))
864
(file-position stream pos)
865
pos)))
866
867
(defun form-number-stream-position (tlf-number form-number stream)
868
"Return the starting character position of a form in STREAM.
869
TLF-NUMBER is the top-level-form number.
870
FORM-NUMBER is an index into a source-path table for the TLF."
871
(multiple-value-bind (tlf position-map) (read-source-form tlf-number stream)
872
(let* ((path-table (di:form-number-translations tlf 0))
873
(source-path
874
(if (<= (length path-table) form-number) ; source out of sync?
875
(list 0) ; should probably signal a condition
876
(reverse (cdr (aref path-table form-number))))))
877
(source-path-source-position source-path tlf position-map))))
878
879
(defun code-location-string-offset (code-location string)
880
"Return the byte offset of CODE-LOCATION in STRING.
881
See CODE-LOCATION-STREAM-POSITION."
882
(with-input-from-string (s string)
883
(code-location-stream-position code-location s 0)))
884
885
886
;;;; Finding definitions
887
888
;;; There are a great many different types of definition for us to
889
;;; find. We search for definitions of every kind and return them in a
890
;;; list.
891
892
(defimplementation find-definitions (name)
893
(append (function-definitions name)
894
(setf-definitions name)
895
(variable-definitions name)
896
(class-definitions name)
897
(type-definitions name)
898
(compiler-macro-definitions name)
899
(source-transform-definitions name)
900
(function-info-definitions name)
901
(ir1-translator-definitions name)))
902
903
;;;;; Functions, macros, generic functions, methods
904
;;;
905
;;; We make extensive use of the compile-time debug information that
906
;;; CMUCL records, in particular "debug functions" and "code
907
;;; locations." Refer to the "Debugger Programmer's Interface" section
908
;;; of the CMUCL manual for more details.
909
910
(defun function-definitions (name)
911
"Return definitions for NAME in the \"function namespace\", i.e.,
912
regular functions, generic functions, methods and macros.
913
NAME can any valid function name (e.g, (setf car))."
914
(let ((macro? (and (symbolp name) (macro-function 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
(function?
922
(let ((function (fdefinition name)))
923
(if (genericp function)
924
(gf-definitions name function)
925
(list (list `(function ,name)
926
(function-location function)))))))))
927
928
;;;;;; Ordinary (non-generic/macro/special) functions
929
;;;
930
;;; First we test if FUNCTION is a closure created by defstruct, and
931
;;; if so extract the defstruct-description (`dd') from the closure
932
;;; and find the constructor for the struct. Defstruct creates a
933
;;; defun for the default constructor and we use that as an
934
;;; approximation to the source location of the defstruct.
935
;;;
936
;;; For an ordinary function we return the source location of the
937
;;; first code-location we find.
938
;;;
939
(defun function-location (function)
940
"Return the source location for FUNCTION."
941
(cond ((struct-closure-p function)
942
(struct-closure-location function))
943
((c::byte-function-or-closure-p function)
944
(byte-function-location function))
945
(t
946
(compiled-function-location function))))
947
948
(defun compiled-function-location (function)
949
"Return the location of a regular compiled function."
950
(multiple-value-bind (code-location error)
951
(safe-definition-finding (function-first-code-location function))
952
(cond (error (list :error (princ-to-string error)))
953
(t (code-location-source-location code-location)))))
954
955
(defun function-first-code-location (function)
956
"Return the first code-location we can find for FUNCTION."
957
(and (function-has-debug-function-p function)
958
(di:debug-function-start-location
959
(di:function-debug-function function))))
960
961
(defun function-has-debug-function-p (function)
962
(di:function-debug-function function))
963
964
(defun function-code-object= (closure function)
965
(and (eq (vm::find-code-object closure)
966
(vm::find-code-object function))
967
(not (eq closure function))))
968
969
(defun byte-function-location (fun)
970
"Return the location of the byte-compiled function FUN."
971
(etypecase fun
972
((or c::hairy-byte-function c::simple-byte-function)
973
(let* ((di (kernel:%code-debug-info (c::byte-function-component fun))))
974
(if di
975
(debug-info-function-name-location di)
976
`(:error
977
,(format nil "Byte-function without debug-info: ~a" fun)))))
978
(c::byte-closure
979
(byte-function-location (c::byte-closure-function fun)))))
980
981
;;; Here we deal with structure accessors. Note that `dd' is a
982
;;; "defstruct descriptor" structure in CMUCL. A `dd' describes a
983
;;; `defstruct''d structure.
984
985
(defun struct-closure-p (function)
986
"Is FUNCTION a closure created by defstruct?"
987
(or (function-code-object= function #'kernel::structure-slot-accessor)
988
(function-code-object= function #'kernel::structure-slot-setter)
989
(function-code-object= function #'kernel::%defstruct)))
990
991
(defun struct-closure-location (function)
992
"Return the location of the structure that FUNCTION belongs to."
993
(assert (struct-closure-p function))
994
(safe-definition-finding
995
(dd-location (struct-closure-dd function))))
996
997
(defun struct-closure-dd (function)
998
"Return the defstruct-definition (dd) of FUNCTION."
999
(assert (= (kernel:get-type function) vm:closure-header-type))
1000
(flet ((find-layout (function)
1001
(sys:find-if-in-closure
1002
(lambda (x)
1003
(let ((value (if (di::indirect-value-cell-p x)
1004
(c:value-cell-ref x)
1005
x)))
1006
(when (kernel::layout-p value)
1007
(return-from find-layout value))))
1008
function)))
1009
(kernel:layout-info (find-layout function))))
1010
1011
(defun dd-location (dd)
1012
"Return the location of a `defstruct'."
1013
;; Find the location in a constructor.
1014
(function-location (struct-constructor dd)))
1015
1016
(defun struct-constructor (dd)
1017
"Return a constructor function from a defstruct definition.
1018
Signal an error if no constructor can be found."
1019
(let* ((constructor (or (kernel:dd-default-constructor dd)
1020
(car (kernel::dd-constructors dd))))
1021
(sym (if (consp constructor) (car constructor) constructor)))
1022
(unless sym
1023
(error "Cannot find structure's constructor: ~S" (kernel::dd-name dd)))
1024
(coerce sym 'function)))
1025
1026
;;;;;; Generic functions and methods
1027
1028
(defun gf-definitions (name function)
1029
"Return the definitions of a generic function and its methods."
1030
(cons (list `(defgeneric ,name) (gf-location function))
1031
(gf-method-definitions function)))
1032
1033
(defun gf-location (gf)
1034
"Return the location of the generic function GF."
1035
(definition-source-location gf (pcl::generic-function-name gf)))
1036
1037
(defun gf-method-definitions (gf)
1038
"Return the locations of all methods of the generic function GF."
1039
(mapcar #'method-definition (pcl::generic-function-methods gf)))
1040
1041
(defun method-definition (method)
1042
(list (method-dspec method)
1043
(method-location method)))
1044
1045
(defun method-dspec (method)
1046
"Return a human-readable \"definition specifier\" for METHOD."
1047
(let* ((gf (pcl:method-generic-function method))
1048
(name (pcl:generic-function-name gf))
1049
(specializers (pcl:method-specializers method))
1050
(qualifiers (pcl:method-qualifiers method)))
1051
`(method ,name ,@qualifiers ,(pcl::unparse-specializers specializers))))
1052
1053
;; XXX maybe special case setters/getters
1054
(defun method-location (method)
1055
(function-location (or (pcl::method-fast-function method)
1056
(pcl:method-function method))))
1057
1058
(defun genericp (fn)
1059
(typep fn 'generic-function))
1060
1061
;;;;;; Types and classes
1062
1063
(defun type-definitions (name)
1064
"Return `deftype' locations for type NAME."
1065
(maybe-make-definition (ext:info :type :expander name) 'deftype name))
1066
1067
(defun maybe-make-definition (function kind name)
1068
"If FUNCTION is non-nil then return its definition location."
1069
(if function
1070
(list (list `(,kind ,name) (function-location function)))))
1071
1072
(defun class-definitions (name)
1073
"Return the definition locations for the class called NAME."
1074
(if (symbolp name)
1075
(let ((class (kernel::find-class name nil)))
1076
(etypecase class
1077
(null '())
1078
(kernel::structure-class
1079
(list (list `(defstruct ,name) (dd-location (find-dd name)))))
1080
#+(or)
1081
(conditions::condition-class
1082
(list (list `(define-condition ,name)
1083
(condition-class-location class))))
1084
(kernel::standard-class
1085
(list (list `(defclass ,name)
1086
(class-location (find-class name)))))
1087
((or kernel::built-in-class
1088
conditions::condition-class
1089
kernel:funcallable-structure-class)
1090
(list (list `(kernel::define-type-class ,name)
1091
`(:error
1092
,(format nil "No source info for ~A" name)))))))))
1093
1094
(defun class-location (class)
1095
"Return the `defclass' location for CLASS."
1096
(definition-source-location class (pcl:class-name class)))
1097
1098
(defun find-dd (name)
1099
"Find the defstruct-definition by the name of its structure-class."
1100
(let ((layout (ext:info :type :compiler-layout name)))
1101
(if layout
1102
(kernel:layout-info layout))))
1103
1104
(defun condition-class-location (class)
1105
(let ((slots (conditions::condition-class-slots class))
1106
(name (conditions::condition-class-name class)))
1107
(cond ((null slots)
1108
`(:error ,(format nil "No location info for condition: ~A" name)))
1109
(t
1110
;; Find the class via one of its slot-reader methods.
1111
(let* ((slot (first slots))
1112
(gf (fdefinition
1113
(first (conditions::condition-slot-readers slot)))))
1114
(method-location
1115
(first
1116
(pcl:compute-applicable-methods-using-classes
1117
gf (list (find-class name))))))))))
1118
1119
(defun make-name-in-file-location (file string)
1120
(multiple-value-bind (filename c)
1121
(ignore-errors
1122
(unix-truename (merge-pathnames (make-pathname :type "lisp")
1123
file)))
1124
(cond (filename (make-location `(:file ,filename)
1125
`(:function-name ,(string string))))
1126
(t (list :error (princ-to-string c))))))
1127
1128
(defun source-location-form-numbers (location)
1129
(c::decode-form-numbers (c::form-numbers-form-numbers location)))
1130
1131
(defun source-location-tlf-number (location)
1132
(nth-value 0 (source-location-form-numbers location)))
1133
1134
(defun source-location-form-number (location)
1135
(nth-value 1 (source-location-form-numbers location)))
1136
1137
(defun resolve-file-source-location (location)
1138
(let ((filename (c::file-source-location-pathname location))
1139
(tlf-number (source-location-tlf-number location))
1140
(form-number (source-location-form-number location)))
1141
(with-open-file (s filename)
1142
(let ((pos (form-number-stream-position tlf-number form-number s)))
1143
(make-location `(:file ,(unix-truename filename))
1144
`(:position ,(1+ pos)))))))
1145
1146
(defun resolve-stream-source-location (location)
1147
(let ((info (c::stream-source-location-user-info location))
1148
(tlf-number (source-location-tlf-number location))
1149
(form-number (source-location-form-number location)))
1150
;; XXX duplication in frame-source-location
1151
(assert (info-from-emacs-buffer-p info))
1152
(destructuring-bind (&key emacs-buffer emacs-buffer-string
1153
emacs-buffer-offset) info
1154
(with-input-from-string (s emacs-buffer-string)
1155
(let ((pos (form-number-stream-position tlf-number form-number s)))
1156
(make-location `(:buffer ,emacs-buffer)
1157
`(:offset ,emacs-buffer-offset ,pos)))))))
1158
1159
;; XXX predicates for 18e backward compatibilty. Remove them when
1160
;; we're 19a only.
1161
(defun file-source-location-p (object)
1162
(when (fboundp 'c::file-source-location-p)
1163
(c::file-source-location-p object)))
1164
1165
(defun stream-source-location-p (object)
1166
(when (fboundp 'c::stream-source-location-p)
1167
(c::stream-source-location-p object)))
1168
1169
(defun source-location-p (object)
1170
(or (file-source-location-p object)
1171
(stream-source-location-p object)))
1172
1173
(defun resolve-source-location (location)
1174
(etypecase location
1175
((satisfies file-source-location-p)
1176
(resolve-file-source-location location))
1177
((satisfies stream-source-location-p)
1178
(resolve-stream-source-location location))))
1179
1180
(defun definition-source-location (object name)
1181
(let ((source (pcl::definition-source object)))
1182
(etypecase source
1183
(null
1184
`(:error ,(format nil "No source info for: ~A" object)))
1185
((satisfies source-location-p)
1186
(resolve-source-location source))
1187
(pathname
1188
(make-name-in-file-location source name))
1189
(cons
1190
(destructuring-bind ((dg name) pathname) source
1191
(declare (ignore dg))
1192
(etypecase pathname
1193
(pathname (make-name-in-file-location pathname (string name)))
1194
(null `(:error ,(format nil "Cannot resolve: ~S" source)))))))))
1195
1196
(defun setf-definitions (name)
1197
(let ((f (or (ext:info :setf :inverse name)
1198
(ext:info :setf :expander name)
1199
(and (symbolp name)
1200
(fboundp `(setf ,name))
1201
(fdefinition `(setf ,name))))))
1202
(if f
1203
`(((setf ,name) ,(function-location (cond ((functionp f) f)
1204
((macro-function f))
1205
((fdefinition f)))))))))
1206
1207
(defun variable-location (symbol)
1208
(multiple-value-bind (location foundp)
1209
;; XXX for 18e compatibilty. rewrite this when we drop 18e
1210
;; support.
1211
(ignore-errors (eval `(ext:info :source-location :defvar ',symbol)))
1212
(if (and foundp location)
1213
(resolve-source-location location)
1214
`(:error ,(format nil "No source info for variable ~S" symbol)))))
1215
1216
(defun variable-definitions (name)
1217
(if (symbolp name)
1218
(multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
1219
(if recorded-p
1220
(list (list `(variable ,kind ,name)
1221
(variable-location name)))))))
1222
1223
(defun compiler-macro-definitions (symbol)
1224
(maybe-make-definition (compiler-macro-function symbol)
1225
'define-compiler-macro
1226
symbol))
1227
1228
(defun source-transform-definitions (name)
1229
(maybe-make-definition (ext:info :function :source-transform name)
1230
'c:def-source-transform
1231
name))
1232
1233
(defun function-info-definitions (name)
1234
(let ((info (ext:info :function :info name)))
1235
(if info
1236
(append (loop for transform in (c::function-info-transforms info)
1237
collect (list `(c:deftransform ,name
1238
,(c::type-specifier
1239
(c::transform-type transform)))
1240
(function-location (c::transform-function
1241
transform))))
1242
(maybe-make-definition (c::function-info-derive-type info)
1243
'c::derive-type name)
1244
(maybe-make-definition (c::function-info-optimizer info)
1245
'c::optimizer name)
1246
(maybe-make-definition (c::function-info-ltn-annotate info)
1247
'c::ltn-annotate name)
1248
(maybe-make-definition (c::function-info-ir2-convert info)
1249
'c::ir2-convert name)
1250
(loop for template in (c::function-info-templates info)
1251
collect (list `(c::vop ,(c::template-name template))
1252
(function-location
1253
(c::vop-info-generator-function
1254
template))))))))
1255
1256
(defun ir1-translator-definitions (name)
1257
(maybe-make-definition (ext:info :function :ir1-convert name)
1258
'c:def-ir1-translator name))
1259
1260
1261
;;;; Documentation.
1262
1263
(defimplementation describe-symbol-for-emacs (symbol)
1264
(let ((result '()))
1265
(flet ((doc (kind)
1266
(or (documentation symbol kind) :not-documented))
1267
(maybe-push (property value)
1268
(when value
1269
(setf result (list* property value result)))))
1270
(maybe-push
1271
:variable (multiple-value-bind (kind recorded-p)
1272
(ext:info variable kind symbol)
1273
(declare (ignore kind))
1274
(if (or (boundp symbol) recorded-p)
1275
(doc 'variable))))
1276
(when (fboundp symbol)
1277
(maybe-push
1278
(cond ((macro-function symbol) :macro)
1279
((special-operator-p symbol) :special-operator)
1280
((genericp (fdefinition symbol)) :generic-function)
1281
(t :function))
1282
(doc 'function)))
1283
(maybe-push
1284
:setf (if (or (ext:info setf inverse symbol)
1285
(ext:info setf expander symbol))
1286
(doc 'setf)))
1287
(maybe-push
1288
:type (if (ext:info type kind symbol)
1289
(doc 'type)))
1290
(maybe-push
1291
:class (if (find-class symbol nil)
1292
(doc 'class)))
1293
(maybe-push
1294
:alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
1295
(doc 'alien-type)))
1296
(maybe-push
1297
:alien-struct (if (ext:info alien-type struct symbol)
1298
(doc nil)))
1299
(maybe-push
1300
:alien-union (if (ext:info alien-type union symbol)
1301
(doc nil)))
1302
(maybe-push
1303
:alien-enum (if (ext:info alien-type enum symbol)
1304
(doc nil)))
1305
result)))
1306
1307
(defimplementation describe-definition (symbol namespace)
1308
(describe (ecase namespace
1309
(:variable
1310
symbol)
1311
((:function :generic-function)
1312
(symbol-function symbol))
1313
(:setf
1314
(or (ext:info setf inverse symbol)
1315
(ext:info setf expander symbol)))
1316
(:type
1317
(kernel:values-specifier-type symbol))
1318
(:class
1319
(find-class symbol))
1320
(:alien-struct
1321
(ext:info :alien-type :struct symbol))
1322
(:alien-union
1323
(ext:info :alien-type :union symbol))
1324
(:alien-enum
1325
(ext:info :alien-type :enum symbol))
1326
(:alien-type
1327
(ecase (ext:info :alien-type :kind symbol)
1328
(:primitive
1329
(let ((alien::*values-type-okay* t))
1330
(funcall (ext:info :alien-type :translator symbol)
1331
(list symbol))))
1332
((:defined)
1333
(ext:info :alien-type :definition symbol))
1334
(:unknown :unkown))))))
1335
1336
;;;;; Argument lists
1337
1338
(defimplementation arglist (fun)
1339
(etypecase fun
1340
(function (function-arglist fun))
1341
(symbol (function-arglist (or (macro-function fun)
1342
(symbol-function fun))))))
1343
1344
(defun function-arglist (fun)
1345
(let ((arglist
1346
(cond ((eval:interpreted-function-p fun)
1347
(eval:interpreted-function-arglist fun))
1348
((pcl::generic-function-p fun)
1349
(pcl:generic-function-lambda-list fun))
1350
((c::byte-function-or-closure-p fun)
1351
(byte-code-function-arglist fun))
1352
((kernel:%function-arglist (kernel:%function-self fun))
1353
(handler-case (read-arglist fun)
1354
(error () :not-available)))
1355
;; this should work both for compiled-debug-function
1356
;; and for interpreted-debug-function
1357
(t
1358
(handler-case (debug-function-arglist
1359
(di::function-debug-function fun))
1360
(di:unhandled-condition () :not-available))))))
1361
(check-type arglist (or list (member :not-available)))
1362
arglist))
1363
1364
(defimplementation function-name (function)
1365
(cond ((eval:interpreted-function-p function)
1366
(eval:interpreted-function-name function))
1367
((pcl::generic-function-p function)
1368
(pcl::generic-function-name function))
1369
((c::byte-function-or-closure-p function)
1370
(c::byte-function-name function))
1371
(t (kernel:%function-name (kernel:%function-self function)))))
1372
1373
;;; A simple case: the arglist is available as a string that we can
1374
;;; `read'.
1375
1376
(defun read-arglist (fn)
1377
"Parse the arglist-string of the function object FN."
1378
(let ((string (kernel:%function-arglist
1379
(kernel:%function-self fn)))
1380
(package (find-package
1381
(c::compiled-debug-info-package
1382
(kernel:%code-debug-info
1383
(vm::find-code-object fn))))))
1384
(with-standard-io-syntax
1385
(let ((*package* (or package *package*)))
1386
(read-from-string string)))))
1387
1388
;;; A harder case: an approximate arglist is derived from available
1389
;;; debugging information.
1390
1391
(defun debug-function-arglist (debug-function)
1392
"Derive the argument list of DEBUG-FUNCTION from debug info."
1393
(let ((args (di::debug-function-lambda-list debug-function))
1394
(required '())
1395
(optional '())
1396
(rest '())
1397
(key '()))
1398
;; collect the names of debug-vars
1399
(dolist (arg args)
1400
(etypecase arg
1401
(di::debug-variable
1402
(push (di::debug-variable-symbol arg) required))
1403
((member :deleted)
1404
(push ':deleted required))
1405
(cons
1406
(ecase (car arg)
1407
(:keyword
1408
(push (second arg) key))
1409
(:optional
1410
(push (debug-variable-symbol-or-deleted (second arg)) optional))
1411
(:rest
1412
(push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1413
;; intersperse lambda keywords as needed
1414
(append (nreverse required)
1415
(if optional (cons '&optional (nreverse optional)))
1416
(if rest (cons '&rest (nreverse rest)))
1417
(if key (cons '&key (nreverse key))))))
1418
1419
(defun debug-variable-symbol-or-deleted (var)
1420
(etypecase var
1421
(di:debug-variable
1422
(di::debug-variable-symbol var))
1423
((member :deleted)
1424
'#:deleted)))
1425
1426
(defun symbol-debug-function-arglist (fname)
1427
"Return FNAME's debug-function-arglist and %function-arglist.
1428
A utility for debugging DEBUG-FUNCTION-ARGLIST."
1429
(let ((fn (fdefinition fname)))
1430
(values (debug-function-arglist (di::function-debug-function fn))
1431
(kernel:%function-arglist (kernel:%function-self fn)))))
1432
1433
;;; Deriving arglists for byte-compiled functions:
1434
;;;
1435
(defun byte-code-function-arglist (fn)
1436
;; There doesn't seem to be much arglist information around for
1437
;; byte-code functions. Use the arg-count and return something like
1438
;; (arg0 arg1 ...)
1439
(etypecase fn
1440
(c::simple-byte-function
1441
(loop for i from 0 below (c::simple-byte-function-num-args fn)
1442
collect (make-arg-symbol i)))
1443
(c::hairy-byte-function
1444
(hairy-byte-function-arglist fn))
1445
(c::byte-closure
1446
(byte-code-function-arglist (c::byte-closure-function fn)))))
1447
1448
(defun make-arg-symbol (i)
1449
(make-symbol (format nil "~A~D" (string 'arg) i)))
1450
1451
;;; A "hairy" byte-function is one that takes a variable number of
1452
;;; arguments. `hairy-byte-function' is a type from the bytecode
1453
;;; interpreter.
1454
;;;
1455
(defun hairy-byte-function-arglist (fn)
1456
(let ((counter -1))
1457
(flet ((next-arg () (make-arg-symbol (incf counter))))
1458
(with-struct (c::hairy-byte-function- min-args max-args rest-arg-p
1459
keywords-p keywords) fn
1460
(let ((arglist '())
1461
(optional (- max-args min-args)))
1462
;; XXX isn't there a better way to write this?
1463
;; (Looks fine to me. -luke)
1464
(dotimes (i min-args)
1465
(push (next-arg) arglist))
1466
(when (plusp optional)
1467
(push '&optional arglist)
1468
(dotimes (i optional)
1469
(push (next-arg) arglist)))
1470
(when rest-arg-p
1471
(push '&rest arglist)
1472
(push (next-arg) arglist))
1473
(when keywords-p
1474
(push '&key arglist)
1475
(loop for (key _ __) in keywords
1476
do (push key arglist))
1477
(when (eq keywords-p :allow-others)
1478
(push '&allow-other-keys arglist)))
1479
(nreverse arglist))))))
1480
1481
1482
;;;; Miscellaneous.
1483
1484
(defimplementation macroexpand-all (form)
1485
(walker:macroexpand-all form))
1486
1487
(defimplementation compiler-macroexpand-1 (form &optional env)
1488
(ext:compiler-macroexpand-1 form env))
1489
1490
(defimplementation compiler-macroexpand (form &optional env)
1491
(ext:compiler-macroexpand form env))
1492
1493
(defimplementation set-default-directory (directory)
1494
(setf (ext:default-directory) (namestring directory))
1495
;; Setting *default-pathname-defaults* to an absolute directory
1496
;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1497
(setf *default-pathname-defaults* (pathname (ext:default-directory)))
1498
(default-directory))
1499
1500
(defimplementation default-directory ()
1501
(namestring (ext:default-directory)))
1502
1503
(defimplementation getpid ()
1504
(unix:unix-getpid))
1505
1506
(defimplementation lisp-implementation-type-name ()
1507
"cmucl")
1508
1509
(defimplementation quit-lisp ()
1510
(ext::quit))
1511
1512
;;; source-path-{stream,file,string,etc}-position moved into
1513
;;; swank-source-path-parser
1514
1515
1516
;;;; Debugging
1517
1518
(defvar *sldb-stack-top*)
1519
1520
(defimplementation call-with-debugging-environment (debugger-loop-fn)
1521
(unix:unix-sigsetmask 0)
1522
(let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1523
(debug:*stack-top-hint* nil)
1524
(kernel:*current-level* 0))
1525
(handler-bind ((di::unhandled-condition
1526
(lambda (condition)
1527
(error (make-condition
1528
'sldb-condition
1529
:original-condition condition)))))
1530
(unwind-protect
1531
(progn
1532
#+(or)(sys:scrub-control-stack)
1533
(funcall debugger-loop-fn))
1534
#+(or)(sys:scrub-control-stack)
1535
))))
1536
1537
(defun frame-down (frame)
1538
(handler-case (di:frame-down frame)
1539
(di:no-debug-info () nil)))
1540
1541
(defun nth-frame (index)
1542
(do ((frame *sldb-stack-top* (frame-down frame))
1543
(i index (1- i)))
1544
((zerop i) frame)))
1545
1546
(defimplementation compute-backtrace (start end)
1547
(let ((end (or end most-positive-fixnum)))
1548
(loop for f = (nth-frame start) then (frame-down f)
1549
for i from start below end
1550
while f collect f)))
1551
1552
(defimplementation print-frame (frame stream)
1553
(let ((*standard-output* stream))
1554
(handler-case
1555
(debug::print-frame-call frame :verbosity 1 :number nil)
1556
(error (e)
1557
(ignore-errors (princ e stream))))))
1558
1559
(defimplementation frame-source-location (index)
1560
(let ((frame (nth-frame index)))
1561
(cond ((foreign-frame-p frame) (foreign-frame-source-location frame))
1562
((code-location-source-location (di:frame-code-location frame))))))
1563
1564
(defimplementation eval-in-frame (form index)
1565
(di:eval-in-frame (nth-frame index) form))
1566
1567
(defun frame-debug-vars (frame)
1568
"Return a vector of debug-variables in frame."
1569
(let ((loc (di:frame-code-location frame)))
1570
(remove-if
1571
(lambda (v)
1572
(not (eq (di:debug-variable-validity v loc) :valid)))
1573
(di::debug-function-debug-variables (di:frame-debug-function frame)))))
1574
1575
(defun debug-var-value (var frame)
1576
(let* ((loc (di:frame-code-location frame))
1577
(validity (di:debug-variable-validity var loc)))
1578
(ecase validity
1579
(:valid (di:debug-variable-value var frame))
1580
((:invalid :unknown) (make-symbol (string validity))))))
1581
1582
(defimplementation frame-locals (index)
1583
(let ((frame (nth-frame index)))
1584
(loop for v across (frame-debug-vars frame)
1585
collect (list :name (di:debug-variable-symbol v)
1586
:id (di:debug-variable-id v)
1587
:value (debug-var-value v frame)))))
1588
1589
(defimplementation frame-var-value (frame var)
1590
(let* ((frame (nth-frame frame))
1591
(dvar (aref (frame-debug-vars frame) var)))
1592
(debug-var-value dvar frame)))
1593
1594
(defimplementation frame-catch-tags (index)
1595
(mapcar #'car (di:frame-catches (nth-frame index))))
1596
1597
(defimplementation return-from-frame (index form)
1598
(let ((sym (find-symbol (string 'find-debug-tag-for-frame)
1599
:debug-internals)))
1600
(if sym
1601
(let* ((frame (nth-frame index))
1602
(probe (funcall sym frame)))
1603
(cond (probe (throw (car probe) (eval-in-frame form index)))
1604
(t (format nil "Cannot return from frame: ~S" frame))))
1605
"return-from-frame is not implemented in this version of CMUCL.")))
1606
1607
(defimplementation activate-stepping (frame)
1608
(set-step-breakpoints (nth-frame frame)))
1609
1610
(defimplementation sldb-break-on-return (frame)
1611
(break-on-return (nth-frame frame)))
1612
1613
;;; We set the breakpoint in the caller which might be a bit confusing.
1614
;;;
1615
(defun break-on-return (frame)
1616
(let* ((caller (di:frame-down frame))
1617
(cl (di:frame-code-location caller)))
1618
(flet ((hook (frame bp)
1619
(when (frame-pointer= frame caller)
1620
(di:delete-breakpoint bp)
1621
(signal-breakpoint bp frame))))
1622
(let* ((info (ecase (di:code-location-kind cl)
1623
((:single-value-return :unknown-return) nil)
1624
(:known-return (debug-function-returns
1625
(di:frame-debug-function frame)))))
1626
(bp (di:make-breakpoint #'hook cl :kind :code-location
1627
:info info)))
1628
(di:activate-breakpoint bp)
1629
`(:ok ,(format nil "Set breakpoint in ~A" caller))))))
1630
1631
(defun frame-pointer= (frame1 frame2)
1632
"Return true if the frame pointers of FRAME1 and FRAME2 are the same."
1633
(sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
1634
1635
;;; The PC in escaped frames at a single-return-value point is
1636
;;; actually vm:single-value-return-byte-offset bytes after the
1637
;;; position given in the debug info. Here we try to recognize such
1638
;;; cases.
1639
;;;
1640
(defun next-code-locations (frame code-location)
1641
"Like `debug::next-code-locations' but be careful in escaped frames."
1642
(let ((next (debug::next-code-locations code-location)))
1643
(flet ((adjust-pc ()
1644
(let ((cl (di::copy-compiled-code-location code-location)))
1645
(incf (di::compiled-code-location-pc cl)
1646
vm:single-value-return-byte-offset)
1647
cl)))
1648
(cond ((and (di::compiled-frame-escaped frame)
1649
(eq (di:code-location-kind code-location)
1650
:single-value-return)
1651
(= (length next) 1)
1652
(di:code-location= (car next) (adjust-pc)))
1653
(debug::next-code-locations (car next)))
1654
(t
1655
next)))))
1656
1657
(defun set-step-breakpoints (frame)
1658
(let ((cl (di:frame-code-location frame)))
1659
(when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
1660
(error "Cannot step in elsewhere code"))
1661
(let* ((debug::*bad-code-location-types*
1662
(remove :call-site debug::*bad-code-location-types*))
1663
(next (next-code-locations frame cl)))
1664
(cond (next
1665
(let ((steppoints '()))
1666
(flet ((hook (bp-frame bp)
1667
(signal-breakpoint bp bp-frame)
1668
(mapc #'di:delete-breakpoint steppoints)))
1669
(dolist (code-location next)
1670
(let ((bp (di:make-breakpoint #'hook code-location
1671
:kind :code-location)))
1672
(di:activate-breakpoint bp)
1673
(push bp steppoints))))))
1674
(t
1675
(break-on-return frame))))))
1676
1677
1678
;; XXX the return values at return breakpoints should be passed to the
1679
;; user hooks. debug-int.lisp should be changed to do this cleanly.
1680
1681
;;; The sigcontext and the PC for a breakpoint invocation are not
1682
;;; passed to user hook functions, but we need them to extract return
1683
;;; values. So we advice di::handle-breakpoint and bind the values to
1684
;;; special variables.
1685
;;;
1686
(defvar *breakpoint-sigcontext*)
1687
(defvar *breakpoint-pc*)
1688
1689
;; XXX don't break old versions without fwrappers. Remove this one day.
1690
#+#.(cl:if (cl:find-package :fwrappers) '(and) '(or))
1691
(progn
1692
(fwrappers:define-fwrapper bind-breakpoint-sigcontext (offset c sigcontext)
1693
(let ((*breakpoint-sigcontext* sigcontext)
1694
(*breakpoint-pc* offset))
1695
(fwrappers:call-next-function)))
1696
(fwrappers:set-fwrappers 'di::handle-breakpoint '())
1697
(fwrappers:fwrap 'di::handle-breakpoint #'bind-breakpoint-sigcontext))
1698
1699
(defun sigcontext-object (sc index)
1700
"Extract the lisp object in sigcontext SC at offset INDEX."
1701
(kernel:make-lisp-obj (vm:sigcontext-register sc index)))
1702
1703
(defun known-return-point-values (sigcontext sc-offsets)
1704
(let ((fp (system:int-sap (vm:sigcontext-register sigcontext
1705
vm::cfp-offset))))
1706
(system:without-gcing
1707
(loop for sc-offset across sc-offsets
1708
collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
1709
1710
;;; CMUCL returns the first few values in registers and the rest on
1711
;;; the stack. In the multiple value case, the number of values is
1712
;;; stored in a dedicated register. The values of the registers can be
1713
;;; accessed in the sigcontext for the breakpoint. There are 3 kinds
1714
;;; of return conventions: :single-value-return, :unknown-return, and
1715
;;; :known-return.
1716
;;;
1717
;;; The :single-value-return convention returns the value in a
1718
;;; register without setting the nargs registers.
1719
;;;
1720
;;; The :unknown-return variant is used for multiple values. A
1721
;;; :unknown-return point consists actually of 2 breakpoints: one for
1722
;;; the single value case and one for the general case. The single
1723
;;; value breakpoint comes vm:single-value-return-byte-offset after
1724
;;; the multiple value breakpoint.
1725
;;;
1726
;;; The :known-return convention is used by local functions.
1727
;;; :known-return is currently not supported because we don't know
1728
;;; where the values are passed.
1729
;;;
1730
(defun breakpoint-values (breakpoint)
1731
"Return the list of return values for a return point."
1732
(flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
1733
(let ((sc (locally (declare (optimize (speed 0)))
1734
(alien:sap-alien *breakpoint-sigcontext* (* unix:sigcontext))))
1735
(cl (di:breakpoint-what breakpoint)))
1736
(ecase (di:code-location-kind cl)
1737
(:single-value-return
1738
(list (1st sc)))
1739
(:known-return
1740
(let ((info (di:breakpoint-info breakpoint)))
1741
(if (vectorp info)
1742
(known-return-point-values sc info)
1743
(progn
1744
;;(break)
1745
(list "<<known-return convention not supported>>" info)))))
1746
(:unknown-return
1747
(let ((mv-return-pc (di::compiled-code-location-pc cl)))
1748
(if (= mv-return-pc *breakpoint-pc*)
1749
(mv-function-end-breakpoint-values sc)
1750
(list (1st sc)))))))))
1751
1752
;; XXX: di::get-function-end-breakpoint-values takes 2 arguments in
1753
;; newer versions of CMUCL (after ~March 2005).
1754
(defun mv-function-end-breakpoint-values (sigcontext)
1755
(let ((sym (find-symbol "FUNCTION-END-BREAKPOINT-VALUES/STANDARD" :di)))
1756
(cond (sym (funcall sym sigcontext))
1757
(t (funcall 'di::get-function-end-breakpoint-values sigcontext)))))
1758
1759
(defun debug-function-returns (debug-fun)
1760
"Return the return style of DEBUG-FUN."
1761
(let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
1762
(c::compiled-debug-function-returns cdfun)))
1763
1764
(define-condition breakpoint (simple-condition)
1765
((message :initarg :message :reader breakpoint.message)
1766
(values :initarg :values :reader breakpoint.values))
1767
(:report (lambda (c stream) (princ (breakpoint.message c) stream))))
1768
1769
(defimplementation condition-extras (condition)
1770
(typecase condition
1771
(breakpoint
1772
;; pop up the source buffer
1773
`((:show-frame-source 0)))
1774
(t '())))
1775
1776
(defun signal-breakpoint (breakpoint frame)
1777
"Signal a breakpoint condition for BREAKPOINT in FRAME.
1778
Try to create a informative message."
1779
(flet ((brk (values fstring &rest args)
1780
(let ((msg (apply #'format nil fstring args))
1781
(debug:*stack-top-hint* frame))
1782
(break 'breakpoint :message msg :values values))))
1783
(with-struct (di::breakpoint- kind what) breakpoint
1784
(case kind
1785
(:code-location
1786
(case (di:code-location-kind what)
1787
((:single-value-return :known-return :unknown-return)
1788
(let ((values (breakpoint-values breakpoint)))
1789
(brk values "Return value: ~{~S ~}" values)))
1790
(t
1791
#+(or)
1792
(when (eq (di:code-location-kind what) :call-site)
1793
(call-site-function breakpoint frame))
1794
(brk nil "Breakpoint: ~S ~S"
1795
(di:code-location-kind what)
1796
(di::compiled-code-location-pc what)))))
1797
(:function-start
1798
(brk nil "Function start breakpoint"))
1799
(t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
1800
1801
(defimplementation sldb-break-at-start (fname)
1802
(let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1803
(cond ((not debug-fun)
1804
`(:error ,(format nil "~S has no debug-function" fname)))
1805
(t
1806
(flet ((hook (frame bp &optional args cookie)
1807
(declare (ignore args cookie))
1808
(signal-breakpoint bp frame)))
1809
(let ((bp (di:make-breakpoint #'hook debug-fun
1810
:kind :function-start)))
1811
(di:activate-breakpoint bp)
1812
`(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1813
1814
(defun frame-cfp (frame)
1815
"Return the Control-Stack-Frame-Pointer for FRAME."
1816
(etypecase frame
1817
(di::compiled-frame (di::frame-pointer frame))
1818
((or di::interpreted-frame null) -1)))
1819
1820
(defun frame-ip (frame)
1821
"Return the (absolute) instruction pointer and the relative pc of FRAME."
1822
(if (not frame)
1823
-1
1824
(let ((debug-fun (di::frame-debug-function frame)))
1825
(etypecase debug-fun
1826
(di::compiled-debug-function
1827
(let* ((code-loc (di:frame-code-location frame))
1828
(component (di::compiled-debug-function-component debug-fun))
1829
(pc (di::compiled-code-location-pc code-loc))
1830
(ip (sys:without-gcing
1831
(sys:sap-int
1832
(sys:sap+ (kernel:code-instructions component) pc)))))
1833
(values ip pc)))
1834
(di::interpreted-debug-function -1)
1835
(di::bogus-debug-function
1836
#-x86 -1
1837
#+x86
1838
(let ((fp (di::frame-pointer (di:frame-up frame))))
1839
(multiple-value-bind (ra ofp) (di::x86-call-context fp)
1840
(declare (ignore ofp))
1841
(values ra 0))))))))
1842
1843
(defun frame-registers (frame)
1844
"Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1845
(let* ((cfp (frame-cfp frame))
1846
(csp (frame-cfp (di::frame-up frame)))
1847
(ip (frame-ip frame))
1848
(ocfp (frame-cfp (di::frame-down frame)))
1849
(lra (frame-ip (di::frame-down frame))))
1850
(values csp cfp ip ocfp lra)))
1851
1852
(defun print-frame-registers (frame-number)
1853
(let ((frame (di::frame-real-frame (nth-frame frame-number))))
1854
(flet ((fixnum (p) (etypecase p
1855
(integer p)
1856
(sys:system-area-pointer (sys:sap-int p)))))
1857
(apply #'format t "~
1858
~8X Stack Pointer
1859
~8X Frame Pointer
1860
~8X Instruction Pointer
1861
~8X Saved Frame Pointer
1862
~8X Saved Instruction Pointer~%" (mapcar #'fixnum
1863
(multiple-value-list (frame-registers frame)))))))
1864
1865
(defvar *gdb-program-name* "/usr/bin/gdb")
1866
1867
(defimplementation disassemble-frame (frame-number)
1868
(print-frame-registers frame-number)
1869
(terpri)
1870
(let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1871
(debug-fun (di::frame-debug-function frame)))
1872
(etypecase debug-fun
1873
(di::compiled-debug-function
1874
(let* ((component (di::compiled-debug-function-component debug-fun))
1875
(fun (di:debug-function-function debug-fun)))
1876
(if fun
1877
(disassemble fun)
1878
(disassem:disassemble-code-component component))))
1879
(di::bogus-debug-function
1880
(cond ((probe-file *gdb-program-name*)
1881
(let ((ip (sys:sap-int (frame-ip frame))))
1882
(princ (gdb-command "disas 0x~x" ip))))
1883
(t
1884
(format t "~%[Disassembling bogus frames not implemented]")))))))
1885
1886
(defmacro with-temporary-file ((stream filename) &body body)
1887
`(call/temporary-file (lambda (,stream ,filename) . ,body)))
1888
1889
(defun call/temporary-file (fun)
1890
(let ((name (system::pick-temporary-file-name)))
1891
(unwind-protect
1892
(with-open-file (stream name :direction :output :if-exists :supersede)
1893
(funcall fun stream name))
1894
(delete-file name))))
1895
1896
(defun gdb-command (format-string &rest args)
1897
(let ((str (gdb-exec (format nil
1898
"interpreter-exec mi2 \"attach ~d\"~%~
1899
interpreter-exec console ~s~%detach"
1900
(getpid)
1901
(apply #'format nil format-string args))))
1902
(prompt (format nil "~%^done~%(gdb) ~%")))
1903
(subseq str (+ (search prompt str) (length prompt)))))
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-from-string (read-word)))
1945
(pathname
1946
(or (probe-file file)
1947
(probe-file (format nil "target:lisp/~a" file))
1948
file)))
1949
(make-location (list :file (unix-truename pathname))
1950
(list :line (parse-integer line))))))
1951
(t
1952
`(: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
(defimplementation eval-context (obj)
2146
(cond ((typep (class-of obj) 'structure-class)
2147
(let* ((dd (kernel:layout-info (kernel:layout-of obj)))
2148
(slots (kernel:dd-slots dd)))
2149
(list* (cons '*package*
2150
(symbol-package (if slots
2151
(kernel:dsd-name (car slots))
2152
(kernel:dd-name dd))))
2153
(loop for slot in slots collect
2154
(cons (kernel:dsd-name slot)
2155
(funcall (kernel:dsd-accessor slot) obj))))))))
2156
2157
2158
;;;; Profiling
2159
(defimplementation profile (fname)
2160
(eval `(profile:profile ,fname)))
2161
2162
(defimplementation unprofile (fname)
2163
(eval `(profile:unprofile ,fname)))
2164
2165
(defimplementation unprofile-all ()
2166
(eval `(profile:unprofile))
2167
"All functions unprofiled.")
2168
2169
(defimplementation profile-report ()
2170
(eval `(profile:report-time)))
2171
2172
(defimplementation profile-reset ()
2173
(eval `(profile:reset-time))
2174
"Reset profiling counters.")
2175
2176
(defimplementation profiled-functions ()
2177
profile:*timed-functions*)
2178
2179
(defimplementation profile-package (package callers methods)
2180
(profile:profile-all :package package
2181
:callers-p callers
2182
#-cmu18e :methods #-cmu18e methods))
2183
2184
2185
;;;; Multiprocessing
2186
2187
#+mp
2188
(progn
2189
(defimplementation initialize-multiprocessing (continuation)
2190
(mp::init-multi-processing)
2191
(mp:make-process continuation :name "swank")
2192
;; Threads magic: this never returns! But top-level becomes
2193
;; available again.
2194
(unless mp::*idle-process*
2195
(mp::startup-idle-and-top-level-loops)))
2196
2197
(defimplementation spawn (fn &key name)
2198
(mp:make-process fn :name (or name "Anonymous")))
2199
2200
(defvar *thread-id-counter* 0)
2201
2202
(defimplementation thread-id (thread)
2203
(or (getf (mp:process-property-list thread) 'id)
2204
(setf (getf (mp:process-property-list thread) 'id)
2205
(incf *thread-id-counter*))))
2206
2207
(defimplementation find-thread (id)
2208
(find id (all-threads)
2209
:key (lambda (p) (getf (mp:process-property-list p) 'id))))
2210
2211
(defimplementation thread-name (thread)
2212
(mp:process-name thread))
2213
2214
(defimplementation thread-status (thread)
2215
(mp:process-whostate thread))
2216
2217
(defimplementation current-thread ()
2218
mp:*current-process*)
2219
2220
(defimplementation all-threads ()
2221
(copy-list mp:*all-processes*))
2222
2223
(defimplementation interrupt-thread (thread fn)
2224
(mp:process-interrupt thread fn))
2225
2226
(defimplementation kill-thread (thread)
2227
(mp:destroy-process thread))
2228
2229
(defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
2230
2231
(defstruct (mailbox (:conc-name mailbox.))
2232
(mutex (mp:make-lock "process mailbox"))
2233
(queue '() :type list))
2234
2235
(defun mailbox (thread)
2236
"Return THREAD's mailbox."
2237
(mp:with-lock-held (*mailbox-lock*)
2238
(or (getf (mp:process-property-list thread) 'mailbox)
2239
(setf (getf (mp:process-property-list thread) 'mailbox)
2240
(make-mailbox)))))
2241
2242
(defimplementation send (thread message)
2243
(check-slime-interrupts)
2244
(let* ((mbox (mailbox thread)))
2245
(mp:with-lock-held ((mailbox.mutex mbox))
2246
(setf (mailbox.queue mbox)
2247
(nconc (mailbox.queue mbox) (list message))))))
2248
2249
(defimplementation receive-if (test &optional timeout)
2250
(let ((mbox (mailbox mp:*current-process*)))
2251
(assert (or (not timeout) (eq timeout t)))
2252
(loop
2253
(check-slime-interrupts)
2254
(mp:with-lock-held ((mailbox.mutex mbox))
2255
(let* ((q (mailbox.queue mbox))
2256
(tail (member-if test q)))
2257
(when tail
2258
(setf (mailbox.queue mbox)
2259
(nconc (ldiff q tail) (cdr tail)))
2260
(return (car tail)))))
2261
(when (eq timeout t) (return (values nil t)))
2262
(mp:process-wait-with-timeout
2263
"receive-if" 0.5
2264
(lambda () (some test (mailbox.queue mbox)))))))
2265
2266
2267
) ;; #+mp
2268
2269
2270
2271
;;;; GC hooks
2272
;;;
2273
;;; Display GC messages in the echo area to avoid cluttering the
2274
;;; normal output.
2275
;;;
2276
2277
;; this should probably not be here, but where else?
2278
(defun background-message (message)
2279
(funcall (find-symbol (string :background-message) :swank)
2280
message))
2281
2282
(defun print-bytes (nbytes &optional stream)
2283
"Print the number NBYTES to STREAM in KB, MB, or GB units."
2284
(let ((names '((0 bytes) (10 kb) (20 mb) (30 gb) (40 tb) (50 eb))))
2285
(multiple-value-bind (power name)
2286
(loop for ((p1 n1) (p2 n2)) on names
2287
while n2 do
2288
(when (<= (expt 2 p1) nbytes (1- (expt 2 p2)))
2289
(return (values p1 n1))))
2290
(cond (name
2291
(format stream "~,1F ~A" (/ nbytes (expt 2 power)) name))
2292
(t
2293
(format stream "~:D bytes" nbytes))))))
2294
2295
(defconstant gc-generations 6)
2296
2297
#+gencgc
2298
(defun generation-stats ()
2299
"Return a string describing the size distribution among the generations."
2300
(let* ((alloc (loop for i below gc-generations
2301
collect (lisp::gencgc-stats i)))
2302
(sum (coerce (reduce #'+ alloc) 'float)))
2303
(format nil "~{~3F~^/~}"
2304
(mapcar (lambda (size) (/ size sum))
2305
alloc))))
2306
2307
(defvar *gc-start-time* 0)
2308
2309
(defun pre-gc-hook (bytes-in-use)
2310
(setq *gc-start-time* (get-internal-real-time))
2311
(let ((msg (format nil "[Commencing GC with ~A in use.]"
2312
(print-bytes bytes-in-use))))
2313
(background-message msg)))
2314
2315
(defun post-gc-hook (bytes-retained bytes-freed trigger)
2316
(declare (ignore trigger))
2317
(let* ((seconds (/ (- (get-internal-real-time) *gc-start-time*)
2318
internal-time-units-per-second))
2319
(msg (format nil "[GC done. ~A freed ~A retained ~A ~4F sec]"
2320
(print-bytes bytes-freed)
2321
(print-bytes bytes-retained)
2322
#+gencgc(generation-stats)
2323
#-gencgc""
2324
seconds)))
2325
(background-message msg)))
2326
2327
(defun install-gc-hooks ()
2328
(setq ext:*gc-notify-before* #'pre-gc-hook)
2329
(setq ext:*gc-notify-after* #'post-gc-hook))
2330
2331
(defun remove-gc-hooks ()
2332
(setq ext:*gc-notify-before* #'lisp::default-gc-notify-before)
2333
(setq ext:*gc-notify-after* #'lisp::default-gc-notify-after))
2334
2335
(defvar *install-gc-hooks* t
2336
"If non-nil install GC hooks")
2337
2338
(defimplementation emacs-connected ()
2339
(when *install-gc-hooks*
2340
(install-gc-hooks)))
2341
2342
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2343
;;Trace implementations
2344
;;In CMUCL, we have:
2345
;; (trace <name>)
2346
;; (trace (method <name> <qualifier>? (<specializer>+)))
2347
;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
2348
;; <name> can be a normal name or a (setf name)
2349
2350
(defun tracedp (spec)
2351
(member spec (eval '(trace)) :test #'equal))
2352
2353
(defun toggle-trace-aux (spec &rest options)
2354
(cond ((tracedp spec)
2355
(eval `(untrace ,spec))
2356
(format nil "~S is now untraced." spec))
2357
(t
2358
(eval `(trace ,spec ,@options))
2359
(format nil "~S is now traced." spec))))
2360
2361
(defimplementation toggle-trace (spec)
2362
(ecase (car spec)
2363
((setf)
2364
(toggle-trace-aux spec))
2365
((:defgeneric)
2366
(let ((name (second spec)))
2367
(toggle-trace-aux name :methods name)))
2368
((:defmethod)
2369
(cond ((fboundp `(method ,@(cdr spec)))
2370
(toggle-trace-aux `(method ,(cdr spec))))
2371
;; Man, is this ugly
2372
((fboundp `(pcl::fast-method ,@(cdr spec)))
2373
(toggle-trace-aux `(pcl::fast-method ,@(cdr spec))))
2374
(t
2375
(error 'undefined-function :name (cdr spec)))))
2376
((:call)
2377
(destructuring-bind (caller callee) (cdr spec)
2378
(toggle-trace-aux (process-fspec callee)
2379
:wherein (list (process-fspec caller)))))
2380
;; doesn't work properly
2381
;; ((:labels :flet) (toggle-trace-aux (process-fspec spec)))
2382
))
2383
2384
(defun process-fspec (fspec)
2385
(cond ((consp fspec)
2386
(ecase (first fspec)
2387
((:defun :defgeneric) (second fspec))
2388
((:defmethod)
2389
`(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
2390
((:labels) `(labels ,(third fspec) ,(process-fspec (second fspec))))
2391
((:flet) `(flet ,(third fspec) ,(process-fspec (second fspec))))))
2392
(t
2393
fspec)))
2394
2395
;;; Weak datastructures
2396
2397
(defimplementation make-weak-key-hash-table (&rest args)
2398
(apply #'make-hash-table :weak-p t args))
2399
2400
2401
;;; Save image
2402
2403
(defimplementation save-image (filename &optional restart-function)
2404
(multiple-value-bind (pid error) (unix:unix-fork)
2405
(when (not pid) (error "fork: ~A" (unix:get-unix-error-msg error)))
2406
(cond ((= pid 0)
2407
(apply #'ext:save-lisp
2408
filename
2409
(if restart-function
2410
`(:init-function ,restart-function))))
2411
(t
2412
(let ((status (waitpid pid)))
2413
(destructuring-bind (&key exited? status &allow-other-keys) status
2414
(assert (and exited? (equal status 0)) ()
2415
"Invalid exit status: ~a" status)))))))
2416
2417
(defun waitpid (pid)
2418
(alien:with-alien ((status c-call:int))
2419
(let ((code (alien:alien-funcall
2420
(alien:extern-alien
2421
waitpid (alien:function c-call:int c-call:int
2422
(* c-call:int) c-call:int))
2423
pid (alien:addr status) 0)))
2424
(cond ((= code -1) (error "waitpid: ~A" (unix:get-unix-error-msg)))
2425
(t (assert (= code pid))
2426
(decode-wait-status status))))))
2427
2428
(defun decode-wait-status (status)
2429
(let ((output (with-output-to-string (s)
2430
(call-program (list (process-status-program)
2431
(format nil "~d" status))
2432
:output s))))
2433
(read-from-string output)))
2434
2435
(defun call-program (args &key output)
2436
(destructuring-bind (program &rest args) args
2437
(let ((process (ext:run-program program args :output output)))
2438
(when (not program) (error "fork failed"))
2439
(unless (and (eq (ext:process-status process) :exited)
2440
(= (ext:process-exit-code process) 0))
2441
(error "Non-zero exit status")))))
2442
2443
(defvar *process-status-program* nil)
2444
2445
(defun process-status-program ()
2446
(or *process-status-program*
2447
(setq *process-status-program*
2448
(compile-process-status-program))))
2449
2450
(defun compile-process-status-program ()
2451
(let ((infile (system::pick-temporary-file-name
2452
"/tmp/process-status~d~c.c")))
2453
(with-open-file (stream infile :direction :output :if-exists :supersede)
2454
(format stream "
2455
#include <stdio.h>
2456
#include <stdlib.h>
2457
#include <sys/types.h>
2458
#include <sys/wait.h>
2459
#include <assert.h>
2460
2461
#define FLAG(value) (value ? \"t\" : \"nil\")
2462
2463
int main (int argc, char** argv) {
2464
assert (argc == 2);
2465
{
2466
char* endptr = NULL;
2467
char* arg = argv[1];
2468
long int status = strtol (arg, &endptr, 10);
2469
assert (endptr != arg && *endptr == '\\0');
2470
printf (\"(:exited? %s :status %d :signal? %s :signal %d :coredump? %s\"
2471
\" :stopped? %s :stopsig %d)\\n\",
2472
FLAG(WIFEXITED(status)), WEXITSTATUS(status),
2473
FLAG(WIFSIGNALED(status)), WTERMSIG(status),
2474
FLAG(WCOREDUMP(status)),
2475
FLAG(WIFSTOPPED(status)), WSTOPSIG(status));
2476
fflush (NULL);
2477
return 0;
2478
}
2479
}
2480
")
2481
(finish-output stream))
2482
(let* ((outfile (system::pick-temporary-file-name))
2483
(args (list "cc" "-o" outfile infile)))
2484
(warn "Running cc: ~{~a ~}~%" args)
2485
(call-program args :output t)
2486
(delete-file infile)
2487
outfile)))
2488
2489