trace.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ;;; Guile VM tracer
  2. ;; Copyright (C) 2001, 2009, 2010, 2012, 2013 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 (build-prefix prefix depth infix numeric-format max-indent)
  34. (let lp ((indent "") (n 0))
  35. (cond
  36. ((= n depth)
  37. (string-append prefix indent))
  38. ((< (+ (string-length indent) (string-length infix)) max-indent)
  39. (lp (string-append indent infix) (1+ n)))
  40. (else
  41. (string-append prefix indent (format #f numeric-format depth))))))
  42. (define (print-application frame depth width prefix max-indent)
  43. (let ((prefix (build-prefix prefix depth "| " "~d> " max-indent)))
  44. (format (current-error-port) "~a~v:@y\n"
  45. prefix
  46. width
  47. (frame-call-representation frame))))
  48. (define* (print-return frame depth width prefix max-indent values)
  49. (let* ((len (frame-num-locals frame))
  50. (prefix (build-prefix prefix depth "| " "~d< "max-indent)))
  51. (case (length values)
  52. ((0)
  53. (format (current-error-port) "~ano values\n" prefix))
  54. ((1)
  55. (format (current-error-port) "~a~v:@y\n"
  56. prefix
  57. width
  58. (car values)))
  59. (else
  60. ;; this should work, but there appears to be a bug
  61. ;; "~a~d values:~:{ ~v:@y~}\n"
  62. (format (current-error-port) "~a~d values:~{ ~a~}\n"
  63. prefix (length values)
  64. (map (lambda (val)
  65. (format #f "~v:@y" width val))
  66. values))))))
  67. (define* (trace-calls-to-procedure proc #:key (width 80) (vm (the-vm))
  68. (prefix "trace: ")
  69. (max-indent (- width 40)))
  70. (define (apply-handler frame depth)
  71. (print-application frame depth width prefix max-indent))
  72. (define (return-handler frame depth . values)
  73. (print-return frame depth width prefix max-indent values))
  74. (trap-calls-to-procedure proc apply-handler return-handler
  75. #:vm vm))
  76. (define* (trace-calls-in-procedure proc #:key (width 80) (vm (the-vm))
  77. (prefix "trace: ")
  78. (max-indent (- width 40)))
  79. (define (apply-handler frame depth)
  80. (print-application frame depth width prefix max-indent))
  81. (define (return-handler frame depth . values)
  82. (print-return frame depth width prefix max-indent values))
  83. (trap-calls-in-dynamic-extent proc apply-handler return-handler
  84. #:vm vm))
  85. (define* (trace-instructions-in-procedure proc #:key (width 80) (vm (the-vm))
  86. (max-indent (- width 40)))
  87. (define (trace-next frame)
  88. (let* ((ip (frame-instruction-pointer frame))
  89. (objcode (program-objcode (frame-procedure frame)))
  90. (opcode (bytevector-u8-ref (objcode->bytecode objcode)
  91. (+ ip *objcode-header-len*))))
  92. (format #t "~8d: ~a\n" ip (opcode->instruction opcode))))
  93. (trap-instructions-in-dynamic-extent proc trace-next
  94. #:vm vm))
  95. ;; Note that because this procedure manipulates the VM trace level
  96. ;; directly, it doesn't compose well with traps at the REPL.
  97. ;;
  98. (define* (call-with-trace thunk #:key (calls? #t) (instructions? #f)
  99. (width 80) (vm (the-vm)) (max-indent (- width 40)))
  100. (let ((call-trap #f)
  101. (inst-trap #f))
  102. (dynamic-wind
  103. (lambda ()
  104. (if calls?
  105. (set! call-trap
  106. (trace-calls-in-procedure thunk #:vm vm #:width width
  107. #:max-indent max-indent)))
  108. (if instructions?
  109. (set! inst-trap
  110. (trace-instructions-in-procedure thunk #:vm vm #:width width
  111. #:max-indent max-indent)))
  112. (set-vm-trace-level! vm (1+ (vm-trace-level vm))))
  113. thunk
  114. (lambda ()
  115. (set-vm-trace-level! vm (1- (vm-trace-level vm)))
  116. (if call-trap (call-trap))
  117. (if inst-trap (inst-trap))
  118. (set! call-trap #f)
  119. (set! inst-trap #f)))))