gdb-mi.el 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725
  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. (defun gud-gdbmi-marker-filter (string)
  1975. "Filter GDB/MI output."
  1976. ;; Record transactions if logging is enabled.
  1977. (when gdb-enable-debug
  1978. (push (cons 'recv string) gdb-debug-log)
  1979. (if (and gdb-debug-log-max
  1980. (> (length gdb-debug-log) gdb-debug-log-max))
  1981. (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
  1982. ;; Recall the left over gud-marker-acc from last time.
  1983. (setq gud-marker-acc (concat gud-marker-acc string))
  1984. ;; Start accumulating output for the GUD buffer.
  1985. (setq gdb-filter-output "")
  1986. (let ((acc-length (length gud-marker-acc)))
  1987. (catch 'gdbmi-incomplete-stream
  1988. (while (and (< gdbmi-bnf-offset acc-length)
  1989. (funcall gdbmi-bnf-state)))))
  1990. (when (/= gdbmi-bnf-offset 0)
  1991. (setq gud-marker-acc (substring gud-marker-acc gdbmi-bnf-offset))
  1992. (setq gdbmi-bnf-offset 0))
  1993. (when (and gdbmi-debug-mode (> (length gud-marker-acc) 0))
  1994. (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc))
  1995. gdb-filter-output)
  1996. (defun gdb-gdb (_output-field))
  1997. (defun gdb-shell (output-field)
  1998. (setq gdb-filter-output
  1999. (concat output-field gdb-filter-output)))
  2000. (defun gdb-ignored-notification (_token _output-field))
  2001. ;; gdb-invalidate-threads is defined to accept 'update-threads signal
  2002. (defun gdb-thread-created (_token _output-field))
  2003. (defun gdb-thread-exited (_token output-field)
  2004. "Handle =thread-exited async record.
  2005. Unset `gdb-thread-number' if current thread exited and update threads list."
  2006. (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
  2007. (if (string= gdb-thread-number thread-id)
  2008. (gdb-setq-thread-number nil))
  2009. ;; When we continue current thread and it quickly exits,
  2010. ;; the pending triggers in gdb-handler-list left after gdb-running
  2011. ;; disallow us to properly call -thread-info without --thread option.
  2012. ;; Thus we need to use gdb-wait-for-pending.
  2013. (gdb-wait-for-pending
  2014. (gdb-emit-signal gdb-buf-publisher 'update-threads))))
  2015. (defun gdb-thread-selected (_token output-field)
  2016. "Handler for =thread-selected MI output record.
  2017. Sets `gdb-thread-number' to new id."
  2018. (let* ((result (gdb-json-string output-field))
  2019. (thread-id (bindat-get-field result 'id)))
  2020. (gdb-setq-thread-number thread-id)
  2021. ;; Typing `thread N' in GUD buffer makes GDB emit `^done' followed
  2022. ;; by `=thread-selected' notification. `^done' causes `gdb-update'
  2023. ;; as usually. Things happen too fast and second call (from
  2024. ;; gdb-thread-selected handler) gets cut off by our beloved
  2025. ;; pending triggers.
  2026. ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
  2027. ;; body will get executed when `gdb-handler-list' if free of
  2028. ;; pending triggers.
  2029. (gdb-wait-for-pending
  2030. (gdb-update))))
  2031. (defun gdb-running (_token output-field)
  2032. (let* ((thread-id
  2033. (bindat-get-field (gdb-json-string output-field) 'thread-id)))
  2034. ;; We reset gdb-frame-number to nil if current thread has gone
  2035. ;; running. This can't be done in gdb-thread-list-handler-custom
  2036. ;; because we need correct gdb-frame-number by the time
  2037. ;; -thread-info command is sent.
  2038. (when (or (string-equal thread-id "all")
  2039. (string-equal thread-id gdb-thread-number))
  2040. (setq gdb-frame-number nil)))
  2041. (setq gdb-inferior-status "running")
  2042. (gdb-force-mode-line-update
  2043. (propertize gdb-inferior-status 'face font-lock-type-face))
  2044. (when (not gdb-non-stop)
  2045. (setq gud-running t))
  2046. (setq gdb-active-process t))
  2047. (defun gdb-starting (_output-field _result)
  2048. ;; CLI commands don't emit ^running at the moment so use gdb-running too.
  2049. (setq gdb-inferior-status "running")
  2050. (gdb-force-mode-line-update
  2051. (propertize gdb-inferior-status 'face font-lock-type-face))
  2052. (setq gdb-active-process t)
  2053. (setq gud-running t))
  2054. ;; -break-insert -t didn't give a reason before gdb 6.9
  2055. (defun gdb-stopped (_token output-field)
  2056. "Given the contents of *stopped MI async record, select new
  2057. current thread and update GDB buffers."
  2058. ;; Reason is available with target-async only
  2059. (let* ((result (gdb-json-string output-field))
  2060. (reason (bindat-get-field result 'reason))
  2061. (thread-id (bindat-get-field result 'thread-id)))
  2062. ;; -data-list-register-names needs to be issued for any stopped
  2063. ;; thread
  2064. (when (not gdb-register-names)
  2065. (gdb-input (concat "-data-list-register-names"
  2066. (if gdb-supports-non-stop
  2067. (concat " --thread " thread-id)))
  2068. 'gdb-register-names-handler))
  2069. ;; Don't set gud-last-frame here as it's currently done in
  2070. ;; gdb-frame-handler because synchronous GDB doesn't give these fields
  2071. ;; with CLI.
  2072. ;;(when file
  2073. ;; (setq
  2074. ;; ;; Extract the frame position from the marker.
  2075. ;; gud-last-frame (cons file
  2076. ;; (string-to-number
  2077. ;; (match-string 6 gud-marker-acc)))))
  2078. (setq gdb-inferior-status (or reason "unknown"))
  2079. (gdb-force-mode-line-update
  2080. (propertize gdb-inferior-status 'face font-lock-warning-face))
  2081. (if (string-equal reason "exited-normally")
  2082. (setq gdb-active-process nil))
  2083. ;; Select new current thread.
  2084. ;; Don't switch if we have no reasons selected
  2085. (when gdb-switch-reasons
  2086. ;; Switch from another stopped thread only if we have
  2087. ;; gdb-switch-when-another-stopped:
  2088. (when (or gdb-switch-when-another-stopped
  2089. (not (string= "stopped"
  2090. (bindat-get-field (gdb-current-buffer-thread) 'state))))
  2091. ;; Switch if current reason has been selected or we have no
  2092. ;; reasons
  2093. (if (or (eq gdb-switch-reasons t)
  2094. (member reason gdb-switch-reasons))
  2095. (when (not (string-equal gdb-thread-number thread-id))
  2096. (message "Switched to thread %s" thread-id)
  2097. (gdb-setq-thread-number thread-id))
  2098. (message "Thread %s stopped" thread-id))))
  2099. ;; Print "(gdb)" to GUD console
  2100. (when gdb-first-done-or-error
  2101. (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
  2102. ;; In non-stop, we update information as soon as another thread gets
  2103. ;; stopped
  2104. (when (or gdb-first-done-or-error
  2105. gdb-non-stop)
  2106. ;; In all-stop this updates gud-running properly as well.
  2107. (gdb-update)
  2108. (setq gdb-first-done-or-error nil))
  2109. (run-hook-with-args 'gdb-stopped-functions result)))
  2110. ;; Remove the trimmings from log stream containing debugging messages
  2111. ;; being produced by GDB's internals, use warning face and send to GUD
  2112. ;; buffer.
  2113. (defun gdb-internals (output-field)
  2114. (setq gdb-filter-output
  2115. (gdb-concat-output
  2116. gdb-filter-output
  2117. (if (string= output-field "\"\\n\"")
  2118. ""
  2119. (let ((error-message
  2120. (read output-field)))
  2121. (put-text-property
  2122. 0 (length error-message)
  2123. 'face font-lock-warning-face
  2124. error-message)
  2125. error-message)))))
  2126. ;; Remove the trimmings from the console stream and send to GUD buffer
  2127. ;; (frontend MI commands should not print to this stream)
  2128. (defun gdb-console (output-field)
  2129. (setq gdb-filter-output
  2130. (gdb-concat-output gdb-filter-output (read output-field))))
  2131. (defun gdb-done (token-number output-field is-complete)
  2132. (gdb-done-or-error token-number 'done output-field is-complete))
  2133. (defun gdb-error (token-number output-field is-complete)
  2134. (gdb-done-or-error token-number 'error output-field is-complete))
  2135. (defun gdb-done-or-error (token-number type output-field is-complete)
  2136. (if (string-equal token-number "")
  2137. ;; Output from command entered by user
  2138. (progn
  2139. (setq gdb-output-sink 'user)
  2140. (setq token-number nil)
  2141. ;; MI error - send to minibuffer
  2142. (when (eq type 'error)
  2143. ;; Skip "msg=" from `output-field'
  2144. (message "%s" (read (substring output-field 4)))
  2145. ;; Don't send to the console twice. (If it is a console error
  2146. ;; it is also in the console stream.)
  2147. (setq output-field nil)))
  2148. ;; Output from command from frontend.
  2149. (setq gdb-output-sink 'emacs))
  2150. ;; The process may already be dead (e.g. C-d at the gdb prompt).
  2151. (let* ((proc (get-buffer-process gud-comint-buffer))
  2152. (no-proc (or (null proc)
  2153. (memq (process-status proc) '(exit signal)))))
  2154. (when (and is-complete gdb-first-done-or-error)
  2155. (unless (or token-number gud-running no-proc)
  2156. (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
  2157. (gdb-update no-proc)
  2158. (setq gdb-first-done-or-error nil))
  2159. (setq gdb-filter-output
  2160. (gdb-concat-output gdb-filter-output output-field))
  2161. ;; We are done concatenating to the output sink. Restore it to user sink:
  2162. (setq gdb-output-sink 'user)
  2163. (when (and token-number is-complete)
  2164. (with-current-buffer
  2165. (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2166. (gdb-handle-reply (string-to-number token-number))))
  2167. (when is-complete
  2168. (gdb-clear-partial-output))))
  2169. (defun gdb-concat-output (so-far new)
  2170. (cond
  2171. ((eq gdb-output-sink 'user) (concat so-far new))
  2172. ((eq gdb-output-sink 'emacs)
  2173. (gdb-append-to-partial-output new)
  2174. so-far)))
  2175. (defun gdb-append-to-partial-output (string)
  2176. (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2177. (goto-char (point-max))
  2178. (insert string)))
  2179. (defun gdb-clear-partial-output ()
  2180. (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2181. (erase-buffer)))
  2182. (defun gdb-jsonify-buffer (&optional fix-key fix-list)
  2183. "Prepare GDB/MI output in current buffer for parsing with `json-read'.
  2184. Field names are wrapped in double quotes and equal signs are
  2185. replaced with semicolons.
  2186. If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
  2187. partial output. This is used to get rid of useless keys in lists
  2188. in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
  2189. -break-info are examples of MI commands which issue such
  2190. responses.
  2191. If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
  2192. \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
  2193. -break-info output when it contains breakpoint script field
  2194. incompatible with GDB/MI output syntax."
  2195. (save-excursion
  2196. (goto-char (point-min))
  2197. (when fix-key
  2198. (save-excursion
  2199. (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
  2200. (replace-match "" nil nil nil 1))))
  2201. (when fix-list
  2202. (save-excursion
  2203. ;; Find positions of braces which enclose broken list
  2204. (while (re-search-forward (concat fix-list "={\"") nil t)
  2205. (let ((p1 (goto-char (- (point) 2)))
  2206. (p2 (progn (forward-sexp)
  2207. (1- (point)))))
  2208. ;; Replace braces with brackets
  2209. (save-excursion
  2210. (goto-char p1)
  2211. (delete-char 1)
  2212. (insert "[")
  2213. (goto-char p2)
  2214. (delete-char 1)
  2215. (insert "]"))))))
  2216. (goto-char (point-min))
  2217. (insert "{")
  2218. (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
  2219. gdb--string-regexp "\\)")))
  2220. (while (re-search-forward re nil t)
  2221. (replace-match "\"\\1\":\\2" nil nil)))
  2222. (goto-char (point-max))
  2223. (insert "}")))
  2224. (defun gdb-json-read-buffer (&optional fix-key fix-list)
  2225. "Prepare and parse GDB/MI output in current buffer with `json-read'.
  2226. FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
  2227. (gdb-jsonify-buffer fix-key fix-list)
  2228. (save-excursion
  2229. (goto-char (point-min))
  2230. (let ((json-array-type 'list))
  2231. (json-read))))
  2232. (defun gdb-json-string (string &optional fix-key fix-list)
  2233. "Prepare and parse STRING containing GDB/MI output with `json-read'.
  2234. FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
  2235. (with-temp-buffer
  2236. (insert string)
  2237. (gdb-json-read-buffer fix-key fix-list)))
  2238. (defun gdb-json-partial-output (&optional fix-key fix-list)
  2239. "Prepare and parse gdb-partial-output-buffer with `json-read'.
  2240. FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
  2241. (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
  2242. (gdb-json-read-buffer fix-key fix-list)))
  2243. (defun gdb-line-posns (line)
  2244. "Return a pair of LINE beginning and end positions."
  2245. (let ((offset (1+ (- line (line-number-at-pos)))))
  2246. (cons
  2247. (line-beginning-position offset)
  2248. (line-end-position offset))))
  2249. (defmacro gdb-mark-line (line variable)
  2250. "Set VARIABLE marker to point at beginning of LINE.
  2251. If current window has no fringes, inverse colors on LINE.
  2252. Return position where LINE begins."
  2253. `(save-excursion
  2254. (let* ((posns (gdb-line-posns ,line))
  2255. (start-posn (car posns))
  2256. (end-posn (cdr posns)))
  2257. (set-marker ,variable (copy-marker start-posn))
  2258. (when (not (> (car (window-fringes)) 0))
  2259. (put-text-property start-posn end-posn
  2260. 'font-lock-face '(:inverse-video t)))
  2261. start-posn)))
  2262. (defun gdb-pad-string (string padding)
  2263. (format (concat "%" (number-to-string padding) "s") string))
  2264. ;; gdb-table struct is a way to programmatically construct simple
  2265. ;; tables. It help to reliably align columns of data in GDB buffers
  2266. ;; and provides
  2267. (cl-defstruct gdb-table
  2268. (column-sizes nil)
  2269. (rows nil)
  2270. (row-properties nil)
  2271. (right-align nil))
  2272. (defun gdb-table-add-row (table row &optional properties)
  2273. "Add ROW of string to TABLE and recalculate column sizes.
  2274. When non-nil, PROPERTIES will be added to the whole row when
  2275. calling `gdb-table-string'."
  2276. (let ((rows (gdb-table-rows table))
  2277. (row-properties (gdb-table-row-properties table))
  2278. (column-sizes (gdb-table-column-sizes table))
  2279. (right-align (gdb-table-right-align table)))
  2280. (when (not column-sizes)
  2281. (setf (gdb-table-column-sizes table)
  2282. (make-list (length row) 0)))
  2283. (setf (gdb-table-rows table)
  2284. (append rows (list row)))
  2285. (setf (gdb-table-row-properties table)
  2286. (append row-properties (list properties)))
  2287. (setf (gdb-table-column-sizes table)
  2288. (cl-mapcar (lambda (x s)
  2289. (let ((new-x
  2290. (max (abs x) (string-width (or s "")))))
  2291. (if right-align new-x (- new-x))))
  2292. (gdb-table-column-sizes table)
  2293. row))
  2294. ;; Avoid trailing whitespace at eol
  2295. (if (not (gdb-table-right-align table))
  2296. (setcar (last (gdb-table-column-sizes table)) 0))))
  2297. (defun gdb-table-string (table &optional sep)
  2298. "Return TABLE as a string with columns separated with SEP."
  2299. (let ((column-sizes (gdb-table-column-sizes table)))
  2300. (mapconcat
  2301. 'identity
  2302. (cl-mapcar
  2303. (lambda (row properties)
  2304. (apply 'propertize
  2305. (mapconcat 'identity
  2306. (cl-mapcar (lambda (s x) (gdb-pad-string s x))
  2307. row column-sizes)
  2308. sep)
  2309. properties))
  2310. (gdb-table-rows table)
  2311. (gdb-table-row-properties table))
  2312. "\n")))
  2313. ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
  2314. (defun gdb-get-many-fields (struct &rest fields)
  2315. "Return a list of FIELDS values from STRUCT."
  2316. (let ((values))
  2317. (dolist (field fields)
  2318. (push (bindat-get-field struct field) values))
  2319. (nreverse values)))
  2320. (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
  2321. handler-name
  2322. &optional signal-list)
  2323. "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
  2324. HANDLER-NAME as its handler. HANDLER-NAME is bound to current
  2325. buffer with `gdb-bind-function-to-buffer'.
  2326. If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
  2327. defined trigger is called with an argument from SIGNAL-LIST. It's
  2328. not recommended to define triggers with empty SIGNAL-LIST.
  2329. Normally triggers should respond at least to 'update signal.
  2330. Normally the trigger defined by this command must be called from
  2331. the buffer where HANDLER-NAME must work. This should be done so
  2332. that buffer-local thread number may be used in GDB-COMMAND (by
  2333. calling `gdb-current-context-command').
  2334. `gdb-bind-function-to-buffer' is used to achieve this, see
  2335. `gdb-get-buffer-create'.
  2336. Triggers defined by this command are meant to be used as a
  2337. trigger argument when describing buffer types with
  2338. `gdb-set-buffer-rules'."
  2339. `(defun ,trigger-name (&optional signal)
  2340. (when
  2341. (or (not ,signal-list)
  2342. (memq signal ,signal-list))
  2343. (gdb-input ,gdb-command
  2344. (gdb-bind-function-to-buffer ',handler-name (current-buffer))
  2345. (cons (current-buffer) ',trigger-name)))))
  2346. ;; Used by disassembly buffer only, the rest use
  2347. ;; def-gdb-trigger-and-handler
  2348. (defmacro def-gdb-auto-update-handler (handler-name custom-defun
  2349. &optional nopreserve)
  2350. "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
  2351. Handlers are normally called from the buffers they put output in.
  2352. Erase current buffer and evaluate CUSTOM-DEFUN.
  2353. Then call `gdb-update-buffer-name'.
  2354. If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
  2355. `(defun ,handler-name ()
  2356. (let* ((inhibit-read-only t)
  2357. ,@(unless nopreserve
  2358. '((window (get-buffer-window (current-buffer) 0))
  2359. (start (window-start window))
  2360. (p (window-point window)))))
  2361. (erase-buffer)
  2362. (,custom-defun)
  2363. (gdb-update-buffer-name)
  2364. ,@(when (not nopreserve)
  2365. '((set-window-start window start)
  2366. (set-window-point window p))))))
  2367. (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
  2368. handler-name custom-defun
  2369. &optional signal-list)
  2370. "Define trigger and handler.
  2371. TRIGGER-NAME trigger is defined to send GDB-COMMAND.
  2372. See `def-gdb-auto-update-trigger'.
  2373. HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
  2374. See `def-gdb-auto-update-handler'."
  2375. `(progn
  2376. (def-gdb-auto-update-trigger ,trigger-name
  2377. ,gdb-command
  2378. ,handler-name ,signal-list)
  2379. (def-gdb-auto-update-handler ,handler-name
  2380. ,custom-defun)))
  2381. ;; Breakpoint buffer : This displays the output of `-break-list'.
  2382. (def-gdb-trigger-and-handler
  2383. gdb-invalidate-breakpoints "-break-list"
  2384. gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
  2385. '(start update))
  2386. (gdb-set-buffer-rules
  2387. 'gdb-breakpoints-buffer
  2388. 'gdb-breakpoints-buffer-name
  2389. 'gdb-breakpoints-mode
  2390. 'gdb-invalidate-breakpoints)
  2391. (defun gdb-breakpoints-list-handler-custom ()
  2392. (let ((breakpoints-list (bindat-get-field
  2393. (gdb-json-partial-output "bkpt" "script")
  2394. 'BreakpointTable 'body))
  2395. (table (make-gdb-table)))
  2396. (setq gdb-breakpoints-list nil)
  2397. (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
  2398. (dolist (breakpoint breakpoints-list)
  2399. (add-to-list 'gdb-breakpoints-list
  2400. (cons (bindat-get-field breakpoint 'number)
  2401. breakpoint))
  2402. (let ((at (bindat-get-field breakpoint 'at))
  2403. (pending (bindat-get-field breakpoint 'pending))
  2404. (func (bindat-get-field breakpoint 'func))
  2405. (type (bindat-get-field breakpoint 'type)))
  2406. (gdb-table-add-row table
  2407. (list
  2408. (bindat-get-field breakpoint 'number)
  2409. (or type "")
  2410. (or (bindat-get-field breakpoint 'disp) "")
  2411. (let ((flag (bindat-get-field breakpoint 'enabled)))
  2412. (if (string-equal flag "y")
  2413. (eval-when-compile
  2414. (propertize "y" 'font-lock-face
  2415. font-lock-warning-face))
  2416. (eval-when-compile
  2417. (propertize "n" 'font-lock-face
  2418. font-lock-comment-face))))
  2419. (bindat-get-field breakpoint 'addr)
  2420. (or (bindat-get-field breakpoint 'times) "")
  2421. (if (and type (string-match ".*watchpoint" type))
  2422. (bindat-get-field breakpoint 'what)
  2423. (or pending at
  2424. (concat "in "
  2425. (propertize (or func "unknown")
  2426. 'font-lock-face font-lock-function-name-face)
  2427. (gdb-frame-location breakpoint)))))
  2428. ;; Add clickable properties only for breakpoints with file:line
  2429. ;; information
  2430. (append (list 'gdb-breakpoint breakpoint)
  2431. (when func '(help-echo "mouse-2, RET: visit breakpoint"
  2432. mouse-face highlight))))))
  2433. (insert (gdb-table-string table " "))
  2434. (gdb-place-breakpoints)))
  2435. ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
  2436. (defun gdb-place-breakpoints ()
  2437. ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
  2438. (dolist (buffer (buffer-list))
  2439. (with-current-buffer buffer
  2440. (if (and (eq gud-minor-mode 'gdbmi)
  2441. (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
  2442. (gdb-remove-breakpoint-icons (point-min) (point-max)))))
  2443. (dolist (breakpoint gdb-breakpoints-list)
  2444. (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
  2445. ; an associative list
  2446. (line (bindat-get-field breakpoint 'line)))
  2447. (when line
  2448. (let ((file (bindat-get-field breakpoint 'fullname))
  2449. (flag (bindat-get-field breakpoint 'enabled))
  2450. (bptno (bindat-get-field breakpoint 'number)))
  2451. (unless (and file (file-exists-p file))
  2452. (setq file (cdr (assoc bptno gdb-location-alist))))
  2453. (if (or (null file)
  2454. (string-equal file "File not found"))
  2455. ;; If the full filename is not recorded in the
  2456. ;; breakpoint structure or in `gdb-location-alist', use
  2457. ;; -file-list-exec-source-file to extract it.
  2458. (when (setq file (bindat-get-field breakpoint 'file))
  2459. (gdb-input (concat "list " file ":1") 'ignore)
  2460. (gdb-input "-file-list-exec-source-file"
  2461. `(lambda () (gdb-get-location
  2462. ,bptno ,line ,flag))))
  2463. (with-current-buffer (find-file-noselect file 'nowarn)
  2464. (gdb-init-buffer)
  2465. ;; Only want one breakpoint icon at each location.
  2466. (gdb-put-breakpoint-icon (string-equal flag "y") bptno
  2467. (string-to-number line)))))))))
  2468. (defconst gdb-source-file-regexp
  2469. (concat "fullname=\\(" gdb--string-regexp "\\)"))
  2470. (defun gdb-get-location (bptno line flag)
  2471. "Find the directory containing the relevant source file.
  2472. Put in buffer and place breakpoint icon."
  2473. (goto-char (point-min))
  2474. (catch 'file-not-found
  2475. (if (re-search-forward gdb-source-file-regexp nil t)
  2476. (delete (cons bptno "File not found") gdb-location-alist)
  2477. ;; FIXME: Why/how do we use (match-string 1) when the search failed?
  2478. (push (cons bptno (match-string 1)) gdb-location-alist)
  2479. (gdb-resync)
  2480. (unless (assoc bptno gdb-location-alist)
  2481. (push (cons bptno "File not found") gdb-location-alist)
  2482. (message-box "Cannot find source file for breakpoint location.
  2483. Add directory to search path for source files using the GDB command, dir."))
  2484. (throw 'file-not-found nil))
  2485. (with-current-buffer (find-file-noselect (match-string 1))
  2486. (gdb-init-buffer)
  2487. ;; only want one breakpoint icon at each location
  2488. (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
  2489. (add-hook 'find-file-hook 'gdb-find-file-hook)
  2490. (defun gdb-find-file-hook ()
  2491. "Set up buffer for debugging if file is part of the source code
  2492. of the current session."
  2493. (if (and (buffer-name gud-comint-buffer)
  2494. ;; in case gud or gdb-ui is just loaded
  2495. gud-comint-buffer
  2496. (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
  2497. 'gdbmi))
  2498. (if (member buffer-file-name gdb-source-file-list)
  2499. (with-current-buffer (find-buffer-visiting buffer-file-name)
  2500. (gdb-init-buffer)))))
  2501. (declare-function gud-remove "gdb-mi" t t) ; gud-def
  2502. (declare-function gud-break "gdb-mi" t t) ; gud-def
  2503. (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
  2504. (defun gdb-mouse-set-clear-breakpoint (event)
  2505. "Set/clear breakpoint in left fringe/margin at mouse click.
  2506. If not in a source or disassembly buffer just set point."
  2507. (interactive "e")
  2508. (mouse-minibuffer-check event)
  2509. (let ((posn (event-end event)))
  2510. (with-selected-window (posn-window posn)
  2511. (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
  2512. (if (numberp (posn-point posn))
  2513. (save-excursion
  2514. (goto-char (posn-point posn))
  2515. (if (or (posn-object posn)
  2516. (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
  2517. 'breakpoint))
  2518. (gud-remove nil)
  2519. (gud-break nil)))))
  2520. (posn-set-point posn))))
  2521. (defun gdb-mouse-toggle-breakpoint-margin (event)
  2522. "Enable/disable breakpoint in left margin with mouse click."
  2523. (interactive "e")
  2524. (mouse-minibuffer-check event)
  2525. (let ((posn (event-end event)))
  2526. (if (numberp (posn-point posn))
  2527. (with-selected-window (posn-window posn)
  2528. (save-excursion
  2529. (goto-char (posn-point posn))
  2530. (if (posn-object posn)
  2531. (gud-basic-call
  2532. (let ((bptno (get-text-property
  2533. 0 'gdb-bptno (car (posn-string posn)))))
  2534. (concat
  2535. (if (get-text-property
  2536. 0 'gdb-enabled (car (posn-string posn)))
  2537. "-break-disable "
  2538. "-break-enable ")
  2539. bptno)))))))))
  2540. (defun gdb-mouse-toggle-breakpoint-fringe (event)
  2541. "Enable/disable breakpoint in left fringe with mouse click."
  2542. (interactive "e")
  2543. (mouse-minibuffer-check event)
  2544. (let* ((posn (event-end event))
  2545. (pos (posn-point posn))
  2546. obj)
  2547. (when (numberp pos)
  2548. (with-selected-window (posn-window posn)
  2549. (with-current-buffer (window-buffer)
  2550. (goto-char pos)
  2551. (dolist (overlay (overlays-in pos pos))
  2552. (when (overlay-get overlay 'put-break)
  2553. (setq obj (overlay-get overlay 'before-string))))
  2554. (when (stringp obj)
  2555. (gud-basic-call
  2556. (concat
  2557. (if (get-text-property 0 'gdb-enabled obj)
  2558. "-break-disable "
  2559. "-break-enable ")
  2560. (get-text-property 0 'gdb-bptno obj)))))))))
  2561. (defun gdb-breakpoints-buffer-name ()
  2562. (concat "*breakpoints of " (gdb-get-target-string) "*"))
  2563. (defun gdb-display-breakpoints-buffer (&optional thread)
  2564. "Display GDB breakpoints."
  2565. (interactive)
  2566. (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
  2567. (defun gdb-frame-breakpoints-buffer (&optional thread)
  2568. "Display GDB breakpoints in another frame."
  2569. (interactive)
  2570. (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
  2571. gdb-display-buffer-other-frame-action))
  2572. (defvar gdb-breakpoints-mode-map
  2573. (let ((map (make-sparse-keymap))
  2574. (menu (make-sparse-keymap "Breakpoints")))
  2575. (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
  2576. (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
  2577. (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
  2578. (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
  2579. (suppress-keymap map)
  2580. (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
  2581. (define-key map " " 'gdb-toggle-breakpoint)
  2582. (define-key map "D" 'gdb-delete-breakpoint)
  2583. ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
  2584. (define-key map "q" 'gdb-delete-frame-or-window)
  2585. (define-key map "\r" 'gdb-goto-breakpoint)
  2586. (define-key map "\t" (lambda ()
  2587. (interactive)
  2588. (gdb-set-window-buffer
  2589. (gdb-get-buffer-create 'gdb-threads-buffer) t)))
  2590. (define-key map [mouse-2] 'gdb-goto-breakpoint)
  2591. (define-key map [follow-link] 'mouse-face)
  2592. map))
  2593. (defun gdb-delete-frame-or-window ()
  2594. "Delete frame if there is only one window. Otherwise delete the window."
  2595. (interactive)
  2596. (if (one-window-p) (delete-frame)
  2597. (delete-window)))
  2598. ;;from make-mode-line-mouse-map
  2599. (defun gdb-make-header-line-mouse-map (mouse function) "\
  2600. Return a keymap with single entry for mouse key MOUSE on the header line.
  2601. MOUSE is defined to run function FUNCTION with no args in the buffer
  2602. corresponding to the mode line clicked."
  2603. (let ((map (make-sparse-keymap)))
  2604. (define-key map (vector 'header-line mouse) function)
  2605. (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
  2606. map))
  2607. (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
  2608. `(propertize ,name
  2609. 'help-echo ,help-echo
  2610. 'mouse-face ',mouse-face
  2611. 'face ',face
  2612. 'local-map
  2613. (gdb-make-header-line-mouse-map
  2614. 'mouse-1
  2615. (lambda (event) (interactive "e")
  2616. (save-selected-window
  2617. (select-window (posn-window (event-start event)))
  2618. (gdb-set-window-buffer
  2619. (gdb-get-buffer-create ',buffer) t) )))))
  2620. ;; uses "-thread-info". Needs GDB 7.0 onwards.
  2621. ;;; Threads view
  2622. (defun gdb-threads-buffer-name ()
  2623. (concat "*threads of " (gdb-get-target-string) "*"))
  2624. (defun gdb-display-threads-buffer (&optional thread)
  2625. "Display GDB threads."
  2626. (interactive)
  2627. (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
  2628. (defun gdb-frame-threads-buffer (&optional thread)
  2629. "Display GDB threads in another frame."
  2630. (interactive)
  2631. (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
  2632. gdb-display-buffer-other-frame-action))
  2633. (def-gdb-trigger-and-handler
  2634. gdb-invalidate-threads (gdb-current-context-command "-thread-info")
  2635. gdb-thread-list-handler gdb-thread-list-handler-custom
  2636. '(start update update-threads))
  2637. (gdb-set-buffer-rules
  2638. 'gdb-threads-buffer
  2639. 'gdb-threads-buffer-name
  2640. 'gdb-threads-mode
  2641. 'gdb-invalidate-threads)
  2642. (defvar gdb-threads-font-lock-keywords
  2643. '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
  2644. (" \\(stopped\\)" (1 font-lock-warning-face))
  2645. (" \\(running\\)" (1 font-lock-string-face))
  2646. ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
  2647. "Font lock keywords used in `gdb-threads-mode'.")
  2648. (defvar gdb-threads-mode-map
  2649. (let ((map (make-sparse-keymap)))
  2650. (define-key map "\r" 'gdb-select-thread)
  2651. (define-key map "f" 'gdb-display-stack-for-thread)
  2652. (define-key map "F" 'gdb-frame-stack-for-thread)
  2653. (define-key map "l" 'gdb-display-locals-for-thread)
  2654. (define-key map "L" 'gdb-frame-locals-for-thread)
  2655. (define-key map "r" 'gdb-display-registers-for-thread)
  2656. (define-key map "R" 'gdb-frame-registers-for-thread)
  2657. (define-key map "d" 'gdb-display-disassembly-for-thread)
  2658. (define-key map "D" 'gdb-frame-disassembly-for-thread)
  2659. (define-key map "i" 'gdb-interrupt-thread)
  2660. (define-key map "c" 'gdb-continue-thread)
  2661. (define-key map "s" 'gdb-step-thread)
  2662. (define-key map "\t"
  2663. (lambda ()
  2664. (interactive)
  2665. (gdb-set-window-buffer
  2666. (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
  2667. (define-key map [mouse-2] 'gdb-select-thread)
  2668. (define-key map [follow-link] 'mouse-face)
  2669. map))
  2670. (defvar gdb-threads-header
  2671. (list
  2672. (gdb-propertize-header
  2673. "Breakpoints" gdb-breakpoints-buffer
  2674. "mouse-1: select" mode-line-highlight mode-line-inactive)
  2675. " "
  2676. (gdb-propertize-header "Threads" gdb-threads-buffer
  2677. nil nil mode-line)))
  2678. (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
  2679. "Major mode for GDB threads."
  2680. (setq gdb-thread-position (make-marker))
  2681. (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
  2682. (setq header-line-format gdb-threads-header)
  2683. (set (make-local-variable 'font-lock-defaults)
  2684. '(gdb-threads-font-lock-keywords))
  2685. 'gdb-invalidate-threads)
  2686. (defun gdb-thread-list-handler-custom ()
  2687. (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
  2688. (table (make-gdb-table))
  2689. (marked-line nil))
  2690. (setq gdb-threads-list nil)
  2691. (setq gdb-running-threads-count 0)
  2692. (setq gdb-stopped-threads-count 0)
  2693. (set-marker gdb-thread-position nil)
  2694. (dolist (thread (reverse threads-list))
  2695. (let ((running (equal (bindat-get-field thread 'state) "running")))
  2696. (add-to-list 'gdb-threads-list
  2697. (cons (bindat-get-field thread 'id)
  2698. thread))
  2699. (cl-incf (if running
  2700. gdb-running-threads-count
  2701. gdb-stopped-threads-count))
  2702. (gdb-table-add-row
  2703. table
  2704. (list
  2705. (bindat-get-field thread 'id)
  2706. (concat
  2707. (if gdb-thread-buffer-verbose-names
  2708. (concat (bindat-get-field thread 'target-id) " ") "")
  2709. (bindat-get-field thread 'state)
  2710. ;; Include frame information for stopped threads
  2711. (if (not running)
  2712. (concat
  2713. " in " (bindat-get-field thread 'frame 'func)
  2714. (if gdb-thread-buffer-arguments
  2715. (concat
  2716. " ("
  2717. (let ((args (bindat-get-field thread 'frame 'args)))
  2718. (mapconcat
  2719. (lambda (arg)
  2720. (apply #'format "%s=%s"
  2721. (gdb-get-many-fields arg 'name 'value)))
  2722. args ","))
  2723. ")")
  2724. "")
  2725. (if gdb-thread-buffer-locations
  2726. (gdb-frame-location (bindat-get-field thread 'frame)) "")
  2727. (if gdb-thread-buffer-addresses
  2728. (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
  2729. "")))
  2730. (list
  2731. 'gdb-thread thread
  2732. 'mouse-face 'highlight
  2733. 'help-echo "mouse-2, RET: select thread")))
  2734. (when (string-equal gdb-thread-number
  2735. (bindat-get-field thread 'id))
  2736. (setq marked-line (length gdb-threads-list))))
  2737. (insert (gdb-table-string table " "))
  2738. (when marked-line
  2739. (gdb-mark-line marked-line gdb-thread-position)))
  2740. ;; We update gud-running here because we need to make sure that
  2741. ;; gdb-threads-list is up-to-date
  2742. (gdb-update-gud-running)
  2743. (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
  2744. (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
  2745. "Define a NAME command which will act upon thread on the current line.
  2746. CUSTOM-DEFUN may use locally bound `thread' variable, which will
  2747. be the value of 'gdb-thread property of the current line.
  2748. If `gdb-thread' is nil, error is signaled."
  2749. `(defun ,name (&optional event)
  2750. ,(when doc doc)
  2751. (interactive (list last-input-event))
  2752. (if event (posn-set-point (event-end event)))
  2753. (save-excursion
  2754. (beginning-of-line)
  2755. (let ((thread (get-text-property (point) 'gdb-thread)))
  2756. (if thread
  2757. ,custom-defun
  2758. (error "Not recognized as thread line"))))))
  2759. (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
  2760. &optional doc)
  2761. "Define a NAME which will call BUFFER-COMMAND with id of thread
  2762. on the current line."
  2763. `(def-gdb-thread-buffer-command ,name
  2764. (,buffer-command (bindat-get-field thread 'id))
  2765. ,doc))
  2766. (def-gdb-thread-buffer-command gdb-select-thread
  2767. (let ((new-id (bindat-get-field thread 'id)))
  2768. (gdb-setq-thread-number new-id)
  2769. (gdb-input (concat "-thread-select " new-id) 'ignore)
  2770. (gdb-update))
  2771. "Select the thread at current line of threads buffer.")
  2772. (def-gdb-thread-buffer-simple-command
  2773. gdb-display-stack-for-thread
  2774. gdb-preemptively-display-stack-buffer
  2775. "Display stack buffer for the thread at current line.")
  2776. (def-gdb-thread-buffer-simple-command
  2777. gdb-display-locals-for-thread
  2778. gdb-preemptively-display-locals-buffer
  2779. "Display locals buffer for the thread at current line.")
  2780. (def-gdb-thread-buffer-simple-command
  2781. gdb-display-registers-for-thread
  2782. gdb-preemptively-display-registers-buffer
  2783. "Display registers buffer for the thread at current line.")
  2784. (def-gdb-thread-buffer-simple-command
  2785. gdb-display-disassembly-for-thread
  2786. gdb-preemptively-display-disassembly-buffer
  2787. "Display disassembly buffer for the thread at current line.")
  2788. (def-gdb-thread-buffer-simple-command
  2789. gdb-frame-stack-for-thread
  2790. gdb-frame-stack-buffer
  2791. "Display another frame with stack buffer for thread at current line.")
  2792. (def-gdb-thread-buffer-simple-command
  2793. gdb-frame-locals-for-thread
  2794. gdb-frame-locals-buffer
  2795. "Display another frame with locals buffer for thread at current line.")
  2796. (def-gdb-thread-buffer-simple-command
  2797. gdb-frame-registers-for-thread
  2798. gdb-frame-registers-buffer
  2799. "Display another frame with registers buffer for the thread at current line.")
  2800. (def-gdb-thread-buffer-simple-command
  2801. gdb-frame-disassembly-for-thread
  2802. gdb-frame-disassembly-buffer
  2803. "Display another frame with disassembly buffer for the thread at current line.")
  2804. (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
  2805. "Define a NAME which will execute GUD-COMMAND with
  2806. `gdb-thread-number' locally bound to id of thread on the current
  2807. line."
  2808. `(def-gdb-thread-buffer-command ,name
  2809. (if gdb-non-stop
  2810. (let ((gdb-thread-number (bindat-get-field thread 'id))
  2811. (gdb-gud-control-all-threads nil))
  2812. (call-interactively #',gud-command))
  2813. (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
  2814. ,doc))
  2815. (def-gdb-thread-buffer-gud-command
  2816. gdb-interrupt-thread
  2817. gud-stop-subjob
  2818. "Interrupt thread at current line.")
  2819. ;; Defined opaquely in M-x gdb via gud-def.
  2820. (declare-function gud-cont "gdb-mi" (arg) t)
  2821. (def-gdb-thread-buffer-gud-command
  2822. gdb-continue-thread
  2823. gud-cont
  2824. "Continue thread at current line.")
  2825. (declare-function gud-step "gdb-mi" (arg) t)
  2826. (def-gdb-thread-buffer-gud-command
  2827. gdb-step-thread
  2828. gud-step
  2829. "Step thread at current line.")
  2830. ;;; Memory view
  2831. (defcustom gdb-memory-rows 8
  2832. "Number of data rows in memory window."
  2833. :type 'integer
  2834. :group 'gud
  2835. :version "23.2")
  2836. (defcustom gdb-memory-columns 4
  2837. "Number of data columns in memory window."
  2838. :type 'integer
  2839. :group 'gud
  2840. :version "23.2")
  2841. (defcustom gdb-memory-format "x"
  2842. "Display format of data items in memory window."
  2843. :type '(choice (const :tag "Hexadecimal" "x")
  2844. (const :tag "Signed decimal" "d")
  2845. (const :tag "Unsigned decimal" "u")
  2846. (const :tag "Octal" "o")
  2847. (const :tag "Binary" "t"))
  2848. :group 'gud
  2849. :version "22.1")
  2850. (defcustom gdb-memory-unit 4
  2851. "Unit size of data items in memory window."
  2852. :type '(choice (const :tag "Byte" 1)
  2853. (const :tag "Halfword" 2)
  2854. (const :tag "Word" 4)
  2855. (const :tag "Giant word" 8))
  2856. :group 'gud
  2857. :version "23.2")
  2858. (def-gdb-trigger-and-handler
  2859. gdb-invalidate-memory
  2860. (format "-data-read-memory %s %s %d %d %d"
  2861. gdb-memory-address
  2862. gdb-memory-format
  2863. gdb-memory-unit
  2864. gdb-memory-rows
  2865. gdb-memory-columns)
  2866. gdb-read-memory-handler
  2867. gdb-read-memory-custom
  2868. '(start update))
  2869. (gdb-set-buffer-rules
  2870. 'gdb-memory-buffer
  2871. 'gdb-memory-buffer-name
  2872. 'gdb-memory-mode
  2873. 'gdb-invalidate-memory)
  2874. (defun gdb-memory-column-width (size format)
  2875. "Return length of string with memory unit of SIZE in FORMAT.
  2876. SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
  2877. in `gdb-memory-format'."
  2878. (let ((format-base (cdr (assoc format
  2879. '(("x" . 16)
  2880. ("d" . 10) ("u" . 10)
  2881. ("o" . 8)
  2882. ("t" . 2))))))
  2883. (if format-base
  2884. (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
  2885. (cond ((string-equal format "x")
  2886. (+ 2 res)) ; hexadecimal numbers have 0x in front
  2887. ((or (string-equal format "d")
  2888. (string-equal format "o"))
  2889. (1+ res))
  2890. (t res)))
  2891. (error "Unknown format"))))
  2892. (defun gdb-read-memory-custom ()
  2893. (let* ((res (gdb-json-partial-output))
  2894. (err-msg (bindat-get-field res 'msg)))
  2895. (if (not err-msg)
  2896. (let ((memory (bindat-get-field res 'memory)))
  2897. (setq gdb-memory-address (bindat-get-field res 'addr))
  2898. (setq gdb-memory-next-page (bindat-get-field res 'next-page))
  2899. (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
  2900. (setq gdb-memory-last-address gdb-memory-address)
  2901. (dolist (row memory)
  2902. (insert (concat (bindat-get-field row 'addr) ":"))
  2903. (dolist (column (bindat-get-field row 'data))
  2904. (insert (gdb-pad-string column
  2905. (+ 2 (gdb-memory-column-width
  2906. gdb-memory-unit
  2907. gdb-memory-format)))))
  2908. (newline)))
  2909. ;; Show last page instead of empty buffer when out of bounds
  2910. (progn
  2911. (let ((gdb-memory-address gdb-memory-last-address))
  2912. (gdb-invalidate-memory 'update)
  2913. (error err-msg))))))
  2914. (defvar gdb-memory-mode-map
  2915. (let ((map (make-sparse-keymap)))
  2916. (suppress-keymap map t)
  2917. (define-key map "q" 'kill-this-buffer)
  2918. (define-key map "n" 'gdb-memory-show-next-page)
  2919. (define-key map "p" 'gdb-memory-show-previous-page)
  2920. (define-key map "a" 'gdb-memory-set-address)
  2921. (define-key map "t" 'gdb-memory-format-binary)
  2922. (define-key map "o" 'gdb-memory-format-octal)
  2923. (define-key map "u" 'gdb-memory-format-unsigned)
  2924. (define-key map "d" 'gdb-memory-format-signed)
  2925. (define-key map "x" 'gdb-memory-format-hexadecimal)
  2926. (define-key map "b" 'gdb-memory-unit-byte)
  2927. (define-key map "h" 'gdb-memory-unit-halfword)
  2928. (define-key map "w" 'gdb-memory-unit-word)
  2929. (define-key map "g" 'gdb-memory-unit-giant)
  2930. (define-key map "R" 'gdb-memory-set-rows)
  2931. (define-key map "C" 'gdb-memory-set-columns)
  2932. map))
  2933. (defun gdb-memory-set-address-event (event)
  2934. "Handle a click on address field in memory buffer header."
  2935. (interactive "e")
  2936. (save-selected-window
  2937. (select-window (posn-window (event-start event)))
  2938. (gdb-memory-set-address)))
  2939. ;; Non-event version for use within keymap
  2940. (defun gdb-memory-set-address ()
  2941. "Set the start memory address."
  2942. (interactive)
  2943. (let ((arg (read-from-minibuffer "Memory address: ")))
  2944. (setq gdb-memory-address arg))
  2945. (gdb-invalidate-memory 'update))
  2946. (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
  2947. "Define a function NAME which reads new VAR value from minibuffer."
  2948. `(defun ,name (event)
  2949. ,(when doc doc)
  2950. (interactive "e")
  2951. (save-selected-window
  2952. (select-window (posn-window (event-start event)))
  2953. (let* ((arg (read-from-minibuffer ,echo-string))
  2954. (count (string-to-number arg)))
  2955. (if (<= count 0)
  2956. (error "Positive number only")
  2957. (customize-set-variable ',variable count)
  2958. (gdb-invalidate-memory 'update))))))
  2959. (def-gdb-set-positive-number
  2960. gdb-memory-set-rows
  2961. gdb-memory-rows
  2962. "Rows: "
  2963. "Set the number of data rows in memory window.")
  2964. (def-gdb-set-positive-number
  2965. gdb-memory-set-columns
  2966. gdb-memory-columns
  2967. "Columns: "
  2968. "Set the number of data columns in memory window.")
  2969. (defmacro def-gdb-memory-format (name format doc)
  2970. "Define a function NAME to switch memory buffer to use FORMAT.
  2971. DOC is an optional documentation string."
  2972. `(defun ,name () ,(when doc doc)
  2973. (interactive)
  2974. (customize-set-variable 'gdb-memory-format ,format)
  2975. (gdb-invalidate-memory 'update)))
  2976. (def-gdb-memory-format
  2977. gdb-memory-format-binary "t"
  2978. "Set the display format to binary.")
  2979. (def-gdb-memory-format
  2980. gdb-memory-format-octal "o"
  2981. "Set the display format to octal.")
  2982. (def-gdb-memory-format
  2983. gdb-memory-format-unsigned "u"
  2984. "Set the display format to unsigned decimal.")
  2985. (def-gdb-memory-format
  2986. gdb-memory-format-signed "d"
  2987. "Set the display format to decimal.")
  2988. (def-gdb-memory-format
  2989. gdb-memory-format-hexadecimal "x"
  2990. "Set the display format to hexadecimal.")
  2991. (defvar gdb-memory-format-map
  2992. (let ((map (make-sparse-keymap)))
  2993. (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
  2994. map)
  2995. "Keymap to select format in the header line.")
  2996. (defvar gdb-memory-format-menu
  2997. (let ((map (make-sparse-keymap "Format")))
  2998. (define-key map [binary]
  2999. '(menu-item "Binary" gdb-memory-format-binary
  3000. :button (:radio . (equal gdb-memory-format "t"))))
  3001. (define-key map [octal]
  3002. '(menu-item "Octal" gdb-memory-format-octal
  3003. :button (:radio . (equal gdb-memory-format "o"))))
  3004. (define-key map [unsigned]
  3005. '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
  3006. :button (:radio . (equal gdb-memory-format "u"))))
  3007. (define-key map [signed]
  3008. '(menu-item "Signed Decimal" gdb-memory-format-signed
  3009. :button (:radio . (equal gdb-memory-format "d"))))
  3010. (define-key map [hexadecimal]
  3011. '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
  3012. :button (:radio . (equal gdb-memory-format "x"))))
  3013. map)
  3014. "Menu of display formats in the header line.")
  3015. (defun gdb-memory-format-menu (event)
  3016. (interactive "@e")
  3017. (x-popup-menu event gdb-memory-format-menu))
  3018. (defun gdb-memory-format-menu-1 (event)
  3019. (interactive "e")
  3020. (save-selected-window
  3021. (select-window (posn-window (event-start event)))
  3022. (let* ((selection (gdb-memory-format-menu event))
  3023. (binding (and selection (lookup-key gdb-memory-format-menu
  3024. (vector (car selection))))))
  3025. (if binding (call-interactively binding)))))
  3026. (defmacro def-gdb-memory-unit (name unit-size doc)
  3027. "Define a function NAME to switch memory unit size to UNIT-SIZE.
  3028. DOC is an optional documentation string."
  3029. `(defun ,name () ,(when doc doc)
  3030. (interactive)
  3031. (customize-set-variable 'gdb-memory-unit ,unit-size)
  3032. (gdb-invalidate-memory 'update)))
  3033. (def-gdb-memory-unit gdb-memory-unit-giant 8
  3034. "Set the unit size to giant words (eight bytes).")
  3035. (def-gdb-memory-unit gdb-memory-unit-word 4
  3036. "Set the unit size to words (four bytes).")
  3037. (def-gdb-memory-unit gdb-memory-unit-halfword 2
  3038. "Set the unit size to halfwords (two bytes).")
  3039. (def-gdb-memory-unit gdb-memory-unit-byte 1
  3040. "Set the unit size to bytes.")
  3041. (defmacro def-gdb-memory-show-page (name address-var &optional doc)
  3042. "Define a function NAME which show new address in memory buffer.
  3043. The defined function switches Memory buffer to show address
  3044. stored in ADDRESS-VAR variable.
  3045. DOC is an optional documentation string."
  3046. `(defun ,name
  3047. ,(when doc doc)
  3048. (interactive)
  3049. (let ((gdb-memory-address ,address-var))
  3050. (gdb-invalidate-memory))))
  3051. (def-gdb-memory-show-page gdb-memory-show-previous-page
  3052. gdb-memory-prev-page)
  3053. (def-gdb-memory-show-page gdb-memory-show-next-page
  3054. gdb-memory-next-page)
  3055. (defvar gdb-memory-unit-map
  3056. (let ((map (make-sparse-keymap)))
  3057. (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
  3058. map)
  3059. "Keymap to select units in the header line.")
  3060. (defvar gdb-memory-unit-menu
  3061. (let ((map (make-sparse-keymap "Unit")))
  3062. (define-key map [giantwords]
  3063. '(menu-item "Giant words" gdb-memory-unit-giant
  3064. :button (:radio . (equal gdb-memory-unit 8))))
  3065. (define-key map [words]
  3066. '(menu-item "Words" gdb-memory-unit-word
  3067. :button (:radio . (equal gdb-memory-unit 4))))
  3068. (define-key map [halfwords]
  3069. '(menu-item "Halfwords" gdb-memory-unit-halfword
  3070. :button (:radio . (equal gdb-memory-unit 2))))
  3071. (define-key map [bytes]
  3072. '(menu-item "Bytes" gdb-memory-unit-byte
  3073. :button (:radio . (equal gdb-memory-unit 1))))
  3074. map)
  3075. "Menu of units in the header line.")
  3076. (defun gdb-memory-unit-menu (event)
  3077. (interactive "@e")
  3078. (x-popup-menu event gdb-memory-unit-menu))
  3079. (defun gdb-memory-unit-menu-1 (event)
  3080. (interactive "e")
  3081. (save-selected-window
  3082. (select-window (posn-window (event-start event)))
  3083. (let* ((selection (gdb-memory-unit-menu event))
  3084. (binding (and selection (lookup-key gdb-memory-unit-menu
  3085. (vector (car selection))))))
  3086. (if binding (call-interactively binding)))))
  3087. (defvar gdb-memory-font-lock-keywords
  3088. '(;; <__function.name+n>
  3089. ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
  3090. (1 font-lock-function-name-face)))
  3091. "Font lock keywords used in `gdb-memory-mode'.")
  3092. (defvar gdb-memory-header
  3093. '(:eval
  3094. (concat
  3095. "Start address["
  3096. (propertize "-"
  3097. 'face font-lock-warning-face
  3098. 'help-echo "mouse-1: decrement address"
  3099. 'mouse-face 'mode-line-highlight
  3100. 'local-map (gdb-make-header-line-mouse-map
  3101. 'mouse-1
  3102. #'gdb-memory-show-previous-page))
  3103. "|"
  3104. (propertize "+"
  3105. 'face font-lock-warning-face
  3106. 'help-echo "mouse-1: increment address"
  3107. 'mouse-face 'mode-line-highlight
  3108. 'local-map (gdb-make-header-line-mouse-map
  3109. 'mouse-1
  3110. #'gdb-memory-show-next-page))
  3111. "]: "
  3112. (propertize gdb-memory-address
  3113. 'face font-lock-warning-face
  3114. 'help-echo "mouse-1: set start address"
  3115. 'mouse-face 'mode-line-highlight
  3116. 'local-map (gdb-make-header-line-mouse-map
  3117. 'mouse-1
  3118. #'gdb-memory-set-address-event))
  3119. " Rows: "
  3120. (propertize (number-to-string gdb-memory-rows)
  3121. 'face font-lock-warning-face
  3122. 'help-echo "mouse-1: set number of columns"
  3123. 'mouse-face 'mode-line-highlight
  3124. 'local-map (gdb-make-header-line-mouse-map
  3125. 'mouse-1
  3126. #'gdb-memory-set-rows))
  3127. " Columns: "
  3128. (propertize (number-to-string gdb-memory-columns)
  3129. 'face font-lock-warning-face
  3130. 'help-echo "mouse-1: set number of columns"
  3131. 'mouse-face 'mode-line-highlight
  3132. 'local-map (gdb-make-header-line-mouse-map
  3133. 'mouse-1
  3134. #'gdb-memory-set-columns))
  3135. " Display Format: "
  3136. (propertize gdb-memory-format
  3137. 'face font-lock-warning-face
  3138. 'help-echo "mouse-3: select display format"
  3139. 'mouse-face 'mode-line-highlight
  3140. 'local-map gdb-memory-format-map)
  3141. " Unit Size: "
  3142. (propertize (number-to-string gdb-memory-unit)
  3143. 'face font-lock-warning-face
  3144. 'help-echo "mouse-3: select unit size"
  3145. 'mouse-face 'mode-line-highlight
  3146. 'local-map gdb-memory-unit-map)))
  3147. "Header line used in `gdb-memory-mode'.")
  3148. (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
  3149. "Major mode for examining memory."
  3150. (setq header-line-format gdb-memory-header)
  3151. (set (make-local-variable 'font-lock-defaults)
  3152. '(gdb-memory-font-lock-keywords))
  3153. 'gdb-invalidate-memory)
  3154. (defun gdb-memory-buffer-name ()
  3155. (concat "*memory of " (gdb-get-target-string) "*"))
  3156. (defun gdb-display-memory-buffer (&optional thread)
  3157. "Display GDB memory contents."
  3158. (interactive)
  3159. (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
  3160. (defun gdb-frame-memory-buffer ()
  3161. "Display memory contents in another frame."
  3162. (interactive)
  3163. (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
  3164. gdb-display-buffer-other-frame-action))
  3165. ;;; Disassembly view
  3166. (defun gdb-disassembly-buffer-name ()
  3167. (gdb-current-context-buffer-name
  3168. (concat "disassembly of " (gdb-get-target-string))))
  3169. (defun gdb-display-disassembly-buffer (&optional thread)
  3170. "Display GDB disassembly information."
  3171. (interactive)
  3172. (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
  3173. (def-gdb-preempt-display-buffer
  3174. gdb-preemptively-display-disassembly-buffer
  3175. 'gdb-disassembly-buffer)
  3176. (defun gdb-frame-disassembly-buffer (&optional thread)
  3177. "Display GDB disassembly information in another frame."
  3178. (interactive)
  3179. (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
  3180. gdb-display-buffer-other-frame-action))
  3181. (def-gdb-auto-update-trigger gdb-invalidate-disassembly
  3182. (let* ((frame (gdb-current-buffer-frame))
  3183. (file (bindat-get-field frame 'fullname))
  3184. (line (bindat-get-field frame 'line)))
  3185. (if file
  3186. (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
  3187. ;; If we're unable to get a file name / line for $PC, simply
  3188. ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
  3189. ;; 150 bytes) instructions.
  3190. "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
  3191. gdb-disassembly-handler
  3192. ;; We update disassembly only after we have actual frame information
  3193. ;; about all threads, so no there's `update' signal in this list
  3194. '(start update-disassembly))
  3195. (def-gdb-auto-update-handler
  3196. gdb-disassembly-handler
  3197. gdb-disassembly-handler-custom
  3198. t)
  3199. (gdb-set-buffer-rules
  3200. 'gdb-disassembly-buffer
  3201. 'gdb-disassembly-buffer-name
  3202. 'gdb-disassembly-mode
  3203. 'gdb-invalidate-disassembly)
  3204. (defvar gdb-disassembly-font-lock-keywords
  3205. '(;; <__function.name+n>
  3206. ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
  3207. (1 font-lock-function-name-face))
  3208. ;; 0xNNNNNNNN <__function.name+n>: opcode
  3209. ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
  3210. (4 font-lock-keyword-face))
  3211. ;; %register(at least i386)
  3212. ("%\\sw+" . font-lock-variable-name-face)
  3213. ("^\\(Dump of assembler code for function\\) \\(.+\\):"
  3214. (1 font-lock-comment-face)
  3215. (2 font-lock-function-name-face))
  3216. ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
  3217. "Font lock keywords used in `gdb-disassembly-mode'.")
  3218. (defvar gdb-disassembly-mode-map
  3219. ;; TODO
  3220. (let ((map (make-sparse-keymap)))
  3221. (suppress-keymap map)
  3222. (define-key map "q" 'kill-this-buffer)
  3223. map))
  3224. (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
  3225. "Major mode for GDB disassembly information."
  3226. ;; TODO Rename overlay variable for disassembly mode
  3227. (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
  3228. (setq fringes-outside-margins t)
  3229. (set (make-local-variable 'gdb-disassembly-position) (make-marker))
  3230. (set (make-local-variable 'font-lock-defaults)
  3231. '(gdb-disassembly-font-lock-keywords))
  3232. 'gdb-invalidate-disassembly)
  3233. (defun gdb-disassembly-handler-custom ()
  3234. (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
  3235. (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
  3236. (table (make-gdb-table))
  3237. (marked-line nil))
  3238. (dolist (instr instructions)
  3239. (gdb-table-add-row table
  3240. (list
  3241. (bindat-get-field instr 'address)
  3242. (let
  3243. ((func-name (bindat-get-field instr 'func-name))
  3244. (offset (bindat-get-field instr 'offset)))
  3245. (if func-name
  3246. (format "<%s+%s>:" func-name offset)
  3247. ""))
  3248. (bindat-get-field instr 'inst)))
  3249. (when (string-equal (bindat-get-field instr 'address)
  3250. address)
  3251. (progn
  3252. (setq marked-line (length (gdb-table-rows table)))
  3253. (setq fringe-indicator-alist
  3254. (if (string-equal gdb-frame-number "0")
  3255. nil
  3256. '((overlay-arrow . hollow-right-triangle)))))))
  3257. (insert (gdb-table-string table " "))
  3258. (gdb-disassembly-place-breakpoints)
  3259. ;; Mark current position with overlay arrow and scroll window to
  3260. ;; that point
  3261. (when marked-line
  3262. (let ((window (get-buffer-window (current-buffer) 0)))
  3263. (set-window-point window (gdb-mark-line marked-line
  3264. gdb-disassembly-position))))
  3265. (setq mode-name
  3266. (gdb-current-context-mode-name
  3267. (concat "Disassembly: "
  3268. (bindat-get-field (gdb-current-buffer-frame) 'func))))))
  3269. (defun gdb-disassembly-place-breakpoints ()
  3270. (gdb-remove-breakpoint-icons (point-min) (point-max))
  3271. (dolist (breakpoint gdb-breakpoints-list)
  3272. (let* ((breakpoint (cdr breakpoint))
  3273. (bptno (bindat-get-field breakpoint 'number))
  3274. (flag (bindat-get-field breakpoint 'enabled))
  3275. (address (bindat-get-field breakpoint 'addr)))
  3276. (save-excursion
  3277. (goto-char (point-min))
  3278. (if (re-search-forward (concat "^" address) nil t)
  3279. (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
  3280. (defvar gdb-breakpoints-header
  3281. (list
  3282. (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
  3283. nil nil mode-line)
  3284. " "
  3285. (gdb-propertize-header "Threads" gdb-threads-buffer
  3286. "mouse-1: select" mode-line-highlight
  3287. mode-line-inactive)))
  3288. ;;; Breakpoints view
  3289. (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
  3290. "Major mode for gdb breakpoints."
  3291. (setq header-line-format gdb-breakpoints-header)
  3292. 'gdb-invalidate-breakpoints)
  3293. (defun gdb-toggle-breakpoint ()
  3294. "Enable/disable breakpoint at current line of breakpoints buffer."
  3295. (interactive)
  3296. (save-excursion
  3297. (beginning-of-line)
  3298. (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
  3299. (if breakpoint
  3300. (gud-basic-call
  3301. (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
  3302. "-break-disable "
  3303. "-break-enable ")
  3304. (bindat-get-field breakpoint 'number)))
  3305. (error "Not recognized as break/watchpoint line")))))
  3306. (defun gdb-delete-breakpoint ()
  3307. "Delete the breakpoint at current line of breakpoints buffer."
  3308. (interactive)
  3309. (save-excursion
  3310. (beginning-of-line)
  3311. (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
  3312. (if breakpoint
  3313. (gud-basic-call (concat "-break-delete "
  3314. (bindat-get-field breakpoint 'number)))
  3315. (error "Not recognized as break/watchpoint line")))))
  3316. (defun gdb-goto-breakpoint (&optional event)
  3317. "Go to the location of breakpoint at current line of breakpoints buffer."
  3318. (interactive (list last-input-event))
  3319. (if event (posn-set-point (event-end event)))
  3320. ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
  3321. (let ((window (get-buffer-window gud-comint-buffer)))
  3322. (if window (save-selected-window (select-window window))))
  3323. (save-excursion
  3324. (beginning-of-line)
  3325. (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
  3326. (if breakpoint
  3327. (let ((bptno (bindat-get-field breakpoint 'number))
  3328. (file (bindat-get-field breakpoint 'fullname))
  3329. (line (bindat-get-field breakpoint 'line)))
  3330. (save-selected-window
  3331. (let* ((buffer (find-file-noselect
  3332. (if (file-exists-p file) file
  3333. (cdr (assoc bptno gdb-location-alist)))))
  3334. (window (or (gdb-display-source-buffer buffer)
  3335. (display-buffer buffer))))
  3336. (setq gdb-source-window window)
  3337. (with-current-buffer buffer
  3338. (goto-char (point-min))
  3339. (forward-line (1- (string-to-number line)))
  3340. (set-window-point window (point))))))
  3341. (error "Not recognized as break/watchpoint line")))))
  3342. ;; Frames buffer. This displays a perpetually correct backtrack trace.
  3343. ;;
  3344. (def-gdb-trigger-and-handler
  3345. gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
  3346. gdb-stack-list-frames-handler gdb-stack-list-frames-custom
  3347. '(start update))
  3348. (gdb-set-buffer-rules
  3349. 'gdb-stack-buffer
  3350. 'gdb-stack-buffer-name
  3351. 'gdb-frames-mode
  3352. 'gdb-invalidate-frames)
  3353. (defun gdb-frame-location (frame)
  3354. "Return \" of file:line\" or \" of library\" for structure FRAME.
  3355. FRAME must have either \"file\" and \"line\" members or \"from\"
  3356. member."
  3357. (let ((file (bindat-get-field frame 'file))
  3358. (line (bindat-get-field frame 'line))
  3359. (from (bindat-get-field frame 'from)))
  3360. (let ((res (or (and file line (concat file ":" line))
  3361. from)))
  3362. (if res (concat " of " res) ""))))
  3363. (defun gdb-stack-list-frames-custom ()
  3364. (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
  3365. (table (make-gdb-table)))
  3366. (set-marker gdb-stack-position nil)
  3367. (dolist (frame stack)
  3368. (gdb-table-add-row table
  3369. (list
  3370. (bindat-get-field frame 'level)
  3371. "in"
  3372. (concat
  3373. (bindat-get-field frame 'func)
  3374. (if gdb-stack-buffer-locations
  3375. (gdb-frame-location frame) "")
  3376. (if gdb-stack-buffer-addresses
  3377. (concat " at " (bindat-get-field frame 'addr)) "")))
  3378. `(mouse-face highlight
  3379. help-echo "mouse-2, RET: Select frame"
  3380. gdb-frame ,frame)))
  3381. (insert (gdb-table-string table " ")))
  3382. (when (and gdb-frame-number
  3383. (gdb-buffer-shows-main-thread-p))
  3384. (gdb-mark-line (1+ (string-to-number gdb-frame-number))
  3385. gdb-stack-position))
  3386. (setq mode-name
  3387. (gdb-current-context-mode-name "Frames")))
  3388. (defun gdb-stack-buffer-name ()
  3389. (gdb-current-context-buffer-name
  3390. (concat "stack frames of " (gdb-get-target-string))))
  3391. (defun gdb-display-stack-buffer (&optional thread)
  3392. "Display GDB backtrace for current stack."
  3393. (interactive)
  3394. (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
  3395. (def-gdb-preempt-display-buffer
  3396. gdb-preemptively-display-stack-buffer
  3397. 'gdb-stack-buffer nil t)
  3398. (defun gdb-frame-stack-buffer (&optional thread)
  3399. "Display GDB backtrace for current stack in another frame."
  3400. (interactive)
  3401. (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
  3402. gdb-display-buffer-other-frame-action))
  3403. (defvar gdb-frames-mode-map
  3404. (let ((map (make-sparse-keymap)))
  3405. (suppress-keymap map)
  3406. (define-key map "q" 'kill-this-buffer)
  3407. (define-key map "\r" 'gdb-select-frame)
  3408. (define-key map [mouse-2] 'gdb-select-frame)
  3409. (define-key map [follow-link] 'mouse-face)
  3410. map))
  3411. (defvar gdb-frames-font-lock-keywords
  3412. '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
  3413. "Font lock keywords used in `gdb-frames-mode'.")
  3414. (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
  3415. "Major mode for gdb call stack."
  3416. (setq gdb-stack-position (make-marker))
  3417. (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
  3418. (setq truncate-lines t) ;; Make it easier to see overlay arrow.
  3419. (set (make-local-variable 'font-lock-defaults)
  3420. '(gdb-frames-font-lock-keywords))
  3421. 'gdb-invalidate-frames)
  3422. (defun gdb-select-frame (&optional event)
  3423. "Select the frame and display the relevant source."
  3424. (interactive (list last-input-event))
  3425. (if event (posn-set-point (event-end event)))
  3426. (let ((frame (get-text-property (point) 'gdb-frame)))
  3427. (if frame
  3428. (if (gdb-buffer-shows-main-thread-p)
  3429. (let ((new-level (bindat-get-field frame 'level)))
  3430. (setq gdb-frame-number new-level)
  3431. (gdb-input (concat "-stack-select-frame " new-level)
  3432. 'ignore)
  3433. (gdb-update))
  3434. (error "Could not select frame for non-current thread"))
  3435. (error "Not recognized as frame line"))))
  3436. ;; Locals buffer.
  3437. ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
  3438. (def-gdb-trigger-and-handler
  3439. gdb-invalidate-locals
  3440. (concat (gdb-current-context-command "-stack-list-locals")
  3441. " --simple-values")
  3442. gdb-locals-handler gdb-locals-handler-custom
  3443. '(start update))
  3444. (gdb-set-buffer-rules
  3445. 'gdb-locals-buffer
  3446. 'gdb-locals-buffer-name
  3447. 'gdb-locals-mode
  3448. 'gdb-invalidate-locals)
  3449. (defvar gdb-locals-watch-map
  3450. (let ((map (make-sparse-keymap)))
  3451. (suppress-keymap map)
  3452. (define-key map "\r" 'gud-watch)
  3453. (define-key map [mouse-2] 'gud-watch)
  3454. map)
  3455. "Keymap to create watch expression of a complex data type local variable.")
  3456. (defvar gdb-edit-locals-map-1
  3457. (let ((map (make-sparse-keymap)))
  3458. (suppress-keymap map)
  3459. (define-key map "\r" 'gdb-edit-locals-value)
  3460. (define-key map [mouse-2] 'gdb-edit-locals-value)
  3461. map)
  3462. "Keymap to edit value of a simple data type local variable.")
  3463. (defun gdb-edit-locals-value (&optional event)
  3464. "Assign a value to a variable displayed in the locals buffer."
  3465. (interactive (list last-input-event))
  3466. (save-excursion
  3467. (if event (posn-set-point (event-end event)))
  3468. (beginning-of-line)
  3469. (let* ((var (bindat-get-field
  3470. (get-text-property (point) 'gdb-local-variable) 'name))
  3471. (value (read-string (format "New value (%s): " var))))
  3472. (gud-basic-call
  3473. (concat "-gdb-set variable " var " = " value)))))
  3474. ;; Don't display values of arrays or structures.
  3475. ;; These can be expanded using gud-watch.
  3476. (defun gdb-locals-handler-custom ()
  3477. (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
  3478. (table (make-gdb-table)))
  3479. (dolist (local locals-list)
  3480. (let ((name (bindat-get-field local 'name))
  3481. (value (bindat-get-field local 'value))
  3482. (type (bindat-get-field local 'type)))
  3483. (if (or (not value)
  3484. (string-match "\\0x" value))
  3485. (add-text-properties 0 (length name)
  3486. `(mouse-face highlight
  3487. help-echo "mouse-2: create watch expression"
  3488. local-map ,gdb-locals-watch-map)
  3489. name)
  3490. (add-text-properties 0 (length value)
  3491. `(mouse-face highlight
  3492. help-echo "mouse-2: edit value"
  3493. local-map ,gdb-edit-locals-map-1)
  3494. value))
  3495. (gdb-table-add-row
  3496. table
  3497. (list
  3498. (propertize type 'font-lock-face font-lock-type-face)
  3499. (propertize name 'font-lock-face font-lock-variable-name-face)
  3500. value)
  3501. `(gdb-local-variable ,local))))
  3502. (insert (gdb-table-string table " "))
  3503. (setq mode-name
  3504. (gdb-current-context-mode-name
  3505. (concat "Locals: "
  3506. (bindat-get-field (gdb-current-buffer-frame) 'func))))))
  3507. (defvar gdb-locals-header
  3508. (list
  3509. (gdb-propertize-header "Locals" gdb-locals-buffer
  3510. nil nil mode-line)
  3511. " "
  3512. (gdb-propertize-header "Registers" gdb-registers-buffer
  3513. "mouse-1: select" mode-line-highlight
  3514. mode-line-inactive)))
  3515. (defvar gdb-locals-mode-map
  3516. (let ((map (make-sparse-keymap)))
  3517. (suppress-keymap map)
  3518. (define-key map "q" 'kill-this-buffer)
  3519. (define-key map "\t" (lambda ()
  3520. (interactive)
  3521. (gdb-set-window-buffer
  3522. (gdb-get-buffer-create
  3523. 'gdb-registers-buffer
  3524. gdb-thread-number) t)))
  3525. map))
  3526. (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
  3527. "Major mode for gdb locals."
  3528. (setq header-line-format gdb-locals-header)
  3529. 'gdb-invalidate-locals)
  3530. (defun gdb-locals-buffer-name ()
  3531. (gdb-current-context-buffer-name
  3532. (concat "locals of " (gdb-get-target-string))))
  3533. (defun gdb-display-locals-buffer (&optional thread)
  3534. "Display the local variables of current GDB stack."
  3535. (interactive)
  3536. (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
  3537. (def-gdb-preempt-display-buffer
  3538. gdb-preemptively-display-locals-buffer
  3539. 'gdb-locals-buffer nil t)
  3540. (defun gdb-frame-locals-buffer (&optional thread)
  3541. "Display the local variables of the current GDB stack in another frame."
  3542. (interactive)
  3543. (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
  3544. gdb-display-buffer-other-frame-action))
  3545. ;; Registers buffer.
  3546. (def-gdb-trigger-and-handler
  3547. gdb-invalidate-registers
  3548. (concat (gdb-current-context-command "-data-list-register-values") " x")
  3549. gdb-registers-handler
  3550. gdb-registers-handler-custom
  3551. '(start update))
  3552. (gdb-set-buffer-rules
  3553. 'gdb-registers-buffer
  3554. 'gdb-registers-buffer-name
  3555. 'gdb-registers-mode
  3556. 'gdb-invalidate-registers)
  3557. (defun gdb-registers-handler-custom ()
  3558. (when gdb-register-names
  3559. (let ((register-values
  3560. (bindat-get-field (gdb-json-partial-output) 'register-values))
  3561. (table (make-gdb-table)))
  3562. (dolist (register register-values)
  3563. (let* ((register-number (bindat-get-field register 'number))
  3564. (value (bindat-get-field register 'value))
  3565. (register-name (nth (string-to-number register-number)
  3566. gdb-register-names)))
  3567. (gdb-table-add-row
  3568. table
  3569. (list
  3570. (propertize register-name
  3571. 'font-lock-face font-lock-variable-name-face)
  3572. (if (member register-number gdb-changed-registers)
  3573. (propertize value 'font-lock-face font-lock-warning-face)
  3574. value))
  3575. `(mouse-face highlight
  3576. help-echo "mouse-2: edit value"
  3577. gdb-register-name ,register-name))))
  3578. (insert (gdb-table-string table " ")))
  3579. (setq mode-name
  3580. (gdb-current-context-mode-name "Registers"))))
  3581. (defun gdb-edit-register-value (&optional event)
  3582. "Assign a value to a register displayed in the registers buffer."
  3583. (interactive (list last-input-event))
  3584. (save-excursion
  3585. (if event (posn-set-point (event-end event)))
  3586. (beginning-of-line)
  3587. (let* ((var (bindat-get-field
  3588. (get-text-property (point) 'gdb-register-name)))
  3589. (value (read-string (format "New value (%s): " var))))
  3590. (gud-basic-call
  3591. (concat "-gdb-set variable $" var " = " value)))))
  3592. (defvar gdb-registers-mode-map
  3593. (let ((map (make-sparse-keymap)))
  3594. (suppress-keymap map)
  3595. (define-key map "\r" 'gdb-edit-register-value)
  3596. (define-key map [mouse-2] 'gdb-edit-register-value)
  3597. (define-key map "q" 'kill-this-buffer)
  3598. (define-key map "\t" (lambda ()
  3599. (interactive)
  3600. (gdb-set-window-buffer
  3601. (gdb-get-buffer-create
  3602. 'gdb-locals-buffer
  3603. gdb-thread-number) t)))
  3604. map))
  3605. (defvar gdb-registers-header
  3606. (list
  3607. (gdb-propertize-header "Locals" gdb-locals-buffer
  3608. "mouse-1: select" mode-line-highlight
  3609. mode-line-inactive)
  3610. " "
  3611. (gdb-propertize-header "Registers" gdb-registers-buffer
  3612. nil nil mode-line)))
  3613. (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
  3614. "Major mode for gdb registers."
  3615. (setq header-line-format gdb-registers-header)
  3616. 'gdb-invalidate-registers)
  3617. (defun gdb-registers-buffer-name ()
  3618. (gdb-current-context-buffer-name
  3619. (concat "registers of " (gdb-get-target-string))))
  3620. (defun gdb-display-registers-buffer (&optional thread)
  3621. "Display GDB register contents."
  3622. (interactive)
  3623. (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
  3624. (def-gdb-preempt-display-buffer
  3625. gdb-preemptively-display-registers-buffer
  3626. 'gdb-registers-buffer nil t)
  3627. (defun gdb-frame-registers-buffer (&optional thread)
  3628. "Display GDB register contents in another frame."
  3629. (interactive)
  3630. (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
  3631. gdb-display-buffer-other-frame-action))
  3632. ;; Needs GDB 6.4 onwards (used to fail with no stack).
  3633. (defun gdb-get-changed-registers ()
  3634. (when (gdb-get-buffer 'gdb-registers-buffer)
  3635. (gdb-input "-data-list-changed-registers"
  3636. 'gdb-changed-registers-handler
  3637. 'gdb-get-changed-registers)))
  3638. (defun gdb-changed-registers-handler ()
  3639. (setq gdb-changed-registers nil)
  3640. (dolist (register-number
  3641. (bindat-get-field (gdb-json-partial-output) 'changed-registers))
  3642. (push register-number gdb-changed-registers)))
  3643. (defun gdb-register-names-handler ()
  3644. ;; Don't use pending triggers because this handler is called
  3645. ;; only once (in gdb-init-1)
  3646. (setq gdb-register-names nil)
  3647. (dolist (register-name
  3648. (bindat-get-field (gdb-json-partial-output) 'register-names))
  3649. (push register-name gdb-register-names))
  3650. (setq gdb-register-names (reverse gdb-register-names)))
  3651. (defun gdb-get-source-file-list ()
  3652. "Create list of source files for current GDB session.
  3653. If buffers already exist for any of these files, `gud-minor-mode'
  3654. is set in them."
  3655. (goto-char (point-min))
  3656. (while (re-search-forward gdb-source-file-regexp nil t)
  3657. (push (read (match-string 1)) gdb-source-file-list))
  3658. (dolist (buffer (buffer-list))
  3659. (with-current-buffer buffer
  3660. (when (member buffer-file-name gdb-source-file-list)
  3661. (gdb-init-buffer)))))
  3662. (defun gdb-get-main-selected-frame ()
  3663. "Trigger for `gdb-frame-handler' which uses main current thread.
  3664. Called from `gdb-update'."
  3665. (gdb-input (gdb-current-context-command "-stack-info-frame")
  3666. 'gdb-frame-handler
  3667. 'gdb-get-main-selected-frame))
  3668. (defun gdb-frame-handler ()
  3669. "Set `gdb-selected-frame' and `gdb-selected-file' to show
  3670. overlay arrow in source buffer."
  3671. (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
  3672. (when frame
  3673. (setq gdb-selected-frame (bindat-get-field frame 'func))
  3674. (setq gdb-selected-file (bindat-get-field frame 'fullname))
  3675. (setq gdb-frame-number (bindat-get-field frame 'level))
  3676. (setq gdb-frame-address (bindat-get-field frame 'addr))
  3677. (let ((line (bindat-get-field frame 'line)))
  3678. (setq gdb-selected-line (and line (string-to-number line)))
  3679. (when (and gdb-selected-file gdb-selected-line)
  3680. (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
  3681. (gud-display-frame)))
  3682. (if gud-overlay-arrow-position
  3683. (let ((buffer (marker-buffer gud-overlay-arrow-position))
  3684. (position (marker-position gud-overlay-arrow-position)))
  3685. (when buffer
  3686. (with-current-buffer buffer
  3687. (setq fringe-indicator-alist
  3688. (if (string-equal gdb-frame-number "0")
  3689. nil
  3690. '((overlay-arrow . hollow-right-triangle))))
  3691. (setq gud-overlay-arrow-position (make-marker))
  3692. (set-marker gud-overlay-arrow-position position))))))))
  3693. (defconst gdb-prompt-name-regexp
  3694. (concat "value=\\(" gdb--string-regexp "\\)"))
  3695. (defun gdb-get-prompt ()
  3696. "Find prompt for GDB session."
  3697. (goto-char (point-min))
  3698. (setq gdb-prompt-name nil)
  3699. (re-search-forward gdb-prompt-name-regexp nil t)
  3700. (setq gdb-prompt-name (read (match-string 1)))
  3701. ;; Insert first prompt.
  3702. (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
  3703. ;;;; Window management
  3704. (defun gdb-display-buffer (buf)
  3705. "Show buffer BUF, and make that window dedicated."
  3706. (let ((window (display-buffer buf)))
  3707. (set-window-dedicated-p window t)
  3708. window))
  3709. ;; (let ((answer (get-buffer-window buf 0)))
  3710. ;; (if answer
  3711. ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
  3712. ;; (let ((window (get-lru-window)))
  3713. ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
  3714. ;; 'gdbmi)
  3715. ;; (let ((largest (get-largest-window)))
  3716. ;; (setq answer (split-window largest))
  3717. ;; (set-window-buffer answer buf)
  3718. ;; (set-window-dedicated-p answer t)
  3719. ;; answer)
  3720. ;; (set-window-buffer window buf)
  3721. ;; window)))))
  3722. (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
  3723. "Find window displaying a buffer with the same
  3724. `gdb-buffer-type' as BUF and show BUF there. If no such window
  3725. exists, just call `gdb-display-buffer' for BUF. If the window
  3726. found is already dedicated, split window according to
  3727. SPLIT-HORIZONTAL and show BUF in the new window."
  3728. (if buf
  3729. (when (not (get-buffer-window buf))
  3730. (let* ((buf-type (gdb-buffer-type buf))
  3731. (existing-window
  3732. (get-window-with-predicate
  3733. #'(lambda (w)
  3734. (and (eq buf-type
  3735. (gdb-buffer-type (window-buffer w)))
  3736. (not (window-dedicated-p w)))))))
  3737. (if existing-window
  3738. (set-window-buffer existing-window buf)
  3739. (let ((dedicated-window
  3740. (get-window-with-predicate
  3741. #'(lambda (w)
  3742. (eq buf-type
  3743. (gdb-buffer-type (window-buffer w)))))))
  3744. (if dedicated-window
  3745. (set-window-buffer
  3746. (split-window dedicated-window nil split-horizontal) buf)
  3747. (gdb-display-buffer buf))))))
  3748. (error "Null buffer")))
  3749. ;;; Shared keymap initialization:
  3750. (let ((menu (make-sparse-keymap "GDB-Windows")))
  3751. (define-key gud-menu-map [displays]
  3752. `(menu-item "GDB-Windows" ,menu
  3753. :visible (eq gud-minor-mode 'gdbmi)))
  3754. (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
  3755. (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
  3756. (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
  3757. (define-key menu [disassembly]
  3758. '("Disassembly" . gdb-display-disassembly-buffer))
  3759. (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
  3760. (define-key menu [inferior]
  3761. '("IO" . gdb-display-io-buffer))
  3762. (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
  3763. (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
  3764. (define-key menu [breakpoints]
  3765. '("Breakpoints" . gdb-display-breakpoints-buffer)))
  3766. (let ((menu (make-sparse-keymap "GDB-Frames")))
  3767. (define-key gud-menu-map [frames]
  3768. `(menu-item "GDB-Frames" ,menu
  3769. :visible (eq gud-minor-mode 'gdbmi)))
  3770. (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
  3771. (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
  3772. (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
  3773. (define-key menu [disassembly]
  3774. '("Disassembly" . gdb-frame-disassembly-buffer))
  3775. (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
  3776. (define-key menu [inferior]
  3777. '("IO" . gdb-frame-io-buffer))
  3778. (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
  3779. (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
  3780. (define-key menu [breakpoints]
  3781. '("Breakpoints" . gdb-frame-breakpoints-buffer)))
  3782. (let ((menu (make-sparse-keymap "GDB-MI")))
  3783. (define-key menu [gdb-customize]
  3784. '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
  3785. :help "Customize Gdb Graphical Mode options."))
  3786. (define-key menu [gdb-many-windows]
  3787. '(menu-item "Display Other Windows" gdb-many-windows
  3788. :help "Toggle display of locals, stack and breakpoint information"
  3789. :button (:toggle . gdb-many-windows)))
  3790. (define-key menu [gdb-restore-windows]
  3791. '(menu-item "Restore Window Layout" gdb-restore-windows
  3792. :help "Restore standard layout for debug session."))
  3793. (define-key menu [sep1]
  3794. '(menu-item "--"))
  3795. (define-key menu [all-threads]
  3796. '(menu-item "GUD controls all threads"
  3797. (lambda ()
  3798. (interactive)
  3799. (setq gdb-gud-control-all-threads t))
  3800. :help "GUD start/stop commands apply to all threads"
  3801. :button (:radio . gdb-gud-control-all-threads)))
  3802. (define-key menu [current-thread]
  3803. '(menu-item "GUD controls current thread"
  3804. (lambda ()
  3805. (interactive)
  3806. (setq gdb-gud-control-all-threads nil))
  3807. :help "GUD start/stop commands apply to current thread only"
  3808. :button (:radio . (not gdb-gud-control-all-threads))))
  3809. (define-key menu [sep2]
  3810. '(menu-item "--"))
  3811. (define-key menu [gdb-customize-reasons]
  3812. '(menu-item "Customize switching..."
  3813. (lambda ()
  3814. (interactive)
  3815. (customize-option 'gdb-switch-reasons))))
  3816. (define-key menu [gdb-switch-when-another-stopped]
  3817. (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
  3818. gdb-switch-when-another-stopped
  3819. "Automatically switch to stopped thread"
  3820. "GDB thread switching %s"
  3821. "Switch to stopped thread"))
  3822. (define-key gud-menu-map [mi]
  3823. `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
  3824. ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
  3825. ;; GDB-MI menu will need to be moved to gud.el. We can't use
  3826. ;; tool-bar-local-item-from-menu here because it appends new buttons
  3827. ;; to toolbar from right to left while we want our A/T throttle to
  3828. ;; show up right before Run button.
  3829. (define-key-after gud-tool-bar-map [all-threads]
  3830. '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
  3831. :image (find-image '((:type xpm :file "gud/thread.xpm")))
  3832. :visible (and (eq gud-minor-mode 'gdbmi)
  3833. gdb-non-stop
  3834. (not gdb-gud-control-all-threads)))
  3835. 'run)
  3836. (define-key-after gud-tool-bar-map [current-thread]
  3837. '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
  3838. :image (find-image '((:type xpm :file "gud/all.xpm")))
  3839. :visible (and (eq gud-minor-mode 'gdbmi)
  3840. gdb-non-stop
  3841. gdb-gud-control-all-threads))
  3842. 'all-threads)
  3843. (defun gdb-frame-gdb-buffer ()
  3844. "Display GUD buffer in another frame."
  3845. (interactive)
  3846. (display-buffer-other-frame gud-comint-buffer))
  3847. (defun gdb-display-gdb-buffer ()
  3848. "Display GUD buffer."
  3849. (interactive)
  3850. (pop-to-buffer gud-comint-buffer nil 0))
  3851. (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
  3852. "Set buffer of selected window to NAME and dedicate window.
  3853. When IGNORE-DEDICATED is non-nil, buffer is set even if selected
  3854. window is dedicated."
  3855. (unless window (setq window (selected-window)))
  3856. (when ignore-dedicated
  3857. (set-window-dedicated-p window nil))
  3858. (set-window-buffer window (get-buffer name))
  3859. (set-window-dedicated-p window t))
  3860. (defun gdb-setup-windows ()
  3861. "Layout the window pattern for option `gdb-many-windows'."
  3862. (gdb-get-buffer-create 'gdb-locals-buffer)
  3863. (gdb-get-buffer-create 'gdb-stack-buffer)
  3864. (gdb-get-buffer-create 'gdb-breakpoints-buffer)
  3865. (set-window-dedicated-p (selected-window) nil)
  3866. (switch-to-buffer gud-comint-buffer)
  3867. (delete-other-windows)
  3868. (let ((win0 (selected-window))
  3869. (win1 (split-window nil ( / ( * (window-height) 3) 4)))
  3870. (win2 (split-window nil ( / (window-height) 3)))
  3871. (win3 (split-window-right)))
  3872. (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
  3873. (select-window win2)
  3874. (set-window-buffer
  3875. win2
  3876. (if gud-last-last-frame
  3877. (gud-find-file (car gud-last-last-frame))
  3878. (if gdb-main-file
  3879. (gud-find-file gdb-main-file)
  3880. ;; Put buffer list in window if we
  3881. ;; can't find a source file.
  3882. (list-buffers-noselect))))
  3883. (setq gdb-source-window (selected-window))
  3884. (let ((win4 (split-window-right)))
  3885. (gdb-set-window-buffer
  3886. (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
  3887. (select-window win1)
  3888. (gdb-set-window-buffer (gdb-stack-buffer-name))
  3889. (let ((win5 (split-window-right)))
  3890. (gdb-set-window-buffer (if gdb-show-threads-by-default
  3891. (gdb-threads-buffer-name)
  3892. (gdb-breakpoints-buffer-name))
  3893. nil win5))
  3894. (select-window win0)))
  3895. (define-minor-mode gdb-many-windows
  3896. "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
  3897. In this case it starts with two windows: one displaying the GUD
  3898. buffer and the other with the source file with the main routine
  3899. of the debugged program. Non-nil means display the layout shown for
  3900. `gdb'."
  3901. :global t
  3902. :group 'gdb
  3903. :version "22.1"
  3904. (if (and gud-comint-buffer
  3905. (buffer-name gud-comint-buffer))
  3906. (ignore-errors
  3907. (gdb-restore-windows))))
  3908. (defun gdb-restore-windows ()
  3909. "Restore the basic arrangement of windows used by gdb.
  3910. This arrangement depends on the value of option `gdb-many-windows'."
  3911. (interactive)
  3912. (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
  3913. (delete-other-windows)
  3914. (if gdb-many-windows
  3915. (gdb-setup-windows)
  3916. (when (or gud-last-last-frame gdb-show-main)
  3917. (let ((win (split-window)))
  3918. (set-window-buffer
  3919. win
  3920. (if gud-last-last-frame
  3921. (gud-find-file (car gud-last-last-frame))
  3922. (gud-find-file gdb-main-file)))
  3923. (setq gdb-source-window win)))))
  3924. ;; Called from `gud-sentinel' in gud.el:
  3925. (defun gdb-reset ()
  3926. "Exit a debugging session cleanly.
  3927. Kills the gdb buffers, and resets variables and the source buffers."
  3928. ;; The gdb-inferior buffer has a pty hooked up to the main gdb
  3929. ;; process. This pty must be deleted explicitly.
  3930. (let ((pty (get-process "gdb-inferior")))
  3931. (if pty (delete-process pty)))
  3932. ;; Find gdb-mi buffers and kill them.
  3933. (dolist (buffer (buffer-list))
  3934. (unless (eq buffer gud-comint-buffer)
  3935. (with-current-buffer buffer
  3936. (if (eq gud-minor-mode 'gdbmi)
  3937. (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
  3938. (kill-buffer nil)
  3939. (gdb-remove-breakpoint-icons (point-min) (point-max) t)
  3940. (setq gud-minor-mode nil)
  3941. (kill-local-variable 'tool-bar-map)
  3942. (kill-local-variable 'gdb-define-alist))))))
  3943. (setq gdb-disassembly-position nil)
  3944. (setq overlay-arrow-variable-list
  3945. (delq 'gdb-disassembly-position overlay-arrow-variable-list))
  3946. (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
  3947. (setq gdb-stack-position nil)
  3948. (setq overlay-arrow-variable-list
  3949. (delq 'gdb-stack-position overlay-arrow-variable-list))
  3950. (setq gdb-thread-position nil)
  3951. (setq overlay-arrow-variable-list
  3952. (delq 'gdb-thread-position overlay-arrow-variable-list))
  3953. (if (boundp 'speedbar-frame) (speedbar-timer-fn))
  3954. (setq gud-running nil)
  3955. (setq gdb-active-process nil)
  3956. (remove-hook 'after-save-hook 'gdb-create-define-alist t))
  3957. (defun gdb-get-source-file ()
  3958. "Find the source file where the program starts and display it with related
  3959. buffers, if required."
  3960. (goto-char (point-min))
  3961. (if (re-search-forward gdb-source-file-regexp nil t)
  3962. (setq gdb-main-file (read (match-string 1))))
  3963. (if gdb-many-windows
  3964. (gdb-setup-windows)
  3965. (gdb-get-buffer-create 'gdb-breakpoints-buffer)
  3966. (and gdb-show-main
  3967. gdb-main-file
  3968. (display-buffer (gud-find-file gdb-main-file))))
  3969. (gdb-force-mode-line-update
  3970. (propertize "ready" 'face font-lock-variable-name-face)))
  3971. ;;from put-image
  3972. (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
  3973. "Put string PUTSTRING in front of POS in the current buffer.
  3974. PUTSTRING is displayed by putting an overlay into the current buffer with a
  3975. `before-string' string that has a `display' property whose value is
  3976. PUTSTRING."
  3977. (let ((string (make-string 1 ?x))
  3978. (buffer (current-buffer)))
  3979. (setq putstring (copy-sequence putstring))
  3980. (let ((overlay (make-overlay pos pos buffer))
  3981. (prop (or dprop
  3982. (list (list 'margin 'left-margin) putstring))))
  3983. (put-text-property 0 1 'display prop string)
  3984. (if sprops
  3985. (add-text-properties 0 1 sprops string))
  3986. (overlay-put overlay 'put-break t)
  3987. (overlay-put overlay 'before-string string))))
  3988. ;;from remove-images
  3989. (defun gdb-remove-strings (start end &optional buffer)
  3990. "Remove strings between START and END in BUFFER.
  3991. Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
  3992. BUFFER nil or omitted means use the current buffer."
  3993. (unless buffer
  3994. (setq buffer (current-buffer)))
  3995. (dolist (overlay (overlays-in start end))
  3996. (when (overlay-get overlay 'put-break)
  3997. (delete-overlay overlay))))
  3998. (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
  3999. (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
  4000. (start (- (car posns) 1))
  4001. (end (+ (cdr posns) 1))
  4002. (putstring (if enabled "B" "b"))
  4003. (source-window (get-buffer-window (current-buffer) 0)))
  4004. (add-text-properties
  4005. 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
  4006. putstring)
  4007. (if enabled
  4008. (add-text-properties
  4009. 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
  4010. (add-text-properties
  4011. 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
  4012. (gdb-remove-breakpoint-icons start end)
  4013. (if (display-images-p)
  4014. (if (>= (or left-fringe-width
  4015. (if source-window (car (window-fringes source-window)))
  4016. gdb-buffer-fringe-width) 8)
  4017. (gdb-put-string
  4018. nil (1+ start)
  4019. `(left-fringe breakpoint
  4020. ,(if enabled
  4021. 'breakpoint-enabled
  4022. 'breakpoint-disabled))
  4023. 'gdb-bptno bptno
  4024. 'gdb-enabled enabled)
  4025. (when (< left-margin-width 2)
  4026. (save-current-buffer
  4027. (setq left-margin-width 2)
  4028. (if source-window
  4029. (set-window-margins
  4030. source-window
  4031. left-margin-width right-margin-width))))
  4032. (put-image
  4033. (if enabled
  4034. (or breakpoint-enabled-icon
  4035. (setq breakpoint-enabled-icon
  4036. (find-image `((:type xpm :data
  4037. ,breakpoint-xpm-data
  4038. :ascent 100 :pointer hand)
  4039. (:type pbm :data
  4040. ,breakpoint-enabled-pbm-data
  4041. :ascent 100 :pointer hand)))))
  4042. (or breakpoint-disabled-icon
  4043. (setq breakpoint-disabled-icon
  4044. (find-image `((:type xpm :data
  4045. ,breakpoint-xpm-data
  4046. :conversion disabled
  4047. :ascent 100 :pointer hand)
  4048. (:type pbm :data
  4049. ,breakpoint-disabled-pbm-data
  4050. :ascent 100 :pointer hand))))))
  4051. (+ start 1)
  4052. putstring
  4053. 'left-margin))
  4054. (when (< left-margin-width 2)
  4055. (save-current-buffer
  4056. (setq left-margin-width 2)
  4057. (let ((window (get-buffer-window (current-buffer) 0)))
  4058. (if window
  4059. (set-window-margins
  4060. window left-margin-width right-margin-width)))))
  4061. (gdb-put-string
  4062. (propertize putstring
  4063. 'face (if enabled
  4064. 'breakpoint-enabled 'breakpoint-disabled))
  4065. (1+ start)))))
  4066. (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
  4067. (gdb-remove-strings start end)
  4068. (if (display-images-p)
  4069. (remove-images start end))
  4070. (when remove-margin
  4071. (setq left-margin-width 0)
  4072. (let ((window (get-buffer-window (current-buffer) 0)))
  4073. (if window
  4074. (set-window-margins
  4075. window left-margin-width right-margin-width)))))
  4076. ;;; Functions for inline completion.
  4077. (defvar gud-gdb-fetch-lines-in-progress)
  4078. (defvar gud-gdb-fetch-lines-string)
  4079. (defvar gud-gdb-fetch-lines-break)
  4080. (defvar gud-gdb-fetched-lines)
  4081. (defun gud-gdbmi-completions (context command)
  4082. "Completion table for GDB/MI commands.
  4083. COMMAND is the prefix for which we seek completion.
  4084. CONTEXT is the text before COMMAND on the line."
  4085. (let ((gud-gdb-fetch-lines-in-progress t)
  4086. (gud-gdb-fetch-lines-string nil)
  4087. (gud-gdb-fetch-lines-break (length context))
  4088. (gud-gdb-fetched-lines nil)
  4089. ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
  4090. (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
  4091. (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
  4092. (gdb-input (concat "complete " context command)
  4093. (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
  4094. (while gud-gdb-fetch-lines-in-progress
  4095. (accept-process-output (get-buffer-process gud-comint-buffer))))
  4096. (gud-gdb-completions-1 gud-gdb-fetched-lines)))
  4097. (defun gud-gdbmi-fetch-lines-filter (string)
  4098. "Custom filter function for `gud-gdbmi-completions'."
  4099. (setq string (concat gud-gdb-fetch-lines-string
  4100. (gud-gdbmi-marker-filter string)))
  4101. (while (string-match "\n" string)
  4102. (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
  4103. gud-gdb-fetched-lines)
  4104. (setq string (substring string (match-end 0))))
  4105. "")
  4106. (provide 'gdb-mi)
  4107. ;;; gdb-mi.el ends here