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