ice-9-debugger-extensions.scm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. (define-module (ice-9 debugging ice-9-debugger-extensions)
  2. #:use-module (ice-9 debugger))
  3. ;;; Upgrade the debugger state object so that it can carry a flag
  4. ;;; indicating whether the debugging session is continuable.
  5. (cond ((string>=? (version) "1.7")
  6. (use-modules (ice-9 debugger state))
  7. (define-module (ice-9 debugger state)))
  8. (else
  9. (define-module (ice-9 debugger))))
  10. (set! state-rtd (make-record-type "debugger-state" '(stack index flags)))
  11. (set! state? (record-predicate state-rtd))
  12. (set! make-state
  13. (let ((make-state-internal (record-constructor state-rtd
  14. '(stack index flags))))
  15. (lambda (stack index . flags)
  16. (make-state-internal stack index flags))))
  17. (set! state-stack (record-accessor state-rtd 'stack))
  18. (set! state-index (record-accessor state-rtd 'index))
  19. (define state-flags (record-accessor state-rtd 'flags))
  20. ;;; Add commands that (ice-9 debugger) doesn't currently have, for
  21. ;;; continuing or single stepping program execution.
  22. (cond ((string>=? (version) "1.7")
  23. (use-modules (ice-9 debugger command-loop))
  24. (define-module (ice-9 debugger command-loop)
  25. #:use-module (ice-9 debugger)
  26. #:use-module (ice-9 debugger state)
  27. #:use-module (ice-9 debugging traps))
  28. (define new-define-command define-command)
  29. (set! define-command
  30. (lambda (name argument-template documentation procedure)
  31. (new-define-command name argument-template procedure))))
  32. (else
  33. (define-module (ice-9 debugger))))
  34. (use-modules (ice-9 debugging steps)
  35. (ice-9 debugging trace))
  36. (define (assert-continuable state)
  37. ;; Check that debugger is in a state where `continuing' makes sense.
  38. ;; If not, signal an error.
  39. (or (memq #:continuable (state-flags state))
  40. (user-error "This debug session is not continuable.")))
  41. (define (debugger:continue state)
  42. "Tell the program being debugged to continue running. (In fact this is
  43. the same as the @code{quit} command, because it exits the debugger
  44. command loop and so allows whatever code it was that invoked the
  45. debugger to continue.)"
  46. (assert-continuable state)
  47. (throw 'exit-debugger))
  48. (define (debugger:finish state)
  49. "Continue until evaluation of the current frame is complete, and
  50. print the result obtained."
  51. (assert-continuable state)
  52. (at-exit (- (stack-length (state-stack state))
  53. (state-index state))
  54. (list trace-trap debug-trap))
  55. (debugger:continue state))
  56. (define (debugger:step state n)
  57. "Tell the debugged program to do @var{n} more steps from its current
  58. position. One @dfn{step} means executing until the next frame entry
  59. or exit of any kind. @var{n} defaults to 1."
  60. (assert-continuable state)
  61. (at-step debug-trap (or n 1))
  62. (debugger:continue state))
  63. (define (debugger:next state n)
  64. "Tell the debugged program to do @var{n} more steps from its current
  65. position, but only counting frame entries and exits where the
  66. corresponding source code comes from the same file as the current
  67. stack frame. (See @ref{Step Traps} for the details of how this
  68. works.) If the current stack frame has no source code, the effect of
  69. this command is the same as of @code{step}. @var{n} defaults to 1."
  70. (assert-continuable state)
  71. (at-step debug-trap
  72. (or n 1)
  73. (frame-file-name (stack-ref (state-stack state)
  74. (state-index state)))
  75. (if (memq #:return (state-flags state))
  76. #f
  77. (- (stack-length (state-stack state)) (state-index state))))
  78. (debugger:continue state))
  79. (define-command "continue" '()
  80. "Continue program execution."
  81. debugger:continue)
  82. (define-command "finish" '()
  83. "Continue until evaluation of the current frame is complete, and
  84. print the result obtained."
  85. debugger:finish)
  86. (define-command "step" '('optional exact-integer)
  87. "Continue until entry to @var{n}th next frame."
  88. debugger:step)
  89. (define-command "next" '('optional exact-integer)
  90. "Continue until entry to @var{n}th next frame in same file."
  91. debugger:next)
  92. ;;; Export a couple of procedures for use by (ice-9 debugging trace).
  93. (cond ((string>=? (version) "1.7"))
  94. (else
  95. (define-module (ice-9 debugger))
  96. (export write-frame-short/expression
  97. write-frame-short/application)))
  98. ;;; Provide a `debug-trap' entry point in (ice-9 debugger). This is
  99. ;;; designed so that it can be called to explore the stack at a
  100. ;;; breakpoint, and to single step from the breakpoint.
  101. (define-module (ice-9 debugger))
  102. (use-modules (ice-9 debugging traps))
  103. (define *not-yet-introduced* #t)
  104. (cond ((string>=? (version) "1.7"))
  105. (else
  106. (define (debugger-command-loop state)
  107. (read-and-dispatch-commands state (current-input-port)))))
  108. (define-public (debug-trap trap-context)
  109. "Invoke the Guile debugger to explore the stack at the specified @var{trap}."
  110. (start-stack 'debugger
  111. (let* ((stack (tc:stack trap-context))
  112. (flags1 (let ((trap-type (tc:type trap-context)))
  113. (case trap-type
  114. ((#:return #:error)
  115. (list trap-type
  116. (tc:return-value trap-context)))
  117. (else
  118. (list trap-type)))))
  119. (flags (if (tc:continuation trap-context)
  120. (cons #:continuable flags1)
  121. flags1))
  122. (state (apply make-state stack 0 flags)))
  123. (if *not-yet-introduced*
  124. (let ((ssize (stack-length stack)))
  125. (display "This is the Guile debugger -- for help, type `help'.\n")
  126. (set! *not-yet-introduced* #f)
  127. (if (= ssize 1)
  128. (display "There is 1 frame on the stack.\n\n")
  129. (format #t "There are ~A frames on the stack.\n\n" ssize))))
  130. (write-state-short-with-source-location state)
  131. (debugger-command-loop state))))
  132. (define write-state-short-with-source-location
  133. (cond ((string>=? (version) "1.7")
  134. write-state-short)
  135. (else
  136. (lambda (state)
  137. (let* ((frame (stack-ref (state-stack state) (state-index state)))
  138. (source (frame-source frame))
  139. (position (and source (source-position source))))
  140. (format #t "Frame ~A at " (frame-number frame))
  141. (if position
  142. (display-position position)
  143. (display "unknown source location"))
  144. (newline)
  145. (write-char #\tab)
  146. (write-frame-short frame)
  147. (newline))))))