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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.105 - (hide annotations)
Mon Aug 4 20:25:42 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.104: +3 -1 lines
Set interactive-p for CCL and ACL.
1 heller 1.79 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;* "; -*-
2 heller 1.1 ;;;
3     ;;; swank-allegro.lisp --- Allegro CL specific code for SLIME.
4     ;;;
5 heller 1.60 ;;; Created 2003
6 heller 1.1 ;;;
7     ;;; This code has been placed in the Public Domain. All warranties
8 heller 1.74 ;;; are disclaimed.
9 heller 1.1 ;;;
10    
11 heller 1.21 (in-package :swank-backend)
12    
13 heller 1.1 (eval-when (:compile-toplevel :load-toplevel :execute)
14     (require :sock)
15 heller 1.79 (require :process))
16 heller 1.1
17 heller 1.79 (import-from :excl *gray-stream-symbols* :swank-backend)
18 heller 1.1
19 mbaringer 1.53 ;;; swank-mop
20    
21 heller 1.60 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
22 mbaringer 1.53
23 mbaringer 1.54 (defun swank-mop:slot-definition-documentation (slot)
24 heller 1.62 (documentation slot t))
25 mbaringer 1.53
26 heller 1.79
27 heller 1.8 ;;;; TCP Server
28 heller 1.1
29 heller 1.21 (defimplementation preferred-communication-style ()
30     :spawn)
31 heller 1.12
32 heller 1.14 (defimplementation create-socket (host port)
33     (socket:make-socket :connect :passive :local-port port
34     :local-host host :reuse-address t))
35 heller 1.5
36 heller 1.10 (defimplementation local-port (socket)
37 heller 1.6 (socket:local-port socket))
38    
39 heller 1.10 (defimplementation close-socket (socket)
40 heller 1.6 (close socket))
41    
42 dcrosher 1.85 (defimplementation accept-connection (socket &key external-format buffering
43     timeout)
44     (declare (ignore buffering timeout))
45 heller 1.94 (let ((s (socket:accept-connection socket :wait t)))
46     (when external-format
47     (setf (stream-external-format s) external-format))
48 heller 1.63 s))
49 heller 1.6
50 heller 1.94 (defvar *external-format-to-coding-system*
51     '((:iso-8859-1
52     "latin-1" "latin-1-unix" "iso-latin-1-unix"
53     "iso-8859-1" "iso-8859-1-unix")
54     (:utf-8 "utf-8" "utf-8-unix")
55     (:euc-jp "euc-jp" "euc-jp-unix")
56     (:us-ascii "us-ascii" "us-ascii-unix")
57     (:emacs-mule "emacs-mule" "emacs-mule-unix")))
58    
59     (defimplementation find-external-format (coding-system)
60     (let ((e (rassoc-if (lambda (x) (member coding-system x :test #'equal))
61     *external-format-to-coding-system*)))
62     (and e (excl:crlf-base-ef
63     (excl:find-external-format (car e)
64     :try-variant t)))))
65 heller 1.64
66 heller 1.34 (defimplementation format-sldb-condition (c)
67     (princ-to-string c))
68    
69 heller 1.39 (defimplementation call-with-syntax-hooks (fn)
70     (funcall fn))
71    
72 heller 1.9 ;;;; Unix signals
73    
74 heller 1.10 (defimplementation call-without-interrupts (fn)
75 heller 1.9 (excl:without-interrupts (funcall fn)))
76    
77 heller 1.10 (defimplementation getpid ()
78 heller 1.8 (excl::getpid))
79 heller 1.6
80 heller 1.15 (defimplementation lisp-implementation-type-name ()
81     "allegro")
82    
83 pseibel 1.28 (defimplementation set-default-directory (directory)
84 mkoeppe 1.86 (let* ((dir (namestring (truename (merge-pathnames directory)))))
85     (setf *default-pathname-defaults* (pathname (excl:chdir dir)))
86 heller 1.66 dir))
87 pseibel 1.28
88 heller 1.35 (defimplementation default-directory ()
89 heller 1.66 (namestring (excl:current-directory)))
90 heller 1.35
91 heller 1.8 ;;;; Misc
92 heller 1.1
93 heller 1.21 (defimplementation arglist (symbol)
94 lgorrie 1.30 (handler-case (excl:arglist symbol)
95     (simple-error () :not-available)))
96 heller 1.21
97     (defimplementation macroexpand-all (form)
98     (excl::walk form))
99 heller 1.1
100 heller 1.10 (defimplementation describe-symbol-for-emacs (symbol)
101 heller 1.1 (let ((result '()))
102     (flet ((doc (kind &optional (sym symbol))
103     (or (documentation sym kind) :not-documented))
104     (maybe-push (property value)
105     (when value
106     (setf result (list* property value result)))))
107     (maybe-push
108     :variable (when (boundp symbol)
109     (doc 'variable)))
110     (maybe-push
111     :function (if (fboundp symbol)
112     (doc 'function)))
113     (maybe-push
114     :class (if (find-class symbol nil)
115     (doc 'class)))
116     result)))
117    
118 heller 1.20 (defimplementation describe-definition (symbol namespace)
119     (ecase namespace
120     (:variable
121     (describe symbol))
122     ((:function :generic-function)
123     (describe (symbol-function symbol)))
124     (:class
125     (describe (find-class symbol)))))
126 heller 1.10
127 lgorrie 1.43 (defimplementation make-stream-interactive (stream)
128 heller 1.105 (setf (interactive-stream-p stream) t)
129     (when (typep stream 'slime-output-stream)
130     (setf (slot-value stream 'interactive-p) t)))
131 lgorrie 1.43
132 heller 1.8 ;;;; Debugger
133    
134 heller 1.1 (defvar *sldb-topframe*)
135 heller 1.4
136 heller 1.10 (defimplementation call-with-debugging-environment (debugger-loop-fn)
137 heller 1.71 (let ((*sldb-topframe* (find-topframe))
138 heller 1.20 (excl::*break-hook* nil))
139 heller 1.4 (funcall debugger-loop-fn)))
140 heller 1.1
141 mkoeppe 1.92 (defimplementation sldb-break-at-start (fname)
142     ;; :print-before is kind of mis-used but we just want to stuff our break form
143     ;; somewhere. This does not work for setf, :before and :after methods, which
144     ;; need special syntax in the trace call, see ACL's doc/debugging.htm chapter 10.
145     (eval `(trace (,fname
146     :print-before
147     ((break "Function start breakpoint of ~A" ',fname)))))
148     `(:ok ,(format nil "Set breakpoint at start of ~S" fname)))
149    
150 heller 1.71 (defun find-topframe ()
151 heller 1.73 (let ((skip-frames 3))
152     (do ((f (excl::int-newest-frame) (next-frame f))
153     (i 0 (1+ i)))
154     ((= i skip-frames) f))))
155 heller 1.71
156 heller 1.42 (defun next-frame (frame)
157     (let ((next (excl::int-next-older-frame frame)))
158     (cond ((not next) nil)
159     ((debugger:frame-visible-p next) next)
160     (t (next-frame next)))))
161    
162 heller 1.1 (defun nth-frame (index)
163 heller 1.42 (do ((frame *sldb-topframe* (next-frame frame))
164 heller 1.1 (i index (1- i)))
165     ((zerop i) frame)))
166    
167 heller 1.20 (defimplementation compute-backtrace (start end)
168 heller 1.1 (let ((end (or end most-positive-fixnum)))
169 heller 1.42 (loop for f = (nth-frame start) then (next-frame f)
170 heller 1.1 for i from start below end
171     while f
172 heller 1.42 collect f)))
173 heller 1.1
174 heller 1.20 (defimplementation print-frame (frame stream)
175     (debugger:output-frame stream frame :moderate))
176 heller 1.4
177 heller 1.10 (defimplementation frame-locals (index)
178 heller 1.1 (let ((frame (nth-frame index)))
179     (loop for i from 0 below (debugger:frame-number-vars frame)
180 mbaringer 1.19 collect (list :name (debugger:frame-var-name frame i)
181 heller 1.1 :id 0
182 mbaringer 1.19 :value (debugger:frame-var-value frame i)))))
183 heller 1.1
184 heller 1.39 (defimplementation frame-var-value (frame var)
185     (let ((frame (nth-frame frame)))
186     (debugger:frame-var-value frame var)))
187    
188 heller 1.10 (defimplementation frame-catch-tags (index)
189 heller 1.1 (declare (ignore index))
190     nil)
191    
192 heller 1.21 (defimplementation disassemble-frame (index)
193     (disassemble (debugger:frame-function (nth-frame index))))
194    
195 heller 1.10 (defimplementation frame-source-location-for-emacs (index)
196 lgorrie 1.37 (let* ((frame (nth-frame index))
197     (expr (debugger:frame-expression frame))
198     (fspec (first expr)))
199     (second (first (fspec-definition-locations fspec)))))
200 heller 1.4
201 heller 1.10 (defimplementation eval-in-frame (form frame-number)
202 heller 1.73 (let ((frame (nth-frame frame-number)))
203     ;; let-bind lexical variables
204     (let ((vars (loop for i below (debugger:frame-number-vars frame)
205     for name = (debugger:frame-var-name frame i)
206     if (symbolp name)
207     collect `(,name ',(debugger:frame-var-value frame i)))))
208     (debugger:eval-form-in-context
209     `(let* ,vars ,form)
210     (debugger:environment-of-frame frame)))))
211 heller 1.10
212 heller 1.11 (defimplementation return-from-frame (frame-number form)
213     (let ((frame (nth-frame frame-number)))
214     (multiple-value-call #'debugger:frame-return
215     frame (debugger:eval-form-in-context
216 heller 1.20 form
217     (debugger:environment-of-frame frame)))))
218 heller 1.67
219 heller 1.11 (defimplementation restart-frame (frame-number)
220     (let ((frame (nth-frame frame-number)))
221 heller 1.69 (cond ((debugger:frame-retryable-p frame)
222     (apply #'debugger:frame-retry frame (debugger:frame-function frame)
223     (cdr (debugger:frame-expression frame))))
224     (t "Frame is not retryable"))))
225 heller 1.67
226 heller 1.8 ;;;; Compiler hooks
227    
228 heller 1.1 (defvar *buffer-name* nil)
229     (defvar *buffer-start-position*)
230     (defvar *buffer-string*)
231 lgorrie 1.33 (defvar *compile-filename* nil)
232 heller 1.1
233 heller 1.66 (defun compiler-note-p (object)
234     (member (type-of object) '(excl::compiler-note compiler::compiler-note)))
235    
236     (defun compiler-undefined-functions-called-warning-p (object)
237     (typep object 'excl:compiler-undefined-functions-called-warning))
238 heller 1.64
239     (deftype compiler-note ()
240     `(satisfies compiler-note-p))
241    
242 heller 1.66 (defun signal-compiler-condition (&rest args)
243     (signal (apply #'make-condition 'compiler-condition args)))
244    
245 heller 1.1 (defun handle-compiler-warning (condition)
246 heller 1.66 (declare (optimize (debug 3) (speed 0) (space 0)))
247     (cond ((and (not *buffer-name*)
248     (compiler-undefined-functions-called-warning-p condition))
249     (handle-undefined-functions-warning condition))
250     (t
251     (signal-compiler-condition
252     :original-condition condition
253     :severity (etypecase condition
254     (warning :warning)
255     (compiler-note :note))
256     :message (format nil "~A" condition)
257     :location (location-for-warning condition)))))
258    
259     (defun location-for-warning (condition)
260 heller 1.1 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
261 heller 1.66 (cond (*buffer-name*
262     (make-location
263     (list :buffer *buffer-name*)
264     (list :position *buffer-start-position*)))
265     (loc
266     (destructuring-bind (file . pos) loc
267     (make-location
268     (list :file (namestring (truename file)))
269     (list :position (1+ pos)))))
270     (t
271     (list :error "No error location available.")))))
272    
273     (defun handle-undefined-functions-warning (condition)
274     (let ((fargs (slot-value condition 'excl::format-arguments)))
275 heller 1.67 (loop for (fname . pos-file) in (car fargs) do
276     (loop for (pos file) in pos-file do
277     (signal-compiler-condition
278     :original-condition condition
279     :severity :warning
280     :message (format nil "Undefined function referenced: ~S"
281     fname)
282     :location (make-location (list :file file)
283     (list :position (1+ pos))))))))
284 lgorrie 1.33
285     (defimplementation call-with-compilation-hooks (function)
286 heller 1.64 (handler-bind ((warning #'handle-compiler-warning)
287 heller 1.70 ;;(compiler-note #'handle-compiler-warning)
288     )
289 lgorrie 1.33 (funcall function)))
290 heller 1.1
291 heller 1.94 (defimplementation swank-compile-file (filename load-p external-format)
292 lgorrie 1.33 (with-compilation-hooks ()
293 heller 1.74 (let ((*buffer-name* nil)
294 heller 1.94 (*compile-filename* filename))
295 heller 1.74 (compile-file *compile-filename* :load-after-compile load-p
296 heller 1.94 :external-format external-format))))
297 heller 1.1
298 heller 1.46 (defun call-with-temp-file (fn)
299     (let ((tmpname (system:make-temp-file-name)))
300     (unwind-protect
301     (with-open-file (file tmpname :direction :output :if-exists :error)
302     (funcall fn file tmpname))
303     (delete-file tmpname))))
304    
305     (defun compile-from-temp-file (string)
306     (call-with-temp-file
307     (lambda (stream filename)
308     (write-string string stream)
309     (finish-output stream)
310 mkoeppe 1.83 (let ((binary-filename
311     (excl:without-redefinition-warnings
312     ;; Suppress Allegro's redefinition warnings; they are
313     ;; pointless when we are compiling via a temporary
314     ;; file.
315     (compile-file filename :load-after-compile t))))
316 heller 1.46 (when binary-filename
317     (delete-file binary-filename))))))
318    
319 heller 1.102 (defimplementation swank-compile-string (string &key buffer position directory
320     debug)
321     (declare (ignore debug))
322 heller 1.46 ;; We store the source buffer in excl::*source-pathname* as a string
323 heller 1.50 ;; of the form <buffername>;<start-offset>. Quite ugly encoding, but
324 heller 1.46 ;; the fasl file is corrupted if we use some other datatype.
325 lgorrie 1.33 (with-compilation-hooks ()
326 heller 1.20 (let ((*buffer-name* buffer)
327 heller 1.1 (*buffer-start-position* position)
328 pseibel 1.51 (*buffer-string* string)
329     (*default-pathname-defaults*
330     (if directory (merge-pathnames (pathname directory))
331     *default-pathname-defaults*)))
332 heller 1.46 (compile-from-temp-file
333     (format nil "~S ~S~%~A"
334     `(in-package ,(package-name *package*))
335     `(eval-when (:compile-toplevel :load-toplevel)
336     (setq excl::*source-pathname*
337 heller 1.52 ',(format nil "~A;~D" buffer position)))
338 heller 1.46 string)))))
339 heller 1.1
340 heller 1.8 ;;;; Definition Finding
341    
342 heller 1.32 (defun fspec-primary-name (fspec)
343     (etypecase fspec
344 heller 1.62 (symbol fspec)
345     (list (fspec-primary-name (second fspec)))))
346 heller 1.32
347 heller 1.62 ;; If Emacs uses DOS-style eol conventions, \n\r are considered as a
348     ;; single character, but file-position counts them as two. Here we do
349     ;; our own conversion.
350     (defun count-cr (file pos)
351     (let* ((bufsize 256)
352 heller 1.63 (type '(unsigned-byte 8))
353     (buf (make-array bufsize :element-type type))
354 heller 1.62 (cr-count 0))
355 heller 1.63 (with-open-file (stream file :direction :input :element-type type)
356     (loop for bytes-read = (read-sequence buf stream) do
357     (incf cr-count (count (char-code #\return) buf
358     :end (min pos bytes-read)))
359     (decf pos bytes-read)
360     (when (<= pos 0)
361     (return cr-count))))))
362 heller 1.62
363 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
364     (let* ((part
365     (or (scm::find-definition-in-definition-group
366     fspec type (scm:section-file :file file)
367     :top-level top-level)
368     (scm::find-definition-in-definition-group
369     (fspec-primary-name fspec)
370     type (scm:section-file :file file)
371     :top-level top-level)))
372     (start (and part
373     (scm::source-part-start part)))
374 heller 1.60 (pos (if start
375 heller 1.62 (list :position (1+ (- start (count-cr file start))))
376     (list :function-name (string (fspec-primary-name fspec))))))
377     (make-location (list :file (namestring (truename file)))
378     pos)))
379 heller 1.60
380     (defun find-definition-in-buffer (filename)
381     (let ((pos (position #\; filename :from-end t)))
382     (make-location
383     (list :buffer (subseq filename 0 pos))
384     (list :position (parse-integer (subseq filename (1+ pos)))))))
385    
386 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
387     (etypecase file
388     (pathname
389     (find-definition-in-file fspec type file top-level))
390     ((member :top-level)
391     (list :error (format nil "Defined at toplevel: ~A"
392     (fspec->string fspec))))
393     (string
394     (find-definition-in-buffer file))))
395 lgorrie 1.49
396 heller 1.50 (defun fspec->string (fspec)
397 lgorrie 1.49 (etypecase fspec
398 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
399     (prin1-to-string fspec)))
400     (list (format nil "(~A ~A)"
401     (prin1-to-string (first fspec))
402     (let ((*package* (find-package :keyword)))
403     (prin1-to-string (second fspec)))))))
404 heller 1.21
405     (defun fspec-definition-locations (fspec)
406 mkoeppe 1.87 (cond
407     ((and (listp fspec)
408     (eql (car fspec) :top-level-form))
409 mkoeppe 1.98 (destructuring-bind (top-level-form file &optional position) fspec
410 mkoeppe 1.87 (list
411     (list (list nil fspec)
412     (make-location (list :buffer file)
413 heller 1.90 (list :position position t))))))
414     ((and (listp fspec) (eq (car fspec) :internal))
415     (destructuring-bind (_internal next _n) fspec
416     (fspec-definition-locations next)))
417 mkoeppe 1.87 (t
418     (let ((defs (excl::find-source-file fspec)))
419 trittweiler 1.103 (when (and (null defs)
420     (listp fspec)
421     (string= (car fspec) '#:method))
422     ;; If methods are defined in a defgeneric form, the source location is
423     ;; recorded for the gf but not for the methods. Therefore fall back to
424     ;; the gf as the likely place of definition.
425     (setq defs (excl::find-source-file (second fspec))))
426 mkoeppe 1.87 (if (null defs)
427     (list
428     (list (list nil fspec)
429     (list :error
430     (format nil "Unknown source location for ~A"
431     (fspec->string fspec)))))
432 mkoeppe 1.81 (loop for (fspec type file top-level) in defs
433 mkoeppe 1.87 collect (list (list type fspec)
434     (find-fspec-location fspec type file top-level))))))))
435 heller 1.21
436     (defimplementation find-definitions (symbol)
437     (fspec-definition-locations symbol))
438 heller 1.1
439 heller 1.8 ;;;; XREF
440    
441 heller 1.21 (defmacro defxref (name relation name1 name2)
442     `(defimplementation ,name (x)
443     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
444    
445     (defxref who-calls :calls :wild x)
446 heller 1.70 (defxref calls-who :calls x :wild)
447 heller 1.21 (defxref who-references :uses :wild x)
448     (defxref who-binds :binds :wild x)
449     (defxref who-macroexpands :macro-calls :wild x)
450     (defxref who-sets :sets :wild x)
451    
452     (defun xref-result (fspecs)
453     (loop for fspec in fspecs
454     append (fspec-definition-locations fspec)))
455 heller 1.46
456     ;; list-callers implemented by groveling through all fbound symbols.
457     ;; Only symbols are considered. Functions in the constant pool are
458 heller 1.71 ;; searched recursively. Closure environments are ignored at the
459 heller 1.46 ;; moment (constants in methods are therefore not found).
460    
461     (defun map-function-constants (function fn depth)
462     "Call FN with the elements of FUNCTION's constant pool."
463     (do ((i 0 (1+ i))
464     (max (excl::function-constant-count function)))
465     ((= i max))
466     (let ((c (excl::function-constant function i)))
467     (cond ((and (functionp c)
468     (not (eq c function))
469     (plusp depth))
470     (map-function-constants c fn (1- depth)))
471     (t
472     (funcall fn c))))))
473    
474 heller 1.64 (defun in-constants-p (fun symbol)
475     (map-function-constants fun
476     (lambda (c)
477     (when (eq c symbol)
478     (return-from in-constants-p t)))
479     3))
480 heller 1.70
481 heller 1.46 (defun function-callers (name)
482     (let ((callers '()))
483     (do-all-symbols (sym)
484     (when (fboundp sym)
485     (let ((fn (fdefinition sym)))
486     (when (in-constants-p fn name)
487     (push sym callers)))))
488     callers))
489    
490     (defimplementation list-callers (name)
491     (xref-result (function-callers name)))
492 heller 1.4
493 heller 1.70 (defimplementation list-callees (name)
494     (let ((result '()))
495     (map-function-constants (fdefinition name)
496     (lambda (c)
497     (when (fboundp c)
498     (push c result)))
499     2)
500     (xref-result result)))
501    
502 mkoeppe 1.83 ;;;; Profiling
503    
504 heller 1.89 ;; Per-function profiling based on description in
505     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
506    
507     (defvar *profiled-functions* ())
508     (defvar *profile-depth* 0)
509    
510     (defmacro with-redirected-y-or-n-p (&body body)
511     ;; If the profiler is restarted when the data from the previous
512     ;; session is not reported yet, the user is warned via Y-OR-N-P.
513     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
514     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
515     ;; overruled.
516     `(let* ((pkg (find-package "common-lisp"))
517     (saved-pdl (excl::package-definition-lock pkg))
518     (saved-ynp (symbol-function 'cl:y-or-n-p)))
519    
520     (setf (excl::package-definition-lock pkg) nil
521     (symbol-function 'cl:y-or-n-p) (symbol-function
522     (find-symbol "y-or-n-p-in-emacs"
523     "swank")))
524     (unwind-protect
525     (progn ,@body)
526    
527     (setf (symbol-function 'cl:y-or-n-p) saved-ynp
528     (excl::package-definition-lock pkg) saved-pdl))))
529    
530     (defun start-acl-profiler ()
531     (with-redirected-y-or-n-p
532     (prof:start-profiler :type :time :count t
533     :start-sampling-p nil :verbose nil)))
534     (defun acl-profiler-active-p ()
535     (not (eq (prof:profiler-status :verbose nil) :inactive)))
536    
537     (defun stop-acl-profiler ()
538     (prof:stop-profiler :verbose nil))
539    
540     (excl:def-fwrapper profile-fwrapper (&rest args)
541     ;; Ensures sampling is done during the execution of the function,
542     ;; taking into account recursion.
543     (declare (ignore args))
544     (cond ((zerop *profile-depth*)
545     (let ((*profile-depth* (1+ *profile-depth*)))
546     (prof:start-sampling)
547     (unwind-protect (excl:call-next-fwrapper)
548     (prof:stop-sampling))))
549     (t
550     (excl:call-next-fwrapper))))
551    
552     (defimplementation profile (fname)
553     (unless (acl-profiler-active-p)
554     (start-acl-profiler))
555     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
556     (push fname *profiled-functions*))
557    
558     (defimplementation profiled-functions ()
559     *profiled-functions*)
560    
561     (defimplementation unprofile (fname)
562     (excl:funwrap fname 'profile-fwrapper)
563     (setq *profiled-functions* (remove fname *profiled-functions*)))
564    
565 mkoeppe 1.83 (defimplementation profile-report ()
566 heller 1.89 (prof:show-flat-profile :verbose nil)
567     (when *profiled-functions*
568     (start-acl-profiler)))
569    
570     (defimplementation profile-reset ()
571     (when (acl-profiler-active-p)
572     (stop-acl-profiler)
573     (start-acl-profiler))
574     "Reset profiling counters.")
575 mkoeppe 1.83
576 heller 1.18 ;;;; Inspecting
577    
578 heller 1.100 (defmethod emacs-inspect ((f function))
579 heller 1.62 (append
580     (label-value-line "Name" (function-name f))
581     `("Formals" ,(princ-to-string (arglist f)) (:newline))
582     (let ((doc (documentation (excl::external-fn_symdef f) 'function)))
583     (when doc
584 heller 1.101 `("Documentation:" (:newline) ,doc)))))
585 heller 1.62
586 heller 1.100 (defmethod emacs-inspect ((o t))
587 heller 1.101 (allegro-inspect o))
588 heller 1.62
589 heller 1.100 (defmethod emacs-inspect ((o function))
590 heller 1.101 (allegro-inspect o))
591 heller 1.62
592 heller 1.100 (defmethod emacs-inspect ((o standard-object))
593 heller 1.101 (allegro-inspect o))
594 heller 1.82
595 heller 1.62 (defun allegro-inspect (o)
596     (loop for (d dd) on (inspect::inspect-ctl o)
597 heller 1.73 append (frob-allegro-field-def o d)
598     until (eq d dd)))
599 heller 1.62
600 heller 1.73 (defun frob-allegro-field-def (object def)
601 heller 1.62 (with-struct (inspect::field-def- name type access) def
602 heller 1.73 (ecase type
603 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
604     :unsigned-long :unsigned-half-long
605     :unsigned-3byte)
606 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
607     ((:lisp :value)
608     (label-value-line name (inspect::component-ref object access)))
609     (:indirect
610     (destructuring-bind (prefix count ref set) access
611     (declare (ignore set prefix))
612     (loop for i below (funcall count object)
613     append (label-value-line (format nil "~A-~D" name i)
614     (funcall ref object i))))))))
615 mbaringer 1.55
616 heller 1.18 ;;;; Multithreading
617 heller 1.8
618 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
619     (mp:start-scheduler)
620     (funcall continuation))
621 heller 1.8
622 heller 1.10 (defimplementation spawn (fn &key name)
623 lgorrie 1.47 (mp:process-run-function name fn))
624 heller 1.8
625 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
626     (defvar *thread-id-counter* 0)
627    
628     (defimplementation thread-id (thread)
629     (mp:with-process-lock (*id-lock*)
630     (or (getf (mp:process-property-list thread) 'id)
631     (setf (getf (mp:process-property-list thread) 'id)
632     (incf *thread-id-counter*)))))
633    
634     (defimplementation find-thread (id)
635     (find id mp:*all-processes*
636     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
637    
638 heller 1.13 (defimplementation thread-name (thread)
639     (mp:process-name thread))
640 heller 1.8
641 heller 1.13 (defimplementation thread-status (thread)
642     (format nil "~A ~D" (mp:process-whostate thread)
643     (mp:process-priority thread)))
644 heller 1.8
645 heller 1.10 (defimplementation make-lock (&key name)
646 heller 1.8 (mp:make-process-lock :name name))
647    
648 heller 1.10 (defimplementation call-with-lock-held (lock function)
649 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
650 heller 1.12
651     (defimplementation current-thread ()
652     mp:*current-process*)
653    
654     (defimplementation all-threads ()
655 heller 1.13 (copy-list mp:*all-processes*))
656 heller 1.12
657     (defimplementation interrupt-thread (thread fn)
658     (mp:process-interrupt thread fn))
659    
660 heller 1.16 (defimplementation kill-thread (thread)
661     (mp:process-kill thread))
662    
663 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
664    
665     (defstruct (mailbox (:conc-name mailbox.))
666     (mutex (mp:make-process-lock :name "process mailbox"))
667     (queue '() :type list))
668    
669     (defun mailbox (thread)
670     "Return THREAD's mailbox."
671     (mp:with-process-lock (*mailbox-lock*)
672     (or (getf (mp:process-property-list thread) 'mailbox)
673     (setf (getf (mp:process-property-list thread) 'mailbox)
674     (make-mailbox)))))
675    
676     (defimplementation send (thread message)
677     (let* ((mbox (mailbox thread))
678     (mutex (mailbox.mutex mbox)))
679     (mp:with-process-lock (mutex)
680     (setf (mailbox.queue mbox)
681     (nconc (mailbox.queue mbox) (list message))))))
682    
683     (defimplementation receive ()
684     (let* ((mbox (mailbox mp:*current-process*))
685     (mutex (mailbox.mutex mbox)))
686     (mp:process-wait "receive" #'mailbox.queue mbox)
687     (mp:with-process-lock (mutex)
688     (pop (mailbox.queue mbox)))))
689 mbaringer 1.27
690 heller 1.104 (defimplementation receive-if (test)
691     (let ((mbox (mailbox mp:*current-process*)))
692     (mp:process-wait "receive-if"
693     (lambda () (some test (mailbox.queue mbox))))
694     (mp:with-process-lock ((mailbox.mutex mbox))
695     (let* ((q (mailbox.queue mbox))
696     (tail (member-if test q)))
697     (assert tail)
698     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
699     (car tail)))))
700    
701 mbaringer 1.27 (defimplementation quit-lisp ()
702     (excl:exit 0 :quiet t))
703 mbaringer 1.68
704 heller 1.69
705 mbaringer 1.68 ;;Trace implementations
706     ;;In Allegro 7.0, we have:
707     ;; (trace <name>)
708     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
709     ;; (trace ((labels <name> <label-name>)))
710     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
711     ;; <name> can be a normal name or a (setf name)
712    
713 heller 1.70 (defimplementation toggle-trace (spec)
714 lgorrie 1.72 (ecase (car spec)
715     ((setf)
716     (toggle-trace-aux spec))
717 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
718 heller 1.71 ((setf :defmethod :labels :flet)
719 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
720 lgorrie 1.72 (:call
721 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
722     (toggle-trace-aux callee
723     :inside (list (process-fspec-for-allegro caller)))))))
724    
725     (defun tracedp (fspec)
726 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
727 heller 1.70
728     (defun toggle-trace-aux (fspec &rest args)
729     (cond ((tracedp fspec)
730     (eval `(untrace ,fspec))
731     (format nil "~S is now untraced." fspec))
732     (t
733     (eval `(trace (,fspec ,@args)))
734     (format nil "~S is now traced." fspec))))
735    
736     (defun toggle-trace-generic-function-methods (name)
737 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
738 heller 1.70 (cond ((tracedp name)
739 mbaringer 1.68 (eval `(untrace ,name))
740     (dolist (method methods (format nil "~S is now untraced." name))
741     (excl:funtrace (mop:method-function method))))
742     (t
743 lgorrie 1.72 (eval `(trace (,name)))
744 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
745 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
746    
747     (defun process-fspec-for-allegro (fspec)
748     (cond ((consp fspec)
749     (ecase (first fspec)
750 heller 1.71 ((setf) fspec)
751 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
752     ((:defmethod) `(method ,@(rest fspec)))
753 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
754     ,(third fspec)))
755     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
756     ,(third fspec)))))
757 mbaringer 1.68 (t
758     fspec)))
759 heller 1.88
760    
761     ;;;; Weak hashtables
762    
763     (defimplementation make-weak-key-hash-table (&rest args)
764     (apply #'make-hash-table :weak-keys t args))
765    
766     (defimplementation make-weak-value-hash-table (&rest args)
767     (apply #'make-hash-table :values :weak args))
768 mkoeppe 1.93
769 alendvai 1.95 (defimplementation hash-table-weakness (hashtable)
770     (cond ((excl:hash-table-weak-keys hashtable) :key)
771     ((eq (excl:hash-table-values hashtable) :weak) :value)))
772    
773    
774 mkoeppe 1.93
775     ;;;; Character names
776    
777     (defimplementation character-completion-set (prefix matchp)
778     (loop for name being the hash-keys of excl::*name-to-char-table*
779     when (funcall matchp prefix name)
780     collect (string-capitalize name)))

  ViewVC Help
Powered by ViewVC 1.1.5