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.

1726 regels
66 KiB

4 jaren geleden
  1. ;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
  2. ;;;
  3. ;;; Scieneer Common Lisp code for SLIME.
  4. ;;;
  5. ;;; This code has been placed in the Public Domain. All warranties
  6. ;;; are disclaimed.
  7. ;;;
  8. (defpackage swank/scl
  9. (:use cl swank/backend swank/source-path-parser swank/source-file-cache))
  10. (in-package swank/scl)
  11. ;;; swank-mop
  12. (import-swank-mop-symbols :clos '(:slot-definition-documentation))
  13. (defun swank-mop:slot-definition-documentation (slot)
  14. (documentation slot t))
  15. ;;;; TCP server
  16. ;;;
  17. ;;; SCL only supports the :spawn communication style.
  18. ;;;
  19. (defimplementation preferred-communication-style ()
  20. :spawn)
  21. (defimplementation create-socket (host port &key backlog)
  22. (let ((addr (resolve-hostname host)))
  23. (ext:create-inet-listener port :stream :host addr :reuse-address t
  24. :backlog (or backlog 5))))
  25. (defimplementation local-port (socket)
  26. (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
  27. (defimplementation close-socket (socket)
  28. (ext:close-socket (socket-fd socket)))
  29. (defimplementation accept-connection (socket
  30. &key external-format buffering timeout)
  31. (let ((buffering (or buffering :full))
  32. (fd (socket-fd socket)))
  33. (loop
  34. (let ((ready (sys:wait-until-fd-usable fd :input timeout)))
  35. (unless ready
  36. (error "Timeout accepting connection on socket: ~S~%" socket)))
  37. (let ((new-fd (ignore-errors (ext:accept-tcp-connection fd))))
  38. (when new-fd
  39. (return (make-socket-io-stream new-fd external-format
  40. (ecase buffering
  41. ((t) :full)
  42. ((nil) :none)
  43. (:line :line)))))))))
  44. (defimplementation set-stream-timeout (stream timeout)
  45. (check-type timeout (or null real))
  46. (if (fboundp 'ext::stream-timeout)
  47. (setf (ext::stream-timeout stream) timeout)
  48. (setf (slot-value (slot-value stream 'lisp::stream) 'lisp::timeout)
  49. timeout)))
  50. ;;;;; Sockets
  51. (defun socket-fd (socket)
  52. "Return the file descriptor for the socket represented by 'socket."
  53. (etypecase socket
  54. (fixnum socket)
  55. (stream (sys:fd-stream-fd socket))))
  56. (defun resolve-hostname (hostname)
  57. "Return the IP address of 'hostname as an integer (in host byte-order)."
  58. (let ((hostent (ext:lookup-host-entry hostname)))
  59. (car (ext:host-entry-addr-list hostent))))
  60. (defvar *external-format-to-coding-system*
  61. '((:iso-8859-1
  62. "latin-1" "latin-1-unix" "iso-latin-1-unix"
  63. "iso-8859-1" "iso-8859-1-unix")
  64. (:utf-8 "utf-8" "utf-8-unix")
  65. (:euc-jp "euc-jp" "euc-jp-unix")))
  66. (defimplementation find-external-format (coding-system)
  67. (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
  68. *external-format-to-coding-system*)))
  69. (defun make-socket-io-stream (fd external-format buffering)
  70. "Create a new input/output fd-stream for 'fd."
  71. (cond ((not external-format)
  72. (sys:make-fd-stream fd :input t :output t :buffering buffering
  73. :element-type '(unsigned-byte 8)))
  74. (t
  75. (let* ((stream (sys:make-fd-stream fd :input t :output t
  76. :element-type 'base-char
  77. :buffering buffering
  78. :external-format external-format)))
  79. ;; Ignore character conversion errors. Without this the
  80. ;; communication channel is prone to lockup if a character
  81. ;; conversion error occurs.
  82. (setf (lisp::character-conversion-stream-input-error-value stream)
  83. #\?)
  84. (setf (lisp::character-conversion-stream-output-error-value stream)
  85. #\?)
  86. stream))))
  87. ;;;; Stream handling
  88. (defimplementation gray-package-name ()
  89. '#:ext)
  90. ;;;; Compilation Commands
  91. (defvar *previous-compiler-condition* nil
  92. "Used to detect duplicates.")
  93. (defvar *previous-context* nil
  94. "Previous compiler error context.")
  95. (defvar *buffer-name* nil
  96. "The name of the Emacs buffer we are compiling from.
  97. Nil if we aren't compiling from a buffer.")
  98. (defvar *buffer-start-position* nil)
  99. (defvar *buffer-substring* nil)
  100. (defimplementation call-with-compilation-hooks (function)
  101. (let ((*previous-compiler-condition* nil)
  102. (*previous-context* nil)
  103. (*print-readably* nil))
  104. (handler-bind ((c::compiler-error #'handle-notification-condition)
  105. (c::style-warning #'handle-notification-condition)
  106. (c::warning #'handle-notification-condition))
  107. (funcall function))))
  108. (defimplementation swank-compile-file (input-file output-file
  109. load-p external-format
  110. &key policy)
  111. (declare (ignore policy))
  112. (with-compilation-hooks ()
  113. (let ((*buffer-name* nil)
  114. (ext:*ignore-extra-close-parentheses* nil))
  115. (multiple-value-bind (output-file warnings-p failure-p)
  116. (compile-file input-file
  117. :output-file output-file
  118. :external-format external-format)
  119. (values output-file warnings-p
  120. (or failure-p
  121. (when load-p
  122. ;; Cache the latest source file for definition-finding.
  123. (source-cache-get input-file
  124. (file-write-date input-file))
  125. (not (load output-file)))))))))
  126. (defimplementation swank-compile-string (string &key buffer position filename
  127. policy)
  128. (declare (ignore filename policy))
  129. (with-compilation-hooks ()
  130. (let ((*buffer-name* buffer)
  131. (*buffer-start-position* position)
  132. (*buffer-substring* string))
  133. (with-input-from-string (stream string)
  134. (ext:compile-from-stream
  135. stream
  136. :source-info `(:emacs-buffer ,buffer
  137. :emacs-buffer-offset ,position
  138. :emacs-buffer-string ,string))))))
  139. ;;;;; Trapping notes
  140. ;;;
  141. ;;; We intercept conditions from the compiler and resignal them as
  142. ;;; `swank:compiler-condition's.
  143. (defun handle-notification-condition (condition)
  144. "Handle a condition caused by a compiler warning."
  145. (unless (eq condition *previous-compiler-condition*)
  146. (let ((context (c::find-error-context nil)))
  147. (setq *previous-compiler-condition* condition)
  148. (setq *previous-context* context)
  149. (signal-compiler-condition condition context))))
  150. (defun signal-compiler-condition (condition context)
  151. (signal 'compiler-condition
  152. :original-condition condition
  153. :severity (severity-for-emacs condition)
  154. :message (brief-compiler-message-for-emacs condition)
  155. :source-context (compiler-error-context context)
  156. :location (if (read-error-p condition)
  157. (read-error-location condition)
  158. (compiler-note-location context))))
  159. (defun severity-for-emacs (condition)
  160. "Return the severity of 'condition."
  161. (etypecase condition
  162. ((satisfies read-error-p) :read-error)
  163. (c::compiler-error :error)
  164. (c::style-warning :note)
  165. (c::warning :warning)))
  166. (defun read-error-p (condition)
  167. (eq (type-of condition) 'c::compiler-read-error))
  168. (defun brief-compiler-message-for-emacs (condition)
  169. "Briefly describe a compiler error for Emacs.
  170. When Emacs presents the message it already has the source popped up
  171. and the source form highlighted. This makes much of the information in
  172. the error-context redundant."
  173. (princ-to-string condition))
  174. (defun compiler-error-context (error-context)
  175. "Describe a compiler error for Emacs including context information."
  176. (declare (type (or c::compiler-error-context null) error-context))
  177. (multiple-value-bind (enclosing source)
  178. (if error-context
  179. (values (c::compiler-error-context-enclosing-source error-context)
  180. (c::compiler-error-context-source error-context)))
  181. (if (and enclosing source)
  182. (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]"
  183. enclosing source))))
  184. (defun read-error-location (condition)
  185. (let* ((finfo (car (c::source-info-current-file c::*source-info*)))
  186. (file (c::file-info-name finfo))
  187. (pos (c::compiler-read-error-position condition)))
  188. (cond ((and (eq file :stream) *buffer-name*)
  189. (make-location (list :buffer *buffer-name*)
  190. (list :offset *buffer-start-position* pos)))
  191. ((and (pathnamep file) (not *buffer-name*))
  192. (make-location (list :file (unix-truename file))
  193. (list :position (1+ pos))))
  194. (t (break)))))
  195. (defun compiler-note-location (context)
  196. "Derive the location of a complier message from its context.
  197. Return a `location' record, or (:error <reason>) on failure."
  198. (if (null context)
  199. (note-error-location)
  200. (let ((file (c::compiler-error-context-file-name context))
  201. (source (c::compiler-error-context-original-source context))
  202. (path
  203. (reverse
  204. (c::compiler-error-context-original-source-path context))))
  205. (or (locate-compiler-note file source path)
  206. (note-error-location)))))
  207. (defun note-error-location ()
  208. "Pseudo-location for notes that can't be located."
  209. (list :error "No error location available."))
  210. (defun locate-compiler-note (file source source-path)
  211. (cond ((and (eq file :stream) *buffer-name*)
  212. ;; Compiling from a buffer
  213. (make-location (list :buffer *buffer-name*)
  214. (list :offset *buffer-start-position*
  215. (source-path-string-position
  216. source-path *buffer-substring*))))
  217. ((and (pathnamep file) (null *buffer-name*))
  218. ;; Compiling from a file
  219. (make-location (list :file (unix-truename file))
  220. (list :position (1+ (source-path-file-position
  221. source-path file)))))
  222. ((and (eq file :lisp) (stringp source))
  223. ;; No location known, but we have the source form.
  224. ;; XXX How is this case triggered? -luke (16/May/2004)
  225. ;; This can happen if the compiler needs to expand a macro
  226. ;; but the macro-expander is not yet compiled. Calling the
  227. ;; (interpreted) macro-expander triggers IR1 conversion of
  228. ;; the lambda expression for the expander and invokes the
  229. ;; compiler recursively.
  230. (make-location (list :source-form source)
  231. (list :position 1)))))
  232. (defun unix-truename (pathname)
  233. (ext:unix-namestring (truename pathname)))
  234. ;;; TODO
  235. (defimplementation who-calls (name) nil)
  236. (defimplementation who-references (name) nil)
  237. (defimplementation who-binds (name) nil)
  238. (defimplementation who-sets (name) nil)
  239. (defimplementation who-specializes (symbol) nil)
  240. (defimplementation who-macroexpands (name) nil)
  241. ;;;; Find callers and callees
  242. ;;;
  243. ;;; Find callers and callees by looking at the constant pool of
  244. ;;; compiled code objects. We assume every fdefn object in the
  245. ;;; constant pool corresponds to a call to that function. A better
  246. ;;; strategy would be to use the disassembler to find actual
  247. ;;; call-sites.
  248. (declaim (inline map-code-constants))
  249. (defun map-code-constants (code fn)
  250. "Call 'fn for each constant in 'code's constant pool."
  251. (check-type code kernel:code-component)
  252. (loop for i from vm:code-constants-offset below (kernel:get-header-data code)
  253. do (funcall fn (kernel:code-header-ref code i))))
  254. (defun function-callees (function)
  255. "Return 'function's callees as a list of functions."
  256. (let ((callees '()))
  257. (map-code-constants
  258. (vm::find-code-object function)
  259. (lambda (obj)
  260. (when (kernel:fdefn-p obj)
  261. (push (kernel:fdefn-function obj) callees))))
  262. callees))
  263. (declaim (ext:maybe-inline map-allocated-code-components))
  264. (defun map-allocated-code-components (spaces fn)
  265. "Call FN for each allocated code component in one of 'spaces. FN
  266. receives the object as argument. 'spaces should be a list of the
  267. symbols :dynamic, :static, or :read-only."
  268. (dolist (space spaces)
  269. (declare (inline vm::map-allocated-objects)
  270. (optimize (ext:inhibit-warnings 3)))
  271. (vm::map-allocated-objects
  272. (lambda (obj header size)
  273. (declare (type fixnum size) (ignore size))
  274. (when (= vm:code-header-type header)
  275. (funcall fn obj)))
  276. space)))
  277. (declaim (ext:maybe-inline map-caller-code-components))
  278. (defun map-caller-code-components (function spaces fn)
  279. "Call 'fn for each code component with a fdefn for 'function in its
  280. constant pool."
  281. (let ((function (coerce function 'function)))
  282. (declare (inline map-allocated-code-components))
  283. (map-allocated-code-components
  284. spaces
  285. (lambda (obj)
  286. (map-code-constants
  287. obj
  288. (lambda (constant)
  289. (when (and (kernel:fdefn-p constant)
  290. (eq (kernel:fdefn-function constant)
  291. function))
  292. (funcall fn obj))))))))
  293. (defun function-callers (function &optional (spaces '(:read-only :static
  294. :dynamic)))
  295. "Return 'function's callers. The result is a list of code-objects."
  296. (let ((referrers '()))
  297. (declare (inline map-caller-code-components))
  298. (map-caller-code-components function spaces
  299. (lambda (code) (push code referrers)))
  300. referrers))
  301. (defun debug-info-definitions (debug-info)
  302. "Return the defintions for a debug-info. This should only be used
  303. for code-object without entry points, i.e., byte compiled
  304. code (are theree others?)"
  305. ;; This mess has only been tested with #'ext::skip-whitespace, a
  306. ;; byte-compiled caller of #'read-char .
  307. (check-type debug-info (and (not c::compiled-debug-info) c::debug-info))
  308. (let ((name (c::debug-info-name debug-info))
  309. (source (c::debug-info-source debug-info)))
  310. (destructuring-bind (first) source
  311. (ecase (c::debug-source-from first)
  312. (:file
  313. (list (list name
  314. (make-location
  315. (list :file (unix-truename (c::debug-source-name first)))
  316. (list :function-name (string name))))))))))
  317. (defun valid-function-name-p (name)
  318. (or (symbolp name) (and (consp name)
  319. (eq (car name) 'setf)
  320. (symbolp (cadr name))
  321. (not (cddr name)))))
  322. (defun code-component-entry-points (code)
  323. "Return a list ((name location) ...) of function definitons for
  324. the code omponent 'code."
  325. (let ((names '()))
  326. (do ((f (kernel:%code-entry-points code) (kernel::%function-next f)))
  327. ((not f))
  328. (let ((name (kernel:%function-name f)))
  329. (when (valid-function-name-p name)
  330. (push (list name (function-location f)) names))))
  331. names))
  332. (defimplementation list-callers (symbol)
  333. "Return a list ((name location) ...) of callers."
  334. (let ((components (function-callers symbol))
  335. (xrefs '()))
  336. (dolist (code components)
  337. (let* ((entry (kernel:%code-entry-points code))
  338. (defs (if entry
  339. (code-component-entry-points code)
  340. ;; byte compiled stuff
  341. (debug-info-definitions
  342. (kernel:%code-debug-info code)))))
  343. (setq xrefs (nconc defs xrefs))))
  344. xrefs))
  345. (defimplementation list-callees (symbol)
  346. (let ((fns (function-callees symbol)))
  347. (mapcar (lambda (fn)
  348. (list (kernel:%function-name fn)
  349. (function-location fn)))
  350. fns)))
  351. ;;;; Resolving source locations
  352. ;;;
  353. ;;; Our mission here is to "resolve" references to code locations into
  354. ;;; actual file/buffer names and character positions. The references
  355. ;;; we work from come out of the compiler's statically-generated debug
  356. ;;; information, such as `code-location''s and `debug-source''s. For
  357. ;;; more details, see the "Debugger Programmer's Interface" section of
  358. ;;; the SCL manual.
  359. ;;;
  360. ;;; The first step is usually to find the corresponding "source-path"
  361. ;;; for the location. Once we have the source-path we can pull up the
  362. ;;; source file and `READ' our way through to the right position. The
  363. ;;; main source-code groveling work is done in
  364. ;;; `source-path-parser.lisp'.
  365. (defvar *debug-definition-finding* nil
  366. "When true don't handle errors while looking for definitions.
  367. This is useful when debugging the definition-finding code.")
  368. (defmacro safe-definition-finding (&body body)
  369. "Execute 'body and return the source-location it returns.
  370. If an error occurs and `*debug-definition-finding*' is false, then
  371. return an error pseudo-location.
  372. The second return value is 'nil if no error occurs, otherwise it is the
  373. condition object."
  374. `(flet ((body () ,@body))
  375. (if *debug-definition-finding*
  376. (body)
  377. (handler-case (values (progn ,@body) nil)
  378. (error (c) (values (list :error (princ-to-string c)) c))))))
  379. (defun code-location-source-location (code-location)
  380. "Safe wrapper around `code-location-from-source-location'."
  381. (safe-definition-finding
  382. (source-location-from-code-location code-location)))
  383. (defun source-location-from-code-location (code-location)
  384. "Return the source location for 'code-location."
  385. (let ((debug-fun (di:code-location-debug-function code-location)))
  386. (when (di::bogus-debug-function-p debug-fun)
  387. ;; Those lousy cheapskates! They've put in a bogus debug source
  388. ;; because the code was compiled at a low debug setting.
  389. (error "Bogus debug function: ~A" debug-fun)))
  390. (let* ((debug-source (di:code-location-debug-source code-location))
  391. (from (di:debug-source-from debug-source))
  392. (name (di:debug-source-name debug-source)))
  393. (ecase from
  394. (:file
  395. (location-in-file name code-location debug-source))
  396. (:stream
  397. (location-in-stream code-location debug-source))
  398. (:lisp
  399. ;; The location comes from a form passed to `compile'.
  400. ;; The best we can do is return the form itself for printing.
  401. (make-location
  402. (list :source-form (with-output-to-string (*standard-output*)
  403. (debug::print-code-location-source-form
  404. code-location 100 t)))
  405. (list :position 1))))))
  406. (defun location-in-file (filename code-location debug-source)
  407. "Resolve the source location for 'code-location in 'filename."
  408. (let* ((code-date (di:debug-source-created debug-source))
  409. (source-code (get-source-code filename code-date)))
  410. (with-input-from-string (s source-code)
  411. (make-location (list :file (unix-truename filename))
  412. (list :position (1+ (code-location-stream-position
  413. code-location s)))
  414. `(:snippet ,(read-snippet s))))))
  415. (defun location-in-stream (code-location debug-source)
  416. "Resolve the source location for a 'code-location from a stream.
  417. This only succeeds if the code was compiled from an Emacs buffer."
  418. (unless (debug-source-info-from-emacs-buffer-p debug-source)
  419. (error "The code is compiled from a non-SLIME stream."))
  420. (let* ((info (c::debug-source-info debug-source))
  421. (string (getf info :emacs-buffer-string))
  422. (position (code-location-string-offset
  423. code-location
  424. string)))
  425. (make-location
  426. (list :buffer (getf info :emacs-buffer))
  427. (list :offset (getf info :emacs-buffer-offset) position)
  428. (list :snippet (with-input-from-string (s string)
  429. (file-position s position)
  430. (read-snippet s))))))
  431. ;;;;; Function-name locations
  432. ;;;
  433. (defun debug-info-function-name-location (debug-info)
  434. "Return a function-name source-location for 'debug-info.
  435. Function-name source-locations are a fallback for when precise
  436. positions aren't available."
  437. (with-struct (c::debug-info- (fname name) source) debug-info
  438. (with-struct (c::debug-source- info from name) (car source)
  439. (ecase from
  440. (:file
  441. (make-location (list :file (namestring (truename name)))
  442. (list :function-name (string fname))))
  443. (:stream
  444. (assert (debug-source-info-from-emacs-buffer-p (car source)))
  445. (make-location (list :buffer (getf info :emacs-buffer))
  446. (list :function-name (string fname))))
  447. (:lisp
  448. (make-location (list :source-form (princ-to-string (aref name 0)))
  449. (list :position 1)))))))
  450. (defun debug-source-info-from-emacs-buffer-p (debug-source)
  451. "Does the `info' slot of 'debug-source contain an Emacs buffer location?
  452. This is true for functions that were compiled directly from buffers."
  453. (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
  454. (defun info-from-emacs-buffer-p (info)
  455. (and info
  456. (consp info)
  457. (eq :emacs-buffer (car info))))
  458. ;;;;; Groveling source-code for positions
  459. (defun code-location-stream-position (code-location stream)
  460. "Return the byte offset of 'code-location in 'stream. Extract the
  461. toplevel-form-number and form-number from 'code-location and use that
  462. to find the position of the corresponding form.
  463. Finish with 'stream positioned at the start of the code location."
  464. (let* ((location (debug::maybe-block-start-location code-location))
  465. (tlf-offset (di:code-location-top-level-form-offset location))
  466. (form-number (di:code-location-form-number location)))
  467. (let ((pos (form-number-stream-position tlf-offset form-number stream)))
  468. (file-position stream pos)
  469. pos)))
  470. (defun form-number-stream-position (tlf-number form-number stream)
  471. "Return the starting character position of a form in 'stream.
  472. 'tlf-number is the top-level-form number.
  473. 'form-number is an index into a source-path table for the TLF."
  474. (multiple-value-bind (tlf position-map) (read-source-form tlf-number stream)
  475. (let* ((path-table (di:form-number-translations tlf 0))
  476. (source-path
  477. (if (<= (length path-table) form-number) ; source out of sync?
  478. (list 0) ; should probably signal a condition
  479. (reverse (cdr (aref path-table form-number))))))
  480. (source-path-source-position source-path tlf position-map))))
  481. (defun code-location-string-offset (code-location string)
  482. "Return the byte offset of 'code-location in 'string.
  483. See 'code-location-stream-position."
  484. (with-input-from-string (s string)
  485. (code-location-stream-position code-location s)))
  486. ;;;; Finding definitions
  487. ;;; There are a great many different types of definition for us to
  488. ;;; find. We search for definitions of every kind and return them in a
  489. ;;; list.
  490. (defimplementation find-definitions (name)
  491. (append (function-definitions name)
  492. (setf-definitions name)
  493. (variable-definitions name)
  494. (class-definitions name)
  495. (type-definitions name)
  496. (compiler-macro-definitions name)
  497. (source-transform-definitions name)
  498. (function-info-definitions name)
  499. (ir1-translator-definitions name)))
  500. ;;;;; Functions, macros, generic functions, methods
  501. ;;;
  502. ;;; We make extensive use of the compile-time debug information that
  503. ;;; SCL records, in particular "debug functions" and "code
  504. ;;; locations." Refer to the "Debugger Programmer's Interface" section
  505. ;;; of the SCL manual for more details.
  506. (defun function-definitions (name)
  507. "Return definitions for 'name in the \"function namespace\", i.e.,
  508. regular functions, generic functions, methods and macros.
  509. 'name can any valid function name (e.g, (setf car))."
  510. (let ((macro? (and (symbolp name) (macro-function name)))
  511. (special? (and (symbolp name) (special-operator-p name)))
  512. (function? (and (valid-function-name-p name)
  513. (ext:info :function :definition name)
  514. (if (symbolp name) (fboundp name) t))))
  515. (cond (macro?
  516. (list `((defmacro ,name)
  517. ,(function-location (macro-function name)))))
  518. (special?
  519. (list `((:special-operator ,name)
  520. (:error ,(format nil "Special operator: ~S" name)))))
  521. (function?
  522. (let ((function (fdefinition name)))
  523. (if (genericp function)
  524. (generic-function-definitions name function)
  525. (list (list `(function ,name)
  526. (function-location function)))))))))
  527. ;;;;;; Ordinary (non-generic/macro/special) functions
  528. ;;;
  529. ;;; First we test if FUNCTION is a closure created by defstruct, and
  530. ;;; if so extract the defstruct-description (`dd') from the closure
  531. ;;; and find the constructor for the struct. Defstruct creates a
  532. ;;; defun for the default constructor and we use that as an
  533. ;;; approximation to the source location of the defstruct.
  534. ;;;
  535. ;;; For an ordinary function we return the source location of the
  536. ;;; first code-location we find.
  537. ;;;
  538. (defun function-location (function)
  539. "Return the source location for FUNCTION."
  540. (cond ((struct-closure-p function)
  541. (struct-closure-location function))
  542. ((c::byte-function-or-closure-p function)
  543. (byte-function-location function))
  544. (t
  545. (compiled-function-location function))))
  546. (defun compiled-function-location (function)
  547. "Return the location of a regular compiled function."
  548. (multiple-value-bind (code-location error)
  549. (safe-definition-finding (function-first-code-location function))
  550. (cond (error (list :error (princ-to-string error)))
  551. (t (code-location-source-location code-location)))))
  552. (defun function-first-code-location (function)
  553. "Return the first code-location we can find for 'function."
  554. (and (function-has-debug-function-p function)
  555. (di:debug-function-start-location
  556. (di:function-debug-function function))))
  557. (defun function-has-debug-function-p (function)
  558. (di:function-debug-function function))
  559. (defun function-code-object= (closure function)
  560. (and (eq (vm::find-code-object closure)
  561. (vm::find-code-object function))
  562. (not (eq closure function))))
  563. (defun byte-function-location (fn)
  564. "Return the location of the byte-compiled function 'fn."
  565. (etypecase fn
  566. ((or c::hairy-byte-function c::simple-byte-function)
  567. (let* ((component (c::byte-function-component fn))
  568. (debug-info (kernel:%code-debug-info component)))
  569. (debug-info-function-name-location debug-info)))
  570. (c::byte-closure
  571. (byte-function-location (c::byte-closure-function fn)))))
  572. ;;; Here we deal with structure accessors. Note that `dd' is a
  573. ;;; "defstruct descriptor" structure in SCL. A `dd' describes a
  574. ;;; `defstruct''d structure.
  575. (defun struct-closure-p (function)
  576. "Is 'function a closure created by defstruct?"
  577. (or (function-code-object= function #'kernel::structure-slot-accessor)
  578. (function-code-object= function #'kernel::structure-slot-setter)
  579. (function-code-object= function #'kernel::%defstruct)))
  580. (defun struct-closure-location (function)
  581. "Return the location of the structure that 'function belongs to."
  582. (assert (struct-closure-p function))
  583. (safe-definition-finding
  584. (dd-location (struct-closure-dd function))))
  585. (defun struct-closure-dd (function)
  586. "Return the defstruct-definition (dd) of FUNCTION."
  587. (assert (= (kernel:get-type function) vm:closure-header-type))
  588. (flet ((find-layout (function)
  589. (sys:find-if-in-closure
  590. (lambda (x)
  591. (let ((value (if (di::indirect-value-cell-p x)
  592. (c:value-cell-ref x)
  593. x)))
  594. (when (kernel::layout-p value)
  595. (return-from find-layout value))))
  596. function)))
  597. (kernel:layout-info (find-layout function))))
  598. (defun dd-location (dd)
  599. "Return the location of a `defstruct'."
  600. ;; Find the location in a constructor.
  601. (function-location (struct-constructor dd)))
  602. (defun struct-constructor (dd)
  603. "Return a constructor function from a defstruct definition.
  604. Signal an error if no constructor can be found."
  605. (let ((constructor (or (kernel:dd-default-constructor dd)
  606. (car (kernel::dd-constructors dd)))))
  607. (when (or (null constructor)
  608. (and (consp constructor) (null (car constructor))))
  609. (error "Cannot find structure's constructor: ~S"
  610. (kernel::dd-name dd)))
  611. (coerce (if (consp constructor) (first constructor) constructor)
  612. 'function)))
  613. ;;;;;; Generic functions and methods
  614. (defun generic-function-definitions (name function)
  615. "Return the definitions of a generic function and its methods."
  616. (cons (list `(defgeneric ,name) (gf-location function))
  617. (gf-method-definitions function)))
  618. (defun gf-location (gf)
  619. "Return the location of the generic function GF."
  620. (definition-source-location gf (clos:generic-function-name gf)))
  621. (defun gf-method-definitions (gf)
  622. "Return the locations of all methods of the generic function GF."
  623. (mapcar #'method-definition (clos:generic-function-methods gf)))
  624. (defun method-definition (method)
  625. (list (method-dspec method)
  626. (method-location method)))
  627. (defun method-dspec (method)
  628. "Return a human-readable \"definition specifier\" for METHOD."
  629. (let* ((gf (clos:method-generic-function method))
  630. (name (clos:generic-function-name gf))
  631. (specializers (clos:method-specializers method))
  632. (qualifiers (clos:method-qualifiers method)))
  633. `(method ,name ,@qualifiers ,specializers
  634. #+nil (clos::unparse-specializers specializers))))
  635. ;; XXX maybe special case setters/getters
  636. (defun method-location (method)
  637. (function-location (clos:method-function method)))
  638. (defun genericp (fn)
  639. (typep fn 'generic-function))
  640. ;;;;;; Types and classes
  641. (defun type-definitions (name)
  642. "Return `deftype' locations for type NAME."
  643. (maybe-make-definition (ext:info :type :expander name) 'deftype name))
  644. (defun maybe-make-definition (function kind name)
  645. "If FUNCTION is non-nil then return its definition location."
  646. (if function
  647. (list (list `(,kind ,name) (function-location function)))))
  648. (defun class-definitions (name)
  649. "Return the definition locations for the class called NAME."
  650. (if (symbolp name)
  651. (let ((class (find-class name nil)))
  652. (etypecase class
  653. (null '())
  654. (structure-class
  655. (list (list `(defstruct ,name)
  656. (dd-location (find-dd name)))))
  657. (standard-class
  658. (list (list `(defclass ,name)
  659. (class-location (find-class name)))))
  660. ((or built-in-class
  661. kernel:funcallable-structure-class)
  662. (list (list `(kernel::define-type-class ,name)
  663. `(:error
  664. ,(format nil "No source info for ~A" name)))))))))
  665. (defun class-location (class)
  666. "Return the `defclass' location for CLASS."
  667. (definition-source-location class (class-name class)))
  668. (defun find-dd (name)
  669. "Find the defstruct-definition by the name of its structure-class."
  670. (let ((layout (ext:info :type :compiler-layout name)))
  671. (if layout
  672. (kernel:layout-info layout))))
  673. (defun condition-class-location (class)
  674. (let ((name (class-name class)))
  675. `(:error ,(format nil "No location info for condition: ~A" name))))
  676. (defun make-name-in-file-location (file string)
  677. (multiple-value-bind (filename c)
  678. (ignore-errors
  679. (unix-truename (merge-pathnames (make-pathname :type "lisp")
  680. file)))
  681. (cond (filename (make-location `(:file ,filename)
  682. `(:function-name ,(string string))))
  683. (t (list :error (princ-to-string c))))))
  684. (defun definition-source-location (object name)
  685. `(:error ,(format nil "No source info for: ~A" object)))
  686. (defun setf-definitions (name)
  687. (let ((function (or (ext:info :setf :inverse name)
  688. (ext:info :setf :expander name))))
  689. (if function
  690. (list (list `(setf ,name)
  691. (function-location (coerce function 'function)))))))
  692. (defun variable-location (symbol)
  693. `(:error ,(format nil "No source info for variable ~S" symbol)))
  694. (defun variable-definitions (name)
  695. (if (symbolp name)
  696. (multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
  697. (if recorded-p
  698. (list (list `(variable ,kind ,name)
  699. (variable-location name)))))))
  700. (defun compiler-macro-definitions (symbol)
  701. (maybe-make-definition (compiler-macro-function symbol)
  702. 'define-compiler-macro
  703. symbol))
  704. (defun source-transform-definitions (name)
  705. (maybe-make-definition (ext:info :function :source-transform name)
  706. 'c:def-source-transform
  707. name))
  708. (defun function-info-definitions (name)
  709. (let ((info (ext:info :function :info name)))
  710. (if info
  711. (append (loop for transform in (c::function-info-transforms info)
  712. collect (list `(c:deftransform ,name
  713. ,(c::type-specifier
  714. (c::transform-type transform)))
  715. (function-location (c::transform-function
  716. transform))))
  717. (maybe-make-definition (c::function-info-derive-type info)
  718. 'c::derive-type name)
  719. (maybe-make-definition (c::function-info-optimizer info)
  720. 'c::optimizer name)
  721. (maybe-make-definition (c::function-info-ltn-annotate info)
  722. 'c::ltn-annotate name)
  723. (maybe-make-definition (c::function-info-ir2-convert info)
  724. 'c::ir2-convert name)
  725. (loop for template in (c::function-info-templates info)
  726. collect (list `(c::vop ,(c::template-name template))
  727. (function-location
  728. (c::vop-info-generator-function
  729. template))))))))
  730. (defun ir1-translator-definitions (name)
  731. (maybe-make-definition (ext:info :function :ir1-convert name)
  732. 'c:def-ir1-translator name))
  733. ;;;; Documentation.
  734. (defimplementation describe-symbol-for-emacs (symbol)
  735. (let ((result '()))
  736. (flet ((doc (kind)
  737. (or (documentation symbol kind) :not-documented))
  738. (maybe-push (property value)
  739. (when value
  740. (setf result (list* property value result)))))
  741. (maybe-push
  742. :variable (multiple-value-bind (kind recorded-p)
  743. (ext:info variable kind symbol)
  744. (declare (ignore kind))
  745. (if (or (boundp symbol) recorded-p)
  746. (doc 'variable))))
  747. (when (fboundp symbol)
  748. (maybe-push
  749. (cond ((macro-function symbol) :macro)
  750. ((special-operator-p symbol) :special-operator)
  751. ((genericp (fdefinition symbol)) :generic-function)
  752. (t :function))
  753. (doc 'function)))
  754. (maybe-push
  755. :setf (if (or (ext:info setf inverse symbol)
  756. (ext:info setf expander symbol))
  757. (doc 'setf)))
  758. (maybe-push
  759. :type (if (ext:info type kind symbol)
  760. (doc 'type)))
  761. (maybe-push
  762. :class (if (find-class symbol nil)
  763. (doc 'class)))
  764. (maybe-push
  765. :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
  766. (doc 'alien-type)))
  767. (maybe-push
  768. :alien-struct (if (ext:info alien-type struct symbol)
  769. (doc nil)))
  770. (maybe-push
  771. :alien-union (if (ext:info alien-type union symbol)
  772. (doc nil)))
  773. (maybe-push
  774. :alien-enum (if (ext:info alien-type enum symbol)
  775. (doc nil)))
  776. result)))
  777. (defimplementation describe-definition (symbol namespace)
  778. (describe (ecase namespace
  779. (:variable
  780. symbol)
  781. ((:function :generic-function)
  782. (symbol-function symbol))
  783. (:setf
  784. (or (ext:info setf inverse symbol)
  785. (ext:info setf expander symbol)))
  786. (:type
  787. (kernel:values-specifier-type symbol))
  788. (:class
  789. (find-class symbol))
  790. (:alien-struct
  791. (ext:info :alien-type :struct symbol))
  792. (:alien-union
  793. (ext:info :alien-type :union symbol))
  794. (:alien-enum
  795. (ext:info :alien-type :enum symbol))
  796. (:alien-type
  797. (ecase (ext:info :alien-type :kind symbol)
  798. (:primitive
  799. (let ((alien::*values-type-okay* t))
  800. (funcall (ext:info :alien-type :translator symbol)
  801. (list symbol))))
  802. ((:defined)
  803. (ext:info :alien-type :definition symbol))
  804. (:unknown :unknown))))))
  805. ;;;;; Argument lists
  806. (defimplementation arglist (fun)
  807. (multiple-value-bind (args winp)
  808. (ext:function-arglist fun)
  809. (if winp args :not-available)))
  810. (defimplementation function-name (function)
  811. (cond ((eval:interpreted-function-p function)
  812. (eval:interpreted-function-name function))
  813. ((typep function 'generic-function)
  814. (clos:generic-function-name function))
  815. ((c::byte-function-or-closure-p function)
  816. (c::byte-function-name function))
  817. (t (kernel:%function-name (kernel:%function-self function)))))
  818. ;;; A harder case: an approximate arglist is derived from available
  819. ;;; debugging information.
  820. (defun debug-function-arglist (debug-function)
  821. "Derive the argument list of DEBUG-FUNCTION from debug info."
  822. (let ((args (di::debug-function-lambda-list debug-function))
  823. (required '())
  824. (optional '())
  825. (rest '())
  826. (key '()))
  827. ;; collect the names of debug-vars
  828. (dolist (arg args)
  829. (etypecase arg
  830. (di::debug-variable
  831. (push (di::debug-variable-symbol arg) required))
  832. ((member :deleted)
  833. (push ':deleted required))
  834. (cons
  835. (ecase (car arg)
  836. (:keyword
  837. (push (second arg) key))
  838. (:optional
  839. (push (debug-variable-symbol-or-deleted (second arg)) optional))
  840. (:rest
  841. (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
  842. ;; intersperse lambda keywords as needed
  843. (append (nreverse required)
  844. (if optional (cons '&optional (nreverse optional)))
  845. (if rest (cons '&rest (nreverse rest)))
  846. (if key (cons '&key (nreverse key))))))
  847. (defun debug-variable-symbol-or-deleted (var)
  848. (etypecase var
  849. (di:debug-variable
  850. (di::debug-variable-symbol var))
  851. ((member :deleted)
  852. '#:deleted)))
  853. (defun symbol-debug-function-arglist (fname)
  854. "Return FNAME's debug-function-arglist and %function-arglist.
  855. A utility for debugging DEBUG-FUNCTION-ARGLIST."
  856. (let ((fn (fdefinition fname)))
  857. (values (debug-function-arglist (di::function-debug-function fn))
  858. (kernel:%function-arglist (kernel:%function-self fn)))))
  859. ;;;; Miscellaneous.
  860. (defimplementation macroexpand-all (form &optional env)
  861. (declare (ignore env))
  862. (macroexpand form))
  863. (defimplementation set-default-directory (directory)
  864. (setf (ext:default-directory) (namestring directory))
  865. ;; Setting *default-pathname-defaults* to an absolute directory
  866. ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
  867. (setf *default-pathname-defaults* (pathname (ext:default-directory)))
  868. (default-directory))
  869. (defimplementation default-directory ()
  870. (namestring (ext:default-directory)))
  871. (defimplementation pathname-to-filename (pathname)
  872. (ext:unix-namestring pathname nil))
  873. (defimplementation getpid ()
  874. (unix:unix-getpid))
  875. (defimplementation lisp-implementation-type-name ()
  876. (if (eq ext:*case-mode* :upper) "scl" "scl-lower"))
  877. (defimplementation quit-lisp ()
  878. (ext:quit))
  879. ;;; source-path-{stream,file,string,etc}-position moved into
  880. ;;; source-path-parser
  881. ;;;; Debugging
  882. (defvar *sldb-stack-top*)
  883. (defimplementation call-with-debugging-environment (debugger-loop-fn)
  884. (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
  885. (debug:*stack-top-hint* nil)
  886. (kernel:*current-level* 0))
  887. (handler-bind ((di::unhandled-condition
  888. (lambda (condition)
  889. (error 'sldb-condition
  890. :original-condition condition))))
  891. (funcall debugger-loop-fn))))
  892. (defun frame-down (frame)
  893. (handler-case (di:frame-down frame)
  894. (di:no-debug-info () nil)))
  895. (defun nth-frame (index)
  896. (do ((frame *sldb-stack-top* (frame-down frame))
  897. (i index (1- i)))
  898. ((zerop i) frame)))
  899. (defimplementation compute-backtrace (start end)
  900. (let ((end (or end most-positive-fixnum)))
  901. (loop for f = (nth-frame start) then (frame-down f)
  902. for i from start below end
  903. while f collect f)))
  904. (defimplementation print-frame (frame stream)
  905. (let ((*standard-output* stream))
  906. (handler-case
  907. (debug::print-frame-call frame :verbosity 1 :number nil)
  908. (error (e)
  909. (ignore-errors (princ e stream))))))
  910. (defimplementation frame-source-location (index)
  911. (code-location-source-location (di:frame-code-location (nth-frame index))))
  912. (defimplementation eval-in-frame (form index)
  913. (di:eval-in-frame (nth-frame index) form))
  914. (defun frame-debug-vars (frame)
  915. "Return a vector of debug-variables in frame."
  916. (di::debug-function-debug-variables (di:frame-debug-function frame)))
  917. (defun debug-var-value (var frame location)
  918. (let ((validity (di:debug-variable-validity var location)))
  919. (ecase validity
  920. (:valid (di:debug-variable-value var frame))
  921. ((:invalid :unknown) (make-symbol (string validity))))))
  922. (defimplementation frame-locals (index)
  923. (let* ((frame (nth-frame index))
  924. (loc (di:frame-code-location frame))
  925. (vars (frame-debug-vars frame)))
  926. (loop for v across vars collect
  927. (list :name (di:debug-variable-symbol v)
  928. :id (di:debug-variable-id v)
  929. :value (debug-var-value v frame loc)))))
  930. (defimplementation frame-var-value (frame var)
  931. (let* ((frame (nth-frame frame))
  932. (dvar (aref (frame-debug-vars frame) var)))
  933. (debug-var-value dvar frame (di:frame-code-location frame))))
  934. (defimplementation frame-catch-tags (index)
  935. (mapcar #'car (di:frame-catches (nth-frame index))))
  936. (defimplementation return-from-frame (index form)
  937. (let ((sym (find-symbol (symbol-name '#:find-debug-tag-for-frame)
  938. :debug-internals)))
  939. (if sym
  940. (let* ((frame (nth-frame index))
  941. (probe (funcall sym frame)))
  942. (cond (probe (throw (car probe) (eval-in-frame form index)))
  943. (t (format nil "Cannot return from frame: ~S" frame))))
  944. "return-from-frame is not implemented in this version of SCL.")))
  945. (defimplementation activate-stepping (frame)
  946. (set-step-breakpoints (nth-frame frame)))
  947. (defimplementation sldb-break-on-return (frame)
  948. (break-on-return (nth-frame frame)))
  949. ;;; We set the breakpoint in the caller which might be a bit confusing.
  950. ;;;
  951. (defun break-on-return (frame)
  952. (let* ((caller (di:frame-down frame))
  953. (cl (di:frame-code-location caller)))
  954. (flet ((hook (frame bp)
  955. (when (frame-pointer= frame caller)
  956. (di:delete-breakpoint bp)
  957. (signal-breakpoint bp frame))))
  958. (let* ((info (ecase (di:code-location-kind cl)
  959. ((:single-value-return :unknown-return) nil)
  960. (:known-return (debug-function-returns
  961. (di:frame-debug-function frame)))))
  962. (bp (di:make-breakpoint #'hook cl :kind :code-location
  963. :info info)))
  964. (di:activate-breakpoint bp)
  965. `(:ok ,(format nil "Set breakpoint in ~A" caller))))))
  966. (defun frame-pointer= (frame1 frame2)
  967. "Return true if the frame pointers of FRAME1 and FRAME2 are the same."
  968. (sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
  969. ;;; The PC in escaped frames at a single-return-value point is
  970. ;;; actually vm:single-value-return-byte-offset bytes after the
  971. ;;; position given in the debug info. Here we try to recognize such
  972. ;;; cases.
  973. ;;;
  974. (defun next-code-locations (frame code-location)
  975. "Like `debug::next-code-locations' but be careful in escaped frames."
  976. (let ((next (debug::next-code-locations code-location)))
  977. (flet ((adjust-pc ()
  978. (let ((cl (di::copy-compiled-code-location code-location)))
  979. (incf (di::compiled-code-location-pc cl)
  980. vm:single-value-return-byte-offset)
  981. cl)))
  982. (cond ((and (di::compiled-frame-escaped frame)
  983. (eq (di:code-location-kind code-location)
  984. :single-value-return)
  985. (= (length next) 1)
  986. (di:code-location= (car next) (adjust-pc)))
  987. (debug::next-code-locations (car next)))
  988. (t
  989. next)))))
  990. (defun set-step-breakpoints (frame)
  991. (let ((cl (di:frame-code-location frame)))
  992. (when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
  993. (error "Cannot step in elsewhere code"))
  994. (let* ((debug::*bad-code-location-types*
  995. (remove :call-site debug::*bad-code-location-types*))
  996. (next (next-code-locations frame cl)))
  997. (cond (next
  998. (let ((steppoints '()))
  999. (flet ((hook (bp-frame bp)
  1000. (signal-breakpoint bp bp-frame)
  1001. (mapc #'di:delete-breakpoint steppoints)))
  1002. (dolist (code-location next)
  1003. (let ((bp (di:make-breakpoint #'hook code-location
  1004. :kind :code-location)))
  1005. (di:activate-breakpoint bp)
  1006. (push bp steppoints))))))
  1007. (t
  1008. (break-on-return frame))))))
  1009. ;; XXX the return values at return breakpoints should be passed to the
  1010. ;; user hooks. debug-int.lisp should be changed to do this cleanly.
  1011. ;;; The sigcontext and the PC for a breakpoint invocation are not
  1012. ;;; passed to user hook functions, but we need them to extract return
  1013. ;;; values. So we advice di::handle-breakpoint and bind the values to
  1014. ;;; special variables.
  1015. ;;;
  1016. (defvar *breakpoint-sigcontext*)
  1017. (defvar *breakpoint-pc*)
  1018. (defun sigcontext-object (sc index)
  1019. "Extract the lisp object in sigcontext SC at offset INDEX."
  1020. (kernel:make-lisp-obj (vm:ucontext-register sc index)))
  1021. (defun known-return-point-values (sigcontext sc-offsets)
  1022. (let ((fp (system:int-sap (vm:ucontext-register sigcontext
  1023. vm::cfp-offset))))
  1024. (system:without-gcing
  1025. (loop for sc-offset across sc-offsets
  1026. collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
  1027. ;;; SCL returns the first few values in registers and the rest on
  1028. ;;; the stack. In the multiple value case, the number of values is
  1029. ;;; stored in a dedicated register. The values of the registers can be
  1030. ;;; accessed in the sigcontext for the breakpoint. There are 3 kinds
  1031. ;;; of return conventions: :single-value-return, :unknown-return, and
  1032. ;;; :known-return.
  1033. ;;;
  1034. ;;; The :single-value-return convention returns the value in a
  1035. ;;; register without setting the nargs registers.
  1036. ;;;
  1037. ;;; The :unknown-return variant is used for multiple values. A
  1038. ;;; :unknown-return point consists actually of 2 breakpoints: one for
  1039. ;;; the single value case and one for the general case. The single
  1040. ;;; value breakpoint comes vm:single-value-return-byte-offset after
  1041. ;;; the multiple value breakpoint.
  1042. ;;;
  1043. ;;; The :known-return convention is used by local functions.
  1044. ;;; :known-return is currently not supported because we don't know
  1045. ;;; where the values are passed.
  1046. ;;;
  1047. (defun breakpoint-values (breakpoint)
  1048. "Return the list of return values for a return point."
  1049. (flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
  1050. (let ((sc (locally (declare (optimize (ext:inhibit-warnings 3)))
  1051. (alien:sap-alien *breakpoint-sigcontext* (* unix:ucontext))))
  1052. (cl (di:breakpoint-what breakpoint)))
  1053. (ecase (di:code-location-kind cl)
  1054. (:single-value-return
  1055. (list (1st sc)))
  1056. (:known-return
  1057. (let ((info (di:breakpoint-info breakpoint)))
  1058. (if (vectorp info)
  1059. (known-return-point-values sc info)
  1060. (progn
  1061. ;;(break)
  1062. (list "<<known-return convention not supported>>" info)))))
  1063. (:unknown-return
  1064. (let ((mv-return-pc (di::compiled-code-location-pc cl)))
  1065. (if (= mv-return-pc *breakpoint-pc*)
  1066. (mv-function-end-breakpoint-values sc)
  1067. (list (1st sc)))))))))
  1068. (defun mv-function-end-breakpoint-values (sigcontext)
  1069. (let ((sym (find-symbol
  1070. (symbol-name '#:function-end-breakpoint-values/standard)
  1071. :debug-internals)))
  1072. (cond (sym (funcall sym sigcontext))
  1073. (t (di::get-function-end-breakpoint-values sigcontext)))))
  1074. (defun debug-function-returns (debug-fun)
  1075. "Return the return style of DEBUG-FUN."
  1076. (let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
  1077. (c::compiled-debug-function-returns cdfun)))
  1078. (define-condition breakpoint (simple-condition)
  1079. ((message :initarg :message :reader breakpoint.message)
  1080. (values :initarg :values :reader breakpoint.values))
  1081. (:report (lambda (c stream) (princ (breakpoint.message c) stream))))
  1082. #+nil
  1083. (defimplementation condition-extras ((c breakpoint))
  1084. ;; simply pop up the source buffer
  1085. `((:short-frame-source 0)))
  1086. (defun signal-breakpoint (breakpoint frame)
  1087. "Signal a breakpoint condition for BREAKPOINT in FRAME.
  1088. Try to create a informative message."
  1089. (flet ((brk (values fstring &rest args)
  1090. (let ((msg (apply #'format nil fstring args))
  1091. (debug:*stack-top-hint* frame))
  1092. (break 'breakpoint :message msg :values values))))
  1093. (with-struct (di::breakpoint- kind what) breakpoint
  1094. (case kind
  1095. (:code-location
  1096. (case (di:code-location-kind what)
  1097. ((:single-value-return :known-return :unknown-return)
  1098. (let ((values (breakpoint-values breakpoint)))
  1099. (brk values "Return value: ~{~S ~}" values)))
  1100. (t
  1101. #+(or)
  1102. (when (eq (di:code-location-kind what) :call-site)
  1103. (call-site-function breakpoint frame))
  1104. (brk nil "Breakpoint: ~S ~S"
  1105. (di:code-location-kind what)
  1106. (di::compiled-code-location-pc what)))))
  1107. (:function-start
  1108. (brk nil "Function start breakpoint"))
  1109. (t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
  1110. #+nil
  1111. (defimplementation sldb-break-at-start (fname)
  1112. (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
  1113. (cond ((not debug-fun)
  1114. `(:error ,(format nil "~S has no debug-function" fname)))
  1115. (t
  1116. (flet ((hook (frame bp &optional args cookie)
  1117. (declare (ignore args cookie))
  1118. (signal-breakpoint bp frame)))
  1119. (let ((bp (di:make-breakpoint #'hook debug-fun
  1120. :kind :function-start)))
  1121. (di:activate-breakpoint bp)
  1122. `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
  1123. (defun frame-cfp (frame)
  1124. "Return the Control-Stack-Frame-Pointer for FRAME."
  1125. (etypecase frame
  1126. (di::compiled-frame (di::frame-pointer frame))
  1127. ((or di::interpreted-frame null) -1)))
  1128. (defun frame-ip (frame)
  1129. "Return the (absolute) instruction pointer and the relative pc of FRAME."
  1130. (if (not frame)
  1131. -1
  1132. (let ((debug-fun (di::frame-debug-function frame)))
  1133. (etypecase debug-fun
  1134. (di::compiled-debug-function
  1135. (let* ((code-loc (di:frame-code-location frame))
  1136. (component (di::compiled-debug-function-component debug-fun))
  1137. (pc (di::compiled-code-location-pc code-loc))
  1138. (ip (sys:without-gcing
  1139. (sys:sap-int
  1140. (sys:sap+ (kernel:code-instructions component) pc)))))
  1141. (values ip pc)))
  1142. ((or di::bogus-debug-function di::interpreted-debug-function)
  1143. -1)))))
  1144. (defun frame-registers (frame)
  1145. "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
  1146. (let* ((cfp (frame-cfp frame))
  1147. (csp (frame-cfp (di::frame-up frame)))
  1148. (ip (frame-ip frame))
  1149. (ocfp (frame-cfp (di::frame-down frame)))
  1150. (lra (frame-ip (di::frame-down frame))))
  1151. (values csp cfp ip ocfp lra)))
  1152. (defun print-frame-registers (frame-number)
  1153. (let ((frame (di::frame-real-frame (nth-frame frame-number))))
  1154. (flet ((fixnum (p) (etypecase p
  1155. (integer p)
  1156. (sys:system-area-pointer (sys:sap-int p)))))
  1157. (apply #'format t "~
  1158. CSP = ~X
  1159. CFP = ~X
  1160. IP = ~X
  1161. OCFP = ~X
  1162. LRA = ~X~%" (mapcar #'fixnum
  1163. (multiple-value-list (frame-registers frame)))))))
  1164. (defimplementation disassemble-frame (frame-number)
  1165. "Return a string with the disassembly of frames code."
  1166. (print-frame-registers frame-number)
  1167. (terpri)
  1168. (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
  1169. (debug-fun (di::frame-debug-function frame)))
  1170. (etypecase debug-fun
  1171. (di::compiled-debug-function
  1172. (let* ((component (di::compiled-debug-function-component debug-fun))
  1173. (fun (di:debug-function-function debug-fun)))
  1174. (if fun
  1175. (disassemble fun)
  1176. (disassem:disassemble-code-component component))))
  1177. (di::bogus-debug-function
  1178. (format t "~%[Disassembling bogus frames not implemented]")))))
  1179. ;;;; Inspecting
  1180. (defconstant +lowtag-symbols+
  1181. '(vm:even-fixnum-type
  1182. vm:instance-pointer-type
  1183. vm:other-immediate-0-type
  1184. vm:list-pointer-type
  1185. vm:odd-fixnum-type
  1186. vm:function-pointer-type
  1187. vm:other-immediate-1-type
  1188. vm:other-pointer-type)
  1189. "Names of the constants that specify type tags.
  1190. The `symbol-value' of each element is a type tag.")
  1191. (defconstant +header-type-symbols+
  1192. (labels ((suffixp (suffix string)
  1193. (and (>= (length string) (length suffix))
  1194. (string= string suffix :start1 (- (length string)
  1195. (length suffix)))))
  1196. (header-type-symbol-p (x)
  1197. (and (suffixp (symbol-name '#:-type) (symbol-name x))
  1198. (not (member x +lowtag-symbols+))
  1199. (boundp x)
  1200. (typep (symbol-value x) 'fixnum))))
  1201. (remove-if-not #'header-type-symbol-p
  1202. (append (apropos-list (symbol-name '#:-type) :vm)
  1203. (apropos-list (symbol-name '#:-type) :bignum))))
  1204. "A list of names of the type codes in boxed objects.")
  1205. (defimplementation describe-primitive-type (object)
  1206. (with-output-to-string (*standard-output*)
  1207. (let* ((lowtag (kernel:get-lowtag object))
  1208. (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
  1209. (format t "lowtag: ~A" lowtag-symbol)
  1210. (when (member lowtag (list vm:other-pointer-type
  1211. vm:function-pointer-type
  1212. vm:other-immediate-0-type
  1213. vm:other-immediate-1-type
  1214. ))
  1215. (let* ((type (kernel:get-type object))
  1216. (type-symbol (find type +header-type-symbols+
  1217. :key #'symbol-value)))
  1218. (format t ", type: ~A" type-symbol))))))
  1219. (defmethod emacs-inspect ((o t))
  1220. (cond ((di::indirect-value-cell-p o)
  1221. `("Value: " (:value ,(c:value-cell-ref o))))
  1222. ((alien::alien-value-p o)
  1223. (inspect-alien-value o))
  1224. (t
  1225. (scl-inspect o))))
  1226. (defun scl-inspect (o)
  1227. (destructuring-bind (text labeledp . parts)
  1228. (inspect::describe-parts o)
  1229. (list* (format nil "~A~%" text)
  1230. (if labeledp
  1231. (loop for (label . value) in parts
  1232. append (label-value-line label value))
  1233. (loop for value in parts for i from 0
  1234. append (label-value-line i value))))))
  1235. (defmethod emacs-inspect ((o function))
  1236. (let ((header (kernel:get-type o)))
  1237. (cond ((= header vm:function-header-type)
  1238. (list* (format nil "~A is a function.~%" o)
  1239. (append (label-value-line*
  1240. ("Self" (kernel:%function-self o))
  1241. ("Next" (kernel:%function-next o))
  1242. ("Name" (kernel:%function-name o))
  1243. ("Arglist" (kernel:%function-arglist o))
  1244. ("Type" (kernel:%function-type o))
  1245. ("Code" (kernel:function-code-header o)))
  1246. (list
  1247. (with-output-to-string (s)
  1248. (disassem:disassemble-function o :stream s))))))
  1249. ((= header vm:closure-header-type)
  1250. (list* (format nil "~A is a closure.~%" o)
  1251. (append
  1252. (label-value-line "Function" (kernel:%closure-function o))
  1253. `("Environment:" (:newline))
  1254. (loop for i from 0 below (- (kernel:get-closure-length o)
  1255. (1- vm:closure-info-offset))
  1256. append (label-value-line
  1257. i (kernel:%closure-index-ref o i))))))
  1258. ((eval::interpreted-function-p o)
  1259. (scl-inspect o))
  1260. (t
  1261. (call-next-method)))))
  1262. (defmethod emacs-inspect ((o kernel:code-component))
  1263. (append
  1264. (label-value-line*
  1265. ("code-size" (kernel:%code-code-size o))
  1266. ("entry-points" (kernel:%code-entry-points o))
  1267. ("debug-info" (kernel:%code-debug-info o))
  1268. ("trace-table-offset" (kernel:code-header-ref
  1269. o vm:code-trace-table-offset-slot)))
  1270. `("Constants:" (:newline))
  1271. (loop for i from vm:code-constants-offset
  1272. below (kernel:get-header-data o)
  1273. append (label-value-line i (kernel:code-header-ref o i)))
  1274. `("Code:" (:newline)
  1275. , (with-output-to-string (s)
  1276. (cond ((kernel:%code-debug-info o)
  1277. (disassem:disassemble-code-component o :stream s))
  1278. (t
  1279. (disassem:disassemble-memory
  1280. (disassem::align
  1281. (+ (logandc2 (kernel:get-lisp-obj-address o)
  1282. vm:lowtag-mask)
  1283. (* vm:code-constants-offset vm:word-bytes))
  1284. (ash 1 vm:lowtag-bits))
  1285. (ash (kernel:%code-code-size o) vm:word-shift)
  1286. :stream s)))))))
  1287. (defmethod emacs-inspect ((o kernel:fdefn))
  1288. (label-value-line*
  1289. ("name" (kernel:fdefn-name o))
  1290. ("function" (kernel:fdefn-function o))
  1291. ("raw-addr" (sys:sap-ref-32
  1292. (sys:int-sap (kernel:get-lisp-obj-address o))
  1293. (* vm:fdefn-raw-addr-slot vm:word-bytes)))))
  1294. (defmethod emacs-inspect ((o array))
  1295. (cond ((kernel:array-header-p o)
  1296. (list* (format nil "~A is an array.~%" o)
  1297. (label-value-line*
  1298. (:header (describe-primitive-type o))
  1299. (:rank (array-rank o))
  1300. (:fill-pointer (kernel:%array-fill-pointer o))
  1301. (:fill-pointer-p (kernel:%array-fill-pointer-p o))
  1302. (:elements (kernel:%array-available-elements o))
  1303. (:data (kernel:%array-data-vector o))
  1304. (:displacement (kernel:%array-displacement o))
  1305. (:displaced-p (kernel:%array-displaced-p o))
  1306. (:dimensions (array-dimensions o)))))
  1307. (t
  1308. (list* (format nil "~A is an simple-array.~%" o)
  1309. (label-value-line*
  1310. (:header (describe-primitive-type o))
  1311. (:length (length o)))))))
  1312. (defmethod emacs-inspect ((o simple-vector))
  1313. (list* (format nil "~A is a vector.~%" o)
  1314. (append
  1315. (label-value-line*
  1316. (:header (describe-primitive-type o))
  1317. (:length (c::vector-length o)))
  1318. (unless (eq (array-element-type o) 'nil)
  1319. (loop for i below (length o)
  1320. append (label-value-line i (aref o i)))))))
  1321. (defun inspect-alien-record (alien)
  1322. (with-struct (alien::alien-value- sap type) alien
  1323. (with-struct (alien::alien-record-type- kind name fields) type
  1324. (append
  1325. (label-value-line*
  1326. (:sap sap)
  1327. (:kind kind)
  1328. (:name name))
  1329. (loop for field in fields
  1330. append (let ((slot (alien::alien-record-field-name field)))
  1331. (label-value-line slot (alien:slot alien slot))))))))
  1332. (defun inspect-alien-pointer (alien)
  1333. (with-struct (alien::alien-value- sap type) alien
  1334. (label-value-line*
  1335. (:sap sap)
  1336. (:type type)
  1337. (:to (alien::deref alien)))))
  1338. (defun inspect-alien-value (alien)
  1339. (typecase (alien::alien-value-type alien)
  1340. (alien::alien-record-type (inspect-alien-record alien))
  1341. (alien::alien-pointer-type (inspect-alien-pointer alien))
  1342. (t (scl-inspect alien))))
  1343. ;;;; Profiling
  1344. (defimplementation profile (fname)
  1345. (eval `(profile:profile ,fname)))
  1346. (defimplementation unprofile (fname)
  1347. (eval `(profile:unprofile ,fname)))
  1348. (defimplementation unprofile-all ()
  1349. (eval `(profile:unprofile))
  1350. "All functions unprofiled.")
  1351. (defimplementation profile-report ()
  1352. (eval `(profile:report-time)))
  1353. (defimplementation profile-reset ()
  1354. (eval `(profile:reset-time))
  1355. "Reset profiling counters.")
  1356. (defimplementation profiled-functions ()
  1357. profile:*timed-functions*)
  1358. (defimplementation profile-package (package callers methods)
  1359. (profile:profile-all :package package
  1360. :callers-p callers
  1361. #+nil :methods #+nil methods))
  1362. ;;;; Multiprocessing
  1363. (defimplementation spawn (fn &key name)
  1364. (thread:thread-create fn :name (or name "Anonymous")))
  1365. (defvar *thread-id-counter* 0)
  1366. (defvar *thread-id-counter-lock* (thread:make-lock "Thread ID counter"))
  1367. (defimplementation thread-id (thread)
  1368. (thread:with-lock-held (*thread-id-counter-lock*)
  1369. (or (getf (thread:thread-plist thread) 'id)
  1370. (setf (getf (thread:thread-plist thread) 'id)
  1371. (incf *thread-id-counter*)))))
  1372. (defimplementation find-thread (id)
  1373. (block find-thread
  1374. (thread:map-over-threads
  1375. #'(lambda (thread)
  1376. (when (eql (getf (thread:thread-plist thread) 'id) id)
  1377. (return-from find-thread thread))))))
  1378. (defimplementation thread-name (thread)
  1379. (princ-to-string (thread:thread-name thread)))
  1380. (defimplementation thread-status (thread)
  1381. (let ((dynamic-values (thread::thread-dynamic-values thread)))
  1382. (if (zerop dynamic-values) "Exited" "Running")))
  1383. (defimplementation make-lock (&key name)
  1384. (thread:make-lock name))
  1385. (defimplementation call-with-lock-held (lock function)
  1386. (declare (type function function))
  1387. (thread:with-lock-held (lock) (funcall function)))
  1388. (defimplementation current-thread ()
  1389. thread:*thread*)
  1390. (defimplementation all-threads ()
  1391. (let ((all-threads nil))
  1392. (thread:map-over-threads #'(lambda (thread) (push thread all-threads)))
  1393. all-threads))
  1394. (defimplementation interrupt-thread (thread fn)
  1395. (thread:thread-interrupt thread #'(lambda ()
  1396. (sys:with-interrupts
  1397. (funcall fn)))))
  1398. (defimplementation kill-thread (thread)
  1399. (thread:destroy-thread thread))
  1400. (defimplementation thread-alive-p (thread)
  1401. (not (zerop (thread::thread-dynamic-values thread))))
  1402. (defvar *mailbox-lock* (thread:make-lock "Mailbox lock" :interruptible nil))
  1403. (defstruct (mailbox)
  1404. (lock (thread:make-lock "Thread mailbox" :type :error-check
  1405. :interruptible nil)
  1406. :type thread:error-check-lock)
  1407. (queue '() :type list))
  1408. (defun mailbox (thread)
  1409. "Return 'thread's mailbox."
  1410. (sys:without-interrupts
  1411. (thread:with-lock-held (*mailbox-lock*)
  1412. (or (getf (thread:thread-plist thread) 'mailbox)
  1413. (setf (getf (thread:thread-plist thread) 'mailbox)
  1414. (make-mailbox))))))
  1415. (defimplementation send (thread message)
  1416. (let* ((mbox (mailbox thread))
  1417. (lock (mailbox-lock mbox)))
  1418. (sys:without-interrupts
  1419. (thread:with-lock-held (lock "Mailbox Send")
  1420. (setf (mailbox-queue mbox) (nconc (mailbox-queue mbox)
  1421. (list message)))))
  1422. (mp:process-wakeup thread)))
  1423. #+nil
  1424. (defimplementation receive ()
  1425. (receive-if (constantly t)))
  1426. (defimplementation receive-if (test &optional timeout)
  1427. (let ((mbox (mailbox thread:*thread*)))
  1428. (assert (or (not timeout) (eq timeout t)))
  1429. (loop
  1430. (check-slime-interrupts)
  1431. (sys:without-interrupts
  1432. (mp:with-lock-held ((mailbox-lock mbox))
  1433. (let* ((q (mailbox-queue mbox))
  1434. (tail (member-if test q)))
  1435. (when tail
  1436. (setf (mailbox-queue mbox)
  1437. (nconc (ldiff q tail) (cdr tail)))
  1438. (return (car tail))))))
  1439. (when (eq timeout t) (return (values nil t)))
  1440. (mp:process-wait-with-timeout
  1441. "Mailbox read wait" 0.5 (lambda () (some test (mailbox-queue mbox)))))))
  1442. (defimplementation emacs-connected ())
  1443. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1444. ;;Trace implementations
  1445. ;; In SCL, we have:
  1446. ;; (trace <name>)
  1447. ;; (trace (method <name> <qualifier>? (<specializer>+)))
  1448. ;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
  1449. ;; <name> can be a normal name or a (setf name)
  1450. (defun tracedp (spec)
  1451. (member spec (eval '(trace)) :test #'equal))
  1452. (defun toggle-trace-aux (spec &rest options)
  1453. (cond ((tracedp spec)
  1454. (eval `(untrace ,spec))
  1455. (format nil "~S is now untraced." spec))
  1456. (t
  1457. (eval `(trace ,spec ,@options))
  1458. (format nil "~S is now traced." spec))))
  1459. (defimplementation toggle-trace (spec)
  1460. (ecase (car spec)
  1461. ((setf)
  1462. (toggle-trace-aux spec))
  1463. ((:defgeneric)
  1464. (let ((name (second spec)))
  1465. (toggle-trace-aux name :methods name)))
  1466. ((:defmethod)
  1467. nil)
  1468. ((:call)
  1469. (destructuring-bind (caller callee) (cdr spec)
  1470. (toggle-trace-aux (process-fspec callee)
  1471. :wherein (list (process-fspec caller)))))))
  1472. (defun process-fspec (fspec)
  1473. (cond ((consp fspec)
  1474. (ecase (first fspec)
  1475. ((:defun :defgeneric) (second fspec))
  1476. ((:defmethod)
  1477. `(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
  1478. ;; this isn't actually supported
  1479. ((:labels) `(labels ,(process-fspec (second fspec)) ,(third fspec)))
  1480. ((:flet) `(flet ,(process-fspec (second fspec)) ,(third fspec)))))
  1481. (t
  1482. fspec)))
  1483. ;;; Weak datastructures
  1484. ;;; Not implemented in SCL.
  1485. (defimplementation make-weak-key-hash-table (&rest args)
  1486. (apply #'make-hash-table :weak-p t args))