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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (show annotations)
Sun Jun 27 15:00:43 2004 UTC (9 years, 9 months ago) by heller
Branch: MAIN
Changes since 1.47: +17 -3 lines
(thread-id, find-thread): New backend function.
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 ((or (not frame)
200 (and (interesting-frame-p frame) (zerop i)))
201 frame)))
202
203 (defun nth-frame (index)
204 (nth-next-frame *sldb-top-frame* index))
205
206 (defun find-top-frame ()
207 "Return the most suitable top-frame for the debugger."
208 (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
209 (nth-next-frame frame 1)))
210 ((and (dbg::call-frame-p frame)
211 (eq (dbg::call-frame-function-name frame)
212 'invoke-debugger))
213 (nth-next-frame frame 1))))
214
215 (defimplementation call-with-debugging-environment (fn)
216 (dbg::with-debugger-stack ()
217 (let ((*sldb-top-frame* (find-top-frame)))
218 (funcall fn))))
219
220 (defimplementation compute-backtrace (start end)
221 (let ((end (or end most-positive-fixnum))
222 (backtrace '()))
223 (do ((frame (nth-frame start) (dbg::frame-next frame))
224 (i start))
225 ((or (not frame) (= i end)) (nreverse backtrace))
226 (when (interesting-frame-p frame)
227 (incf i)
228 (push frame backtrace)))))
229
230 (defun frame-actual-args (frame)
231 (mapcar (lambda (arg)
232 (handler-case (dbg::dbg-eval arg frame)
233 (error (format nil "<~A>" arg))))
234 (dbg::call-frame-arglist frame)))
235
236 (defimplementation print-frame (frame stream)
237 (cond ((dbg::call-frame-p frame)
238 (format stream "~S ~S"
239 (dbg::call-frame-function-name frame)
240 (frame-actual-args frame)))
241 (t (princ frame stream))))
242
243 (defun frame-vars (frame)
244 (first (dbg::frame-locals-format-list frame #'list 75 0)))
245
246 (defimplementation frame-locals (n)
247 (let ((frame (nth-frame n)))
248 (if (dbg::call-frame-p frame)
249 (mapcar (lambda (var)
250 (destructuring-bind (name value symbol location) var
251 (declare (ignore name location))
252 (list :name symbol :id 0
253 :value value)))
254 (frame-vars frame)))))
255
256 (defimplementation frame-var-value (frame var)
257 (let ((frame (nth-frame frame)))
258 (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
259 (declare (ignore _n _s _l))
260 value)))
261
262 (defimplementation frame-catch-tags (index)
263 (declare (ignore index))
264 nil)
265
266 (defimplementation frame-source-location-for-emacs (frame)
267 (let ((frame (nth-frame frame)))
268 (if (dbg::call-frame-p frame)
269 (let ((name (dbg::call-frame-function-name frame)))
270 (if name
271 (function-name-location name))))))
272
273 (defimplementation eval-in-frame (form frame-number)
274 (let ((frame (nth-frame frame-number)))
275 (dbg::dbg-eval form frame)))
276
277 (defimplementation return-from-frame (frame-number form)
278 (let* ((frame (nth-frame frame-number))
279 (return-frame (dbg::find-frame-for-return frame)))
280 (dbg::dbg-return-from-call-frame frame form return-frame
281 dbg::*debugger-stack*)))
282
283 (defimplementation restart-frame (frame-number)
284 (let ((frame (nth-frame frame-number)))
285 (dbg::restart-frame frame :same-args t)))
286
287 ;;; Definition finding
288
289 (defun function-name-location (name)
290 (let ((defs (find-definitions name)))
291 (cond (defs (cadr (first defs)))
292 (t (list :error (format nil "Source location not available for: ~S"
293 name))))))
294
295 (defimplementation find-definitions (name)
296 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
297 (loop for (dspec location) in locations
298 collect (list dspec (make-dspec-location dspec location)))))
299
300 ;;; Compilation
301
302 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
303 (lw:rebinding (location)
304 `(let ((compiler::*error-database* '()))
305 (with-compilation-unit ,options
306 ,@body
307 (signal-error-data-base compiler::*error-database* ,location)
308 (signal-undefined-functions compiler::*unknown-functions* ,location)))))
309
310 (defimplementation swank-compile-file (filename load-p)
311 (with-swank-compilation-unit (filename)
312 (compile-file filename :load load-p)))
313
314 (defun map-error-database (database fn)
315 (loop for (filename . defs) in database do
316 (loop for (dspec . conditions) in defs do
317 (dolist (c conditions)
318 (funcall fn filename dspec c)))))
319
320 (defun lispworks-severity (condition)
321 (cond ((not condition) :warning)
322 (t (etypecase condition
323 (error :error)
324 (style-warning :warning)
325 (warning :warning)))))
326
327 (defun signal-compiler-condition (message location condition)
328 (check-type message string)
329 (signal
330 (make-instance 'compiler-condition :message message
331 :severity (lispworks-severity condition)
332 :location location
333 :original-condition condition)))
334
335 (defun compile-from-temp-file (string filename)
336 (unwind-protect
337 (progn
338 (with-open-file (s filename :direction :output :if-exists :supersede)
339 (write-string string s)
340 (finish-output s))
341 (let ((binary-filename (compile-file filename :load t)))
342 (when binary-filename
343 (delete-file binary-filename))))
344 (delete-file filename)))
345
346 (defun dspec-buffer-position (dspec offset)
347 (etypecase dspec
348 (cons (let ((name (dspec:dspec-primary-name dspec)))
349 (typecase name
350 ((or symbol string)
351 (list :function-name (string name)))
352 (t (list :position offset)))))
353 (null (list :position offset))
354 (symbol (list :function-name (string dspec)))))
355
356 (defmacro with-fairly-standard-io-syntax (&body body)
357 "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
358 (let ((package (gensym))
359 (readtable (gensym)))
360 `(let ((,package *package*)
361 (,readtable *readtable*))
362 (with-standard-io-syntax
363 (let ((*package* ,package)
364 (*readtable* ,readtable))
365 ,@body)))))
366
367 #-(or lispworks-4.1 lispworks-4.2) ; no dspec:parse-form-dspec prior to 4.3
368 (defun dspec-stream-position (stream dspec)
369 (with-fairly-standard-io-syntax
370 (loop (let* ((pos (file-position stream))
371 (form (read stream nil '#1=#:eof)))
372 (when (eq form '#1#)
373 (return nil))
374 (labels ((check-dspec (form)
375 (when (consp form)
376 (let ((operator (car form)))
377 (case operator
378 ((progn)
379 (mapcar #'check-dspec
380 (cdr form)))
381 ((eval-when locally macrolet symbol-macrolet)
382 (mapcar #'check-dspec
383 (cddr form)))
384 ((in-package)
385 (let ((package (find-package (second form))))
386 (when package
387 (setq *package* package))))
388 (otherwise
389 (let ((form-dspec (dspec:parse-form-dspec form)))
390 (when (dspec:dspec-equal dspec form-dspec)
391 (return pos)))))))))
392 (check-dspec form))))))
393
394 (defun emacs-buffer-location-p (location)
395 (and (consp location)
396 (eq (car location) :emacs-buffer)))
397
398 (defun make-dspec-location (dspec location)
399 (flet ((filename (pathname)
400 (multiple-value-bind (truename condition)
401 (ignore-errors (truename pathname))
402 (cond (condition
403 (return-from make-dspec-location
404 (list :error (format nil "~A" condition))))
405 (t (namestring truename)))))
406 (function-name (dspec)
407 (etypecase dspec
408 (symbol (symbol-name dspec))
409 (cons (string (dspec:dspec-primary-name dspec))))))
410 (etypecase location
411 ((or pathname string)
412 (let ((checked-filename (filename location)))
413 (make-location `(:file ,checked-filename)
414 #+(or lispworks-4.1 lispworks-4.2)
415 (dspec-buffer-position dspec 1)
416 #-(or lispworks-4.1 lispworks-4.2)
417 (with-open-file (stream checked-filename)
418 (let ((position (dspec-stream-position stream dspec)))
419 (if position
420 (list :position (1+ position) t)
421 (dspec-buffer-position dspec 1)))))))
422 (symbol `(:error ,(format nil "Cannot resolve location: ~S" location)))
423 ((satisfies emacs-buffer-location-p)
424 (destructuring-bind (_ buffer offset string) location
425 (declare (ignore _ string))
426 (make-location `(:buffer ,buffer)
427 (dspec-buffer-position dspec offset)))))))
428
429 (defun make-dspec-progenitor-location (dspec location)
430 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
431 (make-dspec-location
432 (if canon-dspec
433 (if (dspec:local-dspec-p canon-dspec)
434 (dspec:dspec-progenitor canon-dspec)
435 canon-dspec)
436 nil)
437 location)))
438
439 (defun signal-error-data-base (database location)
440 (map-error-database
441 database
442 (lambda (filename dspec condition)
443 (declare (ignore filename))
444 (signal-compiler-condition
445 (format nil "~A" condition)
446 (make-dspec-progenitor-location dspec location)
447 condition))))
448
449 (defun signal-undefined-functions (htab filename)
450 (maphash (lambda (unfun dspecs)
451 (dolist (dspec dspecs)
452 (signal-compiler-condition
453 (format nil "Undefined function ~A" unfun)
454 (make-dspec-progenitor-location dspec filename)
455 nil)))
456 htab))
457
458 (defimplementation swank-compile-string (string &key buffer position)
459 (assert buffer)
460 (assert position)
461 (let* ((location (list :emacs-buffer buffer position string))
462 (tmpname (hcl:make-temp-file nil "lisp")))
463 (with-swank-compilation-unit (location)
464 (compile-from-temp-file
465 (format nil "~S~%~A" `(eval-when (:compile-toplevel)
466 (setq dspec::*location* (list ,@location)))
467 string)
468 tmpname))))
469
470 ;;; xref
471
472 (defmacro defxref (name function)
473 `(defimplementation ,name (name)
474 (xref-results (,function name))))
475
476 (defxref who-calls hcl:who-calls)
477 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
478 (defxref list-callees hcl:calls-who)
479 (defxref list-callers list-callers-internal)
480
481 (defun list-callers-internal (name)
482 (let ((callers (make-array 100
483 :fill-pointer 0
484 :adjustable t)))
485 (hcl:sweep-all-objects
486 #'(lambda (object)
487 (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
488 #-Harlequin-PC-Lisp (sys::callablep object)
489 (system::find-constant$funcallable name object))
490 (vector-push-extend object callers))))
491 ;; Delay dspec:object-dspec until after sweep-all-objects
492 ;; to reduce allocation problems.
493 (loop for object across callers
494 collect (if (symbolp object)
495 (list 'function object)
496 (dspec:object-dspec object)))))
497
498 ;; only for lispworks 4.2 and above
499 #-lispworks4.1
500 (progn
501 (defxref who-references hcl:who-references)
502 (defxref who-binds hcl:who-binds)
503 (defxref who-sets hcl:who-sets))
504
505 (defimplementation who-specializes (classname)
506 (let ((methods (clos:class-direct-methods (find-class classname))))
507 (xref-results (mapcar #'dspec:object-dspec methods))))
508
509 (defun xref-results (dspecs)
510 (loop for dspec in dspecs
511 nconc (loop for (dspec location)
512 in (dspec:dspec-definition-locations dspec)
513 collect (list dspec
514 (make-dspec-location dspec location)))))
515 ;;; Inspector
516
517 (defmethod inspected-parts (o)
518 (multiple-value-bind (names values _getter _setter type)
519 (lw:get-inspector-values o nil)
520 (declare (ignore _getter _setter))
521 (values (format nil "~A~% is a ~A" o type)
522 (mapcar #'cons names values))))
523
524 ;;; Miscellaneous
525
526 (defimplementation quit-lisp ()
527 (lispworks:quit))
528
529 ;;; Multithreading
530
531 (defimplementation startup-multiprocessing ()
532 (mp:initialize-multiprocessing))
533
534 (defimplementation spawn (fn &key name)
535 (let ((mp:*process-initial-bindings*
536 (remove (find-package :cl)
537 mp:*process-initial-bindings*
538 :key (lambda (x) (symbol-package (car x))))))
539 (mp:process-run-function name () fn)))
540
541 (defvar *id-lock* (mp:make-lock))
542 (defvar *thread-id-counter* 0)
543
544 (defimplementation thread-id (thread)
545 (mp:with-lock (*id-lock*)
546 (or (getf (mp:process-plist thread) 'id)
547 (setf (getf (mp:process-plist thread) 'id)
548 (incf *thread-id-counter*)))))
549
550 (defimplementation find-thread (id)
551 (find id (mp:list-all-processes)
552 :key (lambda (p) (getf (mp:process-plist p) 'id))))
553
554 (defimplementation thread-name (thread)
555 (mp:process-name thread))
556
557 (defimplementation thread-status (thread)
558 (format nil "~A ~D"
559 (mp:process-whostate thread)
560 (mp:process-priority thread)))
561
562 (defimplementation make-lock (&key name)
563 (mp:make-lock :name name))
564
565 (defimplementation call-with-lock-held (lock function)
566 (mp:with-lock (lock) (funcall function)))
567
568 (defimplementation current-thread ()
569 mp:*current-process*)
570
571 (defimplementation all-threads ()
572 (mp:list-all-processes))
573
574 (defimplementation interrupt-thread (thread fn)
575 (mp:process-interrupt thread fn))
576
577 (defimplementation kill-thread (thread)
578 (mp:process-kill thread))
579
580 (defimplementation thread-alive-p (thread)
581 (mp:process-alive-p thread))
582
583 (defvar *mailbox-lock* (mp:make-lock))
584
585 (defun mailbox (thread)
586 (mp:with-lock (*mailbox-lock*)
587 (or (getf (mp:process-plist thread) 'mailbox)
588 (setf (getf (mp:process-plist thread) 'mailbox)
589 (mp:make-mailbox)))))
590
591 (defimplementation receive ()
592 (mp:mailbox-read (mailbox mp:*current-process*)))
593
594 (defimplementation send (thread object)
595 (mp:mailbox-send (mailbox thread) object))
596

  ViewVC Help
Powered by ViewVC 1.1.5