find-gc.el 5.04 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1 2
;;; find-gc.el --- detect functions that call the garbage collector

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1992, 2001-2020 Free Software Foundation, Inc.
Gerd Moellmann's avatar
Gerd Moellmann committed
4

5 6
;; Maintainer: emacs-devel@gnu.org

Gerd Moellmann's avatar
Gerd Moellmann committed
7 8
;; This file is part of GNU Emacs.

9
;; GNU Emacs is free software: you can redistribute it and/or modify
Gerd Moellmann's avatar
Gerd Moellmann committed
10
;; it under the terms of the GNU General Public License as published by
11 12
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Gerd Moellmann's avatar
Gerd Moellmann committed
13 14 15 16 17 18 19

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
20
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Gerd Moellmann's avatar
Gerd Moellmann committed
21 22 23

;;; Commentary:

24 25
;; Produce in find-gc-unsafe-list the set of all functions that may invoke GC.
;; This expects the Emacs sources to live in find-gc-source-directory.
Gerd Moellmann's avatar
Gerd Moellmann committed
26 27 28

;;; Code:

29 30 31
(defvar find-gc-unsafe-list nil
  "The list of unsafe functions is placed here by `find-gc-unsafe'.")

Glenn Morris's avatar
Glenn Morris committed
32 33 34
(defvar find-gc-source-directory
  (file-name-as-directory (expand-file-name "src" source-directory))
  "Directory containing Emacs C sources.")
35

36 37 38 39 40 41 42 43
(defvar find-gc-subrs-callers nil
  "Alist of users of subrs, from GC testing.
Each entry has the form (FUNCTION . FUNCTIONS-THAT-CALL-IT).")

(defvar find-gc-subrs-called nil
  "Alist of subrs called, in GC testing.
Each entry has the form (FUNCTION . FUNCTIONS-IT-CALLS).")

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

;;; Functions on this list are safe, even if they appear to be able
;;; to call the target.

(defvar find-gc-noreturn-list '(Fsignal Fthrow wrong_type_argument))

;;; This was originally generated directory-files, but there were
;;; too many files there that were not actually compiled.  The
;;; list below was created for a HP-UX 7.0 system.

(defvar find-gc-source-files
  '("dispnew.c" "scroll.c" "xdisp.c" "window.c"
    "term.c" "cm.c" "emacs.c" "keyboard.c" "macros.c"
    "keymap.c" "sysdep.c" "buffer.c" "filelock.c"
    "insdel.c" "marker.c" "minibuf.c" "fileio.c"
59
    "dired.c" "cmds.c" "casefiddle.c"
60
    "indent.c" "search.c" "regex-emacs.c" "undo.c"
61 62
    "alloc.c" "data.c" "doc.c" "editfns.c"
    "callint.c" "eval.c" "fns.c" "print.c" "lread.c"
Glenn Morris's avatar
Glenn Morris committed
63
    "syntax.c" "unexcoff.c"
64
    "bytecode.c" "process.c" "callproc.c" "doprnt.c"
Glenn Morris's avatar
Glenn Morris committed
65
    "xterm.c" "xfns.c"))
66 67


Gerd Moellmann's avatar
Gerd Moellmann committed
68
(defun find-gc-unsafe ()
69
  "Return a list of unsafe functions--that is, which can call GC.
Glenn Morris's avatar
Glenn Morris committed
70
Also store it in `find-gc-unsafe-list'."
Gerd Moellmann's avatar
Gerd Moellmann committed
71 72 73
  (trace-call-tree nil)
  (trace-use-tree)
  (find-unsafe-funcs 'Fgarbage_collect)
74 75 76
  (setq find-gc-unsafe-list
	(sort find-gc-unsafe-list
	      (function (lambda (x y)
77
			  (string-lessp (car x) (car y)))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
78 79 80

;;; This does a depth-first search to find all functions that can
;;; 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
Gerd Moellmann's avatar
Gerd Moellmann committed
82 83 84 85
;;; are (one of) the unsafe functions that these functions directly
;;; call.

(defun find-unsafe-funcs (target)
86
  (setq find-gc-unsafe-list (list (list target)))
87
  (trace-unsafe target))
Gerd Moellmann's avatar
Gerd Moellmann committed
88 89

(defun trace-unsafe (func)
90
  (let ((used (assq func find-gc-subrs-callers)))
Gerd Moellmann's avatar
Gerd Moellmann committed
91
    (or used
92
	(error "No find-gc-subrs-callers for %s" (car find-gc-unsafe-list)))
Gerd Moellmann's avatar
Gerd Moellmann committed
93
    (while (setq used (cdr used))
94 95
      (or (assq (car used) find-gc-unsafe-list)
	  (memq (car used) find-gc-noreturn-list)
Gerd Moellmann's avatar
Gerd Moellmann committed
96
	  (progn
97
	    (push (cons (car used) func) find-gc-unsafe-list)
98
	    (trace-unsafe (car used)))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
99 100 101 102




Glenn Morris's avatar
Glenn Morris committed
103
(defun trace-call-tree (&optional ignored)
Gerd Moellmann's avatar
Gerd Moellmann committed
104
  (message "Setting up directories...")
Glenn Morris's avatar
Glenn Morris committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  (setq find-gc-subrs-called nil)
  (let ((case-fold-search nil)
	(default-directory find-gc-source-directory)
	(files find-gc-source-files)
	name entry rtlfile)
    (dolist (file files)
      (message "Compiling %s..." file)
      (call-process "gcc" nil nil nil "-I" "." "-I" "../lib"
		    "-fdump-rtl-expand" "-o" null-device "-c" file)
      (setq rtlfile
	    (file-expand-wildcards (format "%s.*.expand" file) t))
      (if (/= 1 (length rtlfile))
	  (message "Error compiling `%s'?" file)
	(with-temp-buffer
	  (insert-file-contents (setq rtlfile (car rtlfile)))
	  (delete-file rtlfile)
	  (while (re-search-forward ";; Function \\|(call_insn " nil t)
	    (if (= (char-after (- (point) 3)) ?o)
Gerd Moellmann's avatar
Gerd Moellmann committed
123
		(progn
Glenn Morris's avatar
Glenn Morris committed
124 125 126 127 128 129 130 131 132 133 134
		  (looking-at "[a-zA-Z0-9_]+")
		  (setq name (intern (match-string 0)))
		  (message "%s : %s" (car files) name)
		  (setq entry (list name)
			find-gc-subrs-called
			(cons entry find-gc-subrs-called)))
	      (if (looking-at ".*\n?.*\"\\([A-Za-z0-9_]+\\)\"")
		  (progn
		    (setq name (intern (match-string 1)))
		    (or (memq name (cdr entry))
			(setcdr entry (cons name (cdr entry)))))))))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
135 136

(defun trace-use-tree ()
137 138
  (setq find-gc-subrs-callers (mapcar 'list (mapcar 'car find-gc-subrs-called)))
  (let ((ptr find-gc-subrs-called)
Gerd Moellmann's avatar
Gerd Moellmann committed
139 140 141 142
	p2 found)
    (while ptr
      (setq p2 (car ptr))
      (while (setq p2 (cdr p2))
143
	(if (setq found (assq (car p2) find-gc-subrs-callers))
Gerd Moellmann's avatar
Gerd Moellmann committed
144
	    (setcdr found (cons (car (car ptr)) (cdr found)))))
145
      (setq ptr (cdr ptr)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
146 147 148 149

(provide 'find-gc)

;;; find-gc.el ends here