gud.el 131 KB

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