debug.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. ;;; semantic/analyze/debug.el --- Debug the analyzer
  2. ;;; Copyright (C) 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. ;; Provide a top-order debugging tool for figuring out what's going on with
  18. ;; smart completion and analyzer mode.
  19. (require 'semantic)
  20. (require 'semantic/analyze)
  21. (require 'semantic/analyze/complete)
  22. (require 'semantic/db-typecache)
  23. ;; For semantic-find-tags-by-class:
  24. (eval-when-compile (require 'semantic/find))
  25. (declare-function ede-get-locator-object "ede/files")
  26. ;;; Code:
  27. (defun semantic-analyze-debug-assist ()
  28. "Debug semantic analysis at the current point."
  29. (interactive)
  30. (let ((actualfcn (fetch-overload 'semantic-analyze-current-context))
  31. (ctxt (semantic-analyze-current-context))
  32. )
  33. ;; What to show.
  34. (if actualfcn
  35. (message "Mode %s does not use the default analyzer."
  36. major-mode)
  37. ;; Debug our context.
  38. )
  39. (or (semantic-analyzer-debug-test-local-context)
  40. (and ctxt (semantic-analyzer-debug-found-prefix ctxt))
  41. )
  42. ))
  43. ;; @TODO - If this happens, but the last found type is
  44. ;; a datatype, then the below is wrong
  45. (defun semantic-analyzer-debug-found-prefix (ctxt)
  46. "Debug the prefix found by the analyzer output CTXT."
  47. (let* ((pf (oref ctxt prefix))
  48. (pft (oref ctxt prefixtypes))
  49. (idx 0)
  50. (stop nil)
  51. (comp (condition-case nil
  52. (semantic-analyze-possible-completions ctxt)
  53. (error nil)))
  54. )
  55. (while (and (nth idx pf) (not stop))
  56. (let ((pentry (nth idx pf))
  57. (ptentry (nth idx pft)))
  58. (if (or (stringp pentry) (not ptentry))
  59. ;; Found something ok. Stop.
  60. (setq stop t)
  61. (setq idx (1+ idx)))))
  62. ;; We found the first non-tag entry. What is the situation?
  63. (cond
  64. ((and (eq idx 0) (stringp (car pf)))
  65. ;; First part, we couldn't find it.
  66. (semantic-analyzer-debug-global-symbol ctxt (car pf) comp))
  67. ((not (nth (1- idx) pft)) ;; idx can't be 0 here.
  68. ;; The previous entry failed to have an identifiable data
  69. ;; type, which is a global search.
  70. (semantic-analyzer-debug-missing-datatype ctxt idx comp))
  71. ((and (nth (1- idx) pft) (stringp (nth idx pf)))
  72. ;; Non-first search, didn't find string in known data type.
  73. (semantic-analyzer-debug-missing-innertype ctxt idx comp))
  74. (t
  75. ;; Things are ok?
  76. (message "Things look ok."))
  77. )))
  78. (defun semantic-analyzer-debug-global-symbol (ctxt prefix comp)
  79. "Debug why we can't find the first entry in the CTXT PREFIX.
  80. Argument COMP are possible completions here."
  81. (let ((tab semanticdb-current-table)
  82. (finderr nil)
  83. (origbuf (current-buffer))
  84. )
  85. (with-output-to-temp-buffer (help-buffer)
  86. (with-current-buffer standard-output
  87. (princ "Unable to find symbol ")
  88. (princ prefix)
  89. (princ ".\n\n")
  90. ;; NOTE: This line is copied from semantic-analyze-current-context.
  91. ;; You will need to update both places.
  92. (condition-case err
  93. (with-current-buffer origbuf
  94. (let* ((position (or (cdr-safe (oref ctxt bounds)) (point)))
  95. (prefixtypes nil) ; Used as type return
  96. (scope (semantic-calculate-scope position))
  97. )
  98. (semantic-analyze-find-tag-sequence
  99. (list prefix "") scope 'prefixtypes)
  100. )
  101. )
  102. (error (setq finderr err)))
  103. (if finderr
  104. (progn
  105. (princ "The prefix lookup code threw the following error:\n ")
  106. (prin1 finderr)
  107. (princ "\n\nTo debug this error you can do this:
  108. M-x toggle-debug-on-error RET
  109. and then re-run the debug analyzer.\n")
  110. )
  111. ;; No find error, just not found
  112. (princ "The prefix ")
  113. (princ prefix)
  114. (princ " could not be found in the local scope,
  115. nor in any search tables.\n")
  116. )
  117. (princ "\n")
  118. ;; Describe local scope, and why we might not be able to
  119. ;; find it.
  120. (semantic-analyzer-debug-describe-scope ctxt)
  121. (semantic-analyzer-debug-show-completions comp)
  122. (princ "When Semantic cannot find a symbol, it could be because the include
  123. path was setup incorrectly.\n")
  124. (semantic-analyzer-debug-insert-include-summary tab)
  125. ))
  126. (semantic-analyzer-debug-add-buttons)
  127. ))
  128. (defun semantic-analyzer-debug-missing-datatype (ctxt idx comp)
  129. "Debug why we can't find a datatype entry for CTXT prefix at IDX.
  130. Argument COMP are possible completions here."
  131. (let* ((prefixitem (nth idx (oref ctxt prefix)))
  132. (dt (nth (1- idx) (oref ctxt prefixtypes)))
  133. (tt (semantic-tag-type prefixitem))
  134. (tab semanticdb-current-table)
  135. )
  136. (when dt (error "Missing Datatype debugger is confused"))
  137. (with-output-to-temp-buffer (help-buffer)
  138. (with-current-buffer standard-output
  139. (princ "Unable to find datatype for: \"")
  140. (princ (semantic-format-tag-prototype prefixitem))
  141. (princ "\".
  142. Declared type is: ")
  143. (when (semantic-tag-p tt)
  144. (semantic-analyzer-debug-insert-tag tt)
  145. (princ "\nRaw data type is: "))
  146. (princ (format "%S" tt))
  147. (princ "
  148. Semantic could not find this data type in any of its global tables.
  149. Semantic locates datatypes through either the local scope, or the global
  150. typecache.
  151. ")
  152. ;; Describe local scope, and why we might not be able to
  153. ;; find it.
  154. (semantic-analyzer-debug-describe-scope ctxt '(type))
  155. ;; Describe the typecache.
  156. (princ "\nSemantic creates and maintains a type cache for each buffer.
  157. If the type is a global type, then it should appear in they typecache.
  158. To examine the typecache, type:
  159. M-x semanticdb-typecache-dump RET
  160. Current typecache Statistics:\n")
  161. (princ (format " %4d types global in this file\n %4d types from includes.\n"
  162. (length (semanticdb-typecache-file-tags tab))
  163. (length (semanticdb-typecache-include-tags tab))))
  164. (princ "\nIf the datatype is not in the typecache, then your include
  165. path may be incorrect. ")
  166. (semantic-analyzer-debug-insert-include-summary tab)
  167. ;; End with-buffer
  168. ))
  169. (semantic-analyzer-debug-add-buttons)
  170. ))
  171. (defun semantic-analyzer-debug-missing-innertype (ctxt idx comp)
  172. "Debug why we can't find an entry for CTXT prefix at IDX for known type.
  173. We need to see if we have possible completions against the entry before
  174. being too vocal about it.
  175. Argument COMP are possible completions here."
  176. (let* ((prefixitem (nth idx (oref ctxt prefix)))
  177. (prevprefix (nth (1- idx) (oref ctxt prefix)))
  178. (dt (nth (1- idx) (oref ctxt prefixtypes)))
  179. (desired-type (semantic-analyze-type-constraint ctxt))
  180. (orig-buffer (current-buffer))
  181. (ots (semantic-analyze-tag-type prevprefix
  182. (oref ctxt scope)
  183. t ; Don't deref
  184. ))
  185. )
  186. (when (not dt) (error "Missing Innertype debugger is confused"))
  187. (with-output-to-temp-buffer (help-buffer)
  188. (with-current-buffer standard-output
  189. (princ "Cannot find symbol \"")
  190. (princ prefixitem)
  191. (princ "\" in datatype:
  192. ")
  193. (semantic-analyzer-debug-insert-tag dt)
  194. (princ "\n")
  195. (cond
  196. ;; Any language with a namespace.
  197. ((string= (semantic-tag-type dt) "namespace")
  198. (princ "Semantic may not have found all possible namespaces with
  199. the name ")
  200. (princ (semantic-tag-name dt))
  201. (princ ". You can debug the entire typecache, including merged namespaces
  202. with the command:
  203. M-x semanticdb-typecache-dump RET")
  204. )
  205. ;; @todo - external declarations??
  206. (nil
  207. nil)
  208. ;; A generic explanation
  209. (t
  210. (princ "\nSemantic has found the datatype ")
  211. (semantic-analyzer-debug-insert-tag dt)
  212. (if (or (not (semantic-equivalent-tag-p ots dt))
  213. (not (with-current-buffer orig-buffer
  214. (car (semantic-analyze-dereference-metatype
  215. ots (oref ctxt scope))))))
  216. (let ((lasttype ots)
  217. (nexttype (with-current-buffer orig-buffer
  218. (car (semantic-analyze-dereference-metatype
  219. ots (oref ctxt scope))))))
  220. (if (eq nexttype lasttype)
  221. (princ "\n [ Debugger error trying to help with metatypes ]")
  222. (if (eq ots dt)
  223. (princ "\nwhich is a metatype")
  224. (princ "\nwhich is derived from metatype ")
  225. (semantic-analyzer-debug-insert-tag lasttype)))
  226. (princ ".\nThe Metatype stack is:\n")
  227. (princ " ")
  228. (semantic-analyzer-debug-insert-tag lasttype)
  229. (princ "\n")
  230. (while (and nexttype
  231. (not (eq nexttype lasttype)))
  232. (princ " ")
  233. (semantic-analyzer-debug-insert-tag nexttype)
  234. (princ "\n")
  235. (setq lasttype nexttype
  236. nexttype
  237. (with-current-buffer orig-buffer
  238. (car (semantic-analyze-dereference-metatype
  239. nexttype (oref ctxt scope)))))
  240. )
  241. (when (not nexttype)
  242. (princ " nil\n\n")
  243. (princ
  244. "Last metatype is nil. This means that semantic cannot derive
  245. the list of members because the type referred to cannot be found.\n")
  246. )
  247. )
  248. (princ "\nand its list of members.")
  249. (if (not comp)
  250. (progn
  251. (princ " Semantic does not know what
  252. possible completions there are for \"")
  253. (princ prefixitem)
  254. (princ "\". Examine the known
  255. members below for more."))
  256. (princ " Semantic knows of some
  257. possible completions for \"")
  258. (princ prefixitem)
  259. (princ "\".")))
  260. )
  261. ;; end cond
  262. )
  263. (princ "\n")
  264. (semantic-analyzer-debug-show-completions comp)
  265. (princ "\nKnown members of ")
  266. (princ (semantic-tag-name dt))
  267. (princ ":\n")
  268. (dolist (M (semantic-tag-type-members dt))
  269. (princ " ")
  270. ;;(princ (semantic-format-tag-prototype M))
  271. (semantic-analyzer-debug-insert-tag M)
  272. (princ "\n"))
  273. ;; This doesn't refer to in-type completions.
  274. ;;(semantic-analyzer-debug-global-miss-text prefixitem)
  275. ;; More explanation
  276. (when desired-type
  277. (princ "\nWhen there are known members that would make good completion
  278. candidates that are not in the completion list, then the most likely
  279. cause is a type constraint. Semantic has determined that there is a
  280. type constraint looking for the type ")
  281. (if (semantic-tag-p desired-type)
  282. (semantic-analyzer-debug-insert-tag desired-type)
  283. (princ (format "%S" desired-type)))
  284. (princ "."))
  285. ))
  286. (semantic-analyzer-debug-add-buttons)
  287. ))
  288. (defun semantic-analyzer-debug-test-local-context ()
  289. "Test the local context parsed from the file."
  290. (let* ((prefixandbounds (semantic-ctxt-current-symbol-and-bounds (point)))
  291. (prefix (car prefixandbounds))
  292. (bounds (nth 2 prefixandbounds))
  293. )
  294. (when (and (or (not prefixandbounds)
  295. (not prefix)
  296. (not bounds))
  297. )
  298. (with-output-to-temp-buffer (help-buffer)
  299. (with-current-buffer standard-output
  300. (princ "Local Context Parser Failed.
  301. If this is unexpected, then there is likely a bug in the Semantic
  302. local context parser.
  303. Consider debugging the function ")
  304. (let ((lcf (fetch-overload 'semantic-ctxt-current-symbol-and-bounds)))
  305. (if lcf
  306. (princ (symbol-name lcf))
  307. (princ "semantic-ctxt-current-symbol-and-bounds,
  308. or implementing a version specific to ")
  309. (princ (symbol-name major-mode))
  310. )
  311. (princ ".\n"))
  312. (semantic-analyzer-debug-add-buttons)
  313. t)))
  314. ))
  315. ;;; General Inserters with help
  316. ;;
  317. (defun semantic-analyzer-debug-show-completions (comp)
  318. "Show the completion list COMP."
  319. (if (not comp)
  320. (princ "\nNo known possible completions.\n")
  321. (princ "\nPossible completions are:\n")
  322. (dolist (C comp)
  323. (princ " ")
  324. (cond ((stringp C)
  325. (princ C)
  326. )
  327. ((semantic-tag-p C)
  328. (semantic-analyzer-debug-insert-tag C)))
  329. (princ "\n"))
  330. (princ "\n")))
  331. (defvar semantic-dependency-system-include-path)
  332. (defun semantic-analyzer-debug-insert-include-summary (table)
  333. "Display a summary of includes for the semanticdb TABLE."
  334. (require 'semantic/dep)
  335. (semantic-fetch-tags)
  336. (let ((inc (semantic-find-tags-by-class 'include table))
  337. ;;(path (semanticdb-find-test-translate-path-no-loading))
  338. (unk
  339. (with-current-buffer (semanticdb-get-buffer table)
  340. semanticdb-find-lost-includes))
  341. (ip
  342. (with-current-buffer (semanticdb-get-buffer table)
  343. semantic-dependency-system-include-path))
  344. (edeobj
  345. (with-current-buffer (semanticdb-get-buffer table)
  346. (and (boundp 'ede-object)
  347. ede-object)))
  348. (edeproj
  349. (with-current-buffer (semanticdb-get-buffer table)
  350. (and (boundp 'ede-object-project)
  351. ede-object-project))))
  352. (princ "\n\nInclude Path Summary:")
  353. (when edeobj
  354. (princ "\n\nThis file's project include search is handled by the EDE object:\n")
  355. (princ " Buffer Target: ")
  356. (princ (object-print edeobj))
  357. (princ "\n")
  358. (when (not (eq edeobj edeproj))
  359. (princ " Buffer Project: ")
  360. (princ (object-print edeproj))
  361. (princ "\n"))
  362. (when edeproj
  363. (let ((loc (ede-get-locator-object edeproj)))
  364. (princ " Backup Locator: ")
  365. (princ (object-print loc))
  366. (princ "\n")))
  367. )
  368. (princ "\n\nThe system include path is:\n")
  369. (dolist (dir ip)
  370. (princ " ")
  371. (princ dir)
  372. (princ "\n"))
  373. (princ "\n\nInclude Summary: ")
  374. (princ (semanticdb-full-filename table))
  375. (princ "\n\n")
  376. (princ (format "%s contains %d includes.\n"
  377. (file-name-nondirectory
  378. (semanticdb-full-filename table))
  379. (length inc)))
  380. (let ((ok 0)
  381. (unknown 0)
  382. (unparsed 0)
  383. (all 0))
  384. (dolist (i inc)
  385. (let* ((fileinner (semantic-dependency-tag-file i))
  386. (tableinner (when fileinner
  387. (semanticdb-file-table-object fileinner t))))
  388. (cond ((not fileinner)
  389. (setq unknown (1+ unknown)))
  390. ((number-or-marker-p (oref tableinner pointmax))
  391. (setq ok (1+ ok)))
  392. (t
  393. (setq unparsed (1+ unparsed))))))
  394. (setq all (+ ok unknown unparsed))
  395. (when (not (= 0 all))
  396. (princ (format " Unknown Includes: %d\n" unknown))
  397. (princ (format " Unparsed Includes: %d\n" unparsed))
  398. (princ (format " Parsed Includes: %d\n" ok)))
  399. )
  400. ;; Unknowns...
  401. (if unk
  402. (progn
  403. (princ "\nA likely cause of an unfound tag is missing include files.")
  404. (semantic-analyzer-debug-insert-tag-list
  405. "The following includes were not found" unk)
  406. (princ "\nYou can fix the include path for ")
  407. (princ (symbol-name (oref table major-mode)))
  408. (princ " by using this function:
  409. M-x semantic-customize-system-include-path RET
  410. which customizes the mode specific variable for the mode-local
  411. variable `semantic-dependency-system-include-path'.")
  412. )
  413. (princ "\n No unknown includes.\n"))
  414. ))
  415. (defun semantic-analyzer-debug-describe-scope (ctxt &optional classconstraint)
  416. "Describe the scope in CTXT for finding a global symbol.
  417. Optional argument CLASSCONSTRAINT says to output to tags of that class."
  418. (let* ((scope (oref ctxt :scope))
  419. (parents (oref scope parents))
  420. (cc (or classconstraint (oref ctxt prefixclass)))
  421. )
  422. (princ "\nLocal Scope Information:")
  423. (princ "\n * Tag Class Constraint against SCOPE: ")
  424. (princ (format "%S" classconstraint))
  425. (if parents
  426. (semantic-analyzer-debug-insert-tag-list
  427. " >> Known parent types with possible in scope symbols"
  428. parents)
  429. (princ "\n * No known parents in current scope."))
  430. (let ((si (semantic-analyze-tags-of-class-list
  431. (oref scope scope) cc))
  432. (lv (semantic-analyze-tags-of-class-list
  433. (oref scope localvar) cc))
  434. )
  435. (if si
  436. (semantic-analyzer-debug-insert-tag-list
  437. " >> Known symbols within the current scope"
  438. si)
  439. (princ "\n * No known symbols currently in scope."))
  440. (if lv
  441. (semantic-analyzer-debug-insert-tag-list
  442. " >> Known symbols that are declared locally"
  443. lv)
  444. (princ "\n * No known symbols declared locally."))
  445. )
  446. )
  447. )
  448. (defun semantic-analyzer-debug-global-miss-text (name-in)
  449. "Use 'princ' to show text describing not finding symbol NAME-IN.
  450. NAME is the name of the unfound symbol."
  451. (let ((name (cond ((stringp name-in)
  452. name-in)
  453. ((semantic-tag-p name-in)
  454. (semantic-format-tag-name name-in))
  455. (t (format "%S" name-in)))))
  456. (when (not (string= name ""))
  457. (princ "\nIf ")
  458. (princ name)
  459. (princ " is a local variable, argument, or symbol in some
  460. namespace or class exposed via scoping statements, then it should
  461. appear in the scope.
  462. Debugging the scope can be done with:
  463. M-x semantic-calculate-scope RET
  464. If the prefix is a global symbol, in an included file, then
  465. your search path may be incomplete.
  466. "))))
  467. ;;; Utils
  468. ;;
  469. (defun semantic-analyzer-debug-insert-tag-list (text taglist)
  470. "Prefixing with TEXT, dump TAGLIST in a help buffer."
  471. (princ "\n") (princ text) (princ ":\n")
  472. (dolist (M taglist)
  473. (princ " ")
  474. ;;(princ (semantic-format-tag-prototype M))
  475. (semantic-analyzer-debug-insert-tag M)
  476. (princ "\n"))
  477. )
  478. (defun semantic-analyzer-debug-insert-tag (tag &optional parent)
  479. "Display a TAG by name, with possible jumpitude.
  480. PARENT is a possible parent (by nesting) tag."
  481. (let ((str (semantic-format-tag-prototype tag parent)))
  482. (if (and (semantic-tag-with-position-p tag)
  483. (semantic-tag-file-name tag))
  484. (with-current-buffer standard-output
  485. (insert-button str
  486. 'mouse-face 'custom-button-pressed-face
  487. 'tag tag
  488. 'action
  489. `(lambda (button)
  490. (let ((buff nil)
  491. (pnt nil))
  492. (save-excursion
  493. (semantic-go-to-tag
  494. (button-get button 'tag))
  495. (setq buff (current-buffer))
  496. (setq pnt (point)))
  497. (if (get-buffer-window buff)
  498. (select-window (get-buffer-window buff))
  499. (pop-to-buffer buff t))
  500. (goto-char pnt)
  501. (pulse-line-hook-function)))
  502. ))
  503. (princ "\"")
  504. (princ str)
  505. (princ "\""))
  506. ))
  507. (defvar semantic-analyzer-debug-orig nil
  508. "The originating buffer for a help button.")
  509. (defun semantic-analyzer-debug-add-buttons ()
  510. "Add push-buttons to the *Help* buffer.
  511. Look for key expressions, and add push-buttons near them."
  512. (let ((orig-buffer (make-marker)))
  513. (set-marker orig-buffer (point) (current-buffer))
  514. ;; Get a buffer ready.
  515. (with-current-buffer "*Help*"
  516. (let ((inhibit-read-only t))
  517. (goto-char (point-min))
  518. (set (make-local-variable 'semantic-analyzer-debug-orig) orig-buffer)
  519. ;; First, add do-in buttons to recommendations.
  520. (while (re-search-forward "^\\s-*M-x \\(\\(\\w\\|\\s_\\)+\\) " nil t)
  521. (let ((fcn (match-string 1)))
  522. (when (not (fboundp (intern-soft fcn)))
  523. (error "Help Err: Can't find %s" fcn))
  524. (end-of-line)
  525. (insert " ")
  526. (insert-button "[ Do It ]"
  527. 'mouse-face 'custom-button-pressed-face
  528. 'do-fcn fcn
  529. 'action `(lambda (arg)
  530. (let ((M semantic-analyzer-debug-orig))
  531. (set-buffer (marker-buffer M))
  532. (goto-char M))
  533. (call-interactively (quote ,(intern-soft fcn))))))))
  534. ;; Do something else?
  535. ;; Clean up the mess
  536. (set-buffer-modified-p nil))))
  537. (provide 'semantic/analyze/debug)
  538. ;;; semantic/analyze/debug.el ends here