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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.137 - (hide annotations)
Tue Mar 9 08:02:37 2010 UTC (4 years, 1 month ago) by heller
Branch: MAIN
Changes since 1.136: +16 -12 lines
Some more fixes for Allegro

* swank-allegro.lisp (function-source-location): Use
xref::object-to-function-name which seems to work better for some
cases.
(fspec-definition-locations): For :top-level-forms return a list
of ((fspec loc)) not just (fspec loc).  Also deal with the file
vs. buffer issue.
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.135 (require :process)
16     #+(version>= 8 2)
17     (require 'lldb)
18     )
19    
20 heller 1.79 (import-from :excl *gray-stream-symbols* :swank-backend)
21 heller 1.1
22 mbaringer 1.53 ;;; swank-mop
23    
24 heller 1.60 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
25 mbaringer 1.53
26 mbaringer 1.54 (defun swank-mop:slot-definition-documentation (slot)
27 heller 1.62 (documentation slot t))
28 mbaringer 1.53
29 heller 1.79
30 heller 1.8 ;;;; TCP Server
31 heller 1.1
32 heller 1.21 (defimplementation preferred-communication-style ()
33     :spawn)
34 heller 1.12
35 heller 1.14 (defimplementation create-socket (host port)
36     (socket:make-socket :connect :passive :local-port port
37     :local-host host :reuse-address t))
38 heller 1.5
39 heller 1.10 (defimplementation local-port (socket)
40 heller 1.6 (socket:local-port socket))
41    
42 heller 1.10 (defimplementation close-socket (socket)
43 heller 1.6 (close socket))
44    
45 dcrosher 1.85 (defimplementation accept-connection (socket &key external-format buffering
46     timeout)
47     (declare (ignore buffering timeout))
48 heller 1.94 (let ((s (socket:accept-connection socket :wait t)))
49     (when external-format
50     (setf (stream-external-format s) external-format))
51 heller 1.63 s))
52 heller 1.6
53 heller 1.94 (defvar *external-format-to-coding-system*
54     '((:iso-8859-1
55     "latin-1" "latin-1-unix" "iso-latin-1-unix"
56     "iso-8859-1" "iso-8859-1-unix")
57     (:utf-8 "utf-8" "utf-8-unix")
58     (:euc-jp "euc-jp" "euc-jp-unix")
59     (:us-ascii "us-ascii" "us-ascii-unix")
60     (:emacs-mule "emacs-mule" "emacs-mule-unix")))
61    
62     (defimplementation find-external-format (coding-system)
63     (let ((e (rassoc-if (lambda (x) (member coding-system x :test #'equal))
64     *external-format-to-coding-system*)))
65     (and e (excl:crlf-base-ef
66     (excl:find-external-format (car e)
67     :try-variant t)))))
68 heller 1.64
69 heller 1.34 (defimplementation format-sldb-condition (c)
70     (princ-to-string c))
71    
72 heller 1.39 (defimplementation call-with-syntax-hooks (fn)
73     (funcall fn))
74    
75 heller 1.9 ;;;; Unix signals
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 heller 1.135 ;; :print-before is kind of mis-used but we just want to stuff our
138     ;; break form somewhere. This does not work for setf, :before and
139     ;; :after methods, which need special syntax in the trace call, see
140     ;; ACL's doc/debugging.htm chapter 10.
141 mkoeppe 1.92 (eval `(trace (,fname
142     :print-before
143     ((break "Function start breakpoint of ~A" ',fname)))))
144     `(:ok ,(format nil "Set breakpoint at start of ~S" fname)))
145    
146 heller 1.71 (defun find-topframe ()
147 trittweiler 1.125 (let ((magic-symbol (intern (symbol-name :swank-debugger-hook)
148     (find-package :swank)))
149     (top-frame (excl::int-newest-frame)))
150     (loop for frame = top-frame then (next-frame frame)
151     for name = (debugger:frame-name frame)
152     for i from 0
153     when (eq name magic-symbol)
154     return (next-frame frame)
155     until (= i 10) finally (return top-frame))))
156 heller 1.71
157 heller 1.42 (defun next-frame (frame)
158     (let ((next (excl::int-next-older-frame frame)))
159     (cond ((not next) nil)
160     ((debugger:frame-visible-p next) next)
161     (t (next-frame next)))))
162    
163 heller 1.1 (defun nth-frame (index)
164 heller 1.42 (do ((frame *sldb-topframe* (next-frame frame))
165 heller 1.1 (i index (1- i)))
166     ((zerop i) frame)))
167    
168 heller 1.20 (defimplementation compute-backtrace (start end)
169 heller 1.1 (let ((end (or end most-positive-fixnum)))
170 heller 1.42 (loop for f = (nth-frame start) then (next-frame f)
171 heller 1.1 for i from start below end
172 heller 1.117 while f collect f)))
173 heller 1.1
174 heller 1.117 (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 heller 1.1
188 heller 1.21 (defimplementation disassemble-frame (index)
189 heller 1.135 (let ((frame (nth-frame index)))
190     (multiple-value-bind (x fun xx xxx pc) (debugger::dyn-fd-analyze frame)
191     (format t "pc: ~d (~s ~s ~s)~%fun: ~a~%" pc x xx xxx fun)
192     (disassemble (debugger:frame-function frame)))))
193 heller 1.21
194 heller 1.128 (defimplementation frame-source-location (index)
195 heller 1.135 (let* ((frame (nth-frame index)))
196     (multiple-value-bind (x fun xx xxx pc) (debugger::dyn-fd-analyze frame)
197     (declare (ignore x xx xxx))
198 heller 1.136 (cond (pc
199     #+(version>= 8 2)
200     (pc-source-location fun pc)
201     #-(version>= 8 2)
202     (function-source-location fun))
203     (t ; frames for unbound functions etc end up here
204     (cadr (car (fspec-definition-locations
205     (car (debugger:frame-expression frame))))))))))
206 heller 1.135
207     (defun function-source-location (fun)
208 heller 1.137 (cadr (car (fspec-definition-locations (xref::object-to-function-name fun)))))
209 heller 1.135
210     #+(version>= 8 2)
211     (defun pc-source-location (fun pc)
212     (let* ((debug-info (excl::function-source-debug-info fun)))
213     (cond ((not debug-info)
214     (function-source-location fun))
215     (t
216     (let* ((return-loc (find pc debug-info :key #'excl::ldb-code-pc))
217     (prev (and return-loc (excl::ldb-code-prev-rec return-loc)))
218     (call-loc (if (integerp prev)
219     (aref debug-info prev)
220     return-loc)))
221     (cond ((not call-loc)
222     (ldb-code-to-src-loc (aref debug-info 0)))
223     (t
224     (ldb-code-to-src-loc call-loc))))))))
225    
226     #+(version>= 8 2)
227     (defun ldb-code-to-src-loc (code)
228     (let* ((start (excl::ldb-code-start-char code))
229     (func (excl::ldb-code-func code))
230 heller 1.136 (src-file (excl:source-file func)))
231     (cond (start
232     (buffer-or-file-location src-file start))
233 heller 1.135 (t
234     (let* ((debug-info (excl::function-source-debug-info func))
235     (whole (aref debug-info 0))
236     (paths (source-paths-of (excl::ldb-code-source whole)
237     (excl::ldb-code-source code)))
238     (path (longest-common-prefix paths))
239 heller 1.136 (start (excl::ldb-code-start-char whole))
240     (probe (gethash src-file *temp-file-map*)))
241     (cond ((not probe)
242     (make-location `(:file ,(namestring (truename src-file)))
243     `(:source-path (0 . ,path) ,start)))
244     (t
245     (destructuring-bind (buffer bstart file) probe
246     (declare (ignore file))
247     (make-location `(:buffer ,buffer)
248     `(:source-path (0 . ,path)
249     ,(+ bstart start)))))))))))
250 heller 1.135
251     (defun longest-common-prefix (sequences)
252     (assert sequences)
253     (flet ((common-prefix (s1 s2)
254     (let ((diff-pos (mismatch s1 s2)))
255     (if diff-pos (subseq s1 0 diff-pos) s1))))
256     (reduce #'common-prefix sequences)))
257    
258     (defun source-paths-of (whole part)
259     (let ((result '()))
260     (labels ((walk (form path)
261     (cond ((eq form part)
262     (push (reverse path) result))
263     ((consp form)
264     (loop for i from 0 while (consp form) do
265     (walk (pop form) (cons i path)))))))
266     (walk whole '())
267     (reverse result))))
268 heller 1.4
269 heller 1.10 (defimplementation eval-in-frame (form frame-number)
270 heller 1.73 (let ((frame (nth-frame frame-number)))
271     ;; let-bind lexical variables
272     (let ((vars (loop for i below (debugger:frame-number-vars frame)
273     for name = (debugger:frame-var-name frame i)
274     if (symbolp name)
275     collect `(,name ',(debugger:frame-var-value frame i)))))
276     (debugger:eval-form-in-context
277     `(let* ,vars ,form)
278     (debugger:environment-of-frame frame)))))
279 heller 1.10
280 heller 1.11 (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 heller 1.20 form
285     (debugger:environment-of-frame frame)))))
286 heller 1.67
287 heller 1.117 (defimplementation frame-restartable-p (frame)
288 heller 1.119 (handler-case (debugger:frame-retryable-p frame)
289     (serious-condition (c)
290     (funcall (read-from-string "swank::background-message")
291     "~a ~a" frame (princ-to-string c))
292     nil)))
293 heller 1.117
294 heller 1.11 (defimplementation restart-frame (frame-number)
295     (let ((frame (nth-frame frame-number)))
296 heller 1.69 (cond ((debugger:frame-retryable-p frame)
297     (apply #'debugger:frame-retry frame (debugger:frame-function frame)
298     (cdr (debugger:frame-expression frame))))
299     (t "Frame is not retryable"))))
300 heller 1.67
301 heller 1.8 ;;;; Compiler hooks
302    
303 heller 1.1 (defvar *buffer-name* nil)
304     (defvar *buffer-start-position*)
305     (defvar *buffer-string*)
306 lgorrie 1.33 (defvar *compile-filename* nil)
307 heller 1.1
308 heller 1.66 (defun compiler-note-p (object)
309     (member (type-of object) '(excl::compiler-note compiler::compiler-note)))
310    
311 trittweiler 1.131 (defun redefinition-p (condition)
312     (and (typep condition 'style-warning)
313     (every #'char-equal "redefin" (princ-to-string condition))))
314    
315 heller 1.66 (defun compiler-undefined-functions-called-warning-p (object)
316     (typep object 'excl:compiler-undefined-functions-called-warning))
317 heller 1.64
318     (deftype compiler-note ()
319     `(satisfies compiler-note-p))
320    
321 trittweiler 1.131 (deftype redefinition ()
322     `(satisfies redefinition-p))
323    
324 heller 1.66 (defun signal-compiler-condition (&rest args)
325     (signal (apply #'make-condition 'compiler-condition args)))
326    
327 heller 1.1 (defun handle-compiler-warning (condition)
328 heller 1.66 (declare (optimize (debug 3) (speed 0) (space 0)))
329 heller 1.133 (cond ((and (not *buffer-name*)
330 heller 1.66 (compiler-undefined-functions-called-warning-p condition))
331     (handle-undefined-functions-warning condition))
332     (t
333     (signal-compiler-condition
334     :original-condition condition
335     :severity (etypecase condition
336 trittweiler 1.131 (redefinition :redefinition)
337     (style-warning :style-warning)
338     (warning :warning)
339 trittweiler 1.126 (compiler-note :note)
340 trittweiler 1.131 (reader-error :read-error)
341     (error :error))
342 heller 1.66 :message (format nil "~A" condition)
343 trittweiler 1.126 :location (if (typep condition 'reader-error)
344     (location-for-reader-error condition)
345     (location-for-warning condition))))))
346 heller 1.66
347     (defun location-for-warning (condition)
348 heller 1.1 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
349 heller 1.66 (cond (*buffer-name*
350     (make-location
351     (list :buffer *buffer-name*)
352 heller 1.114 (list :offset *buffer-start-position* 0)))
353 heller 1.66 (loc
354     (destructuring-bind (file . pos) loc
355 heller 1.133 (let ((start (cond ((consp pos) ; 8.2 and newer
356     (car pos))
357     (t pos))))
358     (make-location
359     (list :file (namestring (truename file)))
360     (list :position (1+ start))))))
361 heller 1.66 (t
362 sboukarev 1.130 (make-error-location "No error location available.")))))
363 heller 1.66
364 trittweiler 1.126 (defun location-for-reader-error (condition)
365     (let ((pos (car (last (slot-value condition 'excl::format-arguments))))
366     (file (pathname (stream-error-stream condition))))
367     (if (integerp pos)
368     (if *buffer-name*
369     (make-location `(:buffer ,*buffer-name*)
370 heller 1.135 `(:offset ,*buffer-start-position* ,pos))
371 trittweiler 1.126 (make-location `(:file ,(namestring (truename file)))
372     `(:position ,pos)))
373 sboukarev 1.130 (make-error-location "No error location available."))))
374 trittweiler 1.126
375 heller 1.133 ;; TODO: report it as a bug to Franz that the condition's plist
376     ;; slot contains (:loc nil).
377 heller 1.66 (defun handle-undefined-functions-warning (condition)
378     (let ((fargs (slot-value condition 'excl::format-arguments)))
379 heller 1.133 (loop for (fname . locs) in (car fargs) do
380     (dolist (loc locs)
381     (multiple-value-bind (pos file) (ecase (length loc)
382     (2 (values-list loc))
383     (3 (destructuring-bind
384     (start end file) loc
385     (declare (ignore end))
386     (values start file))))
387     (signal-compiler-condition
388     :original-condition condition
389     :severity :warning
390     :message (format nil "Undefined function referenced: ~S"
391     fname)
392     :location (make-location (list :file file)
393     (list :position (1+ pos)))))))))
394    
395 lgorrie 1.33 (defimplementation call-with-compilation-hooks (function)
396 trittweiler 1.126 (handler-bind ((warning #'handle-compiler-warning)
397     (compiler-note #'handle-compiler-warning)
398     (reader-error #'handle-compiler-warning))
399 lgorrie 1.33 (funcall function)))
400 heller 1.1
401 heller 1.124 (defimplementation swank-compile-file (input-file output-file
402 sboukarev 1.132 load-p external-format
403     &key policy)
404     (declare (ignore policy))
405 trittweiler 1.126 (handler-case
406     (with-compilation-hooks ()
407     (let ((*buffer-name* nil)
408     (*compile-filename* input-file))
409     (compile-file *compile-filename*
410     :output-file output-file
411     :load-after-compile load-p
412     :external-format external-format)))
413     (reader-error () (values nil nil t))))
414 heller 1.1
415 heller 1.46 (defun call-with-temp-file (fn)
416     (let ((tmpname (system:make-temp-file-name)))
417     (unwind-protect
418     (with-open-file (file tmpname :direction :output :if-exists :error)
419     (funcall fn file tmpname))
420     (delete-file tmpname))))
421    
422 heller 1.135 (defvar *temp-file-map* (make-hash-table :test #'equal)
423     "A mapping from tempfile names to Emacs buffer names.")
424    
425     (defun compile-from-temp-file (string buffer offset file)
426 heller 1.46 (call-with-temp-file
427     (lambda (stream filename)
428 heller 1.135 (let ((excl:*load-source-file-info* t)
429     (sys:*source-file-types* '(nil)) ; suppress .lisp extension
430     #+(version>= 8 2)
431     (compiler:save-source-level-debug-info-switch t)
432     #+(version>= 8 2)
433     (excl:*load-source-debug-info* t) ; NOTE: requires lldb
434     )
435 heller 1.46 (write-string string stream)
436     (finish-output stream)
437 heller 1.116 (multiple-value-bind (binary-filename warnings? failure?)
438 trittweiler 1.126 (excl:without-redefinition-warnings
439 heller 1.116 ;; Suppress Allegro's redefinition warnings; they are
440     ;; pointless when we are compiling via a temporary
441     ;; file.
442     (compile-file filename :load-after-compile t))
443     (declare (ignore warnings?))
444 heller 1.46 (when binary-filename
445 heller 1.135 (setf (gethash (pathname stream) *temp-file-map*)
446     (list buffer offset file))
447 heller 1.116 (delete-file binary-filename))
448 trittweiler 1.126 (not failure?))))))
449    
450     (defimplementation swank-compile-string (string &key buffer position filename
451     policy)
452     (declare (ignore policy))
453     (handler-case
454     (with-compilation-hooks ()
455     (let ((*buffer-name* buffer)
456     (*buffer-start-position* position)
457     (*buffer-string* string)
458     (*default-pathname-defaults*
459     (if filename
460     (merge-pathnames (pathname filename))
461     *default-pathname-defaults*)))
462 heller 1.135 (compile-from-temp-file string buffer position filename)))
463 trittweiler 1.126 (reader-error () (values nil nil t))))
464 heller 1.46
465 heller 1.8 ;;;; Definition Finding
466    
467 heller 1.137 (defun buffer-or-file (file file-fun buffer-fun)
468 heller 1.135 (let* ((probe (gethash file *temp-file-map*)))
469 heller 1.137 (cond (probe
470 heller 1.135 (destructuring-bind (buffer start file) probe
471     (declare (ignore file))
472 heller 1.137 (funcall buffer-fun buffer start)))
473     (t (funcall file-fun (namestring (truename file)))))))
474    
475     (defun buffer-or-file-location (file offset)
476     (buffer-or-file file
477     (lambda (filename)
478     (make-location `(:file ,filename)
479     `(:position ,(1+ offset))))
480     (lambda (buffer start)
481     (make-location `(:buffer ,buffer)
482     `(:offset ,start ,offset)))))
483 heller 1.135
484 heller 1.32 (defun fspec-primary-name (fspec)
485     (etypecase fspec
486 heller 1.62 (symbol fspec)
487     (list (fspec-primary-name (second fspec)))))
488 heller 1.135
489 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
490     (let* ((part
491     (or (scm::find-definition-in-definition-group
492     fspec type (scm:section-file :file file)
493     :top-level top-level)
494     (scm::find-definition-in-definition-group
495     (fspec-primary-name fspec)
496     type (scm:section-file :file file)
497     :top-level top-level)))
498     (start (and part
499     (scm::source-part-start part)))
500 heller 1.60 (pos (if start
501 heller 1.114 (list :position (1+ start))
502 heller 1.62 (list :function-name (string (fspec-primary-name fspec))))))
503     (make-location (list :file (namestring (truename file)))
504     pos)))
505 heller 1.60
506 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
507 sboukarev 1.130 (handler-case
508     (etypecase file
509     (pathname
510 heller 1.135 (let ((probe (gethash file *temp-file-map*)))
511     (cond (probe
512     (destructuring-bind (buffer offset file) probe
513     (declare (ignore file))
514     (make-location `(:buffer ,buffer)
515     `(:offset ,offset 0))))
516     (t
517     (find-definition-in-file fspec type file top-level)))))
518 sboukarev 1.130 ((member :top-level)
519 heller 1.135 (make-error-location "Defined at toplevel: ~A" (fspec->string fspec))))
520 sboukarev 1.130 (error (e)
521     (make-error-location "Error: ~A" e))))
522 lgorrie 1.49
523 heller 1.50 (defun fspec->string (fspec)
524 heller 1.135 (typecase fspec
525 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
526     (prin1-to-string fspec)))
527     (list (format nil "(~A ~A)"
528     (prin1-to-string (first fspec))
529     (let ((*package* (find-package :keyword)))
530 heller 1.135 (prin1-to-string (second fspec)))))
531     (t (princ-to-string fspec))))
532 heller 1.21
533     (defun fspec-definition-locations (fspec)
534 mkoeppe 1.87 (cond
535 sboukarev 1.130 ((and (listp fspec)
536     (eql (car fspec) :top-level-form))
537     (destructuring-bind (top-level-form file &optional position) fspec
538     (declare (ignore top-level-form))
539 heller 1.137 `((,fspec
540     ,(buffer-or-file-location file position)))))
541 sboukarev 1.130 ((and (listp fspec) (eq (car fspec) :internal))
542     (destructuring-bind (_internal next _n) fspec
543     (declare (ignore _internal _n))
544     (fspec-definition-locations next)))
545     (t
546     (let ((defs (excl::find-source-file fspec)))
547     (when (and (null defs)
548     (listp fspec)
549     (string= (car fspec) '#:method))
550     ;; If methods are defined in a defgeneric form, the source location is
551     ;; recorded for the gf but not for the methods. Therefore fall back to
552     ;; the gf as the likely place of definition.
553     (setq defs (excl::find-source-file (second fspec))))
554     (if (null defs)
555     (list
556     (list fspec
557     (make-error-location "Unknown source location for ~A"
558     (fspec->string fspec))))
559 heller 1.133 (loop for (fspec type file top-level) in defs collect
560     (list (list type fspec)
561     (find-fspec-location fspec type file top-level))))))))
562 heller 1.21
563     (defimplementation find-definitions (symbol)
564     (fspec-definition-locations symbol))
565 heller 1.1
566 heller 1.8 ;;;; XREF
567    
568 heller 1.21 (defmacro defxref (name relation name1 name2)
569     `(defimplementation ,name (x)
570     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
571    
572     (defxref who-calls :calls :wild x)
573 heller 1.70 (defxref calls-who :calls x :wild)
574 heller 1.21 (defxref who-references :uses :wild x)
575     (defxref who-binds :binds :wild x)
576     (defxref who-macroexpands :macro-calls :wild x)
577     (defxref who-sets :sets :wild x)
578    
579     (defun xref-result (fspecs)
580     (loop for fspec in fspecs
581     append (fspec-definition-locations fspec)))
582 heller 1.46
583     ;; list-callers implemented by groveling through all fbound symbols.
584     ;; Only symbols are considered. Functions in the constant pool are
585 heller 1.71 ;; searched recursively. Closure environments are ignored at the
586 heller 1.46 ;; moment (constants in methods are therefore not found).
587    
588     (defun map-function-constants (function fn depth)
589     "Call FN with the elements of FUNCTION's constant pool."
590     (do ((i 0 (1+ i))
591     (max (excl::function-constant-count function)))
592     ((= i max))
593     (let ((c (excl::function-constant function i)))
594     (cond ((and (functionp c)
595     (not (eq c function))
596     (plusp depth))
597     (map-function-constants c fn (1- depth)))
598     (t
599     (funcall fn c))))))
600    
601 heller 1.64 (defun in-constants-p (fun symbol)
602     (map-function-constants fun
603     (lambda (c)
604     (when (eq c symbol)
605     (return-from in-constants-p t)))
606     3))
607 heller 1.70
608 heller 1.46 (defun function-callers (name)
609     (let ((callers '()))
610     (do-all-symbols (sym)
611     (when (fboundp sym)
612     (let ((fn (fdefinition sym)))
613     (when (in-constants-p fn name)
614     (push sym callers)))))
615     callers))
616    
617     (defimplementation list-callers (name)
618     (xref-result (function-callers name)))
619 heller 1.4
620 heller 1.70 (defimplementation list-callees (name)
621     (let ((result '()))
622     (map-function-constants (fdefinition name)
623     (lambda (c)
624     (when (fboundp c)
625     (push c result)))
626     2)
627     (xref-result result)))
628    
629 mkoeppe 1.83 ;;;; Profiling
630    
631 heller 1.89 ;; Per-function profiling based on description in
632     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
633    
634     (defvar *profiled-functions* ())
635     (defvar *profile-depth* 0)
636    
637     (defmacro with-redirected-y-or-n-p (&body body)
638     ;; If the profiler is restarted when the data from the previous
639     ;; session is not reported yet, the user is warned via Y-OR-N-P.
640     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
641     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
642     ;; overruled.
643     `(let* ((pkg (find-package "common-lisp"))
644     (saved-pdl (excl::package-definition-lock pkg))
645     (saved-ynp (symbol-function 'cl:y-or-n-p)))
646    
647     (setf (excl::package-definition-lock pkg) nil
648     (symbol-function 'cl:y-or-n-p) (symbol-function
649     (find-symbol "y-or-n-p-in-emacs"
650     "swank")))
651     (unwind-protect
652     (progn ,@body)
653    
654     (setf (symbol-function 'cl:y-or-n-p) saved-ynp
655     (excl::package-definition-lock pkg) saved-pdl))))
656    
657     (defun start-acl-profiler ()
658     (with-redirected-y-or-n-p
659     (prof:start-profiler :type :time :count t
660     :start-sampling-p nil :verbose nil)))
661     (defun acl-profiler-active-p ()
662     (not (eq (prof:profiler-status :verbose nil) :inactive)))
663    
664     (defun stop-acl-profiler ()
665     (prof:stop-profiler :verbose nil))
666    
667     (excl:def-fwrapper profile-fwrapper (&rest args)
668     ;; Ensures sampling is done during the execution of the function,
669     ;; taking into account recursion.
670     (declare (ignore args))
671     (cond ((zerop *profile-depth*)
672     (let ((*profile-depth* (1+ *profile-depth*)))
673     (prof:start-sampling)
674     (unwind-protect (excl:call-next-fwrapper)
675     (prof:stop-sampling))))
676     (t
677     (excl:call-next-fwrapper))))
678    
679     (defimplementation profile (fname)
680     (unless (acl-profiler-active-p)
681     (start-acl-profiler))
682     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
683     (push fname *profiled-functions*))
684    
685     (defimplementation profiled-functions ()
686     *profiled-functions*)
687    
688     (defimplementation unprofile (fname)
689     (excl:funwrap fname 'profile-fwrapper)
690     (setq *profiled-functions* (remove fname *profiled-functions*)))
691    
692 mkoeppe 1.83 (defimplementation profile-report ()
693 heller 1.89 (prof:show-flat-profile :verbose nil)
694     (when *profiled-functions*
695     (start-acl-profiler)))
696    
697     (defimplementation profile-reset ()
698     (when (acl-profiler-active-p)
699     (stop-acl-profiler)
700     (start-acl-profiler))
701     "Reset profiling counters.")
702 mkoeppe 1.83
703 heller 1.18 ;;;; Inspecting
704    
705 heller 1.111 (excl:without-redefinition-warnings
706 heller 1.100 (defmethod emacs-inspect ((o t))
707 heller 1.111 (allegro-inspect o)))
708 heller 1.62
709 heller 1.100 (defmethod emacs-inspect ((o function))
710 heller 1.101 (allegro-inspect o))
711 heller 1.62
712 heller 1.100 (defmethod emacs-inspect ((o standard-object))
713 heller 1.101 (allegro-inspect o))
714 heller 1.82
715 heller 1.62 (defun allegro-inspect (o)
716     (loop for (d dd) on (inspect::inspect-ctl o)
717 heller 1.73 append (frob-allegro-field-def o d)
718     until (eq d dd)))
719 heller 1.62
720 heller 1.73 (defun frob-allegro-field-def (object def)
721 heller 1.62 (with-struct (inspect::field-def- name type access) def
722 heller 1.73 (ecase type
723 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
724     :unsigned-long :unsigned-half-long
725     :unsigned-3byte)
726 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
727 heller 1.115 ((:lisp :value :func)
728 heller 1.73 (label-value-line name (inspect::component-ref object access)))
729     (:indirect
730     (destructuring-bind (prefix count ref set) access
731     (declare (ignore set prefix))
732     (loop for i below (funcall count object)
733     append (label-value-line (format nil "~A-~D" name i)
734     (funcall ref object i))))))))
735 mbaringer 1.55
736 heller 1.18 ;;;; Multithreading
737 heller 1.8
738 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
739     (mp:start-scheduler)
740     (funcall continuation))
741 heller 1.8
742 heller 1.10 (defimplementation spawn (fn &key name)
743 lgorrie 1.47 (mp:process-run-function name fn))
744 heller 1.8
745 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
746     (defvar *thread-id-counter* 0)
747    
748     (defimplementation thread-id (thread)
749     (mp:with-process-lock (*id-lock*)
750     (or (getf (mp:process-property-list thread) 'id)
751     (setf (getf (mp:process-property-list thread) 'id)
752     (incf *thread-id-counter*)))))
753    
754     (defimplementation find-thread (id)
755     (find id mp:*all-processes*
756     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
757    
758 heller 1.13 (defimplementation thread-name (thread)
759     (mp:process-name thread))
760 heller 1.8
761 heller 1.13 (defimplementation thread-status (thread)
762     (format nil "~A ~D" (mp:process-whostate thread)
763     (mp:process-priority thread)))
764 heller 1.8
765 heller 1.10 (defimplementation make-lock (&key name)
766 heller 1.8 (mp:make-process-lock :name name))
767    
768 heller 1.10 (defimplementation call-with-lock-held (lock function)
769 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
770 heller 1.12
771     (defimplementation current-thread ()
772     mp:*current-process*)
773    
774     (defimplementation all-threads ()
775 heller 1.13 (copy-list mp:*all-processes*))
776 heller 1.12
777     (defimplementation interrupt-thread (thread fn)
778     (mp:process-interrupt thread fn))
779    
780 heller 1.16 (defimplementation kill-thread (thread)
781     (mp:process-kill thread))
782    
783 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
784    
785     (defstruct (mailbox (:conc-name mailbox.))
786 heller 1.107 (lock (mp:make-process-lock :name "process mailbox"))
787     (queue '() :type list)
788 heller 1.108 (gate (mp:make-gate nil)))
789 heller 1.12
790     (defun mailbox (thread)
791     "Return THREAD's mailbox."
792     (mp:with-process-lock (*mailbox-lock*)
793     (or (getf (mp:process-property-list thread) 'mailbox)
794     (setf (getf (mp:process-property-list thread) 'mailbox)
795     (make-mailbox)))))
796    
797     (defimplementation send (thread message)
798 heller 1.107 (let* ((mbox (mailbox thread)))
799     (mp:with-process-lock ((mailbox.lock mbox))
800     (setf (mailbox.queue mbox)
801     (nconc (mailbox.queue mbox) (list message)))
802     (mp:open-gate (mailbox.gate mbox)))))
803 heller 1.12
804 heller 1.110 (defimplementation receive-if (test &optional timeout)
805 heller 1.104 (let ((mbox (mailbox mp:*current-process*)))
806 heller 1.110 (assert (or (not timeout) (eq timeout t)))
807 heller 1.107 (loop
808     (check-slime-interrupts)
809     (mp:with-process-lock ((mailbox.lock mbox))
810     (let* ((q (mailbox.queue mbox))
811     (tail (member-if test q)))
812     (when tail
813     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
814     (return (car tail)))
815     (mp:close-gate (mailbox.gate mbox))))
816 heller 1.110 (when (eq timeout t) (return (values nil t)))
817     (mp:process-wait-with-timeout "receive-if" 0.5
818     #'mp:gate-open-p (mailbox.gate mbox)))))
819 heller 1.104
820 heller 1.123 (defimplementation set-default-initial-binding (var form)
821     (setq excl:*cl-default-special-bindings*
822     (acons var form excl:*cl-default-special-bindings*)))
823    
824 mbaringer 1.27 (defimplementation quit-lisp ()
825     (excl:exit 0 :quiet t))
826 mbaringer 1.68
827 heller 1.69
828 mbaringer 1.68 ;;Trace implementations
829     ;;In Allegro 7.0, we have:
830     ;; (trace <name>)
831     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
832     ;; (trace ((labels <name> <label-name>)))
833     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
834     ;; <name> can be a normal name or a (setf name)
835    
836 heller 1.70 (defimplementation toggle-trace (spec)
837 lgorrie 1.72 (ecase (car spec)
838     ((setf)
839     (toggle-trace-aux spec))
840 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
841 heller 1.71 ((setf :defmethod :labels :flet)
842 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
843 lgorrie 1.72 (:call
844 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
845     (toggle-trace-aux callee
846     :inside (list (process-fspec-for-allegro caller)))))))
847    
848     (defun tracedp (fspec)
849 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
850 heller 1.70
851     (defun toggle-trace-aux (fspec &rest args)
852     (cond ((tracedp fspec)
853     (eval `(untrace ,fspec))
854     (format nil "~S is now untraced." fspec))
855     (t
856     (eval `(trace (,fspec ,@args)))
857     (format nil "~S is now traced." fspec))))
858    
859     (defun toggle-trace-generic-function-methods (name)
860 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
861 heller 1.70 (cond ((tracedp name)
862 mbaringer 1.68 (eval `(untrace ,name))
863     (dolist (method methods (format nil "~S is now untraced." name))
864     (excl:funtrace (mop:method-function method))))
865     (t
866 lgorrie 1.72 (eval `(trace (,name)))
867 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
868 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
869    
870     (defun process-fspec-for-allegro (fspec)
871     (cond ((consp fspec)
872     (ecase (first fspec)
873 heller 1.71 ((setf) fspec)
874 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
875     ((:defmethod) `(method ,@(rest fspec)))
876 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
877     ,(third fspec)))
878     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
879     ,(third fspec)))))
880 mbaringer 1.68 (t
881     fspec)))
882 heller 1.88
883    
884     ;;;; Weak hashtables
885    
886     (defimplementation make-weak-key-hash-table (&rest args)
887     (apply #'make-hash-table :weak-keys t args))
888    
889     (defimplementation make-weak-value-hash-table (&rest args)
890     (apply #'make-hash-table :values :weak args))
891 mkoeppe 1.93
892 alendvai 1.95 (defimplementation hash-table-weakness (hashtable)
893     (cond ((excl:hash-table-weak-keys hashtable) :key)
894     ((eq (excl:hash-table-values hashtable) :weak) :value)))
895    
896    
897 mkoeppe 1.93
898     ;;;; Character names
899    
900     (defimplementation character-completion-set (prefix matchp)
901     (loop for name being the hash-keys of excl::*name-to-char-table*
902     when (funcall matchp prefix name)
903     collect (string-capitalize name)))

  ViewVC Help
Powered by ViewVC 1.1.5