debug.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;;; Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation
  2. ;;;;
  3. ;;;; This program is free software; you can redistribute it and/or modify
  4. ;;;; it under the terms of the GNU General Public License as published by
  5. ;;;; the Free Software Foundation; either version 2, or (at your option)
  6. ;;;; any later version.
  7. ;;;;
  8. ;;;; This program 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
  11. ;;;; GNU General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU General Public License
  14. ;;;; along with this software; see the file COPYING. If not, write to
  15. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. ;;;; Boston, MA 02111-1307 USA
  17. ;;;;
  18. ;;;; The author can be reached at djurfeldt@nada.kth.se
  19. ;;;; Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
  20. ;;;;
  21. (define-module (ice-9 debug))
  22. ;;; {Misc}
  23. ;;;
  24. (define-public (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-public (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. (set! apply-frame-handler trace-entry)
  51. (set! exit-frame-handler trace-exit)
  52. (set! trace-level 0)
  53. (debug-enable 'trace)
  54. (nameify args))))
  55. (define-public (untrace . args)
  56. (if (and (null? args)
  57. (not (null? traced-procedures)))
  58. (apply untrace traced-procedures)
  59. (begin
  60. (for-each (lambda (proc)
  61. (set-procedure-property! proc 'trace #f)
  62. (set! traced-procedures (delq! proc traced-procedures)))
  63. args)
  64. (if (null? traced-procedures)
  65. (debug-disable 'trace))
  66. (nameify args))))
  67. (define (nameify ls)
  68. (map (lambda (proc)
  69. (let ((name (procedure-name proc)))
  70. (or name proc)))
  71. ls))
  72. (define trace-level 0)
  73. (add-hook! abort-hook (lambda () (set! trace-level 0)))
  74. (define (trace-entry key cont tail)
  75. (if (eq? (stack-id cont) 'repl-stack)
  76. (let ((cep (current-error-port))
  77. (frame (last-stack-frame cont)))
  78. (if (not tail)
  79. (set! trace-level (+ trace-level 1)))
  80. (let indent ((n trace-level))
  81. (cond ((> n 1) (display "| " cep) (indent (- n 1)))))
  82. (display-application frame cep)
  83. (newline cep)))
  84. ;; It's not necessary to call the continuation since
  85. ;; execution will continue if the handler returns
  86. ;(cont #f)
  87. )
  88. (define (trace-exit key cont retval)
  89. (if (eq? (stack-id cont) 'repl-stack)
  90. (let ((cep (current-error-port)))
  91. (set! trace-level (- trace-level 1))
  92. (let indent ((n trace-level))
  93. (cond ((> n 0) (display "| " cep) (indent (- n 1)))))
  94. (write retval cep)
  95. (newline cep))))
  96. ;;; A fix to get the error handling working together with the module system.
  97. ;;;
  98. (variable-set! (builtin-variable 'debug-options) debug-options)
  99. (debug-enable 'debug)
  100. (read-enable 'positions)