coverage.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;
  3. ;;; Copyright (C) 2010 Free Software Foundation, Inc.
  4. ;;;
  5. ;;; This library is free software; you can redistribute it and/or
  6. ;;; modify it under the terms of the GNU Lesser General Public
  7. ;;; License as published by the Free Software Foundation; either
  8. ;;; version 3 of the License, or (at your option) any later version.
  9. ;;;
  10. ;;; This library is distributed in the hope that it will be useful,
  11. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;; Lesser General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU Lesser General Public
  16. ;;; License along with this library; if not, write to the Free Software
  17. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (system vm coverage)
  19. #:use-module (system vm vm)
  20. #:use-module (system vm frame)
  21. #:use-module (system vm program)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (srfi srfi-26)
  26. #:export (with-code-coverage
  27. coverage-data?
  28. instrumented-source-files
  29. instrumented/executed-lines
  30. line-execution-counts
  31. procedure-execution-count
  32. coverage-data->lcov))
  33. ;;; Author: Ludovic Courtès
  34. ;;;
  35. ;;; Commentary:
  36. ;;;
  37. ;;; This module provides support to gather code coverage data by instrumenting
  38. ;;; the VM.
  39. ;;;
  40. ;;; Code:
  41. ;;;
  42. ;;; Gathering coverage data.
  43. ;;;
  44. (define (hashq-proc proc n)
  45. ;; Return the hash of PROC's objcode.
  46. (hashq (program-objcode proc) n))
  47. (define (assq-proc proc alist)
  48. ;; Instead of really looking for PROC in ALIST, look for the objcode of PROC.
  49. ;; IOW the alist is indexed by procedures, not objcodes, but those procedures
  50. ;; are taken as an arbitrary representative of all the procedures (closures)
  51. ;; sharing that objcode. This can significantly reduce memory consumption.
  52. (let ((code (program-objcode proc)))
  53. (find (lambda (pair)
  54. (eq? code (program-objcode (car pair))))
  55. alist)))
  56. (define (with-code-coverage vm thunk)
  57. "Run THUNK, a zero-argument procedure, using VM; instrument VM to collect code
  58. coverage data. Return code coverage data and the values returned by THUNK."
  59. (define procedure->ip-counts
  60. ;; Mapping from procedures to hash tables; said hash tables map instruction
  61. ;; pointers to the number of times they were executed.
  62. (make-hash-table 500))
  63. (define (collect! frame)
  64. ;; Update PROCEDURE->IP-COUNTS with info from FRAME.
  65. (let* ((proc (frame-procedure frame))
  66. (ip (frame-instruction-pointer frame))
  67. (proc-entry (hashx-create-handle! hashq-proc assq-proc
  68. procedure->ip-counts proc #f)))
  69. (let loop ()
  70. (define ip-counts (cdr proc-entry))
  71. (if ip-counts
  72. (let ((ip-entry (hashv-create-handle! ip-counts ip 0)))
  73. (set-cdr! ip-entry (+ (cdr ip-entry) 1)))
  74. (begin
  75. (set-cdr! proc-entry (make-hash-table))
  76. (loop))))))
  77. ;; FIXME: It's unclear what the dynamic-wind is for, given that if the
  78. ;; VM is different from the current one, continuations will not be
  79. ;; resumable.
  80. (call-with-values (lambda ()
  81. (let ((level (vm-trace-level vm))
  82. (hook (vm-next-hook vm)))
  83. (dynamic-wind
  84. (lambda ()
  85. (set-vm-trace-level! vm (+ level 1))
  86. (add-hook! hook collect!))
  87. (lambda ()
  88. (call-with-vm vm thunk))
  89. (lambda ()
  90. (set-vm-trace-level! vm level)
  91. (remove-hook! hook collect!)))))
  92. (lambda args
  93. (apply values (make-coverage-data procedure->ip-counts) args))))
  94. ;;;
  95. ;;; Coverage data summary.
  96. ;;;
  97. (define-record-type <coverage-data>
  98. (%make-coverage-data procedure->ip-counts
  99. procedure->sources
  100. file->procedures
  101. file->line-counts)
  102. coverage-data?
  103. ;; Mapping from procedures to hash tables; said hash tables map instruction
  104. ;; pointers to the number of times they were executed.
  105. (procedure->ip-counts data-procedure->ip-counts)
  106. ;; Mapping from procedures to the result of `program-sources'.
  107. (procedure->sources data-procedure->sources)
  108. ;; Mapping from source file names to lists of procedures defined in the file.
  109. (file->procedures data-file->procedures)
  110. ;; Mapping from file names to hash tables, which in turn map from line numbers
  111. ;; to execution counts.
  112. (file->line-counts data-file->line-counts))
  113. (define (make-coverage-data procedure->ip-counts)
  114. ;; Return a `coverage-data' object based on the coverage data available in
  115. ;; PROCEDURE->IP-COUNTS. Precompute the other hash tables that make up
  116. ;; `coverage-data' objects.
  117. (let* ((procedure->sources (make-hash-table 500))
  118. (file->procedures (make-hash-table 100))
  119. (file->line-counts (make-hash-table 100))
  120. (data (%make-coverage-data procedure->ip-counts
  121. procedure->sources
  122. file->procedures
  123. file->line-counts)))
  124. (define (increment-execution-count! file line count)
  125. ;; Make the execution count of FILE:LINE the maximum of its current value
  126. ;; and COUNT. This is so that LINE's execution count is correct when
  127. ;; several instruction pointers map to LINE.
  128. (let ((file-entry (hash-create-handle! file->line-counts file #f)))
  129. (if (not (cdr file-entry))
  130. (set-cdr! file-entry (make-hash-table 500)))
  131. (let ((line-entry (hashv-create-handle! (cdr file-entry) line 0)))
  132. (set-cdr! line-entry (max (cdr line-entry) count)))))
  133. ;; Update execution counts for procs that were executed.
  134. (hash-for-each (lambda (proc ip-counts)
  135. (let* ((sources (program-sources* data proc))
  136. (file (and (pair? sources)
  137. (source:file (car sources)))))
  138. (and file
  139. (begin
  140. ;; Add a zero count for all IPs in SOURCES and in
  141. ;; the sources of procedures closed over by PROC.
  142. (for-each
  143. (lambda (source)
  144. (let ((file (source:file source))
  145. (line (source:line source)))
  146. (increment-execution-count! file line 0)))
  147. (append-map (cut program-sources* data <>)
  148. (closed-over-procedures proc)))
  149. ;; Add the actual execution count collected.
  150. (hash-for-each
  151. (lambda (ip count)
  152. (let ((line (closest-source-line sources ip)))
  153. (increment-execution-count! file line count)))
  154. ip-counts)))))
  155. procedure->ip-counts)
  156. ;; Set the execution count to zero for procedures loaded and not executed.
  157. ;; FIXME: Traversing thousands of procedures here is inefficient.
  158. (for-each (lambda (proc)
  159. (and (not (hashq-ref procedure->sources proc))
  160. (for-each (lambda (proc)
  161. (let* ((sources (program-sources* data proc))
  162. (file (and (pair? sources)
  163. (source:file (car sources)))))
  164. (and file
  165. (for-each
  166. (lambda (ip)
  167. (let ((line (closest-source-line sources ip)))
  168. (increment-execution-count! file line 0)))
  169. (map source:addr sources)))))
  170. (closed-over-procedures proc))))
  171. (append-map module-procedures (loaded-modules)))
  172. data))
  173. (define (procedure-execution-count data proc)
  174. "Return the number of times PROC's code was executed, according to DATA, or #f
  175. if PROC was not executed. When PROC is a closure, the number of times its code
  176. was executed is returned, not the number of times this code associated with this
  177. particular closure was executed."
  178. (let ((sources (program-sources* data proc)))
  179. (and (pair? sources)
  180. (and=> (hashx-ref hashq-proc assq-proc
  181. (data-procedure->ip-counts data) proc)
  182. (lambda (ip-counts)
  183. ;; FIXME: broken with lambda*
  184. (let ((entry-ip (source:addr (car sources))))
  185. (hashv-ref ip-counts entry-ip 0)))))))
  186. (define (program-sources* data proc)
  187. ;; A memoizing version of `program-sources'.
  188. (or (hashq-ref (data-procedure->sources data) proc)
  189. (and (program? proc)
  190. (let ((sources (program-sources proc))
  191. (p->s (data-procedure->sources data))
  192. (f->p (data-file->procedures data)))
  193. (if (pair? sources)
  194. (let* ((file (source:file (car sources)))
  195. (entry (hash-create-handle! f->p file '())))
  196. (hashq-set! p->s proc sources)
  197. (set-cdr! entry (cons proc (cdr entry)))
  198. sources)
  199. sources)))))
  200. (define (file-procedures data file)
  201. ;; Return the list of globally bound procedures defined in FILE.
  202. (hash-ref (data-file->procedures data) file '()))
  203. (define (instrumented/executed-lines data file)
  204. "Return the number of instrumented and the number of executed source lines in
  205. FILE according to DATA."
  206. (define instr+exec
  207. (and=> (hash-ref (data-file->line-counts data) file)
  208. (lambda (line-counts)
  209. (hash-fold (lambda (line count instr+exec)
  210. (let ((instr (car instr+exec))
  211. (exec (cdr instr+exec)))
  212. (cons (+ 1 instr)
  213. (if (> count 0)
  214. (+ 1 exec)
  215. exec))))
  216. '(0 . 0)
  217. line-counts))))
  218. (values (car instr+exec) (cdr instr+exec)))
  219. (define (line-execution-counts data file)
  220. "Return a list of line number/execution count pairs for FILE, or #f if FILE
  221. is not among the files covered by DATA."
  222. (and=> (hash-ref (data-file->line-counts data) file)
  223. (lambda (line-counts)
  224. (hash-fold alist-cons '() line-counts))))
  225. (define (instrumented-source-files data)
  226. "Return the list of `instrumented' source files, i.e., source files whose code
  227. was loaded at the time DATA was collected."
  228. (hash-fold (lambda (file counts files)
  229. (cons file files))
  230. '()
  231. (data-file->line-counts data)))
  232. ;;;
  233. ;;; Helpers.
  234. ;;;
  235. (define (loaded-modules)
  236. ;; Return the list of all the modules currently loaded.
  237. (define seen (make-hash-table))
  238. (let loop ((modules (module-submodules (resolve-module '() #f)))
  239. (result '()))
  240. (hash-fold (lambda (name module result)
  241. (if (hashq-ref seen module)
  242. result
  243. (begin
  244. (hashq-set! seen module #t)
  245. (loop (module-submodules module)
  246. (cons module result)))))
  247. result
  248. modules)))
  249. (define (module-procedures module)
  250. ;; Return the list of procedures bound globally in MODULE.
  251. (hash-fold (lambda (binding var result)
  252. (if (variable-bound? var)
  253. (let ((value (variable-ref var)))
  254. (if (procedure? value)
  255. (cons value result)
  256. result))
  257. result))
  258. '()
  259. (module-obarray module)))
  260. (define (closest-source-line sources ip)
  261. ;; Given SOURCES, as returned by `program-sources' for a given procedure,
  262. ;; return the source line of code that is the closest to IP. This is similar
  263. ;; to what `program-source' does.
  264. (let loop ((sources sources)
  265. (line (and (pair? sources) (source:line (car sources)))))
  266. (if (null? sources)
  267. line
  268. (let ((source (car sources)))
  269. (if (> (source:addr source) ip)
  270. line
  271. (loop (cdr sources) (source:line source)))))))
  272. (define (closed-over-procedures proc)
  273. ;; Return the list of procedures PROC closes over, PROC included.
  274. (let loop ((proc proc)
  275. (result '()))
  276. (if (and (program? proc) (not (memq proc result)))
  277. (fold loop (cons proc result)
  278. (append (vector->list (or (program-objects proc) #()))
  279. (program-free-variables proc)))
  280. result)))
  281. ;;;
  282. ;;; LCOV output.
  283. ;;;
  284. (define* (coverage-data->lcov data port)
  285. "Traverse code coverage information DATA, as obtained with
  286. `with-code-coverage', and write coverage information in the LCOV format to PORT.
  287. The report will include all the modules loaded at the time coverage data was
  288. gathered, even if their code was not executed."
  289. (define (dump-function proc)
  290. ;; Dump source location and basic coverage data for PROC.
  291. (and (program? proc)
  292. (let ((sources (program-sources* data proc)))
  293. (and (pair? sources)
  294. (let* ((line (source:line-for-user (car sources)))
  295. (name (or (procedure-name proc)
  296. (format #f "anonymous-l~a" line))))
  297. (format port "FN:~A,~A~%" line name)
  298. (and=> (procedure-execution-count data proc)
  299. (lambda (count)
  300. (format port "FNDA:~A,~A~%" count name))))))))
  301. ;; Output per-file coverage data.
  302. (format port "TN:~%")
  303. (for-each (lambda (file)
  304. (let ((procs (file-procedures data file))
  305. (path (search-path %load-path file)))
  306. (if (string? path)
  307. (begin
  308. (format port "SF:~A~%" path)
  309. (for-each dump-function procs)
  310. (for-each (lambda (line+count)
  311. (let ((line (car line+count))
  312. (count (cdr line+count)))
  313. (format port "DA:~A,~A~%"
  314. (+ 1 line) count)))
  315. (line-execution-counts data file))
  316. (let-values (((instr exec)
  317. (instrumented/executed-lines data file)))
  318. (format port "LH: ~A~%" exec)
  319. (format port "LF: ~A~%" instr))
  320. (format port "end_of_record~%"))
  321. (begin
  322. (format (current-error-port)
  323. "skipping unknown source file: ~a~%"
  324. file)))))
  325. (instrumented-source-files data)))