trace.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; Guile VM tracer
  2. ;; Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
  3. ;;; This library is free software; you can redistribute it and/or
  4. ;;; modify it under the terms of the GNU Lesser General Public
  5. ;;; License as published by the Free Software Foundation; either
  6. ;;; version 3 of the License, or (at your option) any later version.
  7. ;;;
  8. ;;; This library is distributed in the hope that it will be useful,
  9. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;; Lesser General Public License for more details.
  12. ;;;
  13. ;;; You should have received a copy of the GNU Lesser General Public
  14. ;;; License along with this library; if not, write to the Free Software
  15. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (system vm trace)
  18. #:use-module (system base syntax)
  19. #:use-module (system vm vm)
  20. #:use-module (system vm frame)
  21. #:use-module (system vm program)
  22. #:use-module (system vm objcode)
  23. #:use-module (system vm traps)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (system vm instruction)
  26. #:use-module (ice-9 format)
  27. #:export (trace-calls-in-procedure
  28. trace-calls-to-procedure
  29. trace-instructions-in-procedure
  30. call-with-trace))
  31. ;; FIXME: this constant needs to go in system vm objcode
  32. (define *objcode-header-len* 8)
  33. (define (print-application frame depth width prefix)
  34. (format (current-error-port) "~a~a~v:@y\n"
  35. prefix
  36. (let lp ((depth depth) (s ""))
  37. (if (zero? depth)
  38. s
  39. (lp (1- depth) (string-append "| " s))))
  40. (max (- width (* 3 depth)) 1)
  41. (frame-call-representation frame)))
  42. (define (print-return frame depth width prefix)
  43. (let* ((len (frame-num-locals frame))
  44. (nvalues (frame-local-ref frame (1- len))))
  45. (cond
  46. ((= nvalues 1)
  47. (format (current-error-port) "~a~a~v:@y\n"
  48. prefix
  49. (let lp ((depth depth) (s ""))
  50. (if (zero? depth)
  51. s
  52. (lp (1- depth) (string-append "| " s))))
  53. width (frame-local-ref frame (- len 2))))
  54. (else
  55. ;; this should work, but there appears to be a bug
  56. ;; "~a~d values:~:{ ~v:@y~}\n"
  57. (format (current-error-port) "~a~a~d values:~{ ~a~}\n"
  58. prefix
  59. (let lp ((depth depth) (s ""))
  60. (if (zero? depth)
  61. s
  62. (lp (1- depth) (string-append "| " s))))
  63. nvalues
  64. (map (lambda (val)
  65. (format #f "~v:@y" width val))
  66. (frame-return-values frame)))))))
  67. (define* (trace-calls-to-procedure proc #:key (width 80) (vm (the-vm))
  68. (prefix "trace: "))
  69. (define (apply-handler frame depth)
  70. (print-application frame depth width prefix))
  71. (define (return-handler frame depth)
  72. (print-return frame depth width prefix))
  73. (trap-calls-to-procedure proc apply-handler return-handler
  74. #:vm vm))
  75. (define* (trace-calls-in-procedure proc #:key (width 80) (vm (the-vm))
  76. (prefix "trace: "))
  77. (define (apply-handler frame depth)
  78. (print-application frame depth width prefix))
  79. (define (return-handler frame depth)
  80. (print-return frame depth width prefix))
  81. (trap-calls-in-dynamic-extent proc apply-handler return-handler
  82. #:vm vm))
  83. (define* (trace-instructions-in-procedure proc #:key (width 80) (vm (the-vm)))
  84. (define (trace-next frame)
  85. (let* ((ip (frame-instruction-pointer frame))
  86. (objcode (program-objcode (frame-procedure frame)))
  87. (opcode (bytevector-u8-ref (objcode->bytecode objcode)
  88. (+ ip *objcode-header-len*))))
  89. (format #t "~8d: ~a\n" ip (opcode->instruction opcode))))
  90. (trap-instructions-in-dynamic-extent proc trace-next
  91. #:vm vm))
  92. ;; Note that because this procedure manipulates the VM trace level
  93. ;; directly, it doesn't compose well with traps at the REPL.
  94. ;;
  95. (define* (call-with-trace thunk #:key (calls? #t) (instructions? #f) (width 80) (vm (the-vm)))
  96. (let ((call-trap #f)
  97. (inst-trap #f))
  98. (dynamic-wind
  99. (lambda ()
  100. (if calls?
  101. (set! call-trap
  102. (trace-calls-in-procedure thunk #:vm vm #:width width)))
  103. (if instructions?
  104. (set! inst-trap
  105. (trace-instructions-in-procedure thunk #:vm vm #:width width)))
  106. (set-vm-trace-level! vm (1+ (vm-trace-level vm))))
  107. thunk
  108. (lambda ()
  109. (set-vm-trace-level! vm (1- (vm-trace-level vm)))
  110. (if call-trap (call-trap))
  111. (if inst-trap (inst-trap))
  112. (set! call-trap #f)
  113. (set! inst-trap #f)))))