find-gc.el 5.3 KB

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