1 ;;; find-gc.el --- detect functions that call the garbage collector
3 ;; Copyright (C) 1992, 2001-2012 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
26 ;; It creates a temporary working directory /tmp/esrc.
30 (defvar find-gc-unsafe-list nil
31 "The list of unsafe functions is placed here by `find-gc-unsafe'.")
33 (defvar find-gc-source-directory
)
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).")
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).")
44 ;;; Functions on this list are safe, even if they appear to be able
45 ;;; to call the target.
47 (defvar find-gc-noreturn-list
'(Fsignal Fthrow wrong_type_argument
))
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.
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"
58 "dired.c" "cmds.c" "casefiddle.c"
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"
62 "abbrev.c" "syntax.c" "unexcoff.c"
63 "bytecode.c" "process.c" "callproc.c" "doprnt.c"
64 "x11term.c" "x11fns.c"))
67 (defun find-gc-unsafe ()
68 "Return a list of unsafe functions--that is, which can call GC.
69 Also store it in `find-gc-unsafe'."
72 (find-unsafe-funcs 'Fgarbage_collect
)
73 (setq find-gc-unsafe-list
74 (sort find-gc-unsafe-list
75 (function (lambda (x y
)
76 (string-lessp (car x
) (car y
))))))
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
81 ;;; in find-gc-unsafe-list; the cars are the unsafe functions, and the cdrs
82 ;;; are (one of) the unsafe functions that these functions directly
85 (defun find-unsafe-funcs (target)
86 (setq find-gc-unsafe-list
(list (list target
)))
90 (defun trace-unsafe (func)
91 (let ((used (assq func find-gc-subrs-callers
)))
93 (error "No find-gc-subrs-callers for %s" (car find-gc-unsafe-list
)))
94 (while (setq used
(cdr used
))
95 (or (assq (car used
) find-gc-unsafe-list
)
96 (memq (car used
) find-gc-noreturn-list
)
98 (push (cons (car used
) func
) find-gc-unsafe-list
)
99 (trace-unsafe (car used
))))))
105 (defun trace-call-tree (&optional already-setup
)
106 (message "Setting up directories...")
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"
114 find-gc-source-directory
))))
115 (with-current-buffer (get-buffer-create "*Trace Call Tree*")
116 (setq find-gc-subrs-called nil
)
117 (let ((case-fold-search nil
)
118 (files find-gc-source-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"
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
)
130 (looking-at "[a-zA-Z0-9_]+")
131 (setq name
(intern (buffer-substring (match-beginning 0)
133 (message "%s : %s" (car files
) name
)
134 (setq entry
(list name
)
135 find-gc-subrs-called
(cons entry find-gc-subrs-called
)))
136 (if (looking-at ".*\n?.*\"\\([A-Za-z0-9_]+\\)\"")
138 (setq name
(intern (buffer-substring (match-beginning 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
)))))
147 (defun trace-use-tree ()
148 (setq find-gc-subrs-callers
(mapcar 'list
(mapcar 'car find-gc-subrs-called
)))
149 (let ((ptr find-gc-subrs-called
)
153 (while (setq p2
(cdr p2
))
154 (if (setq found
(assq (car p2
) find-gc-subrs-callers
))
155 (setcdr found
(cons (car (car ptr
)) (cdr found
)))))
156 (setq ptr
(cdr ptr
))))
161 ;;; find-gc.el ends here