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