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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5