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