debug.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ;;;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2006 Free Software Foundation
  2. ;;;;
  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 2.1 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. ;;;;
  17. ;;;; The author can be reached at djurfeldt@nada.kth.se
  18. ;;;; Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
  19. ;;;;
  20. (define-module (ice-9 debug)
  21. :export (frame-number->index trace untrace trace-stack untrace-stack))
  22. ;;; {Misc}
  23. ;;;
  24. (define (frame-number->index n . stack)
  25. (let ((stack (if (null? stack)
  26. (fluid-ref the-last-stack)
  27. (car stack))))
  28. (if (memq 'backwards (debug-options))
  29. n
  30. (- (stack-length stack) n 1))))
  31. ;;; {Trace}
  32. ;;;
  33. ;;; This code is just an experimental prototype (e. g., it is not
  34. ;;; thread safe), but since it's at the same time useful, it's
  35. ;;; included anyway.
  36. ;;;
  37. (define traced-procedures '())
  38. (define (trace . args)
  39. (if (null? args)
  40. (nameify traced-procedures)
  41. (begin
  42. (for-each (lambda (proc)
  43. (if (not (procedure? proc))
  44. (error "trace: Wrong type argument:" proc))
  45. (set-procedure-property! proc 'trace #t)
  46. (if (not (memq proc traced-procedures))
  47. (set! traced-procedures
  48. (cons proc traced-procedures))))
  49. args)
  50. (trap-set! apply-frame-handler trace-entry)
  51. (trap-set! exit-frame-handler trace-exit)
  52. ;; We used to reset `trace-level' here to 0, but this is wrong
  53. ;; if `trace' itself is being traced, since `trace-exit' will
  54. ;; then decrement `trace-level' to -1! It shouldn't actually
  55. ;; be necessary to set `trace-level' here at all.
  56. (debug-enable 'trace)
  57. (nameify args))))
  58. (define (untrace . args)
  59. (if (and (null? args)
  60. (not (null? traced-procedures)))
  61. (apply untrace traced-procedures)
  62. (begin
  63. (for-each (lambda (proc)
  64. (set-procedure-property! proc 'trace #f)
  65. (set! traced-procedures (delq! proc traced-procedures)))
  66. args)
  67. (if (null? traced-procedures)
  68. (debug-disable 'trace))
  69. (nameify args))))
  70. (define (nameify ls)
  71. (map (lambda (proc)
  72. (let ((name (procedure-name proc)))
  73. (or name proc)))
  74. ls))
  75. (define trace-level 0)
  76. (add-hook! abort-hook (lambda () (set! trace-level 0)))
  77. (define traced-stack-ids (list 'repl-stack))
  78. (define trace-all-stacks? #f)
  79. (define (trace-stack id)
  80. "Add ID to the set of stack ids for which tracing is active.
  81. If `#t' is in this set, tracing is active regardless of stack context.
  82. To remove ID again, use `untrace-stack'. If you add the same ID twice
  83. using `trace-stack', you will need to remove it twice."
  84. (set! traced-stack-ids (cons id traced-stack-ids))
  85. (set! trace-all-stacks? (memq #t traced-stack-ids)))
  86. (define (untrace-stack id)
  87. "Remove ID from the set of stack ids for which tracing is active."
  88. (set! traced-stack-ids (delq1! id traced-stack-ids))
  89. (set! trace-all-stacks? (memq #t traced-stack-ids)))
  90. (define (trace-entry key cont tail)
  91. (if (or trace-all-stacks?
  92. (memq (stack-id cont) traced-stack-ids))
  93. (let ((cep (current-error-port))
  94. (frame (last-stack-frame cont)))
  95. (if (not tail)
  96. (set! trace-level (+ trace-level 1)))
  97. (let indent ((n trace-level))
  98. (cond ((> n 1) (display "| " cep) (indent (- n 1)))))
  99. (display-application frame cep)
  100. (newline cep)))
  101. ;; It's not necessary to call the continuation since
  102. ;; execution will continue if the handler returns
  103. ;(cont #f)
  104. )
  105. (define (trace-exit key cont retval)
  106. (if (or trace-all-stacks?
  107. (memq (stack-id cont) traced-stack-ids))
  108. (let ((cep (current-error-port)))
  109. (set! trace-level (- trace-level 1))
  110. (let indent ((n trace-level))
  111. (cond ((> n 0) (display "| " cep) (indent (- n 1)))))
  112. (write retval cep)
  113. (newline cep))))
  114. ;;; A fix to get the error handling working together with the module system.
  115. ;;;
  116. ;;; XXX - Still needed?
  117. (module-set! the-root-module 'debug-options debug-options)