coverage.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;
  3. ;;; Copyright (C) 2010, 2013, 2018 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 (system vm debug)
  23. #:use-module (ice-9 format)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-9)
  26. #:use-module (srfi srfi-9 gnu)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (ice-9 match)
  30. #:export (with-code-coverage
  31. coverage-data?
  32. instrumented-source-files
  33. instrumented/executed-lines
  34. line-execution-counts
  35. procedure-execution-count
  36. coverage-data->lcov))
  37. ;;; Author: Ludovic Courtès
  38. ;;;
  39. ;;; Commentary:
  40. ;;;
  41. ;;; This module provides support to gather code coverage data by instrumenting
  42. ;;; the VM.
  43. ;;;
  44. ;;; Code:
  45. ;;;
  46. ;;; Gathering coverage data.
  47. ;;;
  48. (define (with-code-coverage thunk)
  49. "Run THUNK, a zero-argument procedure, while instrumenting Guile's VM to
  50. collect code coverage data. Return code coverage data and the values returned
  51. by THUNK."
  52. (define ip-counts
  53. ;; A table mapping instruction pointers to the number of times they were
  54. ;; executed.
  55. (make-hash-table 5000))
  56. (define (collect! frame)
  57. ;; Update IP-COUNTS with info from FRAME.
  58. (let* ((ip (frame-instruction-pointer frame))
  59. (ip-entry (hashv-create-handle! ip-counts ip 0)))
  60. (set-cdr! ip-entry (+ (cdr ip-entry) 1))))
  61. ;; FIXME: It's unclear what the dynamic-wind is for, given that if the
  62. ;; VM is different from the current one, continuations will not be
  63. ;; resumable.
  64. (call-with-values (lambda ()
  65. (let ((level (vm-trace-level)))
  66. (dynamic-wind
  67. (lambda ()
  68. (set-vm-trace-level! (+ level 1))
  69. (vm-add-next-hook! collect!))
  70. (lambda ()
  71. (call-with-vm thunk))
  72. (lambda ()
  73. (set-vm-trace-level! level)
  74. (vm-remove-next-hook! collect!)))))
  75. (lambda args
  76. (apply values (make-coverage-data ip-counts) args))))
  77. ;;;
  78. ;;; Source chunks.
  79. ;;;
  80. (define-record-type <source-chunk>
  81. (make-source-chunk base length sources)
  82. source-chunk?
  83. (base source-chunk-base)
  84. (length source-chunk-length)
  85. (sources source-chunk-sources))
  86. (set-record-type-printer!
  87. <source-chunk>
  88. (lambda (obj port)
  89. (format port "<source-chunk #x~x-#x~x>"
  90. (source-chunk-base obj)
  91. (+ (source-chunk-base obj) (source-chunk-length obj)))))
  92. (define (compute-source-chunk ctx)
  93. "Build a sorted vector of source information for a given debugging
  94. context (ELF image). The return value is a @code{<source-chunk>}, which also
  95. records the address range to which the source information applies."
  96. (make-source-chunk
  97. (debug-context-base ctx)
  98. (debug-context-length ctx)
  99. ;; The source locations are sorted already, but collected in reverse order.
  100. (list->vector (reverse! (fold-source-locations cons '() ctx)))))
  101. (define (all-source-information)
  102. "Build and return a vector of source information corresponding to all
  103. loaded code. The vector will be sorted by ascending address order."
  104. (sort! (list->vector (fold-all-debug-contexts
  105. (lambda (ctx seed)
  106. (cons (compute-source-chunk ctx) seed))
  107. '()))
  108. (lambda (x y)
  109. (< (source-chunk-base x) (source-chunk-base y)))))
  110. ;;;
  111. ;;; Coverage data summary.
  112. ;;;
  113. (define-record-type <coverage-data>
  114. (%make-coverage-data ip-counts
  115. sources
  116. file->procedures
  117. file->line-counts)
  118. coverage-data?
  119. ;; Mapping from instruction pointers to the number of times they were
  120. ;; executed, as a sorted vector of IP-count pairs.
  121. (ip-counts data-ip-counts)
  122. ;; Complete source census at the time the coverage analysis was run, as a
  123. ;; sorted vector of <source-chunk> values.
  124. (sources data-sources)
  125. ;; Mapping from source file names to lists of procedures defined in the file.
  126. ;; FIXME.
  127. (file->procedures data-file->procedures)
  128. ;; Mapping from file names to hash tables, which in turn map from line numbers
  129. ;; to execution counts.
  130. (file->line-counts data-file->line-counts))
  131. (set-record-type-printer!
  132. <coverage-data>
  133. (lambda (obj port)
  134. (format port "<coverage-data ~x>" (object-address obj))))
  135. (define (make-coverage-data ip-counts)
  136. ;; Return a `coverage-data' object based on the coverage data available in
  137. ;; IP-COUNTS. Precompute the other hash tables that make up `coverage-data'
  138. ;; objects.
  139. (let* ((all-sources (all-source-information))
  140. (all-counts (sort! (list->vector (hash-fold acons '() ip-counts))
  141. (lambda (x y)
  142. (< (car x) (car y)))))
  143. (file->procedures (make-hash-table 100))
  144. (file->line-counts (make-hash-table 100))
  145. (data (%make-coverage-data all-counts
  146. all-sources
  147. file->procedures
  148. file->line-counts)))
  149. (define (observe-execution-count! file line count)
  150. ;; Make the execution count of FILE:LINE the maximum of its current value
  151. ;; and COUNT. This is so that LINE's execution count is correct when
  152. ;; several instruction pointers map to LINE.
  153. (when file
  154. (let ((file-entry (hash-create-handle! file->line-counts file #f)))
  155. (if (not (cdr file-entry))
  156. (set-cdr! file-entry (make-hash-table 500)))
  157. (let ((line-entry (hashv-create-handle! (cdr file-entry) line 0)))
  158. (set-cdr! line-entry (max (cdr line-entry) count))))))
  159. ;; First, visit every known source location and mark it as instrumented but
  160. ;; unvisited.
  161. ;;
  162. ;; FIXME: This is not always necessary. It's important to have the ability
  163. ;; to know when a source location is not reached, but sometimes all we need
  164. ;; to know is that a particular site *was* reached. In that case we
  165. ;; wouldn't need to load up all the DWARF sections. As it is, though, we
  166. ;; use the complete source census as part of the later phase.
  167. (let visit-chunk ((chunk-idx 0))
  168. (when (< chunk-idx (vector-length all-sources))
  169. (match (vector-ref all-sources chunk-idx)
  170. (($ <source-chunk> base chunk-length chunk-sources)
  171. (let visit-source ((source-idx 0))
  172. (when (< source-idx (vector-length chunk-sources))
  173. (let ((s (vector-ref chunk-sources source-idx)))
  174. (observe-execution-count! (source-file s) (source-line s) 0)
  175. (visit-source (1+ source-idx)))))))
  176. (visit-chunk (1+ chunk-idx))))
  177. ;; Then, visit the measured execution counts, walking the complete source
  178. ;; census at the same time. This allows us to map observed addresses to
  179. ;; source locations. Record observed execution counts.
  180. (let visit-chunk ((chunk-idx 0) (count-idx 0))
  181. (when (< chunk-idx (vector-length all-sources))
  182. (match (vector-ref all-sources chunk-idx)
  183. (($ <source-chunk> base chunk-length chunk-sources)
  184. (let visit-count ((count-idx count-idx) (source-idx 0) (source #f))
  185. (when (< count-idx (vector-length all-counts))
  186. (match (vector-ref all-counts count-idx)
  187. ((ip . count)
  188. (cond
  189. ((< ip base)
  190. ;; Address before chunk base; no corresponding source.
  191. (visit-count (1+ count-idx) source-idx source))
  192. ((< ip (+ base chunk-length))
  193. ;; Address in chunk; count it.
  194. (let visit-source ((source-idx source-idx) (source source))
  195. (define (finish)
  196. (when source
  197. (observe-execution-count! (source-file source)
  198. (source-line source)
  199. count))
  200. (visit-count (1+ count-idx) source-idx source))
  201. (cond
  202. ((< source-idx (vector-length chunk-sources))
  203. (let ((source* (vector-ref chunk-sources source-idx)))
  204. (if (<= (source-pre-pc source*) ip)
  205. (visit-source (1+ source-idx) source*)
  206. (finish))))
  207. (else
  208. (finish)))))
  209. (else
  210. ;; Address past chunk; fetch the next chunk.
  211. (visit-chunk (1+ chunk-idx) count-idx)))))))))))
  212. data))
  213. (define (procedure-execution-count data proc)
  214. "Return the number of times PROC's code was executed, according to DATA. When
  215. PROC is a closure, the number of times its code was executed is returned, not
  216. the number of times this code associated with this particular closure was
  217. executed."
  218. (define (binary-search v key val)
  219. (let lp ((start 0) (end (vector-length v)))
  220. (and (not (eqv? start end))
  221. (let* ((idx (floor/ (+ start end) 2))
  222. (elt (vector-ref v idx))
  223. (val* (key elt)))
  224. (cond
  225. ((< val val*)
  226. (lp start idx))
  227. ((< val* val)
  228. (lp (1+ idx) end))
  229. (else elt))))))
  230. (and (program? proc)
  231. (match (binary-search (data-ip-counts data) car (program-code proc))
  232. (#f 0)
  233. ((ip . code) code))))
  234. (define (instrumented/executed-lines data file)
  235. "Return the number of instrumented and the number of executed source lines in
  236. FILE according to DATA."
  237. (define instr+exec
  238. (and=> (hash-ref (data-file->line-counts data) file)
  239. (lambda (line-counts)
  240. (hash-fold (lambda (line count instr+exec)
  241. (let ((instr (car instr+exec))
  242. (exec (cdr instr+exec)))
  243. (cons (+ 1 instr)
  244. (if (> count 0)
  245. (+ 1 exec)
  246. exec))))
  247. '(0 . 0)
  248. line-counts))))
  249. (values (car instr+exec) (cdr instr+exec)))
  250. (define (line-execution-counts data file)
  251. "Return a list of line number/execution count pairs for FILE, or #f if FILE
  252. is not among the files covered by DATA."
  253. (and=> (hash-ref (data-file->line-counts data) file)
  254. (lambda (line-counts)
  255. (hash-fold alist-cons '() line-counts))))
  256. (define (instrumented-source-files data)
  257. "Return the list of `instrumented' source files, i.e., source files whose code
  258. was loaded at the time DATA was collected."
  259. (hash-fold (lambda (file counts files)
  260. (cons file files))
  261. '()
  262. (data-file->line-counts data)))
  263. ;;;
  264. ;;; LCOV output.
  265. ;;;
  266. (define* (coverage-data->lcov data port)
  267. "Traverse code coverage information DATA, as obtained with
  268. `with-code-coverage', and write coverage information in the LCOV format to PORT.
  269. The report will include all the modules loaded at the time coverage data was
  270. gathered, even if their code was not executed."
  271. ;; FIXME: Re-enable this code, but using for-each-elf-symbol on each source
  272. ;; chunk. Use that to build a map of file -> proc-addr + line + name. Then
  273. ;; use something like procedure-execution-count to get the execution count.
  274. #;
  275. (define (dump-function proc)
  276. ;; Dump source location and basic coverage data for PROC.
  277. (and (or (program? proc))
  278. (let ((sources (program-sources* data proc)))
  279. (and (pair? sources)
  280. (let* ((line (source:line-for-user (car sources)))
  281. (name (or (procedure-name proc)
  282. (format #f "anonymous-l~a" line))))
  283. (format port "FN:~A,~A~%" line name)
  284. (and=> (procedure-execution-count data proc)
  285. (lambda (count)
  286. (format port "FNDA:~A,~A~%" count name))))))))
  287. ;; Output per-file coverage data.
  288. (format port "TN:~%")
  289. (for-each (lambda (file)
  290. (let ((path (search-path %load-path file)))
  291. (if (string? path)
  292. (begin
  293. (format port "SF:~A~%" path)
  294. #;
  295. (for-each dump-function procs)
  296. (for-each (lambda (line+count)
  297. (let ((line (car line+count))
  298. (count (cdr line+count)))
  299. (format port "DA:~A,~A~%"
  300. (+ 1 line) count)))
  301. (line-execution-counts data file))
  302. (let-values (((instr exec)
  303. (instrumented/executed-lines data file)))
  304. (format port "LH: ~A~%" exec)
  305. (format port "LF: ~A~%" instr))
  306. (format port "end_of_record~%"))
  307. (begin
  308. (format (current-error-port)
  309. "skipping unknown source file: ~a~%"
  310. file)))))
  311. (instrumented-source-files data)))