trace.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;;;; (ice-9 debugging trace) -- breakpoint trace behaviour
  2. ;;; Copyright (C) 2002 Free Software Foundation, Inc.
  3. ;;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. (define-module (ice-9 debugging trace)
  18. #:use-module (ice-9 debug)
  19. #:use-module (ice-9 debugger)
  20. #:use-module (ice-9 debugging ice-9-debugger-extensions)
  21. #:use-module (ice-9 debugging steps)
  22. #:use-module (ice-9 debugging traps)
  23. #:export (trace-trap
  24. trace-port
  25. set-trace-layout
  26. trace/pid
  27. trace/stack-id
  28. trace/stack-depth
  29. trace/stack-real-depth
  30. trace/stack
  31. trace/source-file-name
  32. trace/source-line
  33. trace/source-column
  34. trace/source
  35. trace/type
  36. trace/real?
  37. trace/info
  38. trace-at-exit
  39. trace-until-exit))
  40. (cond ((string>=? (version) "1.7")
  41. (use-modules (ice-9 debugger utils))))
  42. (define trace-format-string #f)
  43. (define trace-arg-procs #f)
  44. (define (set-trace-layout format-string . arg-procs)
  45. (set! trace-format-string format-string)
  46. (set! trace-arg-procs arg-procs))
  47. (define (trace/pid trap-context)
  48. (getpid))
  49. (define (trace/stack-id trap-context)
  50. (stack-id (tc:stack trap-context)))
  51. (define (trace/stack-depth trap-context)
  52. (tc:depth trap-context))
  53. (define (trace/stack-real-depth trap-context)
  54. (tc:real-depth trap-context))
  55. (define (trace/stack trap-context)
  56. (format #f "~a:~a+~a"
  57. (stack-id (tc:stack trap-context))
  58. (tc:real-depth trap-context)
  59. (- (tc:depth trap-context) (tc:real-depth trap-context))))
  60. (define (trace/source-file-name trap-context)
  61. (cond ((frame->source-position (tc:frame trap-context)) => car)
  62. (else "")))
  63. (define (trace/source-line trap-context)
  64. (cond ((frame->source-position (tc:frame trap-context)) => cadr)
  65. (else 0)))
  66. (define (trace/source-column trap-context)
  67. (cond ((frame->source-position (tc:frame trap-context)) => caddr)
  68. (else 0)))
  69. (define (trace/source trap-context)
  70. (cond ((frame->source-position (tc:frame trap-context))
  71. =>
  72. (lambda (pos)
  73. (format #f "~a:~a:~a" (car pos) (cadr pos) (caddr pos))))
  74. (else "")))
  75. (define (trace/type trap-context)
  76. (case (tc:type trap-context)
  77. ((#:application) "APP")
  78. ((#:evaluation) "EVA")
  79. ((#:return) "RET")
  80. ((#:error) "ERR")
  81. (else "???")))
  82. (define (trace/real? trap-context)
  83. (if (frame-real? (tc:frame trap-context)) " " "t"))
  84. (define (trace/info trap-context)
  85. (with-output-to-string
  86. (lambda ()
  87. (if (memq (tc:type trap-context) '(#:application #:evaluation))
  88. ((if (tc:expression trap-context)
  89. write-frame-short/expression
  90. write-frame-short/application) (tc:frame trap-context))
  91. (begin
  92. (display "=>")
  93. (write (tc:return-value trap-context)))))))
  94. (set-trace-layout "|~3@a: ~a\n" trace/stack-real-depth trace/info)
  95. ;;; trace-trap
  96. ;;;
  97. ;;; Trace the current location, and install a hook to trace the return
  98. ;;; value when we exit the current frame.
  99. (define (trace-trap trap-context)
  100. (apply format
  101. (trace-port)
  102. trace-format-string
  103. (map (lambda (arg-proc)
  104. (arg-proc trap-context))
  105. trace-arg-procs)))
  106. (set! (behaviour-ordering trace-trap) 50)
  107. ;;; trace-port
  108. ;;;
  109. ;;; The port to which trace information is printed.
  110. (define trace-port
  111. (let ((port (current-output-port)))
  112. (make-procedure-with-setter
  113. (lambda () port)
  114. (lambda (new) (set! port new)))))
  115. ;;; trace-at-exit
  116. ;;;
  117. ;;; Trace return value on exit from the current frame.
  118. (define (trace-at-exit trap-context)
  119. (at-exit (tc:depth trap-context) trace-trap))
  120. ;;; trace-until-exit
  121. ;;;
  122. ;;; Trace absolutely everything until exit from the current frame.
  123. (define (trace-until-exit trap-context)
  124. (let ((step-trap (make <step-trap> #:behaviour trace-trap)))
  125. (install-trap step-trap)
  126. (at-exit (tc:depth trap-context)
  127. (lambda (trap-context)
  128. (uninstall-trap step-trap)))))
  129. ;;; (ice-9 debugging trace) ends here.