command-loop.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. ;;;; Guile Debugger command loop
  2. ;;; Copyright (C) 1999, 2001, 2002, 2003, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (define-module (ice-9 debugger command-loop)
  18. #:use-module ((ice-9 debugger commands) :prefix debugger:)
  19. #:export (debugger-command-loop
  20. debugger-command-loop-error
  21. debugger-command-loop-quit)
  22. #:no-backtrace)
  23. ;;; {Interface used by (ice-9 debugger).}
  24. (define (debugger-command-loop state)
  25. (read-and-dispatch-commands state (current-input-port)))
  26. (define (debugger-command-loop-error message)
  27. (user-error message))
  28. (define (debugger-command-loop-quit)
  29. (throw 'exit-debugger))
  30. ;;; {Implementation.}
  31. (define debugger-prompt "debug> ")
  32. (define (debugger-handler key . args)
  33. (case key
  34. ((exit-debugger) #f)
  35. ((signal)
  36. ;; Restore stack
  37. (fluid-set! the-last-stack (fluid-ref before-signal-stack))
  38. (apply display-error #f (current-error-port) args))
  39. (else
  40. (display "Internal debugger error:\n")
  41. (save-stack debugger-handler)
  42. (apply throw key args)))
  43. (throw 'exit-debugger)) ;Pop the stack
  44. (define (read-and-dispatch-commands state port)
  45. (catch 'exit-debugger
  46. (lambda ()
  47. (lazy-catch #t
  48. (lambda ()
  49. (with-fluids ((last-command #f))
  50. (let loop ()
  51. (read-and-dispatch-command state port)
  52. (loop))))
  53. debugger-handler))
  54. (lambda args
  55. *unspecified*)))
  56. (define set-readline-prompt! #f)
  57. (define (read-and-dispatch-command state port)
  58. (if (using-readline?)
  59. (begin
  60. ;; Import set-readline-prompt! if we haven't already.
  61. (or set-readline-prompt!
  62. (set! set-readline-prompt!
  63. (module-ref (resolve-module '(ice-9 readline))
  64. 'set-readline-prompt!)))
  65. (set-readline-prompt! debugger-prompt debugger-prompt))
  66. (display debugger-prompt))
  67. (force-output) ;This should not be necessary...
  68. (let ((token (read-token port)))
  69. (cond ((eof-object? token)
  70. (throw 'exit-debugger))
  71. ((not token)
  72. (discard-rest-of-line port)
  73. (catch-user-errors port (lambda () (run-last-command state))))
  74. (else
  75. (catch-user-errors port
  76. (lambda ()
  77. (dispatch-command token command-table state port)))))))
  78. (define (run-last-command state)
  79. (let ((procedure (fluid-ref last-command)))
  80. (if procedure
  81. (procedure state))))
  82. (define (catch-user-errors port thunk)
  83. (catch 'debugger-user-error
  84. thunk
  85. (lambda (key . objects)
  86. (apply user-warning objects)
  87. (discard-rest-of-line port))))
  88. (define last-command (make-fluid))
  89. (define (user-warning . objects)
  90. (for-each (lambda (object)
  91. (display object))
  92. objects)
  93. (newline))
  94. (define (user-error . objects)
  95. (apply throw 'debugger-user-error objects))
  96. ;;;; Command dispatch
  97. (define (dispatch-command string table state port)
  98. (let ((value (command-table-value table string)))
  99. (if value
  100. (dispatch-command/value value state port)
  101. (user-error "Unknown command: " string))))
  102. (define (dispatch-command/value value state port)
  103. (cond ((command? value)
  104. (dispatch-command/command value state port))
  105. ((command-table? value)
  106. (dispatch-command/table value state port))
  107. ((list? value)
  108. (dispatch-command/name value state port))
  109. (else
  110. (error "Unrecognized command-table value: " value))))
  111. (define (dispatch-command/command command state port)
  112. (let ((procedure (command-procedure command))
  113. (arguments ((command-parser command) port)))
  114. (let ((procedure (lambda (state) (apply procedure state arguments))))
  115. (warn-about-extra-args port)
  116. (fluid-set! last-command procedure)
  117. (procedure state))))
  118. (define (warn-about-extra-args port)
  119. ;; **** modify this to show the arguments.
  120. (let ((char (skip-whitespace port)))
  121. (cond ((eof-object? char) #f)
  122. ((char=? #\newline char) (read-char port))
  123. (else
  124. (user-warning "Extra arguments at end of line: "
  125. (read-rest-of-line port))))))
  126. (define (dispatch-command/table table state port)
  127. (let ((token (read-token port)))
  128. (if (or (eof-object? token)
  129. (not token))
  130. (user-error "Command name too short.")
  131. (dispatch-command token table state port))))
  132. (define (dispatch-command/name name state port)
  133. (let ((value (lookup-command name)))
  134. (cond ((not value)
  135. (apply user-error "Unknown command name: " name))
  136. ((command-table? value)
  137. (apply user-error "Partial command name: " name))
  138. (else
  139. (dispatch-command/value value state port)))))
  140. ;;;; Command definition
  141. (define (define-command name argument-template procedure)
  142. (let ((name (canonicalize-command-name name)))
  143. (add-command name
  144. (make-command name
  145. (argument-template->parser argument-template)
  146. (procedure-documentation procedure)
  147. procedure)
  148. command-table)
  149. name))
  150. (define (define-command-alias name1 name2)
  151. (let ((name1 (canonicalize-command-name name1)))
  152. (add-command name1 (canonicalize-command-name name2) command-table)
  153. name1))
  154. (define (argument-template->parser template)
  155. ;; Deliberately handles only cases that occur in "commands.scm".
  156. (cond ((eq? 'tokens template)
  157. (lambda (port)
  158. (let loop ((tokens '()))
  159. (let ((token (read-token port)))
  160. (if (or (eof-object? token)
  161. (not token))
  162. (list (reverse! tokens))
  163. (loop (cons token tokens)))))))
  164. ((null? template)
  165. (lambda (port)
  166. '()))
  167. ((and (pair? template)
  168. (null? (cdr template))
  169. (eq? 'object (car template)))
  170. (lambda (port)
  171. (list (read port))))
  172. ((and (pair? template)
  173. (equal? ''optional (car template))
  174. (pair? (cdr template))
  175. (null? (cddr template)))
  176. (case (cadr template)
  177. ((token)
  178. (lambda (port)
  179. (let ((token (read-token port)))
  180. (if (or (eof-object? token)
  181. (not token))
  182. (list #f)
  183. (list token)))))
  184. ((exact-integer)
  185. (lambda (port)
  186. (list (parse-optional-exact-integer port))))
  187. ((exact-nonnegative-integer)
  188. (lambda (port)
  189. (list (parse-optional-exact-nonnegative-integer port))))
  190. ((object)
  191. (lambda (port)
  192. (list (parse-optional-object port))))
  193. (else
  194. (error "Malformed argument template: " template))))
  195. (else
  196. (error "Malformed argument template: " template))))
  197. (define (parse-optional-exact-integer port)
  198. (let ((object (parse-optional-object port)))
  199. (if (or (not object)
  200. (and (integer? object)
  201. (exact? object)))
  202. object
  203. (user-error "Argument not an exact integer: " object))))
  204. (define (parse-optional-exact-nonnegative-integer port)
  205. (let ((object (parse-optional-object port)))
  206. (if (or (not object)
  207. (and (integer? object)
  208. (exact? object)
  209. (not (negative? object))))
  210. object
  211. (user-error "Argument not an exact non-negative integer: " object))))
  212. (define (parse-optional-object port)
  213. (let ((terminator (skip-whitespace port)))
  214. (if (or (eof-object? terminator)
  215. (eq? #\newline terminator))
  216. #f
  217. (let ((object (read port)))
  218. (if (eof-object? object)
  219. #f
  220. object)))))
  221. ;;;; Command tables
  222. (define (lookup-command name)
  223. (let loop ((table command-table) (strings name))
  224. (let ((value (command-table-value table (car strings))))
  225. (cond ((or (not value) (null? (cdr strings))) value)
  226. ((command-table? value) (loop value (cdr strings)))
  227. (else #f)))))
  228. (define (command-table-value table string)
  229. (let ((entry (command-table-entry table string)))
  230. (and entry
  231. (caddr entry))))
  232. (define (command-table-entry table string)
  233. (let loop ((entries (command-table-entries table)))
  234. (and (not (null? entries))
  235. (let ((entry (car entries)))
  236. (if (and (<= (cadr entry)
  237. (string-length string)
  238. (string-length (car entry)))
  239. (= (string-length string)
  240. (match-strings (car entry) string)))
  241. entry
  242. (loop (cdr entries)))))))
  243. (define (match-strings s1 s2)
  244. (let ((n (min (string-length s1) (string-length s2))))
  245. (let loop ((i 0))
  246. (cond ((= i n) i)
  247. ((char=? (string-ref s1 i) (string-ref s2 i)) (loop (+ i 1)))
  248. (else i)))))
  249. (define (write-command-name name)
  250. (display (car name))
  251. (for-each (lambda (string)
  252. (write-char #\space)
  253. (display string))
  254. (cdr name)))
  255. (define (add-command name value table)
  256. (let loop ((strings name) (table table))
  257. (let ((entry
  258. (or (let loop ((entries (command-table-entries table)))
  259. (and (not (null? entries))
  260. (if (string=? (car strings) (caar entries))
  261. (car entries)
  262. (loop (cdr entries)))))
  263. (let ((entry (list (car strings) #f #f)))
  264. (let ((entries
  265. (let ((entries (command-table-entries table)))
  266. (if (or (null? entries)
  267. (string<? (car strings) (caar entries)))
  268. (cons entry entries)
  269. (begin
  270. (let loop ((prev entries) (this (cdr entries)))
  271. (if (or (null? this)
  272. (string<? (car strings) (caar this)))
  273. (set-cdr! prev (cons entry this))
  274. (loop this (cdr this))))
  275. entries)))))
  276. (compute-string-abbreviations! entries)
  277. (set-command-table-entries! table entries))
  278. entry))))
  279. (if (null? (cdr strings))
  280. (set-car! (cddr entry) value)
  281. (loop (cdr strings)
  282. (if (command-table? (caddr entry))
  283. (caddr entry)
  284. (let ((table (make-command-table '())))
  285. (set-car! (cddr entry) table)
  286. table)))))))
  287. (define (canonicalize-command-name name)
  288. (cond ((and (string? name)
  289. (not (string-null? name)))
  290. (list name))
  291. ((let loop ((name name))
  292. (and (pair? name)
  293. (string? (car name))
  294. (not (string-null? (car name)))
  295. (or (null? (cdr name))
  296. (loop (cdr name)))))
  297. name)
  298. (else
  299. (error "Illegal command name: " name))))
  300. (define (compute-string-abbreviations! entries)
  301. (let loop ((entries entries) (index 0))
  302. (let ((groups '()))
  303. (for-each
  304. (lambda (entry)
  305. (let* ((char (string-ref (car entry) index))
  306. (group (assv char groups)))
  307. (if group
  308. (set-cdr! group (cons entry (cdr group)))
  309. (set! groups
  310. (cons (list char entry)
  311. groups)))))
  312. entries)
  313. (for-each
  314. (lambda (group)
  315. (let ((index (+ index 1)))
  316. (if (null? (cddr group))
  317. (set-car! (cdadr group) index)
  318. (loop (let ((entry
  319. (let loop ((entries (cdr group)))
  320. (and (not (null? entries))
  321. (if (= index (string-length (caar entries)))
  322. (car entries)
  323. (loop (cdr entries)))))))
  324. (if entry
  325. (begin
  326. (set-car! (cdr entry) index)
  327. (delq entry (cdr group)))
  328. (cdr group)))
  329. index))))
  330. groups))))
  331. ;;;; Data structures
  332. (define command-table-rtd (make-record-type "command-table" '(entries)))
  333. (define make-command-table (record-constructor command-table-rtd '(entries)))
  334. (define command-table? (record-predicate command-table-rtd))
  335. (define command-table-entries (record-accessor command-table-rtd 'entries))
  336. (define set-command-table-entries!
  337. (record-modifier command-table-rtd 'entries))
  338. (define command-rtd
  339. (make-record-type "command"
  340. '(name parser documentation procedure)))
  341. (define make-command
  342. (record-constructor command-rtd
  343. '(name parser documentation procedure)))
  344. (define command? (record-predicate command-rtd))
  345. (define command-name (record-accessor command-rtd 'name))
  346. (define command-parser (record-accessor command-rtd 'parser))
  347. (define command-documentation (record-accessor command-rtd 'documentation))
  348. (define command-procedure (record-accessor command-rtd 'procedure))
  349. ;;;; Character parsing
  350. (define (read-token port)
  351. (letrec
  352. ((loop
  353. (lambda (chars)
  354. (let ((char (peek-char port)))
  355. (cond ((eof-object? char)
  356. (do-eof char chars))
  357. ((char=? #\newline char)
  358. (do-eot chars))
  359. ((char-whitespace? char)
  360. (do-eot chars))
  361. ((char=? #\# char)
  362. (read-char port)
  363. (let ((terminator (skip-comment port)))
  364. (if (eof-object? char)
  365. (do-eof char chars)
  366. (do-eot chars))))
  367. (else
  368. (read-char port)
  369. (loop (cons char chars)))))))
  370. (do-eof
  371. (lambda (eof chars)
  372. (if (null? chars)
  373. eof
  374. (do-eot chars))))
  375. (do-eot
  376. (lambda (chars)
  377. (if (null? chars)
  378. #f
  379. (list->string (reverse! chars))))))
  380. (skip-whitespace port)
  381. (loop '())))
  382. (define (skip-whitespace port)
  383. (let ((char (peek-char port)))
  384. (cond ((or (eof-object? char)
  385. (char=? #\newline char))
  386. char)
  387. ((char-whitespace? char)
  388. (read-char port)
  389. (skip-whitespace port))
  390. ((char=? #\# char)
  391. (read-char port)
  392. (skip-comment port))
  393. (else char))))
  394. (define (skip-comment port)
  395. (let ((char (peek-char port)))
  396. (if (or (eof-object? char)
  397. (char=? #\newline char))
  398. char
  399. (begin
  400. (read-char port)
  401. (skip-comment port)))))
  402. (define (read-rest-of-line port)
  403. (let loop ((chars '()))
  404. (let ((char (read-char port)))
  405. (if (or (eof-object? char)
  406. (char=? #\newline char))
  407. (list->string (reverse! chars))
  408. (loop (cons char chars))))))
  409. (define (discard-rest-of-line port)
  410. (let loop ()
  411. (if (not (let ((char (read-char port)))
  412. (or (eof-object? char)
  413. (char=? #\newline char))))
  414. (loop))))
  415. ;;;; Commands
  416. (define command-table (make-command-table '()))
  417. (define-command "help" 'tokens
  418. (lambda (state tokens)
  419. "Type \"help\" followed by a command name for full documentation."
  420. (let loop ((name (if (null? tokens) '("help") tokens)))
  421. (let ((value (lookup-command name)))
  422. (cond ((not value)
  423. (write-command-name name)
  424. (display " is not a known command name.")
  425. (newline))
  426. ((command? value)
  427. (display (command-documentation value))
  428. (newline)
  429. (if (equal? '("help") (command-name value))
  430. (begin
  431. (display "Available commands are:")
  432. (newline)
  433. (for-each (lambda (entry)
  434. (if (not (list? (caddr entry)))
  435. (begin
  436. (display " ")
  437. (display (car entry))
  438. (newline))))
  439. (command-table-entries command-table)))))
  440. ((command-table? value)
  441. (display "The \"")
  442. (write-command-name name)
  443. (display "\" command requires a subcommand.")
  444. (newline)
  445. (display "Available subcommands are:")
  446. (newline)
  447. (for-each (lambda (entry)
  448. (if (not (list? (caddr entry)))
  449. (begin
  450. (display " ")
  451. (write-command-name name)
  452. (write-char #\space)
  453. (display (car entry))
  454. (newline))))
  455. (command-table-entries value)))
  456. ((list? value)
  457. (loop value))
  458. (else
  459. (error "Unknown value from lookup-command:" value)))))
  460. state))
  461. (define-command "frame" '('optional exact-nonnegative-integer) debugger:frame)
  462. (define-command "position" '() debugger:position)
  463. (define-command "up" '('optional exact-integer) debugger:up)
  464. (define-command "down" '('optional exact-integer) debugger:down)
  465. (define-command "backtrace" '('optional exact-integer) debugger:backtrace)
  466. (define-command "evaluate" '(object) debugger:evaluate)
  467. (define-command '("info" "args") '() debugger:info-args)
  468. (define-command '("info" "frame") '() debugger:info-frame)
  469. (define-command "quit" '()
  470. (lambda (state)
  471. "Exit the debugger."
  472. (debugger-command-loop-quit)))
  473. (define-command-alias "f" "frame")
  474. (define-command-alias '("info" "f") '("info" "frame"))
  475. (define-command-alias "bt" "backtrace")
  476. (define-command-alias "where" "backtrace")
  477. (define-command-alias "p" "evaluate")
  478. (define-command-alias '("info" "stack") "backtrace")