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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.60 - (show annotations)
Sun Oct 19 20:03:34 2008 UTC (5 years, 5 months ago) by heller
Branch: MAIN
Changes since 1.59: +0 -4 lines
* swank.lisp (frame-locals-and-catch-tags): New function.
Fetch locals and catch tags with a single RPC.

* slime.el (sldb-frame-details): Use it.

* swank-backend.lisp (frame-catch-tags): Provide a default
implementation.  Delete the dummy defs in various backends.
1 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;*"; -*-
2 ;;;
3 ;;; swank-abcl.lisp --- Armedbear CL specific code for SLIME.
4 ;;;
5 ;;; Adapted from swank-acl.lisp, Andras Simon, 2004
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 :collect) ;just so that it doesn't spoil the flying letters
15 (require :pprint))
16
17 (defun sys::break (&optional (format-control "BREAK called")
18 &rest format-arguments)
19 (let ((*saved-backtrace* (backtrace-as-list-ignoring-swank-calls)))
20 (with-simple-restart (continue "Return from BREAK.")
21 (invoke-debugger
22 (sys::%make-condition 'simple-condition
23 (list :format-control format-control
24 :format-arguments format-arguments))))
25 nil))
26
27 (defimplementation make-output-stream (write-string)
28 (ext:make-slime-output-stream write-string))
29
30 (defimplementation make-input-stream (read-string)
31 (ext:make-slime-input-stream read-string
32 (make-synonym-stream '*standard-output*)))
33
34 (defimplementation call-with-compilation-hooks (function)
35 (funcall function))
36
37 ;;; swank-mop
38
39 ;;dummies and definition
40
41 (defclass standard-slot-definition ()())
42
43 ;(defun class-finalized-p (class) t)
44
45 (defun slot-definition-documentation (slot) #+nil (documentation slot 't))
46 (defun slot-definition-type (slot) t)
47 (defun class-prototype (class))
48 (defun generic-function-declarations (gf))
49 (defun specializer-direct-methods (spec) (mop::class-direct-methods spec))
50
51 (defun slot-definition-name (slot)
52 (mop::%slot-definition-name slot))
53
54 (defun class-slots (class)
55 (mop::%class-slots class))
56
57 (defun method-generic-function (method)
58 (mop::%method-generic-function method))
59
60 (defun method-function (method)
61 (mop::%method-function method))
62
63 (defun slot-boundp-using-class (class object slotdef)
64 (system::slot-boundp object (slot-definition-name slotdef)))
65
66 (defun slot-value-using-class (class object slotdef)
67 (system::slot-value object (slot-definition-name slotdef)))
68
69 (import-to-swank-mop
70 '( ;; classes
71 cl:standard-generic-function
72 standard-slot-definition ;;dummy
73 cl:method
74 cl:standard-class
75 ;; standard-class readers
76 mop::class-default-initargs
77 mop::class-direct-default-initargs
78 mop::class-direct-slots
79 mop::class-direct-subclasses
80 mop::class-direct-superclasses
81 mop::eql-specializer
82 mop::class-finalized-p
83 cl:class-name
84 mop::class-precedence-list
85 class-prototype ;;dummy
86 class-slots
87 specializer-direct-methods
88 ;; eql-specializer accessors
89 mop::eql-specializer-object
90 ;; generic function readers
91 mop::generic-function-argument-precedence-order
92 generic-function-declarations ;;dummy
93 mop::generic-function-lambda-list
94 mop::generic-function-methods
95 mop::generic-function-method-class
96 mop::generic-function-method-combination
97 mop::generic-function-name
98 ;; method readers
99 method-generic-function
100 method-function
101 mop::method-lambda-list
102 mop::method-specializers
103 mop::method-qualifiers
104 ;; slot readers
105 mop::slot-definition-allocation
106 slot-definition-documentation ;;dummy
107 mop::slot-definition-initargs
108 mop::slot-definition-initform
109 mop::slot-definition-initfunction
110 slot-definition-name
111 slot-definition-type ;;dummy
112 mop::slot-definition-readers
113 mop::slot-definition-writers
114 slot-boundp-using-class
115 slot-value-using-class
116 ))
117
118 ;;;; TCP Server
119
120
121 (defimplementation preferred-communication-style ()
122 nil)
123
124 (defimplementation create-socket (host port)
125 (ext:make-server-socket port))
126
127 (defimplementation local-port (socket)
128 (java:jcall (java:jmethod "java.net.ServerSocket" "getLocalPort") socket))
129
130 (defimplementation close-socket (socket)
131 (ext:server-socket-close socket))
132
133 (defimplementation accept-connection (socket
134 &key external-format buffering timeout)
135 (declare (ignore buffering timeout external-format))
136 (ext:get-socket-stream (ext:socket-accept socket)))
137
138 ;;;; Unix signals
139
140 (defimplementation call-without-interrupts (fn)
141 (funcall fn))
142
143 (defimplementation getpid ()
144 (handler-case
145 (let* ((runtime
146 (java:jstatic "getRuntime" "java.lang.Runtime"))
147 (command
148 (java:jnew-array-from-array
149 "java.lang.String" #("sh" "-c" "echo $PPID")))
150 (runtime-exec-jmethod
151 ;; Complicated because java.lang.Runtime.exec() is
152 ;; overloaded on a non-primitive type (array of
153 ;; java.lang.String), so we have to use the actual
154 ;; parameter instance to get java.lang.Class
155 (java:jmethod "java.lang.Runtime" "exec"
156 (java:jcall
157 (java:jmethod "java.lang.Object" "getClass")
158 command)))
159 (process
160 (java:jcall runtime-exec-jmethod runtime command))
161 (output
162 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
163 process)))
164 (java:jcall (java:jmethod "java.lang.Process" "waitFor")
165 process)
166 (loop :with b :do
167 (setq b
168 (java:jcall (java:jmethod "java.io.InputStream" "read")
169 output))
170 :until (member b '(-1 #x0a)) ; Either EOF or LF
171 :collecting (code-char b) :into result
172 :finally (return
173 (parse-integer (coerce result 'string)))))
174 (t () 0)))
175
176 (defimplementation lisp-implementation-type-name ()
177 "armedbear")
178
179 (defimplementation set-default-directory (directory)
180 (let ((dir (sys::probe-directory directory)))
181 (when dir (setf *default-pathname-defaults* dir))
182 (namestring dir)))
183
184
185 ;;;; Misc
186
187 (defimplementation arglist (fun)
188 (cond ((symbolp fun)
189 (multiple-value-bind (arglist present) (sys::arglist fun)
190 (if present arglist :not-available)))
191 (t :not-available)))
192
193 (defimplementation function-name (function)
194 (nth-value 2 (function-lambda-expression function)))
195
196 (defimplementation macroexpand-all (form)
197 (macroexpand form))
198
199 (defimplementation describe-symbol-for-emacs (symbol)
200 (let ((result '()))
201 (flet ((doc (kind &optional (sym symbol))
202 (or (documentation sym kind) :not-documented))
203 (maybe-push (property value)
204 (when value
205 (setf result (list* property value result)))))
206 (maybe-push
207 :variable (when (boundp symbol)
208 (doc 'variable)))
209 (maybe-push
210 :function (if (fboundp symbol)
211 (doc 'function)))
212 (maybe-push
213 :class (if (find-class symbol nil)
214 (doc 'class)))
215 result)))
216
217
218 (defimplementation describe-definition (symbol namespace)
219 (ecase namespace
220 (:variable
221 (describe symbol))
222 ((:function :generic-function)
223 (describe (symbol-function symbol)))
224 (:class
225 (describe (find-class symbol)))))
226
227 (defimplementation describe-definition (symbol namespace)
228 (ecase namespace
229 (:variable
230 (describe symbol))
231 ((:function :generic-function)
232 (describe (symbol-function symbol)))
233 (:class
234 (describe (find-class symbol)))))
235
236
237 ;;;; Debugger
238
239 (defvar *sldb-topframe*)
240
241 (defun backtrace-as-list-ignoring-swank-calls ()
242 (let ((list (ext:backtrace-as-list)))
243 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
244
245 (defimplementation call-with-debugging-environment (debugger-loop-fn)
246 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
247 (funcall debugger-loop-fn)))
248
249 (defun nth-frame (index)
250 (nth index (backtrace-as-list-ignoring-swank-calls)))
251
252 (defimplementation compute-backtrace (start end)
253 (let ((end (or end most-positive-fixnum)))
254 (loop for f in (subseq (backtrace-as-list-ignoring-swank-calls) start end)
255 collect f)))
256
257 (defimplementation print-frame (frame stream)
258 (write-string (string-trim '(#\space #\newline)
259 (prin1-to-string frame))
260 stream))
261
262 (defimplementation frame-locals (index)
263 `(,(list :name "??" :id 0 :value "??")))
264
265 #+nil
266 (defimplementation disassemble-frame (index)
267 (disassemble (debugger:frame-function (nth-frame index))))
268
269 (defimplementation frame-source-location-for-emacs (index)
270 (list :error (format nil "Cannot find source for frame: ~A"
271 (nth-frame index))))
272
273 #+nil
274 (defimplementation eval-in-frame (form frame-number)
275 (debugger:eval-form-in-context
276 form
277 (debugger:environment-of-frame (nth-frame frame-number))))
278
279 #+nil
280 (defimplementation return-from-frame (frame-number form)
281 (let ((frame (nth-frame frame-number)))
282 (multiple-value-call #'debugger:frame-return
283 frame (debugger:eval-form-in-context
284 form
285 (debugger:environment-of-frame frame)))))
286
287 ;;; XXX doesn't work for frames with arguments
288 #+nil
289 (defimplementation restart-frame (frame-number)
290 (let ((frame (nth-frame frame-number)))
291 (debugger:frame-retry frame (debugger:frame-function frame))))
292
293 ;;;; Compiler hooks
294
295 (defvar *buffer-name* nil)
296 (defvar *buffer-start-position*)
297 (defvar *buffer-string*)
298 (defvar *compile-filename*)
299
300 (in-package :swank-backend)
301
302 (defun handle-compiler-warning (condition)
303 (let ((loc (when (and jvm::*compile-file-pathname*
304 system::*source-position*)
305 (cons jvm::*compile-file-pathname* system::*source-position*))))
306 ;; filter condition signaled more than once.
307 (unless (member condition *abcl-signaled-conditions*)
308 (push condition *abcl-signaled-conditions*)
309 (signal (make-condition
310 'compiler-condition
311 :original-condition condition
312 :severity :warning
313 :message (format nil "~A" condition)
314 :location (cond (*buffer-name*
315 (make-location
316 (list :buffer *buffer-name*)
317 (list :offset *buffer-start-position* 0)))
318 (loc
319 (destructuring-bind (file . pos) loc
320 (make-location
321 (list :file (namestring (truename file)))
322 (list :position (1+ pos)))))
323 (t
324 (make-location
325 (list :file (namestring *compile-filename*))
326 (list :position 1)))))))))
327
328 (defvar *abcl-signaled-conditions*)
329
330 (defimplementation swank-compile-file (filename load-p external-format)
331 (declare (ignore external-format))
332 (let ((jvm::*resignal-compiler-warnings* t)
333 (*abcl-signaled-conditions* nil))
334 (handler-bind ((warning #'handle-compiler-warning))
335 (let ((*buffer-name* nil)
336 (*compile-filename* filename))
337 (multiple-value-bind (fn warn fail) (compile-file filename)
338 (values fn warn
339 (or fail
340 (and load-p
341 (not (load fn))))))))))
342
343 (defimplementation swank-compile-string (string &key buffer position directory
344 debug)
345 (declare (ignore directory debug))
346 (let ((jvm::*resignal-compiler-warnings* t)
347 (*abcl-signaled-conditions* nil))
348 (handler-bind ((warning #'handle-compiler-warning))
349 (let ((*buffer-name* buffer)
350 (*buffer-start-position* position)
351 (*buffer-string* string))
352 (funcall (compile nil (read-from-string
353 (format nil "(~S () ~A)" 'lambda string))))
354 t))))
355
356 #|
357 ;;;; Definition Finding
358
359 (defun find-fspec-location (fspec type)
360 (let ((file (excl::fspec-pathname fspec type)))
361 (etypecase file
362 (pathname
363 (let ((start (scm:find-definition-in-file fspec type file)))
364 (make-location (list :file (namestring (truename file)))
365 (if start
366 (list :position (1+ start))
367 (list :function-name (string fspec))))))
368 ((member :top-level)
369 (list :error (format nil "Defined at toplevel: ~A" fspec)))
370 (null
371 (list :error (format nil "Unkown source location for ~A" fspec))))))
372
373 (defun fspec-definition-locations (fspec)
374 (let ((defs (excl::find-multiple-definitions fspec)))
375 (loop for (fspec type) in defs
376 collect (list fspec (find-fspec-location fspec type)))))
377
378 (defimplementation find-definitions (symbol)
379 (fspec-definition-locations symbol))
380
381 |#
382
383 (defun source-location (symbol)
384 (when (pathnamep (ext:source-pathname symbol))
385 `(((,symbol)
386 (:location
387 (:file ,(namestring (ext:source-pathname symbol)))
388 (:position ,(or (ext:source-file-position symbol) 1))
389 (:align t))))))
390
391
392 (defimplementation find-definitions (symbol)
393 (source-location symbol))
394
395 #|
396 Uncomment this if you have patched xref.lisp, as in
397 http://article.gmane.org/gmane.lisp.slime.devel/2425
398 Also, make sure that xref.lisp is loaded by modifying the armedbear
399 part of *sysdep-pathnames* in swank.loader.lisp.
400
401 ;;;; XREF
402 (setq pxref:*handle-package-forms* '(cl:in-package))
403
404 (defmacro defxref (name function)
405 `(defimplementation ,name (name)
406 (xref-results (,function name))))
407
408 (defxref who-calls pxref:list-callers)
409 (defxref who-references pxref:list-readers)
410 (defxref who-binds pxref:list-setters)
411 (defxref who-sets pxref:list-setters)
412 (defxref list-callers pxref:list-callers)
413 (defxref list-callees pxref:list-callees)
414
415 (defun xref-results (symbols)
416 (let ((xrefs '()))
417 (dolist (symbol symbols)
418 (push (list symbol (cadar (source-location symbol))) xrefs))
419 xrefs))
420 |#
421
422 ;;;; Inspecting
423
424 (defmethod emacs-inspect ((slot mop::slot-definition))
425 `("Name: " (:value ,(mop::%slot-definition-name slot))
426 (:newline)
427 "Documentation:" (:newline)
428 ,@(when (slot-definition-documentation slot)
429 `((:value ,(slot-definition-documentation slot)) (:newline)))
430 "Initialization:" (:newline)
431 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
432 " Form: " ,(if (mop::%slot-definition-initfunction slot)
433 `(:value ,(mop::%slot-definition-initform slot))
434 "#<unspecified>") (:newline)
435 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
436 (:newline)))
437
438 (defmethod emacs-inspect ((f function))
439 `(,@(when (function-name f)
440 `("Name: "
441 ,(princ-to-string (function-name f)) (:newline)))
442 ,@(multiple-value-bind (args present)
443 (sys::arglist f)
444 (when present `("Argument list: " ,(princ-to-string args) (:newline))))
445 (:newline)
446 #+nil,@(when (documentation f t)
447 `("Documentation:" (:newline) ,(documentation f t) (:newline)))
448 ,@(when (function-lambda-expression f)
449 `("Lambda expression:"
450 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline)))))
451
452 #|
453
454 (defmethod emacs-inspect ((o t))
455 (let* ((class (class-of o))
456 (slots (mop::class-slots class)))
457 (mapcar (lambda (slot)
458 (let ((name (mop::slot-definition-name slot)))
459 (cons (princ-to-string name)
460 (slot-value o name))))
461 slots)))
462 |#
463
464 ;;;; Multithreading
465
466 (defimplementation startup-multiprocessing ()
467 #+nil(mp:start-scheduler))
468
469 (defimplementation spawn (fn &key name)
470 (ext:make-thread (lambda () (funcall fn)) :name name))
471
472 (defvar *thread-props-lock* (ext:make-thread-lock))
473
474 (defvar *thread-props* (make-hash-table) ; should be a weak table
475 "A hashtable mapping threads to a plist.")
476
477 (defvar *thread-id-counter* 0)
478
479 (defimplementation thread-id (thread)
480 (ext:with-thread-lock (*thread-props-lock*)
481 (or (getf (gethash thread *thread-props*) 'id)
482 (setf (getf (gethash thread *thread-props*) 'id)
483 (incf *thread-id-counter*)))))
484
485 (defimplementation find-thread (id)
486 (find id (all-threads)
487 :key (lambda (thread)
488 (getf (gethash thread *thread-props*) 'id))))
489
490 (defimplementation thread-name (thread)
491 (ext:thread-name thread))
492
493 (defimplementation thread-status (thread)
494 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
495
496 (defimplementation make-lock (&key name)
497 (ext:make-thread-lock))
498
499 (defimplementation call-with-lock-held (lock function)
500 (ext:with-thread-lock (lock) (funcall function)))
501
502 (defimplementation current-thread ()
503 (ext:current-thread))
504
505 (defimplementation all-threads ()
506 (copy-list (ext:mapcar-threads #'identity)))
507
508 (defimplementation interrupt-thread (thread fn)
509 (ext:interrupt-thread thread fn))
510
511 (defimplementation kill-thread (thread)
512 (ext:destroy-thread thread))
513
514 (defstruct mailbox
515 (mutex (ext:make-mutex))
516 (queue '()))
517
518 (defun mailbox (thread)
519 "Return THREAD's mailbox."
520 (ext:with-thread-lock (*thread-props-lock*)
521 (or (getf (gethash thread *thread-props*) 'mailbox)
522 (setf (getf (gethash thread *thread-props*) 'mailbox)
523 (make-mailbox)))))
524
525 (defimplementation send (thread object)
526 (let ((mbox (mailbox thread)))
527 (ext:with-mutex ((mailbox-mutex mbox))
528 (setf (mailbox-queue mbox)
529 (nconc (mailbox-queue mbox) (list message))))))
530
531 #+(or)
532 (defimplementation receive-if (thread &optional timeout)
533 (let* ((mbox (mailbox (current-thread))))
534 (assert (or (not timeout) (eq timeout t)))
535 (loop
536 (check-slime-interrupts)
537 (ext:with-mutex ((mailbox-mutex mbox))
538 (let* ((q (mailbox-queue mbox))
539 (tail (member-if test q)))
540 (when tail
541 (setf (mailbox-queue mbox) (nconc (ldiff q tail) (cdr tail)))
542 (return (car tail))))
543 (when (eq timeout t) (return (values nil t)))
544 ;;(java:jcall (java:jmethod "java.lang.Object" "wait")
545 ;; (mailbox-mutex mbox) 1000)
546 ))))
547
548 (defimplementation quit-lisp ()
549 (ext:exit))
550
551 ;; WORKAROUND: call/initialize accessors at load time
552 (let ((c (make-condition 'compiler-condition
553 :original-condition nil
554 :severity ':note :message "" :location nil))
555 (slots `(severity message short-message references location)))
556 (dolist (slot slots)
557 (funcall slot c)))

  ViewVC Help
Powered by ViewVC 1.1.5