gud.el 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481
  1. ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
  2. ;; Copyright (C) 1992-1996, 1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  4. ;; Maintainer: FSF
  5. ;; Keywords: unix, tools
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>.
  19. ;; It was later rewritten by rms. Some ideas were due to Masanobu. Grand
  20. ;; Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com> Barry
  21. ;; Warsaw <bwarsaw@cen.com> hacked the mode to use comint.el. Shane Hartman
  22. ;; <shane@spr.com> added support for xdb (HPUX debugger). Rick Sladkey
  23. ;; <jrs@world.std.com> wrote the GDB command completion code. Dave Love
  24. ;; <d.love@dl.ac.uk> added the IRIX kluge, re-implemented the Mips-ish variant
  25. ;; and added a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX
  26. ;; kluge with the gud-xdb-directories hack producing gud-dbx-directories.
  27. ;; Derek L. Davies <ddavies@world.std.com> added support for jdb (Java
  28. ;; debugger.)
  29. ;;; Code:
  30. (eval-when-compile (require 'cl)) ; for case macro
  31. (require 'comint)
  32. (defvar gdb-active-process)
  33. (defvar gdb-define-alist)
  34. (defvar gdb-macro-info)
  35. (defvar gdb-show-changed-values)
  36. (defvar gdb-source-window)
  37. (defvar gdb-var-list)
  38. (defvar gdb-speedbar-auto-raise)
  39. (defvar gud-tooltip-mode)
  40. (defvar hl-line-mode)
  41. (defvar hl-line-sticky-flag)
  42. (defvar tool-bar-map)
  43. ;; ======================================================================
  44. ;; GUD commands must be visible in C buffers visited by GUD
  45. (defgroup gud nil
  46. "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
  47. Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python) and jdb."
  48. :group 'processes
  49. :group 'tools)
  50. (defcustom gud-key-prefix "\C-x\C-a"
  51. "Prefix of all GUD commands valid in C buffers."
  52. :type 'string
  53. :group 'gud)
  54. (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
  55. (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
  56. (defvar gud-marker-filter nil)
  57. (put 'gud-marker-filter 'permanent-local t)
  58. (defvar gud-find-file nil)
  59. (put 'gud-find-file 'permanent-local t)
  60. (defun gud-marker-filter (&rest args)
  61. (apply gud-marker-filter args))
  62. (defvar gud-minor-mode nil)
  63. (put 'gud-minor-mode 'permanent-local t)
  64. (defvar gud-comint-buffer nil)
  65. (defvar gud-keep-buffer nil)
  66. (defun gud-symbol (sym &optional soft minor-mode)
  67. "Return the symbol used for SYM in MINOR-MODE.
  68. MINOR-MODE defaults to `gud-minor-mode'.
  69. The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
  70. If SOFT is non-nil, returns nil if the symbol doesn't already exist."
  71. (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
  72. (funcall (if soft 'intern-soft 'intern)
  73. (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
  74. (defun gud-val (sym &optional minor-mode)
  75. "Return the value of `gud-symbol' SYM. Default to nil."
  76. (let ((sym (gud-symbol sym t minor-mode)))
  77. (if (boundp sym) (symbol-value sym))))
  78. (defvar gud-running nil
  79. "Non-nil if debugged program is running.
  80. Used to gray out relevant toolbar icons.")
  81. (defvar gud-target-name "--unknown--"
  82. "The apparent name of the program being debugged in a gud buffer.")
  83. ;; Use existing Info buffer, if possible.
  84. (defun gud-goto-info ()
  85. "Go to relevant Emacs info node."
  86. (interactive)
  87. (if (eq gud-minor-mode 'gdbmi)
  88. (info-other-window "(emacs)GDB Graphical Interface")
  89. (info-other-window "(emacs)Debuggers")))
  90. (defun gud-tool-bar-item-visible-no-fringe ()
  91. (not (or (eq (buffer-local-value 'major-mode (window-buffer)) 'speedbar-mode)
  92. (eq (buffer-local-value 'major-mode (window-buffer)) 'gdb-memory-mode)
  93. (and (eq gud-minor-mode 'gdbmi)
  94. (> (car (window-fringes)) 0)))))
  95. (declare-function gdb-gud-context-command "gdb-mi.el")
  96. (defun gud-stop-subjob ()
  97. (interactive)
  98. (with-current-buffer gud-comint-buffer
  99. (cond ((string-equal gud-target-name "emacs")
  100. (comint-stop-subjob))
  101. ((eq gud-minor-mode 'jdb)
  102. (gud-call "suspend"))
  103. ((eq gud-minor-mode 'gdbmi)
  104. (gud-call (gdb-gud-context-command "-exec-interrupt")))
  105. (t
  106. (comint-interrupt-subjob)))))
  107. (easy-mmode-defmap gud-menu-map
  108. '(([help] "Info (debugger)" . gud-goto-info)
  109. ([tooltips] menu-item "Show GUD tooltips" gud-tooltip-mode
  110. :enable (and (not emacs-basic-display)
  111. (display-graphic-p)
  112. (fboundp 'x-show-tip))
  113. :visible (memq gud-minor-mode
  114. '(gdbmi dbx sdb xdb pdb))
  115. :button (:toggle . gud-tooltip-mode))
  116. ([refresh] "Refresh" . gud-refresh)
  117. ([run] menu-item "Run" gud-run
  118. :enable (not gud-running)
  119. :visible (memq gud-minor-mode '(gdbmi gdb dbx jdb)))
  120. ([go] menu-item (if gdb-active-process "Continue" "Run") gud-go
  121. :visible (and (eq gud-minor-mode 'gdbmi)
  122. (gdb-show-run-p)))
  123. ([stop] menu-item "Stop" gud-stop-subjob
  124. :visible (or (not (memq gud-minor-mode '(gdbmi pdb)))
  125. (gdb-show-stop-p)))
  126. ([until] menu-item "Continue to selection" gud-until
  127. :enable (not gud-running)
  128. :visible (and (memq gud-minor-mode '(gdbmi gdb perldb))
  129. (gud-tool-bar-item-visible-no-fringe)))
  130. ([remove] menu-item "Remove Breakpoint" gud-remove
  131. :enable (not gud-running)
  132. :visible (gud-tool-bar-item-visible-no-fringe))
  133. ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
  134. :enable (not gud-running)
  135. :visible (memq gud-minor-mode
  136. '(gdbmi gdb sdb xdb)))
  137. ([break] menu-item "Set Breakpoint" gud-break
  138. :enable (not gud-running)
  139. :visible (gud-tool-bar-item-visible-no-fringe))
  140. ([up] menu-item "Up Stack" gud-up
  141. :enable (not gud-running)
  142. :visible (memq gud-minor-mode
  143. '(gdbmi gdb dbx xdb jdb pdb)))
  144. ([down] menu-item "Down Stack" gud-down
  145. :enable (not gud-running)
  146. :visible (memq gud-minor-mode
  147. '(gdbmi gdb dbx xdb jdb pdb)))
  148. ([pp] menu-item "Print S-expression" gud-pp
  149. :enable (and (not gud-running)
  150. gdb-active-process)
  151. :visible (and (string-equal
  152. (buffer-local-value
  153. 'gud-target-name gud-comint-buffer) "emacs")
  154. (eq gud-minor-mode 'gdbmi)))
  155. ([print*] menu-item (if (eq gud-minor-mode 'jdb)
  156. "Dump object"
  157. "Print Dereference") gud-pstar
  158. :enable (not gud-running)
  159. :visible (memq gud-minor-mode '(gdbmi gdb jdb)))
  160. ([print] menu-item "Print Expression" gud-print
  161. :enable (not gud-running))
  162. ([watch] menu-item "Watch Expression" gud-watch
  163. :enable (not gud-running)
  164. :visible (eq gud-minor-mode 'gdbmi))
  165. ([finish] menu-item "Finish Function" gud-finish
  166. :enable (not gud-running)
  167. :visible (memq gud-minor-mode
  168. '(gdbmi gdb xdb jdb pdb)))
  169. ([stepi] menu-item "Step Instruction" gud-stepi
  170. :enable (not gud-running)
  171. :visible (memq gud-minor-mode '(gdbmi gdb dbx)))
  172. ([nexti] menu-item "Next Instruction" gud-nexti
  173. :enable (not gud-running)
  174. :visible (memq gud-minor-mode '(gdbmi gdb dbx)))
  175. ([step] menu-item "Step Line" gud-step
  176. :enable (not gud-running))
  177. ([next] menu-item "Next Line" gud-next
  178. :enable (not gud-running))
  179. ([cont] menu-item "Continue" gud-cont
  180. :enable (not gud-running)
  181. :visible (not (eq gud-minor-mode 'gdbmi))))
  182. "Menu for `gud-mode'."
  183. :name "Gud")
  184. (easy-mmode-defmap gud-minor-mode-map
  185. (append
  186. `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
  187. ;; Get tool bar like functionality from the menu bar on a text only
  188. ;; terminal.
  189. (unless window-system
  190. `(([menu-bar down]
  191. . (,(propertize "down" 'face 'font-lock-doc-face) . gud-down))
  192. ([menu-bar up]
  193. . (,(propertize "up" 'face 'font-lock-doc-face) . gud-up))
  194. ([menu-bar finish]
  195. . (,(propertize "finish" 'face 'font-lock-doc-face) . gud-finish))
  196. ([menu-bar step]
  197. . (,(propertize "step" 'face 'font-lock-doc-face) . gud-step))
  198. ([menu-bar next]
  199. . (,(propertize "next" 'face 'font-lock-doc-face) . gud-next))
  200. ([menu-bar until] menu-item
  201. ,(propertize "until" 'face 'font-lock-doc-face) gud-until
  202. :visible (memq gud-minor-mode '(gdbmi gdb perldb)))
  203. ([menu-bar cont] menu-item
  204. ,(propertize "cont" 'face 'font-lock-doc-face) gud-cont
  205. :visible (not (eq gud-minor-mode 'gdbmi)))
  206. ([menu-bar run] menu-item
  207. ,(propertize "run" 'face 'font-lock-doc-face) gud-run
  208. :visible (memq gud-minor-mode '(gdbmi gdb dbx jdb)))
  209. ([menu-bar go] menu-item
  210. ,(propertize " go " 'face 'font-lock-doc-face) gud-go
  211. :visible (and (eq gud-minor-mode 'gdbmi)
  212. (gdb-show-run-p)))
  213. ([menu-bar stop] menu-item
  214. ,(propertize "stop" 'face 'font-lock-doc-face) gud-stop-subjob
  215. :visible (or (and (eq gud-minor-mode 'gdbmi)
  216. (gdb-show-stop-p))
  217. (not (eq gud-minor-mode 'gdbmi))))
  218. ([menu-bar print]
  219. . (,(propertize "print" 'face 'font-lock-doc-face) . gud-print))
  220. ([menu-bar tools] . undefined)
  221. ([menu-bar buffer] . undefined)
  222. ([menu-bar options] . undefined)
  223. ([menu-bar edit] . undefined)
  224. ([menu-bar file] . undefined))))
  225. "Map used in visited files.")
  226. (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
  227. (if m (setcdr m gud-minor-mode-map)
  228. (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
  229. (defvar gud-mode-map
  230. ;; Will inherit from comint-mode via define-derived-mode.
  231. (make-sparse-keymap)
  232. "`gud-mode' keymap.")
  233. (defvar gud-tool-bar-map
  234. (let ((map (make-sparse-keymap)))
  235. (dolist (x '((gud-break . "gud/break")
  236. (gud-remove . "gud/remove")
  237. (gud-print . "gud/print")
  238. (gud-pstar . "gud/pstar")
  239. (gud-pp . "gud/pp")
  240. (gud-watch . "gud/watch")
  241. (gud-run . "gud/run")
  242. (gud-go . "gud/go")
  243. (gud-stop-subjob . "gud/stop")
  244. (gud-cont . "gud/cont")
  245. (gud-until . "gud/until")
  246. (gud-next . "gud/next")
  247. (gud-step . "gud/step")
  248. (gud-finish . "gud/finish")
  249. (gud-nexti . "gud/nexti")
  250. (gud-stepi . "gud/stepi")
  251. (gud-up . "gud/up")
  252. (gud-down . "gud/down")
  253. (gud-goto-info . "info"))
  254. map)
  255. (tool-bar-local-item-from-menu
  256. (car x) (cdr x) map gud-minor-mode-map))))
  257. (defun gud-file-name (f)
  258. "Transform a relative file name to an absolute file name.
  259. Uses `gud-<MINOR-MODE>-directories' to find the source files."
  260. ;; When `default-directory' is a remote file name, prepend its
  261. ;; remote part to f, which is the local file name. Fortunately,
  262. ;; `file-remote-p' returns exactly this remote file name part (or
  263. ;; nil otherwise).
  264. (setq f (concat (or (file-remote-p default-directory) "") f))
  265. (if (file-exists-p f) (expand-file-name f)
  266. (let ((directories (gud-val 'directories))
  267. (result nil))
  268. (while directories
  269. (let ((path (expand-file-name f (car directories))))
  270. (if (file-exists-p path)
  271. (setq result path
  272. directories nil)))
  273. (setq directories (cdr directories)))
  274. result)))
  275. (declare-function gdb-create-define-alist "gdb-mi" ())
  276. (defun gud-find-file (file)
  277. ;; Don't get confused by double slashes in the name that comes from GDB.
  278. (while (string-match "//+" file)
  279. (setq file (replace-match "/" t t file)))
  280. (let ((minor-mode gud-minor-mode)
  281. (buf (funcall (or gud-find-file 'gud-file-name) file)))
  282. (when (stringp buf)
  283. (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
  284. (when buf
  285. ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
  286. (with-current-buffer buf
  287. (set (make-local-variable 'gud-minor-mode) minor-mode)
  288. (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
  289. (when (and gud-tooltip-mode
  290. (eq gud-minor-mode 'gdbmi))
  291. (make-local-variable 'gdb-define-alist)
  292. (unless gdb-define-alist (gdb-create-define-alist))
  293. (add-hook 'after-save-hook 'gdb-create-define-alist nil t))
  294. (make-local-variable 'gud-keep-buffer))
  295. buf)))
  296. ;; ======================================================================
  297. ;; command definition
  298. ;; This macro is used below to define some basic debugger interface commands.
  299. ;; Of course you may use `gud-def' with any other debugger command, including
  300. ;; user defined ones.
  301. ;; A macro call like (gud-def FUNC CMD KEY DOC) expands to a form
  302. ;; which defines FUNC to send the command CMD to the debugger, gives
  303. ;; it the docstring DOC, and binds that function to KEY in the GUD
  304. ;; major mode. The function is also bound in the global keymap with the
  305. ;; GUD prefix.
  306. (defmacro gud-def (func cmd key &optional doc)
  307. "Define FUNC to be a command sending CMD and bound to KEY, with
  308. optional doc string DOC. Certain %-escapes in the string arguments
  309. are interpreted specially if present. These are:
  310. %f -- Name (without directory) of current source file.
  311. %F -- Name (without directory or extension) of current source file.
  312. %d -- Directory of current source file.
  313. %l -- Number of current source line.
  314. %e -- Text of the C lvalue or function-call expression surrounding point.
  315. %a -- Text of the hexadecimal address surrounding point.
  316. %p -- Prefix argument to the command (if any) as a number.
  317. %c -- Fully qualified class name derived from the expression
  318. surrounding point (jdb only).
  319. The `current' source file is the file of the current buffer (if
  320. we're in a C file) or the source file current at the last break or
  321. step (if we're in the GUD buffer).
  322. The `current' line is that of the current buffer (if we're in a
  323. source file) or the source line number at the last break or step (if
  324. we're in the GUD buffer)."
  325. `(progn
  326. (defalias ',func (lambda (arg)
  327. ,@(if doc (list doc))
  328. (interactive "p")
  329. (if (not gud-running)
  330. ,(if (stringp cmd)
  331. `(gud-call ,cmd arg)
  332. cmd))))
  333. ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
  334. ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
  335. ;; Where gud-display-frame should put the debugging arrow; a cons of
  336. ;; (filename . line-number). This is set by the marker-filter, which scans
  337. ;; the debugger's output for indications of the current program counter.
  338. (defvar gud-last-frame nil)
  339. ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
  340. ;; the last frame, even if it's been called before and gud-last-frame has
  341. ;; been set to nil.
  342. (defvar gud-last-last-frame nil)
  343. ;; All debugger-specific information is collected here.
  344. ;; Here's how it works, in case you ever need to add a debugger to the mode.
  345. ;;
  346. ;; Each entry must define the following at startup:
  347. ;;
  348. ;;<name>
  349. ;; comint-prompt-regexp
  350. ;; gud-<name>-massage-args
  351. ;; gud-<name>-marker-filter
  352. ;; gud-<name>-find-file
  353. ;;
  354. ;; The job of the massage-args method is to modify the given list of
  355. ;; debugger arguments before running the debugger.
  356. ;;
  357. ;; The job of the marker-filter method is to detect file/line markers in
  358. ;; strings and set the global gud-last-frame to indicate what display
  359. ;; action (if any) should be triggered by the marker. Note that only
  360. ;; whatever the method *returns* is displayed in the buffer; thus, you
  361. ;; can filter the debugger's output, interpreting some and passing on
  362. ;; the rest.
  363. ;;
  364. ;; The job of the find-file method is to visit and return the buffer indicated
  365. ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
  366. ;; something else.
  367. ;; ======================================================================
  368. ;; speedbar support functions and variables.
  369. (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
  370. (defvar gud-last-speedbar-stackframe nil
  371. "Description of the currently displayed GUD stack.
  372. The value t means that there is no stack, and we are in display-file mode.")
  373. (defvar gud-speedbar-key-map nil
  374. "Keymap used when in the buffers display mode.")
  375. (defun gud-speedbar-item-info ()
  376. "Display the data type of the watch expression element."
  377. (let ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list)))
  378. (if (nth 7 var)
  379. (speedbar-message "%s: %s" (nth 7 var) (nth 3 var))
  380. (speedbar-message "%s" (nth 3 var)))))
  381. (defun gud-install-speedbar-variables ()
  382. "Install those variables used by speedbar to enhance gud/gdb."
  383. (if gud-speedbar-key-map
  384. nil
  385. (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
  386. (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
  387. (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
  388. (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
  389. (define-key gud-speedbar-key-map " " 'speedbar-toggle-line-expansion)
  390. (define-key gud-speedbar-key-map "D" 'gdb-var-delete)
  391. (define-key gud-speedbar-key-map "p" 'gud-pp))
  392. (speedbar-add-expansion-list '("GUD" gud-speedbar-menu-items
  393. gud-speedbar-key-map
  394. gud-expansion-speedbar-buttons))
  395. (add-to-list
  396. 'speedbar-mode-functions-list
  397. '("GUD" (speedbar-item-info . gud-speedbar-item-info)
  398. (speedbar-line-directory . ignore))))
  399. (defvar gud-speedbar-menu-items
  400. '(["Jump to stack frame" speedbar-edit-line
  401. :visible (not (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  402. 'gdbmi))]
  403. ["Edit value" speedbar-edit-line
  404. :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  405. 'gdbmi)]
  406. ["Delete expression" gdb-var-delete
  407. :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  408. 'gdbmi)]
  409. ["Auto raise frame" gdb-speedbar-auto-raise
  410. :style toggle :selected gdb-speedbar-auto-raise
  411. :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  412. 'gdbmi)]
  413. ("Output Format"
  414. :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  415. 'gdbmi)
  416. ["Binary" (gdb-var-set-format "binary") t]
  417. ["Natural" (gdb-var-set-format "natural") t]
  418. ["Hexadecimal" (gdb-var-set-format "hexadecimal") t]))
  419. "Additional menu items to add to the speedbar frame.")
  420. ;; Make sure our special speedbar mode is loaded
  421. (if (featurep 'speedbar)
  422. (gud-install-speedbar-variables)
  423. (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
  424. (defun gud-expansion-speedbar-buttons (_directory _zero)
  425. "Wrapper for call to `speedbar-add-expansion-list'.
  426. DIRECTORY and ZERO are not used, but are required by the caller."
  427. (gud-speedbar-buttons gud-comint-buffer))
  428. (defun gud-speedbar-buttons (buffer)
  429. "Create a speedbar display based on the current state of GUD.
  430. If the GUD BUFFER is not running a supported debugger, then turn
  431. off the specialized speedbar mode. BUFFER is not used, but is
  432. required by the caller."
  433. (when (and gud-comint-buffer
  434. ;; gud-comint-buffer might be killed
  435. (buffer-name gud-comint-buffer))
  436. (let* ((minor-mode (with-current-buffer buffer gud-minor-mode))
  437. (window (get-buffer-window (current-buffer) 0))
  438. (start (window-start window))
  439. (p (window-point window)))
  440. (cond
  441. ((eq minor-mode 'gdbmi)
  442. (erase-buffer)
  443. (insert "Watch Expressions:\n")
  444. (let ((var-list gdb-var-list) parent)
  445. (while var-list
  446. (let* (char (depth 0) (start 0) (var (car var-list))
  447. (varnum (car var)) (expr (nth 1 var))
  448. (type (if (nth 3 var) (nth 3 var) " "))
  449. (value (nth 4 var)) (status (nth 5 var))
  450. (has-more (nth 6 var)))
  451. (put-text-property
  452. 0 (length expr) 'face font-lock-variable-name-face expr)
  453. (put-text-property
  454. 0 (length type) 'face font-lock-type-face type)
  455. (while (string-match "\\." varnum start)
  456. (setq depth (1+ depth)
  457. start (1+ (match-beginning 0))))
  458. (if (eq depth 0) (setq parent nil))
  459. (if (and (or (not has-more) (string-equal has-more "0"))
  460. (or (equal (nth 2 var) "0")
  461. (and (equal (nth 2 var) "1")
  462. (string-match "char \\*$" type)) ))
  463. (speedbar-make-tag-line
  464. 'bracket ?? nil nil
  465. (concat expr "\t" value)
  466. (if (or parent (eq status 'out-of-scope))
  467. nil 'gdb-edit-value)
  468. nil
  469. (if gdb-show-changed-values
  470. (or parent (case status
  471. (changed 'font-lock-warning-face)
  472. (out-of-scope 'shadow)
  473. (t t)))
  474. t)
  475. depth)
  476. (if (eq status 'out-of-scope) (setq parent 'shadow))
  477. (if (and (nth 1 var-list)
  478. (string-match (concat varnum "\\.")
  479. (car (nth 1 var-list))))
  480. (setq char ?-)
  481. (setq char ?+))
  482. (if (string-match "\\*$\\|\\*&$" type)
  483. (speedbar-make-tag-line
  484. 'bracket char
  485. 'gdb-speedbar-expand-node varnum
  486. (concat expr "\t" type "\t" value)
  487. (if (or parent (eq status 'out-of-scope))
  488. nil 'gdb-edit-value)
  489. nil
  490. (if gdb-show-changed-values
  491. (or parent (case status
  492. (changed 'font-lock-warning-face)
  493. (out-of-scope 'shadow)
  494. (t t)))
  495. t)
  496. depth)
  497. (speedbar-make-tag-line
  498. 'bracket char
  499. 'gdb-speedbar-expand-node varnum
  500. (concat expr "\t" type)
  501. nil nil
  502. (if (and (or parent status) gdb-show-changed-values)
  503. 'shadow t)
  504. depth))))
  505. (setq var-list (cdr var-list)))))
  506. (t (unless (and (save-excursion
  507. (goto-char (point-min))
  508. (looking-at "Current Stack:"))
  509. (equal gud-last-last-frame gud-last-speedbar-stackframe))
  510. (let ((gud-frame-list
  511. (cond ((eq minor-mode 'gdb)
  512. (gud-gdb-get-stackframe buffer))
  513. ;; Add more debuggers here!
  514. (t (speedbar-remove-localized-speedbar-support buffer)
  515. nil))))
  516. (erase-buffer)
  517. (if (not gud-frame-list)
  518. (insert "No Stack frames\n")
  519. (insert "Current Stack:\n"))
  520. (dolist (frame gud-frame-list)
  521. (insert (nth 1 frame) ":\n")
  522. (if (= (length frame) 2)
  523. (progn
  524. (speedbar-insert-button (car frame)
  525. 'speedbar-directory-face
  526. nil nil nil t))
  527. (speedbar-insert-button
  528. (car frame)
  529. 'speedbar-file-face
  530. 'speedbar-highlight-face
  531. (cond ((memq minor-mode '(gdbmi gdb))
  532. 'gud-gdb-goto-stackframe)
  533. (t (error "Should never be here")))
  534. frame t))))
  535. (setq gud-last-speedbar-stackframe gud-last-last-frame))))
  536. (set-window-start window start)
  537. (set-window-point window p))))
  538. ;; ======================================================================
  539. ;; gdb functions
  540. ;; History of argument lists passed to gdb.
  541. (defvar gud-gdb-history nil)
  542. (defcustom gud-gud-gdb-command-name "gdb --fullname"
  543. "Default command to run an executable under GDB in text command mode.
  544. The option \"--fullname\" must be included in this value."
  545. :type 'string
  546. :group 'gud)
  547. (defvar gud-gdb-marker-regexp
  548. ;; This used to use path-separator instead of ":";
  549. ;; however, we found that on both Windows 32 and MSDOS
  550. ;; a colon is correct here.
  551. (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
  552. "\\([0-9]*\\)" ":" ".*\n"))
  553. ;; There's no guarantee that Emacs will hand the filter the entire
  554. ;; marker at once; it could be broken up across several strings. We
  555. ;; might even receive a big chunk with several markers in it. If we
  556. ;; receive a chunk of text which looks like it might contain the
  557. ;; beginning of a marker, we save it here between calls to the
  558. ;; filter.
  559. (defvar gud-marker-acc "")
  560. (make-variable-buffer-local 'gud-marker-acc)
  561. (defun gud-gdb-marker-filter (string)
  562. (setq gud-marker-acc (concat gud-marker-acc string))
  563. (let ((output ""))
  564. ;; Process all the complete markers in this chunk.
  565. (while (string-match gud-gdb-marker-regexp gud-marker-acc)
  566. (setq
  567. ;; Extract the frame position from the marker.
  568. gud-last-frame (cons (match-string 1 gud-marker-acc)
  569. (string-to-number (match-string 2 gud-marker-acc)))
  570. ;; Append any text before the marker to the output we're going
  571. ;; to return - we don't include the marker in this text.
  572. output (concat output
  573. (substring gud-marker-acc 0 (match-beginning 0)))
  574. ;; Set the accumulator to the remaining text.
  575. gud-marker-acc (substring gud-marker-acc (match-end 0))))
  576. (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
  577. (setq
  578. ;; Append any text before the marker to the output we're going
  579. ;; to return - we don't include the marker in this text.
  580. output (concat output
  581. (substring gud-marker-acc 0 (match-beginning 0)))
  582. ;; Set the accumulator to the remaining text.
  583. gud-marker-acc (substring gud-marker-acc (match-end 0))))
  584. ;; Does the remaining text look like it might end with the
  585. ;; beginning of another marker? If it does, then keep it in
  586. ;; gud-marker-acc until we receive the rest of it. Since we
  587. ;; know the full marker regexp above failed, it's pretty simple to
  588. ;; test for marker starts.
  589. (if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
  590. (progn
  591. ;; Everything before the potential marker start can be output.
  592. (setq output (concat output (substring gud-marker-acc
  593. 0 (match-beginning 0))))
  594. ;; Everything after, we save, to combine with later input.
  595. (setq gud-marker-acc
  596. (substring gud-marker-acc (match-beginning 0))))
  597. (setq output (concat output gud-marker-acc)
  598. gud-marker-acc ""))
  599. output))
  600. (easy-mmode-defmap gud-minibuffer-local-map
  601. '(("\C-i" . comint-dynamic-complete-filename))
  602. "Keymap for minibuffer prompting of gud startup command."
  603. :inherit minibuffer-local-map)
  604. (defun gud-query-cmdline (minor-mode &optional init)
  605. (let* ((hist-sym (gud-symbol 'history nil minor-mode))
  606. (cmd-name (gud-val 'command-name minor-mode)))
  607. (unless (boundp hist-sym) (set hist-sym nil))
  608. (read-from-minibuffer
  609. (format "Run %s (like this): " minor-mode)
  610. (or (car-safe (symbol-value hist-sym))
  611. (concat (or cmd-name (symbol-name minor-mode))
  612. " "
  613. (or init
  614. (let ((file nil))
  615. (dolist (f (directory-files default-directory) file)
  616. (if (and (file-executable-p f)
  617. (not (file-directory-p f))
  618. (or (not file)
  619. (file-newer-than-file-p f file)))
  620. (setq file f)))))))
  621. gud-minibuffer-local-map nil
  622. hist-sym)))
  623. (defvar gdb-first-prompt t)
  624. (defvar gud-filter-pending-text nil
  625. "Non-nil means this is text that has been saved for later in `gud-filter'.")
  626. ;; If in gdb mode, gdb-mi is loaded.
  627. (declare-function gdb-restore-windows "gdb-mi" ())
  628. ;; The old gdb command (text command mode). The new one is in gdb-mi.el.
  629. ;;;###autoload
  630. (defun gud-gdb (command-line)
  631. "Run gdb on program FILE in buffer *gud-FILE*.
  632. The directory containing FILE becomes the initial working
  633. directory and source-file directory for your debugger."
  634. (interactive (list (gud-query-cmdline 'gud-gdb)))
  635. (when (and gud-comint-buffer
  636. (buffer-name gud-comint-buffer)
  637. (get-buffer-process gud-comint-buffer)
  638. (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdbmi)))
  639. (gdb-restore-windows)
  640. (error
  641. "Multiple debugging requires restarting in text command mode"))
  642. (gud-common-init command-line nil 'gud-gdb-marker-filter)
  643. (set (make-local-variable 'gud-minor-mode) 'gdb)
  644. (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
  645. (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
  646. "Set temporary breakpoint at current line.")
  647. (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
  648. (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
  649. (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
  650. (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
  651. (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
  652. (gud-def gud-cont "cont" "\C-r" "Continue with display.")
  653. (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
  654. (gud-def gud-jump
  655. (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
  656. "\C-j" "Set execution address to current line.")
  657. (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
  658. (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
  659. (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
  660. (gud-def gud-pstar "print* %e" nil
  661. "Evaluate C dereferenced pointer expression at point.")
  662. ;; For debugging Emacs only.
  663. (gud-def gud-pv "pv %e" "\C-v" "Print the value of the lisp variable.")
  664. (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
  665. (gud-def gud-run "run" nil "Run the program.")
  666. (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
  667. nil 'local)
  668. (set (make-local-variable 'gud-gdb-completion-function) 'gud-gdb-completions)
  669. (local-set-key "\C-i" 'completion-at-point)
  670. (setq comint-prompt-regexp "^(.*gdb[+]?) *")
  671. (setq paragraph-start comint-prompt-regexp)
  672. (setq gdb-first-prompt t)
  673. (setq gud-running nil)
  674. (setq gud-filter-pending-text nil)
  675. (run-hooks 'gud-gdb-mode-hook))
  676. ;; One of the nice features of GDB is its impressive support for
  677. ;; context-sensitive command completion. We preserve that feature
  678. ;; in the GUD buffer by using a GDB command designed just for Emacs.
  679. (defvar gud-gdb-completion-function nil
  680. "Completion function for GDB commands.
  681. It receives two arguments: COMMAND, the prefix for which we seek
  682. completion; and CONTEXT, the text before COMMAND on the line.
  683. It should return a list of completion strings.")
  684. ;; The completion process filter indicates when it is finished.
  685. (defvar gud-gdb-fetch-lines-in-progress)
  686. ;; Since output may arrive in fragments we accumulate partials strings here.
  687. (defvar gud-gdb-fetch-lines-string)
  688. ;; We need to know how much of the completion to chop off.
  689. (defvar gud-gdb-fetch-lines-break)
  690. ;; The completion list is constructed by the process filter.
  691. (defvar gud-gdb-fetched-lines)
  692. (defun gud-gdb-completions (context command)
  693. "Completion table for GDB commands.
  694. COMMAND is the prefix for which we seek completion.
  695. CONTEXT is the text before COMMAND on the line."
  696. (let* ((start (- (point) (field-beginning)))
  697. (complete-list
  698. (gud-gdb-run-command-fetch-lines (concat "complete " context command)
  699. (current-buffer)
  700. ;; From string-match above.
  701. (length context))))
  702. ;; `gud-gdb-run-command-fetch-lines' has some nasty side-effects on the
  703. ;; buffer (via `gud-delete-prompt-marker'): it removes the prompt and then
  704. ;; re-adds it later, thus messing up markers and overlays along the way.
  705. ;; This is a problem for completion-in-region which uses an overlay to
  706. ;; create a field.
  707. ;; So we restore completion-in-region's field if needed.
  708. ;; FIXME: change gud-gdb-run-command-fetch-lines so it doesn't modify the
  709. ;; buffer at all.
  710. (when (/= start (- (point) (field-beginning)))
  711. (dolist (ol (overlays-at (1- (point))))
  712. (when (eq (overlay-get ol 'field) 'completion)
  713. (move-overlay ol (- (point) start) (overlay-end ol)))))
  714. ;; Protect against old versions of GDB.
  715. (and complete-list
  716. (string-match "^Undefined command: \"complete\"" (car complete-list))
  717. (error "This version of GDB doesn't support the `complete' command"))
  718. (gud-gdb-completions-1 complete-list)))
  719. ;; This function is also used by `gud-gdbmi-completions'.
  720. (defun gud-gdb-completions-1 (complete-list)
  721. ;; Sort the list like readline.
  722. (setq complete-list (sort complete-list (function string-lessp)))
  723. ;; Remove duplicates.
  724. (let ((first complete-list)
  725. (second (cdr complete-list)))
  726. (while second
  727. (if (string-equal (car first) (car second))
  728. (setcdr first (setq second (cdr second)))
  729. (setq first second
  730. second (cdr second)))))
  731. ;; Add a trailing single quote if there is a unique completion
  732. ;; and it contains an odd number of unquoted single quotes.
  733. (and (= (length complete-list) 1)
  734. (let ((str (car complete-list))
  735. (pos 0)
  736. (count 0))
  737. (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
  738. (setq count (1+ count)
  739. pos (match-end 0)))
  740. (and (= (mod count 2) 1)
  741. (setq complete-list (list (concat str "'"))))))
  742. complete-list)
  743. (defun gud-gdb-completion-at-point ()
  744. "Return the data to complete the GDB command before point."
  745. (let ((end (point))
  746. (start
  747. (save-excursion
  748. (skip-chars-backward "^ " (comint-line-beginning-position))
  749. (point))))
  750. (list start end
  751. (completion-table-dynamic
  752. (apply-partially gud-gdb-completion-function
  753. (buffer-substring (comint-line-beginning-position)
  754. start))))))
  755. ;; (defun gud-gdb-complete-command ()
  756. ;; "Perform completion on the GDB command preceding point.
  757. ;; This is implemented using the GDB `complete' command which isn't
  758. ;; available with older versions of GDB."
  759. ;; (interactive)
  760. ;; (apply #'completion-in-region (gud-gdb-completion-at-point)))
  761. ;; The completion process filter is installed temporarily to slurp the
  762. ;; output of GDB up to the next prompt and build the completion list.
  763. (defun gud-gdb-fetch-lines-filter (string)
  764. "Filter used to read the list of lines output by a command.
  765. STRING is the output to filter.
  766. It is passed through `gud-gdb-marker-filter' before we look at it."
  767. (setq string (gud-gdb-marker-filter string))
  768. (setq string (concat gud-gdb-fetch-lines-string string))
  769. (while (string-match "\n" string)
  770. (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
  771. gud-gdb-fetched-lines)
  772. (setq string (substring string (match-end 0))))
  773. (if (string-match comint-prompt-regexp string)
  774. (progn
  775. (setq gud-gdb-fetch-lines-in-progress nil)
  776. string)
  777. (progn
  778. (setq gud-gdb-fetch-lines-string string)
  779. "")))
  780. ;; gdb speedbar functions
  781. (defun gud-gdb-goto-stackframe (_text token _indent)
  782. "Goto the stackframe described by TEXT, TOKEN, and INDENT."
  783. (speedbar-with-attached-buffer
  784. (gud-basic-call (concat "server frame " (nth 1 token)))
  785. (sit-for 1)))
  786. (defvar gud-gdb-fetched-stack-frame nil
  787. "Stack frames we are fetching from GDB.")
  788. (defun gud-gdb-get-stackframe (buffer)
  789. "Extract the current stack frame out of the GUD GDB BUFFER."
  790. (let ((newlst nil)
  791. (fetched-stack-frame-list
  792. (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
  793. (if (and (car fetched-stack-frame-list)
  794. (string-match "No stack" (car fetched-stack-frame-list)))
  795. ;; Go into some other mode???
  796. nil
  797. (dolist (e fetched-stack-frame-list)
  798. (let ((name nil) (num nil))
  799. (if (not (or
  800. (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
  801. (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
  802. (if (not (string-match
  803. "at \\([-0-9a-zA-Z_/.]+\\):\\([0-9]+\\)$" e))
  804. nil
  805. (setcar newlst
  806. (list (nth 0 (car newlst))
  807. (nth 1 (car newlst))
  808. (match-string 1 e)
  809. (match-string 2 e))))
  810. (setq num (match-string 1 e)
  811. name (match-string 2 e))
  812. (setq newlst
  813. (cons
  814. (if (string-match
  815. "at \\([-0-9a-zA-Z_/.]+\\):\\([0-9]+\\)$" e)
  816. (list name num (match-string 1 e)
  817. (match-string 2 e))
  818. (list name num))
  819. newlst)))))
  820. (nreverse newlst))))
  821. ;(defun gud-gdb-selected-frame-info (buffer)
  822. ; "Learn GDB information for the currently selected stack frame in BUFFER."
  823. ; )
  824. (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
  825. "Run COMMAND, and return the list of lines it outputs.
  826. BUFFER is the current buffer which may be the GUD buffer in which to run.
  827. SKIP is the number of chars to skip on each line, it defaults to 0."
  828. (with-current-buffer gud-comint-buffer
  829. (unless (and (eq gud-comint-buffer buffer)
  830. (save-excursion
  831. (goto-char (point-max))
  832. (forward-line 0)
  833. (not (looking-at comint-prompt-regexp))))
  834. (let ((gud-gdb-fetch-lines-in-progress t)
  835. (gud-gdb-fetched-lines nil)
  836. (gud-gdb-fetch-lines-string nil)
  837. (gud-gdb-fetch-lines-break (or skip 0))
  838. (gud-marker-filter #'gud-gdb-fetch-lines-filter))
  839. ;; Issue the command to GDB.
  840. (gud-basic-call command)
  841. ;; Slurp the output.
  842. (while gud-gdb-fetch-lines-in-progress
  843. (accept-process-output (get-buffer-process gud-comint-buffer)))
  844. (nreverse gud-gdb-fetched-lines)))))
  845. ;; ======================================================================
  846. ;; sdb functions
  847. ;; History of argument lists passed to sdb.
  848. (defvar gud-sdb-history nil)
  849. (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
  850. "If nil, we're on a System V Release 4 and don't need the tags hack.")
  851. (defvar gud-sdb-lastfile nil)
  852. (defun gud-sdb-marker-filter (string)
  853. (setq gud-marker-acc
  854. (if gud-marker-acc (concat gud-marker-acc string) string))
  855. (let (start)
  856. ;; Process all complete markers in this chunk
  857. (while
  858. (cond
  859. ;; System V Release 3.2 uses this format
  860. ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
  861. gud-marker-acc start)
  862. (setq gud-last-frame
  863. (cons (match-string 3 gud-marker-acc)
  864. (string-to-number (match-string 4 gud-marker-acc)))))
  865. ;; System V Release 4.0 quite often clumps two lines together
  866. ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
  867. gud-marker-acc start)
  868. (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
  869. (setq gud-last-frame
  870. (cons gud-sdb-lastfile
  871. (string-to-number (match-string 3 gud-marker-acc)))))
  872. ;; System V Release 4.0
  873. ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
  874. gud-marker-acc start)
  875. (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
  876. ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
  877. gud-marker-acc start))
  878. (setq gud-last-frame
  879. (cons gud-sdb-lastfile
  880. (string-to-number (match-string 1 gud-marker-acc)))))
  881. (t
  882. (setq gud-sdb-lastfile nil)))
  883. (setq start (match-end 0)))
  884. ;; Search for the last incomplete line in this chunk
  885. (while (string-match "\n" gud-marker-acc start)
  886. (setq start (match-end 0)))
  887. ;; If we have an incomplete line, store it in gud-marker-acc.
  888. (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
  889. string)
  890. (defun gud-sdb-find-file (f)
  891. (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
  892. ;;;###autoload
  893. (defun sdb (command-line)
  894. "Run sdb on program FILE in buffer *gud-FILE*.
  895. The directory containing FILE becomes the initial working directory
  896. and source-file directory for your debugger."
  897. (interactive (list (gud-query-cmdline 'sdb)))
  898. (if gud-sdb-needs-tags (require 'etags))
  899. (if (and gud-sdb-needs-tags
  900. (not (and (boundp 'tags-file-name)
  901. (stringp tags-file-name)
  902. (file-exists-p tags-file-name))))
  903. (error "The sdb support requires a valid tags table to work"))
  904. (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
  905. (set (make-local-variable 'gud-minor-mode) 'sdb)
  906. (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
  907. (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
  908. (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
  909. (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
  910. (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
  911. (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
  912. (gud-def gud-cont "c" "\C-r" "Continue with display.")
  913. (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
  914. (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
  915. (setq paragraph-start comint-prompt-regexp)
  916. (run-hooks 'sdb-mode-hook)
  917. )
  918. ;; ======================================================================
  919. ;; dbx functions
  920. ;; History of argument lists passed to dbx.
  921. (defvar gud-dbx-history nil)
  922. (defcustom gud-dbx-directories nil
  923. "*A list of directories that dbx should search for source code.
  924. If nil, only source files in the program directory
  925. will be known to dbx.
  926. The file names should be absolute, or relative to the directory
  927. containing the executable being debugged."
  928. :type '(choice (const :tag "Current Directory" nil)
  929. (repeat :value ("")
  930. directory))
  931. :group 'gud)
  932. (defun gud-dbx-massage-args (_file args)
  933. (nconc (let ((directories gud-dbx-directories)
  934. (result nil))
  935. (while directories
  936. (setq result (cons (car directories) (cons "-I" result)))
  937. (setq directories (cdr directories)))
  938. (nreverse result))
  939. args))
  940. (defun gud-dbx-marker-filter (string)
  941. (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
  942. (let (start)
  943. ;; Process all complete markers in this chunk.
  944. (while (or (string-match
  945. "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
  946. gud-marker-acc start)
  947. (string-match
  948. "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
  949. gud-marker-acc start))
  950. (setq gud-last-frame
  951. (cons (match-string 2 gud-marker-acc)
  952. (string-to-number (match-string 1 gud-marker-acc)))
  953. start (match-end 0)))
  954. ;; Search for the last incomplete line in this chunk
  955. (while (string-match "\n" gud-marker-acc start)
  956. (setq start (match-end 0)))
  957. ;; If the incomplete line APPEARS to begin with another marker, keep it
  958. ;; in the accumulator. Otherwise, clear the accumulator to avoid an
  959. ;; unnecessary concat during the next call.
  960. (setq gud-marker-acc
  961. (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
  962. (substring gud-marker-acc (match-beginning 0))
  963. nil)))
  964. string)
  965. ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
  966. ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
  967. (defvar gud-mips-p
  968. (or (string-match "^mips-[^-]*-ultrix" system-configuration)
  969. ;; We haven't tested gud on this system:
  970. (string-match "^mips-[^-]*-riscos" system-configuration)
  971. ;; It's documented on OSF/1.3
  972. (string-match "^mips-[^-]*-osf1" system-configuration)
  973. (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
  974. "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
  975. (defvar gud-dbx-command-name
  976. (concat "dbx" (if gud-mips-p " -emacs")))
  977. ;; This is just like the gdb one except for the regexps since we need to cope
  978. ;; with an optional breakpoint number in [] before the ^Z^Z
  979. (defun gud-mipsdbx-marker-filter (string)
  980. (setq gud-marker-acc (concat gud-marker-acc string))
  981. (let ((output ""))
  982. ;; Process all the complete markers in this chunk.
  983. (while (string-match
  984. ;; This is like th gdb marker but with an optional
  985. ;; leading break point number like `[1] '
  986. "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
  987. gud-marker-acc)
  988. (setq
  989. ;; Extract the frame position from the marker.
  990. gud-last-frame
  991. (cons (match-string 1 gud-marker-acc)
  992. (string-to-number (match-string 2 gud-marker-acc)))
  993. ;; Append any text before the marker to the output we're going
  994. ;; to return - we don't include the marker in this text.
  995. output (concat output
  996. (substring gud-marker-acc 0 (match-beginning 0)))
  997. ;; Set the accumulator to the remaining text.
  998. gud-marker-acc (substring gud-marker-acc (match-end 0))))
  999. ;; Does the remaining text look like it might end with the
  1000. ;; beginning of another marker? If it does, then keep it in
  1001. ;; gud-marker-acc until we receive the rest of it. Since we
  1002. ;; know the full marker regexp above failed, it's pretty simple to
  1003. ;; test for marker starts.
  1004. (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
  1005. (progn
  1006. ;; Everything before the potential marker start can be output.
  1007. (setq output (concat output (substring gud-marker-acc
  1008. 0 (match-beginning 0))))
  1009. ;; Everything after, we save, to combine with later input.
  1010. (setq gud-marker-acc
  1011. (substring gud-marker-acc (match-beginning 0))))
  1012. (setq output (concat output gud-marker-acc)
  1013. gud-marker-acc ""))
  1014. output))
  1015. ;; The dbx in IRIX is a pain. It doesn't print the file name when
  1016. ;; stopping at a breakpoint (but you do get it from the `up' and
  1017. ;; `down' commands...). The only way to extract the information seems
  1018. ;; to be with a `file' command, although the current line number is
  1019. ;; available in $curline. Thus we have to look for output which
  1020. ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
  1021. ;; to output the information we want with a combination of the
  1022. ;; `printf' and `file' commands as a pseudo marker which we can
  1023. ;; recognize next time through the marker-filter. This would be like
  1024. ;; the gdb marker but you can't get the file name without a newline...
  1025. ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
  1026. ;; number rather than a line number etc. Maybe this could be made to
  1027. ;; work by listing all the breakpoints and picking the one(s) with the
  1028. ;; correct line number, but life's too short.
  1029. ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
  1030. (defvar gud-irix-p
  1031. (and (string-match "^mips-[^-]*-irix" system-configuration)
  1032. (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
  1033. "Non-nil to assume the interface appropriate for IRIX dbx.
  1034. This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
  1035. a better solution in 6.1 upwards.")
  1036. (defvar gud-dbx-use-stopformat-p
  1037. (string-match "irix[6-9]\\.[1-9]" system-configuration)
  1038. "Non-nil to use the dbx feature present at least from Irix 6.1
  1039. whereby $stopformat=1 produces an output format compatible with
  1040. `gud-dbx-marker-filter'.")
  1041. ;; [Irix dbx seems to be a moving target. The dbx output changed
  1042. ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
  1043. ;; the output from `up' is no longer spotted by gud (and it's probably
  1044. ;; not distinctive enough to try to match it -- use C-<, C->
  1045. ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
  1046. ;; `long long'(why?!), so the printf stuff needed changing. The line
  1047. ;; number was cast to `long' as a compromise between the new `long
  1048. ;; long' and the original `int'. This is reported not to work in 6.2,
  1049. ;; so it's changed back to int -- don't make your sources too long.
  1050. ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
  1051. ;; whereby `set $stopformat=1' reportedly produces output compatible
  1052. ;; with `gud-dbx-marker-filter', which we prefer.
  1053. ;; The process filter is also somewhat
  1054. ;; unreliable, sometimes not spotting the markers; I don't know
  1055. ;; whether there's anything that can be done about that. It would be
  1056. ;; much better if SGI could be persuaded to (re?)instate the MIPS
  1057. ;; -emacs flag for gdb-like output (which ought to be possible as most
  1058. ;; of the communication I've had over it has been from sgi.com).]
  1059. ;; this filter is influenced by the xdb one rather than the gdb one
  1060. (defun gud-irixdbx-marker-filter (string)
  1061. (let (result (case-fold-search nil))
  1062. (if (or (string-match comint-prompt-regexp string)
  1063. (string-match ".*\012" string))
  1064. (setq result (concat gud-marker-acc string)
  1065. gud-marker-acc "")
  1066. (setq gud-marker-acc (concat gud-marker-acc string)))
  1067. (if result
  1068. (cond
  1069. ;; look for breakpoint or signal indication e.g.:
  1070. ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
  1071. ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
  1072. ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
  1073. ((string-match
  1074. "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
  1075. result)
  1076. ;; prod dbx into printing out the line number and file
  1077. ;; name in a form we can grok as below
  1078. (process-send-string (get-buffer-process gud-comint-buffer)
  1079. "printf \"\032\032%1d:\",(int)$curline;file\n"))
  1080. ;; look for result of, say, "up" e.g.:
  1081. ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
  1082. ;; (this will also catch one of the lines printed by "where")
  1083. ((string-match
  1084. "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
  1085. result)
  1086. (let ((file (match-string 1 result)))
  1087. (if (file-exists-p file)
  1088. (setq gud-last-frame
  1089. (cons (match-string 1 result)
  1090. (string-to-number (match-string 2 result))))))
  1091. result)
  1092. ((string-match ; kluged-up marker as above
  1093. "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
  1094. (let ((file (gud-file-name (match-string 2 result))))
  1095. (if (and file (file-exists-p file))
  1096. (setq gud-last-frame
  1097. (cons file
  1098. (string-to-number (match-string 1 result))))))
  1099. (setq result (substring result 0 (match-beginning 0))))))
  1100. (or result "")))
  1101. ;; There are a couple of differences between DG's dbx output and normal
  1102. ;; dbx output which make it nontrivial to integrate this into the
  1103. ;; standard dbx-marker-filter (mainly, there are a different number of
  1104. ;; backreferences). The markers look like:
  1105. ;;
  1106. ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
  1107. ;;
  1108. ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
  1109. ;; number), and
  1110. ;;
  1111. ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
  1112. ;;
  1113. ;; from signals and
  1114. ;;
  1115. ;; Frame 21, line 974, routine command_loop(), file keyboard.c
  1116. ;;
  1117. ;; from up/down/where.
  1118. (defun gud-dguxdbx-marker-filter (string)
  1119. (setq gud-marker-acc (if gud-marker-acc
  1120. (concat gud-marker-acc string)
  1121. string))
  1122. (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
  1123. " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
  1124. start)
  1125. ;; Process all complete markers in this chunk.
  1126. (while (string-match re gud-marker-acc start)
  1127. (setq gud-last-frame
  1128. (cons (match-string 4 gud-marker-acc)
  1129. (string-to-number (match-string 3 gud-marker-acc)))
  1130. start (match-end 0)))
  1131. ;; Search for the last incomplete line in this chunk
  1132. (while (string-match "\n" gud-marker-acc start)
  1133. (setq start (match-end 0)))
  1134. ;; If the incomplete line APPEARS to begin with another marker, keep it
  1135. ;; in the accumulator. Otherwise, clear the accumulator to avoid an
  1136. ;; unnecessary concat during the next call.
  1137. (setq gud-marker-acc
  1138. (if (string-match "Stopped\\|Frame" gud-marker-acc start)
  1139. (substring gud-marker-acc (match-beginning 0))
  1140. nil)))
  1141. string)
  1142. ;;;###autoload
  1143. (defun dbx (command-line)
  1144. "Run dbx on program FILE in buffer *gud-FILE*.
  1145. The directory containing FILE becomes the initial working directory
  1146. and source-file directory for your debugger."
  1147. (interactive (list (gud-query-cmdline 'dbx)))
  1148. (cond
  1149. (gud-mips-p
  1150. (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
  1151. (gud-irix-p
  1152. (gud-common-init command-line 'gud-dbx-massage-args
  1153. 'gud-irixdbx-marker-filter))
  1154. (t
  1155. (gud-common-init command-line 'gud-dbx-massage-args
  1156. 'gud-dbx-marker-filter)))
  1157. (set (make-local-variable 'gud-minor-mode) 'dbx)
  1158. (cond
  1159. (gud-mips-p
  1160. (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
  1161. (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
  1162. (gud-def gud-break "stop at \"%f\":%l"
  1163. "\C-b" "Set breakpoint at current line.")
  1164. (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
  1165. (gud-irix-p
  1166. (gud-def gud-break "stop at \"%d%f\":%l"
  1167. "\C-b" "Set breakpoint at current line.")
  1168. (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
  1169. (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
  1170. "<" "Up (numeric arg) stack frames.")
  1171. (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
  1172. ">" "Down (numeric arg) stack frames.")
  1173. ;; Make dbx give out the source location info that we need.
  1174. (process-send-string (get-buffer-process gud-comint-buffer)
  1175. "printf \"\032\032%1d:\",(int)$curline;file\n"))
  1176. (t
  1177. (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
  1178. (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
  1179. (gud-def gud-break "file \"%d%f\"\nstop at %l"
  1180. "\C-b" "Set breakpoint at current line.")
  1181. (if gud-dbx-use-stopformat-p
  1182. (process-send-string (get-buffer-process gud-comint-buffer)
  1183. "set $stopformat=1\n"))))
  1184. (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
  1185. (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
  1186. (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
  1187. (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
  1188. (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
  1189. (gud-def gud-cont "cont" "\C-r" "Continue with display.")
  1190. (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
  1191. (gud-def gud-run "run" nil "Run the program.")
  1192. (setq comint-prompt-regexp "^[^)\n]*dbx) *")
  1193. (setq paragraph-start comint-prompt-regexp)
  1194. (run-hooks 'dbx-mode-hook)
  1195. )
  1196. ;; ======================================================================
  1197. ;; xdb (HP PARISC debugger) functions
  1198. ;; History of argument lists passed to xdb.
  1199. (defvar gud-xdb-history nil)
  1200. (defcustom gud-xdb-directories nil
  1201. "*A list of directories that xdb should search for source code.
  1202. If nil, only source files in the program directory
  1203. will be known to xdb.
  1204. The file names should be absolute, or relative to the directory
  1205. containing the executable being debugged."
  1206. :type '(choice (const :tag "Current Directory" nil)
  1207. (repeat :value ("")
  1208. directory))
  1209. :group 'gud)
  1210. (defun gud-xdb-massage-args (_file args)
  1211. (nconc (let ((directories gud-xdb-directories)
  1212. (result nil))
  1213. (while directories
  1214. (setq result (cons (car directories) (cons "-d" result)))
  1215. (setq directories (cdr directories)))
  1216. (nreverse result))
  1217. args))
  1218. ;; xdb does not print the lines all at once, so we have to accumulate them
  1219. (defun gud-xdb-marker-filter (string)
  1220. (let (result)
  1221. (if (or (string-match comint-prompt-regexp string)
  1222. (string-match ".*\012" string))
  1223. (setq result (concat gud-marker-acc string)
  1224. gud-marker-acc "")
  1225. (setq gud-marker-acc (concat gud-marker-acc string)))
  1226. (if result
  1227. (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
  1228. result)
  1229. (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
  1230. result))
  1231. (let ((line (string-to-number (match-string 2 result)))
  1232. (file (gud-file-name (match-string 1 result))))
  1233. (if file
  1234. (setq gud-last-frame (cons file line))))))
  1235. (or result "")))
  1236. ;;;###autoload
  1237. (defun xdb (command-line)
  1238. "Run xdb on program FILE in buffer *gud-FILE*.
  1239. The directory containing FILE becomes the initial working directory
  1240. and source-file directory for your debugger.
  1241. You can set the variable `gud-xdb-directories' to a list of program source
  1242. directories if your program contains sources from more than one directory."
  1243. (interactive (list (gud-query-cmdline 'xdb)))
  1244. (gud-common-init command-line 'gud-xdb-massage-args
  1245. 'gud-xdb-marker-filter)
  1246. (set (make-local-variable 'gud-minor-mode) 'xdb)
  1247. (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
  1248. (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
  1249. "Set temporary breakpoint at current line.")
  1250. (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
  1251. (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
  1252. (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
  1253. (gud-def gud-cont "c" "\C-r" "Continue with display.")
  1254. (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
  1255. (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
  1256. (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
  1257. (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
  1258. (setq comint-prompt-regexp "^>")
  1259. (setq paragraph-start comint-prompt-regexp)
  1260. (run-hooks 'xdb-mode-hook))
  1261. ;; ======================================================================
  1262. ;; perldb functions
  1263. ;; History of argument lists passed to perldb.
  1264. (defvar gud-perldb-history nil)
  1265. (defun gud-perldb-massage-args (_file args)
  1266. "Convert a command line as would be typed normally to run perldb
  1267. into one that invokes an Emacs-enabled debugging session.
  1268. \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
  1269. ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
  1270. ;; arguments ?
  1271. (let* ((new-args nil)
  1272. (seen-e nil)
  1273. (shift (lambda () (push (pop args) new-args))))
  1274. ;; Pass all switches and -e scripts through.
  1275. (while (and args
  1276. (string-match "^-" (car args))
  1277. (not (equal "-" (car args)))
  1278. (not (equal "--" (car args))))
  1279. (when (equal "-e" (car args))
  1280. ;; -e goes with the next arg, so shift one extra.
  1281. (or (funcall shift)
  1282. ;; -e as the last arg is an error in Perl.
  1283. (error "No code specified for -e"))
  1284. (setq seen-e t))
  1285. (funcall shift))
  1286. (unless seen-e
  1287. (if (or (not args)
  1288. (string-match "^-" (car args)))
  1289. (error "Can't use stdin as the script to debug"))
  1290. ;; This is the program name.
  1291. (funcall shift))
  1292. ;; If -e specified, make sure there is a -- so -emacs is not taken
  1293. ;; as -e macs.
  1294. (if (and args (equal "--" (car args)))
  1295. (funcall shift)
  1296. (and seen-e (push "--" new-args)))
  1297. (push "-emacs" new-args)
  1298. (while args
  1299. (funcall shift))
  1300. (nreverse new-args)))
  1301. ;; There's no guarantee that Emacs will hand the filter the entire
  1302. ;; marker at once; it could be broken up across several strings. We
  1303. ;; might even receive a big chunk with several markers in it. If we
  1304. ;; receive a chunk of text which looks like it might contain the
  1305. ;; beginning of a marker, we save it here between calls to the
  1306. ;; filter.
  1307. (defun gud-perldb-marker-filter (string)
  1308. (setq gud-marker-acc (concat gud-marker-acc string))
  1309. (let ((output ""))
  1310. ;; Process all the complete markers in this chunk.
  1311. (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
  1312. gud-marker-acc)
  1313. (setq
  1314. ;; Extract the frame position from the marker.
  1315. gud-last-frame
  1316. (cons (match-string 1 gud-marker-acc)
  1317. (string-to-number (match-string 3 gud-marker-acc)))
  1318. ;; Append any text before the marker to the output we're going
  1319. ;; to return - we don't include the marker in this text.
  1320. output (concat output
  1321. (substring gud-marker-acc 0 (match-beginning 0)))
  1322. ;; Set the accumulator to the remaining text.
  1323. gud-marker-acc (substring gud-marker-acc (match-end 0))))
  1324. ;; Does the remaining text look like it might end with the
  1325. ;; beginning of another marker? If it does, then keep it in
  1326. ;; gud-marker-acc until we receive the rest of it. Since we
  1327. ;; know the full marker regexp above failed, it's pretty simple to
  1328. ;; test for marker starts.
  1329. (if (string-match "\032.*\\'" gud-marker-acc)
  1330. (progn
  1331. ;; Everything before the potential marker start can be output.
  1332. (setq output (concat output (substring gud-marker-acc
  1333. 0 (match-beginning 0))))
  1334. ;; Everything after, we save, to combine with later input.
  1335. (setq gud-marker-acc
  1336. (substring gud-marker-acc (match-beginning 0))))
  1337. (setq output (concat output gud-marker-acc)
  1338. gud-marker-acc ""))
  1339. output))
  1340. (defcustom gud-perldb-command-name "perl -d"
  1341. "Default command to execute a Perl script under debugger."
  1342. :type 'string
  1343. :group 'gud)
  1344. ;;;###autoload
  1345. (defun perldb (command-line)
  1346. "Run perldb on program FILE in buffer *gud-FILE*.
  1347. The directory containing FILE becomes the initial working directory
  1348. and source-file directory for your debugger."
  1349. (interactive
  1350. (list (gud-query-cmdline 'perldb
  1351. (concat (or (buffer-file-name) "-e 0") " "))))
  1352. (gud-common-init command-line 'gud-perldb-massage-args
  1353. 'gud-perldb-marker-filter)
  1354. (set (make-local-variable 'gud-minor-mode) 'perldb)
  1355. (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
  1356. (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
  1357. (gud-def gud-step "s" "\C-s" "Step one source line with display.")
  1358. (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
  1359. (gud-def gud-cont "c" "\C-r" "Continue with display.")
  1360. ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
  1361. ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
  1362. ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
  1363. (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
  1364. (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
  1365. (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
  1366. (setq paragraph-start comint-prompt-regexp)
  1367. (run-hooks 'perldb-mode-hook))
  1368. ;; ======================================================================
  1369. ;; pdb (Python debugger) functions
  1370. ;; History of argument lists passed to pdb.
  1371. (defvar gud-pdb-history nil)
  1372. ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
  1373. ;; Either file or function name may be omitted: "> <string>(0)?()"
  1374. (defvar gud-pdb-marker-regexp
  1375. "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\|<module>\\)()\\(->[^\n\r]*\\)?[\n\r]")
  1376. (defvar gud-pdb-marker-regexp-file-group 1)
  1377. (defvar gud-pdb-marker-regexp-line-group 2)
  1378. (defvar gud-pdb-marker-regexp-fnname-group 3)
  1379. (defvar gud-pdb-marker-regexp-start "^> ")
  1380. ;; There's no guarantee that Emacs will hand the filter the entire
  1381. ;; marker at once; it could be broken up across several strings. We
  1382. ;; might even receive a big chunk with several markers in it. If we
  1383. ;; receive a chunk of text which looks like it might contain the
  1384. ;; beginning of a marker, we save it here between calls to the
  1385. ;; filter.
  1386. (defun gud-pdb-marker-filter (string)
  1387. (setq gud-marker-acc (concat gud-marker-acc string))
  1388. (let ((output ""))
  1389. ;; Process all the complete markers in this chunk.
  1390. (while (string-match gud-pdb-marker-regexp gud-marker-acc)
  1391. (setq
  1392. ;; Extract the frame position from the marker.
  1393. gud-last-frame
  1394. (let ((file (match-string gud-pdb-marker-regexp-file-group
  1395. gud-marker-acc))
  1396. (line (string-to-number
  1397. (match-string gud-pdb-marker-regexp-line-group
  1398. gud-marker-acc))))
  1399. (if (string-equal file "<string>")
  1400. gud-last-frame
  1401. (cons file line)))
  1402. ;; Output everything instead of the below
  1403. output (concat output (substring gud-marker-acc 0 (match-end 0)))
  1404. ;; ;; Append any text before the marker to the output we're going
  1405. ;; ;; to return - we don't include the marker in this text.
  1406. ;; output (concat output
  1407. ;; (substring gud-marker-acc 0 (match-beginning 0)))
  1408. ;; Set the accumulator to the remaining text.
  1409. gud-marker-acc (substring gud-marker-acc (match-end 0))))
  1410. ;; Does the remaining text look like it might end with the
  1411. ;; beginning of another marker? If it does, then keep it in
  1412. ;; gud-marker-acc until we receive the rest of it. Since we
  1413. ;; know the full marker regexp above failed, it's pretty simple to
  1414. ;; test for marker starts.
  1415. (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
  1416. (progn
  1417. ;; Everything before the potential marker start can be output.
  1418. (setq output (concat output (substring gud-marker-acc
  1419. 0 (match-beginning 0))))
  1420. ;; Everything after, we save, to combine with later input.
  1421. (setq gud-marker-acc
  1422. (substring gud-marker-acc (match-beginning 0))))
  1423. (setq output (concat output gud-marker-acc)
  1424. gud-marker-acc ""))
  1425. output))
  1426. (defcustom gud-pdb-command-name "pdb"
  1427. "File name for executing the Python debugger.
  1428. This should be an executable on your path, or an absolute file name."
  1429. :type 'string
  1430. :group 'gud)
  1431. ;;;###autoload
  1432. (defun pdb (command-line)
  1433. "Run pdb on program FILE in buffer `*gud-FILE*'.
  1434. The directory containing FILE becomes the initial working directory
  1435. and source-file directory for your debugger."
  1436. (interactive
  1437. (list (gud-query-cmdline 'pdb)))
  1438. (gud-common-init command-line nil 'gud-pdb-marker-filter)
  1439. (set (make-local-variable 'gud-minor-mode) 'pdb)
  1440. (gud-def gud-break "break %d%f:%l" "\C-b" "Set breakpoint at current line.")
  1441. (gud-def gud-remove "clear %d%f:%l" "\C-d" "Remove breakpoint at current line")
  1442. (gud-def gud-step "step" "\C-s" "Step one source line with display.")
  1443. (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
  1444. (gud-def gud-cont "continue" "\C-r" "Continue with display.")
  1445. (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
  1446. (gud-def gud-up "up" "<" "Up one stack frame.")
  1447. (gud-def gud-down "down" ">" "Down one stack frame.")
  1448. (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
  1449. ;; Is this right?
  1450. (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
  1451. ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
  1452. (setq comint-prompt-regexp "^(Pdb) *")
  1453. (setq paragraph-start comint-prompt-regexp)
  1454. (run-hooks 'pdb-mode-hook))
  1455. ;; ======================================================================
  1456. ;;
  1457. ;; JDB support.
  1458. ;;
  1459. ;; AUTHOR: Derek Davies <ddavies@world.std.com>
  1460. ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
  1461. ;;
  1462. ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
  1463. ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
  1464. ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
  1465. ;;
  1466. ;; INVOCATION NOTES:
  1467. ;;
  1468. ;; You invoke jdb-mode with:
  1469. ;;
  1470. ;; M-x jdb <enter>
  1471. ;;
  1472. ;; It responds with:
  1473. ;;
  1474. ;; Run jdb (like this): jdb
  1475. ;;
  1476. ;; type any jdb switches followed by the name of the class you'd like to debug.
  1477. ;; Supply a fully qualified classname (these don't have the ".class" extension)
  1478. ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
  1479. ;; See the known problems section below for restrictions when specifying jdb
  1480. ;; command line switches (search forward for '-classpath').
  1481. ;;
  1482. ;; You should see something like the following:
  1483. ;;
  1484. ;; Current directory is ~/src/java/hello/
  1485. ;; Initializing jdb...
  1486. ;; 0xed2f6628:class(hello)
  1487. ;; >
  1488. ;;
  1489. ;; To set an initial breakpoint try:
  1490. ;;
  1491. ;; > stop in hello.main
  1492. ;; Breakpoint set in hello.main
  1493. ;; >
  1494. ;;
  1495. ;; To execute the program type:
  1496. ;;
  1497. ;; > run
  1498. ;; run hello
  1499. ;;
  1500. ;; Breakpoint hit: running ...
  1501. ;; hello.main (hello:12)
  1502. ;;
  1503. ;; Type M-n to step over the current line and M-s to step into it. That,
  1504. ;; along with the JDB 'help' command should get you started. The 'quit'
  1505. ;; JDB command will get out out of the debugger. There is some truly
  1506. ;; pathetic JDB documentation available at:
  1507. ;;
  1508. ;; http://java.sun.com/products/jdk/1.1/debugging/
  1509. ;;
  1510. ;; KNOWN PROBLEMS AND FIXME's:
  1511. ;;
  1512. ;; Not sure what happens with inner classes ... haven't tried them.
  1513. ;;
  1514. ;; Does not grok UNICODE id's. Only ASCII id's are supported.
  1515. ;;
  1516. ;; You must not put whitespace between "-classpath" and the path to
  1517. ;; search for java classes even though it is required when invoking jdb
  1518. ;; from the command line. See gud-jdb-massage-args for details.
  1519. ;; The same applies for "-sourcepath".
  1520. ;;
  1521. ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
  1522. ;; refer to the documentation of `gud-jdb-use-classpath' and
  1523. ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
  1524. ;; on using the classpath for locating java source files.
  1525. ;;
  1526. ;; If any of the source files in the directories listed in
  1527. ;; gud-jdb-directories won't parse you'll have problems. Make sure
  1528. ;; every file ending in ".java" in these directories parses without error.
  1529. ;;
  1530. ;; All the .java files in the directories in gud-jdb-directories are
  1531. ;; syntactically analyzed each time gud jdb is invoked. It would be
  1532. ;; nice to keep as much information as possible between runs. It would
  1533. ;; be really nice to analyze the files only as necessary (when the
  1534. ;; source needs to be displayed.) I'm not sure to what extent the former
  1535. ;; can be accomplished and I'm not sure the latter can be done at all
  1536. ;; since I don't know of any general way to tell which .class files are
  1537. ;; defined by which .java file without analyzing all the .java files.
  1538. ;; If anyone knows why JavaSoft didn't put the source file names in
  1539. ;; debuggable .class files please clue me in so I find something else
  1540. ;; to be spiteful and bitter about.
  1541. ;;
  1542. ;; ======================================================================
  1543. ;; gud jdb variables and functions
  1544. (defcustom gud-jdb-command-name "jdb"
  1545. "Command that executes the Java debugger."
  1546. :type 'string
  1547. :group 'gud)
  1548. (defcustom gud-jdb-use-classpath t
  1549. "If non-nil, search for Java source files in classpath directories.
  1550. The list of directories to search is the value of `gud-jdb-classpath'.
  1551. The file pathname is obtained by converting the fully qualified
  1552. class information output by jdb to a relative pathname and appending
  1553. it to `gud-jdb-classpath' element by element until a match is found.
  1554. This method has a significant jdb startup time reduction advantage
  1555. since it does not require the scanning of all `gud-jdb-directories'
  1556. and parsing all Java files for class information.
  1557. Set to nil to use `gud-jdb-directories' to scan java sources for
  1558. class information on jdb startup (original method)."
  1559. :type 'boolean
  1560. :group 'gud)
  1561. (defvar gud-jdb-classpath nil
  1562. "Java/jdb classpath directories list.
  1563. If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
  1564. list automatically using the following methods in sequence
  1565. \(with subsequent successful steps overriding the results of previous
  1566. steps):
  1567. 1) Read the CLASSPATH environment variable,
  1568. 2) Read any \"-classpath\" argument used to run jdb,
  1569. or detected in jdb output (e.g. if jdb is run by a script
  1570. that echoes the actual jdb command before starting jdb),
  1571. 3) Send a \"classpath\" command to jdb and scan jdb output for
  1572. classpath information if jdb is invoked with an \"-attach\" (to
  1573. an already running VM) argument (This case typically does not
  1574. have a \"-classpath\" command line argument - that is provided
  1575. to the VM when it is started).
  1576. Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
  1577. those debuggers do not support the classpath command. Use 1) or 2).")
  1578. (defvar gud-jdb-sourcepath nil
  1579. "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
  1580. This list is prepended to `gud-jdb-classpath' to form the complete
  1581. list of directories searched for source files.")
  1582. (defvar gud-marker-acc-max-length 4000
  1583. "Maximum number of debugger output characters to keep.
  1584. This variable limits the size of `gud-marker-acc' which holds
  1585. the most recent debugger output history while searching for
  1586. source file information.")
  1587. (defvar gud-jdb-history nil
  1588. "History of argument lists passed to jdb.")
  1589. ;; List of Java source file directories.
  1590. (defvar gud-jdb-directories (list ".")
  1591. "*A list of directories that gud jdb should search for source code.
  1592. The file names should be absolute, or relative to the current
  1593. directory.
  1594. The set of .java files residing in the directories listed are
  1595. syntactically analyzed to determine the classes they define and the
  1596. packages in which these classes belong. In this way gud jdb maps the
  1597. package-qualified class names output by the jdb debugger to the source
  1598. file from which the class originated. This allows gud mode to keep
  1599. the source code display in sync with the debugging session.")
  1600. (defvar gud-jdb-source-files nil
  1601. "List of the java source files for this debugging session.")
  1602. ;; Association list of fully qualified class names (package + class name)
  1603. ;; and their source files.
  1604. (defvar gud-jdb-class-source-alist nil
  1605. "Association list of fully qualified class names and source files.")
  1606. ;; This is used to hold a source file during analysis.
  1607. (defvar gud-jdb-analysis-buffer nil)
  1608. (defvar gud-jdb-classpath-string nil
  1609. "Holds temporary classpath values.")
  1610. (defun gud-jdb-build-source-files-list (path extn)
  1611. "Return a list of java source files (absolute paths).
  1612. PATH gives the directories in which to search for files with
  1613. extension EXTN. Normally EXTN is given as the regular expression
  1614. \"\\.java$\" ."
  1615. (apply 'nconc (mapcar (lambda (d)
  1616. (when (file-directory-p d)
  1617. (directory-files d t extn nil)))
  1618. path)))
  1619. ;; Move point past whitespace.
  1620. (defun gud-jdb-skip-whitespace ()
  1621. (skip-chars-forward " \n\r\t\014"))
  1622. ;; Move point past a "// <eol>" type of comment.
  1623. (defun gud-jdb-skip-single-line-comment ()
  1624. (end-of-line))
  1625. ;; Move point past a "/* */" or "/** */" type of comment.
  1626. (defun gud-jdb-skip-traditional-or-documentation-comment ()
  1627. (forward-char 2)
  1628. (catch 'break
  1629. (while (not (eobp))
  1630. (if (eq (following-char) ?*)
  1631. (progn
  1632. (forward-char)
  1633. (if (not (eobp))
  1634. (if (eq (following-char) ?/)
  1635. (progn
  1636. (forward-char)
  1637. (throw 'break nil)))))
  1638. (forward-char)))))
  1639. ;; Move point past any number of consecutive whitespace chars and/or comments.
  1640. (defun gud-jdb-skip-whitespace-and-comments ()
  1641. (gud-jdb-skip-whitespace)
  1642. (catch 'done
  1643. (while t
  1644. (cond
  1645. ((looking-at "//")
  1646. (gud-jdb-skip-single-line-comment)
  1647. (gud-jdb-skip-whitespace))
  1648. ((looking-at "/\\*")
  1649. (gud-jdb-skip-traditional-or-documentation-comment)
  1650. (gud-jdb-skip-whitespace))
  1651. (t (throw 'done nil))))))
  1652. ;; Move point past things that are id-like. The intent is to skip regular
  1653. ;; id's, such as class or interface names as well as package and interface
  1654. ;; names.
  1655. (defun gud-jdb-skip-id-ish-thing ()
  1656. (skip-chars-forward "^ /\n\r\t\014,;{"))
  1657. ;; Move point past a string literal.
  1658. (defun gud-jdb-skip-string-literal ()
  1659. (forward-char)
  1660. (while (not (cond
  1661. ((eq (following-char) ?\\)
  1662. (forward-char))
  1663. ((eq (following-char) ?\042))))
  1664. (forward-char))
  1665. (forward-char))
  1666. ;; Move point past a character literal.
  1667. (defun gud-jdb-skip-character-literal ()
  1668. (forward-char)
  1669. (while
  1670. (progn
  1671. (if (eq (following-char) ?\\)
  1672. (forward-char 2))
  1673. (not (eq (following-char) ?\')))
  1674. (forward-char))
  1675. (forward-char))
  1676. ;; Move point past the following block. There may be (legal) cruft before
  1677. ;; the block's opening brace. There must be a block or it's the end of life
  1678. ;; in petticoat junction.
  1679. (defun gud-jdb-skip-block ()
  1680. ;; Find the beginning of the block.
  1681. (while
  1682. (not (eq (following-char) ?{))
  1683. ;; Skip any constructs that can harbor literal block delimiter
  1684. ;; characters and/or the delimiters for the constructs themselves.
  1685. (cond
  1686. ((looking-at "//")
  1687. (gud-jdb-skip-single-line-comment))
  1688. ((looking-at "/\\*")
  1689. (gud-jdb-skip-traditional-or-documentation-comment))
  1690. ((eq (following-char) ?\042)
  1691. (gud-jdb-skip-string-literal))
  1692. ((eq (following-char) ?\')
  1693. (gud-jdb-skip-character-literal))
  1694. (t (forward-char))))
  1695. ;; Now at the beginning of the block.
  1696. (forward-char)
  1697. ;; Skip over the body of the block as well as the final brace.
  1698. (let ((open-level 1))
  1699. (while (not (eq open-level 0))
  1700. (cond
  1701. ((looking-at "//")
  1702. (gud-jdb-skip-single-line-comment))
  1703. ((looking-at "/\\*")
  1704. (gud-jdb-skip-traditional-or-documentation-comment))
  1705. ((eq (following-char) ?\042)
  1706. (gud-jdb-skip-string-literal))
  1707. ((eq (following-char) ?\')
  1708. (gud-jdb-skip-character-literal))
  1709. ((eq (following-char) ?{)
  1710. (setq open-level (+ open-level 1))
  1711. (forward-char))
  1712. ((eq (following-char) ?})
  1713. (setq open-level (- open-level 1))
  1714. (forward-char))
  1715. (t (forward-char))))))
  1716. ;; Find the package and class definitions in Java source file FILE. Assumes
  1717. ;; that FILE contains a legal Java program. BUF is a scratch buffer used
  1718. ;; to hold the source during analysis.
  1719. (defun gud-jdb-analyze-source (buf file)
  1720. (let ((l nil))
  1721. (set-buffer buf)
  1722. (insert-file-contents file nil nil nil t)
  1723. (goto-char 0)
  1724. (catch 'abort
  1725. (let ((p ""))
  1726. (while (progn
  1727. (gud-jdb-skip-whitespace)
  1728. (not (eobp)))
  1729. (cond
  1730. ;; Any number of semi's following a block is legal. Move point
  1731. ;; past them. Note that comments and whitespace may be
  1732. ;; interspersed as well.
  1733. ((eq (following-char) ?\073)
  1734. (forward-char))
  1735. ;; Move point past a single line comment.
  1736. ((looking-at "//")
  1737. (gud-jdb-skip-single-line-comment))
  1738. ;; Move point past a traditional or documentation comment.
  1739. ((looking-at "/\\*")
  1740. (gud-jdb-skip-traditional-or-documentation-comment))
  1741. ;; Move point past a package statement, but save the PackageName.
  1742. ((looking-at "package")
  1743. (forward-char 7)
  1744. (gud-jdb-skip-whitespace-and-comments)
  1745. (let ((s (point)))
  1746. (gud-jdb-skip-id-ish-thing)
  1747. (setq p (concat (buffer-substring s (point)) "."))
  1748. (gud-jdb-skip-whitespace-and-comments)
  1749. (if (eq (following-char) ?\073)
  1750. (forward-char))))
  1751. ;; Move point past an import statement.
  1752. ((looking-at "import")
  1753. (forward-char 6)
  1754. (gud-jdb-skip-whitespace-and-comments)
  1755. (gud-jdb-skip-id-ish-thing)
  1756. (gud-jdb-skip-whitespace-and-comments)
  1757. (if (eq (following-char) ?\073)
  1758. (forward-char)))
  1759. ;; Move point past the various kinds of ClassModifiers.
  1760. ((looking-at "public")
  1761. (forward-char 6))
  1762. ((looking-at "abstract")
  1763. (forward-char 8))
  1764. ((looking-at "final")
  1765. (forward-char 5))
  1766. ;; Move point past a ClassDeclaration, but save the class
  1767. ;; Identifier.
  1768. ((looking-at "class")
  1769. (forward-char 5)
  1770. (gud-jdb-skip-whitespace-and-comments)
  1771. (let ((s (point)))
  1772. (gud-jdb-skip-id-ish-thing)
  1773. (setq
  1774. l (nconc l (list (concat p (buffer-substring s (point)))))))
  1775. (gud-jdb-skip-block))
  1776. ;; Move point past an interface statement.
  1777. ((looking-at "interface")
  1778. (forward-char 9)
  1779. (gud-jdb-skip-block))
  1780. ;; Anything else means the input is invalid.
  1781. (t
  1782. (message "Error parsing file %s." file)
  1783. (throw 'abort nil))))))
  1784. l))
  1785. (defun gud-jdb-build-class-source-alist-for-file (file)
  1786. (mapcar
  1787. (lambda (c)
  1788. (cons c file))
  1789. (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
  1790. ;; Return an alist of fully qualified classes and the source files
  1791. ;; holding their definitions. SOURCES holds a list of all the source
  1792. ;; files to examine.
  1793. (defun gud-jdb-build-class-source-alist (sources)
  1794. (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
  1795. (prog1
  1796. (apply
  1797. 'nconc
  1798. (mapcar
  1799. 'gud-jdb-build-class-source-alist-for-file
  1800. sources))
  1801. (kill-buffer gud-jdb-analysis-buffer)
  1802. (setq gud-jdb-analysis-buffer nil)))
  1803. ;; Change what was given in the minibuffer to something that can be used to
  1804. ;; invoke the debugger.
  1805. (defun gud-jdb-massage-args (_file args)
  1806. ;; The jdb executable must have whitespace between "-classpath" and
  1807. ;; its value while gud-common-init expects all switch values to
  1808. ;; follow the switch keyword without intervening whitespace. We
  1809. ;; require that when the user enters the "-classpath" switch in the
  1810. ;; EMACS minibuffer that they do so without the intervening
  1811. ;; whitespace. This function adds it back (it's called after
  1812. ;; gud-common-init). There are more switches like this (for
  1813. ;; instance "-host" and "-password") but I don't care about them
  1814. ;; yet.
  1815. (if args
  1816. (let (massaged-args user-error)
  1817. (while (and args (not user-error))
  1818. (cond
  1819. ((setq user-error (string-match "-classpath$" (car args))))
  1820. ((setq user-error (string-match "-sourcepath$" (car args))))
  1821. ((string-match "-classpath\\(.+\\)" (car args))
  1822. (setq massaged-args
  1823. (append massaged-args
  1824. (list "-classpath"
  1825. (setq gud-jdb-classpath-string
  1826. (match-string 1 (car args)))))))
  1827. ((string-match "-sourcepath\\(.+\\)" (car args))
  1828. (setq massaged-args
  1829. (append massaged-args
  1830. (list "-sourcepath"
  1831. (setq gud-jdb-sourcepath
  1832. (match-string 1 (car args)))))))
  1833. (t (setq massaged-args (append massaged-args (list (car args))))))
  1834. (setq args (cdr args)))
  1835. ;; By this point the current directory is all screwed up. Maybe we
  1836. ;; could fix things and re-invoke gud-common-init, but for now I think
  1837. ;; issuing the error is good enough.
  1838. (if user-error
  1839. (progn
  1840. (kill-buffer (current-buffer))
  1841. (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
  1842. massaged-args)))
  1843. ;; Search for an association with P, a fully qualified class name, in
  1844. ;; gud-jdb-class-source-alist. The association gives the fully
  1845. ;; qualified file name of the source file which produced the class.
  1846. (defun gud-jdb-find-source-file (p)
  1847. (cdr (assoc p gud-jdb-class-source-alist)))
  1848. ;; Note: Reset to this value every time a prompt is seen
  1849. (defvar gud-jdb-lowest-stack-level 999)
  1850. (defun gud-jdb-find-source-using-classpath (p)
  1851. "Find source file corresponding to fully qualified class P.
  1852. Convert P from jdb's output, converted to a pathname
  1853. relative to a classpath directory."
  1854. (save-match-data
  1855. (let
  1856. (;; Replace dots with slashes and append ".java" to generate file
  1857. ;; name relative to classpath
  1858. (filename
  1859. (concat
  1860. (mapconcat 'identity
  1861. (split-string
  1862. ;; Eliminate any subclass references in the class
  1863. ;; name string. These start with a "$"
  1864. ((lambda (x)
  1865. (if (string-match "$.*" x)
  1866. (replace-match "" t t x) p))
  1867. p)
  1868. "\\.") "/")
  1869. ".java"))
  1870. (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
  1871. found-file)
  1872. (while (and cplist
  1873. (not (setq found-file
  1874. (file-readable-p
  1875. (concat (car cplist) "/" filename)))))
  1876. (setq cplist (cdr cplist)))
  1877. (if found-file (concat (car cplist) "/" filename)))))
  1878. (defun gud-jdb-find-source (_string)
  1879. "Alias for function used to locate source files.
  1880. Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
  1881. during jdb initialization depending on the value of
  1882. `gud-jdb-use-classpath'."
  1883. nil)
  1884. (defun gud-jdb-parse-classpath-string (string)
  1885. "Parse the classpath list and convert each item to an absolute pathname."
  1886. (mapcar (lambda (s) (if (string-match "[/\\]$" s)
  1887. (replace-match "" nil nil s) s))
  1888. (mapcar 'file-truename
  1889. (split-string
  1890. string
  1891. (concat "[ \t\n\r,\"" path-separator "]+")))))
  1892. ;; See commentary for other debugger's marker filters - there you will find
  1893. ;; important notes about STRING.
  1894. (defun gud-jdb-marker-filter (string)
  1895. ;; Build up the accumulator.
  1896. (setq gud-marker-acc
  1897. (if gud-marker-acc
  1898. (concat gud-marker-acc string)
  1899. string))
  1900. ;; Look for classpath information until gud-jdb-classpath-string is found
  1901. ;; (interactive, multiple settings of classpath from jdb
  1902. ;; not supported/followed)
  1903. (if (and gud-jdb-use-classpath
  1904. (not gud-jdb-classpath-string)
  1905. (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
  1906. (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
  1907. (setq gud-jdb-classpath
  1908. (gud-jdb-parse-classpath-string
  1909. (setq gud-jdb-classpath-string
  1910. (match-string 1 gud-marker-acc)))))
  1911. ;; We process STRING from left to right. Each time through the
  1912. ;; following loop we process at most one marker. After we've found a
  1913. ;; marker, delete gud-marker-acc up to and including the match
  1914. (let (file-found)
  1915. ;; Process each complete marker in the input.
  1916. (while
  1917. ;; Do we see a marker?
  1918. (string-match
  1919. ;; jdb puts out a string of the following form when it
  1920. ;; hits a breakpoint:
  1921. ;;
  1922. ;; <fully-qualified-class><method> (<class>:<line-number>)
  1923. ;;
  1924. ;; <fully-qualified-class>'s are composed of Java ID's
  1925. ;; separated by periods. <method> and <class> are
  1926. ;; also Java ID's. <method> begins with a period and
  1927. ;; may contain less-than and greater-than (constructors,
  1928. ;; for instance, are called <init> in the symbol table.)
  1929. ;; Java ID's begin with a letter followed by letters
  1930. ;; and/or digits. The set of letters includes underscore
  1931. ;; and dollar sign.
  1932. ;;
  1933. ;; The first group matches <fully-qualified-class>,
  1934. ;; the second group matches <class> and the third group
  1935. ;; matches <line-number>. We don't care about using
  1936. ;; <method> so we don't "group" it.
  1937. ;;
  1938. ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
  1939. ;; ID's only.
  1940. ;;
  1941. ;; The ".," in the last square-bracket are necessary because
  1942. ;; of Sun's total disrespect for backwards compatibility in
  1943. ;; reported line numbers from jdb - starting in 1.4.0 they
  1944. ;; print line numbers using LOCALE, inserting a comma or a
  1945. ;; period at the thousands positions (how ingenious!).
  1946. "\\(\\[[0-9]+] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
  1947. \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9.,]+\\)"
  1948. gud-marker-acc)
  1949. ;; A good marker is one that:
  1950. ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
  1951. ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
  1952. ;; since the last prompt
  1953. ;; Figure out the line on which to position the debugging arrow.
  1954. ;; Return the info as a cons of the form:
  1955. ;;
  1956. ;; (<file-name> . <line-number>) .
  1957. (if (if (match-beginning 1)
  1958. (let (n)
  1959. (setq n (string-to-number (substring
  1960. gud-marker-acc
  1961. (1+ (match-beginning 1))
  1962. (- (match-end 1) 2))))
  1963. (if (< n gud-jdb-lowest-stack-level)
  1964. (progn (setq gud-jdb-lowest-stack-level n) t)))
  1965. t)
  1966. (if (setq file-found
  1967. (gud-jdb-find-source (match-string 2 gud-marker-acc)))
  1968. (setq gud-last-frame
  1969. (cons file-found
  1970. (string-to-number
  1971. (let
  1972. ((numstr (match-string 4 gud-marker-acc)))
  1973. (if (string-match "[.,]" numstr)
  1974. (replace-match "" nil nil numstr)
  1975. numstr)))))
  1976. (message "Could not find source file.")))
  1977. ;; Set the accumulator to the remaining text.
  1978. (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
  1979. (if (string-match comint-prompt-regexp gud-marker-acc)
  1980. (setq gud-jdb-lowest-stack-level 999)))
  1981. ;; Do not allow gud-marker-acc to grow without bound. If the source
  1982. ;; file information is not within the last 3/4
  1983. ;; gud-marker-acc-max-length characters, well,...
  1984. (if (> (length gud-marker-acc) gud-marker-acc-max-length)
  1985. (setq gud-marker-acc
  1986. (substring gud-marker-acc
  1987. (- (/ (* gud-marker-acc-max-length 3) 4)))))
  1988. ;; We don't filter any debugger output so just return what we were given.
  1989. string)
  1990. (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
  1991. ;;;###autoload
  1992. (defun jdb (command-line)
  1993. "Run jdb with command line COMMAND-LINE in a buffer.
  1994. The buffer is named \"*gud*\" if no initial class is given or
  1995. \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
  1996. switch is given, omit all whitespace between it and its value.
  1997. See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
  1998. information on how jdb accesses source files. Alternatively (if
  1999. `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
  2000. original source file access method.
  2001. For general information about commands available to control jdb from
  2002. gud, see `gud-mode'."
  2003. (interactive
  2004. (list (gud-query-cmdline 'jdb)))
  2005. (setq gud-jdb-classpath nil)
  2006. (setq gud-jdb-sourcepath nil)
  2007. ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
  2008. ;; if CLASSPATH is set.
  2009. (setq gud-jdb-classpath-string (or (getenv "CLASSPATH") "."))
  2010. (if gud-jdb-classpath-string
  2011. (setq gud-jdb-classpath
  2012. (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
  2013. (setq gud-jdb-classpath-string nil) ; prepare for next
  2014. (gud-common-init command-line 'gud-jdb-massage-args
  2015. 'gud-jdb-marker-filter)
  2016. (set (make-local-variable 'gud-minor-mode) 'jdb)
  2017. ;; If a -classpath option was provided, set gud-jdb-classpath
  2018. (if gud-jdb-classpath-string
  2019. (setq gud-jdb-classpath
  2020. (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
  2021. (setq gud-jdb-classpath-string nil) ; prepare for next
  2022. ;; If a -sourcepath option was provided, parse it
  2023. (if gud-jdb-sourcepath
  2024. (setq gud-jdb-sourcepath
  2025. (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
  2026. (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
  2027. (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
  2028. (gud-def gud-step "step" "\C-s" "Step one source line with display.")
  2029. (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
  2030. (gud-def gud-cont "cont" "\C-r" "Continue with display.")
  2031. (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
  2032. (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
  2033. (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
  2034. (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
  2035. (gud-def gud-print "print %e" "\C-p" "Print value of expression at point.")
  2036. (gud-def gud-pstar "dump %e" nil "Print all object information at point.")
  2037. (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
  2038. (setq paragraph-start comint-prompt-regexp)
  2039. (run-hooks 'jdb-mode-hook)
  2040. (if gud-jdb-use-classpath
  2041. ;; Get the classpath information from the debugger
  2042. (progn
  2043. (if (string-match "-attach" command-line)
  2044. (gud-call "classpath"))
  2045. (fset 'gud-jdb-find-source
  2046. 'gud-jdb-find-source-using-classpath))
  2047. ;; Else create and bind the class/source association list as well
  2048. ;; as the source file list.
  2049. (setq gud-jdb-class-source-alist
  2050. (gud-jdb-build-class-source-alist
  2051. (setq gud-jdb-source-files
  2052. (gud-jdb-build-source-files-list gud-jdb-directories
  2053. "\\.java$"))))
  2054. (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
  2055. ;;
  2056. ;; End of debugger-specific information
  2057. ;;
  2058. ;; When we send a command to the debugger via gud-call, it's annoying
  2059. ;; to see the command and the new prompt inserted into the debugger's
  2060. ;; buffer; we have other ways of knowing the command has completed.
  2061. ;;
  2062. ;; If the buffer looks like this:
  2063. ;; --------------------
  2064. ;; (gdb) set args foo bar
  2065. ;; (gdb) -!-
  2066. ;; --------------------
  2067. ;; (the -!- marks the location of point), and we type `C-x SPC' in a
  2068. ;; source file to set a breakpoint, we want the buffer to end up like
  2069. ;; this:
  2070. ;; --------------------
  2071. ;; (gdb) set args foo bar
  2072. ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
  2073. ;; (gdb) -!-
  2074. ;; --------------------
  2075. ;; Essentially, the old prompt is deleted, and the command's output
  2076. ;; and the new prompt take its place.
  2077. ;;
  2078. ;; Not echoing the command is easy enough; you send it directly using
  2079. ;; process-send-string, and it never enters the buffer. However,
  2080. ;; getting rid of the old prompt is trickier; you don't want to do it
  2081. ;; when you send the command, since that will result in an annoying
  2082. ;; flicker as the prompt is deleted, redisplay occurs while Emacs
  2083. ;; waits for a response from the debugger, and the new prompt is
  2084. ;; inserted. Instead, we'll wait until we actually get some output
  2085. ;; from the subprocess before we delete the prompt. If the command
  2086. ;; produced no output other than a new prompt, that prompt will most
  2087. ;; likely be in the first chunk of output received, so we will delete
  2088. ;; the prompt and then replace it with an identical one. If the
  2089. ;; command produces output, the prompt is moving anyway, so the
  2090. ;; flicker won't be annoying.
  2091. ;;
  2092. ;; So - when we want to delete the prompt upon receipt of the next
  2093. ;; chunk of debugger output, we position gud-delete-prompt-marker at
  2094. ;; the start of the prompt; the process filter will notice this, and
  2095. ;; delete all text between it and the process output marker. If
  2096. ;; gud-delete-prompt-marker points nowhere, we leave the current
  2097. ;; prompt alone.
  2098. (defvar gud-delete-prompt-marker nil)
  2099. (put 'gud-mode 'mode-class 'special)
  2100. (define-derived-mode gud-mode comint-mode "Debugger"
  2101. "Major mode for interacting with an inferior debugger process.
  2102. You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
  2103. M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
  2104. hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
  2105. `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
  2106. After startup, the following commands are available in both the GUD
  2107. interaction buffer and any source buffer GUD visits due to a breakpoint stop
  2108. or step operation:
  2109. \\[gud-break] sets a breakpoint at the current file and line. In the
  2110. GUD buffer, the current file and line are those of the last breakpoint or
  2111. step. In a source buffer, they are the buffer's file and current line.
  2112. \\[gud-remove] removes breakpoints on the current file and line.
  2113. \\[gud-refresh] displays in the source window the last line referred to
  2114. in the gud buffer.
  2115. \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
  2116. step-one-line (not entering function calls), and step-one-instruction
  2117. and then update the source window with the current file and position.
  2118. \\[gud-cont] continues execution.
  2119. \\[gud-print] tries to find the largest C lvalue or function-call expression
  2120. around point, and sends it to the debugger for value display.
  2121. The above commands are common to all supported debuggers except xdb which
  2122. does not support stepping instructions.
  2123. Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
  2124. except that the breakpoint is temporary; that is, it is removed when
  2125. execution stops on it.
  2126. Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
  2127. frame. \\[gud-down] drops back down through one.
  2128. If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
  2129. the current function and stops.
  2130. All the keystrokes above are accessible in the GUD buffer
  2131. with the prefix C-c, and in all buffers through the prefix C-x C-a.
  2132. All pre-defined functions for which the concept make sense repeat
  2133. themselves the appropriate number of times if you give a prefix
  2134. argument.
  2135. You may use the `gud-def' macro in the initialization hook to define other
  2136. commands.
  2137. Other commands for interacting with the debugger process are inherited from
  2138. comint mode, which see."
  2139. (setq mode-line-process '(":%s"))
  2140. (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
  2141. (set (make-local-variable 'gud-last-frame) nil)
  2142. (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
  2143. (make-local-variable 'comint-prompt-regexp)
  2144. ;; Don't put repeated commands in command history many times.
  2145. (set (make-local-variable 'comint-input-ignoredups) t)
  2146. (make-local-variable 'paragraph-start)
  2147. (set (make-local-variable 'gud-delete-prompt-marker) (make-marker))
  2148. (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook nil t))
  2149. (defcustom gud-chdir-before-run t
  2150. "Non-nil if GUD should `cd' to the debugged executable."
  2151. :group 'gud
  2152. :type 'boolean)
  2153. (declare-function tramp-file-name-localname "tramp" (vec))
  2154. (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault))
  2155. ;; Perform initializations common to all debuggers.
  2156. ;; The first arg is the specified command line,
  2157. ;; which starts with the program to debug.
  2158. ;; The other three args specify the values to use
  2159. ;; for local variables in the debugger buffer.
  2160. (defun gud-common-init (command-line massage-args marker-filter
  2161. &optional find-file)
  2162. (let* ((words (split-string-and-unquote command-line))
  2163. (program (car words))
  2164. (dir default-directory)
  2165. ;; Extract the file name from WORDS
  2166. ;; and put t in its place.
  2167. ;; Later on we will put the modified file name arg back there.
  2168. (file-word (let ((w (cdr words)))
  2169. (while (and w (= ?- (aref (car w) 0)))
  2170. (setq w (cdr w)))
  2171. (and w
  2172. (prog1 (car w)
  2173. (setcar w t)))))
  2174. (file-subst
  2175. (and file-word (substitute-in-file-name file-word)))
  2176. (args (cdr words))
  2177. ;; If a directory was specified, expand the file name.
  2178. ;; Otherwise, don't expand it, so GDB can use the PATH.
  2179. ;; A file name without directory is literally valid
  2180. ;; only if the file exists in ., and in that case,
  2181. ;; omitting the expansion here has no visible effect.
  2182. (file (and file-word
  2183. (if (file-name-directory file-subst)
  2184. (expand-file-name file-subst)
  2185. file-subst)))
  2186. (filepart (and file-word (concat "-" (file-name-nondirectory file))))
  2187. (existing-buffer (get-buffer (concat "*gud" filepart "*"))))
  2188. (switch-to-buffer (concat "*gud" filepart "*"))
  2189. (when (and existing-buffer (get-buffer-process existing-buffer))
  2190. (error "This program is already being debugged"))
  2191. ;; Set the dir, in case the buffer already existed with a different dir.
  2192. (setq default-directory dir)
  2193. ;; Set default-directory to the file's directory.
  2194. (and file-word
  2195. gud-chdir-before-run
  2196. ;; Don't set default-directory if no directory was specified.
  2197. ;; In that case, either the file is found in the current directory,
  2198. ;; in which case this setq is a no-op,
  2199. ;; or it is found by searching PATH,
  2200. ;; in which case we don't know what directory it was found in.
  2201. (file-name-directory file)
  2202. (setq default-directory (file-name-directory file)))
  2203. (or (bolp) (newline))
  2204. (insert "Current directory is " default-directory "\n")
  2205. ;; Put the substituted and expanded file name back in its place.
  2206. (let ((w args))
  2207. (while (and w (not (eq (car w) t)))
  2208. (setq w (cdr w)))
  2209. (if w
  2210. (setcar w
  2211. (if (file-remote-p file)
  2212. ;; Tramp has already been loaded if we are here.
  2213. (setq file (tramp-file-name-localname
  2214. (tramp-dissect-file-name file)))
  2215. file))))
  2216. (apply 'make-comint (concat "gud" filepart) program nil
  2217. (if massage-args (funcall massage-args file args) args))
  2218. ;; Since comint clobbered the mode, we don't set it until now.
  2219. (gud-mode)
  2220. (set (make-local-variable 'gud-target-name)
  2221. (and file-word (file-name-nondirectory file))))
  2222. (set (make-local-variable 'gud-marker-filter) marker-filter)
  2223. (if find-file (set (make-local-variable 'gud-find-file) find-file))
  2224. (setq gud-last-last-frame nil)
  2225. (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
  2226. (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
  2227. (gud-set-buffer))
  2228. (defun gud-set-buffer ()
  2229. (when (derived-mode-p 'gud-mode)
  2230. (setq gud-comint-buffer (current-buffer))))
  2231. (defvar gud-filter-defer-flag nil
  2232. "Non-nil means don't process anything from the debugger right now.
  2233. It is saved for when this flag is not set.")
  2234. ;; These functions are responsible for inserting output from your debugger
  2235. ;; into the buffer. The hard work is done by the method that is
  2236. ;; the value of gud-marker-filter.
  2237. (defun gud-filter (proc string)
  2238. ;; Here's where the actual buffer insertion is done
  2239. (let (output process-window)
  2240. (if (buffer-name (process-buffer proc))
  2241. (if gud-filter-defer-flag
  2242. ;; If we can't process any text now,
  2243. ;; save it for later.
  2244. (setq gud-filter-pending-text
  2245. (concat (or gud-filter-pending-text "") string))
  2246. ;; If we have to ask a question during the processing,
  2247. ;; defer any additional text that comes from the debugger
  2248. ;; during that time.
  2249. (let ((gud-filter-defer-flag t))
  2250. ;; Process now any text we previously saved up.
  2251. (if gud-filter-pending-text
  2252. (setq string (concat gud-filter-pending-text string)
  2253. gud-filter-pending-text nil))
  2254. (with-current-buffer (process-buffer proc)
  2255. ;; If we have been so requested, delete the debugger prompt.
  2256. (save-restriction
  2257. (widen)
  2258. (if (marker-buffer gud-delete-prompt-marker)
  2259. (let ((inhibit-read-only t))
  2260. (delete-region (process-mark proc)
  2261. gud-delete-prompt-marker)
  2262. (comint-update-fence)
  2263. (set-marker gud-delete-prompt-marker nil)))
  2264. ;; Save the process output, checking for source file markers.
  2265. (setq output (gud-marker-filter string))
  2266. ;; Check for a filename-and-line number.
  2267. ;; Don't display the specified file
  2268. ;; unless (1) point is at or after the position where output appears
  2269. ;; and (2) this buffer is on the screen.
  2270. (setq process-window
  2271. (and gud-last-frame
  2272. (>= (point) (process-mark proc))
  2273. (get-buffer-window (current-buffer)))))
  2274. ;; Let the comint filter do the actual insertion.
  2275. ;; That lets us inherit various comint features.
  2276. (comint-output-filter proc output))
  2277. ;; Put the arrow on the source line.
  2278. ;; This must be outside of the save-excursion
  2279. ;; in case the source file is our current buffer.
  2280. (if process-window
  2281. (with-selected-window process-window
  2282. (gud-display-frame))
  2283. ;; We have to be in the proper buffer, (process-buffer proc),
  2284. ;; but not in a save-excursion, because that would restore point.
  2285. (with-current-buffer (process-buffer proc)
  2286. (gud-display-frame))))
  2287. ;; If we deferred text that arrived during this processing,
  2288. ;; handle it now.
  2289. (if gud-filter-pending-text
  2290. (gud-filter proc ""))))))
  2291. (defvar gud-minor-mode-type nil)
  2292. (defvar gud-overlay-arrow-position nil)
  2293. (add-to-list 'overlay-arrow-variable-list 'gud-overlay-arrow-position)
  2294. (declare-function gdb-reset "gdb-mi" ())
  2295. (defun gud-sentinel (proc msg)
  2296. (cond ((null (buffer-name (process-buffer proc)))
  2297. ;; buffer killed
  2298. ;; Stop displaying an arrow in a source file.
  2299. (setq gud-overlay-arrow-position nil)
  2300. (set-process-buffer proc nil)
  2301. (if (and (boundp 'speedbar-frame)
  2302. (string-equal speedbar-initial-expansion-list-name "GUD"))
  2303. (speedbar-change-initial-expansion-list
  2304. speedbar-previously-used-expansion-list-name))
  2305. (if (eq gud-minor-mode-type 'gdbmi)
  2306. (gdb-reset)
  2307. (gud-reset)))
  2308. ((memq (process-status proc) '(signal exit))
  2309. ;; Stop displaying an arrow in a source file.
  2310. (setq gud-overlay-arrow-position nil)
  2311. (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  2312. 'gdbmi)
  2313. (gdb-reset)
  2314. (gud-reset))
  2315. (let* ((obuf (current-buffer)))
  2316. ;; save-excursion isn't the right thing if
  2317. ;; process-buffer is current-buffer
  2318. (unwind-protect
  2319. (progn
  2320. ;; Write something in the GUD buffer and hack its mode line,
  2321. (set-buffer (process-buffer proc))
  2322. ;; Fix the mode line.
  2323. (setq mode-line-process
  2324. (concat ":"
  2325. (symbol-name (process-status proc))))
  2326. (force-mode-line-update)
  2327. (if (eobp)
  2328. (insert ?\n mode-name " " msg)
  2329. (save-excursion
  2330. (goto-char (point-max))
  2331. (insert ?\n mode-name " " msg)))
  2332. ;; If buffer and mode line will show that the process
  2333. ;; is dead, we can delete it now. Otherwise it
  2334. ;; will stay around until M-x list-processes.
  2335. (delete-process proc))
  2336. ;; Restore old buffer, but don't restore old point
  2337. ;; if obuf is the gud buffer.
  2338. (set-buffer obuf))))))
  2339. (defun gud-kill-buffer-hook ()
  2340. (setq gud-minor-mode-type gud-minor-mode)
  2341. (condition-case nil
  2342. (progn
  2343. (kill-process (get-buffer-process (current-buffer)))
  2344. (delete-process (get-process "gdb-inferior")))
  2345. (error nil)))
  2346. (defun gud-reset ()
  2347. (dolist (buffer (buffer-list))
  2348. (unless (eq buffer gud-comint-buffer)
  2349. (with-current-buffer buffer
  2350. (when gud-minor-mode
  2351. (setq gud-minor-mode nil)
  2352. (kill-local-variable 'tool-bar-map))))))
  2353. (defun gud-display-frame ()
  2354. "Find and obey the last filename-and-line marker from the debugger.
  2355. Obeying it means displaying in another window the specified file and line."
  2356. (interactive)
  2357. (when gud-last-frame
  2358. (gud-set-buffer)
  2359. (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
  2360. (setq gud-last-last-frame gud-last-frame
  2361. gud-last-frame nil)))
  2362. (declare-function global-hl-line-highlight "hl-line" ())
  2363. (declare-function hl-line-highlight "hl-line" ())
  2364. (declare-function gdb-display-source-buffer "gdb-mi" (buffer))
  2365. (declare-function gdb-display-buffer "gdb-mi" (buf dedicated &optional size))
  2366. ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
  2367. ;; and that its line LINE is visible.
  2368. ;; Put the overlay-arrow on the line LINE in that buffer.
  2369. ;; Most of the trickiness in here comes from wanting to preserve the current
  2370. ;; region-restriction if that's possible. We use an explicit display-buffer
  2371. ;; to get around the fact that this is called inside a save-excursion.
  2372. (defun gud-display-line (true-file line)
  2373. (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
  2374. (buffer
  2375. (with-current-buffer gud-comint-buffer
  2376. (gud-find-file true-file)))
  2377. (window (and buffer
  2378. (or (get-buffer-window buffer)
  2379. (if (eq gud-minor-mode 'gdbmi)
  2380. (or (if (get-buffer-window buffer 'visible)
  2381. (display-buffer buffer nil 'visible))
  2382. (unless (gdb-display-source-buffer buffer)
  2383. (gdb-display-buffer buffer nil 'visible))))
  2384. (display-buffer buffer))))
  2385. (pos))
  2386. (if buffer
  2387. (progn
  2388. (with-current-buffer buffer
  2389. (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
  2390. (if (yes-or-no-p
  2391. (format "File %s changed on disk. Reread from disk? "
  2392. (buffer-name)))
  2393. (revert-buffer t t)
  2394. (setq gud-keep-buffer t)))
  2395. (save-restriction
  2396. (widen)
  2397. (goto-char (point-min))
  2398. (forward-line (1- line))
  2399. (setq pos (point))
  2400. (or gud-overlay-arrow-position
  2401. (setq gud-overlay-arrow-position (make-marker)))
  2402. (set-marker gud-overlay-arrow-position (point) (current-buffer))
  2403. ;; If they turned on hl-line, move the hl-line highlight to
  2404. ;; the arrow's line.
  2405. (when (featurep 'hl-line)
  2406. (cond
  2407. (global-hl-line-mode
  2408. (global-hl-line-highlight))
  2409. ((and hl-line-mode hl-line-sticky-flag)
  2410. (hl-line-highlight)))))
  2411. (cond ((or (< pos (point-min)) (> pos (point-max)))
  2412. (widen)
  2413. (goto-char pos))))
  2414. (when window
  2415. (set-window-point window gud-overlay-arrow-position)
  2416. (if (eq gud-minor-mode 'gdbmi)
  2417. (setq gdb-source-window window)))))))
  2418. ;; The gud-call function must do the right thing whether its invoking
  2419. ;; keystroke is from the GUD buffer itself (via major-mode binding)
  2420. ;; or a C buffer. In the former case, we want to supply data from
  2421. ;; gud-last-frame. Here's how we do it:
  2422. (defun gud-format-command (str arg)
  2423. (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
  2424. (frame (or gud-last-frame gud-last-last-frame))
  2425. result)
  2426. (while (and str
  2427. (let ((case-fold-search nil))
  2428. (string-match "\\([^%]*\\)%\\([adefFlpc]\\)" str)))
  2429. (let ((key (string-to-char (match-string 2 str)))
  2430. subst)
  2431. (cond
  2432. ((eq key ?f)
  2433. (setq subst (file-name-nondirectory (if insource
  2434. (buffer-file-name)
  2435. (car frame)))))
  2436. ((eq key ?F)
  2437. (setq subst (file-name-sans-extension
  2438. (file-name-nondirectory (if insource
  2439. (buffer-file-name)
  2440. (car frame))))))
  2441. ((eq key ?d)
  2442. (setq subst (file-name-directory (if insource
  2443. (buffer-file-name)
  2444. (car frame)))))
  2445. ((eq key ?l)
  2446. (setq subst (int-to-string
  2447. (if insource
  2448. (save-restriction
  2449. (widen)
  2450. (+ (count-lines (point-min) (point))
  2451. (if (bolp) 1 0)))
  2452. (cdr frame)))))
  2453. ((eq key ?e)
  2454. (setq subst (gud-find-expr)))
  2455. ((eq key ?a)
  2456. (setq subst (gud-read-address)))
  2457. ((eq key ?c)
  2458. (setq subst
  2459. (gud-find-class
  2460. (if insource
  2461. (buffer-file-name)
  2462. (car frame))
  2463. (if insource
  2464. (save-restriction
  2465. (widen)
  2466. (+ (count-lines (point-min) (point))
  2467. (if (bolp) 1 0)))
  2468. (cdr frame)))))
  2469. ((eq key ?p)
  2470. (setq subst (if arg (int-to-string arg)))))
  2471. (setq result (concat result (match-string 1 str) subst)))
  2472. (setq str (substring str (match-end 2))))
  2473. ;; There might be text left in STR when the loop ends.
  2474. (concat result str)))
  2475. (defun gud-read-address ()
  2476. "Return a string containing the core-address found in the buffer at point."
  2477. (save-match-data
  2478. (save-excursion
  2479. (let ((pt (point)) found begin)
  2480. (setq found (if (search-backward "0x" (- pt 7) t) (point)))
  2481. (cond
  2482. (found (forward-char 2)
  2483. (buffer-substring found
  2484. (progn (re-search-forward "[^0-9a-f]")
  2485. (forward-char -1)
  2486. (point))))
  2487. (t (setq begin (progn (re-search-backward "[^0-9]")
  2488. (forward-char 1)
  2489. (point)))
  2490. (forward-char 1)
  2491. (re-search-forward "[^0-9]")
  2492. (forward-char -1)
  2493. (buffer-substring begin (point))))))))
  2494. (defun gud-call (fmt &optional arg)
  2495. (let ((msg (gud-format-command fmt arg)))
  2496. (message "Command: %s" msg)
  2497. (sit-for 0)
  2498. (gud-basic-call msg)))
  2499. (defun gud-basic-call (command)
  2500. "Invoke the debugger COMMAND displaying source in other window."
  2501. (interactive)
  2502. (gud-set-buffer)
  2503. (let ((proc (get-buffer-process gud-comint-buffer)))
  2504. (or proc (error "Current buffer has no process"))
  2505. ;; Arrange for the current prompt to get deleted.
  2506. (with-current-buffer gud-comint-buffer
  2507. (save-excursion
  2508. (save-restriction
  2509. (widen)
  2510. (if (marker-position gud-delete-prompt-marker)
  2511. ;; We get here when printing an expression.
  2512. (goto-char gud-delete-prompt-marker)
  2513. (goto-char (process-mark proc))
  2514. (forward-line 0))
  2515. (if (looking-at comint-prompt-regexp)
  2516. (set-marker gud-delete-prompt-marker (point)))
  2517. (if (eq gud-minor-mode 'gdbmi)
  2518. (apply comint-input-sender (list proc command))
  2519. (process-send-string proc (concat command "\n"))))))))
  2520. (defun gud-refresh (&optional arg)
  2521. "Fix up a possibly garbled display, and redraw the arrow."
  2522. (interactive "P")
  2523. (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
  2524. (gud-display-frame)
  2525. (recenter arg))
  2526. ;; Code for parsing expressions out of C or Fortran code. The single entry
  2527. ;; point is gud-find-expr, which tries to return an lvalue expression from
  2528. ;; around point.
  2529. (defvar gud-find-expr-function 'gud-find-c-expr)
  2530. (defun gud-find-expr (&rest args)
  2531. (let ((expr (if (and transient-mark-mode mark-active)
  2532. (buffer-substring (region-beginning) (region-end))
  2533. (apply gud-find-expr-function args))))
  2534. (save-match-data
  2535. (if (string-match "\n" expr)
  2536. (error "Expression must not include a newline"))
  2537. (with-current-buffer gud-comint-buffer
  2538. (save-excursion
  2539. (goto-char (process-mark (get-buffer-process gud-comint-buffer)))
  2540. (forward-line 0)
  2541. (when (looking-at comint-prompt-regexp)
  2542. (set-marker gud-delete-prompt-marker (point))
  2543. (set-marker-insertion-type gud-delete-prompt-marker t))
  2544. (unless (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  2545. 'jdb)
  2546. (insert (concat expr " = "))))))
  2547. expr))
  2548. ;; The next eight functions are hacked from gdbsrc.el by
  2549. ;; Debby Ayers <ayers@asc.slb.com>,
  2550. ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
  2551. (defun gud-find-c-expr ()
  2552. "Returns the expr that surrounds point."
  2553. (interactive)
  2554. (save-excursion
  2555. (let ((p (point))
  2556. (expr (gud-innermost-expr))
  2557. (test-expr (gud-prev-expr)))
  2558. (while (and test-expr (gud-expr-compound test-expr expr))
  2559. (let ((prev-expr expr))
  2560. (setq expr (cons (car test-expr) (cdr expr)))
  2561. (goto-char (car expr))
  2562. (setq test-expr (gud-prev-expr))
  2563. ;; If we just pasted on the condition of an if or while,
  2564. ;; throw it away again.
  2565. (if (member (buffer-substring (car test-expr) (cdr test-expr))
  2566. '("if" "while" "for"))
  2567. (setq test-expr nil
  2568. expr prev-expr))))
  2569. (goto-char p)
  2570. (setq test-expr (gud-next-expr))
  2571. (while (gud-expr-compound expr test-expr)
  2572. (setq expr (cons (car expr) (cdr test-expr)))
  2573. (setq test-expr (gud-next-expr)))
  2574. (buffer-substring (car expr) (cdr expr)))))
  2575. (defun gud-innermost-expr ()
  2576. "Returns the smallest expr that point is in; move point to beginning of it.
  2577. The expr is represented as a cons cell, where the car specifies the point in
  2578. the current buffer that marks the beginning of the expr and the cdr specifies
  2579. the character after the end of the expr."
  2580. (let ((p (point)) begin end)
  2581. (gud-backward-sexp)
  2582. (setq begin (point))
  2583. (gud-forward-sexp)
  2584. (setq end (point))
  2585. (if (>= p end)
  2586. (progn
  2587. (setq begin p)
  2588. (goto-char p)
  2589. (gud-forward-sexp)
  2590. (setq end (point)))
  2591. )
  2592. (goto-char begin)
  2593. (cons begin end)))
  2594. (defun gud-backward-sexp ()
  2595. "Version of `backward-sexp' that catches errors."
  2596. (condition-case nil
  2597. (backward-sexp)
  2598. (error t)))
  2599. (defun gud-forward-sexp ()
  2600. "Version of `forward-sexp' that catches errors."
  2601. (condition-case nil
  2602. (forward-sexp)
  2603. (error t)))
  2604. (defun gud-prev-expr ()
  2605. "Returns the previous expr, point is set to beginning of that expr.
  2606. The expr is represented as a cons cell, where the car specifies the point in
  2607. the current buffer that marks the beginning of the expr and the cdr specifies
  2608. the character after the end of the expr"
  2609. (let ((begin) (end))
  2610. (gud-backward-sexp)
  2611. (setq begin (point))
  2612. (gud-forward-sexp)
  2613. (setq end (point))
  2614. (goto-char begin)
  2615. (cons begin end)))
  2616. (defun gud-next-expr ()
  2617. "Returns the following expr, point is set to beginning of that expr.
  2618. The expr is represented as a cons cell, where the car specifies the point in
  2619. the current buffer that marks the beginning of the expr and the cdr specifies
  2620. the character after the end of the expr."
  2621. (let ((begin) (end))
  2622. (gud-forward-sexp)
  2623. (gud-forward-sexp)
  2624. (setq end (point))
  2625. (gud-backward-sexp)
  2626. (setq begin (point))
  2627. (cons begin end)))
  2628. (defun gud-expr-compound-sep (span-start span-end)
  2629. "Scan from SPAN-START to SPAN-END for punctuation characters.
  2630. If `->' is found, return `?.'. If `.' is found, return `?.'.
  2631. If any other punctuation is found, return `??'.
  2632. If no punctuation is found, return `? '."
  2633. (let ((result ?\s)
  2634. (syntax))
  2635. (while (< span-start span-end)
  2636. (setq syntax (char-syntax (char-after span-start)))
  2637. (cond
  2638. ((= syntax ?\s) t)
  2639. ((= syntax ?.) (setq syntax (char-after span-start))
  2640. (cond
  2641. ((= syntax ?.) (setq result ?.))
  2642. ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
  2643. (setq result ?.)
  2644. (setq span-start (+ span-start 1)))
  2645. (t (setq span-start span-end)
  2646. (setq result ??)))))
  2647. (setq span-start (+ span-start 1)))
  2648. result))
  2649. (defun gud-expr-compound (first second)
  2650. "Non-nil if concatenating FIRST and SECOND makes a single C expression.
  2651. The two exprs are represented as a cons cells, where the car
  2652. specifies the point in the current buffer that marks the beginning of the
  2653. expr and the cdr specifies the character after the end of the expr.
  2654. Link exprs of the form:
  2655. Expr -> Expr
  2656. Expr . Expr
  2657. Expr (Expr)
  2658. Expr [Expr]
  2659. (Expr) Expr
  2660. [Expr] Expr"
  2661. (let ((span-start (cdr first))
  2662. (span-end (car second))
  2663. (syntax))
  2664. (setq syntax (gud-expr-compound-sep span-start span-end))
  2665. (cond
  2666. ((= (car first) (car second)) nil)
  2667. ((= (cdr first) (cdr second)) nil)
  2668. ((= syntax ?.) t)
  2669. ((= syntax ?\s)
  2670. (setq span-start (char-after (- span-start 1)))
  2671. (setq span-end (char-after span-end))
  2672. (cond
  2673. ((= span-start ?)) t)
  2674. ((= span-start ?]) t)
  2675. ((= span-end ?() t)
  2676. ((= span-end ?[) t)
  2677. (t nil)))
  2678. (t nil))))
  2679. (declare-function c-langelem-sym "cc-defs" (langelem))
  2680. (declare-function c-langelem-pos "cc-defs" (langelem))
  2681. (defun gud-find-class (f _line)
  2682. "Find fully qualified class in file F at line LINE.
  2683. This function uses the `gud-jdb-classpath' (and optional
  2684. `gud-jdb-sourcepath') list(s) to derive a file
  2685. pathname relative to its classpath directory. The values in
  2686. `gud-jdb-classpath' are assumed to have been converted to absolute
  2687. pathname standards using file-truename.
  2688. If F is visited by a buffer and its mode is CC-mode(Java),
  2689. syntactic information of LINE is used to find the enclosing (nested)
  2690. class string which is appended to the top level
  2691. class of the file (using s to separate nested class ids)."
  2692. ;; Convert f to a standard representation and remove suffix
  2693. (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
  2694. (save-match-data
  2695. (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
  2696. (fbuffer (get-file-buffer f))
  2697. class-found
  2698. ;; Syntax-symbol returns the symbol of the *first* element
  2699. ;; in the syntactical analysis result list, syntax-point
  2700. ;; returns the buffer position of same
  2701. (syntax-symbol (lambda (x) (c-langelem-sym (car x))))
  2702. (syntax-point (lambda (x) (c-langelem-pos (car x)))))
  2703. (setq f (file-name-sans-extension (file-truename f)))
  2704. ;; Search through classpath list for an entry that is
  2705. ;; contained in f
  2706. (while (and cplist (not class-found))
  2707. (if (string-match (car cplist) f)
  2708. (setq class-found
  2709. (mapconcat 'identity
  2710. (split-string
  2711. (substring f (+ (match-end 0) 1))
  2712. "/") ".")))
  2713. (setq cplist (cdr cplist)))
  2714. ;; if f is visited by a java(cc-mode) buffer, walk up the
  2715. ;; syntactic information chain and collect any 'inclass
  2716. ;; symbols until 'topmost-intro is reached to find out if
  2717. ;; point is within a nested class
  2718. ;; FIXME: Yuck!!! cc-mode should provide a function instead.
  2719. (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
  2720. (with-current-buffer fbuffer
  2721. (let ((nclass) (syntax))
  2722. ;; While the c-syntactic information does not start
  2723. ;; with the 'topmost-intro symbol, there may be
  2724. ;; nested classes...
  2725. (while (not (eq 'topmost-intro
  2726. (funcall syntax-symbol (c-guess-basic-syntax))))
  2727. ;; Check if the current position c-syntactic
  2728. ;; analysis has 'inclass
  2729. (setq syntax (c-guess-basic-syntax))
  2730. (while
  2731. (and (not (eq 'inclass (funcall syntax-symbol syntax)))
  2732. (cdr syntax))
  2733. (setq syntax (cdr syntax)))
  2734. (if (eq 'inclass (funcall syntax-symbol syntax))
  2735. (progn
  2736. (goto-char (funcall syntax-point syntax))
  2737. ;; Now we're at the beginning of a class
  2738. ;; definition. Find class name
  2739. (looking-at
  2740. "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
  2741. (setq nclass
  2742. (append (list (match-string-no-properties 1))
  2743. nclass)))
  2744. (setq syntax (c-guess-basic-syntax))
  2745. (while (and (not (funcall syntax-point syntax)) (cdr syntax))
  2746. (setq syntax (cdr syntax)))
  2747. (goto-char (funcall syntax-point syntax))
  2748. ))
  2749. (string-match (concat (car nclass) "$") class-found)
  2750. (setq class-found
  2751. (replace-match (mapconcat 'identity nclass "$")
  2752. t t class-found)))))
  2753. (if (not class-found)
  2754. (message "gud-find-class: class for file %s not found!" f))
  2755. class-found))
  2756. ;; Not using classpath - try class/source association list
  2757. (let ((class-found (rassoc f gud-jdb-class-source-alist)))
  2758. (if class-found
  2759. (car class-found)
  2760. (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
  2761. nil))))
  2762. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2763. ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2764. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2765. (defvar gdb-script-mode-syntax-table
  2766. (let ((st (make-syntax-table)))
  2767. (modify-syntax-entry ?' "\"" st)
  2768. (modify-syntax-entry ?# "<" st)
  2769. (modify-syntax-entry ?\n ">" st)
  2770. st))
  2771. (defvar gdb-script-font-lock-keywords
  2772. '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
  2773. ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
  2774. ("^\\s-*\\(\\w\\(\\w\\|\\s_\\)*\\)" (1 font-lock-keyword-face))))
  2775. (defconst gdb-script-syntax-propertize-function
  2776. (syntax-propertize-rules
  2777. ("^document\\s-.*\\(\n\\)" (1 "< b"))
  2778. ("^end\\(\\>\\)"
  2779. (1 (ignore
  2780. (when (and (> (match-beginning 0) (point-min))
  2781. (eq 1 (nth 7 (save-excursion
  2782. (syntax-ppss (1- (match-beginning 0)))))))
  2783. ;; We change the \n in front, which is more difficult, but results
  2784. ;; in better highlighting. If the doc is empty, the single \n is
  2785. ;; both the beginning and the end of the docstring, which can't be
  2786. ;; expressed in syntax-tables. Instead, we place the "> b" after
  2787. ;; placing the "< b", so the start marker is overwritten by the
  2788. ;; termination marker and in the end Emacs simply considers that
  2789. ;; there's no docstring at all, which is fine.
  2790. (put-text-property (1- (match-beginning 0)) (match-beginning 0)
  2791. 'syntax-table (eval-when-compile
  2792. (string-to-syntax "> b")))
  2793. ;; Make sure that rehighlighting the previous line won't erase our
  2794. ;; syntax-table property and that modifying `end' will.
  2795. (put-text-property (1- (match-beginning 0)) (match-end 0)
  2796. 'syntax-multiline t)))))))
  2797. (defun gdb-script-font-lock-syntactic-face (state)
  2798. (cond
  2799. ((nth 3 state) font-lock-string-face)
  2800. ((nth 7 state) font-lock-doc-face)
  2801. (t font-lock-comment-face)))
  2802. (defvar gdb-script-basic-indent 2)
  2803. (defun gdb-script-skip-to-head ()
  2804. "We're just in front of an `end' and we need to go to its head."
  2805. (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\|commands\\)\\>" nil 'move)
  2806. (match-end 2))
  2807. (gdb-script-skip-to-head)))
  2808. (defun gdb-script-calculate-indentation ()
  2809. (cond
  2810. ((looking-at "end\\>")
  2811. (gdb-script-skip-to-head)
  2812. (current-indentation))
  2813. ((looking-at "else\\>")
  2814. (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
  2815. (match-end 2))
  2816. (gdb-script-skip-to-head))
  2817. (current-indentation))
  2818. (t
  2819. (forward-comment (- (point-max)))
  2820. (forward-line 0)
  2821. (skip-chars-forward " \t")
  2822. (+ (current-indentation)
  2823. (if (looking-at "\\(if\\|while\\|define\\|else\\|commands\\)\\>")
  2824. gdb-script-basic-indent 0)))))
  2825. (defun gdb-script-indent-line ()
  2826. "Indent current line of GDB script."
  2827. (interactive)
  2828. (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
  2829. (save-excursion
  2830. (forward-line 0)
  2831. (skip-chars-forward " \t")
  2832. (not (looking-at "end\\>"))))
  2833. 'noindent
  2834. (let* ((savep (point))
  2835. (indent (condition-case nil
  2836. (save-excursion
  2837. (forward-line 0)
  2838. (skip-chars-forward " \t")
  2839. (if (>= (point) savep) (setq savep nil))
  2840. (max (gdb-script-calculate-indentation) 0))
  2841. (error 0))))
  2842. (if savep
  2843. (save-excursion (indent-line-to indent))
  2844. (indent-line-to indent)))))
  2845. ;; Derived from cfengine.el.
  2846. (defun gdb-script-beginning-of-defun ()
  2847. "`beginning-of-defun' function for Gdb script mode.
  2848. Treats actions as defuns."
  2849. (unless (<= (current-column) (current-indentation))
  2850. (end-of-line))
  2851. (if (re-search-backward "^define \\|^document " nil t)
  2852. (beginning-of-line)
  2853. (goto-char (point-min)))
  2854. t)
  2855. ;; Derived from cfengine.el.
  2856. (defun gdb-script-end-of-defun ()
  2857. "`end-of-defun' function for Gdb script mode.
  2858. Treats actions as defuns."
  2859. (end-of-line)
  2860. (if (re-search-forward "^end" nil t)
  2861. (beginning-of-line)
  2862. (goto-char (point-max)))
  2863. t)
  2864. ;;;###autoload
  2865. (define-derived-mode gdb-script-mode prog-mode "GDB-Script"
  2866. "Major mode for editing GDB scripts."
  2867. (set (make-local-variable 'comment-start) "#")
  2868. (set (make-local-variable 'comment-start-skip) "#+\\s-*")
  2869. (set (make-local-variable 'outline-regexp) "[ \t]")
  2870. (set (make-local-variable 'imenu-generic-expression)
  2871. '((nil "^define[ \t]+\\(\\w+\\)" 1)))
  2872. (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
  2873. (set (make-local-variable 'beginning-of-defun-function)
  2874. #'gdb-script-beginning-of-defun)
  2875. (set (make-local-variable 'end-of-defun-function)
  2876. #'gdb-script-end-of-defun)
  2877. (set (make-local-variable 'font-lock-defaults)
  2878. '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
  2879. (font-lock-syntactic-face-function
  2880. . gdb-script-font-lock-syntactic-face)))
  2881. ;; Recognize docstrings.
  2882. (set (make-local-variable 'syntax-propertize-function)
  2883. gdb-script-syntax-propertize-function)
  2884. (add-hook 'syntax-propertize-extend-region-functions
  2885. #'syntax-propertize-multiline 'append 'local))
  2886. ;;; tooltips for GUD
  2887. ;;; Customizable settings
  2888. ;;;###autoload
  2889. (define-minor-mode gud-tooltip-mode
  2890. "Toggle the display of GUD tooltips.
  2891. With a prefix argument ARG, enable the feature if ARG is
  2892. positive, and disable it otherwise. If called from Lisp, enable
  2893. it if ARG is omitted or nil."
  2894. :global t
  2895. :group 'gud
  2896. :group 'tooltip
  2897. (require 'tooltip)
  2898. (if gud-tooltip-mode
  2899. (progn
  2900. (add-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
  2901. (add-hook 'pre-command-hook 'tooltip-hide)
  2902. (add-hook 'tooltip-functions 'gud-tooltip-tips)
  2903. (define-key global-map [mouse-movement] 'gud-tooltip-mouse-motion))
  2904. (unless tooltip-mode (remove-hook 'pre-command-hook 'tooltip-hide)
  2905. (remove-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
  2906. (remove-hook 'tooltip-functions 'gud-tooltip-tips)
  2907. (define-key global-map [mouse-movement] 'ignore)))
  2908. (gud-tooltip-activate-mouse-motions-if-enabled)
  2909. (if (and gud-comint-buffer
  2910. (buffer-name gud-comint-buffer); gud-comint-buffer might be killed
  2911. (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  2912. 'gdbmi))
  2913. (if gud-tooltip-mode
  2914. (progn
  2915. (dolist (buffer (buffer-list))
  2916. (unless (eq buffer gud-comint-buffer)
  2917. (with-current-buffer buffer
  2918. (when (and (eq gud-minor-mode 'gdbmi)
  2919. (not (string-match "\\`\\*.+\\*\\'"
  2920. (buffer-name))))
  2921. (make-local-variable 'gdb-define-alist)
  2922. (gdb-create-define-alist)
  2923. (add-hook 'after-save-hook
  2924. 'gdb-create-define-alist nil t))))))
  2925. (kill-local-variable 'gdb-define-alist)
  2926. (remove-hook 'after-save-hook 'gdb-create-define-alist t))))
  2927. (define-obsolete-variable-alias 'tooltip-gud-modes
  2928. 'gud-tooltip-modes "22.1")
  2929. (defcustom gud-tooltip-modes '(gud-mode c-mode c++-mode fortran-mode
  2930. python-mode)
  2931. "List of modes for which to enable GUD tooltips."
  2932. :type 'sexp
  2933. :group 'gud
  2934. :group 'tooltip)
  2935. (define-obsolete-variable-alias 'tooltip-gud-display
  2936. 'gud-tooltip-display "22.1")
  2937. (defcustom gud-tooltip-display
  2938. '((eq (tooltip-event-buffer gud-tooltip-event)
  2939. (marker-buffer gud-overlay-arrow-position)))
  2940. "List of forms determining where GUD tooltips are displayed.
  2941. Forms in the list are combined with AND. The default is to display
  2942. only tooltips in the buffer containing the overlay arrow."
  2943. :type 'sexp
  2944. :group 'gud
  2945. :group 'tooltip)
  2946. (defcustom gud-tooltip-echo-area nil
  2947. "Use the echo area instead of frames for GUD tooltips."
  2948. :type 'boolean
  2949. :group 'gud
  2950. :group 'tooltip)
  2951. ;;; Reacting on mouse movements
  2952. (defun gud-tooltip-change-major-mode ()
  2953. "Function added to `change-major-mode-hook' when tooltip mode is on."
  2954. (add-hook 'post-command-hook 'gud-tooltip-activate-mouse-motions-if-enabled))
  2955. (defun gud-tooltip-activate-mouse-motions-if-enabled ()
  2956. "Reconsider for all buffers whether mouse motion events are desired."
  2957. (remove-hook 'post-command-hook
  2958. 'gud-tooltip-activate-mouse-motions-if-enabled)
  2959. (dolist (buffer (buffer-list))
  2960. (with-current-buffer buffer
  2961. (if (and gud-tooltip-mode
  2962. (memq major-mode gud-tooltip-modes))
  2963. (gud-tooltip-activate-mouse-motions t)
  2964. (gud-tooltip-activate-mouse-motions nil)))))
  2965. (defvar gud-tooltip-mouse-motions-active nil
  2966. "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
  2967. ;; We don't set track-mouse globally because this is a big redisplay
  2968. ;; problem in buffers having a pre-command-hook or such installed,
  2969. ;; which does a set-buffer, like the summary buffer of Gnus. Calling
  2970. ;; set-buffer prevents redisplay optimizations, so every mouse motion
  2971. ;; would be accompanied by a full redisplay.
  2972. (defun gud-tooltip-activate-mouse-motions (activatep)
  2973. "Activate/deactivate mouse motion events for the current buffer.
  2974. ACTIVATEP non-nil means activate mouse motion events."
  2975. (if activatep
  2976. (progn
  2977. (set (make-local-variable 'gud-tooltip-mouse-motions-active) t)
  2978. (set (make-local-variable 'track-mouse) t))
  2979. (when gud-tooltip-mouse-motions-active
  2980. (kill-local-variable 'gud-tooltip-mouse-motions-active)
  2981. (kill-local-variable 'track-mouse))))
  2982. (defvar tooltip-last-mouse-motion-event)
  2983. (declare-function tooltip-hide "tooltip" (&optional ignored-arg))
  2984. (declare-function tooltip-start-delayed-tip "tooltip" ())
  2985. (defun gud-tooltip-mouse-motion (event)
  2986. "Command handler for mouse movement events in `global-map'."
  2987. (interactive "e")
  2988. (tooltip-hide)
  2989. (when (car (mouse-pixel-position))
  2990. (setq tooltip-last-mouse-motion-event (copy-sequence event))
  2991. (tooltip-start-delayed-tip)))
  2992. ;;; Tips for `gud'
  2993. (defvar gud-tooltip-original-filter nil
  2994. "Process filter to restore after GUD output has been received.")
  2995. (defvar gud-tooltip-dereference nil
  2996. "Non-nil means print expressions with a `*' in front of them.
  2997. For C this would dereference a pointer expression.")
  2998. (defvar gud-tooltip-event nil
  2999. "The mouse movement event that led to a tooltip display.
  3000. This event can be examined by forms in `gud-tooltip-display'.")
  3001. (defun gud-tooltip-dereference (&optional arg)
  3002. "Toggle whether tooltips should show `* expr' or `expr'.
  3003. With arg, dereference expr if ARG is positive, otherwise do not dereference."
  3004. (interactive "P")
  3005. (setq gud-tooltip-dereference
  3006. (if (null arg)
  3007. (not gud-tooltip-dereference)
  3008. (> (prefix-numeric-value arg) 0)))
  3009. (message "Dereferencing is now %s."
  3010. (if gud-tooltip-dereference "on" "off")))
  3011. (define-obsolete-function-alias 'tooltip-gud-toggle-dereference
  3012. 'gud-tooltip-dereference "22.1")
  3013. (defvar tooltip-use-echo-area)
  3014. (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
  3015. (declare-function tooltip-strip-prompt "tooltip" (process output))
  3016. ; This will only display data that comes in one chunk.
  3017. ; Larger arrays (say 400 elements) are displayed in
  3018. ; the tooltip incompletely and spill over into the gud buffer.
  3019. ; Switching the process-filter creates timing problems and
  3020. ; it may be difficult to do better. Using GDB/MI as in
  3021. ; gdb-mi.el gets round this problem.
  3022. (defun gud-tooltip-process-output (process output)
  3023. "Process debugger output and show it in a tooltip window."
  3024. (set-process-filter process gud-tooltip-original-filter)
  3025. (tooltip-show (tooltip-strip-prompt process output)
  3026. (or gud-tooltip-echo-area tooltip-use-echo-area)))
  3027. (defun gud-tooltip-print-command (expr)
  3028. "Return a suitable command to print the expression EXPR."
  3029. (case gud-minor-mode
  3030. (gdbmi (concat "-data-evaluate-expression " expr))
  3031. (dbx (concat "print " expr))
  3032. ((xdb pdb) (concat "p " expr))
  3033. (sdb (concat expr "/"))))
  3034. (declare-function gdb-input "gdb-mi" (command handler))
  3035. (declare-function tooltip-expr-to-print "tooltip" (event))
  3036. (declare-function tooltip-event-buffer "tooltip" (event))
  3037. (defun gud-tooltip-tips (event)
  3038. "Show tip for identifier or selection under the mouse.
  3039. The mouse must either point at an identifier or inside a selected
  3040. region for the tip window to be shown. If `gud-tooltip-dereference' is t,
  3041. add a `*' in front of the printed expression. In the case of a C program
  3042. controlled by GDB, show the associated #define directives when program is
  3043. not executing.
  3044. This function must return nil if it doesn't handle EVENT."
  3045. (let (process)
  3046. (when (and (eventp event)
  3047. gud-tooltip-mode
  3048. gud-comint-buffer
  3049. (buffer-name gud-comint-buffer); might be killed
  3050. (setq process (get-buffer-process gud-comint-buffer))
  3051. (posn-point (event-end event))
  3052. (or (and (eq gud-minor-mode 'gdbmi) (not gdb-active-process))
  3053. (progn (setq gud-tooltip-event event)
  3054. (eval (cons 'and gud-tooltip-display)))))
  3055. (let ((expr (tooltip-expr-to-print event)))
  3056. (when expr
  3057. (if (and (eq gud-minor-mode 'gdbmi)
  3058. (not gdb-active-process))
  3059. (progn
  3060. (with-current-buffer (tooltip-event-buffer event)
  3061. (let ((define-elt (assoc expr gdb-define-alist)))
  3062. (unless (null define-elt)
  3063. (tooltip-show
  3064. (cdr define-elt)
  3065. (or gud-tooltip-echo-area tooltip-use-echo-area))
  3066. expr))))
  3067. (when gud-tooltip-dereference
  3068. (setq expr (concat "*" expr)))
  3069. (let ((cmd (gud-tooltip-print-command expr)))
  3070. (when (and gud-tooltip-mode (eq gud-minor-mode 'gdb))
  3071. (gud-tooltip-mode -1)
  3072. (message-box "Using GUD tooltips in this mode is unsafe\n\
  3073. so they have been disabled."))
  3074. (unless (null cmd) ; CMD can be nil if unknown debugger
  3075. (if (eq gud-minor-mode 'gdbmi)
  3076. (if gdb-macro-info
  3077. (gdb-input
  3078. (concat
  3079. "server macro expand " expr "\n")
  3080. `(lambda () (gdb-tooltip-print-1 ,expr)))
  3081. (gdb-input
  3082. (concat cmd "\n")
  3083. `(lambda () (gdb-tooltip-print ,expr))))
  3084. (setq gud-tooltip-original-filter (process-filter process))
  3085. (set-process-filter process 'gud-tooltip-process-output)
  3086. (gud-basic-call cmd))
  3087. expr))))))))
  3088. (provide 'gud)
  3089. ;;; gud.el ends here