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