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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (show annotations)
Wed Jun 9 12:40:52 2004 UTC (9 years, 10 months ago) by msimmons
Branch: MAIN
Changes since 1.41: +82 -15 lines
(dspec-stream-position): New function to make source location work for anything
complicated e.g. methods.
(with-swank-compilation-unit): Refactoring.
(who-macroexpands): Implemented.
(list-callers): Implemented.
1 ;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; swank-lispworks.lisp --- LispWorks specific code for SLIME.
4 ;;;
5 ;;; Created 2003, Helmut Eller
6 ;;;
7 ;;; This code has been placed in the Public Domain. All warranties
8 ;;; are disclaimed.
9 ;;;
10
11 (in-package :swank-backend)
12
13 (eval-when (:compile-toplevel :load-toplevel :execute)
14 (require "comm"))
15
16 (import
17 '(stream:fundamental-character-output-stream
18 stream:stream-write-char
19 stream:stream-force-output
20 stream:fundamental-character-input-stream
21 stream:stream-read-char
22 stream:stream-listen
23 stream:stream-unread-char
24 stream:stream-clear-input
25 stream:stream-line-column
26 ))
27
28 ;;; TCP server
29
30 (defimplementation preferred-communication-style ()
31 :spawn)
32
33 (defun socket-fd (socket)
34 (etypecase socket
35 (fixnum socket)
36 (comm:socket-stream (comm:socket-stream-socket socket))))
37
38 (defimplementation create-socket (host port)
39 (multiple-value-bind (socket where errno)
40 #-lispworks4.1(comm::create-tcp-socket-for-service port :address host)
41 #+lispworks4.1(comm::create-tcp-socket-for-service port)
42 (cond (socket socket)
43 (t (error 'network-error
44 :format-control "~A failed: ~A (~D)"
45 :format-arguments (list where
46 (list #+unix (lw:get-unix-error errno))
47 errno))))))
48
49 (defimplementation local-port (socket)
50 (nth-value 1 (comm:get-socket-address (socket-fd socket))))
51
52 (defimplementation close-socket (socket)
53 (comm::close-socket (socket-fd socket)))
54
55 (defimplementation accept-connection (socket)
56 (let ((fd (comm::get-fd-from-socket socket)))
57 (assert (/= fd -1))
58 (make-instance 'comm:socket-stream :socket fd :direction :io
59 :element-type 'base-char)))
60
61 (defun set-sigint-handler ()
62 ;; Set SIGINT handler on Swank request handler thread.
63 #-win32
64 (sys::set-signal-handler +sigint+
65 (make-sigint-handler mp:*current-process*)))
66
67 (defimplementation emacs-connected ()
68 (set-sigint-handler)
69 (let ((lw:*handle-warn-on-redefinition* :warn))
70 (defmethod stream:stream-soft-force-output ((o comm:socket-stream))
71 (force-output o))
72 (defmethod stream:stream-soft-force-output ((o slime-output-stream))
73 (force-output o))
74 (defmethod env-internals:environment-display-notifier
75 (env &key restarts condition)
76 (declare (ignore restarts))
77 (funcall (find-symbol (string :swank-debugger-hook) :swank)
78 condition *debugger-hook*))))
79
80 ;;; Unix signals
81
82 (defun sigint-handler ()
83 (with-simple-restart (continue "Continue from SIGINT handler.")
84 (invoke-debugger "SIGINT")))
85
86 (defun make-sigint-handler (process)
87 (lambda (&rest args)
88 (declare (ignore args))
89 (mp:process-interrupt process #'sigint-handler)))
90
91 (defimplementation call-without-interrupts (fn)
92 (lw:without-interrupts (funcall fn)))
93
94 (defimplementation getpid ()
95 #+win32 (win32:get-current-process-id)
96 #-win32 (system::getpid))
97
98 (defimplementation lisp-implementation-type-name ()
99 "lispworks")
100
101 (defimplementation set-default-directory (directory)
102 (namestring (hcl:change-directory directory)))
103
104 ;;;; Documentation
105
106 (defimplementation arglist (symbol)
107 (let ((arglist (lw:function-lambda-list symbol)))
108 (etypecase arglist
109 ((member :dont-know)
110 :not-available)
111 (list
112 arglist))))
113
114 (defimplementation macroexpand-all (form)
115 (walker:walk-form form))
116
117 (defun generic-function-p (object)
118 (typep object 'generic-function))
119
120 (defimplementation describe-symbol-for-emacs (symbol)
121 "Return a plist describing SYMBOL.
122 Return NIL if the symbol is unbound."
123 (let ((result '()))
124 (labels ((first-line (string)
125 (let ((pos (position #\newline string)))
126 (if (null pos) string (subseq string 0 pos))))
127 (doc (kind &optional (sym symbol))
128 (let ((string (documentation sym kind)))
129 (if string
130 (first-line string)
131 :not-documented)))
132 (maybe-push (property value)
133 (when value
134 (setf result (list* property value result)))))
135 (maybe-push
136 :variable (when (boundp symbol)
137 (doc 'variable)))
138 (maybe-push
139 :generic-function (if (and (fboundp symbol)
140 (generic-function-p (fdefinition symbol)))
141 (doc 'function)))
142 (maybe-push
143 :function (if (and (fboundp symbol)
144 (not (generic-function-p (fdefinition symbol))))
145 (doc 'function)))
146 (maybe-push
147 :class (if (find-class symbol nil)
148 (doc 'class)))
149 result)))
150
151 (defimplementation describe-definition (symbol type)
152 (ecase type
153 (:variable (describe-symbol symbol))
154 (:class (describe (find-class symbol)))
155 ((:function :generic-function) (describe-function symbol))))
156
157 (defun describe-function (symbol)
158 (cond ((fboundp symbol)
159 (format t "~%(~A~{ ~A~})~%~%~:[(not documented)~;~:*~A~]~%"
160 (string-downcase symbol)
161 (mapcar #'string-upcase
162 (lispworks:function-lambda-list symbol))
163 (documentation symbol 'function))
164 (describe (fdefinition symbol)))
165 (t (format t "~S is not fbound" symbol))))
166
167 (defun describe-symbol (sym)
168 (format t "~A is a symbol in package ~A." sym (symbol-package sym))
169 (when (boundp sym)
170 (format t "~%~%Value: ~A" (symbol-value sym)))
171 (let ((doc (documentation sym 'variable)))
172 (when doc
173 (format t "~%~%Variable documentation:~%~A" doc)))
174 (when (fboundp sym)
175 (describe-function sym)))
176
177 ;;; Debugging
178
179 (defvar *sldb-top-frame*)
180
181 (defun interesting-frame-p (frame)
182 (cond ((or (dbg::call-frame-p frame)
183 (dbg::derived-call-frame-p frame)
184 (dbg::foreign-frame-p frame)
185 (dbg::interpreted-call-frame-p frame))
186 t)
187 ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
188 ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
189 ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
190 ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
191 ((dbg::open-frame-p frame) dbg:*print-open-frames*)
192 (t nil)))
193
194 (defun nth-next-frame (frame n)
195 "Unwind FRAME N times."
196 (do ((frame frame (dbg::frame-next frame))
197 (i n (if (interesting-frame-p frame) (1- i) i)))
198 ((and (interesting-frame-p frame) (zerop i)) frame)
199 (assert frame)))
200
201 (defun nth-frame (index)
202 (nth-next-frame *sldb-top-frame* index))
203
204 (defun find-top-frame ()
205 "Return the most suitable top-frame for the debugger."
206 (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
207 (nth-next-frame frame 1)))
208 ((and (dbg::call-frame-p frame)
209 (eq (dbg::call-frame-function-name frame)
210 'invoke-debugger))
211 (nth-next-frame frame 1))))
212
213 (defimplementation call-with-debugging-environment (fn)
214 (dbg::with-debugger-stack ()
215 (let ((*sldb-top-frame* (find-top-frame)))
216 (funcall fn))))
217
218 (defimplementation compute-backtrace (start end)
219 (let ((end (or end most-positive-fixnum))
220 (backtrace '()))
221 (do ((frame (nth-frame start) (dbg::frame-next frame))
222 (i start))
223 ((or (not frame) (= i end)) (nreverse backtrace))
224 (when (interesting-frame-p frame)
225 (incf i)
226 (push frame backtrace)))))
227
228 (defun frame-actual-args (frame)
229 (mapcar (lambda (arg)
230 (handler-case (dbg::dbg-eval arg frame)
231 (error (format nil "<~A>" arg))))
232 (dbg::call-frame-arglist frame)))
233
234 (defimplementation print-frame (frame stream)
235 (cond ((dbg::call-frame-p frame)
236 (format stream "~S ~S"
237 (dbg::call-frame-function-name frame)
238 (frame-actual-args frame)))
239 (t (princ frame stream))))
240
241 (defimplementation frame-locals (n)
242 (let ((frame (nth-frame n)))
243 (if (dbg::call-frame-p frame)
244 (destructuring-bind (vars with)
245 (dbg::frame-locals-format-list frame #'list 75 0)
246 (declare (ignore with))
247 (mapcar (lambda (var)
248 (destructuring-bind (name value symbol location) var
249 (declare (ignore name location))
250 (list :name symbol :id 0
251 :value value)))
252 vars)))))
253
254 (defimplementation frame-catch-tags (index)
255 (declare (ignore index))
256 nil)
257
258 (defimplementation frame-source-location-for-emacs (frame)
259 (let ((frame (nth-frame frame)))
260 (if (dbg::call-frame-p frame)
261 (let ((name (dbg::call-frame-function-name frame)))
262 (if name
263 (function-name-location name))))))
264
265 (defimplementation eval-in-frame (form frame-number)
266 (let ((frame (nth-frame frame-number)))
267 (dbg::dbg-eval form frame)))
268
269 (defimplementation return-from-frame (frame-number form)
270 (let* ((frame (nth-frame frame-number))
271 (return-frame (dbg::find-frame-for-return frame)))
272 (dbg::dbg-return-from-call-frame frame form return-frame
273 dbg::*debugger-stack*)))
274
275 (defimplementation restart-frame (frame-number)
276 (let ((frame (nth-frame frame-number)))
277 (dbg::restart-frame frame :same-args t)))
278
279 ;;; Definition finding
280
281 (defun function-name-location (name)
282 (let ((defs (find-definitions name)))
283 (cond (defs (cadr (first defs)))
284 (t (list :error (format nil "Source location not available for: ~S"
285 name))))))
286
287 (defimplementation find-definitions (name)
288 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
289 (loop for (dspec location) in locations
290 collect (list dspec (make-dspec-location dspec location)))))
291
292 ;;; Compilation
293
294 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
295 (lw:rebinding (location)
296 `(let ((compiler::*error-database* '()))
297 (with-compilation-unit ,options
298 ,@body
299 (signal-error-data-base compiler::*error-database* ,location)
300 (signal-undefined-functions compiler::*unknown-functions* ,location)))))
301
302 (defimplementation swank-compile-file (filename load-p)
303 (with-swank-compilation-unit (filename)
304 (compile-file filename :load load-p)))
305
306 (defun map-error-database (database fn)
307 (loop for (filename . defs) in database do
308 (loop for (dspec . conditions) in defs do
309 (dolist (c conditions)
310 (funcall fn filename dspec c)))))
311
312 (defun lispworks-severity (condition)
313 (cond ((not condition) :warning)
314 (t (etypecase condition
315 (error :error)
316 (style-warning :warning)
317 (warning :warning)))))
318
319 (defun signal-compiler-condition (message location condition)
320 (check-type message string)
321 (signal
322 (make-instance 'compiler-condition :message message
323 :severity (lispworks-severity condition)
324 :location location
325 :original-condition condition)))
326
327 (defun compile-from-temp-file (string filename)
328 (unwind-protect
329 (progn
330 (with-open-file (s filename :direction :output :if-exists :supersede)
331 (write-string string s)
332 (finish-output s))
333 (let ((binary-filename (compile-file filename :load t)))
334 (when binary-filename
335 (delete-file binary-filename))))
336 (delete-file filename)))
337
338 (defun dspec-buffer-position (dspec offset)
339 (etypecase dspec
340 (cons (let ((name (dspec:dspec-primary-name dspec)))
341 (typecase name
342 ((or symbol string)
343 (list :function-name (string name)))
344 (t (list :position offset)))))
345 (null (list :position offset))
346 (symbol (list :function-name (string dspec)))))
347
348 #-(or lispworks-4.1 lispworks-4.2) ; no dspec:parse-form-dspec prior to 4.3
349 (defun dspec-stream-position (stream dspec)
350 (with-standard-io-syntax
351 (let ((*read-eval* nil))
352 (loop (let* ((pos (file-position stream))
353 (form (read stream nil '#1=#:eof)))
354 (when (eq form '#1#)
355 (return nil))
356 (labels ((check-dspec (form)
357 (when (consp form)
358 (let ((operator (car form)))
359 (case operator
360 ((progn)
361 (mapcar #'check-dspec
362 (cdr form)))
363 ((eval-when locally macrolet symbol-macrolet)
364 (mapcar #'check-dspec
365 (cddr form)))
366 ((in-package)
367 (let ((package (find-package (second form))))
368 (when package
369 (setq *package* package))))
370 (otherwise
371 (let ((form-dspec (dspec:parse-form-dspec form)))
372 (when (dspec:dspec-equal dspec form-dspec)
373 (return pos)))))))))
374 (check-dspec form)))))))
375
376 (defun emacs-buffer-location-p (location)
377 (and (consp location)
378 (eq (car location) :emacs-buffer)))
379
380 (defun make-dspec-location (dspec location)
381 (flet ((filename (pathname)
382 (multiple-value-bind (truename condition)
383 (ignore-errors (truename pathname))
384 (cond (condition
385 (return-from make-dspec-location
386 (list :error (format nil "~A" condition))))
387 (t (namestring truename)))))
388 (function-name (dspec)
389 (etypecase dspec
390 (symbol (symbol-name dspec))
391 (cons (string (dspec:dspec-primary-name dspec))))))
392 (etypecase location
393 ((or pathname string)
394 (let ((checked-filename (filename location)))
395 (make-location `(:file ,checked-filename)
396 #+(or lispworks-4.1 lispworks-4.2)
397 (dspec-buffer-position dspec 1)
398 #-(or lispworks-4.1 lispworks-4.2)
399 (with-open-file (stream checked-filename)
400 (let ((position (dspec-stream-position stream dspec)))
401 (if position
402 (list :position (1+ position) t)
403 (dspec-buffer-position dspec 1)))))))
404 (symbol `(:error ,(format nil "Cannot resolve location: ~S" location)))
405 ((satisfies emacs-buffer-location-p)
406 (destructuring-bind (_ buffer offset string) location
407 (declare (ignore _ string))
408 (make-location `(:buffer ,buffer)
409 (dspec-buffer-position dspec offset)))))))
410
411 (defun make-dspec-progenitor-location (dspec location)
412 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
413 (make-dspec-location
414 (if canon-dspec
415 (if (dspec:local-dspec-p canon-dspec)
416 (dspec:dspec-progenitor canon-dspec)
417 canon-dspec)
418 nil)
419 location)))
420
421 (defun signal-error-data-base (database location)
422 (map-error-database
423 database
424 (lambda (filename dspec condition)
425 (declare (ignore filename))
426 (signal-compiler-condition
427 (format nil "~A" condition)
428 (make-dspec-progenitor-location dspec location)
429 condition))))
430
431 (defun signal-undefined-functions (htab filename)
432 (maphash (lambda (unfun dspecs)
433 (dolist (dspec dspecs)
434 (signal-compiler-condition
435 (format nil "Undefined function ~A" unfun)
436 (make-dspec-progenitor-location dspec filename)
437 nil)))
438 htab))
439
440 (defimplementation swank-compile-string (string &key buffer position)
441 (assert buffer)
442 (assert position)
443 (let* ((location (list :emacs-buffer buffer position string))
444 (tmpname (hcl:make-temp-file nil "lisp")))
445 (with-swank-compilation-unit (location)
446 (compile-from-temp-file
447 (format nil "~S~%~A" `(eval-when (:compile-toplevel)
448 (setq dspec::*location* (list ,@location)))
449 string)
450 tmpname))))
451
452 ;;; xref
453
454 (defmacro defxref (name function)
455 `(defimplementation ,name (name)
456 (xref-results (,function name))))
457
458 (defxref who-calls hcl:who-calls)
459 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
460 (defxref list-callees hcl:calls-who)
461 (defxref list-callers list-callers-internal)
462
463 (defun list-callers-internal (name)
464 (let ((callers (make-array 100
465 :fill-pointer 0
466 :adjustable t)))
467 (hcl:sweep-all-objects
468 #'(lambda (object)
469 (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
470 #-Harlequin-PC-Lisp (sys::callablep object)
471 (system::find-constant$funcallable name object))
472 (vector-push-extend object callers))))
473 ;; Delay dspec:object-dspec until after sweep-all-objects
474 ;; to reduce allocation problems.
475 (loop for object across callers
476 collect (if (symbolp object)
477 (list 'function object)
478 (dspec:object-dspec object)))))
479
480 ;; only for lispworks 4.2 and above
481 #-lispworks4.1
482 (progn
483 (defxref who-references hcl:who-references)
484 (defxref who-binds hcl:who-binds)
485 (defxref who-sets hcl:who-sets))
486
487 (defimplementation who-specializes (classname)
488 (let ((methods (clos:class-direct-methods (find-class classname))))
489 (xref-results (mapcar #'dspec:object-dspec methods))))
490
491 (defun xref-results (dspecs)
492 (loop for dspec in dspecs
493 nconc (loop for (dspec location)
494 in (dspec:dspec-definition-locations dspec)
495 collect (list dspec
496 (make-dspec-location dspec location)))))
497 ;;; Inspector
498
499 (defmethod inspected-parts (o)
500 (multiple-value-bind (names values _getter _setter type)
501 (lw:get-inspector-values o nil)
502 (declare (ignore _getter _setter))
503 (values (format nil "~A~% is a ~A" o type)
504 (mapcar #'cons names values))))
505
506 ;;; Multithreading
507
508 (defimplementation startup-multiprocessing ()
509 (mp:initialize-multiprocessing))
510
511 (defimplementation spawn (fn &key name)
512 (let ((mp:*process-initial-bindings*
513 (remove (find-package :cl)
514 mp:*process-initial-bindings*
515 :key (lambda (x) (symbol-package (car x))))))
516 (mp:process-run-function name () fn)))
517
518 (defimplementation thread-name (thread)
519 (mp:process-name thread))
520
521 (defimplementation thread-status (thread)
522 (format nil "~A ~D"
523 (mp:process-whostate thread)
524 (mp:process-priority thread)))
525
526 (defimplementation make-lock (&key name)
527 (mp:make-lock :name name))
528
529 (defimplementation call-with-lock-held (lock function)
530 (mp:with-lock (lock) (funcall function)))
531
532 (defimplementation current-thread ()
533 mp:*current-process*)
534
535 (defimplementation all-threads ()
536 (mp:list-all-processes))
537
538 (defimplementation interrupt-thread (thread fn)
539 (mp:process-interrupt thread fn))
540
541 (defimplementation kill-thread (thread)
542 (mp:process-kill thread))
543
544 (defimplementation thread-alive-p (thread)
545 (mp:process-alive-p thread))
546
547 (defvar *mailbox-lock* (mp:make-lock))
548
549 (defun mailbox (thread)
550 (mp:with-lock (*mailbox-lock*)
551 (or (getf (mp:process-plist thread) 'mailbox)
552 (setf (getf (mp:process-plist thread) 'mailbox)
553 (mp:make-mailbox)))))
554
555 (defimplementation receive ()
556 (mp:mailbox-read (mailbox mp:*current-process*)))
557
558 (defimplementation send (thread object)
559 (mp:mailbox-send (mailbox thread) object))
560

  ViewVC Help
Powered by ViewVC 1.1.5