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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5