debugger.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. ;;;; Guile Debugger
  2. ;;; Copyright (C) 1999 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This program is free software; you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU General Public License as
  6. ;;; published by the Free Software Foundation; either version 2, or
  7. ;;; (at your option) any later version.
  8. ;;;
  9. ;;; This program 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. ;;; General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with this software; see the file COPYING. If not, write to
  16. ;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. ;;; Boston, MA 02111-1307 USA
  18. (define-module (ice-9 debugger)
  19. :use-module (ice-9 debug)
  20. :use-module (ice-9 format)
  21. :no-backtrace
  22. )
  23. (if (memq 'readline *features*)
  24. (define-module (ice-9 debugger)
  25. :use-module (ice-9 readline)))
  26. (define debugger-prompt "debug> ")
  27. (define-public (debug)
  28. (let ((stack (fluid-ref the-last-stack)))
  29. (if stack
  30. (let ((state (make-state stack 0)))
  31. (display "This is the Guile debugger; type \"help\" for help.")
  32. (newline)
  33. (display "There are ")
  34. (write (stack-length stack))
  35. (display " frames on the stack.")
  36. (newline)
  37. (newline)
  38. (write-state-short state)
  39. (read-and-dispatch-commands state (current-input-port)))
  40. (display "Nothing to debug.\n"))))
  41. (define (debugger-handler key . args)
  42. (case key
  43. ((exit-debugger) #f)
  44. ((signal)
  45. ;; Restore stack
  46. (fluid-set! the-last-stack (fluid-ref before-signal-stack))
  47. (apply display-error #f (current-error-port) args))
  48. (else
  49. (display "Internal debugger error:\n")
  50. (save-stack debugger-handler)
  51. (apply throw key args)))
  52. (throw 'exit-debugger)) ;Pop the stack
  53. (define (read-and-dispatch-commands state port)
  54. (catch 'exit-debugger
  55. (lambda ()
  56. (lazy-catch #t
  57. (lambda ()
  58. (with-fluids ((last-command #f))
  59. (let loop ((state state))
  60. (loop (read-and-dispatch-command state port)))))
  61. debugger-handler))
  62. (lambda args
  63. *unspecified*)))
  64. (define (read-and-dispatch-command state port)
  65. (if (using-readline?)
  66. (set-readline-prompt! debugger-prompt)
  67. (display debugger-prompt))
  68. (force-output) ;This should not be necessary...
  69. (let ((token (read-token port)))
  70. (cond ((eof-object? token)
  71. (throw 'exit-debugger))
  72. ((not token)
  73. (discard-rest-of-line port)
  74. (catch-user-errors port (lambda () (run-last-command state))))
  75. (else
  76. (or (catch-user-errors port
  77. (lambda ()
  78. (dispatch-command token command-table state port)))
  79. state)))))
  80. (define (run-last-command state)
  81. (let ((procedure (fluid-ref last-command)))
  82. (if procedure
  83. (procedure state))))
  84. (define (catch-user-errors port thunk)
  85. (catch 'debugger-user-error
  86. thunk
  87. (lambda (key . objects)
  88. (apply user-warning objects)
  89. (discard-rest-of-line port)
  90. #f)))
  91. (define last-command (make-fluid))
  92. (define (user-warning . objects)
  93. (for-each (lambda (object)
  94. (display object))
  95. objects)
  96. (newline))
  97. (define (user-error . objects)
  98. (apply throw 'debugger-user-error objects))
  99. ;;;; Command dispatch
  100. (define (dispatch-command string table state port)
  101. (let ((value (command-table-value table string)))
  102. (if value
  103. (dispatch-command/value value state port)
  104. (user-error "Unknown command: " string))))
  105. (define (dispatch-command/value value state port)
  106. (cond ((command? value)
  107. (dispatch-command/command value state port))
  108. ((command-table? value)
  109. (dispatch-command/table value state port))
  110. ((list? value)
  111. (dispatch-command/name value state port))
  112. (else
  113. (error "Unrecognized command-table value: " value))))
  114. (define (dispatch-command/command command state port)
  115. (let ((procedure (command-procedure command))
  116. (arguments ((command-parser command) port)))
  117. (let ((procedure (lambda (state) (apply procedure state arguments))))
  118. (warn-about-extra-args port)
  119. (fluid-set! last-command procedure)
  120. (procedure state))))
  121. (define (warn-about-extra-args port)
  122. ;; **** modify this to show the arguments.
  123. (let ((char (skip-whitespace port)))
  124. (cond ((eof-object? char) #f)
  125. ((char=? #\newline char) (read-char port))
  126. (else
  127. (user-warning "Extra arguments at end of line: "
  128. (read-rest-of-line port))))))
  129. (define (dispatch-command/table table state port)
  130. (let ((token (read-token port)))
  131. (if (or (eof-object? token)
  132. (not token))
  133. (user-error "Command name too short.")
  134. (dispatch-command token table state port))))
  135. (define (dispatch-command/name name state port)
  136. (let ((value (lookup-command name)))
  137. (cond ((not value)
  138. (apply user-error "Unknown command name: " name))
  139. ((command-table? value)
  140. (apply user-error "Partial command name: " name))
  141. (else
  142. (dispatch-command/value value state port)))))
  143. ;;;; Command definition
  144. (define (define-command name argument-template documentation procedure)
  145. (let ((name (canonicalize-command-name name)))
  146. (add-command name
  147. (make-command name
  148. (argument-template->parser argument-template)
  149. documentation
  150. procedure)
  151. command-table)
  152. name))
  153. (define (define-command-alias name1 name2)
  154. (let ((name1 (canonicalize-command-name name1)))
  155. (add-command name1 (canonicalize-command-name name2) command-table)
  156. name1))
  157. (define (argument-template->parser template)
  158. ;; Deliberately handles only cases that occur in "commands.scm".
  159. (cond ((eq? 'tokens template)
  160. (lambda (port)
  161. (let loop ((tokens '()))
  162. (let ((token (read-token port)))
  163. (if (or (eof-object? token)
  164. (not token))
  165. (list (reverse! tokens))
  166. (loop (cons token tokens)))))))
  167. ((null? template)
  168. (lambda (port)
  169. '()))
  170. ((and (pair? template)
  171. (null? (cdr template))
  172. (eq? 'object (car template)))
  173. (lambda (port)
  174. (list (read port))))
  175. ((and (pair? template)
  176. (equal? ''optional (car template))
  177. (pair? (cdr template))
  178. (null? (cddr template)))
  179. (case (cadr template)
  180. ((token)
  181. (lambda (port)
  182. (let ((token (read-token port)))
  183. (if (or (eof-object? token)
  184. (not token))
  185. (list #f)
  186. (list token)))))
  187. ((exact-integer)
  188. (lambda (port)
  189. (list (parse-optional-exact-integer port))))
  190. ((exact-nonnegative-integer)
  191. (lambda (port)
  192. (list (parse-optional-exact-nonnegative-integer port))))
  193. ((object)
  194. (lambda (port)
  195. (list (parse-optional-object port))))
  196. (else
  197. (error "Malformed argument template: " template))))
  198. (else
  199. (error "Malformed argument template: " template))))
  200. (define (parse-optional-exact-integer port)
  201. (let ((object (parse-optional-object port)))
  202. (if (or (not object)
  203. (and (integer? object)
  204. (exact? object)))
  205. object
  206. (user-error "Argument not an exact integer: " object))))
  207. (define (parse-optional-exact-nonnegative-integer port)
  208. (let ((object (parse-optional-object port)))
  209. (if (or (not object)
  210. (and (integer? object)
  211. (exact? object)
  212. (not (negative? object))))
  213. object
  214. (user-error "Argument not an exact non-negative integer: " object))))
  215. (define (parse-optional-object port)
  216. (let ((terminator (skip-whitespace port)))
  217. (if (or (eof-object? terminator)
  218. (eq? #\newline terminator))
  219. #f
  220. (let ((object (read port)))
  221. (if (eof-object? object)
  222. #f
  223. object)))))
  224. ;;;; Command tables
  225. (define (lookup-command name)
  226. (let loop ((table command-table) (strings name))
  227. (let ((value (command-table-value table (car strings))))
  228. (cond ((or (not value) (null? (cdr strings))) value)
  229. ((command-table? value) (loop value (cdr strings)))
  230. (else #f)))))
  231. (define (command-table-value table string)
  232. (let ((entry (command-table-entry table string)))
  233. (and entry
  234. (caddr entry))))
  235. (define (command-table-entry table string)
  236. (let loop ((entries (command-table-entries table)))
  237. (and (not (null? entries))
  238. (let ((entry (car entries)))
  239. (if (and (<= (cadr entry)
  240. (string-length string)
  241. (string-length (car entry)))
  242. (= (string-length string)
  243. (match-strings (car entry) string)))
  244. entry
  245. (loop (cdr entries)))))))
  246. (define (match-strings s1 s2)
  247. (let ((n (min (string-length s1) (string-length s2))))
  248. (let loop ((i 0))
  249. (cond ((= i n) i)
  250. ((char=? (string-ref s1 i) (string-ref s2 i)) (loop (+ i 1)))
  251. (else i)))))
  252. (define (write-command-name name)
  253. (display (car name))
  254. (for-each (lambda (string)
  255. (write-char #\space)
  256. (display string))
  257. (cdr name)))
  258. (define (add-command name value table)
  259. (let loop ((strings name) (table table))
  260. (let ((entry
  261. (or (let loop ((entries (command-table-entries table)))
  262. (and (not (null? entries))
  263. (if (string=? (car strings) (caar entries))
  264. (car entries)
  265. (loop (cdr entries)))))
  266. (let ((entry (list (car strings) #f #f)))
  267. (let ((entries
  268. (let ((entries (command-table-entries table)))
  269. (if (or (null? entries)
  270. (string<? (car strings) (caar entries)))
  271. (cons entry entries)
  272. (begin
  273. (let loop ((prev entries) (this (cdr entries)))
  274. (if (or (null? this)
  275. (string<? (car strings) (caar this)))
  276. (set-cdr! prev (cons entry this))
  277. (loop this (cdr this))))
  278. entries)))))
  279. (compute-string-abbreviations! entries)
  280. (set-command-table-entries! table entries))
  281. entry))))
  282. (if (null? (cdr strings))
  283. (set-car! (cddr entry) value)
  284. (loop (cdr strings)
  285. (if (command-table? (caddr entry))
  286. (caddr entry)
  287. (let ((table (make-command-table '())))
  288. (set-car! (cddr entry) table)
  289. table)))))))
  290. (define (canonicalize-command-name name)
  291. (cond ((and (string? name)
  292. (not (string-null? name)))
  293. (list name))
  294. ((let loop ((name name))
  295. (and (pair? name)
  296. (string? (car name))
  297. (not (string-null? (car name)))
  298. (or (null? (cdr name))
  299. (loop (cdr name)))))
  300. name)
  301. (else
  302. (error "Illegal command name: " name))))
  303. (define (compute-string-abbreviations! entries)
  304. (let loop ((entries entries) (index 0))
  305. (let ((groups '()))
  306. (for-each
  307. (lambda (entry)
  308. (let* ((char (string-ref (car entry) index))
  309. (group (assv char groups)))
  310. (if group
  311. (set-cdr! group (cons entry (cdr group)))
  312. (set! groups
  313. (cons (list char entry)
  314. groups)))))
  315. entries)
  316. (for-each
  317. (lambda (group)
  318. (let ((index (+ index 1)))
  319. (if (null? (cddr group))
  320. (set-car! (cdadr group) index)
  321. (loop (let ((entry
  322. (let loop ((entries (cdr group)))
  323. (and (not (null? entries))
  324. (if (= index (string-length (caar entries)))
  325. (car entries)
  326. (loop (cdr entries)))))))
  327. (if entry
  328. (begin
  329. (set-car! (cdr entry) index)
  330. (delq entry (cdr group)))
  331. (cdr group)))
  332. index))))
  333. groups))))
  334. ;;;; Data structures
  335. (define command-table-rtd (make-record-type "command-table" '(entries)))
  336. (define make-command-table (record-constructor command-table-rtd '(entries)))
  337. (define command-table? (record-predicate command-table-rtd))
  338. (define command-table-entries (record-accessor command-table-rtd 'entries))
  339. (define set-command-table-entries!
  340. (record-modifier command-table-rtd 'entries))
  341. (define command-rtd
  342. (make-record-type "command"
  343. '(name parser documentation procedure)))
  344. (define make-command
  345. (record-constructor command-rtd
  346. '(name parser documentation procedure)))
  347. (define command? (record-predicate command-rtd))
  348. (define command-name (record-accessor command-rtd 'name))
  349. (define command-parser (record-accessor command-rtd 'parser))
  350. (define command-documentation (record-accessor command-rtd 'documentation))
  351. (define command-procedure (record-accessor command-rtd 'procedure))
  352. (define state-rtd (make-record-type "debugger-state" '(stack index)))
  353. (define state? (record-predicate state-rtd))
  354. (define make-state (record-constructor state-rtd '(stack index)))
  355. (define state-stack (record-accessor state-rtd 'stack))
  356. (define state-index (record-accessor state-rtd 'index))
  357. (define (new-state-index state index)
  358. (make-state (state-stack state) index))
  359. ;;;; Character parsing
  360. (define (read-token port)
  361. (letrec
  362. ((loop
  363. (lambda (chars)
  364. (let ((char (peek-char port)))
  365. (cond ((eof-object? char)
  366. (do-eof char chars))
  367. ((char=? #\newline char)
  368. (do-eot chars))
  369. ((char-whitespace? char)
  370. (do-eot chars))
  371. ((char=? #\# char)
  372. (read-char port)
  373. (let ((terminator (skip-comment port)))
  374. (if (eof-object? char)
  375. (do-eof char chars)
  376. (do-eot chars))))
  377. (else
  378. (read-char port)
  379. (loop (cons char chars)))))))
  380. (do-eof
  381. (lambda (eof chars)
  382. (if (null? chars)
  383. eof
  384. (do-eot chars))))
  385. (do-eot
  386. (lambda (chars)
  387. (if (null? chars)
  388. #f
  389. (list->string (reverse! chars))))))
  390. (skip-whitespace port)
  391. (loop '())))
  392. (define (skip-whitespace port)
  393. (let ((char (peek-char port)))
  394. (cond ((or (eof-object? char)
  395. (char=? #\newline char))
  396. char)
  397. ((char-whitespace? char)
  398. (read-char port)
  399. (skip-whitespace port))
  400. ((char=? #\# char)
  401. (read-char port)
  402. (skip-comment port))
  403. (else char))))
  404. (define (skip-comment port)
  405. (let ((char (peek-char port)))
  406. (if (or (eof-object? char)
  407. (char=? #\newline char))
  408. char
  409. (begin
  410. (read-char port)
  411. (skip-comment port)))))
  412. (define (read-rest-of-line port)
  413. (let loop ((chars '()))
  414. (let ((char (read-char port)))
  415. (if (or (eof-object? char)
  416. (char=? #\newline char))
  417. (list->string (reverse! chars))
  418. (loop (cons char chars))))))
  419. (define (discard-rest-of-line port)
  420. (let loop ()
  421. (if (not (let ((char (read-char port)))
  422. (or (eof-object? char)
  423. (char=? #\newline char))))
  424. (loop))))
  425. ;;;; Commands
  426. (define command-table (make-command-table '()))
  427. (define-command "help" 'tokens
  428. "Type \"help\" followed by a command name for full documentation."
  429. (lambda (state tokens)
  430. (let loop ((name (if (null? tokens) '("help") tokens)))
  431. (let ((value (lookup-command name)))
  432. (cond ((not value)
  433. (write-command-name name)
  434. (display " is not a known command name.")
  435. (newline))
  436. ((command? value)
  437. (display (command-documentation value))
  438. (newline)
  439. (if (equal? '("help") (command-name value))
  440. (begin
  441. (display "Available commands are:")
  442. (newline)
  443. (for-each (lambda (entry)
  444. (if (not (list? (caddr entry)))
  445. (begin
  446. (display " ")
  447. (display (car entry))
  448. (newline))))
  449. (command-table-entries command-table)))))
  450. ((command-table? value)
  451. (display "The \"")
  452. (write-command-name name)
  453. (display "\" command requires a subcommand.")
  454. (newline)
  455. (display "Available subcommands are:")
  456. (newline)
  457. (for-each (lambda (entry)
  458. (if (not (list? (caddr entry)))
  459. (begin
  460. (display " ")
  461. (write-command-name name)
  462. (write-char #\space)
  463. (display (car entry))
  464. (newline))))
  465. (command-table-entries value)))
  466. ((list? value)
  467. (loop value))
  468. (else
  469. (error "Unknown value from lookup-command:" value)))))
  470. state))
  471. (define-command "frame" '('optional exact-nonnegative-integer)
  472. "Select and print a stack frame.
  473. With no argument, print the selected stack frame. (See also \"info frame\").
  474. An argument specifies the frame to select; it must be a stack-frame number."
  475. (lambda (state n)
  476. (let ((state (if n (select-frame-absolute state n) state)))
  477. (write-state-short state)
  478. state)))
  479. (define-command "position" '()
  480. "Display the position of the current expression."
  481. (lambda (state)
  482. (let* ((frame (stack-ref (state-stack state) (state-index state)))
  483. (source (frame-source frame)))
  484. (if (not source)
  485. (display "No source available for this frame.")
  486. (let ((position (source-position source)))
  487. (if (not position)
  488. (display "No position information available for this frame.")
  489. (display-position position)))))
  490. (newline)
  491. state))
  492. (define-command "up" '('optional exact-integer)
  493. "Move N frames up the stack. For positive numbers N, this advances
  494. toward the outermost frame, to higher frame numbers, to frames
  495. that have existed longer. N defaults to one."
  496. (lambda (state n)
  497. (let ((state (select-frame-relative state (or n 1))))
  498. (write-state-short state)
  499. state)))
  500. (define-command "down" '('optional exact-integer)
  501. "Move N frames down the stack. For positive numbers N, this
  502. advances toward the innermost frame, to lower frame numbers, to
  503. frames that were created more recently. N defaults to one."
  504. (lambda (state n)
  505. (let ((state (select-frame-relative state (- (or n 1)))))
  506. (write-state-short state)
  507. state)))
  508. (define (eval-handler key . args)
  509. (let ((stack (make-stack #t eval-handler)))
  510. (if (= (length args) 4)
  511. (apply display-error stack (current-error-port) args)
  512. ;; We want display-error to be the "final common pathway"
  513. (catch #t
  514. (lambda ()
  515. (apply bad-throw key args))
  516. (lambda (key . args)
  517. (apply display-error stack (current-error-port) args)))))
  518. (throw 'continue))
  519. (define-command "evaluate" '(object)
  520. "Evaluate an expression.
  521. The expression must appear on the same line as the command,
  522. however it may be continued over multiple lines."
  523. (lambda (state expression)
  524. (let ((source (frame-source (stack-ref (state-stack state)
  525. (state-index state)))))
  526. (if (not source)
  527. (display "No environment for this frame.\n")
  528. (catch 'continue
  529. (lambda ()
  530. (lazy-catch #t
  531. (lambda ()
  532. (let* ((env (memoized-environment source))
  533. (value (local-eval expression env)))
  534. (display ";value: ")
  535. (write value)
  536. (newline)))
  537. eval-handler))
  538. (lambda args args)))
  539. state)))
  540. (define-command "backtrace" '('optional exact-integer)
  541. "Print backtrace of all stack frames, or innermost COUNT frames.
  542. With a negative argument, print outermost -COUNT frames.
  543. If the number of frames aren't explicitly given, the debug option
  544. `depth' determines the maximum number of frames printed."
  545. (lambda (state n-frames)
  546. (let ((stack (state-stack state)))
  547. ;; Kludge around lack of call-with-values.
  548. (let ((values
  549. (lambda (start end)
  550. ;;(do ((index start (+ index 1)))
  551. ;; ((= index end))
  552. ;;(write-state-short* stack index))
  553. ;;
  554. ;; Use builtin backtrace instead:
  555. (display-backtrace stack
  556. (current-output-port)
  557. (if (memq 'backwards (debug-options))
  558. start
  559. (- end 1))
  560. (- end start))
  561. )))
  562. (let ((end (stack-length stack)))
  563. (cond ((not n-frames) ;(>= (abs n-frames) end))
  564. (values 0 (min end (cadr (memq 'depth (debug-options))))))
  565. ((>= n-frames 0)
  566. (values 0 n-frames))
  567. (else
  568. (values (+ end n-frames) end))))))
  569. state))
  570. (define-command "quit" '()
  571. "Exit the debugger."
  572. (lambda (state)
  573. (throw 'exit-debugger)))
  574. (define-command '("info" "frame") '()
  575. "All about selected stack frame."
  576. (lambda (state)
  577. (write-state-long state)
  578. state))
  579. (define-command '("info" "args") '()
  580. "Argument variables of current stack frame."
  581. (lambda (state)
  582. (let ((index (state-index state)))
  583. (let ((frame (stack-ref (state-stack state) index)))
  584. (write-frame-index-long frame)
  585. (write-frame-args-long frame)))
  586. state))
  587. (define-command-alias "f" "frame")
  588. (define-command-alias '("info" "f") '("info" "frame"))
  589. (define-command-alias "bt" "backtrace")
  590. (define-command-alias "where" "backtrace")
  591. (define-command-alias "p" "evaluate")
  592. (define-command-alias '("info" "stack") "backtrace")
  593. ;;;; Command Support
  594. (define (select-frame-absolute state number)
  595. (new-state-index state
  596. (frame-number->index
  597. (let ((end (stack-length (state-stack state))))
  598. (if (>= number end)
  599. (- end 1)
  600. number))
  601. (state-stack state))))
  602. (define (select-frame-relative state delta)
  603. (new-state-index state
  604. (let ((index (+ (state-index state) delta))
  605. (end (stack-length (state-stack state))))
  606. (cond ((< index 0) 0)
  607. ((>= index end) (- end 1))
  608. (else index)))))
  609. (define (write-state-short state)
  610. (display "Frame ")
  611. (write-state-short* (state-stack state) (state-index state)))
  612. (define (write-state-short* stack index)
  613. (write-frame-index-short stack index)
  614. (write-char #\space)
  615. (write-frame-short (stack-ref stack index))
  616. (newline))
  617. (define (write-frame-index-short stack index)
  618. (let ((s (number->string (frame-number (stack-ref stack index)))))
  619. (display s)
  620. (write-char #\:)
  621. (write-chars #\space (- 4 (string-length s)))))
  622. (define (write-frame-short frame)
  623. (if (frame-procedure? frame)
  624. (write-frame-short/application frame)
  625. (write-frame-short/expression frame)))
  626. (define (write-frame-short/application frame)
  627. (write-char #\[)
  628. (write (let ((procedure (frame-procedure frame)))
  629. (or (and (procedure? procedure)
  630. (procedure-name procedure))
  631. procedure)))
  632. (if (frame-evaluating-args? frame)
  633. (display " ...")
  634. (begin
  635. (for-each (lambda (argument)
  636. (write-char #\space)
  637. (write argument))
  638. (frame-arguments frame))
  639. (write-char #\]))))
  640. ;;; Use builtin function instead:
  641. (set! write-frame-short/application
  642. (lambda (frame)
  643. (display-application frame (current-output-port) 12)))
  644. (define (write-frame-short/expression frame)
  645. (write (let* ((source (frame-source frame))
  646. (copy (source-property source 'copy)))
  647. (if (pair? copy)
  648. copy
  649. (unmemoize source)))))
  650. (define (write-state-long state)
  651. (let ((index (state-index state)))
  652. (let ((frame (stack-ref (state-stack state) index)))
  653. (write-frame-index-long frame)
  654. (write-frame-long frame))))
  655. (define (write-frame-index-long frame)
  656. (display "Stack frame: ")
  657. (write (frame-number frame))
  658. (if (frame-real? frame)
  659. (display " (real)"))
  660. (newline))
  661. (define (write-frame-long frame)
  662. (if (frame-procedure? frame)
  663. (write-frame-long/application frame)
  664. (write-frame-long/expression frame)))
  665. (define (write-frame-long/application frame)
  666. (display "This frame is an application.")
  667. (newline)
  668. (if (frame-source frame)
  669. (begin
  670. (display "The corresponding expression is:")
  671. (newline)
  672. (display-source frame)
  673. (newline)))
  674. (display "The procedure being applied is: ")
  675. (write (let ((procedure (frame-procedure frame)))
  676. (or (and (procedure? procedure)
  677. (procedure-name procedure))
  678. procedure)))
  679. (newline)
  680. (display "The procedure's arguments are")
  681. (if (frame-evaluating-args? frame)
  682. (display " being evaluated.")
  683. (begin
  684. (display ": ")
  685. (write (frame-arguments frame))))
  686. (newline))
  687. (define (display-source frame)
  688. (let* ((source (frame-source frame))
  689. (copy (source-property source 'copy)))
  690. (cond ((source-position source)
  691. => (lambda (p) (display-position p) (display ":\n"))))
  692. (display " ")
  693. (write (or copy (unmemoize source)))))
  694. (define (source-position source)
  695. (let ((fname (source-property source 'filename))
  696. (line (source-property source 'line))
  697. (column (source-property source 'column)))
  698. (and fname
  699. (list fname line column))))
  700. (define (display-position pos)
  701. (format #t "~A:~D:~D" (car pos) (+ 1 (cadr pos)) (+ 1 (caddr pos))))
  702. (define (write-frame-long/expression frame)
  703. (display "This frame is an evaluation.")
  704. (newline)
  705. (display "The expression being evaluated is:")
  706. (newline)
  707. (display-source frame)
  708. (newline))
  709. (define (write-frame-args-long frame)
  710. (if (frame-procedure? frame)
  711. (let ((arguments (frame-arguments frame)))
  712. (let ((n (length arguments)))
  713. (display "This frame has ")
  714. (write n)
  715. (display " argument")
  716. (if (not (= n 1))
  717. (display "s"))
  718. (write-char (if (null? arguments) #\. #\:))
  719. (newline))
  720. (for-each (lambda (argument)
  721. (display " ")
  722. (write argument)
  723. (newline))
  724. arguments))
  725. (begin
  726. (display "This frame is an evaluation frame; it has no arguments.")
  727. (newline))))
  728. (define (write-chars char n)
  729. (do ((i 0 (+ i 1)))
  730. ((>= i n))
  731. (write-char char)))