Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2470 lines
94 KiB

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