entered into RCS
[emacs.git] / lisp / find-gc.el
blob38088a3ba588fe8f18ed6316a0d2d0c943b03100
1 ;;; find-gc.el --- detect functions that call the garbage collector
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
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 2, or (at your option)
12 ;; 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; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;; Code:
25 ;;; Produce in unsafe-list the set of all functions that may invoke GC.
26 ;;; This expects the Emacs sources to live in emacs-source-directory.
27 ;;; It creates a temporary working directory /tmp/esrc.
29 (defun find-gc-unsafe ()
30 (trace-call-tree nil)
31 (trace-use-tree)
32 (find-unsafe-funcs 'Fgarbage_collect)
33 (setq unsafe-list (sort unsafe-list
34 (function (lambda (x y)
35 (string-lessp (car x) (car y))))))
38 (setq emacs-source-directory "/usr/gnu/src/dist/src")
41 ;;; This does a depth-first search to find all functions that can
42 ;;; ultimately call the function "target". The result is an a-list
43 ;;; in unsafe-list; the cars are the unsafe functions, and the cdrs
44 ;;; are (one of) the unsafe functions that these functions directly
45 ;;; call.
47 (defun find-unsafe-funcs (target)
48 (setq unsafe-list (list (list target)))
49 (trace-unsafe target)
52 (defun trace-unsafe (func)
53 (let ((used (assq func subrs-used)))
54 (or used
55 (error "No subrs-used for %s" (car unsafe-list)))
56 (while (setq used (cdr used))
57 (or (assq (car used) unsafe-list)
58 (memq (car used) noreturn-list)
59 (progn
60 (setq unsafe-list (cons (cons (car used) func) unsafe-list))
61 (trace-unsafe (car used))))))
65 ;;; Functions on this list are safe, even if they appear to be able
66 ;;; to call the target.
68 (setq noreturn-list '( Fsignal Fthrow wrong_type_argument ))
71 ;;; This produces an a-list of functions in subrs-called. The cdr of
72 ;;; each entry is a list of functions which the function in car calls.
74 (defun trace-call-tree (&optional already-setup)
75 (message "Setting up directories...")
76 (or already-setup
77 (progn
78 ;; Gee, wouldn't a built-in "system" function be handy here.
79 (call-process "csh" nil nil nil "-c" "rm -rf /tmp/esrc")
80 (call-process "csh" nil nil nil "-c" "mkdir /tmp/esrc")
81 (call-process "csh" nil nil nil "-c"
82 (format "ln -s %s/*.[ch] /tmp/esrc"
83 emacs-source-directory))))
84 (save-excursion
85 (set-buffer (get-buffer-create "*Trace Call Tree*"))
86 (setq subrs-called nil)
87 (let ((case-fold-search nil)
88 (files source-files)
89 name entry)
90 (while files
91 (message "Compiling %s..." (car files))
92 (call-process "csh" nil nil nil "-c"
93 (format "gcc -dr -c /tmp/esrc/%s -o /dev/null"
94 (car files)))
95 (erase-buffer)
96 (insert-file-contents (concat "/tmp/esrc/" (car files) ".rtl"))
97 (while (re-search-forward ";; Function \\|(call_insn " nil t)
98 (if (= (char-after (- (point) 3)) ?o)
99 (progn
100 (looking-at "[a-zA-Z0-9_]+")
101 (setq name (intern (buffer-substring (match-beginning 0)
102 (match-end 0))))
103 (message "%s : %s" (car files) name)
104 (setq entry (list name)
105 subrs-called (cons entry subrs-called)))
106 (if (looking-at ".*\n?.*\"\\([A-Za-z0-9_]+\\)\"")
107 (progn
108 (setq name (intern (buffer-substring (match-beginning 1)
109 (match-end 1))))
110 (or (memq name (cdr entry))
111 (setcdr entry (cons name (cdr entry))))))))
112 (delete-file (concat "/tmp/esrc/" (car files) ".rtl"))
113 (setq files (cdr files)))))
117 ;;; This was originally generated directory-files, but there were
118 ;;; too many files there that were not actually compiled. The
119 ;;; list below was created for a HP-UX 7.0 system.
121 (setq source-files '("dispnew.c" "scroll.c" "xdisp.c" "window.c"
122 "term.c" "cm.c" "emacs.c" "keyboard.c" "macros.c"
123 "keymap.c" "sysdep.c" "buffer.c" "filelock.c"
124 "insdel.c" "marker.c" "minibuf.c" "fileio.c"
125 "dired.c" "filemode.c" "cmds.c" "casefiddle.c"
126 "indent.c" "search.c" "regex.c" "undo.c"
127 "alloc.c" "data.c" "doc.c" "editfns.c"
128 "callint.c" "eval.c" "fns.c" "print.c" "lread.c"
129 "abbrev.c" "syntax.c" "unexec.c" "mocklisp.c"
130 "bytecode.c" "process.c" "callproc.c" "doprnt.c"
131 "x11term.c" "x11fns.c"))
134 ;;; This produces an inverted a-list in subrs-used. The cdr of each
135 ;;; entry is a list of functions that call the function in car.
137 (defun trace-use-tree ()
138 (setq subrs-used (mapcar 'list (mapcar 'car subrs-called)))
139 (let ((ptr subrs-called)
140 p2 found)
141 (while ptr
142 (setq p2 (car ptr))
143 (while (setq p2 (cdr p2))
144 (if (setq found (assq (car p2) subrs-used))
145 (setcdr found (cons (car (car ptr)) (cdr found)))))
146 (setq ptr (cdr ptr))))
149 ;;; find-gc.el ends here