gdb-mi.el 177 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784
  1. ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. ;; Author: Nick Roberts <nickrob@gnu.org>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: unix, tools
  6. ;; This file is part of GNU Emacs.
  7. ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Credits:
  19. ;; This file was written by Nick Roberts following the general design
  20. ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It was further developed
  21. ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
  22. ;; of Code 2009 Project "Emacs GDB/MI migration".
  23. ;;; Commentary:
  24. ;; This mode acts as a graphical user interface to GDB. You can interact with
  25. ;; GDB through the GUD buffer in the usual way, but there are also further
  26. ;; buffers which control the execution and describe the state of your program.
  27. ;; It separates the input/output of your program from that of GDB and displays
  28. ;; expressions and their current values in their own buffers. It also uses
  29. ;; features of Emacs 21 such as the fringe/display margin for breakpoints, and
  30. ;; the toolbar (see the GDB Graphical Interface section in the Emacs info
  31. ;; manual).
  32. ;; M-x gdb will start the debugger.
  33. ;; This file uses GDB/MI as the primary interface to GDB. It runs gdb with
  34. ;; GDB/MI (-interp=mi) and access CLI using "-interpreter-exec console
  35. ;; cli-command". This code replaces gdb-ui.el and uses MI tokens instead
  36. ;; of queues. Eventually MI should be asynchronous.
  37. ;; Windows Platforms:
  38. ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
  39. ;; explicitly in your program if you want timely display of I/O in Emacs.
  40. ;; Alternatively you can make the output stream unbuffered, for example, by
  41. ;; using a macro:
  42. ;; #ifdef UNBUFFERED
  43. ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
  44. ;; #endif
  45. ;; and compiling with -DUNBUFFERED while debugging.
  46. ;; If you are using Cygwin GDB and find that the source is not being displayed
  47. ;; in Emacs when you step through it, possible solutions are to:
  48. ;; 1) Use Cygwin X Windows and Cygwin Emacs.
  49. ;; (Since 22.1 Emacs builds under Cygwin.)
  50. ;; 2) Use MinGW GDB instead.
  51. ;; 3) Use cygwin-mount.el
  52. ;;; Mac OSX:
  53. ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
  54. ;; some changes to the version that they include as part of Mac OSX.
  55. ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
  56. ;; as earlier versions do not compile on Mac OSX.
  57. ;;; Known Bugs:
  58. ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
  59. ;; line information, e.g., a routine in libc (just a TODO item).
  60. ;; TODO:
  61. ;; 2) Watch windows to work with threads.
  62. ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
  63. ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
  64. ;;; Code:
  65. (require 'gud)
  66. (require 'json)
  67. (require 'bindat)
  68. (require 'cl-lib)
  69. (declare-function speedbar-change-initial-expansion-list
  70. "speedbar" (new-default))
  71. (declare-function speedbar-timer-fn "speedbar" ())
  72. (declare-function speedbar-line-text "speedbar" (&optional p))
  73. (declare-function speedbar-change-expand-button-char "speedbar" (char))
  74. (declare-function speedbar-delete-subblock "speedbar" (indent))
  75. (declare-function speedbar-center-buffer-smartly "speedbar" ())
  76. (defvar tool-bar-map)
  77. (defvar speedbar-initial-expansion-list-name)
  78. (defvar speedbar-frame)
  79. (defvar gdb-memory-address "main")
  80. (defvar gdb-memory-last-address nil
  81. "Last successfully accessed memory address.")
  82. (defvar gdb-memory-next-page nil
  83. "Address of next memory page for program memory buffer.")
  84. (defvar gdb-memory-prev-page nil
  85. "Address of previous memory page for program memory buffer.")
  86. (defvar gdb-thread-number nil
  87. "Main current thread.
  88. Invalidation triggers use this variable to query GDB for
  89. information on the specified thread by wrapping GDB/MI commands
  90. in `gdb-current-context-command'.
  91. This variable may be updated implicitly by GDB via `gdb-stopped'
  92. or explicitly by `gdb-select-thread'.
  93. Only `gdb-setq-thread-number' should be used to change this
  94. value.")
  95. (defvar gdb-frame-number nil
  96. "Selected frame level for main current thread.
  97. Updated according to the following rules:
  98. When a thread is selected or current thread stops, set to \"0\".
  99. When current thread goes running (and possibly exits eventually),
  100. set to nil.
  101. May be manually changed by user with `gdb-select-frame'.")
  102. (defvar gdb-frame-address nil "Identity of frame for watch expression.")
  103. ;; Used to show overlay arrow in source buffer. All set in
  104. ;; gdb-get-main-selected-frame. Disassembly buffer should not use
  105. ;; these but rely on buffer-local thread information instead.
  106. (defvar gdb-selected-frame nil
  107. "Name of selected function for main current thread.")
  108. (defvar gdb-selected-file nil
  109. "Name of selected file for main current thread.")
  110. (defvar gdb-selected-line nil
  111. "Number of selected line for main current thread.")
  112. (defvar gdb-threads-list nil
  113. "Associative list of threads provided by \"-thread-info\" MI command.
  114. Keys are thread numbers (in strings) and values are structures as
  115. returned from -thread-info by `gdb-json-partial-output'. Updated in
  116. `gdb-thread-list-handler-custom'.")
  117. (defvar gdb-running-threads-count nil
  118. "Number of currently running threads.
  119. If nil, no information is available.
  120. Updated in `gdb-thread-list-handler-custom'.")
  121. (defvar gdb-stopped-threads-count nil
  122. "Number of currently stopped threads.
  123. See also `gdb-running-threads-count'.")
  124. (defvar gdb-breakpoints-list nil
  125. "Associative list of breakpoints provided by \"-break-list\" MI command.
  126. Keys are breakpoint numbers (in string) and values are structures
  127. as returned from \"-break-list\" by `gdb-json-partial-output'
  128. \(\"body\" field is used). Updated in
  129. `gdb-breakpoints-list-handler-custom'.")
  130. (defvar gdb-current-language nil)
  131. (defvar gdb-var-list nil
  132. "List of variables in watch window.
  133. Each element has the form
  134. (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
  135. where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
  136. address for root variables.")
  137. (defvar gdb-main-file nil "Source file from which program execution begins.")
  138. ;; Overlay arrow markers
  139. (defvar gdb-stack-position nil)
  140. (defvar gdb-thread-position nil)
  141. (defvar gdb-disassembly-position nil)
  142. (defvar gdb-location-alist nil
  143. "Alist of breakpoint numbers and full filenames.
  144. Only used for files that Emacs can't find.")
  145. (defvar gdb-active-process nil
  146. "GUD tooltips display variable values when t, and macro definitions otherwise.")
  147. (defvar gdb-error "Non-nil when GDB is reporting an error.")
  148. (defvar gdb-macro-info nil
  149. "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
  150. (defvar gdb-register-names nil "List of register names.")
  151. (defvar gdb-changed-registers nil
  152. "List of changed register numbers (strings).")
  153. (defvar gdb-buffer-fringe-width nil)
  154. (defvar gdb-last-command nil)
  155. (defvar gdb-prompt-name nil)
  156. (defvar gdb-token-number 0)
  157. (defvar gdb-handler-list '()
  158. "List of gdb-handler keeping track of all pending GDB commands.")
  159. (defvar gdb-source-file-list nil
  160. "List of source files for the current executable.")
  161. (defvar gdb-first-done-or-error t)
  162. (defvar gdb-source-window nil)
  163. (defvar gdb-inferior-status nil)
  164. (defvar gdb-continuation nil)
  165. (defvar gdb-supports-non-stop nil)
  166. (defvar gdb-filter-output nil
  167. "Message to be shown in GUD console.
  168. This variable is updated in `gdb-done-or-error' and returned by
  169. `gud-gdbmi-marker-filter'.")
  170. (defvar gdb-non-stop nil
  171. "Indicates whether current GDB session is using non-stop mode.
  172. It is initialized to `gdb-non-stop-setting' at the beginning of
  173. every GDB session.")
  174. (defvar-local gdb-buffer-type nil
  175. "One of the symbols bound in `gdb-buffer-rules'.")
  176. (defvar gdb-output-sink 'nil
  177. "The disposition of the output of the current gdb command.
  178. Possible values are these symbols:
  179. `user' -- gdb output should be copied to the GUD buffer
  180. for the user to see.
  181. `emacs' -- output should be collected in the partial-output-buffer
  182. for subsequent processing by a command. This is the
  183. disposition of output generated by commands that
  184. gdb mode sends to gdb on its own behalf.")
  185. (defcustom gdb-discard-unordered-replies t
  186. "Non-nil means discard any out-of-order GDB replies.
  187. This protects against lost GDB replies, assuming that GDB always
  188. replies in the same order as Emacs sends commands. When receiving a
  189. reply with a given token-number, assume any pending messages with a
  190. lower token-number are out-of-order."
  191. :type 'boolean
  192. :group 'gud
  193. :version "24.4")
  194. (cl-defstruct gdb-handler
  195. "Data required to handle the reply of a command sent to GDB."
  196. ;; Prefix of the command sent to GDB. The GDB reply for this command
  197. ;; will be prefixed with this same TOKEN-NUMBER
  198. (token-number nil :read-only t)
  199. ;; Callback to invoke when the reply is received from GDB
  200. (function nil :read-only t)
  201. ;; PENDING-TRIGGER is used to prevent congestion: Emacs won't send
  202. ;; two requests with the same PENDING-TRIGGER until a reply is received
  203. ;; for the first one."
  204. (pending-trigger nil))
  205. (defun gdb-add-handler (token-number handler-function &optional pending-trigger)
  206. "Insert a new GDB command handler in `gdb-handler-list'.
  207. Handlers are used to keep track of the commands sent to GDB
  208. and to handle the replies received.
  209. Upon reception of a reply prefixed with TOKEN-NUMBER,
  210. invoke the callback HANDLER-FUNCTION.
  211. If PENDING-TRIGGER is specified, no new GDB commands will be
  212. sent with this same PENDING-TRIGGER until a reply is received
  213. for this handler."
  214. (push (make-gdb-handler :token-number token-number
  215. :function handler-function
  216. :pending-trigger pending-trigger)
  217. gdb-handler-list))
  218. (defun gdb-delete-handler (token-number)
  219. "Remove the handler TOKEN-NUMBER from `gdb-handler-list'.
  220. Additionally, if `gdb-discard-unordered-replies' is non-nil,
  221. discard all handlers having a token number less than TOKEN-NUMBER."
  222. (if gdb-discard-unordered-replies
  223. (setq gdb-handler-list
  224. (cl-delete-if
  225. (lambda (handler)
  226. "Discard any HANDLER with a token number `<=' than TOKEN-NUMBER."
  227. (when (< (gdb-handler-token-number handler) token-number)
  228. (message "WARNING! Discarding GDB handler with token #%d\n"
  229. (gdb-handler-token-number handler)))
  230. (<= (gdb-handler-token-number handler) token-number))
  231. gdb-handler-list))
  232. (setq gdb-handler-list
  233. (cl-delete-if
  234. (lambda (handler)
  235. "Discard any HANDLER with a token number `eq' to TOKEN-NUMBER."
  236. (eq (gdb-handler-token-number handler) token-number))
  237. gdb-handler-list))))
  238. (defun gdb-get-handler-function (token-number)
  239. "Return the function callback registered with the handler TOKEN-NUMBER."
  240. (gdb-handler-function
  241. (cl-find-if (lambda (handler) (eq (gdb-handler-token-number handler)
  242. token-number))
  243. gdb-handler-list)))
  244. (defun gdb-pending-handler-p (pending-trigger)
  245. "Return non-nil if a command handler is pending with trigger PENDING-TRIGGER."
  246. (cl-find-if (lambda (handler) (eq (gdb-handler-pending-trigger handler)
  247. pending-trigger))
  248. gdb-handler-list))
  249. (defun gdb-handle-reply (token-number)
  250. "Handle the GDB reply TOKEN-NUMBER.
  251. This invokes the handler registered with this token number
  252. in `gdb-handler-list' and clears all pending handlers invalidated
  253. by the reception of this reply."
  254. (let ((handler-function (gdb-get-handler-function token-number)))
  255. (when handler-function
  256. (funcall handler-function)
  257. (gdb-delete-handler token-number))))
  258. (defun gdb-remove-all-pending-triggers ()
  259. "Remove all pending triggers from gdb-handler-list.
  260. The handlers are left in gdb-handler-list so that replies received
  261. from GDB could still be handled. However, removing the pending triggers
  262. allows Emacs to send new commands even if replies of previous commands
  263. were not yet received."
  264. (dolist (handler gdb-handler-list)
  265. (setf (gdb-handler-pending-trigger handler) nil)))
  266. (defmacro gdb-wait-for-pending (&rest body)
  267. "Wait for all pending GDB commands to finish and evaluate BODY.
  268. This function checks every 0.5 seconds if there are any pending
  269. triggers in `gdb-handler-list'."
  270. `(run-with-timer
  271. 0.5 nil
  272. '(lambda ()
  273. (if (not (cl-find-if (lambda (handler)
  274. (gdb-handler-pending-trigger handler))
  275. gdb-handler-list))
  276. (progn ,@body)
  277. (gdb-wait-for-pending ,@body)))))
  278. ;; Publish-subscribe
  279. (defmacro gdb-add-subscriber (publisher subscriber)
  280. "Register new PUBLISHER's SUBSCRIBER.
  281. SUBSCRIBER must be a pair, where cdr is a function of one
  282. argument (see `gdb-emit-signal')."
  283. `(add-to-list ',publisher ,subscriber t))
  284. (defmacro gdb-delete-subscriber (publisher subscriber)
  285. "Unregister SUBSCRIBER from PUBLISHER."
  286. `(setq ,publisher (delete ,subscriber
  287. ,publisher)))
  288. (defun gdb-get-subscribers (publisher)
  289. publisher)
  290. (defun gdb-emit-signal (publisher &optional signal)
  291. "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
  292. (dolist (subscriber (gdb-get-subscribers publisher))
  293. (funcall (cdr subscriber) signal)))
  294. (defvar gdb-buf-publisher '()
  295. "Used to invalidate GDB buffers by emitting a signal in `gdb-update'.
  296. Must be a list of pairs with cars being buffers and cdr's being
  297. valid signal handlers.")
  298. (defgroup gdb nil
  299. "GDB graphical interface"
  300. :group 'tools
  301. :link '(info-link "(emacs)GDB Graphical Interface")
  302. :version "23.2")
  303. (defgroup gdb-non-stop nil
  304. "GDB non-stop debugging settings"
  305. :group 'gdb
  306. :version "23.2")
  307. (defgroup gdb-buffers nil
  308. "GDB buffers"
  309. :group 'gdb
  310. :version "23.2")
  311. (defcustom gdb-debug-log-max 128
  312. "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
  313. :group 'gdb
  314. :type '(choice (integer :tag "Number of elements")
  315. (const :tag "Unlimited" nil))
  316. :version "22.1")
  317. (defcustom gdb-non-stop-setting t
  318. "When in non-stop mode, stopped threads can be examined while
  319. other threads continue to execute.
  320. GDB session needs to be restarted for this setting to take effect."
  321. :type 'boolean
  322. :group 'gdb-non-stop
  323. :version "23.2")
  324. ;; TODO Some commands can't be called with --all (give a notice about
  325. ;; it in setting doc)
  326. (defcustom gdb-gud-control-all-threads t
  327. "When non-nil, GUD execution commands affect all threads when
  328. in non-stop mode. Otherwise, only current thread is affected."
  329. :type 'boolean
  330. :group 'gdb-non-stop
  331. :version "23.2")
  332. (defcustom gdb-switch-reasons t
  333. "List of stop reasons for which Emacs should switch thread.
  334. When t, switch to stopped thread no matter what the reason was.
  335. When nil, never switch to stopped thread automatically.
  336. This setting is used in non-stop mode only. In all-stop mode,
  337. Emacs always switches to the thread which caused the stop."
  338. ;; exited, exited-normally and exited-signaled are not
  339. ;; thread-specific stop reasons and therefore are not included in
  340. ;; this list
  341. :type '(choice
  342. (const :tag "All reasons" t)
  343. (set :tag "Selection of reasons..."
  344. (const :tag "A breakpoint was reached." "breakpoint-hit")
  345. (const :tag "A watchpoint was triggered." "watchpoint-trigger")
  346. (const :tag "A read watchpoint was triggered."
  347. "read-watchpoint-trigger")
  348. (const :tag "An access watchpoint was triggered."
  349. "access-watchpoint-trigger")
  350. (const :tag "Function finished execution." "function-finished")
  351. (const :tag "Location reached." "location-reached")
  352. (const :tag "Watchpoint has gone out of scope"
  353. "watchpoint-scope")
  354. (const :tag "End of stepping range reached."
  355. "end-stepping-range")
  356. (const :tag "Signal received (like interruption)."
  357. "signal-received"))
  358. (const :tag "None" nil))
  359. :group 'gdb-non-stop
  360. :version "23.2"
  361. :link '(info-link "(gdb)GDB/MI Async Records"))
  362. (defcustom gdb-stopped-functions nil
  363. "List of functions called whenever GDB stops.
  364. Each function takes one argument, a parsed MI response, which
  365. contains fields of corresponding MI *stopped async record:
  366. ((stopped-threads . \"all\")
  367. (thread-id . \"1\")
  368. (frame (line . \"38\")
  369. (fullname . \"/home/sphinx/projects/gsoc/server.c\")
  370. (file . \"server.c\")
  371. (args ((value . \"0x804b038\")
  372. (name . \"arg\")))
  373. (func . \"hello\")
  374. (addr . \"0x0804869e\"))
  375. (reason . \"end-stepping-range\"))
  376. Note that \"reason\" is only present in non-stop debugging mode.
  377. `bindat-get-field' may be used to access the fields of response.
  378. Each function is called after the new current thread was selected
  379. and GDB buffers were updated in `gdb-stopped'."
  380. :type '(repeat function)
  381. :group 'gdb
  382. :version "23.2"
  383. :link '(info-link "(gdb)GDB/MI Async Records"))
  384. (defcustom gdb-switch-when-another-stopped t
  385. "When nil, don't switch to stopped thread if some other
  386. stopped thread is already selected."
  387. :type 'boolean
  388. :group 'gdb-non-stop
  389. :version "23.2")
  390. (defcustom gdb-stack-buffer-locations t
  391. "Show file information or library names in stack buffers."
  392. :type 'boolean
  393. :group 'gdb-buffers
  394. :version "23.2")
  395. (defcustom gdb-stack-buffer-addresses nil
  396. "Show frame addresses in stack buffers."
  397. :type 'boolean
  398. :group 'gdb-buffers
  399. :version "23.2")
  400. (defcustom gdb-thread-buffer-verbose-names t
  401. "Show long thread names in threads buffer."
  402. :type 'boolean
  403. :group 'gdb-buffers
  404. :version "23.2")
  405. (defcustom gdb-thread-buffer-arguments t
  406. "Show function arguments in threads buffer."
  407. :type 'boolean
  408. :group 'gdb-buffers
  409. :version "23.2")
  410. (defcustom gdb-thread-buffer-locations t
  411. "Show file information or library names in threads buffer."
  412. :type 'boolean
  413. :group 'gdb-buffers
  414. :version "23.2")
  415. (defcustom gdb-thread-buffer-addresses nil
  416. "Show addresses for thread frames in threads buffer."
  417. :type 'boolean
  418. :group 'gdb-buffers
  419. :version "23.2")
  420. (defcustom gdb-show-threads-by-default nil
  421. "Show threads list buffer instead of breakpoints list by default."
  422. :type 'boolean
  423. :group 'gdb-buffers
  424. :version "23.2")
  425. (defvar gdb-debug-log nil
  426. "List of commands sent to and replies received from GDB.
  427. Most recent commands are listed first. This list stores only the last
  428. `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
  429. ;;;###autoload
  430. (define-minor-mode gdb-enable-debug
  431. "Toggle logging of transaction between Emacs and Gdb.
  432. The log is stored in `gdb-debug-log' as an alist with elements
  433. whose cons is send, send-item or recv and whose cdr is the string
  434. being transferred. This list may grow up to a size of
  435. `gdb-debug-log-max' after which the oldest element (at the end of
  436. the list) is deleted every time a new one is added (at the front)."
  437. :global t
  438. :group 'gdb
  439. :version "22.1")
  440. (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
  441. "Shell command for generating a list of defined macros in a source file.
  442. This list is used to display the #define directive associated
  443. with an identifier as a tooltip. It works in a debug session with
  444. GDB, when `gud-tooltip-mode' is t.
  445. Set `gdb-cpp-define-alist-flags' for any include paths or
  446. predefined macros."
  447. :type 'string
  448. :group 'gdb
  449. :version "22.1")
  450. (defcustom gdb-cpp-define-alist-flags ""
  451. "Preprocessor flags for `gdb-cpp-define-alist-program'."
  452. :type 'string
  453. :group 'gdb
  454. :version "22.1")
  455. (defcustom gdb-create-source-file-list t
  456. "Non-nil means create a list of files from which the executable was built.
  457. Set this to nil if the GUD buffer displays \"initializing...\" in the mode
  458. line for a long time when starting, possibly because your executable was
  459. built from a large number of files. This allows quicker initialization
  460. but means that these files are not automatically enabled for debugging,
  461. e.g., you won't be able to click in the fringe to set a breakpoint until
  462. execution has already stopped there."
  463. :type 'boolean
  464. :group 'gdb
  465. :version "23.1")
  466. (defcustom gdb-show-main nil
  467. "Non-nil means display source file containing the main routine at startup.
  468. Also display the main routine in the disassembly buffer if present."
  469. :type 'boolean
  470. :group 'gdb
  471. :version "22.1")
  472. (defvar gdbmi-debug-mode nil
  473. "When non-nil, print the messages sent/received from GDB/MI in *Messages*.")
  474. (defun gdb-force-mode-line-update (status)
  475. (let ((buffer gud-comint-buffer))
  476. (if (and buffer (buffer-name buffer))
  477. (with-current-buffer buffer
  478. (setq mode-line-process
  479. (format ":%s [%s]"
  480. (process-status (get-buffer-process buffer)) status))
  481. ;; Force mode line redisplay soon.
  482. (force-mode-line-update)))))
  483. ;; These two are used for menu and toolbar
  484. (defun gdb-control-all-threads ()
  485. "Switch to non-stop/A mode."
  486. (interactive)
  487. (setq gdb-gud-control-all-threads t)
  488. ;; Actually forcing the tool-bar to update.
  489. (force-mode-line-update)
  490. (message "Now in non-stop/A mode."))
  491. (defun gdb-control-current-thread ()
  492. "Switch to non-stop/T mode."
  493. (interactive)
  494. (setq gdb-gud-control-all-threads nil)
  495. ;; Actually forcing the tool-bar to update.
  496. (force-mode-line-update)
  497. (message "Now in non-stop/T mode."))
  498. (defun gdb-find-watch-expression ()
  499. (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
  500. (varnum (car var)) expr)
  501. (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
  502. (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
  503. (component-list (split-string (match-string 2 varnum) "\\." t)))
  504. (setq expr (nth 1 var1))
  505. (setq varnumlet (car var1))
  506. (dolist (component component-list)
  507. (setq var2 (assoc varnumlet gdb-var-list))
  508. (setq expr (concat expr
  509. (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
  510. (concat "[" component "]")
  511. (concat "." component))))
  512. (setq varnumlet (concat varnumlet "." component)))
  513. expr)))
  514. ;; noall is used for commands which don't take --all, but only
  515. ;; --thread.
  516. (defun gdb-gud-context-command (command &optional noall)
  517. "When `gdb-non-stop' is t, add --thread option to COMMAND if
  518. `gdb-gud-control-all-threads' is nil and --all option otherwise.
  519. If NOALL is t, always add --thread option no matter what
  520. `gdb-gud-control-all-threads' value is.
  521. When `gdb-non-stop' is nil, return COMMAND unchanged."
  522. (if gdb-non-stop
  523. (if (and gdb-gud-control-all-threads
  524. (not noall)
  525. gdb-supports-non-stop)
  526. (concat command " --all ")
  527. (gdb-current-context-command command))
  528. command))
  529. (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
  530. "`gud-call' wrapper which adds --thread/--all options between
  531. CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
  532. NOARG must be t when this macro is used outside `gud-def'"
  533. `(gud-call
  534. (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
  535. ,(when (not noarg) 'arg)))
  536. (defun gdb--check-interpreter (filter proc string)
  537. (unless (zerop (length string))
  538. (remove-function (process-filter proc) #'gdb--check-interpreter)
  539. (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
  540. ;; Apparently we're not running with -i=mi.
  541. (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
  542. (message msg)
  543. (setq string (concat (propertize msg 'font-lock-face 'error)
  544. "\n" string)))
  545. ;; Use the old gud-gbd filter, not because it works, but because it
  546. ;; will properly display GDB's answers rather than hanging waiting for
  547. ;; answers that aren't coming.
  548. (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
  549. (funcall filter proc string)))
  550. (defvar gdb-control-level 0)
  551. ;;;###autoload
  552. (defun gdb (command-line)
  553. "Run gdb on program FILE in buffer *gud-FILE*.
  554. The directory containing FILE becomes the initial working directory
  555. and source-file directory for your debugger.
  556. COMMAND-LINE is the shell command for starting the gdb session.
  557. It should be a string consisting of the name of the gdb
  558. executable followed by command line options. The command line
  559. options should include \"-i=mi\" to use gdb's MI text interface.
  560. Note that the old \"--annotate\" option is no longer supported.
  561. If option `gdb-many-windows' is nil (the default value) then gdb just
  562. pops up the GUD buffer unless `gdb-show-main' is t. In this case
  563. it starts with two windows: one displaying the GUD buffer and the
  564. other with the source file with the main routine of the inferior.
  565. If option `gdb-many-windows' is t, regardless of the value of
  566. `gdb-show-main', the layout below will appear. Keybindings are
  567. shown in some of the buffers.
  568. Watch expressions appear in the speedbar/slowbar.
  569. The following commands help control operation :
  570. `gdb-many-windows' - Toggle the number of windows gdb uses.
  571. `gdb-restore-windows' - To restore the window layout.
  572. See Info node `(emacs)GDB Graphical Interface' for a more
  573. detailed description of this mode.
  574. +----------------------------------------------------------------------+
  575. | GDB Toolbar |
  576. +-----------------------------------+----------------------------------+
  577. | GUD buffer (I/O of GDB) | Locals buffer |
  578. | | |
  579. | | |
  580. | | |
  581. +-----------------------------------+----------------------------------+
  582. | Source buffer | I/O buffer (of debugged program) |
  583. | | (comint-mode) |
  584. | | |
  585. | | |
  586. | | |
  587. | | |
  588. | | |
  589. | | |
  590. +-----------------------------------+----------------------------------+
  591. | Stack buffer | Breakpoints buffer |
  592. | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
  593. | | RET gdb-goto-breakpoint |
  594. | | D gdb-delete-breakpoint |
  595. +-----------------------------------+----------------------------------+"
  596. ;;
  597. (interactive (list (gud-query-cmdline 'gdb)))
  598. (when (and gud-comint-buffer
  599. (buffer-name gud-comint-buffer)
  600. (get-buffer-process gud-comint-buffer)
  601. (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
  602. (gdb-restore-windows)
  603. (error
  604. "Multiple debugging requires restarting in text command mode"))
  605. ;;
  606. (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
  607. ;; Setup a temporary process filter to warn when GDB was not started
  608. ;; with -i=mi.
  609. (let ((proc (get-buffer-process gud-comint-buffer)))
  610. (add-function :around (process-filter proc) #'gdb--check-interpreter))
  611. (set (make-local-variable 'gud-minor-mode) 'gdbmi)
  612. (set (make-local-variable 'gdb-control-level) 0)
  613. (setq comint-input-sender 'gdb-send)
  614. (when (ring-empty-p comint-input-ring) ; cf shell-mode
  615. (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
  616. (if (eq system-type 'ms-dos)
  617. "_gdb_history"
  618. ".gdb_history"))))
  619. ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
  620. (hsize (getenv "HISTSIZE")))
  621. (dolist (file (append '("~/.gdbinit")
  622. (unless (string-equal (expand-file-name ".")
  623. (expand-file-name "~"))
  624. '(".gdbinit"))))
  625. (if (file-readable-p (setq file (expand-file-name file)))
  626. (with-temp-buffer
  627. (insert-file-contents file)
  628. ;; TODO? check for "set history save\\( *on\\)?" and do
  629. ;; not use history otherwise?
  630. (while (re-search-forward
  631. "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
  632. (cond ((string-equal (match-string 1) "filename")
  633. (setq hfile (expand-file-name
  634. (match-string 2)
  635. (file-name-directory file))))
  636. ((string-equal (match-string 1) "size")
  637. (setq hsize (match-string 2))))))))
  638. (and (stringp hsize)
  639. (integerp (setq hsize (string-to-number hsize)))
  640. (> hsize 0)
  641. (set (make-local-variable 'comint-input-ring-size) hsize))
  642. (if (stringp hfile)
  643. (set (make-local-variable 'comint-input-ring-file-name) hfile))
  644. (comint-read-input-ring t)))
  645. (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
  646. "Set temporary breakpoint at current line.")
  647. (gud-def gud-jump
  648. (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
  649. "\C-j" "Set execution address to current line.")
  650. (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
  651. (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
  652. (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
  653. (gud-def gud-pstar "print* %e" nil
  654. "Evaluate C dereferenced pointer expression at point.")
  655. (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
  656. "\C-s"
  657. "Step one source line with display.")
  658. (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
  659. "\C-i"
  660. "Step one instruction with display.")
  661. (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
  662. "\C-n"
  663. "Step one line (skip functions).")
  664. (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
  665. nil
  666. "Step one instruction (skip functions).")
  667. (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
  668. "\C-r"
  669. "Continue with display.")
  670. (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
  671. "\C-f"
  672. "Finish executing current function.")
  673. (gud-def gud-run "-exec-run"
  674. nil
  675. "Run the program.")
  676. (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
  677. (gud-call "break %f:%l" arg)
  678. (save-excursion
  679. (beginning-of-line)
  680. (forward-char 2)
  681. (gud-call "break *%a" arg)))
  682. "\C-b" "Set breakpoint at current line or address.")
  683. (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
  684. (gud-call "clear %f:%l" arg)
  685. (save-excursion
  686. (beginning-of-line)
  687. (forward-char 2)
  688. (gud-call "clear *%a" arg)))
  689. "\C-d" "Remove breakpoint at current line or address.")
  690. ;; -exec-until doesn't support --all yet
  691. (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
  692. (gud-call "-exec-until %f:%l" arg)
  693. (save-excursion
  694. (beginning-of-line)
  695. (forward-char 2)
  696. (gud-call "-exec-until *%a" arg)))
  697. "\C-u" "Continue to current line or address.")
  698. ;; TODO Why arg here?
  699. (gud-def
  700. gud-go (gud-call (if gdb-active-process
  701. (gdb-gud-context-command "-exec-continue")
  702. "-exec-run") arg)
  703. nil "Start or continue execution.")
  704. ;; For debugging Emacs only.
  705. (gud-def gud-pp
  706. (gud-call
  707. (concat
  708. "pp " (if (eq (buffer-local-value
  709. 'major-mode (window-buffer)) 'speedbar-mode)
  710. (gdb-find-watch-expression) "%e")) arg)
  711. nil "Print the Emacs s-expression.")
  712. (define-key gud-minor-mode-map [left-margin mouse-1]
  713. 'gdb-mouse-set-clear-breakpoint)
  714. (define-key gud-minor-mode-map [left-fringe mouse-1]
  715. 'gdb-mouse-set-clear-breakpoint)
  716. (define-key gud-minor-mode-map [left-margin C-mouse-1]
  717. 'gdb-mouse-toggle-breakpoint-margin)
  718. (define-key gud-minor-mode-map [left-fringe C-mouse-1]
  719. 'gdb-mouse-toggle-breakpoint-fringe)
  720. (define-key gud-minor-mode-map [left-margin drag-mouse-1]
  721. 'gdb-mouse-until)
  722. (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
  723. 'gdb-mouse-until)
  724. (define-key gud-minor-mode-map [left-margin mouse-3]
  725. 'gdb-mouse-until)
  726. (define-key gud-minor-mode-map [left-fringe mouse-3]
  727. 'gdb-mouse-until)
  728. (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
  729. 'gdb-mouse-jump)
  730. (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
  731. 'gdb-mouse-jump)
  732. (define-key gud-minor-mode-map [left-fringe C-mouse-3]
  733. 'gdb-mouse-jump)
  734. (define-key gud-minor-mode-map [left-margin C-mouse-3]
  735. 'gdb-mouse-jump)
  736. (set (make-local-variable 'gud-gdb-completion-function)
  737. 'gud-gdbmi-completions)
  738. (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
  739. nil 'local)
  740. (local-set-key "\C-i" 'completion-at-point)
  741. (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
  742. (setq gdb-first-prompt t)
  743. (setq gud-running nil)
  744. (gdb-update)
  745. (run-hooks 'gdb-mode-hook))
  746. (defun gdb-init-1 ()
  747. ;; (Re-)initialize.
  748. (setq gdb-selected-frame nil
  749. gdb-frame-number nil
  750. gdb-thread-number nil
  751. gdb-var-list nil
  752. gdb-output-sink 'user
  753. gdb-location-alist nil
  754. gdb-source-file-list nil
  755. gdb-last-command nil
  756. gdb-token-number 0
  757. gdb-handler-list '()
  758. gdb-prompt-name nil
  759. gdb-first-done-or-error t
  760. gdb-buffer-fringe-width (car (window-fringes))
  761. gdb-debug-log nil
  762. gdb-source-window nil
  763. gdb-inferior-status nil
  764. gdb-continuation nil
  765. gdb-buf-publisher '()
  766. gdb-threads-list '()
  767. gdb-breakpoints-list '()
  768. gdb-register-names '()
  769. gdb-non-stop gdb-non-stop-setting)
  770. ;;
  771. (gdbmi-bnf-init)
  772. ;;
  773. (setq gdb-buffer-type 'gdbmi)
  774. ;;
  775. (gdb-force-mode-line-update
  776. (propertize "initializing..." 'face font-lock-variable-name-face))
  777. (gdb-get-buffer-create 'gdb-inferior-io)
  778. (gdb-clear-inferior-io)
  779. (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
  780. (when (eq system-type 'windows-nt)
  781. ;; Don't create a separate console window for the debuggee.
  782. (gdb-input "-gdb-set new-console off" 'ignore)
  783. ;; Force GDB to behave as if its input and output stream were
  784. ;; connected to a TTY device (since on Windows we use pipes for
  785. ;; communicating with GDB).
  786. (gdb-input "-gdb-set interactive-mode on" 'ignore))
  787. (gdb-input "-gdb-set height 0" 'ignore)
  788. (when gdb-non-stop
  789. (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
  790. (gdb-input "-enable-pretty-printing" 'ignore)
  791. ;; Find source file and compilation directory here.
  792. (if gdb-create-source-file-list
  793. ;; Needs GDB 6.2 onwards.
  794. (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
  795. ;; Needs GDB 6.0 onwards.
  796. (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
  797. (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
  798. (defun gdb-non-stop-handler ()
  799. (goto-char (point-min))
  800. (if (re-search-forward "No symbol" nil t)
  801. (progn
  802. (message
  803. "This version of GDB doesn't support non-stop mode. Turning it off.")
  804. (setq gdb-non-stop nil)
  805. (setq gdb-supports-non-stop nil))
  806. (setq gdb-supports-non-stop t)
  807. (gdb-input "-gdb-set target-async 1" 'ignore)
  808. (gdb-input "-list-target-features" 'gdb-check-target-async)))
  809. (defun gdb-check-target-async ()
  810. (goto-char (point-min))
  811. (unless (re-search-forward "async" nil t)
  812. (message
  813. "Target doesn't support non-stop mode. Turning it off.")
  814. (setq gdb-non-stop nil)
  815. (gdb-input "-gdb-set non-stop 0" 'ignore)))
  816. (defun gdb-delchar-or-quit (arg)
  817. "Delete ARG characters or send a quit command to GDB.
  818. Send a quit only if point is at the end of the buffer, there is
  819. no input, and GDB is waiting for input."
  820. (interactive "p")
  821. (unless (and (eq (current-buffer) gud-comint-buffer)
  822. (eq gud-minor-mode 'gdbmi))
  823. (error "Not in a GDB-MI buffer"))
  824. (let ((proc (get-buffer-process gud-comint-buffer)))
  825. (if (and (eobp)
  826. (process-live-p proc)
  827. (not gud-running)
  828. (= (point) (marker-position (process-mark proc))))
  829. ;; Sending an EOF does not work with GDB-MI; submit an
  830. ;; explicit quit command.
  831. (progn
  832. (insert "quit")
  833. (comint-send-input t t))
  834. (delete-char arg))))
  835. (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
  836. (defun gdb-create-define-alist ()
  837. "Create an alist of #define directives for GUD tooltips."
  838. (let* ((file (buffer-file-name))
  839. (output
  840. (with-output-to-string
  841. (with-current-buffer standard-output
  842. (and file
  843. (file-exists-p file)
  844. ;; call-process doesn't work with remote file names.
  845. (not (file-remote-p default-directory))
  846. (call-process shell-file-name file
  847. (list t nil) nil "-c"
  848. (concat gdb-cpp-define-alist-program " "
  849. gdb-cpp-define-alist-flags))))))
  850. (define-list (split-string output "\n" t))
  851. (name))
  852. (setq gdb-define-alist nil)
  853. (dolist (define define-list)
  854. (setq name (nth 1 (split-string define "[( ]")))
  855. (push (cons name define) gdb-define-alist))))
  856. (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
  857. (defconst gdb--string-regexp "\"\\(?:[^\\\"]\\|\\\\.\\)*\"")
  858. (defun gdb-tooltip-print (expr)
  859. (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
  860. (goto-char (point-min))
  861. (cond
  862. ((re-search-forward (concat ".*value=\\(" gdb--string-regexp
  863. "\\)")
  864. nil t)
  865. (tooltip-show
  866. (concat expr " = " (read (match-string 1)))
  867. (or gud-tooltip-echo-area
  868. (not (display-graphic-p)))))
  869. ((re-search-forward "msg=\\(\".+\"\\)$" nil t)
  870. (tooltip-show (read (match-string 1))
  871. (or gud-tooltip-echo-area
  872. (not (display-graphic-p))))))))
  873. ;; If expr is a macro for a function don't print because of possible dangerous
  874. ;; side-effects. Also printing a function within a tooltip generates an
  875. ;; unexpected starting annotation (phase error).
  876. (defun gdb-tooltip-print-1 (expr)
  877. (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
  878. (goto-char (point-min))
  879. (if (search-forward "expands to: " nil t)
  880. (unless (looking-at "\\S-+.*(.*).*")
  881. (gdb-input (concat "-data-evaluate-expression \"" expr "\"")
  882. `(lambda () (gdb-tooltip-print ,expr)))))))
  883. (defun gdb-init-buffer ()
  884. (set (make-local-variable 'gud-minor-mode) 'gdbmi)
  885. (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
  886. (when gud-tooltip-mode
  887. (make-local-variable 'gdb-define-alist)
  888. (gdb-create-define-alist)
  889. (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
  890. (defmacro gdb--if-arrow (arrow-position start-posn end-posn &rest body)
  891. (declare (indent 3))
  892. (let ((buffer (make-symbol "buffer")))
  893. `(if ,arrow-position
  894. (let ((,buffer (marker-buffer ,arrow-position)))
  895. (if (equal ,buffer (window-buffer (posn-window ,end-posn)))
  896. (with-current-buffer ,buffer
  897. (when (or (equal ,start-posn ,end-posn)
  898. (equal (posn-point ,start-posn)
  899. (marker-position ,arrow-position)))
  900. ,@body)))))))
  901. (defun gdb-mouse-until (event)
  902. "Continue running until a source line past the current line.
  903. The destination source line can be selected either by clicking
  904. with mouse-3 on the fringe/margin or dragging the arrow
  905. with mouse-1 (default bindings)."
  906. (interactive "e")
  907. (let ((start (event-start event))
  908. (end (event-end event)))
  909. (gdb--if-arrow gud-overlay-arrow-position start end
  910. (let ((line (line-number-at-pos (posn-point end))))
  911. (gud-call (concat "until " (number-to-string line)))))
  912. (gdb--if-arrow gdb-disassembly-position start end
  913. (save-excursion
  914. (goto-char (point-min))
  915. (forward-line (1- (line-number-at-pos (posn-point end))))
  916. (forward-char 2)
  917. (gud-call (concat "until *%a"))))))
  918. (defun gdb-mouse-jump (event)
  919. "Set execution address/line.
  920. The destination source line can be selected either by clicking with C-mouse-3
  921. on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
  922. Unlike `gdb-mouse-until' the destination address can be before the current
  923. line, and no execution takes place."
  924. (interactive "e")
  925. (let ((start (event-start event))
  926. (end (event-end event)))
  927. (gdb--if-arrow gud-overlay-arrow-position start end
  928. (let ((line (line-number-at-pos (posn-point end))))
  929. (gud-call (concat "tbreak " (number-to-string line)))
  930. (gud-call (concat "jump " (number-to-string line)))))
  931. (gdb--if-arrow gdb-disassembly-position start end
  932. (save-excursion
  933. (goto-char (point-min))
  934. (forward-line (1- (line-number-at-pos (posn-point end))))
  935. (forward-char 2)
  936. (gud-call (concat "tbreak *%a"))
  937. (gud-call (concat "jump *%a"))))))
  938. (defcustom gdb-show-changed-values t
  939. "If non-nil change the face of out of scope variables and changed values.
  940. Out of scope variables are suppressed with `shadow' face.
  941. Changed values are highlighted with the face `font-lock-warning-face'."
  942. :type 'boolean
  943. :group 'gdb
  944. :version "22.1")
  945. (defcustom gdb-max-children 40
  946. "Maximum number of children before expansion requires confirmation."
  947. :type 'integer
  948. :group 'gdb
  949. :version "22.1")
  950. (defcustom gdb-delete-out-of-scope t
  951. "If non-nil delete watch expressions automatically when they go out of scope."
  952. :type 'boolean
  953. :group 'gdb
  954. :version "22.2")
  955. (define-minor-mode gdb-speedbar-auto-raise
  956. "Minor mode to automatically raise the speedbar for watch expressions.
  957. With prefix argument ARG, automatically raise speedbar if ARG is
  958. positive, otherwise don't automatically raise it."
  959. :global t
  960. :group 'gdb
  961. :version "22.1")
  962. (defcustom gdb-use-colon-colon-notation nil
  963. "If non-nil use FUN::VAR format to display variables in the speedbar."
  964. :type 'boolean
  965. :group 'gdb
  966. :version "22.1")
  967. (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
  968. (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
  969. (declare-function tooltip-identifier-from-point "tooltip" (point))
  970. (defun gud-watch (&optional arg event)
  971. "Watch expression at point.
  972. With arg, enter name of variable to be watched in the minibuffer."
  973. (interactive (list current-prefix-arg last-input-event))
  974. (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
  975. (if (eq minor-mode 'gdbmi)
  976. (progn
  977. (if event (posn-set-point (event-end event)))
  978. (require 'tooltip)
  979. (save-selected-window
  980. (let ((expr
  981. (if arg
  982. (completing-read "Name of variable: "
  983. 'gud-gdb-complete-command)
  984. (if (and transient-mark-mode mark-active)
  985. (buffer-substring (region-beginning) (region-end))
  986. (concat (if (derived-mode-p 'gdb-registers-mode) "$")
  987. (tooltip-identifier-from-point (point)))))))
  988. (set-text-properties 0 (length expr) nil expr)
  989. (gdb-input (concat "-var-create - * " expr "")
  990. `(lambda () (gdb-var-create-handler ,expr))))))
  991. (message "gud-watch is a no-op in this mode."))))
  992. (defun gdb-var-create-handler (expr)
  993. (let* ((result (gdb-json-partial-output)))
  994. (if (not (bindat-get-field result 'msg))
  995. (let ((var
  996. (list (bindat-get-field result 'name)
  997. (if (and (string-equal gdb-current-language "c")
  998. gdb-use-colon-colon-notation gdb-selected-frame)
  999. (setq expr (concat gdb-selected-frame "::" expr))
  1000. expr)
  1001. (bindat-get-field result 'numchild)
  1002. (bindat-get-field result 'type)
  1003. (bindat-get-field result 'value)
  1004. nil
  1005. (bindat-get-field result 'has_more)
  1006. gdb-frame-address)))
  1007. (push var gdb-var-list)
  1008. (speedbar 1)
  1009. (unless (string-equal
  1010. speedbar-initial-expansion-list-name "GUD")
  1011. (speedbar-change-initial-expansion-list "GUD")))
  1012. (message-box "No symbol \"%s\" in current context." expr))))
  1013. (defun gdb-speedbar-update ()
  1014. (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
  1015. ;; Dummy command to update speedbar even when idle.
  1016. (gdb-input "-environment-pwd"
  1017. 'gdb-speedbar-timer-fn
  1018. 'gdb-speedbar-update)))
  1019. (defun gdb-speedbar-timer-fn ()
  1020. (if gdb-speedbar-auto-raise
  1021. (raise-frame speedbar-frame))
  1022. (speedbar-timer-fn))
  1023. (defun gdb-var-evaluate-expression-handler (varnum changed)
  1024. (goto-char (point-min))
  1025. (re-search-forward (concat ".*value=\\(" gdb--string-regexp "\\)")
  1026. nil t)
  1027. (let ((var (assoc varnum gdb-var-list)))
  1028. (when var
  1029. (if changed (setcar (nthcdr 5 var) 'changed))
  1030. (setcar (nthcdr 4 var) (read (match-string 1)))))
  1031. (gdb-speedbar-update))
  1032. ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
  1033. (defun gdb-var-list-children (varnum)
  1034. (gdb-input (concat "-var-update " varnum) 'ignore)
  1035. (gdb-input (concat "-var-list-children --all-values " varnum)
  1036. `(lambda () (gdb-var-list-children-handler ,varnum))))
  1037. (defun gdb-var-list-children-handler (varnum)
  1038. (let* ((var-list nil)
  1039. (output (bindat-get-field (gdb-json-partial-output "child")))
  1040. (children (bindat-get-field output 'children)))
  1041. (catch 'child-already-watched
  1042. (dolist (var gdb-var-list)
  1043. (if (string-equal varnum (car var))
  1044. (progn
  1045. ;; With dynamic varobjs numchild may have increased.
  1046. (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
  1047. (push var var-list)
  1048. (dolist (child children)
  1049. (let ((varchild (list (bindat-get-field child 'name)
  1050. (bindat-get-field child 'exp)
  1051. (bindat-get-field child 'numchild)
  1052. (bindat-get-field child 'type)
  1053. (bindat-get-field child 'value)
  1054. nil
  1055. (bindat-get-field child 'has_more))))
  1056. (if (assoc (car varchild) gdb-var-list)
  1057. (throw 'child-already-watched nil))
  1058. (push varchild var-list))))
  1059. (push var var-list)))
  1060. (setq gdb-var-list (nreverse var-list))))
  1061. (gdb-speedbar-update))
  1062. (defun gdb-var-set-format (format)
  1063. "Set the output format for a variable displayed in the speedbar."
  1064. (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
  1065. (varnum (car var)))
  1066. (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
  1067. (gdb-var-update)))
  1068. (defun gdb-var-delete-1 (var varnum)
  1069. (gdb-input (concat "-var-delete " varnum) 'ignore)
  1070. (setq gdb-var-list (delq var gdb-var-list))
  1071. (dolist (varchild gdb-var-list)
  1072. (if (string-match (concat (car var) "\\.") (car varchild))
  1073. (setq gdb-var-list (delq varchild gdb-var-list)))))
  1074. (defun gdb-var-delete ()
  1075. "Delete watch expression at point from the speedbar."
  1076. (interactive)
  1077. (let ((text (speedbar-line-text)))
  1078. (string-match "\\(\\S-+\\)" text)
  1079. (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
  1080. (varnum (car var)))
  1081. (if (string-match "\\." (car var))
  1082. (message-box "Can only delete a root expression")
  1083. (gdb-var-delete-1 var varnum)))))
  1084. (defun gdb-var-delete-children (varnum)
  1085. "Delete children of variable object at point from the speedbar."
  1086. (gdb-input (concat "-var-delete -c " varnum) 'ignore))
  1087. (defun gdb-edit-value (_text _token _indent)
  1088. "Assign a value to a variable displayed in the speedbar."
  1089. (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
  1090. (varnum (car var))
  1091. (value (read-string "New value: ")))
  1092. (gdb-input (concat "-var-assign " varnum " " value)
  1093. `(lambda () (gdb-edit-value-handler ,value)))))
  1094. (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
  1095. (defun gdb-edit-value-handler (value)
  1096. (goto-char (point-min))
  1097. (if (re-search-forward gdb-error-regexp nil t)
  1098. (message-box "Invalid number or expression (%s)" value)))
  1099. ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
  1100. (defun gdb-var-update ()
  1101. (gdb-input "-var-update --all-values *"
  1102. 'gdb-var-update-handler
  1103. 'gdb-var-update))
  1104. (defun gdb-var-update-handler ()
  1105. (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
  1106. (dolist (var gdb-var-list)
  1107. (setcar (nthcdr 5 var) nil))
  1108. (let ((temp-var-list gdb-var-list))
  1109. (dolist (change changelist)
  1110. (let* ((varnum (bindat-get-field change 'name))
  1111. (var (assoc varnum gdb-var-list))
  1112. (new-num (bindat-get-field change 'new_num_children)))
  1113. (when var
  1114. (let ((scope (bindat-get-field change 'in_scope))
  1115. (has-more (bindat-get-field change 'has_more)))
  1116. (cond ((string-equal scope "false")
  1117. (if gdb-delete-out-of-scope
  1118. (gdb-var-delete-1 var varnum)
  1119. (setcar (nthcdr 5 var) 'out-of-scope)))
  1120. ((string-equal scope "true")
  1121. (setcar (nthcdr 6 var) has-more)
  1122. (when (and (or (not has-more)
  1123. (string-equal has-more "0"))
  1124. (not new-num)
  1125. (string-equal (nth 2 var) "0"))
  1126. (setcar (nthcdr 4 var)
  1127. (bindat-get-field change 'value))
  1128. (setcar (nthcdr 5 var) 'changed)))
  1129. ((string-equal scope "invalid")
  1130. (gdb-var-delete-1 var varnum)))))
  1131. (let ((var-list nil) var1
  1132. (children (bindat-get-field change 'new_children)))
  1133. (when new-num
  1134. (setq var1 (pop temp-var-list))
  1135. (while var1
  1136. (if (string-equal varnum (car var1))
  1137. (let ((new (string-to-number new-num))
  1138. (previous (string-to-number (nth 2 var1))))
  1139. (setcar (nthcdr 2 var1) new-num)
  1140. (push var1 var-list)
  1141. (cond
  1142. ((> new previous)
  1143. ;; Add new children to list.
  1144. (dotimes (_ previous)
  1145. (push (pop temp-var-list) var-list))
  1146. (dolist (child children)
  1147. (let ((varchild
  1148. (list (bindat-get-field child 'name)
  1149. (bindat-get-field child 'exp)
  1150. (bindat-get-field child 'numchild)
  1151. (bindat-get-field child 'type)
  1152. (bindat-get-field child 'value)
  1153. 'changed
  1154. (bindat-get-field child 'has_more))))
  1155. (push varchild var-list))))
  1156. ;; Remove deleted children from list.
  1157. ((< new previous)
  1158. (dotimes (_ new)
  1159. (push (pop temp-var-list) var-list))
  1160. (dotimes (_ (- previous new))
  1161. (pop temp-var-list)))))
  1162. (push var1 var-list))
  1163. (setq var1 (pop temp-var-list)))
  1164. (setq gdb-var-list (nreverse var-list))))))))
  1165. (gdb-speedbar-update))
  1166. (defun gdb-speedbar-expand-node (text token indent)
  1167. "Expand the node the user clicked on.
  1168. TEXT is the text of the button we clicked on, a + or - item.
  1169. TOKEN is data related to this node.
  1170. INDENT is the current indentation depth."
  1171. (cond ((string-match "+" text) ;expand this node
  1172. (let* ((var (assoc token gdb-var-list))
  1173. (expr (nth 1 var)) (children (nth 2 var)))
  1174. (if (or (<= (string-to-number children) gdb-max-children)
  1175. (y-or-n-p
  1176. (format "%s has %s children. Continue? " expr children)))
  1177. (gdb-var-list-children token))))
  1178. ((string-match "-" text) ;contract this node
  1179. (dolist (var gdb-var-list)
  1180. (if (string-match (concat token "\\.") (car var))
  1181. (setq gdb-var-list (delq var gdb-var-list))))
  1182. (gdb-var-delete-children token)
  1183. (speedbar-change-expand-button-char ?+)
  1184. (speedbar-delete-subblock indent))
  1185. (t (error "Ooops... not sure what to do")))
  1186. (speedbar-center-buffer-smartly))
  1187. (defun gdb-get-target-string ()
  1188. (with-current-buffer gud-comint-buffer
  1189. gud-target-name))
  1190. ;;
  1191. ;; gdb buffers.
  1192. ;;
  1193. ;; Each buffer has a TYPE -- a symbol that identifies the function
  1194. ;; of that particular buffer.
  1195. ;;
  1196. ;; The usual gdb interaction buffer is given the type `gdbmi' and
  1197. ;; is constructed specially.
  1198. ;;
  1199. ;; Others are constructed by gdb-get-buffer-create and
  1200. ;; named according to the rules set forth in the gdb-buffer-rules
  1201. (defvar gdb-buffer-rules '())
  1202. (defun gdb-rules-name-maker (rules-entry)
  1203. (cadr rules-entry))
  1204. (defun gdb-rules-buffer-mode (rules-entry)
  1205. (nth 2 rules-entry))
  1206. (defun gdb-rules-update-trigger (rules-entry)
  1207. (nth 3 rules-entry))
  1208. (defun gdb-update-buffer-name ()
  1209. "Rename current buffer according to name-maker associated with
  1210. it in `gdb-buffer-rules'."
  1211. (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
  1212. gdb-buffer-rules))))
  1213. (when f (rename-buffer (funcall f)))))
  1214. (defun gdb-current-buffer-rules ()
  1215. "Get `gdb-buffer-rules' entry for current buffer type."
  1216. (assoc gdb-buffer-type gdb-buffer-rules))
  1217. (defun gdb-current-buffer-thread ()
  1218. "Get thread object of current buffer from `gdb-threads-list'.
  1219. When current buffer is not bound to any thread, return main
  1220. thread."
  1221. (cdr (assoc gdb-thread-number gdb-threads-list)))
  1222. (defun gdb-current-buffer-frame ()
  1223. "Get current stack frame object for thread of current buffer."
  1224. (bindat-get-field (gdb-current-buffer-thread) 'frame))
  1225. (defun gdb-buffer-type (buffer)
  1226. "Get value of `gdb-buffer-type' for BUFFER."
  1227. (with-current-buffer buffer
  1228. gdb-buffer-type))
  1229. (defun gdb-buffer-shows-main-thread-p ()
  1230. "Return t if current GDB buffer shows main selected thread and
  1231. is not bound to it."
  1232. (current-buffer)
  1233. (not (local-variable-p 'gdb-thread-number)))
  1234. (defun gdb-get-buffer (buffer-type &optional thread)
  1235. "Get a specific GDB buffer.
  1236. In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
  1237. and `gdb-thread-number' (if provided) must be equal to THREAD."
  1238. (catch 'found
  1239. (dolist (buffer (buffer-list) nil)
  1240. (with-current-buffer buffer
  1241. (when (and (eq gdb-buffer-type buffer-type)
  1242. (or (not thread)
  1243. (equal gdb-thread-number thread)))
  1244. (throw 'found buffer))))))
  1245. (defun gdb-get-buffer-create (buffer-type &optional thread)
  1246. "Create a new GDB buffer of the type specified by BUFFER-TYPE.
  1247. The buffer-type should be one of the cars in `gdb-buffer-rules'.
  1248. If THREAD is non-nil, it is assigned to `gdb-thread-number'
  1249. buffer-local variable of the new buffer.
  1250. Buffer mode and name are selected according to buffer type.
  1251. If buffer has trigger associated with it in `gdb-buffer-rules',
  1252. this trigger is subscribed to `gdb-buf-publisher' and called with
  1253. 'update argument."
  1254. (or (gdb-get-buffer buffer-type thread)
  1255. (let ((rules (assoc buffer-type gdb-buffer-rules))
  1256. (new (generate-new-buffer "limbo")))
  1257. (with-current-buffer new
  1258. (let ((mode (gdb-rules-buffer-mode rules))
  1259. (trigger (gdb-rules-update-trigger rules)))
  1260. (when mode (funcall mode))
  1261. (setq gdb-buffer-type buffer-type)
  1262. (when thread
  1263. (set (make-local-variable 'gdb-thread-number) thread))
  1264. (set (make-local-variable 'gud-minor-mode)
  1265. (buffer-local-value 'gud-minor-mode gud-comint-buffer))
  1266. (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
  1267. (rename-buffer (funcall (gdb-rules-name-maker rules)))
  1268. (when trigger
  1269. (gdb-add-subscriber gdb-buf-publisher
  1270. (cons (current-buffer)
  1271. (gdb-bind-function-to-buffer
  1272. trigger (current-buffer))))
  1273. (funcall trigger 'start))
  1274. (current-buffer))))))
  1275. (defun gdb-bind-function-to-buffer (expr buffer)
  1276. "Return a function which will evaluate EXPR in BUFFER."
  1277. `(lambda (&rest args)
  1278. (with-current-buffer ,buffer
  1279. (apply ',expr args))))
  1280. ;; Used to display windows with thread-bound buffers
  1281. (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
  1282. split-horizontal)
  1283. `(defun ,name (&optional thread)
  1284. ,(when doc doc)
  1285. (message "%s" thread)
  1286. (gdb-preempt-existing-or-display-buffer
  1287. (gdb-get-buffer-create ,buffer thread)
  1288. ,split-horizontal)))
  1289. ;; This assoc maps buffer type symbols to rules. Each rule is a list of
  1290. ;; at least one and possible more functions. The functions have these
  1291. ;; roles in defining a buffer type:
  1292. ;;
  1293. ;; NAME - Return a name for this buffer type.
  1294. ;;
  1295. ;; The remaining function(s) are optional:
  1296. ;;
  1297. ;; MODE - called in a new buffer with no arguments, should establish
  1298. ;; the proper mode for the buffer.
  1299. ;;
  1300. (defun gdb-set-buffer-rules (buffer-type &rest rules)
  1301. (let ((binding (assoc buffer-type gdb-buffer-rules)))
  1302. (if binding
  1303. (setcdr binding rules)
  1304. (push (cons buffer-type rules)
  1305. gdb-buffer-rules))))
  1306. (defun gdb-parent-mode ()
  1307. "Generic mode to derive all other GDB buffer modes from."
  1308. (kill-all-local-variables)
  1309. (setq buffer-read-only t)
  1310. (buffer-disable-undo)
  1311. ;; Delete buffer from gdb-buf-publisher when it's killed
  1312. ;; (if it has an associated update trigger)
  1313. (add-hook
  1314. 'kill-buffer-hook
  1315. (function
  1316. (lambda ()
  1317. (let ((trigger (gdb-rules-update-trigger
  1318. (gdb-current-buffer-rules))))
  1319. (when trigger
  1320. (gdb-delete-subscriber
  1321. gdb-buf-publisher
  1322. ;; This should match gdb-add-subscriber done in
  1323. ;; gdb-get-buffer-create
  1324. (cons (current-buffer)
  1325. (gdb-bind-function-to-buffer trigger (current-buffer))))))))
  1326. nil t))
  1327. ;; Partial-output buffer : This accumulates output from a command executed on
  1328. ;; behalf of emacs (rather than the user).
  1329. ;;
  1330. (gdb-set-buffer-rules 'gdb-partial-output-buffer
  1331. 'gdb-partial-output-name)
  1332. (defun gdb-partial-output-name ()
  1333. (concat " *partial-output-"
  1334. (gdb-get-target-string)
  1335. "*"))
  1336. (gdb-set-buffer-rules 'gdb-inferior-io
  1337. 'gdb-inferior-io-name
  1338. 'gdb-inferior-io-mode)
  1339. (defun gdb-inferior-io-name ()
  1340. (concat "*input/output of "
  1341. (gdb-get-target-string)
  1342. "*"))
  1343. (defun gdb-display-io-buffer ()
  1344. "Display IO of debugged program in a separate window."
  1345. (interactive)
  1346. (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
  1347. (defun gdb-inferior-io--init-proc (proc)
  1348. ;; Set up inferior I/O. Needs GDB 6.4 onwards.
  1349. (set-process-filter proc 'gdb-inferior-filter)
  1350. (set-process-sentinel proc 'gdb-inferior-io-sentinel)
  1351. ;; The process can run on a remote host.
  1352. (let ((tty (or (process-get proc 'remote-tty)
  1353. (process-tty-name proc))))
  1354. (unless (or (null tty)
  1355. (string= tty ""))
  1356. (gdb-input
  1357. (concat "-inferior-tty-set " tty) 'ignore))))
  1358. (defun gdb-inferior-io-sentinel (proc _str)
  1359. (when (eq (process-status proc) 'failed)
  1360. ;; When the debugged process exits, Emacs gets an EIO error on
  1361. ;; read from the pty, and stops listening to it. If the gdb
  1362. ;; process is still running, remove the pty, make a new one, and
  1363. ;; pass it to gdb.
  1364. (let ((io-buffer (process-buffer proc)))
  1365. (when (and (process-live-p (get-buffer-process gud-comint-buffer))
  1366. (buffer-live-p io-buffer))
  1367. ;; `comint-exec' deletes the original process as a side effect.
  1368. (comint-exec io-buffer "gdb-inferior" nil nil nil)
  1369. (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
  1370. (defcustom gdb-display-buffer-other-frame-action
  1371. '((display-buffer-reuse-window display-buffer-pop-up-frame)
  1372. (reusable-frames . visible)
  1373. (inhibit-same-window . t)
  1374. (pop-up-frame-parameters (height . 14)
  1375. (width . 80)
  1376. (unsplittable . t)
  1377. (tool-bar-lines . nil)
  1378. (menu-bar-lines . nil)
  1379. (minibuffer . nil)))
  1380. "`display-buffer' action for displaying GDB utility frames."
  1381. :group 'gdb
  1382. :type display-buffer--action-custom-type
  1383. :risky t
  1384. :version "24.3")
  1385. (defun gdb-frame-io-buffer ()
  1386. "Display IO of debugged program in another frame."
  1387. (interactive)
  1388. (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)
  1389. gdb-display-buffer-other-frame-action))
  1390. (defvar gdb-inferior-io-mode-map
  1391. (let ((map (make-sparse-keymap)))
  1392. (define-key map "\C-c\C-c" 'gdb-io-interrupt)
  1393. (define-key map "\C-c\C-z" 'gdb-io-stop)
  1394. (define-key map "\C-c\C-\\" 'gdb-io-quit)
  1395. (define-key map "\C-c\C-d" 'gdb-io-eof)
  1396. (define-key map "\C-d" 'gdb-io-eof)
  1397. map))
  1398. ;; We want to use comint because it has various nifty and familiar features.
  1399. (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
  1400. "Major mode for gdb inferior-io."
  1401. :syntax-table nil :abbrev-table nil
  1402. (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
  1403. (defcustom gdb-display-io-nopopup nil
  1404. "When non-nil, and the 'gdb-inferior-io buffer is buried, don't pop it up."
  1405. :type 'boolean
  1406. :group 'gdb
  1407. :version "25.1")
  1408. (defun gdb-inferior-filter (proc string)
  1409. (unless (string-equal string "")
  1410. (let (buf)
  1411. (unless (and gdb-display-io-nopopup
  1412. (setq buf (gdb-get-buffer 'gdb-inferior-io))
  1413. (null (get-buffer-window buf)))
  1414. (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
  1415. (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
  1416. (comint-output-filter proc string)))
  1417. (defun gdb-io-interrupt ()
  1418. "Interrupt the program being debugged."
  1419. (interactive)
  1420. (interrupt-process
  1421. (get-buffer-process gud-comint-buffer) comint-ptyp))
  1422. (defun gdb-io-quit ()
  1423. "Send quit signal to the program being debugged."
  1424. (interactive)
  1425. (quit-process
  1426. (get-buffer-process gud-comint-buffer) comint-ptyp))
  1427. (defun gdb-io-stop ()
  1428. "Stop the program being debugged."
  1429. (interactive)
  1430. (stop-process
  1431. (get-buffer-process gud-comint-buffer) comint-ptyp))
  1432. (defun gdb-io-eof ()
  1433. "Send end-of-file to the program being debugged."
  1434. (interactive)
  1435. (process-send-eof
  1436. (get-buffer-process gud-comint-buffer)))
  1437. (defun gdb-clear-inferior-io ()
  1438. (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
  1439. (erase-buffer)))
  1440. (defconst breakpoint-xpm-data
  1441. "/* XPM */
  1442. static char *magick[] = {
  1443. /* columns rows colors chars-per-pixel */
  1444. \"10 10 2 1\",
  1445. \" c red\",
  1446. \"+ c None\",
  1447. /* pixels */
  1448. \"+++ +++\",
  1449. \"++ ++\",
  1450. \"+ +\",
  1451. \" \",
  1452. \" \",
  1453. \" \",
  1454. \" \",
  1455. \"+ +\",
  1456. \"++ ++\",
  1457. \"+++ +++\",
  1458. };"
  1459. "XPM data used for breakpoint icon.")
  1460. (defconst breakpoint-enabled-pbm-data
  1461. "P1
  1462. 10 10\",
  1463. 0 0 0 0 1 1 1 1 0 0 0 0
  1464. 0 0 0 1 1 1 1 1 1 0 0 0
  1465. 0 0 1 1 1 1 1 1 1 1 0 0
  1466. 0 1 1 1 1 1 1 1 1 1 1 0
  1467. 0 1 1 1 1 1 1 1 1 1 1 0
  1468. 0 1 1 1 1 1 1 1 1 1 1 0
  1469. 0 1 1 1 1 1 1 1 1 1 1 0
  1470. 0 0 1 1 1 1 1 1 1 1 0 0
  1471. 0 0 0 1 1 1 1 1 1 0 0 0
  1472. 0 0 0 0 1 1 1 1 0 0 0 0"
  1473. "PBM data used for enabled breakpoint icon.")
  1474. (defconst breakpoint-disabled-pbm-data
  1475. "P1
  1476. 10 10\",
  1477. 0 0 1 0 1 0 1 0 0 0
  1478. 0 1 0 1 0 1 0 1 0 0
  1479. 1 0 1 0 1 0 1 0 1 0
  1480. 0 1 0 1 0 1 0 1 0 1
  1481. 1 0 1 0 1 0 1 0 1 0
  1482. 0 1 0 1 0 1 0 1 0 1
  1483. 1 0 1 0 1 0 1 0 1 0
  1484. 0 1 0 1 0 1 0 1 0 1
  1485. 0 0 1 0 1 0 1 0 1 0
  1486. 0 0 0 1 0 1 0 1 0 0"
  1487. "PBM data used for disabled breakpoint icon.")
  1488. (defvar breakpoint-enabled-icon nil
  1489. "Icon for enabled breakpoint in display margin.")
  1490. (defvar breakpoint-disabled-icon nil
  1491. "Icon for disabled breakpoint in display margin.")
  1492. (declare-function define-fringe-bitmap "fringe.c"
  1493. (bitmap bits &optional height width align))
  1494. (and (display-images-p)
  1495. ;; Bitmap for breakpoint in fringe
  1496. (define-fringe-bitmap 'breakpoint
  1497. "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
  1498. ;; Bitmap for gud-overlay-arrow in fringe
  1499. (define-fringe-bitmap 'hollow-right-triangle
  1500. "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
  1501. (defface breakpoint-enabled
  1502. '((t
  1503. :foreground "red1"
  1504. :weight bold))
  1505. "Face for enabled breakpoint icon in fringe."
  1506. :group 'gdb)
  1507. (defface breakpoint-disabled
  1508. '((((class color) (min-colors 88)) :foreground "grey70")
  1509. ;; Ensure that on low-color displays that we end up something visible.
  1510. (((class color) (min-colors 8) (background light))
  1511. :foreground "black")
  1512. (((class color) (min-colors 8) (background dark))
  1513. :foreground "white")
  1514. (((type tty) (class mono))
  1515. :inverse-video t)
  1516. (t :background "gray"))
  1517. "Face for disabled breakpoint icon in fringe."
  1518. :group 'gdb)
  1519. (defvar gdb-control-commands-regexp
  1520. (concat
  1521. "^\\("
  1522. "commands\\|if\\|while\\|define\\|document\\|python\\|"
  1523. "while-stepping\\|stepping\\|ws\\|actions"
  1524. "\\)\\([[:blank:]]+.*\\)?$")
  1525. "Regexp matching GDB commands that enter a recursive reading loop.
  1526. As long as GDB is in the recursive reading loop, it does not expect
  1527. commands to be prefixed by \"-interpreter-exec console\".")
  1528. (defun gdb-strip-string-backslash (string)
  1529. (replace-regexp-in-string "\\\\$" "" string))
  1530. (defun gdb-send (proc string)
  1531. "A comint send filter for gdb."
  1532. (with-current-buffer gud-comint-buffer
  1533. (let ((inhibit-read-only t))
  1534. (remove-text-properties (point-min) (point-max) '(face))))
  1535. ;; mimic <RET> key to repeat previous command in GDB
  1536. (if (not (string= "" string))
  1537. (if gdb-continuation
  1538. (setq gdb-last-command (concat gdb-continuation
  1539. (gdb-strip-string-backslash string)
  1540. " "))
  1541. (setq gdb-last-command (gdb-strip-string-backslash string)))
  1542. (if gdb-last-command (setq string gdb-last-command))
  1543. (setq gdb-continuation nil))
  1544. (if (and (not gdb-continuation) (or (string-match "^-" string)
  1545. (> gdb-control-level 0)))
  1546. ;; Either MI command or we are feeding GDB's recursive reading loop.
  1547. (progn
  1548. (setq gdb-first-done-or-error t)
  1549. (process-send-string proc (concat string "\n"))
  1550. (if (and (string-match "^end$" string)
  1551. (> gdb-control-level 0))
  1552. (setq gdb-control-level (1- gdb-control-level))))
  1553. ;; CLI command
  1554. (if (string-match "\\\\$" string)
  1555. (setq gdb-continuation
  1556. (concat gdb-continuation (gdb-strip-string-backslash
  1557. string)
  1558. " "))
  1559. (setq gdb-first-done-or-error t)
  1560. (let ((to-send (concat "-interpreter-exec console "
  1561. (gdb-mi-quote (concat gdb-continuation string " "))
  1562. "\n")))
  1563. (if gdb-enable-debug
  1564. (push (cons 'mi-send to-send) gdb-debug-log))
  1565. (process-send-string proc to-send))
  1566. (if (and (string-match "^end$" string)
  1567. (> gdb-control-level 0))
  1568. (setq gdb-control-level (1- gdb-control-level)))
  1569. (setq gdb-continuation nil)))
  1570. (if (string-match gdb-control-commands-regexp string)
  1571. (setq gdb-control-level (1+ gdb-control-level))))
  1572. (defun gdb-mi-quote (string)
  1573. "Return STRING quoted properly as an MI argument.
  1574. The string is enclosed in double quotes.
  1575. All embedded quotes, newlines, and backslashes are preceded with a backslash."
  1576. (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
  1577. (setq string (replace-regexp-in-string "\n" "\\n" string t t))
  1578. (concat "\"" string "\""))
  1579. (defun gdb-input (command handler-function &optional trigger-name)
  1580. "Send COMMAND to GDB via the MI interface.
  1581. Run the function HANDLER-FUNCTION, with no arguments, once the command is
  1582. complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
  1583. Emacs is still waiting for a reply from another command previously
  1584. sent with the same TRIGGER-NAME."
  1585. (when (or (not trigger-name)
  1586. (not (gdb-pending-handler-p trigger-name)))
  1587. (setq gdb-token-number (1+ gdb-token-number))
  1588. (setq command (concat (number-to-string gdb-token-number) command))
  1589. (if gdb-enable-debug (push (list 'send-item command handler-function)
  1590. gdb-debug-log))
  1591. (gdb-add-handler gdb-token-number handler-function trigger-name)
  1592. (if gdbmi-debug-mode (message "gdb-input: %s" command))
  1593. (process-send-string (get-buffer-process gud-comint-buffer)
  1594. (concat command "\n"))))
  1595. ;; NOFRAME is used for gud execution control commands
  1596. (defun gdb-current-context-command (command)
  1597. "Add --thread to gdb COMMAND when needed."
  1598. (if (and gdb-thread-number
  1599. gdb-supports-non-stop)
  1600. (concat command " --thread " gdb-thread-number)
  1601. command))
  1602. (defun gdb-current-context-buffer-name (name)
  1603. "Add thread information and asterisks to string NAME.
  1604. If `gdb-thread-number' is nil, just wrap NAME in asterisks."
  1605. (concat "*" name
  1606. (if (local-variable-p 'gdb-thread-number)
  1607. (format " (bound to thread %s)" gdb-thread-number)
  1608. "")
  1609. "*"))
  1610. (defun gdb-current-context-mode-name (mode)
  1611. "Add thread information to MODE which is to be used as `mode-name'."
  1612. (concat mode
  1613. (if gdb-thread-number
  1614. (format " [thread %s]" gdb-thread-number)
  1615. "")))
  1616. (defcustom gud-gdb-command-name "gdb -i=mi"
  1617. "Default command to execute an executable under the GDB debugger."
  1618. :type 'string
  1619. :group 'gdb)
  1620. (defun gdb-resync()
  1621. (setq gud-running nil)
  1622. (setq gdb-output-sink 'user)
  1623. (gdb-remove-all-pending-triggers))
  1624. (defun gdb-update (&optional no-proc)
  1625. "Update buffers showing status of debug session.
  1626. If NO-PROC is non-nil, do not try to contact the GDB process."
  1627. (when gdb-first-prompt
  1628. (gdb-force-mode-line-update
  1629. (propertize "initializing..." 'face font-lock-variable-name-face))
  1630. (gdb-init-1)
  1631. (setq gdb-first-prompt nil))
  1632. (unless no-proc
  1633. (gdb-get-main-selected-frame))
  1634. ;; We may need to update gdb-threads-list so we can use
  1635. (gdb-get-buffer-create 'gdb-threads-buffer)
  1636. ;; gdb-break-list is maintained in breakpoints handler
  1637. (gdb-get-buffer-create 'gdb-breakpoints-buffer)
  1638. (unless no-proc
  1639. (gdb-emit-signal gdb-buf-publisher 'update))
  1640. (gdb-get-changed-registers)
  1641. (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
  1642. (dolist (var gdb-var-list)
  1643. (setcar (nthcdr 5 var) nil))
  1644. (gdb-var-update)))
  1645. ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
  1646. ;; because we may need to update current gud-running value without
  1647. ;; changing current thread (see gdb-running)
  1648. (defun gdb-setq-thread-number (number)
  1649. "Set `gdb-thread-number' to NUMBER.
  1650. Only this function must be used to change `gdb-thread-number'
  1651. value to NUMBER, because `gud-running' and `gdb-frame-number'
  1652. need to be updated appropriately when current thread changes."
  1653. ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
  1654. (unless (string-equal number "0") (setq gdb-thread-number number))
  1655. (setq gdb-frame-number "0")
  1656. (gdb-update-gud-running))
  1657. (defun gdb-update-gud-running ()
  1658. "Set `gud-running' according to the state of current thread.
  1659. `gdb-frame-number' is set to 0 if current thread is now stopped.
  1660. Note that when `gdb-gud-control-all-threads' is t, `gud-running'
  1661. cannot be reliably used to determine whether or not execution
  1662. control buttons should be shown in menu or toolbar. Use
  1663. `gdb-running-threads-count' and `gdb-stopped-threads-count'
  1664. instead.
  1665. For all-stop mode, thread information is unavailable while target
  1666. is running."
  1667. (let ((old-value gud-running))
  1668. (setq gud-running
  1669. (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
  1670. "running"))
  1671. ;; Set frame number to "0" when _current_ threads stops.
  1672. (when (and (gdb-current-buffer-thread)
  1673. (not (eq gud-running old-value)))
  1674. (setq gdb-frame-number "0"))))
  1675. (defun gdb-show-run-p ()
  1676. "Return t if \"Run/continue\" should be shown on the toolbar."
  1677. (or (not gdb-active-process)
  1678. (and (or
  1679. (not gdb-gud-control-all-threads)
  1680. (not gdb-non-stop))
  1681. (not gud-running))
  1682. (and gdb-gud-control-all-threads
  1683. (> gdb-stopped-threads-count 0))))
  1684. (defun gdb-show-stop-p ()
  1685. "Return t if \"Stop\" should be shown on the toolbar."
  1686. (or (and (or
  1687. (not gdb-gud-control-all-threads)
  1688. (not gdb-non-stop))
  1689. gud-running)
  1690. (and gdb-gud-control-all-threads
  1691. (> gdb-running-threads-count 0))))
  1692. ;; GUD displays the selected GDB frame. This might might not be the current
  1693. ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
  1694. ;; visited breakpoint is, use that window.
  1695. (defun gdb-display-source-buffer (buffer)
  1696. (let* ((last-window (if gud-last-last-frame
  1697. (get-buffer-window
  1698. (gud-find-file (car gud-last-last-frame)))))
  1699. (source-window (or last-window
  1700. (if (and gdb-source-window
  1701. (window-live-p gdb-source-window))
  1702. gdb-source-window))))
  1703. (when source-window
  1704. (setq gdb-source-window source-window)
  1705. (set-window-buffer source-window buffer))
  1706. source-window))
  1707. (defun gdbmi-start-with (str offset match)
  1708. "Return non-nil if string STR starts with MATCH, else returns nil.
  1709. OFFSET is the position in STR at which the comparison takes place."
  1710. (let ((match-length (length match))
  1711. (str-length (- (length str) offset)))
  1712. (when (>= str-length match-length)
  1713. (string-equal match (substring str offset (+ offset match-length))))))
  1714. (defun gdbmi-same-start (str offset match)
  1715. "Return non-nil if STR and MATCH are equal up to the end of either strings.
  1716. OFFSET is the position in STR at which the comparison takes place."
  1717. (let* ((str-length (- (length str) offset))
  1718. (match-length (length match))
  1719. (compare-length (min str-length match-length)))
  1720. (when (> compare-length 0)
  1721. (string-equal (substring str offset (+ offset compare-length))
  1722. (substring match 0 compare-length)))))
  1723. (defun gdbmi-is-number (character)
  1724. "Return non-nil if CHARACTER is a numerical character between 0 and 9."
  1725. (and (>= character ?0)
  1726. (<= character ?9)))
  1727. (defvar-local gdbmi-bnf-state 'gdbmi-bnf-output
  1728. "Current GDB/MI output parser state.
  1729. The parser is placed in a different state when an incomplete data steam is
  1730. received from GDB.
  1731. This variable will preserve the state required to resume the parsing
  1732. when more data arrives.")
  1733. (defvar-local gdbmi-bnf-offset 0
  1734. "Offset in `gud-marker-acc' at which the parser is reading.
  1735. This offset is used to be able to parse the GDB/MI message
  1736. in-place, without the need of copying the string in a temporary buffer
  1737. or discarding parsed tokens by substringing the message.")
  1738. (defun gdbmi-bnf-init ()
  1739. "Initialize the GDB/MI message parser."
  1740. (setq gdbmi-bnf-state 'gdbmi-bnf-output)
  1741. (setq gdbmi-bnf-offset 0)
  1742. (setq gud-marker-acc ""))
  1743. (defun gdbmi-bnf-output ()
  1744. "Implementation of the following GDB/MI output grammar rule:
  1745. output ==>
  1746. ( out-of-band-record )* [ result-record ] gdb-prompt"
  1747. (gdbmi-bnf-skip-unrecognized)
  1748. (while (gdbmi-bnf-out-of-band-record))
  1749. (gdbmi-bnf-result-record)
  1750. (gdbmi-bnf-gdb-prompt))
  1751. (defun gdbmi-bnf-skip-unrecognized ()
  1752. "Skip characters until is encounters the beginning of a valid record.
  1753. Used as a protection mechanism in case something goes wrong when parsing
  1754. a GDB/MI reply message."
  1755. (let ((acc-length (length gud-marker-acc))
  1756. (prefix-offset gdbmi-bnf-offset)
  1757. (prompt "(gdb) \n"))
  1758. (while (and (< prefix-offset acc-length)
  1759. (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
  1760. (setq prefix-offset (1+ prefix-offset)))
  1761. (if (and (< prefix-offset acc-length)
  1762. (not (memq (aref gud-marker-acc prefix-offset)
  1763. '(?^ ?* ?+ ?= ?~ ?@ ?&)))
  1764. (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt))
  1765. (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
  1766. gdbmi-bnf-offset))
  1767. (let ((unrecognized-str (match-string 0 gud-marker-acc)))
  1768. (setq gdbmi-bnf-offset (match-end 0))
  1769. (if gdbmi-debug-mode
  1770. (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str))
  1771. (gdb-shell unrecognized-str)
  1772. t))))
  1773. (defun gdbmi-bnf-gdb-prompt ()
  1774. "Implementation of the following GDB/MI output grammar rule:
  1775. gdb-prompt ==>
  1776. `(gdb)' nl
  1777. nl ==>
  1778. CR | CR-LF"
  1779. (let ((prompt "(gdb) \n"))
  1780. (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt)
  1781. (if gdbmi-debug-mode (message "gdbmi-bnf-gdb-prompt: %s" prompt))
  1782. (gdb-gdb prompt)
  1783. (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length prompt)))
  1784. ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
  1785. ;; the end of a GDB reply message.
  1786. t)))
  1787. (defun gdbmi-bnf-result-record ()
  1788. "Implementation of the following GDB/MI output grammar rule:
  1789. result-record ==>
  1790. [ token ] `^' result-class ( `,' result )* nl
  1791. token ==>
  1792. any sequence of digits."
  1793. (gdbmi-bnf-result-and-async-record-impl))
  1794. (defun gdbmi-bnf-out-of-band-record ()
  1795. "Implementation of the following GDB/MI output grammar rule:
  1796. out-of-band-record ==>
  1797. async-record | stream-record"
  1798. (or (gdbmi-bnf-async-record)
  1799. (gdbmi-bnf-stream-record)))
  1800. (defun gdbmi-bnf-async-record ()
  1801. "Implementation of the following GDB/MI output grammar rules:
  1802. async-record ==>
  1803. exec-async-output | status-async-output | notify-async-output
  1804. exec-async-output ==>
  1805. [ token ] `*' async-output
  1806. status-async-output ==>
  1807. [ token ] `+' async-output
  1808. notify-async-output ==>
  1809. [ token ] `=' async-output
  1810. async-output ==>
  1811. async-class ( `,' result )* nl"
  1812. (gdbmi-bnf-result-and-async-record-impl))
  1813. (defun gdbmi-bnf-stream-record ()
  1814. "Implement the following GDB/MI output grammar rule:
  1815. stream-record ==>
  1816. console-stream-output | target-stream-output | log-stream-output
  1817. console-stream-output ==>
  1818. `~' c-string
  1819. target-stream-output ==>
  1820. `@' c-string
  1821. log-stream-output ==>
  1822. `&' c-string"
  1823. (when (< gdbmi-bnf-offset (length gud-marker-acc))
  1824. (if (and (member (aref gud-marker-acc gdbmi-bnf-offset) '(?~ ?@ ?&))
  1825. (string-match (concat "\\([~@&]\\)\\(" gdb--string-regexp "\\)\n")
  1826. gud-marker-acc
  1827. gdbmi-bnf-offset))
  1828. (let ((prefix (match-string 1 gud-marker-acc))
  1829. (c-string (match-string 2 gud-marker-acc)))
  1830. (setq gdbmi-bnf-offset (match-end 0))
  1831. (if gdbmi-debug-mode (message "gdbmi-bnf-stream-record: %s"
  1832. (match-string 0 gud-marker-acc)))
  1833. (cond ((string-equal prefix "~")
  1834. (gdbmi-bnf-console-stream-output c-string))
  1835. ((string-equal prefix "@")
  1836. (gdbmi-bnf-target-stream-output c-string))
  1837. ((string-equal prefix "&")
  1838. (gdbmi-bnf-log-stream-output c-string)))
  1839. t))))
  1840. (defun gdbmi-bnf-console-stream-output (c-string)
  1841. "Handler for the console-stream-output GDB/MI output grammar rule."
  1842. (gdb-console c-string))
  1843. (defun gdbmi-bnf-target-stream-output (_c-string)
  1844. "Handler for the target-stream-output GDB/MI output grammar rule."
  1845. ;; Not currently used.
  1846. )
  1847. (defun gdbmi-bnf-log-stream-output (c-string)
  1848. "Handler for the log-stream-output GDB/MI output grammar rule."
  1849. ;; Suppress "No registers." GDB 6.8 and earlier
  1850. ;; duplicates MI error message on internal stream.
  1851. ;; Don't print to GUD buffer.
  1852. (if (not (string-equal (read c-string) "No registers.\n"))
  1853. (gdb-internals c-string)))
  1854. (defconst gdbmi-bnf-result-state-configs
  1855. '(("^" . (("done" . (gdb-done . progressive))
  1856. ("error" . (gdb-error . progressive))
  1857. ("running" . (gdb-starting . atomic))))
  1858. ("*" . (("stopped" . (gdb-stopped . atomic))
  1859. ("running" . (gdb-running . atomic))))
  1860. ("+" . ())
  1861. ("=" . (("thread-created" . (gdb-thread-created . atomic))
  1862. ("thread-selected" . (gdb-thread-selected . atomic))
  1863. ("thread-existed" . (gdb-ignored-notification . atomic))
  1864. ('default . (gdb-ignored-notification . atomic)))))
  1865. "Alist of alists, mapping the type and class of message to a handler function.
  1866. Handler functions are all flagged as either `progressive' or `atomic'.
  1867. `progressive' handlers are capable of parsing incomplete messages.
  1868. They can be called several time with new data chunk as they arrive from GDB.
  1869. `progressive' handlers must have an extra argument that is set to a non-nil
  1870. value when the message is complete.
  1871. Implement the following GDB/MI output grammar rule:
  1872. result-class ==>
  1873. `done' | `running' | `connected' | `error' | `exit'
  1874. async-class ==>
  1875. `stopped' | others (where others will be added depending on the needs
  1876. --this is still in development).")
  1877. (defun gdbmi-bnf-result-and-async-record-impl ()
  1878. "Common implementation of the result-record and async-record rule.
  1879. Both rules share the same syntax. Those records may be very large in size.
  1880. For that reason, the \"result\" part of the record is parsed by
  1881. `gdbmi-bnf-incomplete-record-result', which will keep
  1882. receiving characters as they arrive from GDB until the record is complete."
  1883. (let ((acc-length (length gud-marker-acc))
  1884. (prefix-offset gdbmi-bnf-offset))
  1885. (while (and (< prefix-offset acc-length)
  1886. (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
  1887. (setq prefix-offset (1+ prefix-offset)))
  1888. (if (and (< prefix-offset acc-length)
  1889. (member (aref gud-marker-acc prefix-offset) '(?* ?+ ?= ?^))
  1890. (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
  1891. gud-marker-acc gdbmi-bnf-offset))
  1892. (let ((token (match-string 1 gud-marker-acc))
  1893. (prefix (match-string 2 gud-marker-acc))
  1894. (class (match-string 3 gud-marker-acc))
  1895. (complete (string-equal (match-string 4 gud-marker-acc) "\n"))
  1896. class-alist
  1897. class-command)
  1898. (setq gdbmi-bnf-offset (match-end 0))
  1899. (if gdbmi-debug-mode (message "gdbmi-bnf-result-record: %s"
  1900. (match-string 0 gud-marker-acc)))
  1901. (setq class-alist
  1902. (cdr (assoc prefix gdbmi-bnf-result-state-configs)))
  1903. (setq class-command (cdr (assoc class class-alist)))
  1904. (if (null class-command)
  1905. (setq class-command (cdr (assoc 'default class-alist))))
  1906. (if complete
  1907. (if class-command
  1908. (if (equal (cdr class-command) 'progressive)
  1909. (funcall (car class-command) token "" complete)
  1910. (funcall (car class-command) token "")))
  1911. (setq gdbmi-bnf-state
  1912. (lambda ()
  1913. (gdbmi-bnf-incomplete-record-result token class-command)))
  1914. (funcall gdbmi-bnf-state))
  1915. t))))
  1916. (defun gdbmi-bnf-incomplete-record-result (token class-command)
  1917. "State of the parser used to progressively parse a result-record or async-record
  1918. rule from an incomplete data stream. The parser will stay in this state until
  1919. the end of the current result or async record is reached."
  1920. (when (< gdbmi-bnf-offset (length gud-marker-acc))
  1921. ;; Search the data stream for the end of the current record:
  1922. (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset))
  1923. (is-progressive (equal (cdr class-command) 'progressive))
  1924. (is-complete (not (null newline-pos)))
  1925. result-str)
  1926. (when gdbmi-debug-mode
  1927. (message "gdbmi-bnf-incomplete-record-result: %s"
  1928. (substring gud-marker-acc gdbmi-bnf-offset newline-pos)))
  1929. ;; Update the gdbmi-bnf-offset only if the current chunk of data can
  1930. ;; be processed by the class-command handler:
  1931. (when (or is-complete is-progressive)
  1932. (setq result-str
  1933. (substring gud-marker-acc gdbmi-bnf-offset newline-pos))
  1934. ;; Move gdbmi-bnf-offset past the end of the chunk.
  1935. (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length result-str)))
  1936. (when newline-pos
  1937. (setq gdbmi-bnf-offset (1+ gdbmi-bnf-offset))))
  1938. ;; Update the parsing state before invoking the handler in class-command
  1939. ;; to make sure it's not left in an invalid state if the handler was
  1940. ;; to generate an error.
  1941. (if is-complete
  1942. (setq gdbmi-bnf-state 'gdbmi-bnf-output))
  1943. (if class-command
  1944. (if is-progressive
  1945. (funcall (car class-command) token result-str is-complete)
  1946. (if is-complete
  1947. (funcall (car class-command) token result-str))))
  1948. (unless is-complete
  1949. ;; Incomplete gdb response: abort parsing until we receive more data.
  1950. (if gdbmi-debug-mode (message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
  1951. (throw 'gdbmi-incomplete-stream nil))
  1952. is-complete)))
  1953. ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
  1954. ; The handling of those rules is currently done by the handlers registered
  1955. ; in gdbmi-bnf-result-state-configs
  1956. ;
  1957. ; result ==>
  1958. ; variable "=" value
  1959. ;
  1960. ; variable ==>
  1961. ; string
  1962. ;
  1963. ; value ==>
  1964. ; const | tuple | list
  1965. ;
  1966. ; const ==>
  1967. ; c-string
  1968. ;
  1969. ; tuple ==>
  1970. ; "{}" | "{" result ( "," result )* "}"
  1971. ;
  1972. ; list ==>
  1973. ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
  1974. (defcustom gdb-mi-decode-strings nil
  1975. "When non-nil, decode octal escapes in GDB output into non-ASCII text.
  1976. If the value is a coding-system, use that coding-system to decode
  1977. the bytes reconstructed from octal escapes. Any other non-nil value
  1978. means to decode using the coding-system set for the GDB process.
  1979. Warning: setting this non-nil might mangle strings reported by GDB
  1980. that have literal substrings which match the \\nnn octal escape
  1981. patterns, where nnn is an octal number between 200 and 377. So
  1982. we only recommend to set this variable non-nil if the program you
  1983. are debugging really reports non-ASCII text, or some of its source
  1984. file names include non-ASCII characters."
  1985. :type '(choice
  1986. (const :tag "Don't decode" nil)
  1987. (const :tag "Decode using default coding-system" t)
  1988. (coding-system :tag "Decode using this coding-system"))
  1989. :group 'gdb
  1990. :version "25.1")
  1991. ;; The idea of the following function was suggested
  1992. ;; by Kenichi Handa <handa@gnu.org>.
  1993. ;;
  1994. ;; FIXME: This is fragile: it relies on the assumption that all the
  1995. ;; non-ASCII strings output by GDB, including names of the source
  1996. ;; files, values of string variables in the inferior, etc., are all
  1997. ;; encoded in the same encoding. It also assumes that the \nnn
  1998. ;; sequences are not split between chunks of output of the GDB process
  1999. ;; due to buffering, and arrive together. Finally, if some string
  2000. ;; included literal \nnn strings (as opposed to non-ASCII characters
  2001. ;; converted by by GDB/MI to octal escapes), this decoding will mangle
  2002. ;; those strings. When/if GDB acquires the ability to not
  2003. ;; escape-protect non-ASCII characters in its MI output, this kludge
  2004. ;; should be removed.
  2005. (defun gdb-mi-decode (string)
  2006. "Decode octal escapes in MI output STRING into multibyte text."
  2007. (let ((coding
  2008. (if (coding-system-p gdb-mi-decode-strings)
  2009. gdb-mi-decode-strings
  2010. (with-current-buffer
  2011. (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2012. buffer-file-coding-system))))
  2013. (with-temp-buffer
  2014. (set-buffer-multibyte nil)
  2015. (prin1 string (current-buffer))
  2016. (goto-char (point-min))
  2017. ;; prin1 quotes the octal escapes as well, which interferes with
  2018. ;; their interpretation by 'read' below. Remove the extra
  2019. ;; backslashes to countermand that.
  2020. (while (re-search-forward "\\\\\\(\\\\[2-3][0-7][0-7]\\)" nil t)
  2021. (replace-match "\\1" nil nil))
  2022. (goto-char (point-min))
  2023. (decode-coding-string (read (current-buffer)) coding))))
  2024. (defun gud-gdbmi-marker-filter (string)
  2025. "Filter GDB/MI output."
  2026. ;; If required, decode non-ASCII text encoded with octal escapes.
  2027. (or (null gdb-mi-decode-strings)
  2028. (setq string (gdb-mi-decode string)))
  2029. ;; Record transactions if logging is enabled.
  2030. (when gdb-enable-debug
  2031. (push (cons 'recv string) gdb-debug-log)
  2032. (if (and gdb-debug-log-max
  2033. (> (length gdb-debug-log) gdb-debug-log-max))
  2034. (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
  2035. ;; Recall the left over gud-marker-acc from last time.
  2036. (setq gud-marker-acc (concat gud-marker-acc string))
  2037. ;; Start accumulating output for the GUD buffer.
  2038. (setq gdb-filter-output "")
  2039. (let ((acc-length (length gud-marker-acc)))
  2040. (catch 'gdbmi-incomplete-stream
  2041. (while (and (< gdbmi-bnf-offset acc-length)
  2042. (funcall gdbmi-bnf-state)))))
  2043. (when (/= gdbmi-bnf-offset 0)
  2044. (setq gud-marker-acc (substring gud-marker-acc gdbmi-bnf-offset))
  2045. (setq gdbmi-bnf-offset 0))
  2046. (when (and gdbmi-debug-mode (> (length gud-marker-acc) 0))
  2047. (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc))
  2048. gdb-filter-output)
  2049. (defun gdb-gdb (_output-field))
  2050. (defun gdb-shell (output-field)
  2051. (setq gdb-filter-output
  2052. (concat output-field gdb-filter-output)))
  2053. (defun gdb-ignored-notification (_token _output-field))
  2054. ;; gdb-invalidate-threads is defined to accept 'update-threads signal
  2055. (defun gdb-thread-created (_token _output-field))
  2056. (defun gdb-thread-exited (_token output-field)
  2057. "Handle =thread-exited async record.
  2058. Unset `gdb-thread-number' if current thread exited and update threads list."
  2059. (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
  2060. (if (string= gdb-thread-number thread-id)
  2061. (gdb-setq-thread-number nil))
  2062. ;; When we continue current thread and it quickly exits,
  2063. ;; the pending triggers in gdb-handler-list left after gdb-running
  2064. ;; disallow us to properly call -thread-info without --thread option.
  2065. ;; Thus we need to use gdb-wait-for-pending.
  2066. (gdb-wait-for-pending
  2067. (gdb-emit-signal gdb-buf-publisher 'update-threads))))
  2068. (defun gdb-thread-selected (_token output-field)
  2069. "Handler for =thread-selected MI output record.
  2070. Sets `gdb-thread-number' to new id."
  2071. (let* ((result (gdb-json-string output-field))
  2072. (thread-id (bindat-get-field result 'id)))
  2073. (gdb-setq-thread-number thread-id)
  2074. ;; Typing `thread N' in GUD buffer makes GDB emit `^done' followed
  2075. ;; by `=thread-selected' notification. `^done' causes `gdb-update'
  2076. ;; as usually. Things happen too fast and second call (from
  2077. ;; gdb-thread-selected handler) gets cut off by our beloved
  2078. ;; pending triggers.
  2079. ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
  2080. ;; body will get executed when `gdb-handler-list' if free of
  2081. ;; pending triggers.
  2082. (gdb-wait-for-pending
  2083. (gdb-update))))
  2084. (defun gdb-running (_token output-field)
  2085. (let* ((thread-id
  2086. (bindat-get-field (gdb-json-string output-field) 'thread-id)))
  2087. ;; We reset gdb-frame-number to nil if current thread has gone
  2088. ;; running. This can't be done in gdb-thread-list-handler-custom
  2089. ;; because we need correct gdb-frame-number by the time
  2090. ;; -thread-info command is sent.
  2091. (when (or (string-equal thread-id "all")
  2092. (string-equal thread-id gdb-thread-number))
  2093. (setq gdb-frame-number nil)))
  2094. (setq gdb-inferior-status "running")
  2095. (gdb-force-mode-line-update
  2096. (propertize gdb-inferior-status 'face font-lock-type-face))
  2097. (when (not gdb-non-stop)
  2098. (setq gud-running t))
  2099. (setq gdb-active-process t))
  2100. (defun gdb-starting (_output-field _result)
  2101. ;; CLI commands don't emit ^running at the moment so use gdb-running too.
  2102. (setq gdb-inferior-status "running")
  2103. (gdb-force-mode-line-update
  2104. (propertize gdb-inferior-status 'face font-lock-type-face))
  2105. (setq gdb-active-process t)
  2106. (setq gud-running t))
  2107. ;; -break-insert -t didn't give a reason before gdb 6.9
  2108. (defun gdb-stopped (_token output-field)
  2109. "Given the contents of *stopped MI async record, select new
  2110. current thread and update GDB buffers."
  2111. ;; Reason is available with target-async only
  2112. (let* ((result (gdb-json-string output-field))
  2113. (reason (bindat-get-field result 'reason))
  2114. (thread-id (bindat-get-field result 'thread-id)))
  2115. ;; -data-list-register-names needs to be issued for any stopped
  2116. ;; thread
  2117. (when (not gdb-register-names)
  2118. (gdb-input (concat "-data-list-register-names"
  2119. (if gdb-supports-non-stop
  2120. (concat " --thread " thread-id)))
  2121. 'gdb-register-names-handler))
  2122. ;; Don't set gud-last-frame here as it's currently done in
  2123. ;; gdb-frame-handler because synchronous GDB doesn't give these fields
  2124. ;; with CLI.
  2125. ;;(when file
  2126. ;; (setq
  2127. ;; ;; Extract the frame position from the marker.
  2128. ;; gud-last-frame (cons file
  2129. ;; (string-to-number
  2130. ;; (match-string 6 gud-marker-acc)))))
  2131. (setq gdb-inferior-status (or reason "unknown"))
  2132. (gdb-force-mode-line-update
  2133. (propertize gdb-inferior-status 'face font-lock-warning-face))
  2134. (if (string-equal reason "exited-normally")
  2135. (setq gdb-active-process nil))
  2136. ;; Select new current thread.
  2137. ;; Don't switch if we have no reasons selected
  2138. (when gdb-switch-reasons
  2139. ;; Switch from another stopped thread only if we have
  2140. ;; gdb-switch-when-another-stopped:
  2141. (when (or gdb-switch-when-another-stopped
  2142. (not (string= "stopped"
  2143. (bindat-get-field (gdb-current-buffer-thread) 'state))))
  2144. ;; Switch if current reason has been selected or we have no
  2145. ;; reasons
  2146. (if (or (eq gdb-switch-reasons t)
  2147. (member reason gdb-switch-reasons))
  2148. (when (not (string-equal gdb-thread-number thread-id))
  2149. (message "Switched to thread %s" thread-id)
  2150. (gdb-setq-thread-number thread-id))
  2151. (message "Thread %s stopped" thread-id))))
  2152. ;; Print "(gdb)" to GUD console
  2153. (when gdb-first-done-or-error
  2154. (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
  2155. ;; In non-stop, we update information as soon as another thread gets
  2156. ;; stopped
  2157. (when (or gdb-first-done-or-error
  2158. gdb-non-stop)
  2159. ;; In all-stop this updates gud-running properly as well.
  2160. (gdb-update)
  2161. (setq gdb-first-done-or-error nil))
  2162. (run-hook-with-args 'gdb-stopped-functions result)))
  2163. ;; Remove the trimmings from log stream containing debugging messages
  2164. ;; being produced by GDB's internals, use warning face and send to GUD
  2165. ;; buffer.
  2166. (defun gdb-internals (output-field)
  2167. (setq gdb-filter-output
  2168. (gdb-concat-output
  2169. gdb-filter-output
  2170. (if (string= output-field "\"\\n\"")
  2171. ""
  2172. (let ((error-message
  2173. (read output-field)))
  2174. (put-text-property
  2175. 0 (length error-message)
  2176. 'face font-lock-warning-face
  2177. error-message)
  2178. error-message)))))
  2179. ;; Remove the trimmings from the console stream and send to GUD buffer
  2180. ;; (frontend MI commands should not print to this stream)
  2181. (defun gdb-console (output-field)
  2182. (setq gdb-filter-output
  2183. (gdb-concat-output gdb-filter-output (read output-field))))
  2184. (defun gdb-done (token-number output-field is-complete)
  2185. (gdb-done-or-error token-number 'done output-field is-complete))
  2186. (defun gdb-error (token-number output-field is-complete)
  2187. (gdb-done-or-error token-number 'error output-field is-complete))
  2188. (defun gdb-done-or-error (token-number type output-field is-complete)
  2189. (if (string-equal token-number "")
  2190. ;; Output from command entered by user
  2191. (progn
  2192. (setq gdb-output-sink 'user)
  2193. (setq token-number nil)
  2194. ;; MI error - send to minibuffer
  2195. (when (eq type 'error)
  2196. ;; Skip "msg=" from `output-field'
  2197. (message "%s" (read (substring output-field 4)))
  2198. ;; Don't send to the console twice. (If it is a console error
  2199. ;; it is also in the console stream.)
  2200. (setq output-field nil)))
  2201. ;; Output from command from frontend.
  2202. (setq gdb-output-sink 'emacs))
  2203. ;; The process may already be dead (e.g. C-d at the gdb prompt).
  2204. (let* ((proc (get-buffer-process gud-comint-buffer))
  2205. (no-proc (or (null proc)
  2206. (memq (process-status proc) '(exit signal)))))
  2207. (when (and is-complete gdb-first-done-or-error)
  2208. (unless (or token-number gud-running no-proc)
  2209. (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
  2210. (gdb-update no-proc)
  2211. (setq gdb-first-done-or-error nil))
  2212. (setq gdb-filter-output
  2213. (gdb-concat-output gdb-filter-output output-field))
  2214. ;; We are done concatenating to the output sink. Restore it to user sink:
  2215. (setq gdb-output-sink 'user)
  2216. (when (and token-number is-complete)
  2217. (with-current-buffer
  2218. (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2219. (gdb-handle-reply (string-to-number token-number))))
  2220. (when is-complete
  2221. (gdb-clear-partial-output))))
  2222. (defun gdb-concat-output (so-far new)
  2223. (cond
  2224. ((eq gdb-output-sink 'user) (concat so-far new))
  2225. ((eq gdb-output-sink 'emacs)
  2226. (gdb-append-to-partial-output new)
  2227. so-far)))
  2228. (defun gdb-append-to-partial-output (string)
  2229. (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2230. (goto-char (point-max))
  2231. (insert string)))
  2232. (defun gdb-clear-partial-output ()
  2233. (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2234. (erase-buffer)))
  2235. (defun gdb-jsonify-buffer (&optional fix-key fix-list)
  2236. "Prepare GDB/MI output in current buffer for parsing with `json-read'.
  2237. Field names are wrapped in double quotes and equal signs are
  2238. replaced with semicolons.
  2239. If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
  2240. partial output. This is used to get rid of useless keys in lists
  2241. in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
  2242. -break-info are examples of MI commands which issue such
  2243. responses.
  2244. If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
  2245. \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
  2246. -break-info output when it contains breakpoint script field
  2247. incompatible with GDB/MI output syntax."
  2248. (save-excursion
  2249. (goto-char (point-min))
  2250. (when fix-key
  2251. (save-excursion
  2252. (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
  2253. (replace-match "" nil nil nil 1))))
  2254. (when fix-list
  2255. (save-excursion
  2256. ;; Find positions of braces which enclose broken list
  2257. (while (re-search-forward (concat fix-list "={\"") nil t)
  2258. (let ((p1 (goto-char (- (point) 2)))
  2259. (p2 (progn (forward-sexp)
  2260. (1- (point)))))
  2261. ;; Replace braces with brackets
  2262. (save-excursion
  2263. (goto-char p1)
  2264. (delete-char 1)
  2265. (insert "[")
  2266. (goto-char p2)
  2267. (delete-char 1)
  2268. (insert "]"))))))
  2269. (goto-char (point-min))
  2270. (insert "{")
  2271. (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
  2272. gdb--string-regexp "\\)")))
  2273. (while (re-search-forward re nil t)
  2274. (replace-match "\"\\1\":\\2" nil nil)))
  2275. (goto-char (point-max))
  2276. (insert "}")))
  2277. (defun gdb-json-read-buffer (&optional fix-key fix-list)
  2278. "Prepare and parse GDB/MI output in current buffer with `json-read'.
  2279. FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
  2280. (gdb-jsonify-buffer fix-key fix-list)
  2281. (save-excursion
  2282. (goto-char (point-min))
  2283. (let ((json-array-type 'list))
  2284. (json-read))))
  2285. (defun gdb-json-string (string &optional fix-key fix-list)
  2286. "Prepare and parse STRING containing GDB/MI output with `json-read'.
  2287. FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
  2288. (with-temp-buffer
  2289. (insert string)
  2290. (gdb-json-read-buffer fix-key fix-list)))
  2291. (defun gdb-json-partial-output (&optional fix-key fix-list)
  2292. "Prepare and parse gdb-partial-output-buffer with `json-read'.
  2293. FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
  2294. (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2295. (gdb-json-read-buffer fix-key fix-list)))
  2296. (defun gdb-line-posns (line)
  2297. "Return a pair of LINE beginning and end positions."
  2298. (let ((offset (1+ (- line (line-number-at-pos)))))
  2299. (cons
  2300. (line-beginning-position offset)
  2301. (line-end-position offset))))
  2302. (defmacro gdb-mark-line (line variable)
  2303. "Set VARIABLE marker to point at beginning of LINE.
  2304. If current window has no fringes, inverse colors on LINE.
  2305. Return position where LINE begins."
  2306. `(save-excursion
  2307. (let* ((posns (gdb-line-posns ,line))
  2308. (start-posn (car posns))
  2309. (end-posn (cdr posns)))
  2310. (set-marker ,variable (copy-marker start-posn))
  2311. (when (not (> (car (window-fringes)) 0))
  2312. (put-text-property start-posn end-posn
  2313. 'font-lock-face '(:inverse-video t)))
  2314. start-posn)))
  2315. (defun gdb-pad-string (string padding)
  2316. (format (concat "%" (number-to-string padding) "s") string))
  2317. ;; gdb-table struct is a way to programmatically construct simple
  2318. ;; tables. It help to reliably align columns of data in GDB buffers
  2319. ;; and provides
  2320. (cl-defstruct gdb-table
  2321. (column-sizes nil)
  2322. (rows nil)
  2323. (row-properties nil)
  2324. (right-align nil))
  2325. (defun gdb-table-add-row (table row &optional properties)
  2326. "Add ROW of string to TABLE and recalculate column sizes.
  2327. When non-nil, PROPERTIES will be added to the whole row when
  2328. calling `gdb-table-string'."
  2329. (let ((rows (gdb-table-rows table))
  2330. (row-properties (gdb-table-row-properties table))
  2331. (column-sizes (gdb-table-column-sizes table))
  2332. (right-align (gdb-table-right-align table)))
  2333. (when (not column-sizes)
  2334. (setf (gdb-table-column-sizes table)
  2335. (make-list (length row) 0)))
  2336. (setf (gdb-table-rows table)
  2337. (append rows (list row)))
  2338. (setf (gdb-table-row-properties table)
  2339. (append row-properties (list properties)))
  2340. (setf (gdb-table-column-sizes table)
  2341. (cl-mapcar (lambda (x s)
  2342. (let ((new-x
  2343. (max (abs x) (string-width (or s "")))))
  2344. (if right-align new-x (- new-x))))
  2345. (gdb-table-column-sizes table)
  2346. row))
  2347. ;; Avoid trailing whitespace at eol
  2348. (if (not (gdb-table-right-align table))
  2349. (setcar (last (gdb-table-column-sizes table)) 0))))
  2350. (defun gdb-table-string (table &optional sep)
  2351. "Return TABLE as a string with columns separated with SEP."
  2352. (let ((column-sizes (gdb-table-column-sizes table)))
  2353. (mapconcat
  2354. 'identity
  2355. (cl-mapcar
  2356. (lambda (row properties)
  2357. (apply 'propertize
  2358. (mapconcat 'identity
  2359. (cl-mapcar (lambda (s x) (gdb-pad-string s x))
  2360. row column-sizes)
  2361. sep)
  2362. properties))
  2363. (gdb-table-rows table)
  2364. (gdb-table-row-properties table))
  2365. "\n")))
  2366. ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
  2367. (defun gdb-get-many-fields (struct &rest fields)
  2368. "Return a list of FIELDS values from STRUCT."
  2369. (let ((values))
  2370. (dolist (field fields)
  2371. (push (bindat-get-field struct field) values))
  2372. (nreverse values)))
  2373. (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
  2374. handler-name
  2375. &optional signal-list)
  2376. "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
  2377. HANDLER-NAME as its handler. HANDLER-NAME is bound to current
  2378. buffer with `gdb-bind-function-to-buffer'.
  2379. If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
  2380. defined trigger is called with an argument from SIGNAL-LIST. It's
  2381. not recommended to define triggers with empty SIGNAL-LIST.
  2382. Normally triggers should respond at least to 'update signal.
  2383. Normally the trigger defined by this command must be called from
  2384. the buffer where HANDLER-NAME must work. This should be done so
  2385. that buffer-local thread number may be used in GDB-COMMAND (by
  2386. calling `gdb-current-context-command').
  2387. `gdb-bind-function-to-buffer' is used to achieve this, see
  2388. `gdb-get-buffer-create'.
  2389. Triggers defined by this command are meant to be used as a
  2390. trigger argument when describing buffer types with
  2391. `gdb-set-buffer-rules'."
  2392. `(defun ,trigger-name (&optional signal)
  2393. (when
  2394. (or (not ,signal-list)
  2395. (memq signal ,signal-list))
  2396. (gdb-input ,gdb-command
  2397. (gdb-bind-function-to-buffer ',handler-name (current-buffer))
  2398. (cons (current-buffer) ',trigger-name)))))
  2399. ;; Used by disassembly buffer only, the rest use
  2400. ;; def-gdb-trigger-and-handler
  2401. (defmacro def-gdb-auto-update-handler (handler-name custom-defun
  2402. &optional nopreserve)
  2403. "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
  2404. Handlers are normally called from the buffers they put output in.
  2405. Erase current buffer and evaluate CUSTOM-DEFUN.
  2406. Then call `gdb-update-buffer-name'.
  2407. If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
  2408. `(defun ,handler-name ()
  2409. (let* ((inhibit-read-only t)
  2410. ,@(unless nopreserve
  2411. '((window (get-buffer-window (current-buffer) 0))
  2412. (start (window-start window))
  2413. (p (window-point window)))))
  2414. (erase-buffer)
  2415. (,custom-defun)
  2416. (gdb-update-buffer-name)
  2417. ,@(when (not nopreserve)
  2418. '((set-window-start window start)
  2419. (set-window-point window p))))))
  2420. (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
  2421. handler-name custom-defun
  2422. &optional signal-list)
  2423. "Define trigger and handler.
  2424. TRIGGER-NAME trigger is defined to send GDB-COMMAND.
  2425. See `def-gdb-auto-update-trigger'.
  2426. HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
  2427. See `def-gdb-auto-update-handler'."
  2428. `(progn
  2429. (def-gdb-auto-update-trigger ,trigger-name
  2430. ,gdb-command
  2431. ,handler-name ,signal-list)
  2432. (def-gdb-auto-update-handler ,handler-name
  2433. ,custom-defun)))
  2434. ;; Breakpoint buffer : This displays the output of `-break-list'.
  2435. (def-gdb-trigger-and-handler
  2436. gdb-invalidate-breakpoints "-break-list"
  2437. gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
  2438. '(start update))
  2439. (gdb-set-buffer-rules
  2440. 'gdb-breakpoints-buffer
  2441. 'gdb-breakpoints-buffer-name
  2442. 'gdb-breakpoints-mode
  2443. 'gdb-invalidate-breakpoints)
  2444. (defun gdb-breakpoints-list-handler-custom ()
  2445. (let ((breakpoints-list (bindat-get-field
  2446. (gdb-json-partial-output "bkpt" "script")
  2447. 'BreakpointTable 'body))
  2448. (table (make-gdb-table)))
  2449. (setq gdb-breakpoints-list nil)
  2450. (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
  2451. (dolist (breakpoint breakpoints-list)
  2452. (add-to-list 'gdb-breakpoints-list
  2453. (cons (bindat-get-field breakpoint 'number)
  2454. breakpoint))
  2455. (let ((at (bindat-get-field breakpoint 'at))
  2456. (pending (bindat-get-field breakpoint 'pending))
  2457. (func (bindat-get-field breakpoint 'func))
  2458. (type (bindat-get-field breakpoint 'type)))
  2459. (gdb-table-add-row table
  2460. (list
  2461. (bindat-get-field breakpoint 'number)
  2462. (or type "")
  2463. (or (bindat-get-field breakpoint 'disp) "")
  2464. (let ((flag (bindat-get-field breakpoint 'enabled)))
  2465. (if (string-equal flag "y")
  2466. (eval-when-compile
  2467. (propertize "y" 'font-lock-face
  2468. font-lock-warning-face))
  2469. (eval-when-compile
  2470. (propertize "n" 'font-lock-face
  2471. font-lock-comment-face))))
  2472. (bindat-get-field breakpoint 'addr)
  2473. (or (bindat-get-field breakpoint 'times) "")
  2474. (if (and type (string-match ".*watchpoint" type))
  2475. (bindat-get-field breakpoint 'what)
  2476. (or pending at
  2477. (concat "in "
  2478. (propertize (or func "unknown")
  2479. 'font-lock-face font-lock-function-name-face)
  2480. (gdb-frame-location breakpoint)))))
  2481. ;; Add clickable properties only for breakpoints with file:line
  2482. ;; information
  2483. (append (list 'gdb-breakpoint breakpoint)
  2484. (when func '(help-echo "mouse-2, RET: visit breakpoint"
  2485. mouse-face highlight))))))
  2486. (insert (gdb-table-string table " "))
  2487. (gdb-place-breakpoints)))
  2488. ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
  2489. (defun gdb-place-breakpoints ()
  2490. ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
  2491. (dolist (buffer (buffer-list))
  2492. (with-current-buffer buffer
  2493. (if (and (eq gud-minor-mode 'gdbmi)
  2494. (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
  2495. (gdb-remove-breakpoint-icons (point-min) (point-max)))))
  2496. (dolist (breakpoint gdb-breakpoints-list)
  2497. (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
  2498. ; an associative list
  2499. (line (bindat-get-field breakpoint 'line)))
  2500. (when line
  2501. (let ((file (bindat-get-field breakpoint 'fullname))
  2502. (flag (bindat-get-field breakpoint 'enabled))
  2503. (bptno (bindat-get-field breakpoint 'number)))
  2504. (unless (and file (file-exists-p file))
  2505. (setq file (cdr (assoc bptno gdb-location-alist))))
  2506. (if (or (null file)
  2507. (string-equal file "File not found"))
  2508. ;; If the full filename is not recorded in the
  2509. ;; breakpoint structure or in `gdb-location-alist', use
  2510. ;; -file-list-exec-source-file to extract it.
  2511. (when (setq file (bindat-get-field breakpoint 'file))
  2512. (gdb-input (concat "list " file ":1") 'ignore)
  2513. (gdb-input "-file-list-exec-source-file"
  2514. `(lambda () (gdb-get-location
  2515. ,bptno ,line ,flag))))
  2516. (with-current-buffer (find-file-noselect file 'nowarn)
  2517. (gdb-init-buffer)
  2518. ;; Only want one breakpoint icon at each location.
  2519. (gdb-put-breakpoint-icon (string-equal flag "y") bptno
  2520. (string-to-number line)))))))))
  2521. (defconst gdb-source-file-regexp
  2522. (concat "fullname=\\(" gdb--string-regexp "\\)"))
  2523. (defun gdb-get-location (bptno line flag)
  2524. "Find the directory containing the relevant source file.
  2525. Put in buffer and place breakpoint icon."
  2526. (goto-char (point-min))
  2527. (catch 'file-not-found
  2528. (if (re-search-forward gdb-source-file-regexp nil t)
  2529. (delete (cons bptno "File not found") gdb-location-alist)
  2530. ;; FIXME: Why/how do we use (match-string 1) when the search failed?
  2531. (push (cons bptno (match-string 1)) gdb-location-alist)
  2532. (gdb-resync)
  2533. (unless (assoc bptno gdb-location-alist)
  2534. (push (cons bptno "File not found") gdb-location-alist)
  2535. (message-box "Cannot find source file for breakpoint location.
  2536. Add directory to search path for source files using the GDB command, dir."))
  2537. (throw 'file-not-found nil))
  2538. (with-current-buffer (find-file-noselect (match-string 1))
  2539. (gdb-init-buffer)
  2540. ;; only want one breakpoint icon at each location
  2541. (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
  2542. (add-hook 'find-file-hook 'gdb-find-file-hook)
  2543. (defun gdb-find-file-hook ()
  2544. "Set up buffer for debugging if file is part of the source code
  2545. of the current session."
  2546. (if (and (buffer-name gud-comint-buffer)
  2547. ;; in case gud or gdb-ui is just loaded
  2548. gud-comint-buffer
  2549. (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  2550. 'gdbmi))
  2551. (if (member buffer-file-name gdb-source-file-list)
  2552. (with-current-buffer (find-buffer-visiting buffer-file-name)
  2553. (gdb-init-buffer)))))
  2554. (declare-function gud-remove "gdb-mi" t t) ; gud-def
  2555. (declare-function gud-break "gdb-mi" t t) ; gud-def
  2556. (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
  2557. (defun gdb-mouse-set-clear-breakpoint (event)
  2558. "Set/clear breakpoint in left fringe/margin at mouse click.
  2559. If not in a source or disassembly buffer just set point."
  2560. (interactive "e")
  2561. (mouse-minibuffer-check event)
  2562. (let ((posn (event-end event)))
  2563. (with-selected-window (posn-window posn)
  2564. (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
  2565. (if (numberp (posn-point posn))
  2566. (save-excursion
  2567. (goto-char (posn-point posn))
  2568. (if (or (posn-object posn)
  2569. (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
  2570. 'breakpoint))
  2571. (gud-remove nil)
  2572. (gud-break nil)))))
  2573. (posn-set-point posn))))
  2574. (defun gdb-mouse-toggle-breakpoint-margin (event)
  2575. "Enable/disable breakpoint in left margin with mouse click."
  2576. (interactive "e")
  2577. (mouse-minibuffer-check event)
  2578. (let ((posn (event-end event)))
  2579. (if (numberp (posn-point posn))
  2580. (with-selected-window (posn-window posn)
  2581. (save-excursion
  2582. (goto-char (posn-point posn))
  2583. (if (posn-object posn)
  2584. (gud-basic-call
  2585. (let ((bptno (get-text-property
  2586. 0 'gdb-bptno (car (posn-string posn)))))
  2587. (concat
  2588. (if (get-text-property
  2589. 0 'gdb-enabled (car (posn-string posn)))
  2590. "-break-disable "
  2591. "-break-enable ")
  2592. bptno)))))))))
  2593. (defun gdb-mouse-toggle-breakpoint-fringe (event)
  2594. "Enable/disable breakpoint in left fringe with mouse click."
  2595. (interactive "e")
  2596. (mouse-minibuffer-check event)
  2597. (let* ((posn (event-end event))
  2598. (pos (posn-point posn))
  2599. obj)
  2600. (when (numberp pos)
  2601. (with-selected-window (posn-window posn)
  2602. (with-current-buffer (window-buffer)
  2603. (goto-char pos)
  2604. (dolist (overlay (overlays-in pos pos))
  2605. (when (overlay-get overlay 'put-break)
  2606. (setq obj (overlay-get overlay 'before-string))))
  2607. (when (stringp obj)
  2608. (gud-basic-call
  2609. (concat
  2610. (if (get-text-property 0 'gdb-enabled obj)
  2611. "-break-disable "
  2612. "-break-enable ")
  2613. (get-text-property 0 'gdb-bptno obj)))))))))
  2614. (defun gdb-breakpoints-buffer-name ()
  2615. (concat "*breakpoints of " (gdb-get-target-string) "*"))
  2616. (defun gdb-display-breakpoints-buffer (&optional thread)
  2617. "Display GDB breakpoints."
  2618. (interactive)
  2619. (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
  2620. (defun gdb-frame-breakpoints-buffer (&optional thread)
  2621. "Display GDB breakpoints in another frame."
  2622. (interactive)
  2623. (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
  2624. gdb-display-buffer-other-frame-action))
  2625. (defvar gdb-breakpoints-mode-map
  2626. (let ((map (make-sparse-keymap))
  2627. (menu (make-sparse-keymap "Breakpoints")))
  2628. (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
  2629. (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
  2630. (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
  2631. (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
  2632. (suppress-keymap map)
  2633. (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
  2634. (define-key map " " 'gdb-toggle-breakpoint)
  2635. (define-key map "D" 'gdb-delete-breakpoint)
  2636. ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
  2637. (define-key map "q" 'gdb-delete-frame-or-window)
  2638. (define-key map "\r" 'gdb-goto-breakpoint)
  2639. (define-key map "\t" (lambda ()
  2640. (interactive)
  2641. (gdb-set-window-buffer
  2642. (gdb-get-buffer-create 'gdb-threads-buffer) t)))
  2643. (define-key map [mouse-2] 'gdb-goto-breakpoint)
  2644. (define-key map [follow-link] 'mouse-face)
  2645. map))
  2646. (defun gdb-delete-frame-or-window ()
  2647. "Delete frame if there is only one window. Otherwise delete the window."
  2648. (interactive)
  2649. (if (one-window-p) (delete-frame)
  2650. (delete-window)))
  2651. ;;from make-mode-line-mouse-map
  2652. (defun gdb-make-header-line-mouse-map (mouse function) "\
  2653. Return a keymap with single entry for mouse key MOUSE on the header line.
  2654. MOUSE is defined to run function FUNCTION with no args in the buffer
  2655. corresponding to the mode line clicked."
  2656. (let ((map (make-sparse-keymap)))
  2657. (define-key map (vector 'header-line mouse) function)
  2658. (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
  2659. map))
  2660. (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
  2661. `(propertize ,name
  2662. 'help-echo ,help-echo
  2663. 'mouse-face ',mouse-face
  2664. 'face ',face
  2665. 'local-map
  2666. (gdb-make-header-line-mouse-map
  2667. 'mouse-1
  2668. (lambda (event) (interactive "e")
  2669. (save-selected-window
  2670. (select-window (posn-window (event-start event)))
  2671. (gdb-set-window-buffer
  2672. (gdb-get-buffer-create ',buffer) t) )))))
  2673. ;; uses "-thread-info". Needs GDB 7.0 onwards.
  2674. ;;; Threads view
  2675. (defun gdb-threads-buffer-name ()
  2676. (concat "*threads of " (gdb-get-target-string) "*"))
  2677. (defun gdb-display-threads-buffer (&optional thread)
  2678. "Display GDB threads."
  2679. (interactive)
  2680. (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
  2681. (defun gdb-frame-threads-buffer (&optional thread)
  2682. "Display GDB threads in another frame."
  2683. (interactive)
  2684. (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
  2685. gdb-display-buffer-other-frame-action))
  2686. (def-gdb-trigger-and-handler
  2687. gdb-invalidate-threads (gdb-current-context-command "-thread-info")
  2688. gdb-thread-list-handler gdb-thread-list-handler-custom
  2689. '(start update update-threads))
  2690. (gdb-set-buffer-rules
  2691. 'gdb-threads-buffer
  2692. 'gdb-threads-buffer-name
  2693. 'gdb-threads-mode
  2694. 'gdb-invalidate-threads)
  2695. (defvar gdb-threads-font-lock-keywords
  2696. '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
  2697. (" \\(stopped\\)" (1 font-lock-warning-face))
  2698. (" \\(running\\)" (1 font-lock-string-face))
  2699. ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
  2700. "Font lock keywords used in `gdb-threads-mode'.")
  2701. (defvar gdb-threads-mode-map
  2702. (let ((map (make-sparse-keymap)))
  2703. (define-key map "\r" 'gdb-select-thread)
  2704. (define-key map "f" 'gdb-display-stack-for-thread)
  2705. (define-key map "F" 'gdb-frame-stack-for-thread)
  2706. (define-key map "l" 'gdb-display-locals-for-thread)
  2707. (define-key map "L" 'gdb-frame-locals-for-thread)
  2708. (define-key map "r" 'gdb-display-registers-for-thread)
  2709. (define-key map "R" 'gdb-frame-registers-for-thread)
  2710. (define-key map "d" 'gdb-display-disassembly-for-thread)
  2711. (define-key map "D" 'gdb-frame-disassembly-for-thread)
  2712. (define-key map "i" 'gdb-interrupt-thread)
  2713. (define-key map "c" 'gdb-continue-thread)
  2714. (define-key map "s" 'gdb-step-thread)
  2715. (define-key map "\t"
  2716. (lambda ()
  2717. (interactive)
  2718. (gdb-set-window-buffer
  2719. (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
  2720. (define-key map [mouse-2] 'gdb-select-thread)
  2721. (define-key map [follow-link] 'mouse-face)
  2722. map))
  2723. (defvar gdb-threads-header
  2724. (list
  2725. (gdb-propertize-header
  2726. "Breakpoints" gdb-breakpoints-buffer
  2727. "mouse-1: select" mode-line-highlight mode-line-inactive)
  2728. " "
  2729. (gdb-propertize-header "Threads" gdb-threads-buffer
  2730. nil nil mode-line)))
  2731. (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
  2732. "Major mode for GDB threads."
  2733. (setq gdb-thread-position (make-marker))
  2734. (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
  2735. (setq header-line-format gdb-threads-header)
  2736. (set (make-local-variable 'font-lock-defaults)
  2737. '(gdb-threads-font-lock-keywords))
  2738. 'gdb-invalidate-threads)
  2739. (defun gdb-thread-list-handler-custom ()
  2740. (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
  2741. (table (make-gdb-table))
  2742. (marked-line nil))
  2743. (setq gdb-threads-list nil)
  2744. (setq gdb-running-threads-count 0)
  2745. (setq gdb-stopped-threads-count 0)
  2746. (set-marker gdb-thread-position nil)
  2747. (dolist (thread (reverse threads-list))
  2748. (let ((running (equal (bindat-get-field thread 'state) "running")))
  2749. (add-to-list 'gdb-threads-list
  2750. (cons (bindat-get-field thread 'id)
  2751. thread))
  2752. (cl-incf (if running
  2753. gdb-running-threads-count
  2754. gdb-stopped-threads-count))
  2755. (gdb-table-add-row
  2756. table
  2757. (list
  2758. (bindat-get-field thread 'id)
  2759. (concat
  2760. (if gdb-thread-buffer-verbose-names
  2761. (concat (bindat-get-field thread 'target-id) " ") "")
  2762. (bindat-get-field thread 'state)
  2763. ;; Include frame information for stopped threads
  2764. (if (not running)
  2765. (concat
  2766. " in " (bindat-get-field thread 'frame 'func)
  2767. (if gdb-thread-buffer-arguments
  2768. (concat
  2769. " ("
  2770. (let ((args (bindat-get-field thread 'frame 'args)))
  2771. (mapconcat
  2772. (lambda (arg)
  2773. (apply #'format "%s=%s"
  2774. (gdb-get-many-fields arg 'name 'value)))
  2775. args ","))
  2776. ")")
  2777. "")
  2778. (if gdb-thread-buffer-locations
  2779. (gdb-frame-location (bindat-get-field thread 'frame)) "")
  2780. (if gdb-thread-buffer-addresses
  2781. (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
  2782. "")))
  2783. (list
  2784. 'gdb-thread thread
  2785. 'mouse-face 'highlight
  2786. 'help-echo "mouse-2, RET: select thread")))
  2787. (when (string-equal gdb-thread-number
  2788. (bindat-get-field thread 'id))
  2789. (setq marked-line (length gdb-threads-list))))
  2790. (insert (gdb-table-string table " "))
  2791. (when marked-line
  2792. (gdb-mark-line marked-line gdb-thread-position)))
  2793. ;; We update gud-running here because we need to make sure that
  2794. ;; gdb-threads-list is up-to-date
  2795. (gdb-update-gud-running)
  2796. (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
  2797. (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
  2798. "Define a NAME command which will act upon thread on the current line.
  2799. CUSTOM-DEFUN may use locally bound `thread' variable, which will
  2800. be the value of 'gdb-thread property of the current line.
  2801. If `gdb-thread' is nil, error is signaled."
  2802. `(defun ,name (&optional event)
  2803. ,(when doc doc)
  2804. (interactive (list last-input-event))
  2805. (if event (posn-set-point (event-end event)))
  2806. (save-excursion
  2807. (beginning-of-line)
  2808. (let ((thread (get-text-property (point) 'gdb-thread)))
  2809. (if thread
  2810. ,custom-defun
  2811. (error "Not recognized as thread line"))))))
  2812. (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
  2813. &optional doc)
  2814. "Define a NAME which will call BUFFER-COMMAND with id of thread
  2815. on the current line."
  2816. `(def-gdb-thread-buffer-command ,name
  2817. (,buffer-command (bindat-get-field thread 'id))
  2818. ,doc))
  2819. (def-gdb-thread-buffer-command gdb-select-thread
  2820. (let ((new-id (bindat-get-field thread 'id)))
  2821. (gdb-setq-thread-number new-id)
  2822. (gdb-input (concat "-thread-select " new-id) 'ignore)
  2823. (gdb-update))
  2824. "Select the thread at current line of threads buffer.")
  2825. (def-gdb-thread-buffer-simple-command
  2826. gdb-display-stack-for-thread
  2827. gdb-preemptively-display-stack-buffer
  2828. "Display stack buffer for the thread at current line.")
  2829. (def-gdb-thread-buffer-simple-command
  2830. gdb-display-locals-for-thread
  2831. gdb-preemptively-display-locals-buffer
  2832. "Display locals buffer for the thread at current line.")
  2833. (def-gdb-thread-buffer-simple-command
  2834. gdb-display-registers-for-thread
  2835. gdb-preemptively-display-registers-buffer
  2836. "Display registers buffer for the thread at current line.")
  2837. (def-gdb-thread-buffer-simple-command
  2838. gdb-display-disassembly-for-thread
  2839. gdb-preemptively-display-disassembly-buffer
  2840. "Display disassembly buffer for the thread at current line.")
  2841. (def-gdb-thread-buffer-simple-command
  2842. gdb-frame-stack-for-thread
  2843. gdb-frame-stack-buffer
  2844. "Display another frame with stack buffer for thread at current line.")
  2845. (def-gdb-thread-buffer-simple-command
  2846. gdb-frame-locals-for-thread
  2847. gdb-frame-locals-buffer
  2848. "Display another frame with locals buffer for thread at current line.")
  2849. (def-gdb-thread-buffer-simple-command
  2850. gdb-frame-registers-for-thread
  2851. gdb-frame-registers-buffer
  2852. "Display another frame with registers buffer for the thread at current line.")
  2853. (def-gdb-thread-buffer-simple-command
  2854. gdb-frame-disassembly-for-thread
  2855. gdb-frame-disassembly-buffer
  2856. "Display another frame with disassembly buffer for the thread at current line.")
  2857. (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
  2858. "Define a NAME which will execute GUD-COMMAND with
  2859. `gdb-thread-number' locally bound to id of thread on the current
  2860. line."
  2861. `(def-gdb-thread-buffer-command ,name
  2862. (if gdb-non-stop
  2863. (let ((gdb-thread-number (bindat-get-field thread 'id))
  2864. (gdb-gud-control-all-threads nil))
  2865. (call-interactively #',gud-command))
  2866. (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
  2867. ,doc))
  2868. (def-gdb-thread-buffer-gud-command
  2869. gdb-interrupt-thread
  2870. gud-stop-subjob
  2871. "Interrupt thread at current line.")
  2872. ;; Defined opaquely in M-x gdb via gud-def.
  2873. (declare-function gud-cont "gdb-mi" (arg) t)
  2874. (def-gdb-thread-buffer-gud-command
  2875. gdb-continue-thread
  2876. gud-cont
  2877. "Continue thread at current line.")
  2878. (declare-function gud-step "gdb-mi" (arg) t)
  2879. (def-gdb-thread-buffer-gud-command
  2880. gdb-step-thread
  2881. gud-step
  2882. "Step thread at current line.")
  2883. ;;; Memory view
  2884. (defcustom gdb-memory-rows 8
  2885. "Number of data rows in memory window."
  2886. :type 'integer
  2887. :group 'gud
  2888. :version "23.2")
  2889. (defcustom gdb-memory-columns 4
  2890. "Number of data columns in memory window."
  2891. :type 'integer
  2892. :group 'gud
  2893. :version "23.2")
  2894. (defcustom gdb-memory-format "x"
  2895. "Display format of data items in memory window."
  2896. :type '(choice (const :tag "Hexadecimal" "x")
  2897. (const :tag "Signed decimal" "d")
  2898. (const :tag "Unsigned decimal" "u")
  2899. (const :tag "Octal" "o")
  2900. (const :tag "Binary" "t"))
  2901. :group 'gud
  2902. :version "22.1")
  2903. (defcustom gdb-memory-unit 4
  2904. "Unit size of data items in memory window."
  2905. :type '(choice (const :tag "Byte" 1)
  2906. (const :tag "Halfword" 2)
  2907. (const :tag "Word" 4)
  2908. (const :tag "Giant word" 8))
  2909. :group 'gud
  2910. :version "23.2")
  2911. (def-gdb-trigger-and-handler
  2912. gdb-invalidate-memory
  2913. (format "-data-read-memory %s %s %d %d %d"
  2914. gdb-memory-address
  2915. gdb-memory-format
  2916. gdb-memory-unit
  2917. gdb-memory-rows
  2918. gdb-memory-columns)
  2919. gdb-read-memory-handler
  2920. gdb-read-memory-custom
  2921. '(start update))
  2922. (gdb-set-buffer-rules
  2923. 'gdb-memory-buffer
  2924. 'gdb-memory-buffer-name
  2925. 'gdb-memory-mode
  2926. 'gdb-invalidate-memory)
  2927. (defun gdb-memory-column-width (size format)
  2928. "Return length of string with memory unit of SIZE in FORMAT.
  2929. SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
  2930. in `gdb-memory-format'."
  2931. (let ((format-base (cdr (assoc format
  2932. '(("x" . 16)
  2933. ("d" . 10) ("u" . 10)
  2934. ("o" . 8)
  2935. ("t" . 2))))))
  2936. (if format-base
  2937. (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
  2938. (cond ((string-equal format "x")
  2939. (+ 2 res)) ; hexadecimal numbers have 0x in front
  2940. ((or (string-equal format "d")
  2941. (string-equal format "o"))
  2942. (1+ res))
  2943. (t res)))
  2944. (error "Unknown format"))))
  2945. (defun gdb-read-memory-custom ()
  2946. (let* ((res (gdb-json-partial-output))
  2947. (err-msg (bindat-get-field res 'msg)))
  2948. (if (not err-msg)
  2949. (let ((memory (bindat-get-field res 'memory)))
  2950. (setq gdb-memory-address (bindat-get-field res 'addr))
  2951. (setq gdb-memory-next-page (bindat-get-field res 'next-page))
  2952. (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
  2953. (setq gdb-memory-last-address gdb-memory-address)
  2954. (dolist (row memory)
  2955. (insert (concat (bindat-get-field row 'addr) ":"))
  2956. (dolist (column (bindat-get-field row 'data))
  2957. (insert (gdb-pad-string column
  2958. (+ 2 (gdb-memory-column-width
  2959. gdb-memory-unit
  2960. gdb-memory-format)))))
  2961. (newline)))
  2962. ;; Show last page instead of empty buffer when out of bounds
  2963. (progn
  2964. (let ((gdb-memory-address gdb-memory-last-address))
  2965. (gdb-invalidate-memory 'update)
  2966. (error err-msg))))))
  2967. (defvar gdb-memory-mode-map
  2968. (let ((map (make-sparse-keymap)))
  2969. (suppress-keymap map t)
  2970. (define-key map "q" 'kill-this-buffer)
  2971. (define-key map "n" 'gdb-memory-show-next-page)
  2972. (define-key map "p" 'gdb-memory-show-previous-page)
  2973. (define-key map "a" 'gdb-memory-set-address)
  2974. (define-key map "t" 'gdb-memory-format-binary)
  2975. (define-key map "o" 'gdb-memory-format-octal)
  2976. (define-key map "u" 'gdb-memory-format-unsigned)
  2977. (define-key map "d" 'gdb-memory-format-signed)
  2978. (define-key map "x" 'gdb-memory-format-hexadecimal)
  2979. (define-key map "b" 'gdb-memory-unit-byte)
  2980. (define-key map "h" 'gdb-memory-unit-halfword)
  2981. (define-key map "w" 'gdb-memory-unit-word)
  2982. (define-key map "g" 'gdb-memory-unit-giant)
  2983. (define-key map "R" 'gdb-memory-set-rows)
  2984. (define-key map "C" 'gdb-memory-set-columns)
  2985. map))
  2986. (defun gdb-memory-set-address-event (event)
  2987. "Handle a click on address field in memory buffer header."
  2988. (interactive "e")
  2989. (save-selected-window
  2990. (select-window (posn-window (event-start event)))
  2991. (gdb-memory-set-address)))
  2992. ;; Non-event version for use within keymap
  2993. (defun gdb-memory-set-address ()
  2994. "Set the start memory address."
  2995. (interactive)
  2996. (let ((arg (read-from-minibuffer "Memory address: ")))
  2997. (setq gdb-memory-address arg))
  2998. (gdb-invalidate-memory 'update))
  2999. (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
  3000. "Define a function NAME which reads new VAR value from minibuffer."
  3001. `(defun ,name (event)
  3002. ,(when doc doc)
  3003. (interactive "e")
  3004. (save-selected-window
  3005. (select-window (posn-window (event-start event)))
  3006. (let* ((arg (read-from-minibuffer ,echo-string))
  3007. (count (string-to-number arg)))
  3008. (if (<= count 0)
  3009. (error "Positive number only")
  3010. (customize-set-variable ',variable count)
  3011. (gdb-invalidate-memory 'update))))))
  3012. (def-gdb-set-positive-number
  3013. gdb-memory-set-rows
  3014. gdb-memory-rows
  3015. "Rows: "
  3016. "Set the number of data rows in memory window.")
  3017. (def-gdb-set-positive-number
  3018. gdb-memory-set-columns
  3019. gdb-memory-columns
  3020. "Columns: "
  3021. "Set the number of data columns in memory window.")
  3022. (defmacro def-gdb-memory-format (name format doc)
  3023. "Define a function NAME to switch memory buffer to use FORMAT.
  3024. DOC is an optional documentation string."
  3025. `(defun ,name () ,(when doc doc)
  3026. (interactive)
  3027. (customize-set-variable 'gdb-memory-format ,format)
  3028. (gdb-invalidate-memory 'update)))
  3029. (def-gdb-memory-format
  3030. gdb-memory-format-binary "t"
  3031. "Set the display format to binary.")
  3032. (def-gdb-memory-format
  3033. gdb-memory-format-octal "o"
  3034. "Set the display format to octal.")
  3035. (def-gdb-memory-format
  3036. gdb-memory-format-unsigned "u"
  3037. "Set the display format to unsigned decimal.")
  3038. (def-gdb-memory-format
  3039. gdb-memory-format-signed "d"
  3040. "Set the display format to decimal.")
  3041. (def-gdb-memory-format
  3042. gdb-memory-format-hexadecimal "x"
  3043. "Set the display format to hexadecimal.")
  3044. (defvar gdb-memory-format-map
  3045. (let ((map (make-sparse-keymap)))
  3046. (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
  3047. map)
  3048. "Keymap to select format in the header line.")
  3049. (defvar gdb-memory-format-menu
  3050. (let ((map (make-sparse-keymap "Format")))
  3051. (define-key map [binary]
  3052. '(menu-item "Binary" gdb-memory-format-binary
  3053. :button (:radio . (equal gdb-memory-format "t"))))
  3054. (define-key map [octal]
  3055. '(menu-item "Octal" gdb-memory-format-octal
  3056. :button (:radio . (equal gdb-memory-format "o"))))
  3057. (define-key map [unsigned]
  3058. '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
  3059. :button (:radio . (equal gdb-memory-format "u"))))
  3060. (define-key map [signed]
  3061. '(menu-item "Signed Decimal" gdb-memory-format-signed
  3062. :button (:radio . (equal gdb-memory-format "d"))))
  3063. (define-key map [hexadecimal]
  3064. '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
  3065. :button (:radio . (equal gdb-memory-format "x"))))
  3066. map)
  3067. "Menu of display formats in the header line.")
  3068. (defun gdb-memory-format-menu (event)
  3069. (interactive "@e")
  3070. (x-popup-menu event gdb-memory-format-menu))
  3071. (defun gdb-memory-format-menu-1 (event)
  3072. (interactive "e")
  3073. (save-selected-window
  3074. (select-window (posn-window (event-start event)))
  3075. (let* ((selection (gdb-memory-format-menu event))
  3076. (binding (and selection (lookup-key gdb-memory-format-menu
  3077. (vector (car selection))))))
  3078. (if binding (call-interactively binding)))))
  3079. (defmacro def-gdb-memory-unit (name unit-size doc)
  3080. "Define a function NAME to switch memory unit size to UNIT-SIZE.
  3081. DOC is an optional documentation string."
  3082. `(defun ,name () ,(when doc doc)
  3083. (interactive)
  3084. (customize-set-variable 'gdb-memory-unit ,unit-size)
  3085. (gdb-invalidate-memory 'update)))
  3086. (def-gdb-memory-unit gdb-memory-unit-giant 8
  3087. "Set the unit size to giant words (eight bytes).")
  3088. (def-gdb-memory-unit gdb-memory-unit-word 4
  3089. "Set the unit size to words (four bytes).")
  3090. (def-gdb-memory-unit gdb-memory-unit-halfword 2
  3091. "Set the unit size to halfwords (two bytes).")
  3092. (def-gdb-memory-unit gdb-memory-unit-byte 1
  3093. "Set the unit size to bytes.")
  3094. (defmacro def-gdb-memory-show-page (name address-var &optional doc)
  3095. "Define a function NAME which show new address in memory buffer.
  3096. The defined function switches Memory buffer to show address
  3097. stored in ADDRESS-VAR variable.
  3098. DOC is an optional documentation string."
  3099. `(defun ,name
  3100. ,(when doc doc)
  3101. (interactive)
  3102. (let ((gdb-memory-address ,address-var))
  3103. (gdb-invalidate-memory))))
  3104. (def-gdb-memory-show-page gdb-memory-show-previous-page
  3105. gdb-memory-prev-page)
  3106. (def-gdb-memory-show-page gdb-memory-show-next-page
  3107. gdb-memory-next-page)
  3108. (defvar gdb-memory-unit-map
  3109. (let ((map (make-sparse-keymap)))
  3110. (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
  3111. map)
  3112. "Keymap to select units in the header line.")
  3113. (defvar gdb-memory-unit-menu
  3114. (let ((map (make-sparse-keymap "Unit")))
  3115. (define-key map [giantwords]
  3116. '(menu-item "Giant words" gdb-memory-unit-giant
  3117. :button (:radio . (equal gdb-memory-unit 8))))
  3118. (define-key map [words]
  3119. '(menu-item "Words" gdb-memory-unit-word
  3120. :button (:radio . (equal gdb-memory-unit 4))))
  3121. (define-key map [halfwords]
  3122. '(menu-item "Halfwords" gdb-memory-unit-halfword
  3123. :button (:radio . (equal gdb-memory-unit 2))))
  3124. (define-key map [bytes]
  3125. '(menu-item "Bytes" gdb-memory-unit-byte
  3126. :button (:radio . (equal gdb-memory-unit 1))))
  3127. map)
  3128. "Menu of units in the header line.")
  3129. (defun gdb-memory-unit-menu (event)
  3130. (interactive "@e")
  3131. (x-popup-menu event gdb-memory-unit-menu))
  3132. (defun gdb-memory-unit-menu-1 (event)
  3133. (interactive "e")
  3134. (save-selected-window
  3135. (select-window (posn-window (event-start event)))
  3136. (let* ((selection (gdb-memory-unit-menu event))
  3137. (binding (and selection (lookup-key gdb-memory-unit-menu
  3138. (vector (car selection))))))
  3139. (if binding (call-interactively binding)))))
  3140. (defvar gdb-memory-font-lock-keywords
  3141. '(;; <__function.name+n>
  3142. ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
  3143. (1 font-lock-function-name-face)))
  3144. "Font lock keywords used in `gdb-memory-mode'.")
  3145. (defvar gdb-memory-header
  3146. '(:eval
  3147. (concat
  3148. "Start address["
  3149. (propertize "-"
  3150. 'face font-lock-warning-face
  3151. 'help-echo "mouse-1: decrement address"
  3152. 'mouse-face 'mode-line-highlight
  3153. 'local-map (gdb-make-header-line-mouse-map
  3154. 'mouse-1
  3155. #'gdb-memory-show-previous-page))
  3156. "|"
  3157. (propertize "+"
  3158. 'face font-lock-warning-face
  3159. 'help-echo "mouse-1: increment address"
  3160. 'mouse-face 'mode-line-highlight
  3161. 'local-map (gdb-make-header-line-mouse-map
  3162. 'mouse-1
  3163. #'gdb-memory-show-next-page))
  3164. "]: "
  3165. (propertize gdb-memory-address
  3166. 'face font-lock-warning-face
  3167. 'help-echo "mouse-1: set start address"
  3168. 'mouse-face 'mode-line-highlight
  3169. 'local-map (gdb-make-header-line-mouse-map
  3170. 'mouse-1
  3171. #'gdb-memory-set-address-event))
  3172. " Rows: "
  3173. (propertize (number-to-string gdb-memory-rows)
  3174. 'face font-lock-warning-face
  3175. 'help-echo "mouse-1: set number of columns"
  3176. 'mouse-face 'mode-line-highlight
  3177. 'local-map (gdb-make-header-line-mouse-map
  3178. 'mouse-1
  3179. #'gdb-memory-set-rows))
  3180. " Columns: "
  3181. (propertize (number-to-string gdb-memory-columns)
  3182. 'face font-lock-warning-face
  3183. 'help-echo "mouse-1: set number of columns"
  3184. 'mouse-face 'mode-line-highlight
  3185. 'local-map (gdb-make-header-line-mouse-map
  3186. 'mouse-1
  3187. #'gdb-memory-set-columns))
  3188. " Display Format: "
  3189. (propertize gdb-memory-format
  3190. 'face font-lock-warning-face
  3191. 'help-echo "mouse-3: select display format"
  3192. 'mouse-face 'mode-line-highlight
  3193. 'local-map gdb-memory-format-map)
  3194. " Unit Size: "
  3195. (propertize (number-to-string gdb-memory-unit)
  3196. 'face font-lock-warning-face
  3197. 'help-echo "mouse-3: select unit size"
  3198. 'mouse-face 'mode-line-highlight
  3199. 'local-map gdb-memory-unit-map)))
  3200. "Header line used in `gdb-memory-mode'.")
  3201. (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
  3202. "Major mode for examining memory."
  3203. (setq header-line-format gdb-memory-header)
  3204. (set (make-local-variable 'font-lock-defaults)
  3205. '(gdb-memory-font-lock-keywords))
  3206. 'gdb-invalidate-memory)
  3207. (defun gdb-memory-buffer-name ()
  3208. (concat "*memory of " (gdb-get-target-string) "*"))
  3209. (defun gdb-display-memory-buffer (&optional thread)
  3210. "Display GDB memory contents."
  3211. (interactive)
  3212. (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
  3213. (defun gdb-frame-memory-buffer ()
  3214. "Display memory contents in another frame."
  3215. (interactive)
  3216. (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
  3217. gdb-display-buffer-other-frame-action))
  3218. ;;; Disassembly view
  3219. (defun gdb-disassembly-buffer-name ()
  3220. (gdb-current-context-buffer-name
  3221. (concat "disassembly of " (gdb-get-target-string))))
  3222. (defun gdb-display-disassembly-buffer (&optional thread)
  3223. "Display GDB disassembly information."
  3224. (interactive)
  3225. (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
  3226. (def-gdb-preempt-display-buffer
  3227. gdb-preemptively-display-disassembly-buffer
  3228. 'gdb-disassembly-buffer)
  3229. (defun gdb-frame-disassembly-buffer (&optional thread)
  3230. "Display GDB disassembly information in another frame."
  3231. (interactive)
  3232. (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
  3233. gdb-display-buffer-other-frame-action))
  3234. (def-gdb-auto-update-trigger gdb-invalidate-disassembly
  3235. (let* ((frame (gdb-current-buffer-frame))
  3236. (file (bindat-get-field frame 'fullname))
  3237. (line (bindat-get-field frame 'line)))
  3238. (if file
  3239. (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
  3240. ;; If we're unable to get a file name / line for $PC, simply
  3241. ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
  3242. ;; 150 bytes) instructions.
  3243. "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
  3244. gdb-disassembly-handler
  3245. ;; We update disassembly only after we have actual frame information
  3246. ;; about all threads, so no there's `update' signal in this list
  3247. '(start update-disassembly))
  3248. (def-gdb-auto-update-handler
  3249. gdb-disassembly-handler
  3250. gdb-disassembly-handler-custom
  3251. t)
  3252. (gdb-set-buffer-rules
  3253. 'gdb-disassembly-buffer
  3254. 'gdb-disassembly-buffer-name
  3255. 'gdb-disassembly-mode
  3256. 'gdb-invalidate-disassembly)
  3257. (defvar gdb-disassembly-font-lock-keywords
  3258. '(;; <__function.name+n>
  3259. ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
  3260. (1 font-lock-function-name-face))
  3261. ;; 0xNNNNNNNN <__function.name+n>: opcode
  3262. ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
  3263. (4 font-lock-keyword-face))
  3264. ;; %register(at least i386)
  3265. ("%\\sw+" . font-lock-variable-name-face)
  3266. ("^\\(Dump of assembler code for function\\) \\(.+\\):"
  3267. (1 font-lock-comment-face)
  3268. (2 font-lock-function-name-face))
  3269. ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
  3270. "Font lock keywords used in `gdb-disassembly-mode'.")
  3271. (defvar gdb-disassembly-mode-map
  3272. ;; TODO
  3273. (let ((map (make-sparse-keymap)))
  3274. (suppress-keymap map)
  3275. (define-key map "q" 'kill-this-buffer)
  3276. map))
  3277. (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
  3278. "Major mode for GDB disassembly information."
  3279. ;; TODO Rename overlay variable for disassembly mode
  3280. (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
  3281. (setq fringes-outside-margins t)
  3282. (set (make-local-variable 'gdb-disassembly-position) (make-marker))
  3283. (set (make-local-variable 'font-lock-defaults)
  3284. '(gdb-disassembly-font-lock-keywords))
  3285. 'gdb-invalidate-disassembly)
  3286. (defun gdb-disassembly-handler-custom ()
  3287. (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
  3288. (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
  3289. (table (make-gdb-table))
  3290. (marked-line nil))
  3291. (dolist (instr instructions)
  3292. (gdb-table-add-row table
  3293. (list
  3294. (bindat-get-field instr 'address)
  3295. (let
  3296. ((func-name (bindat-get-field instr 'func-name))
  3297. (offset (bindat-get-field instr 'offset)))
  3298. (if func-name
  3299. (format "<%s+%s>:" func-name offset)
  3300. ""))
  3301. (bindat-get-field instr 'inst)))
  3302. (when (string-equal (bindat-get-field instr 'address)
  3303. address)
  3304. (progn
  3305. (setq marked-line (length (gdb-table-rows table)))
  3306. (setq fringe-indicator-alist
  3307. (if (string-equal gdb-frame-number "0")
  3308. nil
  3309. '((overlay-arrow . hollow-right-triangle)))))))
  3310. (insert (gdb-table-string table " "))
  3311. (gdb-disassembly-place-breakpoints)
  3312. ;; Mark current position with overlay arrow and scroll window to
  3313. ;; that point
  3314. (when marked-line
  3315. (let ((window (get-buffer-window (current-buffer) 0)))
  3316. (set-window-point window (gdb-mark-line marked-line
  3317. gdb-disassembly-position))))
  3318. (setq mode-name
  3319. (gdb-current-context-mode-name
  3320. (concat "Disassembly: "
  3321. (bindat-get-field (gdb-current-buffer-frame) 'func))))))
  3322. (defun gdb-disassembly-place-breakpoints ()
  3323. (gdb-remove-breakpoint-icons (point-min) (point-max))
  3324. (dolist (breakpoint gdb-breakpoints-list)
  3325. (let* ((breakpoint (cdr breakpoint))
  3326. (bptno (bindat-get-field breakpoint 'number))
  3327. (flag (bindat-get-field breakpoint 'enabled))
  3328. (address (bindat-get-field breakpoint 'addr)))
  3329. (save-excursion
  3330. (goto-char (point-min))
  3331. (if (re-search-forward (concat "^" address) nil t)
  3332. (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
  3333. (defvar gdb-breakpoints-header
  3334. (list
  3335. (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
  3336. nil nil mode-line)
  3337. " "
  3338. (gdb-propertize-header "Threads" gdb-threads-buffer
  3339. "mouse-1: select" mode-line-highlight
  3340. mode-line-inactive)))
  3341. ;;; Breakpoints view
  3342. (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
  3343. "Major mode for gdb breakpoints."
  3344. (setq header-line-format gdb-breakpoints-header)
  3345. 'gdb-invalidate-breakpoints)
  3346. (defun gdb-toggle-breakpoint ()
  3347. "Enable/disable breakpoint at current line of breakpoints buffer."
  3348. (interactive)
  3349. (save-excursion
  3350. (beginning-of-line)
  3351. (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
  3352. (if breakpoint
  3353. (gud-basic-call
  3354. (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
  3355. "-break-disable "
  3356. "-break-enable ")
  3357. (bindat-get-field breakpoint 'number)))
  3358. (error "Not recognized as break/watchpoint line")))))
  3359. (defun gdb-delete-breakpoint ()
  3360. "Delete the breakpoint at current line of breakpoints buffer."
  3361. (interactive)
  3362. (save-excursion
  3363. (beginning-of-line)
  3364. (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
  3365. (if breakpoint
  3366. (gud-basic-call (concat "-break-delete "
  3367. (bindat-get-field breakpoint 'number)))
  3368. (error "Not recognized as break/watchpoint line")))))
  3369. (defun gdb-goto-breakpoint (&optional event)
  3370. "Go to the location of breakpoint at current line of breakpoints buffer."
  3371. (interactive (list last-input-event))
  3372. (if event (posn-set-point (event-end event)))
  3373. ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
  3374. (let ((window (get-buffer-window gud-comint-buffer)))
  3375. (if window (save-selected-window (select-window window))))
  3376. (save-excursion
  3377. (beginning-of-line)
  3378. (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
  3379. (if breakpoint
  3380. (let ((bptno (bindat-get-field breakpoint 'number))
  3381. (file (bindat-get-field breakpoint 'fullname))
  3382. (line (bindat-get-field breakpoint 'line)))
  3383. (save-selected-window
  3384. (let* ((buffer (find-file-noselect
  3385. (if (file-exists-p file) file
  3386. (cdr (assoc bptno gdb-location-alist)))))
  3387. (window (or (gdb-display-source-buffer buffer)
  3388. (display-buffer buffer))))
  3389. (setq gdb-source-window window)
  3390. (with-current-buffer buffer
  3391. (goto-char (point-min))
  3392. (forward-line (1- (string-to-number line)))
  3393. (set-window-point window (point))))))
  3394. (error "Not recognized as break/watchpoint line")))))
  3395. ;; Frames buffer. This displays a perpetually correct backtrack trace.
  3396. ;;
  3397. (def-gdb-trigger-and-handler
  3398. gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
  3399. gdb-stack-list-frames-handler gdb-stack-list-frames-custom
  3400. '(start update))
  3401. (gdb-set-buffer-rules
  3402. 'gdb-stack-buffer
  3403. 'gdb-stack-buffer-name
  3404. 'gdb-frames-mode
  3405. 'gdb-invalidate-frames)
  3406. (defun gdb-frame-location (frame)
  3407. "Return \" of file:line\" or \" of library\" for structure FRAME.
  3408. FRAME must have either \"file\" and \"line\" members or \"from\"
  3409. member."
  3410. (let ((file (bindat-get-field frame 'file))
  3411. (line (bindat-get-field frame 'line))
  3412. (from (bindat-get-field frame 'from)))
  3413. (let ((res (or (and file line (concat file ":" line))
  3414. from)))
  3415. (if res (concat " of " res) ""))))
  3416. (defun gdb-stack-list-frames-custom ()
  3417. (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
  3418. (table (make-gdb-table)))
  3419. (set-marker gdb-stack-position nil)
  3420. (dolist (frame stack)
  3421. (gdb-table-add-row table
  3422. (list
  3423. (bindat-get-field frame 'level)
  3424. "in"
  3425. (concat
  3426. (bindat-get-field frame 'func)
  3427. (if gdb-stack-buffer-locations
  3428. (gdb-frame-location frame) "")
  3429. (if gdb-stack-buffer-addresses
  3430. (concat " at " (bindat-get-field frame 'addr)) "")))
  3431. `(mouse-face highlight
  3432. help-echo "mouse-2, RET: Select frame"
  3433. gdb-frame ,frame)))
  3434. (insert (gdb-table-string table " ")))
  3435. (when (and gdb-frame-number
  3436. (gdb-buffer-shows-main-thread-p))
  3437. (gdb-mark-line (1+ (string-to-number gdb-frame-number))
  3438. gdb-stack-position))
  3439. (setq mode-name
  3440. (gdb-current-context-mode-name "Frames")))
  3441. (defun gdb-stack-buffer-name ()
  3442. (gdb-current-context-buffer-name
  3443. (concat "stack frames of " (gdb-get-target-string))))
  3444. (defun gdb-display-stack-buffer (&optional thread)
  3445. "Display GDB backtrace for current stack."
  3446. (interactive)
  3447. (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
  3448. (def-gdb-preempt-display-buffer
  3449. gdb-preemptively-display-stack-buffer
  3450. 'gdb-stack-buffer nil t)
  3451. (defun gdb-frame-stack-buffer (&optional thread)
  3452. "Display GDB backtrace for current stack in another frame."
  3453. (interactive)
  3454. (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
  3455. gdb-display-buffer-other-frame-action))
  3456. (defvar gdb-frames-mode-map
  3457. (let ((map (make-sparse-keymap)))
  3458. (suppress-keymap map)
  3459. (define-key map "q" 'kill-this-buffer)
  3460. (define-key map "\r" 'gdb-select-frame)
  3461. (define-key map [mouse-2] 'gdb-select-frame)
  3462. (define-key map [follow-link] 'mouse-face)
  3463. map))
  3464. (defvar gdb-frames-font-lock-keywords
  3465. '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
  3466. "Font lock keywords used in `gdb-frames-mode'.")
  3467. (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
  3468. "Major mode for gdb call stack."
  3469. (setq gdb-stack-position (make-marker))
  3470. (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
  3471. (setq truncate-lines t) ;; Make it easier to see overlay arrow.
  3472. (set (make-local-variable 'font-lock-defaults)
  3473. '(gdb-frames-font-lock-keywords))
  3474. 'gdb-invalidate-frames)
  3475. (defun gdb-select-frame (&optional event)
  3476. "Select the frame and display the relevant source."
  3477. (interactive (list last-input-event))
  3478. (if event (posn-set-point (event-end event)))
  3479. (let ((frame (get-text-property (point) 'gdb-frame)))
  3480. (if frame
  3481. (if (gdb-buffer-shows-main-thread-p)
  3482. (let ((new-level (bindat-get-field frame 'level)))
  3483. (setq gdb-frame-number new-level)
  3484. (gdb-input (concat "-stack-select-frame " new-level)
  3485. 'ignore)
  3486. (gdb-update))
  3487. (error "Could not select frame for non-current thread"))
  3488. (error "Not recognized as frame line"))))
  3489. ;; Locals buffer.
  3490. ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
  3491. (def-gdb-trigger-and-handler
  3492. gdb-invalidate-locals
  3493. (concat (gdb-current-context-command "-stack-list-locals")
  3494. " --simple-values")
  3495. gdb-locals-handler gdb-locals-handler-custom
  3496. '(start update))
  3497. (gdb-set-buffer-rules
  3498. 'gdb-locals-buffer
  3499. 'gdb-locals-buffer-name
  3500. 'gdb-locals-mode
  3501. 'gdb-invalidate-locals)
  3502. (defvar gdb-locals-watch-map
  3503. (let ((map (make-sparse-keymap)))
  3504. (suppress-keymap map)
  3505. (define-key map "\r" 'gud-watch)
  3506. (define-key map [mouse-2] 'gud-watch)
  3507. map)
  3508. "Keymap to create watch expression of a complex data type local variable.")
  3509. (defvar gdb-edit-locals-map-1
  3510. (let ((map (make-sparse-keymap)))
  3511. (suppress-keymap map)
  3512. (define-key map "\r" 'gdb-edit-locals-value)
  3513. (define-key map [mouse-2] 'gdb-edit-locals-value)
  3514. map)
  3515. "Keymap to edit value of a simple data type local variable.")
  3516. (defun gdb-edit-locals-value (&optional event)
  3517. "Assign a value to a variable displayed in the locals buffer."
  3518. (interactive (list last-input-event))
  3519. (save-excursion
  3520. (if event (posn-set-point (event-end event)))
  3521. (beginning-of-line)
  3522. (let* ((var (bindat-get-field
  3523. (get-text-property (point) 'gdb-local-variable) 'name))
  3524. (value (read-string (format "New value (%s): " var))))
  3525. (gud-basic-call
  3526. (concat "-gdb-set variable " var " = " value)))))
  3527. ;; Don't display values of arrays or structures.
  3528. ;; These can be expanded using gud-watch.
  3529. (defun gdb-locals-handler-custom ()
  3530. (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
  3531. (table (make-gdb-table)))
  3532. (dolist (local locals-list)
  3533. (let ((name (bindat-get-field local 'name))
  3534. (value (bindat-get-field local 'value))
  3535. (type (bindat-get-field local 'type)))
  3536. (when (not value)
  3537. (setq value "<complex data type>"))
  3538. (if (or (not value)
  3539. (string-match "\\0x" value))
  3540. (add-text-properties 0 (length name)
  3541. `(mouse-face highlight
  3542. help-echo "mouse-2: create watch expression"
  3543. local-map ,gdb-locals-watch-map)
  3544. name)
  3545. (add-text-properties 0 (length value)
  3546. `(mouse-face highlight
  3547. help-echo "mouse-2: edit value"
  3548. local-map ,gdb-edit-locals-map-1)
  3549. value))
  3550. (gdb-table-add-row
  3551. table
  3552. (list
  3553. (propertize type 'font-lock-face font-lock-type-face)
  3554. (propertize name 'font-lock-face font-lock-variable-name-face)
  3555. value)
  3556. `(gdb-local-variable ,local))))
  3557. (insert (gdb-table-string table " "))
  3558. (setq mode-name
  3559. (gdb-current-context-mode-name
  3560. (concat "Locals: "
  3561. (bindat-get-field (gdb-current-buffer-frame) 'func))))))
  3562. (defvar gdb-locals-header
  3563. (list
  3564. (gdb-propertize-header "Locals" gdb-locals-buffer
  3565. nil nil mode-line)
  3566. " "
  3567. (gdb-propertize-header "Registers" gdb-registers-buffer
  3568. "mouse-1: select" mode-line-highlight
  3569. mode-line-inactive)))
  3570. (defvar gdb-locals-mode-map
  3571. (let ((map (make-sparse-keymap)))
  3572. (suppress-keymap map)
  3573. (define-key map "q" 'kill-this-buffer)
  3574. (define-key map "\t" (lambda ()
  3575. (interactive)
  3576. (gdb-set-window-buffer
  3577. (gdb-get-buffer-create
  3578. 'gdb-registers-buffer
  3579. gdb-thread-number) t)))
  3580. map))
  3581. (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
  3582. "Major mode for gdb locals."
  3583. (setq header-line-format gdb-locals-header)
  3584. 'gdb-invalidate-locals)
  3585. (defun gdb-locals-buffer-name ()
  3586. (gdb-current-context-buffer-name
  3587. (concat "locals of " (gdb-get-target-string))))
  3588. (defun gdb-display-locals-buffer (&optional thread)
  3589. "Display the local variables of current GDB stack."
  3590. (interactive)
  3591. (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
  3592. (def-gdb-preempt-display-buffer
  3593. gdb-preemptively-display-locals-buffer
  3594. 'gdb-locals-buffer nil t)
  3595. (defun gdb-frame-locals-buffer (&optional thread)
  3596. "Display the local variables of the current GDB stack in another frame."
  3597. (interactive)
  3598. (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
  3599. gdb-display-buffer-other-frame-action))
  3600. ;; Registers buffer.
  3601. (def-gdb-trigger-and-handler
  3602. gdb-invalidate-registers
  3603. (concat (gdb-current-context-command "-data-list-register-values") " x")
  3604. gdb-registers-handler
  3605. gdb-registers-handler-custom
  3606. '(start update))
  3607. (gdb-set-buffer-rules
  3608. 'gdb-registers-buffer
  3609. 'gdb-registers-buffer-name
  3610. 'gdb-registers-mode
  3611. 'gdb-invalidate-registers)
  3612. (defun gdb-registers-handler-custom ()
  3613. (when gdb-register-names
  3614. (let ((register-values
  3615. (bindat-get-field (gdb-json-partial-output) 'register-values))
  3616. (table (make-gdb-table)))
  3617. (dolist (register register-values)
  3618. (let* ((register-number (bindat-get-field register 'number))
  3619. (value (bindat-get-field register 'value))
  3620. (register-name (nth (string-to-number register-number)
  3621. gdb-register-names)))
  3622. (gdb-table-add-row
  3623. table
  3624. (list
  3625. (propertize register-name
  3626. 'font-lock-face font-lock-variable-name-face)
  3627. (if (member register-number gdb-changed-registers)
  3628. (propertize value 'font-lock-face font-lock-warning-face)
  3629. value))
  3630. `(mouse-face highlight
  3631. help-echo "mouse-2: edit value"
  3632. gdb-register-name ,register-name))))
  3633. (insert (gdb-table-string table " ")))
  3634. (setq mode-name
  3635. (gdb-current-context-mode-name "Registers"))))
  3636. (defun gdb-edit-register-value (&optional event)
  3637. "Assign a value to a register displayed in the registers buffer."
  3638. (interactive (list last-input-event))
  3639. (save-excursion
  3640. (if event (posn-set-point (event-end event)))
  3641. (beginning-of-line)
  3642. (let* ((var (bindat-get-field
  3643. (get-text-property (point) 'gdb-register-name)))
  3644. (value (read-string (format "New value (%s): " var))))
  3645. (gud-basic-call
  3646. (concat "-gdb-set variable $" var " = " value)))))
  3647. (defvar gdb-registers-mode-map
  3648. (let ((map (make-sparse-keymap)))
  3649. (suppress-keymap map)
  3650. (define-key map "\r" 'gdb-edit-register-value)
  3651. (define-key map [mouse-2] 'gdb-edit-register-value)
  3652. (define-key map "q" 'kill-this-buffer)
  3653. (define-key map "\t" (lambda ()
  3654. (interactive)
  3655. (gdb-set-window-buffer
  3656. (gdb-get-buffer-create
  3657. 'gdb-locals-buffer
  3658. gdb-thread-number) t)))
  3659. map))
  3660. (defvar gdb-registers-header
  3661. (list
  3662. (gdb-propertize-header "Locals" gdb-locals-buffer
  3663. "mouse-1: select" mode-line-highlight
  3664. mode-line-inactive)
  3665. " "
  3666. (gdb-propertize-header "Registers" gdb-registers-buffer
  3667. nil nil mode-line)))
  3668. (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
  3669. "Major mode for gdb registers."
  3670. (setq header-line-format gdb-registers-header)
  3671. 'gdb-invalidate-registers)
  3672. (defun gdb-registers-buffer-name ()
  3673. (gdb-current-context-buffer-name
  3674. (concat "registers of " (gdb-get-target-string))))
  3675. (defun gdb-display-registers-buffer (&optional thread)
  3676. "Display GDB register contents."
  3677. (interactive)
  3678. (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
  3679. (def-gdb-preempt-display-buffer
  3680. gdb-preemptively-display-registers-buffer
  3681. 'gdb-registers-buffer nil t)
  3682. (defun gdb-frame-registers-buffer (&optional thread)
  3683. "Display GDB register contents in another frame."
  3684. (interactive)
  3685. (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
  3686. gdb-display-buffer-other-frame-action))
  3687. ;; Needs GDB 6.4 onwards (used to fail with no stack).
  3688. (defun gdb-get-changed-registers ()
  3689. (when (gdb-get-buffer 'gdb-registers-buffer)
  3690. (gdb-input "-data-list-changed-registers"
  3691. 'gdb-changed-registers-handler
  3692. 'gdb-get-changed-registers)))
  3693. (defun gdb-changed-registers-handler ()
  3694. (setq gdb-changed-registers nil)
  3695. (dolist (register-number
  3696. (bindat-get-field (gdb-json-partial-output) 'changed-registers))
  3697. (push register-number gdb-changed-registers)))
  3698. (defun gdb-register-names-handler ()
  3699. ;; Don't use pending triggers because this handler is called
  3700. ;; only once (in gdb-init-1)
  3701. (setq gdb-register-names nil)
  3702. (dolist (register-name
  3703. (bindat-get-field (gdb-json-partial-output) 'register-names))
  3704. (push register-name gdb-register-names))
  3705. (setq gdb-register-names (reverse gdb-register-names)))
  3706. (defun gdb-get-source-file-list ()
  3707. "Create list of source files for current GDB session.
  3708. If buffers already exist for any of these files, `gud-minor-mode'
  3709. is set in them."
  3710. (goto-char (point-min))
  3711. (while (re-search-forward gdb-source-file-regexp nil t)
  3712. (push (read (match-string 1)) gdb-source-file-list))
  3713. (dolist (buffer (buffer-list))
  3714. (with-current-buffer buffer
  3715. (when (member buffer-file-name gdb-source-file-list)
  3716. (gdb-init-buffer)))))
  3717. (defun gdb-get-main-selected-frame ()
  3718. "Trigger for `gdb-frame-handler' which uses main current thread.
  3719. Called from `gdb-update'."
  3720. (gdb-input (gdb-current-context-command "-stack-info-frame")
  3721. 'gdb-frame-handler
  3722. 'gdb-get-main-selected-frame))
  3723. (defun gdb-frame-handler ()
  3724. "Set `gdb-selected-frame' and `gdb-selected-file' to show
  3725. overlay arrow in source buffer."
  3726. (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
  3727. (when frame
  3728. (setq gdb-selected-frame (bindat-get-field frame 'func))
  3729. (setq gdb-selected-file (bindat-get-field frame 'fullname))
  3730. (setq gdb-frame-number (bindat-get-field frame 'level))
  3731. (setq gdb-frame-address (bindat-get-field frame 'addr))
  3732. (let ((line (bindat-get-field frame 'line)))
  3733. (setq gdb-selected-line (and line (string-to-number line)))
  3734. (when (and gdb-selected-file gdb-selected-line)
  3735. (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
  3736. (gud-display-frame)))
  3737. (if gud-overlay-arrow-position
  3738. (let ((buffer (marker-buffer gud-overlay-arrow-position))
  3739. (position (marker-position gud-overlay-arrow-position)))
  3740. (when buffer
  3741. (with-current-buffer buffer
  3742. (setq fringe-indicator-alist
  3743. (if (string-equal gdb-frame-number "0")
  3744. nil
  3745. '((overlay-arrow . hollow-right-triangle))))
  3746. (setq gud-overlay-arrow-position (make-marker))
  3747. (set-marker gud-overlay-arrow-position position))))))))
  3748. (defconst gdb-prompt-name-regexp
  3749. (concat "value=\\(" gdb--string-regexp "\\)"))
  3750. (defun gdb-get-prompt ()
  3751. "Find prompt for GDB session."
  3752. (goto-char (point-min))
  3753. (setq gdb-prompt-name nil)
  3754. (re-search-forward gdb-prompt-name-regexp nil t)
  3755. (setq gdb-prompt-name (read (match-string 1)))
  3756. ;; Insert first prompt.
  3757. (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
  3758. ;;;; Window management
  3759. (defun gdb-display-buffer (buf)
  3760. "Show buffer BUF, and make that window dedicated."
  3761. (let ((window (display-buffer buf)))
  3762. (set-window-dedicated-p window t)
  3763. window))
  3764. ;; (let ((answer (get-buffer-window buf 0)))
  3765. ;; (if answer
  3766. ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
  3767. ;; (let ((window (get-lru-window)))
  3768. ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
  3769. ;; 'gdbmi)
  3770. ;; (let ((largest (get-largest-window)))
  3771. ;; (setq answer (split-window largest))
  3772. ;; (set-window-buffer answer buf)
  3773. ;; (set-window-dedicated-p answer t)
  3774. ;; answer)
  3775. ;; (set-window-buffer window buf)
  3776. ;; window)))))
  3777. (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
  3778. "Find window displaying a buffer with the same
  3779. `gdb-buffer-type' as BUF and show BUF there. If no such window
  3780. exists, just call `gdb-display-buffer' for BUF. If the window
  3781. found is already dedicated, split window according to
  3782. SPLIT-HORIZONTAL and show BUF in the new window."
  3783. (if buf
  3784. (when (not (get-buffer-window buf))
  3785. (let* ((buf-type (gdb-buffer-type buf))
  3786. (existing-window
  3787. (get-window-with-predicate
  3788. #'(lambda (w)
  3789. (and (eq buf-type
  3790. (gdb-buffer-type (window-buffer w)))
  3791. (not (window-dedicated-p w)))))))
  3792. (if existing-window
  3793. (set-window-buffer existing-window buf)
  3794. (let ((dedicated-window
  3795. (get-window-with-predicate
  3796. #'(lambda (w)
  3797. (eq buf-type
  3798. (gdb-buffer-type (window-buffer w)))))))
  3799. (if dedicated-window
  3800. (set-window-buffer
  3801. (split-window dedicated-window nil split-horizontal) buf)
  3802. (gdb-display-buffer buf))))))
  3803. (error "Null buffer")))
  3804. ;;; Shared keymap initialization:
  3805. (let ((menu (make-sparse-keymap "GDB-Windows")))
  3806. (define-key gud-menu-map [displays]
  3807. `(menu-item "GDB-Windows" ,menu
  3808. :visible (eq gud-minor-mode 'gdbmi)))
  3809. (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
  3810. (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
  3811. (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
  3812. (define-key menu [disassembly]
  3813. '("Disassembly" . gdb-display-disassembly-buffer))
  3814. (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
  3815. (define-key menu [inferior]
  3816. '("IO" . gdb-display-io-buffer))
  3817. (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
  3818. (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
  3819. (define-key menu [breakpoints]
  3820. '("Breakpoints" . gdb-display-breakpoints-buffer)))
  3821. (let ((menu (make-sparse-keymap "GDB-Frames")))
  3822. (define-key gud-menu-map [frames]
  3823. `(menu-item "GDB-Frames" ,menu
  3824. :visible (eq gud-minor-mode 'gdbmi)))
  3825. (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
  3826. (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
  3827. (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
  3828. (define-key menu [disassembly]
  3829. '("Disassembly" . gdb-frame-disassembly-buffer))
  3830. (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
  3831. (define-key menu [inferior]
  3832. '("IO" . gdb-frame-io-buffer))
  3833. (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
  3834. (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
  3835. (define-key menu [breakpoints]
  3836. '("Breakpoints" . gdb-frame-breakpoints-buffer)))
  3837. (let ((menu (make-sparse-keymap "GDB-MI")))
  3838. (define-key menu [gdb-customize]
  3839. '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
  3840. :help "Customize Gdb Graphical Mode options."))
  3841. (define-key menu [gdb-many-windows]
  3842. '(menu-item "Display Other Windows" gdb-many-windows
  3843. :help "Toggle display of locals, stack and breakpoint information"
  3844. :button (:toggle . gdb-many-windows)))
  3845. (define-key menu [gdb-restore-windows]
  3846. '(menu-item "Restore Window Layout" gdb-restore-windows
  3847. :help "Restore standard layout for debug session."))
  3848. (define-key menu [sep1]
  3849. '(menu-item "--"))
  3850. (define-key menu [all-threads]
  3851. '(menu-item "GUD controls all threads"
  3852. (lambda ()
  3853. (interactive)
  3854. (setq gdb-gud-control-all-threads t))
  3855. :help "GUD start/stop commands apply to all threads"
  3856. :button (:radio . gdb-gud-control-all-threads)))
  3857. (define-key menu [current-thread]
  3858. '(menu-item "GUD controls current thread"
  3859. (lambda ()
  3860. (interactive)
  3861. (setq gdb-gud-control-all-threads nil))
  3862. :help "GUD start/stop commands apply to current thread only"
  3863. :button (:radio . (not gdb-gud-control-all-threads))))
  3864. (define-key menu [sep2]
  3865. '(menu-item "--"))
  3866. (define-key menu [gdb-customize-reasons]
  3867. '(menu-item "Customize switching..."
  3868. (lambda ()
  3869. (interactive)
  3870. (customize-option 'gdb-switch-reasons))))
  3871. (define-key menu [gdb-switch-when-another-stopped]
  3872. (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
  3873. gdb-switch-when-another-stopped
  3874. "Automatically switch to stopped thread"
  3875. "GDB thread switching %s"
  3876. "Switch to stopped thread"))
  3877. (define-key gud-menu-map [mi]
  3878. `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
  3879. ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
  3880. ;; GDB-MI menu will need to be moved to gud.el. We can't use
  3881. ;; tool-bar-local-item-from-menu here because it appends new buttons
  3882. ;; to toolbar from right to left while we want our A/T throttle to
  3883. ;; show up right before Run button.
  3884. (define-key-after gud-tool-bar-map [all-threads]
  3885. '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
  3886. :image (find-image '((:type xpm :file "gud/thread.xpm")))
  3887. :visible (and (eq gud-minor-mode 'gdbmi)
  3888. gdb-non-stop
  3889. (not gdb-gud-control-all-threads)))
  3890. 'run)
  3891. (define-key-after gud-tool-bar-map [current-thread]
  3892. '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
  3893. :image (find-image '((:type xpm :file "gud/all.xpm")))
  3894. :visible (and (eq gud-minor-mode 'gdbmi)
  3895. gdb-non-stop
  3896. gdb-gud-control-all-threads))
  3897. 'all-threads)
  3898. (defun gdb-frame-gdb-buffer ()
  3899. "Display GUD buffer in another frame."
  3900. (interactive)
  3901. (display-buffer-other-frame gud-comint-buffer))
  3902. (defun gdb-display-gdb-buffer ()
  3903. "Display GUD buffer."
  3904. (interactive)
  3905. (pop-to-buffer gud-comint-buffer nil 0))
  3906. (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
  3907. "Set buffer of selected window to NAME and dedicate window.
  3908. When IGNORE-DEDICATED is non-nil, buffer is set even if selected
  3909. window is dedicated."
  3910. (unless window (setq window (selected-window)))
  3911. (when ignore-dedicated
  3912. (set-window-dedicated-p window nil))
  3913. (set-window-buffer window (get-buffer name))
  3914. (set-window-dedicated-p window t))
  3915. (defun gdb-setup-windows ()
  3916. "Layout the window pattern for option `gdb-many-windows'."
  3917. (gdb-get-buffer-create 'gdb-locals-buffer)
  3918. (gdb-get-buffer-create 'gdb-stack-buffer)
  3919. (gdb-get-buffer-create 'gdb-breakpoints-buffer)
  3920. (set-window-dedicated-p (selected-window) nil)
  3921. (switch-to-buffer gud-comint-buffer)
  3922. (delete-other-windows)
  3923. (let ((win0 (selected-window))
  3924. (win1 (split-window nil ( / ( * (window-height) 3) 4)))
  3925. (win2 (split-window nil ( / (window-height) 3)))
  3926. (win3 (split-window-right)))
  3927. (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
  3928. (select-window win2)
  3929. (set-window-buffer
  3930. win2
  3931. (if gud-last-last-frame
  3932. (gud-find-file (car gud-last-last-frame))
  3933. (if gdb-main-file
  3934. (gud-find-file gdb-main-file)
  3935. ;; Put buffer list in window if we
  3936. ;; can't find a source file.
  3937. (list-buffers-noselect))))
  3938. (setq gdb-source-window (selected-window))
  3939. (let ((win4 (split-window-right)))
  3940. (gdb-set-window-buffer
  3941. (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
  3942. (select-window win1)
  3943. (gdb-set-window-buffer (gdb-stack-buffer-name))
  3944. (let ((win5 (split-window-right)))
  3945. (gdb-set-window-buffer (if gdb-show-threads-by-default
  3946. (gdb-threads-buffer-name)
  3947. (gdb-breakpoints-buffer-name))
  3948. nil win5))
  3949. (select-window win0)))
  3950. (define-minor-mode gdb-many-windows
  3951. "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
  3952. In this case it starts with two windows: one displaying the GUD
  3953. buffer and the other with the source file with the main routine
  3954. of the debugged program. Non-nil means display the layout shown for
  3955. `gdb'."
  3956. :global t
  3957. :group 'gdb
  3958. :version "22.1"
  3959. (if (and gud-comint-buffer
  3960. (buffer-name gud-comint-buffer))
  3961. (ignore-errors
  3962. (gdb-restore-windows))))
  3963. (defun gdb-restore-windows ()
  3964. "Restore the basic arrangement of windows used by gdb.
  3965. This arrangement depends on the value of option `gdb-many-windows'."
  3966. (interactive)
  3967. (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
  3968. (delete-other-windows)
  3969. (if gdb-many-windows
  3970. (gdb-setup-windows)
  3971. (when (or gud-last-last-frame gdb-show-main)
  3972. (let ((win (split-window)))
  3973. (set-window-buffer
  3974. win
  3975. (if gud-last-last-frame
  3976. (gud-find-file (car gud-last-last-frame))
  3977. (gud-find-file gdb-main-file)))
  3978. (setq gdb-source-window win)))))
  3979. ;; Called from `gud-sentinel' in gud.el:
  3980. (defun gdb-reset ()
  3981. "Exit a debugging session cleanly.
  3982. Kills the gdb buffers, and resets variables and the source buffers."
  3983. ;; The gdb-inferior buffer has a pty hooked up to the main gdb
  3984. ;; process. This pty must be deleted explicitly.
  3985. (let ((pty (get-process "gdb-inferior")))
  3986. (if pty (delete-process pty)))
  3987. ;; Find gdb-mi buffers and kill them.
  3988. (dolist (buffer (buffer-list))
  3989. (unless (eq buffer gud-comint-buffer)
  3990. (with-current-buffer buffer
  3991. (if (eq gud-minor-mode 'gdbmi)
  3992. (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
  3993. (kill-buffer nil)
  3994. (gdb-remove-breakpoint-icons (point-min) (point-max) t)
  3995. (setq gud-minor-mode nil)
  3996. (kill-local-variable 'tool-bar-map)
  3997. (kill-local-variable 'gdb-define-alist))))))
  3998. (setq gdb-disassembly-position nil)
  3999. (setq overlay-arrow-variable-list
  4000. (delq 'gdb-disassembly-position overlay-arrow-variable-list))
  4001. (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
  4002. (setq gdb-stack-position nil)
  4003. (setq overlay-arrow-variable-list
  4004. (delq 'gdb-stack-position overlay-arrow-variable-list))
  4005. (setq gdb-thread-position nil)
  4006. (setq overlay-arrow-variable-list
  4007. (delq 'gdb-thread-position overlay-arrow-variable-list))
  4008. (if (boundp 'speedbar-frame) (speedbar-timer-fn))
  4009. (setq gud-running nil)
  4010. (setq gdb-active-process nil)
  4011. (remove-hook 'after-save-hook 'gdb-create-define-alist t))
  4012. (defun gdb-get-source-file ()
  4013. "Find the source file where the program starts and display it with related
  4014. buffers, if required."
  4015. (goto-char (point-min))
  4016. (if (re-search-forward gdb-source-file-regexp nil t)
  4017. (setq gdb-main-file (read (match-string 1))))
  4018. (if gdb-many-windows
  4019. (gdb-setup-windows)
  4020. (gdb-get-buffer-create 'gdb-breakpoints-buffer)
  4021. (and gdb-show-main
  4022. gdb-main-file
  4023. (display-buffer (gud-find-file gdb-main-file))))
  4024. (gdb-force-mode-line-update
  4025. (propertize "ready" 'face font-lock-variable-name-face)))
  4026. ;;from put-image
  4027. (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
  4028. "Put string PUTSTRING in front of POS in the current buffer.
  4029. PUTSTRING is displayed by putting an overlay into the current buffer with a
  4030. `before-string' string that has a `display' property whose value is
  4031. PUTSTRING."
  4032. (let ((string (make-string 1 ?x))
  4033. (buffer (current-buffer)))
  4034. (setq putstring (copy-sequence putstring))
  4035. (let ((overlay (make-overlay pos pos buffer))
  4036. (prop (or dprop
  4037. (list (list 'margin 'left-margin) putstring))))
  4038. (put-text-property 0 1 'display prop string)
  4039. (if sprops
  4040. (add-text-properties 0 1 sprops string))
  4041. (overlay-put overlay 'put-break t)
  4042. (overlay-put overlay 'before-string string))))
  4043. ;;from remove-images
  4044. (defun gdb-remove-strings (start end &optional buffer)
  4045. "Remove strings between START and END in BUFFER.
  4046. Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
  4047. BUFFER nil or omitted means use the current buffer."
  4048. (unless buffer
  4049. (setq buffer (current-buffer)))
  4050. (dolist (overlay (overlays-in start end))
  4051. (when (overlay-get overlay 'put-break)
  4052. (delete-overlay overlay))))
  4053. (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
  4054. (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
  4055. (start (- (car posns) 1))
  4056. (end (+ (cdr posns) 1))
  4057. (putstring (if enabled "B" "b"))
  4058. (source-window (get-buffer-window (current-buffer) 0)))
  4059. (add-text-properties
  4060. 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
  4061. putstring)
  4062. (if enabled
  4063. (add-text-properties
  4064. 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
  4065. (add-text-properties
  4066. 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
  4067. (gdb-remove-breakpoint-icons start end)
  4068. (if (display-images-p)
  4069. (if (>= (or left-fringe-width
  4070. (if source-window (car (window-fringes source-window)))
  4071. gdb-buffer-fringe-width) 8)
  4072. (gdb-put-string
  4073. nil (1+ start)
  4074. `(left-fringe breakpoint
  4075. ,(if enabled
  4076. 'breakpoint-enabled
  4077. 'breakpoint-disabled))
  4078. 'gdb-bptno bptno
  4079. 'gdb-enabled enabled)
  4080. (when (< left-margin-width 2)
  4081. (save-current-buffer
  4082. (setq left-margin-width 2)
  4083. (if source-window
  4084. (set-window-margins
  4085. source-window
  4086. left-margin-width right-margin-width))))
  4087. (put-image
  4088. (if enabled
  4089. (or breakpoint-enabled-icon
  4090. (setq breakpoint-enabled-icon
  4091. (find-image `((:type xpm :data
  4092. ,breakpoint-xpm-data
  4093. :ascent 100 :pointer hand)
  4094. (:type pbm :data
  4095. ,breakpoint-enabled-pbm-data
  4096. :ascent 100 :pointer hand)))))
  4097. (or breakpoint-disabled-icon
  4098. (setq breakpoint-disabled-icon
  4099. (find-image `((:type xpm :data
  4100. ,breakpoint-xpm-data
  4101. :conversion disabled
  4102. :ascent 100 :pointer hand)
  4103. (:type pbm :data
  4104. ,breakpoint-disabled-pbm-data
  4105. :ascent 100 :pointer hand))))))
  4106. (+ start 1)
  4107. putstring
  4108. 'left-margin))
  4109. (when (< left-margin-width 2)
  4110. (save-current-buffer
  4111. (setq left-margin-width 2)
  4112. (let ((window (get-buffer-window (current-buffer) 0)))
  4113. (if window
  4114. (set-window-margins
  4115. window left-margin-width right-margin-width)))))
  4116. (gdb-put-string
  4117. (propertize putstring
  4118. 'face (if enabled
  4119. 'breakpoint-enabled 'breakpoint-disabled))
  4120. (1+ start)))))
  4121. (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
  4122. (gdb-remove-strings start end)
  4123. (if (display-images-p)
  4124. (remove-images start end))
  4125. (when remove-margin
  4126. (setq left-margin-width 0)
  4127. (let ((window (get-buffer-window (current-buffer) 0)))
  4128. (if window
  4129. (set-window-margins
  4130. window left-margin-width right-margin-width)))))
  4131. ;;; Functions for inline completion.
  4132. (defvar gud-gdb-fetch-lines-in-progress)
  4133. (defvar gud-gdb-fetch-lines-string)
  4134. (defvar gud-gdb-fetch-lines-break)
  4135. (defvar gud-gdb-fetched-lines)
  4136. (defun gud-gdbmi-completions (context command)
  4137. "Completion table for GDB/MI commands.
  4138. COMMAND is the prefix for which we seek completion.
  4139. CONTEXT is the text before COMMAND on the line."
  4140. (let ((gud-gdb-fetch-lines-in-progress t)
  4141. (gud-gdb-fetch-lines-string nil)
  4142. (gud-gdb-fetch-lines-break (length context))
  4143. (gud-gdb-fetched-lines nil)
  4144. ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
  4145. (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
  4146. (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
  4147. (gdb-input (concat "complete " context command)
  4148. (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
  4149. (while gud-gdb-fetch-lines-in-progress
  4150. (accept-process-output (get-buffer-process gud-comint-buffer))))
  4151. (gud-gdb-completions-1 gud-gdb-fetched-lines)))
  4152. (defun gud-gdbmi-fetch-lines-filter (string)
  4153. "Custom filter function for `gud-gdbmi-completions'."
  4154. (setq string (concat gud-gdb-fetch-lines-string
  4155. (gud-gdbmi-marker-filter string)))
  4156. (while (string-match "\n" string)
  4157. (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
  4158. gud-gdb-fetched-lines)
  4159. (setq string (substring string (match-end 0))))
  4160. "")
  4161. (provide 'gdb-mi)
  4162. ;;; gdb-mi.el ends here