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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.128 - (hide annotations)
Sun Jun 21 07:22:56 2009 UTC (4 years, 9 months ago) by heller
Branch: MAIN
Changes since 1.127: +1 -1 lines
* swank-backend.lisp (frame-source-location): Renamed from
frame-source-location-for-emacs.  Update callers accordingly.
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 heller 1.8 ;;;; Debugger
128    
129 heller 1.1 (defvar *sldb-topframe*)
130 heller 1.4
131 heller 1.10 (defimplementation call-with-debugging-environment (debugger-loop-fn)
132 heller 1.71 (let ((*sldb-topframe* (find-topframe))
133 heller 1.20 (excl::*break-hook* nil))
134 heller 1.4 (funcall debugger-loop-fn)))
135 heller 1.1
136 mkoeppe 1.92 (defimplementation sldb-break-at-start (fname)
137     ;; :print-before is kind of mis-used but we just want to stuff our break form
138     ;; somewhere. This does not work for setf, :before and :after methods, which
139     ;; need special syntax in the trace call, see ACL's doc/debugging.htm chapter 10.
140     (eval `(trace (,fname
141     :print-before
142     ((break "Function start breakpoint of ~A" ',fname)))))
143     `(:ok ,(format nil "Set breakpoint at start of ~S" fname)))
144    
145 heller 1.71 (defun find-topframe ()
146 trittweiler 1.125 (let ((magic-symbol (intern (symbol-name :swank-debugger-hook)
147     (find-package :swank)))
148     (top-frame (excl::int-newest-frame)))
149     (loop for frame = top-frame then (next-frame frame)
150     for name = (debugger:frame-name frame)
151     for i from 0
152     when (eq name magic-symbol)
153     return (next-frame frame)
154     until (= i 10) finally (return top-frame))))
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 heller 1.117 while f collect f)))
172 heller 1.1
173 heller 1.117 (defimplementation print-frame (frame stream)
174     (debugger:output-frame stream frame :moderate))
175 heller 1.4
176 heller 1.10 (defimplementation frame-locals (index)
177 heller 1.1 (let ((frame (nth-frame index)))
178     (loop for i from 0 below (debugger:frame-number-vars frame)
179 mbaringer 1.19 collect (list :name (debugger:frame-var-name frame i)
180 heller 1.1 :id 0
181 mbaringer 1.19 :value (debugger:frame-var-value frame i)))))
182 heller 1.1
183 heller 1.39 (defimplementation frame-var-value (frame var)
184     (let ((frame (nth-frame frame)))
185     (debugger:frame-var-value frame var)))
186 heller 1.1
187 heller 1.21 (defimplementation disassemble-frame (index)
188     (disassemble (debugger:frame-function (nth-frame index))))
189    
190 heller 1.128 (defimplementation frame-source-location (index)
191 lgorrie 1.37 (let* ((frame (nth-frame index))
192     (expr (debugger:frame-expression frame))
193     (fspec (first expr)))
194     (second (first (fspec-definition-locations fspec)))))
195 heller 1.4
196 heller 1.10 (defimplementation eval-in-frame (form frame-number)
197 heller 1.73 (let ((frame (nth-frame frame-number)))
198     ;; let-bind lexical variables
199     (let ((vars (loop for i below (debugger:frame-number-vars frame)
200     for name = (debugger:frame-var-name frame i)
201     if (symbolp name)
202     collect `(,name ',(debugger:frame-var-value frame i)))))
203     (debugger:eval-form-in-context
204     `(let* ,vars ,form)
205     (debugger:environment-of-frame frame)))))
206 heller 1.10
207 heller 1.11 (defimplementation return-from-frame (frame-number form)
208     (let ((frame (nth-frame frame-number)))
209     (multiple-value-call #'debugger:frame-return
210     frame (debugger:eval-form-in-context
211 heller 1.20 form
212     (debugger:environment-of-frame frame)))))
213 heller 1.67
214 heller 1.117 (defimplementation frame-restartable-p (frame)
215 heller 1.119 (handler-case (debugger:frame-retryable-p frame)
216     (serious-condition (c)
217     (funcall (read-from-string "swank::background-message")
218     "~a ~a" frame (princ-to-string c))
219     nil)))
220 heller 1.117
221 heller 1.11 (defimplementation restart-frame (frame-number)
222     (let ((frame (nth-frame frame-number)))
223 heller 1.69 (cond ((debugger:frame-retryable-p frame)
224     (apply #'debugger:frame-retry frame (debugger:frame-function frame)
225     (cdr (debugger:frame-expression frame))))
226     (t "Frame is not retryable"))))
227 heller 1.67
228 heller 1.8 ;;;; Compiler hooks
229    
230 heller 1.1 (defvar *buffer-name* nil)
231     (defvar *buffer-start-position*)
232     (defvar *buffer-string*)
233 lgorrie 1.33 (defvar *compile-filename* nil)
234 trittweiler 1.126 (defvar *temp-file-header-end-position* nil)
235 heller 1.1
236 heller 1.66 (defun compiler-note-p (object)
237     (member (type-of object) '(excl::compiler-note compiler::compiler-note)))
238    
239     (defun compiler-undefined-functions-called-warning-p (object)
240     (typep object 'excl:compiler-undefined-functions-called-warning))
241 heller 1.64
242     (deftype compiler-note ()
243     `(satisfies compiler-note-p))
244    
245 heller 1.66 (defun signal-compiler-condition (&rest args)
246     (signal (apply #'make-condition 'compiler-condition args)))
247    
248 heller 1.1 (defun handle-compiler-warning (condition)
249 heller 1.66 (declare (optimize (debug 3) (speed 0) (space 0)))
250 trittweiler 1.126 (cond ((and (not *buffer-name*)
251 heller 1.66 (compiler-undefined-functions-called-warning-p condition))
252     (handle-undefined-functions-warning condition))
253     (t
254     (signal-compiler-condition
255     :original-condition condition
256     :severity (etypecase condition
257     (warning :warning)
258 trittweiler 1.126 (compiler-note :note)
259     (reader-error :read-error))
260 heller 1.66 :message (format nil "~A" condition)
261 trittweiler 1.126 :location (if (typep condition 'reader-error)
262     (location-for-reader-error condition)
263     (location-for-warning condition))))))
264 heller 1.66
265     (defun location-for-warning (condition)
266 heller 1.1 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
267 heller 1.66 (cond (*buffer-name*
268     (make-location
269     (list :buffer *buffer-name*)
270 heller 1.114 (list :offset *buffer-start-position* 0)))
271 heller 1.66 (loc
272     (destructuring-bind (file . pos) loc
273     (make-location
274     (list :file (namestring (truename file)))
275     (list :position (1+ pos)))))
276     (t
277     (list :error "No error location available.")))))
278    
279 trittweiler 1.126 (defun location-for-reader-error (condition)
280     (let ((pos (car (last (slot-value condition 'excl::format-arguments))))
281     (file (pathname (stream-error-stream condition))))
282     (if (integerp pos)
283     (if *buffer-name*
284     (make-location `(:buffer ,*buffer-name*)
285     `(:offset ,*buffer-start-position*
286     ,(- pos *temp-file-header-end-position* 1)))
287     (make-location `(:file ,(namestring (truename file)))
288     `(:position ,pos)))
289     (list :error "No error location available."))))
290    
291 heller 1.66 (defun handle-undefined-functions-warning (condition)
292     (let ((fargs (slot-value condition 'excl::format-arguments)))
293 heller 1.67 (loop for (fname . pos-file) in (car fargs) do
294     (loop for (pos file) in pos-file do
295     (signal-compiler-condition
296     :original-condition condition
297     :severity :warning
298     :message (format nil "Undefined function referenced: ~S"
299     fname)
300     :location (make-location (list :file file)
301     (list :position (1+ pos))))))))
302 lgorrie 1.33 (defimplementation call-with-compilation-hooks (function)
303 trittweiler 1.126 (handler-bind ((warning #'handle-compiler-warning)
304     (compiler-note #'handle-compiler-warning)
305     (reader-error #'handle-compiler-warning))
306 lgorrie 1.33 (funcall function)))
307 heller 1.1
308 heller 1.124 (defimplementation swank-compile-file (input-file output-file
309     load-p external-format)
310 trittweiler 1.126 (handler-case
311     (with-compilation-hooks ()
312     (let ((*buffer-name* nil)
313     (*compile-filename* input-file))
314     (compile-file *compile-filename*
315     :output-file output-file
316     :load-after-compile load-p
317     :external-format external-format)))
318     (reader-error () (values nil nil t))))
319 heller 1.1
320 heller 1.46 (defun call-with-temp-file (fn)
321     (let ((tmpname (system:make-temp-file-name)))
322     (unwind-protect
323     (with-open-file (file tmpname :direction :output :if-exists :error)
324     (funcall fn file tmpname))
325     (delete-file tmpname))))
326    
327 trittweiler 1.126 (defun compile-from-temp-file (header string)
328 heller 1.46 (call-with-temp-file
329     (lambda (stream filename)
330 trittweiler 1.126 (write-string header stream)
331     (let ((*temp-file-header-end-position* (file-position stream)))
332 heller 1.46 (write-string string stream)
333     (finish-output stream)
334 heller 1.116 (multiple-value-bind (binary-filename warnings? failure?)
335 trittweiler 1.126 (excl:without-redefinition-warnings
336 heller 1.116 ;; Suppress Allegro's redefinition warnings; they are
337     ;; pointless when we are compiling via a temporary
338     ;; file.
339     (compile-file filename :load-after-compile t))
340     (declare (ignore warnings?))
341 heller 1.46 (when binary-filename
342 heller 1.116 (delete-file binary-filename))
343 trittweiler 1.126 (not failure?))))))
344    
345     (defimplementation swank-compile-string (string &key buffer position filename
346     policy)
347     (declare (ignore policy))
348     (handler-case
349     (with-compilation-hooks ()
350     (let ((*buffer-name* buffer)
351     (*buffer-start-position* position)
352     (*buffer-string* string)
353     (*default-pathname-defaults*
354     (if filename
355     (merge-pathnames (pathname filename))
356     *default-pathname-defaults*)))
357     ;; We store the source buffer in excl::*source-pathname* as a
358     ;; string of the form <buffername>;<start-offset>. Quite ugly
359     ;; encoding, but the fasl file is corrupted if we use some
360     ;; other datatype.
361     (compile-from-temp-file
362     (format nil "~S~%~S~%"
363     `(in-package ,(package-name *package*))
364     `(eval-when (:compile-toplevel :load-toplevel)
365     (setq excl::*source-pathname*
366     ',(format nil "~A;~D" buffer position))))
367     string)))
368     (reader-error () (values nil nil t))))
369 heller 1.46
370 heller 1.8 ;;;; Definition Finding
371    
372 heller 1.32 (defun fspec-primary-name (fspec)
373     (etypecase fspec
374 heller 1.62 (symbol fspec)
375     (list (fspec-primary-name (second fspec)))))
376 heller 1.32
377 heller 1.62 ;; If Emacs uses DOS-style eol conventions, \n\r are considered as a
378     ;; single character, but file-position counts them as two. Here we do
379     ;; our own conversion.
380     (defun count-cr (file pos)
381     (let* ((bufsize 256)
382 heller 1.63 (type '(unsigned-byte 8))
383     (buf (make-array bufsize :element-type type))
384 heller 1.62 (cr-count 0))
385 heller 1.63 (with-open-file (stream file :direction :input :element-type type)
386     (loop for bytes-read = (read-sequence buf stream) do
387     (incf cr-count (count (char-code #\return) buf
388     :end (min pos bytes-read)))
389     (decf pos bytes-read)
390     (when (<= pos 0)
391     (return cr-count))))))
392 heller 1.62
393 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
394     (let* ((part
395     (or (scm::find-definition-in-definition-group
396     fspec type (scm:section-file :file file)
397     :top-level top-level)
398     (scm::find-definition-in-definition-group
399     (fspec-primary-name fspec)
400     type (scm:section-file :file file)
401     :top-level top-level)))
402     (start (and part
403     (scm::source-part-start part)))
404 heller 1.60 (pos (if start
405 heller 1.114 (list :position (1+ start))
406 heller 1.62 (list :function-name (string (fspec-primary-name fspec))))))
407     (make-location (list :file (namestring (truename file)))
408     pos)))
409 heller 1.60
410     (defun find-definition-in-buffer (filename)
411     (let ((pos (position #\; filename :from-end t)))
412     (make-location
413     (list :buffer (subseq filename 0 pos))
414 heller 1.114 (list :offset (parse-integer (subseq filename (1+ pos))) 0))))
415 heller 1.60
416 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
417     (etypecase file
418     (pathname
419     (find-definition-in-file fspec type file top-level))
420     ((member :top-level)
421     (list :error (format nil "Defined at toplevel: ~A"
422     (fspec->string fspec))))
423     (string
424     (find-definition-in-buffer file))))
425 lgorrie 1.49
426 heller 1.50 (defun fspec->string (fspec)
427 lgorrie 1.49 (etypecase fspec
428 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
429     (prin1-to-string fspec)))
430     (list (format nil "(~A ~A)"
431     (prin1-to-string (first fspec))
432     (let ((*package* (find-package :keyword)))
433     (prin1-to-string (second fspec)))))))
434 heller 1.21
435     (defun fspec-definition-locations (fspec)
436 mkoeppe 1.87 (cond
437     ((and (listp fspec)
438     (eql (car fspec) :top-level-form))
439 mkoeppe 1.98 (destructuring-bind (top-level-form file &optional position) fspec
440 heller 1.111 (declare (ignore top-level-form))
441 mkoeppe 1.87 (list
442     (list (list nil fspec)
443 heller 1.114 (make-location (list :buffer file) ; FIXME: should use :file
444     (list :position position)
445     (list :align t))))))
446 heller 1.90 ((and (listp fspec) (eq (car fspec) :internal))
447     (destructuring-bind (_internal next _n) fspec
448 heller 1.111 (declare (ignore _internal _n))
449 heller 1.90 (fspec-definition-locations next)))
450 mkoeppe 1.87 (t
451     (let ((defs (excl::find-source-file fspec)))
452 trittweiler 1.103 (when (and (null defs)
453     (listp fspec)
454     (string= (car fspec) '#:method))
455     ;; If methods are defined in a defgeneric form, the source location is
456     ;; recorded for the gf but not for the methods. Therefore fall back to
457     ;; the gf as the likely place of definition.
458     (setq defs (excl::find-source-file (second fspec))))
459 mkoeppe 1.87 (if (null defs)
460     (list
461     (list (list nil fspec)
462     (list :error
463     (format nil "Unknown source location for ~A"
464     (fspec->string fspec)))))
465 mkoeppe 1.81 (loop for (fspec type file top-level) in defs
466 mkoeppe 1.87 collect (list (list type fspec)
467     (find-fspec-location fspec type file top-level))))))))
468 heller 1.21
469     (defimplementation find-definitions (symbol)
470     (fspec-definition-locations symbol))
471 heller 1.1
472 heller 1.8 ;;;; XREF
473    
474 heller 1.21 (defmacro defxref (name relation name1 name2)
475     `(defimplementation ,name (x)
476     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
477    
478     (defxref who-calls :calls :wild x)
479 heller 1.70 (defxref calls-who :calls x :wild)
480 heller 1.21 (defxref who-references :uses :wild x)
481     (defxref who-binds :binds :wild x)
482     (defxref who-macroexpands :macro-calls :wild x)
483     (defxref who-sets :sets :wild x)
484    
485     (defun xref-result (fspecs)
486     (loop for fspec in fspecs
487     append (fspec-definition-locations fspec)))
488 heller 1.46
489     ;; list-callers implemented by groveling through all fbound symbols.
490     ;; Only symbols are considered. Functions in the constant pool are
491 heller 1.71 ;; searched recursively. Closure environments are ignored at the
492 heller 1.46 ;; moment (constants in methods are therefore not found).
493    
494     (defun map-function-constants (function fn depth)
495     "Call FN with the elements of FUNCTION's constant pool."
496     (do ((i 0 (1+ i))
497     (max (excl::function-constant-count function)))
498     ((= i max))
499     (let ((c (excl::function-constant function i)))
500     (cond ((and (functionp c)
501     (not (eq c function))
502     (plusp depth))
503     (map-function-constants c fn (1- depth)))
504     (t
505     (funcall fn c))))))
506    
507 heller 1.64 (defun in-constants-p (fun symbol)
508     (map-function-constants fun
509     (lambda (c)
510     (when (eq c symbol)
511     (return-from in-constants-p t)))
512     3))
513 heller 1.70
514 heller 1.46 (defun function-callers (name)
515     (let ((callers '()))
516     (do-all-symbols (sym)
517     (when (fboundp sym)
518     (let ((fn (fdefinition sym)))
519     (when (in-constants-p fn name)
520     (push sym callers)))))
521     callers))
522    
523     (defimplementation list-callers (name)
524     (xref-result (function-callers name)))
525 heller 1.4
526 heller 1.70 (defimplementation list-callees (name)
527     (let ((result '()))
528     (map-function-constants (fdefinition name)
529     (lambda (c)
530     (when (fboundp c)
531     (push c result)))
532     2)
533     (xref-result result)))
534    
535 mkoeppe 1.83 ;;;; Profiling
536    
537 heller 1.89 ;; Per-function profiling based on description in
538     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
539    
540     (defvar *profiled-functions* ())
541     (defvar *profile-depth* 0)
542    
543     (defmacro with-redirected-y-or-n-p (&body body)
544     ;; If the profiler is restarted when the data from the previous
545     ;; session is not reported yet, the user is warned via Y-OR-N-P.
546     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
547     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
548     ;; overruled.
549     `(let* ((pkg (find-package "common-lisp"))
550     (saved-pdl (excl::package-definition-lock pkg))
551     (saved-ynp (symbol-function 'cl:y-or-n-p)))
552    
553     (setf (excl::package-definition-lock pkg) nil
554     (symbol-function 'cl:y-or-n-p) (symbol-function
555     (find-symbol "y-or-n-p-in-emacs"
556     "swank")))
557     (unwind-protect
558     (progn ,@body)
559    
560     (setf (symbol-function 'cl:y-or-n-p) saved-ynp
561     (excl::package-definition-lock pkg) saved-pdl))))
562    
563     (defun start-acl-profiler ()
564     (with-redirected-y-or-n-p
565     (prof:start-profiler :type :time :count t
566     :start-sampling-p nil :verbose nil)))
567     (defun acl-profiler-active-p ()
568     (not (eq (prof:profiler-status :verbose nil) :inactive)))
569    
570     (defun stop-acl-profiler ()
571     (prof:stop-profiler :verbose nil))
572    
573     (excl:def-fwrapper profile-fwrapper (&rest args)
574     ;; Ensures sampling is done during the execution of the function,
575     ;; taking into account recursion.
576     (declare (ignore args))
577     (cond ((zerop *profile-depth*)
578     (let ((*profile-depth* (1+ *profile-depth*)))
579     (prof:start-sampling)
580     (unwind-protect (excl:call-next-fwrapper)
581     (prof:stop-sampling))))
582     (t
583     (excl:call-next-fwrapper))))
584    
585     (defimplementation profile (fname)
586     (unless (acl-profiler-active-p)
587     (start-acl-profiler))
588     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
589     (push fname *profiled-functions*))
590    
591     (defimplementation profiled-functions ()
592     *profiled-functions*)
593    
594     (defimplementation unprofile (fname)
595     (excl:funwrap fname 'profile-fwrapper)
596     (setq *profiled-functions* (remove fname *profiled-functions*)))
597    
598 mkoeppe 1.83 (defimplementation profile-report ()
599 heller 1.89 (prof:show-flat-profile :verbose nil)
600     (when *profiled-functions*
601     (start-acl-profiler)))
602    
603     (defimplementation profile-reset ()
604     (when (acl-profiler-active-p)
605     (stop-acl-profiler)
606     (start-acl-profiler))
607     "Reset profiling counters.")
608 mkoeppe 1.83
609 heller 1.18 ;;;; Inspecting
610    
611 heller 1.111 (excl:without-redefinition-warnings
612 heller 1.100 (defmethod emacs-inspect ((o t))
613 heller 1.111 (allegro-inspect o)))
614 heller 1.62
615 heller 1.100 (defmethod emacs-inspect ((o function))
616 heller 1.101 (allegro-inspect o))
617 heller 1.62
618 heller 1.100 (defmethod emacs-inspect ((o standard-object))
619 heller 1.101 (allegro-inspect o))
620 heller 1.82
621 heller 1.62 (defun allegro-inspect (o)
622     (loop for (d dd) on (inspect::inspect-ctl o)
623 heller 1.73 append (frob-allegro-field-def o d)
624     until (eq d dd)))
625 heller 1.62
626 heller 1.73 (defun frob-allegro-field-def (object def)
627 heller 1.62 (with-struct (inspect::field-def- name type access) def
628 heller 1.73 (ecase type
629 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
630     :unsigned-long :unsigned-half-long
631     :unsigned-3byte)
632 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
633 heller 1.115 ((:lisp :value :func)
634 heller 1.73 (label-value-line name (inspect::component-ref object access)))
635     (:indirect
636     (destructuring-bind (prefix count ref set) access
637     (declare (ignore set prefix))
638     (loop for i below (funcall count object)
639     append (label-value-line (format nil "~A-~D" name i)
640     (funcall ref object i))))))))
641 mbaringer 1.55
642 heller 1.18 ;;;; Multithreading
643 heller 1.8
644 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
645     (mp:start-scheduler)
646     (funcall continuation))
647 heller 1.8
648 heller 1.10 (defimplementation spawn (fn &key name)
649 lgorrie 1.47 (mp:process-run-function name fn))
650 heller 1.8
651 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
652     (defvar *thread-id-counter* 0)
653    
654     (defimplementation thread-id (thread)
655     (mp:with-process-lock (*id-lock*)
656     (or (getf (mp:process-property-list thread) 'id)
657     (setf (getf (mp:process-property-list thread) 'id)
658     (incf *thread-id-counter*)))))
659    
660     (defimplementation find-thread (id)
661     (find id mp:*all-processes*
662     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
663    
664 heller 1.13 (defimplementation thread-name (thread)
665     (mp:process-name thread))
666 heller 1.8
667 heller 1.13 (defimplementation thread-status (thread)
668     (format nil "~A ~D" (mp:process-whostate thread)
669     (mp:process-priority thread)))
670 heller 1.8
671 heller 1.10 (defimplementation make-lock (&key name)
672 heller 1.8 (mp:make-process-lock :name name))
673    
674 heller 1.10 (defimplementation call-with-lock-held (lock function)
675 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
676 heller 1.12
677     (defimplementation current-thread ()
678     mp:*current-process*)
679    
680     (defimplementation all-threads ()
681 heller 1.13 (copy-list mp:*all-processes*))
682 heller 1.12
683     (defimplementation interrupt-thread (thread fn)
684     (mp:process-interrupt thread fn))
685    
686 heller 1.16 (defimplementation kill-thread (thread)
687     (mp:process-kill thread))
688    
689 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
690    
691     (defstruct (mailbox (:conc-name mailbox.))
692 heller 1.107 (lock (mp:make-process-lock :name "process mailbox"))
693     (queue '() :type list)
694 heller 1.108 (gate (mp:make-gate nil)))
695 heller 1.12
696     (defun mailbox (thread)
697     "Return THREAD's mailbox."
698     (mp:with-process-lock (*mailbox-lock*)
699     (or (getf (mp:process-property-list thread) 'mailbox)
700     (setf (getf (mp:process-property-list thread) 'mailbox)
701     (make-mailbox)))))
702    
703     (defimplementation send (thread message)
704 heller 1.107 (let* ((mbox (mailbox thread)))
705     (mp:with-process-lock ((mailbox.lock mbox))
706     (setf (mailbox.queue mbox)
707     (nconc (mailbox.queue mbox) (list message)))
708     (mp:open-gate (mailbox.gate mbox)))))
709 heller 1.12
710 heller 1.110 (defimplementation receive-if (test &optional timeout)
711 heller 1.104 (let ((mbox (mailbox mp:*current-process*)))
712 heller 1.110 (assert (or (not timeout) (eq timeout t)))
713 heller 1.107 (loop
714     (check-slime-interrupts)
715     (mp:with-process-lock ((mailbox.lock mbox))
716     (let* ((q (mailbox.queue mbox))
717     (tail (member-if test q)))
718     (when tail
719     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
720     (return (car tail)))
721     (mp:close-gate (mailbox.gate mbox))))
722 heller 1.110 (when (eq timeout t) (return (values nil t)))
723     (mp:process-wait-with-timeout "receive-if" 0.5
724     #'mp:gate-open-p (mailbox.gate mbox)))))
725 heller 1.104
726 heller 1.123 (defimplementation set-default-initial-binding (var form)
727     (setq excl:*cl-default-special-bindings*
728     (acons var form excl:*cl-default-special-bindings*)))
729    
730 mbaringer 1.27 (defimplementation quit-lisp ()
731     (excl:exit 0 :quiet t))
732 mbaringer 1.68
733 heller 1.69
734 mbaringer 1.68 ;;Trace implementations
735     ;;In Allegro 7.0, we have:
736     ;; (trace <name>)
737     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
738     ;; (trace ((labels <name> <label-name>)))
739     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
740     ;; <name> can be a normal name or a (setf name)
741    
742 heller 1.70 (defimplementation toggle-trace (spec)
743 lgorrie 1.72 (ecase (car spec)
744     ((setf)
745     (toggle-trace-aux spec))
746 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
747 heller 1.71 ((setf :defmethod :labels :flet)
748 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
749 lgorrie 1.72 (:call
750 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
751     (toggle-trace-aux callee
752     :inside (list (process-fspec-for-allegro caller)))))))
753    
754     (defun tracedp (fspec)
755 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
756 heller 1.70
757     (defun toggle-trace-aux (fspec &rest args)
758     (cond ((tracedp fspec)
759     (eval `(untrace ,fspec))
760     (format nil "~S is now untraced." fspec))
761     (t
762     (eval `(trace (,fspec ,@args)))
763     (format nil "~S is now traced." fspec))))
764    
765     (defun toggle-trace-generic-function-methods (name)
766 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
767 heller 1.70 (cond ((tracedp name)
768 mbaringer 1.68 (eval `(untrace ,name))
769     (dolist (method methods (format nil "~S is now untraced." name))
770     (excl:funtrace (mop:method-function method))))
771     (t
772 lgorrie 1.72 (eval `(trace (,name)))
773 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
774 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
775    
776     (defun process-fspec-for-allegro (fspec)
777     (cond ((consp fspec)
778     (ecase (first fspec)
779 heller 1.71 ((setf) fspec)
780 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
781     ((:defmethod) `(method ,@(rest fspec)))
782 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
783     ,(third fspec)))
784     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
785     ,(third fspec)))))
786 mbaringer 1.68 (t
787     fspec)))
788 heller 1.88
789    
790     ;;;; Weak hashtables
791    
792     (defimplementation make-weak-key-hash-table (&rest args)
793     (apply #'make-hash-table :weak-keys t args))
794    
795     (defimplementation make-weak-value-hash-table (&rest args)
796     (apply #'make-hash-table :values :weak args))
797 mkoeppe 1.93
798 alendvai 1.95 (defimplementation hash-table-weakness (hashtable)
799     (cond ((excl:hash-table-weak-keys hashtable) :key)
800     ((eq (excl:hash-table-values hashtable) :weak) :value)))
801    
802    
803 mkoeppe 1.93
804     ;;;; Character names
805    
806     (defimplementation character-completion-set (prefix matchp)
807     (loop for name being the hash-keys of excl::*name-to-char-table*
808     when (funcall matchp prefix name)
809     collect (string-capitalize name)))

  ViewVC Help
Powered by ViewVC 1.1.5