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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations)
Tue Feb 24 23:31:34 2004 UTC (10 years, 1 month ago) by heller
Branch: MAIN
Changes since 1.20: +3 -0 lines
* slime.el: Various bits of support for maintaining multiple SLIME
connections to different Lisp implementations simultaneously.

* swank-{backend,cmucl,sbcl,clisp,lispworks,openmcl}.lisp
(lisp-implementation-type-name): Add function to
return simple name of lisp implementation; used by new
multi-connection functionality in slime.el.
1 ;;;; SWANK support for CLISP.
2
3 ;;;; Copyright (C) 2003, 2004 W. Jenkner, V. Sedach
4
5 ;;;; swank-clisp.lisp is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU General Public License as
7 ;;;; published by the Free Software Foundation; either version 2, or
8 ;;;; (at your option) any later version.
9
10 ;;; This is work in progress, but it's already usable. Many things
11 ;;; are adapted from other swank-*.lisp, in particular from
12 ;;; swank-allegro (I don't use allegro at all, but it's the shortest
13 ;;; one and I found Helmut Eller's code there enlightening).
14
15 ;;; This code is developed using the current CVS version of CLISP and
16 ;;; CLISP 2.32 on Linux. Older versions may not work (2.29 and below
17 ;;; are confirmed non-working; please upgrade). You need an image
18 ;;; containing the "SOCKET", "REGEXP", and "LINUX" packages. The
19 ;;; portable xref from the CMU AI repository and metering.lisp from
20 ;;; CLOCC [1] are also required (alternatively, you have to manually
21 ;;; comment out some code below).
22 ;;;
23 ;;; [1] http://cvs.sourceforge.net/viewcvs.py/clocc/clocc/src/tools/metering/
24
25 (in-package "SWANK")
26
27 (eval-when (:compile-toplevel :load-toplevel :execute)
28 (use-package "SOCKET")
29 (use-package "GRAY"))
30
31 (eval-when (:compile-toplevel :execute)
32 (when (find-package "LINUX")
33 (pushnew :linux *features*)))
34
35 #+linux
36 (defmacro with-blocked-signals ((&rest signals) &body body)
37 (ext:with-gensyms ("SIGPROCMASK" ret mask)
38 `(multiple-value-bind (,ret ,mask)
39 (linux:sigprocmask-set-n-save
40 ,linux:SIG_BLOCK
41 ,(do ((sigset (linux:sigset-empty)
42 (linux:sigset-add sigset (the fixnum (pop signals)))))
43 ((null signals) sigset)))
44 (linux:check-res ,ret 'linux:sigprocmask-set-n-save)
45 (unwind-protect
46 (progn ,@body)
47 (linux:sigprocmask-set ,linux:SIG_SETMASK ,mask nil)))))
48
49 ;; #+linux
50 ;; (defmethod call-without-interrupts (fn)
51 ;; (with-blocked-signals (#.linux:SIGINT) (funcall fn)))
52 ;;
53 ;; #-linux
54 (defmethod call-without-interrupts (fn)
55 (funcall fn))
56
57 #+unix (defmethod getpid () (system::program-id))
58 #+win32 (defmethod getpid () (or (system::getenv "PID") -1))
59 ;; the above is likely broken; we need windows NT users!
60
61 (defimplementation lisp-implementation-type-name ()
62 "clisp")
63
64
65 ;;; TCP Server
66
67 (setq *swank-in-background* nil)
68
69 (defimplementation create-socket (host port)
70 (declare (ignore host))
71 (socket:socket-server port))
72
73 (defimplementation local-port (socket)
74 (socket:socket-server-port socket))
75
76 (defimplementation close-socket (socket)
77 (socket:socket-server-close socket))
78
79 (defimplementation accept-connection (socket)
80 (socket:socket-accept socket
81 :buffered nil ;; XXX should be t
82 :element-type 'character
83 :external-format (ext:make-encoding
84 :charset 'charset:iso-8859-1
85 :line-terminator :unix)))
86
87 (defvar *sigio-handlers* '()
88 "List of (key . fn) pairs to be called on SIGIO.")
89
90 (defun sigio-handler (signal)
91 (mapc (lambda (handler) (funcall (cdr handler))) *sigio-handlers*))
92
93 ;(trace sigio-handler)
94
95 (defvar *saved-sigio-handler*)
96
97 #+(or)
98 (progn
99 (defun set-sigio-handler ()
100 (setf *saved-sigio-handler*
101 (linux:set-signal-handler linux:SIGIO
102 (lambda (signal) (sigio-handler signal))))
103 (let* ((action (linux:signal-action-retrieve linux:SIGIO))
104 (flags (linux:sa-flags action)))
105 (setf (linux:sa-flags action) (logior flags linux:SA_NODEFER))
106 (linux:signal-action-install linux:SIGIO action)))
107
108 (defimplementation add-input-handler (socket fn)
109 (set-sigio-handler)
110 (let ((fd (socket:socket-stream-handle socket)))
111 (format *debug-io* "Adding input handler: ~S ~%" fd)
112 ;; XXX error checking
113 (linux:fcntl3l fd linux:F_SETOWN (getpid))
114 (linux:fcntl3l fd linux:F_SETFL linux:O_ASYNC)
115 (push (cons fd fn) *sigio-handlers*)))
116
117 (defimplementation remove-input-handlers (socket)
118 (let ((fd (socket:socket-stream-handle socket)))
119 (remove-sigio-handler fd)
120 (setf *sigio-handlers* (delete fd *sigio-handlers* :key #'car)))
121 (close socket))
122 )
123
124 ;;; Swank functions
125
126 (defimplementation arglist-string (fname)
127 (format-arglist fname #'ext:arglist))
128
129 (defimplementation macroexpand-all (form)
130 (ext:expand-form form))
131
132 (defimplementation describe-symbol-for-emacs (symbol)
133 "Return a plist describing SYMBOL.
134 Return NIL if the symbol is unbound."
135 (let ((result ()))
136 (labels ((doc (kind)
137 (or (documentation symbol kind) :not-documented))
138 (maybe-push (property value)
139 (when value
140 (setf result (list* property value result)))))
141 (when (fboundp symbol)
142 (if (macro-function symbol)
143 (setf (getf result :macro) (doc 'function))
144 (setf (getf result :function) (doc 'function))))
145 (maybe-push :variable (when (boundp symbol) (doc 'variable)))
146 (maybe-push :class (when (find-class symbol nil)
147 (doc 'type))) ;this should be fixed
148 result)))
149
150 (defun fspec-pathname (symbol &optional type)
151 (declare (ignore type))
152 (let ((path (getf (gethash symbol sys::*documentation*) 'sys::file)))
153 (if (and path
154 (member (pathname-type path)
155 custom:*compiled-file-types* :test #'string=))
156 (loop
157 for suffix in custom:*source-file-types*
158 thereis (make-pathname :defaults path :type suffix))
159 path)))
160
161 (defun find-multiple-definitions (fspec)
162 (list `(,fspec t)))
163
164 (defun find-definition-in-file (fspec type file)
165 (declare (ignore fspec type file))
166 ;; FIXME
167 0)
168
169 (defun fspec-source-locations (fspec)
170 (let ((defs (find-multiple-definitions fspec)))
171 (let ((locations '()))
172 (loop for (fspec type) in defs do
173 (let ((file (fspec-pathname fspec type)))
174 (etypecase file
175 (pathname
176 (let ((start (find-definition-in-file fspec type file)))
177 (push (make-location
178 (list :file (namestring (truename file)))
179 (if start
180 (list :position (1+ start))
181 (list :function-name (string fspec))))
182 locations)))
183 ((member :top-level)
184 (push (list :error (format nil "Defined at toplevel: ~A"
185 fspec))
186 locations))
187 (null
188 (push (list :error (format nil
189 "Unkown source location for ~A"
190 fspec))
191 locations))
192 )))
193 locations)))
194
195 (defimplementation find-function-locations (symbol-name)
196 (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)
197 (cond ((not foundp)
198 (list (list :error (format nil "Unkown symbol: ~A" symbol-name))))
199 ((macro-function symbol)
200 (fspec-source-locations symbol))
201 ((special-operator-p symbol)
202 (list (list :error (format nil "~A is a special-operator" symbol))))
203 ((fboundp symbol)
204 (fspec-source-locations symbol))
205 (t (list (list :error
206 (format nil "Symbol not fbound: ~A" symbol-name))))
207 )))
208
209 (defvar *sldb-topframe*)
210 (defvar *sldb-botframe*)
211 (defvar *sldb-source*)
212 (defvar *sldb-restarts*)
213 (defvar *sldb-debugmode* 4)
214
215 (defimplementation call-with-debugging-environment (debugger-loop-fn)
216 (let* ((sys::*break-count* (1+ sys::*break-count*))
217 (sys::*driver* debugger-loop-fn)
218 (sys::*fasoutput-stream* nil)
219 ;;; (sys::*frame-limit1* (sys::frame-limit1 43))
220 (sys::*frame-limit1* (sys::frame-limit1 0))
221 ;;; (sys::*frame-limit2* (sys::frame-limit2))
222 (sys::*debug-mode* *sldb-debugmode*)
223 (*sldb-topframe*
224 (sys::frame-down-1
225 (sys::frame-up-1 sys::*frame-limit1* sys::*debug-mode*)
226 sys::*debug-mode*))
227 (*sldb-botframe* (sys::frame-up *sldb-topframe* sys::*debug-mode*))
228 (*debugger-hook* nil)
229 (*package* *buffer-package*)
230 (*sldb-restarts*
231 (compute-restarts *swank-debugger-condition*))
232 (*print-pretty* nil)
233 (*print-readably* nil))
234 ;;; (*print-level* 3)
235 ;;; (*print-length* 10))
236 (funcall debugger-loop-fn)))
237
238 (defun format-restarts-for-emacs ()
239 (loop for restart in *sldb-restarts*
240 collect (list (princ-to-string (restart-name restart))
241 (princ-to-string restart))))
242
243 (defun nth-frame (index)
244 (loop for frame = *sldb-topframe* then (sys::frame-up-1 frame
245 sys::*debug-mode*)
246 repeat index
247 never (eq frame *sldb-botframe*)
248 finally (return frame)))
249
250 (defun compute-backtrace (start end)
251 (let ((end (or end most-positive-fixnum)))
252 (loop for f = (nth-frame start)
253 then (sys::frame-up-1 f sys::*debug-mode*)
254 for i from start below end
255 until (eq f *sldb-botframe*)
256 collect f)))
257
258 (defimplementation backtrace (start-frame-number end-frame-number)
259 (flet ((format-frame (f i)
260 (print-with-frame-label
261 i (lambda (s)
262 (princ (string-left-trim
263 '(#\Newline)
264 (with-output-to-string (stream)
265 (sys::describe-frame stream f)))
266 s)))))
267 (loop for i from start-frame-number
268 for f in (compute-backtrace start-frame-number end-frame-number)
269 collect (list i (format-frame f i)))))
270
271 (defimplementation eval-in-frame (form frame-number)
272 (sys::eval-at (nth-frame frame-number) form))
273
274 (defimplementation frame-locals (frame-number)
275 (let* ((frame (nth-frame frame-number))
276 (frame-env (sys::eval-at frame '(sys::the-environment))))
277 (append
278 (frame-do-venv frame (svref frame-env 0))
279 (frame-do-fenv frame (svref frame-env 1))
280 (frame-do-benv frame (svref frame-env 2))
281 (frame-do-genv frame (svref frame-env 3))
282 (frame-do-denv frame (svref frame-env 4)))))
283
284 ;; Interpreter-Variablen-Environment has the shape
285 ;; NIL or #(v1 val1 ... vn valn NEXT-ENV).
286
287 (defun frame-do-venv (frame venv)
288 (loop for i from 1 below (length venv) by 2
289 as symbol = (svref venv (1- i))
290 and value = (svref venv i)
291 collect (list :name (to-string symbol) :id 0
292 :value-string (to-string
293 (if (eq sys::specdecl value)
294 ;; special variable
295 (sys::eval-at frame symbol)
296 ;; lexical variable or symbol macro
297 value)))))
298
299 (defun frame-do-fenv (frame fenv)
300 (declare (ignore frame fenv))
301 nil)
302
303 (defun frame-do-benv (frame benv)
304 (declare (ignore frame benv))
305 nil)
306
307 (defun frame-do-genv (frame genv)
308 (declare (ignore frame genv))
309 nil)
310
311 (defun frame-do-denv (frame denv)
312 (declare (ignore frame denv))
313 nil)
314
315 (defimplementation frame-catch-tags (index)
316 (declare (ignore index))
317 nil)
318
319 (defimplementation return-from-frame (index form)
320 (sys::return-from-eval-frame (nth-frame index) (from-string form)))
321
322 (defimplementation restart-frame (index)
323 (sys::redo-eval-frame (nth-frame index)))
324
325 (defimplementation frame-source-location-for-emacs (index)
326 (list :error (format nil "Cannot find source for frame: ~A"
327 (nth-frame index))))
328
329 (defimplementation debugger-info-for-emacs (start end)
330 (list (debugger-condition-for-emacs)
331 (format-restarts-for-emacs)
332 (backtrace start end)))
333
334 (defun nth-restart (index)
335 (nth index *sldb-restarts*))
336
337 (defslimefun invoke-nth-restart (index)
338 (invoke-restart-interactively (nth-restart index)))
339
340 (defslimefun sldb-abort ()
341 (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))
342
343 ;;; Profiling
344
345 (defimplementation profile (fname)
346 (eval `(mon:monitor ,fname))) ;monitor is a macro
347
348 (defimplementation profiled-functions ()
349 mon:*monitored-functions*)
350
351 (defimplementation unprofile (fname)
352 (eval `(mon:unmonitor ,fname))) ;unmonitor is a macro
353
354 (defimplementation unprofile-all ()
355 (mon:unmonitor))
356
357 (defimplementation profile-report ()
358 (mon:report-monitoring))
359
360 (defimplementation profile-reset ()
361 (mon:reset-all-monitoring))
362
363 (defimplementation profile-package (package callers-p methods)
364 (declare (ignore callers-p methods))
365 (mon:monitor-all package))
366
367 ;;; Handle compiler conditions (find out location of error etc.)
368
369 (defmacro compile-file-frobbing-notes ((&rest args) &body body)
370 "Pass ARGS to COMPILE-FILE, send the compiler notes to
371 *STANDARD-INPUT* and frob them in BODY."
372 `(let ((*error-output* (make-string-output-stream))
373 (*compile-verbose* t))
374 (multiple-value-prog1
375 (compile-file ,@args)
376 (handler-case
377 (with-input-from-string
378 (*standard-input* (get-output-stream-string *error-output*))
379 ,@body)
380 (sys::simple-end-of-file () nil)))))
381
382 (defimplementation call-with-compilation-hooks (function)
383 (handler-bind ((compiler-condition #'handle-notification-condition))
384 (funcall function)))
385
386 (defun handle-notification-condition (condition)
387 "Handle a condition caused by a compiler warning."
388 (declare (ignore condition)))
389
390 (defvar *buffer-name* nil)
391 (defvar *buffer-offset*)
392
393 (defvar *compiler-note-line-regexp*
394 (regexp:regexp-compile
395 "^(WARNING|ERROR) .* in lines ([0-9]+)\\.\\.[0-9]+ :$"
396 :extended t))
397
398 (defun split-compiler-note-line (line)
399 (multiple-value-bind (all head tail)
400 (regexp:regexp-exec *compiler-note-line-regexp* line)
401 (declare (ignore all))
402 (if head
403 (list (let ((*package* (find-package :keyword)))
404 (read-from-string (regexp:match-string line head)))
405 (read-from-string (regexp:match-string line tail)))
406 (list nil line))))
407
408 ;;; Ugly but essentially working.
409 ;;; TODO: Do something with the summary about undefined functions etc.
410
411 (defimplementation compile-file-for-emacs (filename load-p)
412 (with-compilation-hooks ()
413 (multiple-value-bind (fas-file w-p f-p)
414 (compile-file-frobbing-notes (filename)
415 (read-line) ;""
416 (read-line) ;"Compiling file ..."
417 (loop
418 with condition
419 for (severity message) = (split-compiler-note-line (read-line))
420 until (and (stringp message) (string= message ""))
421 if severity
422 do (when condition
423 (signal condition))
424 (setq condition
425 (make-condition 'compiler-condition
426 :severity severity
427 :message ""
428 :location `(:location (:file ,filename)
429 (:line ,message))))
430 else do (setf (message condition)
431 (format nil "~a~&~a" (message condition) message))
432 finally (when condition
433 (signal condition))))
434 ;; w-p = errors + warnings, f-p = errors + warnings - style warnings,
435 ;; where a result of 0 is replaced by NIL. It follows that w-p
436 ;; is non-NIL iff there was any note whatsoever and that f-p is
437 ;; non-NIL iff there was anything more severe than a style
438 ;; warning. This is completely ANSI compliant.
439 (declare (ignore w-p f-p))
440 (if (and fas-file load-p)
441 (load fas-file)
442 fas-file))))
443
444 (defimplementation compile-string-for-emacs (string &key buffer position)
445 (with-compilation-hooks ()
446 (let ((*package* *buffer-package*)
447 (*buffer-name* buffer)
448 (*buffer-offset* position))
449 (eval (from-string
450 (format nil "(funcall (compile nil '(lambda () ~A)))"
451 string))))))
452
453 ;;; Portable XREF from the CMU AI repository.
454
455 (setq xref::*handle-package-forms* '(cl:in-package))
456
457 (defun lookup-xrefs (finder name)
458 (xref-results-for-emacs (funcall finder (from-string name))))
459
460 (defimplementation who-calls (function-name)
461 (lookup-xrefs #'xref:list-callers function-name))
462
463 (defimplementation who-references (variable)
464 (lookup-xrefs #'xref:list-readers variable))
465
466 (defimplementation who-binds (variable)
467 (lookup-xrefs #'xref:list-setters variable))
468
469 (defimplementation who-sets (variable)
470 (lookup-xrefs #'xref:list-setters variable))
471
472 (defimplementation list-callers (symbol-name)
473 (lookup-xrefs #'xref:who-calls symbol-name))
474
475 (defimplementation list-callees (symbol-name)
476 (lookup-xrefs #'xref:list-callees symbol-name))
477
478 (defun xref-results-for-emacs (fspecs)
479 (let ((xrefs '()))
480 (dolist (fspec fspecs)
481 (dolist (location (fspec-source-locations fspec))
482 (push (cons (to-string fspec) location) xrefs)))
483 (group-xrefs xrefs)))
484
485 (when (find-package :swank-loader)
486 (setf (symbol-function (intern "USER-INIT-FILE" :swank-loader))
487 (lambda ()
488 (let ((home (user-homedir-pathname)))
489 (and (ext:probe-directory home)
490 (probe-file (format nil "~A/.swank.lisp"
491 (namestring (truename home)))))))))
492
493 ;; Don't set *debugger-hook* to nil on break.
494 (ext:without-package-lock ()
495 (defun break (&optional (format-string "Break") &rest args)
496 (if (not sys::*use-clcs*)
497 (progn
498 (terpri *error-output*)
499 (apply #'format *error-output*
500 (concatenate 'string "*** - " format-string)
501 args)
502 (funcall ext:*break-driver* t))
503 (let ((condition
504 (make-condition 'simple-condition
505 :format-control format-string
506 :format-arguments args))
507 ;;(*debugger-hook* nil)
508 ;; Issue 91
509 )
510 (ext:with-restarts
511 ((CONTINUE
512 :report (lambda (stream)
513 (format stream (sys::TEXT "Return from ~S loop")
514 'break))
515 ()))
516 (with-condition-restarts condition (list (find-restart 'CONTINUE))
517 (invoke-debugger condition)))))
518 nil))
519
520 ;;; Local Variables:
521 ;;; eval: (put 'compile-file-frobbing-notes 'lisp-indent-function 1)
522 ;;; End:

  ViewVC Help
Powered by ViewVC 1.1.5