debug.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. ;;; semantic/debug.el --- Language Debugger framework
  2. ;; Copyright (C) 2003-2005, 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs 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
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; To provide better support for debugging parsers, this framework
  18. ;; provides the interface for debugging. The work of parsing and
  19. ;; controlling and stepping through the parsing work must be implemented
  20. ;; by the parser.
  21. ;;
  22. ;; Fortunately, the nature of language support files means that the parser
  23. ;; may not need to be instrumented first.
  24. ;;
  25. ;; The debugger uses EIEIO objects. One object controls the user
  26. ;; interface, including stepping, data-view, queries. A second
  27. ;; object implemented here represents the parser itself. A third represents
  28. ;; a parser independent frame which knows how to highlight the parser buffer.
  29. ;; Each parser must implement the interface and override any methods as needed.
  30. ;;
  31. (eval-when-compile (require 'cl))
  32. (require 'semantic)
  33. (require 'eieio)
  34. (eval-when-compile (require 'semantic/find))
  35. ;;; Code:
  36. ;;;###autoload
  37. (defvar semantic-debug-parser-source nil
  38. "For any buffer, the file name (no path) of the parser.
  39. This would be a parser for a specific language, not the source
  40. to one of the parser generators.")
  41. ;;;###autoload
  42. (make-variable-buffer-local 'semantic-debug-parser-source)
  43. ;;;###autoload
  44. (defvar semantic-debug-parser-class nil
  45. "Class to create when building a debug parser object.")
  46. ;;;###autoload
  47. (make-variable-buffer-local 'semantic-debug-parser-class)
  48. (defvar semantic-debug-enabled nil
  49. "Non-nil when debugging a parser.")
  50. ;;; Variables used during a debug session.
  51. (defvar semantic-debug-current-interface nil
  52. "The debugger interface currently active for this buffer.")
  53. (defvar semantic-debug-current-parser nil
  54. "The parser current active for this buffer.")
  55. ;;; User Interface Portion
  56. ;;
  57. (defclass semantic-debug-interface ()
  58. ((parser-buffer :initarg :parser-buffer
  59. :type buffer
  60. :documentation
  61. "The buffer containing the parser we are debugging.")
  62. (parser-local-map :initarg :parser-local-map
  63. :type keymap
  64. :documentation
  65. "The local keymap originally in the PARSER buffer.")
  66. (parser-location :type marker
  67. :documentation
  68. "A marker representing where we are in the parser buffer.")
  69. (source-buffer :initarg :source-buffer
  70. :type buffer
  71. :documentation
  72. "The buffer containing the source we are parsing.
  73. The :parser-buffer defines a parser that can parse the text in the
  74. :source-buffer.")
  75. (source-local-map :initarg :source-local-map
  76. :type keymap
  77. :documentation
  78. "The local keymap originally in the SOURCE buffer.")
  79. (source-location :type marker
  80. :documentation
  81. "A marker representing where we are in the parser buffer.")
  82. (data-buffer :initarg :data-buffer
  83. :type buffer
  84. :documentation
  85. "Buffer being used to display some useful data.
  86. These buffers are brought into view when layout occurs.")
  87. (current-frame :type semantic-debug-frame
  88. :documentation
  89. "The currently displayed frame.")
  90. (overlays :type list
  91. :initarg nil
  92. :documentation
  93. "Any active overlays being used to show the debug position.")
  94. )
  95. "Controls action when in `semantic-debug-mode'")
  96. ;; Methods
  97. (defmethod semantic-debug-set-frame ((iface semantic-debug-interface) frame)
  98. "Set the current frame on IFACE to FRAME."
  99. (if frame
  100. (oset iface current-frame frame)
  101. (slot-makeunbound iface 'current-frame)))
  102. (defmethod semantic-debug-set-parser-location ((iface semantic-debug-interface) point)
  103. "Set the parser location in IFACE to POINT."
  104. (with-current-buffer (oref iface parser-buffer)
  105. (if (not (slot-boundp iface 'parser-location))
  106. (oset iface parser-location (make-marker)))
  107. (move-marker (oref iface parser-location) point))
  108. )
  109. (defmethod semantic-debug-set-source-location ((iface semantic-debug-interface) point)
  110. "Set the source location in IFACE to POINT."
  111. (with-current-buffer (oref iface source-buffer)
  112. (if (not (slot-boundp iface 'source-location))
  113. (oset iface source-location (make-marker)))
  114. (move-marker (oref iface source-location) point))
  115. )
  116. (defmethod semantic-debug-interface-layout ((iface semantic-debug-interface))
  117. "Layout windows in the current frame to facilitate debugging."
  118. (delete-other-windows)
  119. ;; Deal with the data buffer
  120. (when (slot-boundp iface 'data-buffer)
  121. (let ((lines (/ (frame-height (selected-frame)) 3))
  122. (cnt (with-current-buffer (oref iface data-buffer)
  123. (count-lines (point-min) (point-max))))
  124. )
  125. ;; Set the number of lines to 1/3, or the size of the data buffer.
  126. (if (< cnt lines) (setq cnt lines))
  127. (split-window-vertically cnt)
  128. (switch-to-buffer (oref iface data-buffer))
  129. )
  130. (other-window 1))
  131. ;; Parser
  132. (switch-to-buffer (oref iface parser-buffer))
  133. (when (slot-boundp iface 'parser-location)
  134. (goto-char (oref iface parser-location)))
  135. (split-window-vertically)
  136. (other-window 1)
  137. ;; Source
  138. (switch-to-buffer (oref iface source-buffer))
  139. (when (slot-boundp iface 'source-location)
  140. (goto-char (oref iface source-location)))
  141. )
  142. (defmethod semantic-debug-highlight-lexical-token ((iface semantic-debug-interface) token)
  143. "For IFACE, highlight TOKEN in the source buffer .
  144. TOKEN is a lexical token."
  145. (set-buffer (oref iface :source-buffer))
  146. (object-add-to-list iface 'overlays
  147. (semantic-lex-highlight-token token))
  148. (semantic-debug-set-source-location iface (semantic-lex-token-start token))
  149. )
  150. (defmethod semantic-debug-highlight-rule ((iface semantic-debug-interface) nonterm &optional rule match)
  151. "For IFACE, highlight NONTERM in the parser buffer.
  152. NONTERM is the name of the rule currently being processed that shows up
  153. as a nonterminal (or tag) in the source buffer.
  154. If RULE and MATCH indices are specified, highlight those also."
  155. (set-buffer (oref iface :parser-buffer))
  156. (let* ((rules (semantic-find-tags-by-class 'nonterminal (current-buffer)))
  157. (nt (semantic-find-first-tag-by-name nonterm rules))
  158. (o nil)
  159. )
  160. (when nt
  161. ;; I know it is the first symbol appearing in the body of this token.
  162. (goto-char (semantic-tag-start nt))
  163. (setq o (semantic-make-overlay (point) (progn (forward-sexp 1) (point))))
  164. (semantic-overlay-put o 'face 'highlight)
  165. (object-add-to-list iface 'overlays o)
  166. (semantic-debug-set-parser-location iface (semantic-overlay-start o))
  167. (when (and rule match)
  168. ;; Rule, an int, is the rule inside the nonterminal we are following.
  169. (re-search-forward ":\\s-*")
  170. (while (/= 0 rule)
  171. (re-search-forward "^\\s-*|\\s-*")
  172. (setq rule (1- rule)))
  173. ;; Now find the match inside the rule
  174. (while (/= 0 match)
  175. (forward-sexp 1)
  176. (skip-chars-forward " \t")
  177. (setq match (1- match)))
  178. ;; Now highlight the thingy we find there.
  179. (setq o (semantic-make-overlay (point) (progn (forward-sexp 1) (point))))
  180. (semantic-overlay-put o 'face 'highlight)
  181. (object-add-to-list iface 'overlays o)
  182. ;; If we have a match for a sub-rule, have the parser position
  183. ;; move so we can see it in the output window for very long rules.
  184. (semantic-debug-set-parser-location iface (semantic-overlay-start o))
  185. ))))
  186. (defmethod semantic-debug-unhighlight ((iface semantic-debug-interface))
  187. "Remove all debugging overlays."
  188. (mapc 'semantic-overlay-delete (oref iface overlays))
  189. (oset iface overlays nil))
  190. ;; Call from the parser at a breakpoint
  191. (defvar semantic-debug-user-command nil
  192. "The command the user is requesting.")
  193. (defun semantic-debug-break (frame)
  194. "Stop parsing now at FRAME.
  195. FRAME is an object that represents the parser's view of the
  196. current state of the world.
  197. This function enters a recursive edit. It returns
  198. on an `exit-recursive-edit', or if someone uses one
  199. of the `semantic-debug-mode' commands.
  200. It returns the command specified. Parsers need to take action
  201. on different types of return values."
  202. (save-window-excursion
  203. ;; Set up displaying information
  204. (semantic-debug-mode t)
  205. (unwind-protect
  206. (progn
  207. (semantic-debug-frame-highlight frame)
  208. (semantic-debug-interface-layout semantic-debug-current-interface)
  209. (condition-case nil
  210. ;; Enter recursive edit... wait for user command.
  211. (recursive-edit)
  212. (error nil)))
  213. (semantic-debug-unhighlight semantic-debug-current-interface)
  214. (semantic-debug-mode nil))
  215. ;; Find the requested user state. Do something.
  216. (let ((returnstate semantic-debug-user-command))
  217. (setq semantic-debug-user-command nil)
  218. returnstate)
  219. ))
  220. ;;; Frame
  221. ;;
  222. ;; A frame can represent the state at a break point.
  223. (defclass semantic-debug-frame ()
  224. (
  225. )
  226. "One frame representation.")
  227. (defmethod semantic-debug-frame-highlight ((frame semantic-debug-frame))
  228. "Highlight one parser frame."
  229. )
  230. (defmethod semantic-debug-frame-info ((frame semantic-debug-frame))
  231. "Display info about this one parser frame."
  232. )
  233. ;;; Major Mode
  234. ;;
  235. (defvar semantic-debug-mode-map
  236. (let ((km (make-sparse-keymap)))
  237. (define-key km "n" 'semantic-debug-next)
  238. (define-key km " " 'semantic-debug-next)
  239. (define-key km "s" 'semantic-debug-step)
  240. (define-key km "u" 'semantic-debug-up)
  241. (define-key km "d" 'semantic-debug-down)
  242. (define-key km "f" 'semantic-debug-fail-match)
  243. (define-key km "h" 'semantic-debug-print-state)
  244. (define-key km "s" 'semantic-debug-jump-to-source)
  245. (define-key km "p" 'semantic-debug-jump-to-parser)
  246. (define-key km "q" 'semantic-debug-quit)
  247. (define-key km "a" 'semantic-debug-abort)
  248. (define-key km "g" 'semantic-debug-go)
  249. (define-key km "b" 'semantic-debug-set-breakpoint)
  250. ;; Some boring bindings.
  251. (define-key km "e" 'eval-expression)
  252. km)
  253. "Keymap used when in semantic-debug-node.")
  254. (defun semantic-debug-mode (onoff)
  255. "Turn `semantic-debug-mode' on and off.
  256. Argument ONOFF is non-nil when we are entering debug mode.
  257. \\{semantic-debug-mode-map}"
  258. (let ((iface semantic-debug-current-interface))
  259. (if onoff
  260. ;; Turn it on
  261. (with-current-buffer (oref iface parser-buffer)
  262. ;; Install our map onto this buffer
  263. (use-local-map semantic-debug-mode-map)
  264. ;; Make the buffer read only
  265. (toggle-read-only 1)
  266. (set-buffer (oref iface source-buffer))
  267. ;; Use our map in the source buffer also
  268. (use-local-map semantic-debug-mode-map)
  269. ;; Make the buffer read only
  270. (toggle-read-only 1)
  271. ;; Hooks
  272. (run-hooks 'semantic-debug-mode-hook)
  273. )
  274. ;; Restore old mode information
  275. (with-current-buffer
  276. (oref semantic-debug-current-interface parser-buffer)
  277. (use-local-map
  278. (oref semantic-debug-current-interface parser-local-map))
  279. )
  280. (with-current-buffer
  281. (oref semantic-debug-current-interface source-buffer)
  282. (use-local-map
  283. (oref semantic-debug-current-interface source-local-map))
  284. )
  285. (run-hooks 'semantic-debug-exit-hook)
  286. )))
  287. (defun semantic-debug ()
  288. "Parse the current buffer and run in debug mode."
  289. (interactive)
  290. (if semantic-debug-current-interface
  291. (error "You are already in a debug session"))
  292. (if (not semantic-debug-parser-class)
  293. (error "This major mode does not support parser debugging"))
  294. ;; Clear the cache to force a full reparse.
  295. (semantic-clear-toplevel-cache)
  296. ;; Do the parse
  297. (let ((semantic-debug-enabled t)
  298. ;; Create an interface
  299. (semantic-debug-current-interface
  300. (let ((parserb (semantic-debug-find-parser-source)))
  301. (semantic-debug-interface
  302. "Debug Interface"
  303. :parser-buffer parserb
  304. :parser-local-map (with-current-buffer parserb
  305. (current-local-map))
  306. :source-buffer (current-buffer)
  307. :source-local-map (current-local-map)
  308. )))
  309. ;; Create a parser debug interface
  310. (semantic-debug-current-parser
  311. (funcall semantic-debug-parser-class "parser"))
  312. )
  313. ;; We could recurse into a parser while debugging.
  314. ;; Is that a problem?
  315. (semantic-fetch-tags)
  316. ;; We should turn the auto-parser back on, but don't do it for
  317. ;; now until the debugger is working well.
  318. ))
  319. (defun semantic-debug-find-parser-source ()
  320. "Return a buffer containing the parser source file for the current buffer.
  321. The parser needs to be on the load path, or this routine returns nil."
  322. (if (not semantic-debug-parser-source)
  323. (error "No parser is associated with this buffer"))
  324. (let ((parser (locate-library semantic-debug-parser-source t)))
  325. (if parser
  326. (find-file-noselect parser)
  327. (error "Cannot find parser source. It should be on the load-path"))))
  328. ;;; Debugger commands
  329. ;;
  330. (defun semantic-debug-next ()
  331. "Perform one parser operation.
  332. In the recursive parser, this steps past one match rule.
  333. In other parsers, this may be just like `semantic-debug-step'."
  334. (interactive)
  335. (let ((parser semantic-debug-current-parser))
  336. (semantic-debug-parser-next parser)
  337. (exit-recursive-edit)
  338. )
  339. )
  340. (defun semantic-debug-step ()
  341. "Perform one parser operation."
  342. (interactive)
  343. (let ((parser semantic-debug-current-parser))
  344. (semantic-debug-parser-step parser)
  345. (exit-recursive-edit)
  346. )
  347. )
  348. (defun semantic-debug-up ()
  349. "Move highlighting representation up one level."
  350. (interactive)
  351. (message "Not implemented yet.")
  352. )
  353. (defun semantic-debug-down ()
  354. "Move highlighting representation down one level."
  355. (interactive)
  356. (message "Not implemented yet.")
  357. )
  358. (defun semantic-debug-fail-match ()
  359. "Artificially fail the current match."
  360. (interactive)
  361. (let ((parser semantic-debug-current-parser))
  362. (semantic-debug-parser-fail parser)
  363. (exit-recursive-edit)
  364. )
  365. )
  366. (defun semantic-debug-print-state ()
  367. "Show interesting parser state."
  368. (interactive)
  369. (let ((parser semantic-debug-current-parser))
  370. (semantic-debug-parser-print-state parser)
  371. )
  372. )
  373. (defun semantic-debug-jump-to-source ()
  374. "Move cursor to the source code being parsed at the current lexical token."
  375. (interactive)
  376. (let* ((interface semantic-debug-current-interface)
  377. (buf (oref interface source-buffer)))
  378. (if (get-buffer-window buf)
  379. (progn
  380. (select-frame (window-frame (get-buffer-window buf)))
  381. (select-window (get-buffer-window buf)))
  382. ;; Technically, this should do a window layout operation
  383. (switch-to-buffer buf))
  384. )
  385. )
  386. (defun semantic-debug-jump-to-parser ()
  387. "Move cursor to the parser being debugged."
  388. (interactive)
  389. (let* ((interface semantic-debug-current-interface)
  390. (buf (oref interface parser-buffer)))
  391. (if (get-buffer-window buf)
  392. (progn
  393. (select-frame (window-frame (get-buffer-window buf)))
  394. (select-window (get-buffer-window buf)))
  395. ;; Technically, this should do a window layout operation
  396. (switch-to-buffer buf))
  397. )
  398. )
  399. (defun semantic-debug-quit ()
  400. "Exit debug mode, blowing all stack, and leaving the parse incomplete.
  401. Do not update any tokens already parsed."
  402. (interactive)
  403. (let ((parser semantic-debug-current-parser))
  404. (semantic-debug-parser-quit parser)
  405. (exit-recursive-edit)
  406. )
  407. )
  408. (defun semantic-debug-abort ()
  409. "Abort one level of debug mode, blowing all stack."
  410. (interactive)
  411. (let ((parser semantic-debug-current-parser))
  412. (semantic-debug-parser-abort parser)
  413. (exit-recursive-edit)
  414. )
  415. )
  416. (defun semantic-debug-go ()
  417. "Continue parsing till finish or breakpoint."
  418. (interactive)
  419. (let ((parser semantic-debug-current-parser))
  420. (semantic-debug-parser-go parser)
  421. (exit-recursive-edit)
  422. )
  423. )
  424. (defun semantic-debug-set-breakpoint ()
  425. "Set a breakpoint at the current rule location."
  426. (interactive)
  427. (let ((parser semantic-debug-current-parser)
  428. ;; Get the location as semantic tokens.
  429. (location (semantic-current-tag))
  430. )
  431. (if location
  432. (semantic-debug-parser-break parser location)
  433. (error "Not on a rule"))
  434. )
  435. )
  436. ;;; Debugger superclass
  437. ;;
  438. (defclass semantic-debug-parser ()
  439. (
  440. )
  441. "Represents a parser and its state.
  442. When implementing the debug parser you can add extra functionality
  443. by overriding one of the command methods. Be sure to use
  444. `call-next-method' so that the debug command is saved, and passed
  445. down to your parser later."
  446. :abstract t)
  447. (defmethod semantic-debug-parser-next ((parser semantic-debug-parser))
  448. "Execute next for this PARSER."
  449. (setq semantic-debug-user-command 'next)
  450. )
  451. (defmethod semantic-debug-parser-step ((parser semantic-debug-parser))
  452. "Execute a step for this PARSER."
  453. (setq semantic-debug-user-command 'step)
  454. )
  455. (defmethod semantic-debug-parser-go ((parser semantic-debug-parser))
  456. "Continue execution in this PARSER until the next breakpoint."
  457. (setq semantic-debug-user-command 'go)
  458. )
  459. (defmethod semantic-debug-parser-fail ((parser semantic-debug-parser))
  460. "Continue execution in this PARSER until the next breakpoint."
  461. (setq semantic-debug-user-command 'fail)
  462. )
  463. (defmethod semantic-debug-parser-quit ((parser semantic-debug-parser))
  464. "Continue execution in this PARSER until the next breakpoint."
  465. (setq semantic-debug-user-command 'quit)
  466. )
  467. (defmethod semantic-debug-parser-abort ((parser semantic-debug-parser))
  468. "Continue execution in this PARSER until the next breakpoint."
  469. (setq semantic-debug-user-command 'abort)
  470. )
  471. (defmethod semantic-debug-parser-print-state ((parser semantic-debug-parser))
  472. "Print state for this PARSER at the current breakpoint."
  473. (with-slots (current-frame) semantic-debug-current-interface
  474. (when current-frame
  475. (semantic-debug-frame-info current-frame)
  476. )))
  477. (defmethod semantic-debug-parser-break ((parser semantic-debug-parser))
  478. "Set a breakpoint for this PARSER."
  479. )
  480. ;; Stack stuff
  481. (defmethod semantic-debug-parser-frames ((parser semantic-debug-parser))
  482. "Return a list of frames for the current parser.
  483. A frame is of the form:
  484. ( .. .what ? .. )
  485. "
  486. (error "Parser has not implemented frame values")
  487. )
  488. (provide 'semantic/debug)
  489. ;; Local variables:
  490. ;; generated-autoload-file: "loaddefs.el"
  491. ;; generated-autoload-load-name: "semantic/debug"
  492. ;; End:
  493. ;;; semantic/debug.el ends here