trace.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ;;; Guile VM tracer
  2. ;; Copyright (C) 2001,2009-2010,2012-2014,2018 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 frame depth width prefix max-indent)
  45. (let ((prefix (build-prefix prefix depth "| " "~d< "max-indent))
  46. (values (frame-return-values frame)))
  47. (case (length values)
  48. ((0)
  49. (format (current-error-port) "~ano values\n" prefix))
  50. ((1)
  51. (format (current-error-port) "~a~v:@y\n"
  52. prefix
  53. width
  54. (car values)))
  55. (else
  56. ;; this should work, but there appears to be a bug
  57. ;; "~a~d values:~:{ ~v:@y~}\n"
  58. (format (current-error-port) "~a~d values:~{ ~a~}\n"
  59. prefix (length values)
  60. (map (lambda (val)
  61. (format #f "~v:@y" width val))
  62. values))))))
  63. (define* (trace-calls-to-procedure proc #:key (width 80)
  64. (prefix "trace: ")
  65. (max-indent (- width 40)))
  66. (define (apply-handler frame depth)
  67. (print-application frame depth width prefix max-indent))
  68. (define (return-handler frame depth values)
  69. (print-return frame depth width prefix max-indent))
  70. (trap-calls-to-procedure proc apply-handler return-handler))
  71. (define* (trace-calls-in-procedure proc #:key (width 80)
  72. (prefix "trace: ")
  73. (max-indent (- width 40)))
  74. (define (apply-handler frame depth)
  75. (print-application frame depth width prefix max-indent))
  76. (define (return-handler frame depth)
  77. (print-return frame depth width prefix max-indent))
  78. (trap-calls-in-dynamic-extent proc apply-handler return-handler))
  79. (define* (trace-instructions-in-procedure proc #:key (width 80)
  80. (max-indent (- width 40)))
  81. (define (trace-next frame)
  82. ;; FIXME: We could disassemble this instruction here.
  83. (let ((ip (frame-instruction-pointer frame)))
  84. (format #t "0x~x\n" ip)))
  85. (trap-instructions-in-dynamic-extent proc trace-next))
  86. ;; Note that because this procedure manipulates the VM trace level
  87. ;; directly, it doesn't compose well with traps at the REPL.
  88. ;;
  89. (define* (call-with-trace thunk #:key (calls? #t) (instructions? #f)
  90. (width 80) (max-indent (- width 40)))
  91. (let ((call-trap #f)
  92. (inst-trap #f))
  93. (dynamic-wind
  94. (lambda ()
  95. (if calls?
  96. (set! call-trap
  97. (trace-calls-in-procedure thunk #:width width
  98. #:max-indent max-indent)))
  99. (if instructions?
  100. (set! inst-trap
  101. (trace-instructions-in-procedure thunk #:width width
  102. #:max-indent max-indent)))
  103. (set-vm-trace-level! (1+ (vm-trace-level))))
  104. thunk
  105. (lambda ()
  106. (set-vm-trace-level! (1- (vm-trace-level)))
  107. (if call-trap (call-trap))
  108. (if inst-trap (inst-trap))
  109. (set! call-trap #f)
  110. (set! inst-trap #f)))))