trace.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;;; Guile VM tracer
  2. ;; Copyright (C) 2001, 2009, 2010, 2012, 2013, 2014 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 traps)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (ice-9 format)
  25. #:export (trace-calls-in-procedure
  26. trace-calls-to-procedure
  27. trace-instructions-in-procedure
  28. call-with-trace))
  29. (define (build-prefix prefix depth infix numeric-format max-indent)
  30. (let lp ((indent "") (n 0))
  31. (cond
  32. ((= n depth)
  33. (string-append prefix indent))
  34. ((< (+ (string-length indent) (string-length infix)) max-indent)
  35. (lp (string-append indent infix) (1+ n)))
  36. (else
  37. (string-append prefix indent (format #f numeric-format depth))))))
  38. (define (print-application frame depth width prefix max-indent)
  39. (let ((prefix (build-prefix prefix depth "| " "~d> " max-indent)))
  40. (format (current-error-port) "~a~v:@y\n"
  41. prefix
  42. width
  43. (frame-call-representation frame #:top-frame? #t))))
  44. (define (print-return depth width prefix max-indent values)
  45. (let ((prefix (build-prefix prefix depth "| " "~d< "max-indent)))
  46. (case (length values)
  47. ((0)
  48. (format (current-error-port) "~ano values\n" prefix))
  49. ((1)
  50. (format (current-error-port) "~a~v:@y\n"
  51. prefix
  52. width
  53. (car values)))
  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~d values:~{ ~a~}\n"
  58. prefix (length values)
  59. (map (lambda (val)
  60. (format #f "~v:@y" width val))
  61. values))))))
  62. (define* (trace-calls-to-procedure proc #:key (width 80)
  63. (prefix "trace: ")
  64. (max-indent (- width 40)))
  65. (define (apply-handler frame depth)
  66. (print-application frame depth width prefix max-indent))
  67. (define (return-handler frame depth . values)
  68. (print-return depth width prefix max-indent values))
  69. (trap-calls-to-procedure proc apply-handler return-handler))
  70. (define* (trace-calls-in-procedure proc #:key (width 80)
  71. (prefix "trace: ")
  72. (max-indent (- width 40)))
  73. (define (apply-handler frame depth)
  74. (print-application frame depth width prefix max-indent))
  75. (define (return-handler frame depth . values)
  76. (print-return depth width prefix max-indent values))
  77. (trap-calls-in-dynamic-extent proc apply-handler return-handler))
  78. (define* (trace-instructions-in-procedure proc #:key (width 80)
  79. (max-indent (- width 40)))
  80. (define (trace-next frame)
  81. ;; FIXME: We could disassemble this instruction here.
  82. (let ((ip (frame-instruction-pointer frame)))
  83. (format #t "0x~x\n" ip)))
  84. (trap-instructions-in-dynamic-extent proc trace-next))
  85. ;; Note that because this procedure manipulates the VM trace level
  86. ;; directly, it doesn't compose well with traps at the REPL.
  87. ;;
  88. (define* (call-with-trace thunk #:key (calls? #t) (instructions? #f)
  89. (width 80) (max-indent (- width 40)))
  90. (let ((call-trap #f)
  91. (inst-trap #f))
  92. (dynamic-wind
  93. (lambda ()
  94. (if calls?
  95. (set! call-trap
  96. (trace-calls-in-procedure thunk #:width width
  97. #:max-indent max-indent)))
  98. (if instructions?
  99. (set! inst-trap
  100. (trace-instructions-in-procedure thunk #:width width
  101. #:max-indent max-indent)))
  102. (set-vm-trace-level! (1+ (vm-trace-level))))
  103. thunk
  104. (lambda ()
  105. (set-vm-trace-level! (1- (vm-trace-level)))
  106. (if call-trap (call-trap))
  107. (if inst-trap (inst-trap))
  108. (set! call-trap #f)
  109. (set! inst-trap #f)))))