Add 2011 to FSF/AIST copyright years.
[emacs.git] / lisp / emacs-lisp / find-gc.el
blob7f4d8918a35bae16696462828821d14267e4a015
1 ;;; find-gc.el --- detect functions that call the garbage collector
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Produce in find-gc-unsafe-list the set of all functions that may invoke GC.
26 ;; This expects the Emacs sources to live in find-gc-source-directory.
27 ;; It creates a temporary working directory /tmp/esrc.
29 ;;; Code:
31 (defvar find-gc-unsafe-list nil
32 "The list of unsafe functions is placed here by `find-gc-unsafe'.")
34 (defvar find-gc-source-directory)
36 (defvar find-gc-subrs-callers nil
37 "Alist of users of subrs, from GC testing.
38 Each entry has the form (FUNCTION . FUNCTIONS-THAT-CALL-IT).")
40 (defvar find-gc-subrs-called nil
41 "Alist of subrs called, in GC testing.
42 Each entry has the form (FUNCTION . FUNCTIONS-IT-CALLS).")
45 ;;; Functions on this list are safe, even if they appear to be able
46 ;;; to call the target.
48 (defvar find-gc-noreturn-list '(Fsignal Fthrow wrong_type_argument))
50 ;;; This was originally generated directory-files, but there were
51 ;;; too many files there that were not actually compiled. The
52 ;;; list below was created for a HP-UX 7.0 system.
54 (defvar find-gc-source-files
55 '("dispnew.c" "scroll.c" "xdisp.c" "window.c"
56 "term.c" "cm.c" "emacs.c" "keyboard.c" "macros.c"
57 "keymap.c" "sysdep.c" "buffer.c" "filelock.c"
58 "insdel.c" "marker.c" "minibuf.c" "fileio.c"
59 "dired.c" "filemode.c" "cmds.c" "casefiddle.c"
60 "indent.c" "search.c" "regex.c" "undo.c"
61 "alloc.c" "data.c" "doc.c" "editfns.c"
62 "callint.c" "eval.c" "fns.c" "print.c" "lread.c"
63 "abbrev.c" "syntax.c" "unexec.c"
64 "bytecode.c" "process.c" "callproc.c" "doprnt.c"
65 "x11term.c" "x11fns.c"))
68 (defun find-gc-unsafe ()
69 "Return a list of unsafe functions--that is, which can call GC.
70 Also store it in `find-gc-unsafe'."
71 (trace-call-tree nil)
72 (trace-use-tree)
73 (find-unsafe-funcs 'Fgarbage_collect)
74 (setq find-gc-unsafe-list
75 (sort find-gc-unsafe-list
76 (function (lambda (x y)
77 (string-lessp (car x) (car y))))))
80 ;;; This does a depth-first search to find all functions that can
81 ;;; ultimately call the function "target". The result is an a-list
82 ;;; in find-gc-unsafe-list; the cars are the unsafe functions, and the cdrs
83 ;;; are (one of) the unsafe functions that these functions directly
84 ;;; call.
86 (defun find-unsafe-funcs (target)
87 (setq find-gc-unsafe-list (list (list target)))
88 (trace-unsafe target)
91 (defun trace-unsafe (func)
92 (let ((used (assq func find-gc-subrs-callers)))
93 (or used
94 (error "No find-gc-subrs-callers for %s" (car find-gc-unsafe-list)))
95 (while (setq used (cdr used))
96 (or (assq (car used) find-gc-unsafe-list)
97 (memq (car used) find-gc-noreturn-list)
98 (progn
99 (push (cons (car used) func) find-gc-unsafe-list)
100 (trace-unsafe (car used))))))
106 (defun trace-call-tree (&optional already-setup)
107 (message "Setting up directories...")
108 (or already-setup
109 (progn
110 ;; Gee, wouldn't a built-in "system" function be handy here.
111 (call-process "csh" nil nil nil "-c" "rm -rf /tmp/esrc")
112 (call-process "csh" nil nil nil "-c" "mkdir /tmp/esrc")
113 (call-process "csh" nil nil nil "-c"
114 (format "ln -s %s/*.[ch] /tmp/esrc"
115 find-gc-source-directory))))
116 (with-current-buffer (get-buffer-create "*Trace Call Tree*")
117 (setq find-gc-subrs-called nil)
118 (let ((case-fold-search nil)
119 (files find-gc-source-files)
120 name entry)
121 (while files
122 (message "Compiling %s..." (car files))
123 (call-process "csh" nil nil nil "-c"
124 (format "gcc -dr -c /tmp/esrc/%s -o /dev/null"
125 (car files)))
126 (erase-buffer)
127 (insert-file-contents (concat "/tmp/esrc/" (car files) ".rtl"))
128 (while (re-search-forward ";; Function \\|(call_insn " nil t)
129 (if (= (char-after (- (point) 3)) ?o)
130 (progn
131 (looking-at "[a-zA-Z0-9_]+")
132 (setq name (intern (buffer-substring (match-beginning 0)
133 (match-end 0))))
134 (message "%s : %s" (car files) name)
135 (setq entry (list name)
136 find-gc-subrs-called (cons entry find-gc-subrs-called)))
137 (if (looking-at ".*\n?.*\"\\([A-Za-z0-9_]+\\)\"")
138 (progn
139 (setq name (intern (buffer-substring (match-beginning 1)
140 (match-end 1))))
141 (or (memq name (cdr entry))
142 (setcdr entry (cons name (cdr entry))))))))
143 (delete-file (concat "/tmp/esrc/" (car files) ".rtl"))
144 (setq files (cdr files)))))
148 (defun trace-use-tree ()
149 (setq find-gc-subrs-callers (mapcar 'list (mapcar 'car find-gc-subrs-called)))
150 (let ((ptr find-gc-subrs-called)
151 p2 found)
152 (while ptr
153 (setq p2 (car ptr))
154 (while (setq p2 (cdr p2))
155 (if (setq found (assq (car p2) find-gc-subrs-callers))
156 (setcdr found (cons (car (car ptr)) (cdr found)))))
157 (setq ptr (cdr ptr))))
160 (provide 'find-gc)
162 ;; arch-tag: 4a26a538-a008-40d9-a1ef-23bb6dbecef4
163 ;;; find-gc.el ends here