gdb-mi.el 161 KB

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