trace.scm 4.7 KB

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