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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (show annotations)
Wed Dec 15 22:35:20 2004 UTC (9 years, 4 months ago) by heller
Branch: MAIN
Changes since 1.44: +4 -4 lines
(*buffer-name* *buffer-offset*): Move definitions upward before the
first use.
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-backend)
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 ;;;; if this listp has the complete CLOS then we use it, othewise we
36 ;;;; build up a "fake" swank-mop and then overide the methods in the
37 ;;;; inspector.
38
39 (eval-when (:compile-toplevel :load-toplevel :execute)
40 (defvar *have-mop*
41 (and (find-package :clos)
42 (eql :external
43 (nth-value 1 (find-symbol (string ':standard-slot-definition)
44 :clos))))
45 "True in those CLISP imagse which have a complete MOP implementation."))
46
47 #+#.(cl:if swank-backend::*have-mop* '(cl:and) '(cl:or))
48 (progn
49 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
50
51 (defun swank-mop:slot-definition-documentation (slot)
52 (clos::slot-definition-documentation slot)))
53
54 #-#.(cl:if swank-backend::*have-mop* '(and) '(or))
55 (defclass swank-mop:standard-slot-definition ()
56 ()
57 (:documentation
58 "Dummy class created so that swank.lisp will compile and load."))
59
60 #+linux
61 (defmacro with-blocked-signals ((&rest signals) &body body)
62 (ext:with-gensyms ("SIGPROCMASK" ret mask)
63 `(multiple-value-bind (,ret ,mask)
64 (linux:sigprocmask-set-n-save
65 ,linux:SIG_BLOCK
66 ,(do ((sigset (linux:sigset-empty)
67 (linux:sigset-add sigset (the fixnum (pop signals)))))
68 ((null signals) sigset)))
69 (linux:check-res ,ret 'linux:sigprocmask-set-n-save)
70 (unwind-protect
71 (progn ,@body)
72 (linux:sigprocmask-set ,linux:SIG_SETMASK ,mask nil)))))
73
74 ;; XXX currently only works in CVS version. 2.32 breaks.
75 ;; #+linux
76 ;; (defimplementation call-without-interrupts (fn)
77 ;; (with-blocked-signals (#.linux:SIGINT) (funcall fn)))
78 ;;
79 ;; #-linux
80 (defimplementation call-without-interrupts (fn)
81 (funcall fn))
82
83 #+unix
84 (defmethod getpid ()
85 (system::program-id))
86 #+win32
87 (defmethod getpid ()
88 (cond ((find-package :win32)
89 (funcall (find-symbol "GetCurrentProcessId" :win32)))
90 (t
91 (system::getenv "PID"))))
92
93 ;; the above is likely broken; we need windows NT users!
94
95 (defimplementation lisp-implementation-type-name ()
96 "clisp")
97
98 (defimplementation set-default-directory (directory)
99 (setf (ext:default-directory) directory)
100 (namestring (setf *default-pathname-defaults* (ext:default-directory))))
101
102
103 ;;; TCP Server
104
105 (defimplementation create-socket (host port)
106 (declare (ignore host))
107 (socket:socket-server port))
108
109 (defimplementation local-port (socket)
110 (socket:socket-server-port socket))
111
112 (defimplementation close-socket (socket)
113 (socket:socket-server-close socket))
114
115 (defun find-encoding (external-format)
116 (ecase external-format
117 (:iso-latin-1-unix (ext:make-encoding :charset 'charset:iso-8859-1
118 :line-terminator :unix))
119 (:utf-8-unix (ext:make-encoding :charset 'charset:utf-8
120 :line-terminator :unix))))
121
122 (defimplementation accept-connection (socket
123 &key (external-format :iso-latin-1-unix))
124 (socket:socket-accept socket
125 :buffered nil ;; XXX should be t
126 :element-type 'character
127 :external-format (find-encoding external-format)))
128
129 ;;; Swank functions
130
131 (defimplementation arglist (fname)
132 (block nil
133 (or (ignore-errors (return (ext:arglist fname)))
134 :not-available)))
135
136 (defimplementation macroexpand-all (form)
137 (ext:expand-form form))
138
139 (defimplementation describe-symbol-for-emacs (symbol)
140 "Return a plist describing SYMBOL.
141 Return NIL if the symbol is unbound."
142 (let ((result ()))
143 (labels ((doc (kind)
144 (or (documentation symbol kind) :not-documented))
145 (maybe-push (property value)
146 (when value
147 (setf result (list* property value result)))))
148 (when (fboundp symbol)
149 (if (macro-function symbol)
150 (setf (getf result :macro) (doc 'function))
151 (setf (getf result :function) (doc 'function))))
152 (maybe-push :variable (when (boundp symbol) (doc 'variable)))
153 (maybe-push :class (when (find-class symbol nil)
154 (doc 'type))) ;this should be fixed
155 result)))
156
157 (defimplementation describe-definition (symbol namespace)
158 (ecase namespace
159 (:variable (describe symbol))
160 (:macro (describe (macro-function symbol)))
161 (:function (describe (symbol-function symbol)))
162 (:class (describe (find-class symbol)))))
163
164 (defun fspec-pathname (symbol)
165 (let ((path (getf (gethash symbol sys::*documentation*) 'sys::file)))
166 (if (and path
167 (member (pathname-type path)
168 custom:*compiled-file-types* :test #'string=))
169 (loop for suffix in custom:*source-file-types*
170 thereis (make-pathname :defaults path :type suffix))
171 path)))
172
173 (defun fspec-location (fspec)
174 (let ((file (fspec-pathname fspec)))
175 (cond (file
176 (multiple-value-bind (truename c) (ignore-errors (truename file))
177 (cond (truename
178 (make-location (list :file (namestring truename))
179 (list :function-name (string fspec))))
180 (t (list :error (princ-to-string c))))))
181 (t (list :error (format nil "No source information available for: ~S"
182 fspec))))))
183
184 (defimplementation find-definitions (name)
185 (list (list name (fspec-location name))))
186
187 (defvar *sldb-topframe*)
188 (defvar *sldb-botframe*)
189 (defvar *sldb-source*)
190 (defvar *sldb-debugmode* 4)
191
192 (defun frame-down (frame)
193 (sys::frame-down-1 frame sys::*debug-mode*))
194
195 (defun frame-up (frame)
196 (sys::frame-up-1 frame sys::*debug-mode*))
197
198 (defimplementation call-with-debugging-environment (debugger-loop-fn)
199 (let* ((sys::*break-count* (1+ sys::*break-count*))
200 (sys::*driver* debugger-loop-fn)
201 (sys::*fasoutput-stream* nil)
202 (sys::*frame-limit1* (sys::frame-limit1 0))
203 (sys::*frame-limit2* (sys::frame-limit2))
204 (sys::*debug-mode* *sldb-debugmode*)
205 (*sldb-topframe* sys::*frame-limit1*))
206 (funcall debugger-loop-fn)))
207
208 (defun nth-frame (index)
209 (loop for frame = *sldb-topframe* then (frame-up frame)
210 repeat index
211 finally (return frame)))
212
213 (defimplementation compute-backtrace (start end)
214 (let ((end (or end most-positive-fixnum)))
215 (loop for last = nil then frame
216 for frame = (nth-frame start) then (frame-up frame)
217 for i from start below end
218 until (or (eq frame last) (system::driver-frame-p frame))
219 collect frame)))
220
221 (defimplementation print-frame (frame stream)
222 (write-string (string-left-trim '(#\Newline)
223 (with-output-to-string (stream)
224 (sys::describe-frame stream frame)))
225 stream))
226
227 (defimplementation eval-in-frame (form frame-number)
228 (sys::eval-at (nth-frame frame-number) form))
229
230 (defimplementation frame-locals (frame-number)
231 (let* ((frame (nth-frame frame-number))
232 (frame-env (sys::eval-at frame '(sys::the-environment))))
233 (append
234 (frame-do-venv frame (svref frame-env 0))
235 (frame-do-fenv frame (svref frame-env 1))
236 (frame-do-benv frame (svref frame-env 2))
237 (frame-do-genv frame (svref frame-env 3))
238 (frame-do-denv frame (svref frame-env 4)))))
239
240 (defimplementation frame-var-value (frame var)
241 (getf (nth var (frame-locals frame)) :value))
242
243 ;; Interpreter-Variablen-Environment has the shape
244 ;; NIL or #(v1 val1 ... vn valn NEXT-ENV).
245
246 (defun frame-do-venv (frame venv)
247 (loop for i from 1 below (length venv) by 2
248 as symbol = (svref venv (1- i))
249 and value = (svref venv i)
250 collect (list :name symbol :id 0
251 :value (if (eq sys::specdecl value)
252 ;; special variable
253 (sys::eval-at frame symbol)
254 ;; lexical variable or symbol macro
255 value))))
256
257 (defun frame-do-fenv (frame fenv)
258 (declare (ignore frame fenv))
259 nil)
260
261 (defun frame-do-benv (frame benv)
262 (declare (ignore frame benv))
263 nil)
264
265 (defun frame-do-genv (frame genv)
266 (declare (ignore frame genv))
267 nil)
268
269 (defun frame-do-denv (frame denv)
270 (declare (ignore frame denv))
271 nil)
272
273 (defimplementation frame-catch-tags (index)
274 (declare (ignore index))
275 nil)
276
277 (defimplementation return-from-frame (index form)
278 (sys::return-from-eval-frame (nth-frame index) form))
279
280 (defimplementation restart-frame (index)
281 (sys::redo-eval-frame (nth-frame index)))
282
283 (defimplementation frame-source-location-for-emacs (index)
284 (let ((f (nth-frame index)))
285 (list :error (format nil "Cannot find source for frame: ~A ~A ~A"
286 f
287 (sys::eval-frame-p f)
288 (sys::the-frame)))))
289
290 ;;; Profiling
291
292 (defimplementation profile (fname)
293 (eval `(mon:monitor ,fname))) ;monitor is a macro
294
295 (defimplementation profiled-functions ()
296 mon:*monitored-functions*)
297
298 (defimplementation unprofile (fname)
299 (eval `(mon:unmonitor ,fname))) ;unmonitor is a macro
300
301 (defimplementation unprofile-all ()
302 (mon:unmonitor))
303
304 (defimplementation profile-report ()
305 (mon:report-monitoring))
306
307 (defimplementation profile-reset ()
308 (mon:reset-all-monitoring))
309
310 (defimplementation profile-package (package callers-p methods)
311 (declare (ignore callers-p methods))
312 (mon:monitor-all package))
313
314 ;;; Handle compiler conditions (find out location of error etc.)
315
316 (defmacro compile-file-frobbing-notes ((&rest args) &body body)
317 "Pass ARGS to COMPILE-FILE, send the compiler notes to
318 *STANDARD-INPUT* and frob them in BODY."
319 `(let ((*error-output* (make-string-output-stream))
320 (*compile-verbose* t))
321 (multiple-value-prog1
322 (compile-file ,@args)
323 (handler-case
324 (with-input-from-string
325 (*standard-input* (get-output-stream-string *error-output*))
326 ,@body)
327 (sys::simple-end-of-file () nil)))))
328
329 (defvar *orig-c-warn* (symbol-function 'system::c-warn))
330 (defvar *orig-c-style-warn* (symbol-function 'system::c-style-warn))
331 (defvar *orig-c-error* (symbol-function 'system::c-error))
332 (defvar *orig-c-report-problems* (symbol-function 'system::c-report-problems))
333
334 (defmacro dynamic-flet (names-functions &body body)
335 "(dynamic-flet ((NAME FUNCTION) ...) BODY ...)
336 Execute BODY with NAME's funtion slot set to FUNCTION."
337 `(ext:letf* ,(loop for (name function) in names-functions
338 collect `((symbol-function ',name) ,function))
339 ,@body))
340
341 (defvar *buffer-name* nil)
342 (defvar *buffer-offset*)
343
344 (defun compiler-note-location ()
345 "Return the current compiler location."
346 (let ((lineno1 sys::*compile-file-lineno1*)
347 (lineno2 sys::*compile-file-lineno2*)
348 (file sys::*compile-file-truename*))
349 (cond ((and file lineno1 lineno2)
350 (make-location (list ':file (namestring file))
351 (list ':line lineno1)))
352 (*buffer-name*
353 (make-location (list ':buffer *buffer-name*)
354 (list ':position *buffer-offset*)))
355 (t
356 (list :error "No error location available")))))
357
358 (defun signal-compiler-warning (cstring args severity orig-fn)
359 (signal (make-condition 'compiler-condition
360 :severity severity
361 :message (apply #'format nil cstring args)
362 :location (compiler-note-location)))
363 (apply orig-fn cstring args))
364
365 (defun c-warn (cstring &rest args)
366 (signal-compiler-warning cstring args :warning *orig-c-warn*))
367
368 (defun c-style-warn (cstring &rest args)
369 (dynamic-flet ((sys::c-warn *orig-c-warn*))
370 (signal-compiler-warning cstring args :style-warning *orig-c-style-warn*)))
371
372 (defun c-error (cstring &rest args)
373 (signal-compiler-warning cstring args :error *orig-c-error*))
374
375 (defimplementation call-with-compilation-hooks (function)
376 (handler-bind ((warning #'handle-notification-condition))
377 (dynamic-flet ((system::c-warn #'c-warn)
378 (system::c-style-warn #'c-style-warn)
379 (system::c-error #'c-error))
380 (funcall function))))
381
382 (defun handle-notification-condition (condition)
383 "Handle a condition caused by a compiler warning."
384 (signal (make-condition 'compiler-condition
385 :original-condition condition
386 :severity :warning
387 :message (princ-to-string condition)
388 :location (compiler-note-location))))
389
390 (defimplementation swank-compile-file (filename load-p)
391 (with-compilation-hooks ()
392 (with-compilation-unit ()
393 (let ((fasl-file (compile-file filename)))
394 (when (and load-p fasl-file)
395 (load fasl-file))
396 nil))))
397
398 (defimplementation swank-compile-string (string &key buffer position directory)
399 (declare (ignore directory))
400 (with-compilation-hooks ()
401 (let ((*buffer-name* buffer)
402 (*buffer-offset* position))
403 (funcall (compile nil (read-from-string
404 (format nil "(~S () ~A)" 'lambda string)))))))
405
406 ;;; Portable XREF from the CMU AI repository.
407
408 (setq pxref::*handle-package-forms* '(cl:in-package))
409
410 (defmacro defxref (name function)
411 `(defimplementation ,name (name)
412 (xref-results (,function name))))
413
414 (defxref who-calls pxref:list-callers)
415 (defxref who-references pxref:list-readers)
416 (defxref who-binds pxref:list-setters)
417 (defxref who-sets pxref:list-setters)
418 (defxref list-callers pxref:list-callers)
419 (defxref list-callees pxref:list-callees)
420
421 (defun xref-results (symbols)
422 (let ((xrefs '()))
423 (dolist (symbol symbols)
424 (push (list symbol (fspec-location symbol)) xrefs))
425 xrefs))
426
427 (when (find-package :swank-loader)
428 (setf (symbol-function (intern "USER-INIT-FILE" :swank-loader))
429 (lambda ()
430 (let ((home (user-homedir-pathname)))
431 (and (ext:probe-directory home)
432 (probe-file (format nil "~A/.swank.lisp"
433 (namestring (truename home)))))))))
434
435 ;; Don't set *debugger-hook* to nil on break.
436 (ext:without-package-lock ()
437 (defun break (&optional (format-string "Break") &rest args)
438 (if (not sys::*use-clcs*)
439 (progn
440 (terpri *error-output*)
441 (apply #'format *error-output*
442 (concatenate 'string "*** - " format-string)
443 args)
444 (funcall ext:*break-driver* t))
445 (let ((condition
446 (make-condition 'simple-condition
447 :format-control format-string
448 :format-arguments args))
449 ;;(*debugger-hook* nil)
450 ;; Issue 91
451 )
452 (ext:with-restarts
453 ((continue
454 :report (lambda (stream)
455 (format stream (sys::text "Return from ~S loop")
456 'break))
457 ()))
458 (with-condition-restarts condition (list (find-restart 'continue))
459 (invoke-debugger condition)))))
460 nil))
461
462 ;;; Inspecting
463
464 (defclass clisp-inspector (inspector)
465 ())
466
467 (defimplementation make-default-inspector ()
468 (make-instance 'clisp-inspector))
469
470 (defmethod inspect-for-emacs ((o t) (inspector clisp-inspector))
471 (declare (ignore inspector))
472 (let* ((*print-array* nil) (*print-pretty* t)
473 (*print-circle* t) (*print-escape* t)
474 (*print-lines* custom:*inspect-print-lines*)
475 (*print-level* custom:*inspect-print-level*)
476 (*print-length* custom:*inspect-print-length*)
477 (sys::*inspect-all* (make-array 10 :fill-pointer 0 :adjustable t))
478 (tmp-pack (make-package (gensym "INSPECT-TMP-PACKAGE-")))
479 (*package* tmp-pack)
480 (sys::*inspect-unbound-value* (intern "#<unbound>" tmp-pack)))
481 (let ((inspection (sys::inspect-backend o)))
482 (values (format nil "~S~% ~A~{~%~A~}" o
483 (sys::insp-title inspection)
484 (sys::insp-blurb inspection))
485 (loop with count = (sys::insp-num-slots inspection)
486 for i upto count
487 for (value name) = (multiple-value-list
488 (funcall (sys::insp-nth-slot
489 inspection) i))
490 collect `((:value ,name) " = " (:value ,value)
491 (:newline)))))))
492
493 (defimplementation quit-lisp ()
494 #+lisp=cl (ext:quit)
495 #-lisp=cl (lisp:quit))
496
497 ;;; Local Variables:
498 ;;; eval: (put 'compile-file-frobbing-notes 'lisp-indent-function 1)
499 ;;; eval: (put 'dynamic-flet 'common-lisp-indent-function 1)
500 ;;; End:

  ViewVC Help
Powered by ViewVC 1.1.5