linespec.c 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423
  1. /* Parser for linespec for the GNU debugger, GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "frame.h"
  17. #include "command.h"
  18. #include "symfile.h"
  19. #include "objfiles.h"
  20. #include "source.h"
  21. #include "demangle.h"
  22. #include "value.h"
  23. #include "completer.h"
  24. #include "cp-abi.h"
  25. #include "cp-support.h"
  26. #include "parser-defs.h"
  27. #include "block.h"
  28. #include "objc-lang.h"
  29. #include "linespec.h"
  30. #include "language.h"
  31. #include "interps.h"
  32. #include "mi/mi-cmds.h"
  33. #include "target.h"
  34. #include "arch-utils.h"
  35. #include <ctype.h>
  36. #include "cli/cli-utils.h"
  37. #include "filenames.h"
  38. #include "ada-lang.h"
  39. #include "stack.h"
  40. #include "location.h"
  41. #include "gdbsupport/function-view.h"
  42. #include "gdbsupport/def-vector.h"
  43. #include <algorithm>
  44. #include "inferior.h"
  45. /* An enumeration of the various things a user might attempt to
  46. complete for a linespec location. */
  47. enum class linespec_complete_what
  48. {
  49. /* Nothing, no possible completion. */
  50. NOTHING,
  51. /* A function/method name. Due to ambiguity between
  52. (gdb) b source[TAB]
  53. source_file.c
  54. source_function
  55. this can also indicate a source filename, iff we haven't seen a
  56. separate source filename component, as in "b source.c:function". */
  57. FUNCTION,
  58. /* A label symbol. E.g., break file.c:function:LABEL. */
  59. LABEL,
  60. /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
  61. EXPRESSION,
  62. /* A linespec keyword ("if"/"thread"/"task"/"-force-condition").
  63. E.g., "break func threa<tab>". */
  64. KEYWORD,
  65. };
  66. /* An address entry is used to ensure that any given location is only
  67. added to the result a single time. It holds an address and the
  68. program space from which the address came. */
  69. struct address_entry
  70. {
  71. struct program_space *pspace;
  72. CORE_ADDR addr;
  73. };
  74. /* A linespec. Elements of this structure are filled in by a parser
  75. (either parse_linespec or some other function). The structure is
  76. then converted into SALs by convert_linespec_to_sals. */
  77. struct linespec
  78. {
  79. /* An explicit location describing the SaLs. */
  80. struct explicit_location explicit_loc {};
  81. /* The list of symtabs to search to which to limit the search.
  82. If explicit.SOURCE_FILENAME is NULL (no user-specified filename),
  83. FILE_SYMTABS should contain one single NULL member. This will cause the
  84. code to use the default symtab. */
  85. std::vector<symtab *> file_symtabs;
  86. /* A list of matching function symbols and minimal symbols. Both lists
  87. may be empty if no matching symbols were found. */
  88. std::vector<block_symbol> function_symbols;
  89. std::vector<bound_minimal_symbol> minimal_symbols;
  90. /* A structure of matching label symbols and the corresponding
  91. function symbol in which the label was found. Both may be empty
  92. or both must be non-empty. */
  93. struct
  94. {
  95. std::vector<block_symbol> label_symbols;
  96. std::vector<block_symbol> function_symbols;
  97. } labels;
  98. };
  99. /* A canonical linespec represented as a symtab-related string.
  100. Each entry represents the "SYMTAB:SUFFIX" linespec string.
  101. SYMTAB can be converted for example by symtab_to_fullname or
  102. symtab_to_filename_for_display as needed. */
  103. struct linespec_canonical_name
  104. {
  105. /* Remaining text part of the linespec string. */
  106. char *suffix;
  107. /* If NULL then SUFFIX is the whole linespec string. */
  108. struct symtab *symtab;
  109. };
  110. /* An instance of this is used to keep all state while linespec
  111. operates. This instance is passed around as a 'this' pointer to
  112. the various implementation methods. */
  113. struct linespec_state
  114. {
  115. /* The language in use during linespec processing. */
  116. const struct language_defn *language;
  117. /* The program space as seen when the module was entered. */
  118. struct program_space *program_space;
  119. /* If not NULL, the search is restricted to just this program
  120. space. */
  121. struct program_space *search_pspace;
  122. /* The default symtab to use, if no other symtab is specified. */
  123. struct symtab *default_symtab;
  124. /* The default line to use. */
  125. int default_line;
  126. /* The 'funfirstline' value that was passed in to decode_line_1 or
  127. decode_line_full. */
  128. int funfirstline;
  129. /* Nonzero if we are running in 'list' mode; see decode_line_list. */
  130. int list_mode;
  131. /* The 'canonical' value passed to decode_line_full, or NULL. */
  132. struct linespec_result *canonical;
  133. /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
  134. struct linespec_canonical_name *canonical_names;
  135. /* This is a set of address_entry objects which is used to prevent
  136. duplicate symbols from being entered into the result. */
  137. htab_t addr_set;
  138. /* Are we building a linespec? */
  139. int is_linespec;
  140. };
  141. /* This is a helper object that is used when collecting symbols into a
  142. result. */
  143. struct collect_info
  144. {
  145. /* The linespec object in use. */
  146. struct linespec_state *state;
  147. /* A list of symtabs to which to restrict matches. */
  148. const std::vector<symtab *> *file_symtabs;
  149. /* The result being accumulated. */
  150. struct
  151. {
  152. std::vector<block_symbol> *symbols;
  153. std::vector<bound_minimal_symbol> *minimal_symbols;
  154. } result;
  155. /* Possibly add a symbol to the results. */
  156. virtual bool add_symbol (block_symbol *bsym);
  157. };
  158. bool
  159. collect_info::add_symbol (block_symbol *bsym)
  160. {
  161. /* In list mode, add all matching symbols, regardless of class.
  162. This allows the user to type "list a_global_variable". */
  163. if (bsym->symbol->aclass () == LOC_BLOCK || this->state->list_mode)
  164. this->result.symbols->push_back (*bsym);
  165. /* Continue iterating. */
  166. return true;
  167. }
  168. /* Custom collect_info for symbol_searcher. */
  169. struct symbol_searcher_collect_info
  170. : collect_info
  171. {
  172. bool add_symbol (block_symbol *bsym) override
  173. {
  174. /* Add everything. */
  175. this->result.symbols->push_back (*bsym);
  176. /* Continue iterating. */
  177. return true;
  178. }
  179. };
  180. /* Token types */
  181. enum ls_token_type
  182. {
  183. /* A keyword */
  184. LSTOKEN_KEYWORD = 0,
  185. /* A colon "separator" */
  186. LSTOKEN_COLON,
  187. /* A string */
  188. LSTOKEN_STRING,
  189. /* A number */
  190. LSTOKEN_NUMBER,
  191. /* A comma */
  192. LSTOKEN_COMMA,
  193. /* EOI (end of input) */
  194. LSTOKEN_EOI,
  195. /* Consumed token */
  196. LSTOKEN_CONSUMED
  197. };
  198. typedef enum ls_token_type linespec_token_type;
  199. /* List of keywords. This is NULL-terminated so that it can be used
  200. as enum completer. */
  201. const char * const linespec_keywords[] = { "if", "thread", "task", "-force-condition", NULL };
  202. #define IF_KEYWORD_INDEX 0
  203. #define FORCE_KEYWORD_INDEX 3
  204. /* A token of the linespec lexer */
  205. struct linespec_token
  206. {
  207. /* The type of the token */
  208. linespec_token_type type;
  209. /* Data for the token */
  210. union
  211. {
  212. /* A string, given as a stoken */
  213. struct stoken string;
  214. /* A keyword */
  215. const char *keyword;
  216. } data;
  217. };
  218. #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
  219. #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
  220. /* An instance of the linespec parser. */
  221. struct linespec_parser
  222. {
  223. linespec_parser (int flags, const struct language_defn *language,
  224. struct program_space *search_pspace,
  225. struct symtab *default_symtab,
  226. int default_line,
  227. struct linespec_result *canonical);
  228. ~linespec_parser ();
  229. DISABLE_COPY_AND_ASSIGN (linespec_parser);
  230. /* Lexer internal data */
  231. struct
  232. {
  233. /* Save head of input stream. */
  234. const char *saved_arg;
  235. /* Head of the input stream. */
  236. const char *stream;
  237. #define PARSER_STREAM(P) ((P)->lexer.stream)
  238. /* The current token. */
  239. linespec_token current;
  240. } lexer {};
  241. /* Is the entire linespec quote-enclosed? */
  242. int is_quote_enclosed = 0;
  243. /* The state of the parse. */
  244. struct linespec_state state {};
  245. #define PARSER_STATE(PPTR) (&(PPTR)->state)
  246. /* The result of the parse. */
  247. linespec result;
  248. #define PARSER_RESULT(PPTR) (&(PPTR)->result)
  249. /* What the parser believes the current word point should complete
  250. to. */
  251. linespec_complete_what complete_what = linespec_complete_what::NOTHING;
  252. /* The completion word point. The parser advances this as it skips
  253. tokens. At some point the input string will end or parsing will
  254. fail, and then we attempt completion at the captured completion
  255. word point, interpreting the string at completion_word as
  256. COMPLETE_WHAT. */
  257. const char *completion_word = nullptr;
  258. /* If the current token was a quoted string, then this is the
  259. quoting character (either " or '). */
  260. int completion_quote_char = 0;
  261. /* If the current token was a quoted string, then this points at the
  262. end of the quoted string. */
  263. const char *completion_quote_end = nullptr;
  264. /* If parsing for completion, then this points at the completion
  265. tracker. Otherwise, this is NULL. */
  266. struct completion_tracker *completion_tracker = nullptr;
  267. };
  268. /* A convenience macro for accessing the explicit location result of
  269. the parser. */
  270. #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
  271. /* Prototypes for local functions. */
  272. static void iterate_over_file_blocks
  273. (struct symtab *symtab, const lookup_name_info &name,
  274. domain_enum domain,
  275. gdb::function_view<symbol_found_callback_ftype> callback);
  276. static void initialize_defaults (struct symtab **default_symtab,
  277. int *default_line);
  278. CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
  279. static std::vector<symtab_and_line> decode_objc (struct linespec_state *self,
  280. linespec *ls,
  281. const char *arg);
  282. static std::vector<symtab *> symtabs_from_filename
  283. (const char *, struct program_space *pspace);
  284. static std::vector<block_symbol> find_label_symbols
  285. (struct linespec_state *self,
  286. const std::vector<block_symbol> &function_symbols,
  287. std::vector<block_symbol> *label_funcs_ret,
  288. const char *name, bool completion_mode = false);
  289. static void find_linespec_symbols (struct linespec_state *self,
  290. const std::vector<symtab *> &file_symtabs,
  291. const char *name,
  292. symbol_name_match_type name_match_type,
  293. std::vector<block_symbol> *symbols,
  294. std::vector<bound_minimal_symbol> *minsyms);
  295. static struct line_offset
  296. linespec_parse_variable (struct linespec_state *self,
  297. const char *variable);
  298. static int symbol_to_sal (struct symtab_and_line *result,
  299. int funfirstline, struct symbol *sym);
  300. static void add_matching_symbols_to_info (const char *name,
  301. symbol_name_match_type name_match_type,
  302. enum search_domain search_domain,
  303. struct collect_info *info,
  304. struct program_space *pspace);
  305. static void add_all_symbol_names_from_pspace
  306. (struct collect_info *info, struct program_space *pspace,
  307. const std::vector<const char *> &names, enum search_domain search_domain);
  308. static std::vector<symtab *>
  309. collect_symtabs_from_filename (const char *file,
  310. struct program_space *pspace);
  311. static std::vector<symtab_and_line> decode_digits_ordinary
  312. (struct linespec_state *self,
  313. linespec *ls,
  314. int line,
  315. linetable_entry **best_entry);
  316. static std::vector<symtab_and_line> decode_digits_list_mode
  317. (struct linespec_state *self,
  318. linespec *ls,
  319. struct symtab_and_line val);
  320. static void minsym_found (struct linespec_state *self, struct objfile *objfile,
  321. struct minimal_symbol *msymbol,
  322. std::vector<symtab_and_line> *result);
  323. static bool compare_symbols (const block_symbol &a, const block_symbol &b);
  324. static bool compare_msymbols (const bound_minimal_symbol &a,
  325. const bound_minimal_symbol &b);
  326. /* Permitted quote characters for the parser. This is different from the
  327. completer's quote characters to allow backward compatibility with the
  328. previous parser. */
  329. static const char linespec_quote_characters[] = "\"\'";
  330. /* Lexer functions. */
  331. /* Lex a number from the input in PARSER. This only supports
  332. decimal numbers.
  333. Return true if input is decimal numbers. Return false if not. */
  334. static int
  335. linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
  336. {
  337. tokenp->type = LSTOKEN_NUMBER;
  338. LS_TOKEN_STOKEN (*tokenp).length = 0;
  339. LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
  340. /* Keep any sign at the start of the stream. */
  341. if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
  342. {
  343. ++LS_TOKEN_STOKEN (*tokenp).length;
  344. ++(PARSER_STREAM (parser));
  345. }
  346. while (isdigit (*PARSER_STREAM (parser)))
  347. {
  348. ++LS_TOKEN_STOKEN (*tokenp).length;
  349. ++(PARSER_STREAM (parser));
  350. }
  351. /* If the next character in the input buffer is not a space, comma,
  352. quote, or colon, this input does not represent a number. */
  353. if (*PARSER_STREAM (parser) != '\0'
  354. && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
  355. && *PARSER_STREAM (parser) != ':'
  356. && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
  357. {
  358. PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
  359. return 0;
  360. }
  361. return 1;
  362. }
  363. /* See linespec.h. */
  364. const char *
  365. linespec_lexer_lex_keyword (const char *p)
  366. {
  367. int i;
  368. if (p != NULL)
  369. {
  370. for (i = 0; linespec_keywords[i] != NULL; ++i)
  371. {
  372. int len = strlen (linespec_keywords[i]);
  373. /* If P begins with
  374. - "thread" or "task" and the next character is
  375. whitespace, we may have found a keyword. It is only a
  376. keyword if it is not followed by another keyword.
  377. - "-force-condition", the next character may be EOF
  378. since this keyword does not take any arguments. Otherwise,
  379. it should be followed by a keyword.
  380. - "if", ALWAYS stop the lexer, since it is not possible to
  381. predict what is going to appear in the condition, which can
  382. only be parsed after SaLs have been found. */
  383. if (strncmp (p, linespec_keywords[i], len) == 0)
  384. {
  385. int j;
  386. if (i == FORCE_KEYWORD_INDEX && p[len] == '\0')
  387. return linespec_keywords[i];
  388. if (!isspace (p[len]))
  389. continue;
  390. if (i == FORCE_KEYWORD_INDEX)
  391. {
  392. p += len;
  393. p = skip_spaces (p);
  394. for (j = 0; linespec_keywords[j] != NULL; ++j)
  395. {
  396. int nextlen = strlen (linespec_keywords[j]);
  397. if (strncmp (p, linespec_keywords[j], nextlen) == 0
  398. && isspace (p[nextlen]))
  399. return linespec_keywords[i];
  400. }
  401. }
  402. else if (i != IF_KEYWORD_INDEX)
  403. {
  404. /* We matched a "thread" or "task". */
  405. p += len;
  406. p = skip_spaces (p);
  407. for (j = 0; linespec_keywords[j] != NULL; ++j)
  408. {
  409. int nextlen = strlen (linespec_keywords[j]);
  410. if (strncmp (p, linespec_keywords[j], nextlen) == 0
  411. && isspace (p[nextlen]))
  412. return NULL;
  413. }
  414. }
  415. return linespec_keywords[i];
  416. }
  417. }
  418. }
  419. return NULL;
  420. }
  421. /* See description in linespec.h. */
  422. int
  423. is_ada_operator (const char *string)
  424. {
  425. const struct ada_opname_map *mapping;
  426. for (mapping = ada_opname_table;
  427. mapping->encoded != NULL
  428. && !startswith (string, mapping->decoded); ++mapping)
  429. ;
  430. return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
  431. }
  432. /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
  433. the location of QUOTE_CHAR, or NULL if not found. */
  434. static const char *
  435. skip_quote_char (const char *string, char quote_char)
  436. {
  437. const char *p, *last;
  438. p = last = find_toplevel_char (string, quote_char);
  439. while (p && *p != '\0' && *p != ':')
  440. {
  441. p = find_toplevel_char (p, quote_char);
  442. if (p != NULL)
  443. last = p++;
  444. }
  445. return last;
  446. }
  447. /* Make a writable copy of the string given in TOKEN, trimming
  448. any trailing whitespace. */
  449. static gdb::unique_xmalloc_ptr<char>
  450. copy_token_string (linespec_token token)
  451. {
  452. const char *str, *s;
  453. if (token.type == LSTOKEN_KEYWORD)
  454. return make_unique_xstrdup (LS_TOKEN_KEYWORD (token));
  455. str = LS_TOKEN_STOKEN (token).ptr;
  456. s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
  457. return gdb::unique_xmalloc_ptr<char> (savestring (str, s - str));
  458. }
  459. /* Does P represent the end of a quote-enclosed linespec? */
  460. static int
  461. is_closing_quote_enclosed (const char *p)
  462. {
  463. if (strchr (linespec_quote_characters, *p))
  464. ++p;
  465. p = skip_spaces ((char *) p);
  466. return (*p == '\0' || linespec_lexer_lex_keyword (p));
  467. }
  468. /* Find the end of the parameter list that starts with *INPUT.
  469. This helper function assists with lexing string segments
  470. which might contain valid (non-terminating) commas. */
  471. static const char *
  472. find_parameter_list_end (const char *input)
  473. {
  474. char end_char, start_char;
  475. int depth;
  476. const char *p;
  477. start_char = *input;
  478. if (start_char == '(')
  479. end_char = ')';
  480. else if (start_char == '<')
  481. end_char = '>';
  482. else
  483. return NULL;
  484. p = input;
  485. depth = 0;
  486. while (*p)
  487. {
  488. if (*p == start_char)
  489. ++depth;
  490. else if (*p == end_char)
  491. {
  492. if (--depth == 0)
  493. {
  494. ++p;
  495. break;
  496. }
  497. }
  498. ++p;
  499. }
  500. return p;
  501. }
  502. /* If the [STRING, STRING_LEN) string ends with what looks like a
  503. keyword, return the keyword start offset in STRING. Return -1
  504. otherwise. */
  505. static size_t
  506. string_find_incomplete_keyword_at_end (const char * const *keywords,
  507. const char *string, size_t string_len)
  508. {
  509. const char *end = string + string_len;
  510. const char *p = end;
  511. while (p > string && *p != ' ')
  512. --p;
  513. if (p > string)
  514. {
  515. p++;
  516. size_t len = end - p;
  517. for (size_t i = 0; keywords[i] != NULL; ++i)
  518. if (strncmp (keywords[i], p, len) == 0)
  519. return p - string;
  520. }
  521. return -1;
  522. }
  523. /* Lex a string from the input in PARSER. */
  524. static linespec_token
  525. linespec_lexer_lex_string (linespec_parser *parser)
  526. {
  527. linespec_token token;
  528. const char *start = PARSER_STREAM (parser);
  529. token.type = LSTOKEN_STRING;
  530. /* If the input stream starts with a quote character, skip to the next
  531. quote character, regardless of the content. */
  532. if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
  533. {
  534. const char *end;
  535. char quote_char = *PARSER_STREAM (parser);
  536. /* Special case: Ada operators. */
  537. if (PARSER_STATE (parser)->language->la_language == language_ada
  538. && quote_char == '\"')
  539. {
  540. int len = is_ada_operator (PARSER_STREAM (parser));
  541. if (len != 0)
  542. {
  543. /* The input is an Ada operator. Return the quoted string
  544. as-is. */
  545. LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
  546. LS_TOKEN_STOKEN (token).length = len;
  547. PARSER_STREAM (parser) += len;
  548. return token;
  549. }
  550. /* The input does not represent an Ada operator -- fall through
  551. to normal quoted string handling. */
  552. }
  553. /* Skip past the beginning quote. */
  554. ++(PARSER_STREAM (parser));
  555. /* Mark the start of the string. */
  556. LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
  557. /* Skip to the ending quote. */
  558. end = skip_quote_char (PARSER_STREAM (parser), quote_char);
  559. /* This helps the completer mode decide whether we have a
  560. complete string. */
  561. parser->completion_quote_char = quote_char;
  562. parser->completion_quote_end = end;
  563. /* Error if the input did not terminate properly, unless in
  564. completion mode. */
  565. if (end == NULL)
  566. {
  567. if (parser->completion_tracker == NULL)
  568. error (_("unmatched quote"));
  569. /* In completion mode, we'll try to complete the incomplete
  570. token. */
  571. token.type = LSTOKEN_STRING;
  572. while (*PARSER_STREAM (parser) != '\0')
  573. PARSER_STREAM (parser)++;
  574. LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 1 - start;
  575. }
  576. else
  577. {
  578. /* Skip over the ending quote and mark the length of the string. */
  579. PARSER_STREAM (parser) = (char *) ++end;
  580. LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
  581. }
  582. }
  583. else
  584. {
  585. const char *p;
  586. /* Otherwise, only identifier characters are permitted.
  587. Spaces are the exception. In general, we keep spaces,
  588. but only if the next characters in the input do not resolve
  589. to one of the keywords.
  590. This allows users to forgo quoting CV-qualifiers, template arguments,
  591. and similar common language constructs. */
  592. while (1)
  593. {
  594. if (isspace (*PARSER_STREAM (parser)))
  595. {
  596. p = skip_spaces (PARSER_STREAM (parser));
  597. /* When we get here we know we've found something followed by
  598. a space (we skip over parens and templates below).
  599. So if we find a keyword now, we know it is a keyword and not,
  600. say, a function name. */
  601. if (linespec_lexer_lex_keyword (p) != NULL)
  602. {
  603. LS_TOKEN_STOKEN (token).ptr = start;
  604. LS_TOKEN_STOKEN (token).length
  605. = PARSER_STREAM (parser) - start;
  606. return token;
  607. }
  608. /* Advance past the whitespace. */
  609. PARSER_STREAM (parser) = p;
  610. }
  611. /* If the next character is EOI or (single) ':', the
  612. string is complete; return the token. */
  613. if (*PARSER_STREAM (parser) == 0)
  614. {
  615. LS_TOKEN_STOKEN (token).ptr = start;
  616. LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
  617. return token;
  618. }
  619. else if (PARSER_STREAM (parser)[0] == ':')
  620. {
  621. /* Do not tokenize the C++ scope operator. */
  622. if (PARSER_STREAM (parser)[1] == ':')
  623. ++(PARSER_STREAM (parser));
  624. /* Do not tokenize ABI tags such as "[abi:cxx11]". */
  625. else if (PARSER_STREAM (parser) - start > 4
  626. && startswith (PARSER_STREAM (parser) - 4, "[abi"))
  627. {
  628. /* Nothing. */
  629. }
  630. /* Do not tokenify if the input length so far is one
  631. (i.e, a single-letter drive name) and the next character
  632. is a directory separator. This allows Windows-style
  633. paths to be recognized as filenames without quoting it. */
  634. else if ((PARSER_STREAM (parser) - start) != 1
  635. || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
  636. {
  637. LS_TOKEN_STOKEN (token).ptr = start;
  638. LS_TOKEN_STOKEN (token).length
  639. = PARSER_STREAM (parser) - start;
  640. return token;
  641. }
  642. }
  643. /* Special case: permit quote-enclosed linespecs. */
  644. else if (parser->is_quote_enclosed
  645. && strchr (linespec_quote_characters,
  646. *PARSER_STREAM (parser))
  647. && is_closing_quote_enclosed (PARSER_STREAM (parser)))
  648. {
  649. LS_TOKEN_STOKEN (token).ptr = start;
  650. LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
  651. return token;
  652. }
  653. /* Because commas may terminate a linespec and appear in
  654. the middle of valid string input, special cases for
  655. '<' and '(' are necessary. */
  656. else if (*PARSER_STREAM (parser) == '<'
  657. || *PARSER_STREAM (parser) == '(')
  658. {
  659. /* Don't interpret 'operator<' / 'operator<<' as a
  660. template parameter list though. */
  661. if (*PARSER_STREAM (parser) == '<'
  662. && (PARSER_STATE (parser)->language->la_language
  663. == language_cplus)
  664. && (PARSER_STREAM (parser) - start) >= CP_OPERATOR_LEN)
  665. {
  666. const char *op = PARSER_STREAM (parser);
  667. while (op > start && isspace (op[-1]))
  668. op--;
  669. if (op - start >= CP_OPERATOR_LEN)
  670. {
  671. op -= CP_OPERATOR_LEN;
  672. if (strncmp (op, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
  673. && (op == start
  674. || !(isalnum (op[-1]) || op[-1] == '_')))
  675. {
  676. /* This is an operator name. Keep going. */
  677. ++(PARSER_STREAM (parser));
  678. if (*PARSER_STREAM (parser) == '<')
  679. ++(PARSER_STREAM (parser));
  680. continue;
  681. }
  682. }
  683. }
  684. const char *end = find_parameter_list_end (PARSER_STREAM (parser));
  685. PARSER_STREAM (parser) = end;
  686. /* Don't loop around to the normal \0 case above because
  687. we don't want to misinterpret a potential keyword at
  688. the end of the token when the string isn't
  689. "()<>"-balanced. This handles "b
  690. function(thread<tab>" in completion mode. */
  691. if (*end == '\0')
  692. {
  693. LS_TOKEN_STOKEN (token).ptr = start;
  694. LS_TOKEN_STOKEN (token).length
  695. = PARSER_STREAM (parser) - start;
  696. return token;
  697. }
  698. else
  699. continue;
  700. }
  701. /* Commas are terminators, but not if they are part of an
  702. operator name. */
  703. else if (*PARSER_STREAM (parser) == ',')
  704. {
  705. if ((PARSER_STATE (parser)->language->la_language
  706. == language_cplus)
  707. && (PARSER_STREAM (parser) - start) > CP_OPERATOR_LEN)
  708. {
  709. const char *op = strstr (start, CP_OPERATOR_STR);
  710. if (op != NULL && is_operator_name (op))
  711. {
  712. /* This is an operator name. Keep going. */
  713. ++(PARSER_STREAM (parser));
  714. continue;
  715. }
  716. }
  717. /* Comma terminates the string. */
  718. LS_TOKEN_STOKEN (token).ptr = start;
  719. LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
  720. return token;
  721. }
  722. /* Advance the stream. */
  723. gdb_assert (*(PARSER_STREAM (parser)) != '\0');
  724. ++(PARSER_STREAM (parser));
  725. }
  726. }
  727. return token;
  728. }
  729. /* Lex a single linespec token from PARSER. */
  730. static linespec_token
  731. linespec_lexer_lex_one (linespec_parser *parser)
  732. {
  733. const char *keyword;
  734. if (parser->lexer.current.type == LSTOKEN_CONSUMED)
  735. {
  736. /* Skip any whitespace. */
  737. PARSER_STREAM (parser) = skip_spaces (PARSER_STREAM (parser));
  738. /* Check for a keyword, they end the linespec. */
  739. keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
  740. if (keyword != NULL)
  741. {
  742. parser->lexer.current.type = LSTOKEN_KEYWORD;
  743. LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
  744. /* We do not advance the stream here intentionally:
  745. we would like lexing to stop when a keyword is seen.
  746. PARSER_STREAM (parser) += strlen (keyword); */
  747. return parser->lexer.current;
  748. }
  749. /* Handle other tokens. */
  750. switch (*PARSER_STREAM (parser))
  751. {
  752. case 0:
  753. parser->lexer.current.type = LSTOKEN_EOI;
  754. break;
  755. case '+': case '-':
  756. case '0': case '1': case '2': case '3': case '4':
  757. case '5': case '6': case '7': case '8': case '9':
  758. if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
  759. parser->lexer.current = linespec_lexer_lex_string (parser);
  760. break;
  761. case ':':
  762. /* If we have a scope operator, lex the input as a string.
  763. Otherwise, return LSTOKEN_COLON. */
  764. if (PARSER_STREAM (parser)[1] == ':')
  765. parser->lexer.current = linespec_lexer_lex_string (parser);
  766. else
  767. {
  768. parser->lexer.current.type = LSTOKEN_COLON;
  769. ++(PARSER_STREAM (parser));
  770. }
  771. break;
  772. case '\'': case '\"':
  773. /* Special case: permit quote-enclosed linespecs. */
  774. if (parser->is_quote_enclosed
  775. && is_closing_quote_enclosed (PARSER_STREAM (parser)))
  776. {
  777. ++(PARSER_STREAM (parser));
  778. parser->lexer.current.type = LSTOKEN_EOI;
  779. }
  780. else
  781. parser->lexer.current = linespec_lexer_lex_string (parser);
  782. break;
  783. case ',':
  784. parser->lexer.current.type = LSTOKEN_COMMA;
  785. LS_TOKEN_STOKEN (parser->lexer.current).ptr
  786. = PARSER_STREAM (parser);
  787. LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
  788. ++(PARSER_STREAM (parser));
  789. break;
  790. default:
  791. /* If the input is not a number, it must be a string.
  792. [Keywords were already considered above.] */
  793. parser->lexer.current = linespec_lexer_lex_string (parser);
  794. break;
  795. }
  796. }
  797. return parser->lexer.current;
  798. }
  799. /* Consume the current token and return the next token in PARSER's
  800. input stream. Also advance the completion word for completion
  801. mode. */
  802. static linespec_token
  803. linespec_lexer_consume_token (linespec_parser *parser)
  804. {
  805. gdb_assert (parser->lexer.current.type != LSTOKEN_EOI);
  806. bool advance_word = (parser->lexer.current.type != LSTOKEN_STRING
  807. || *PARSER_STREAM (parser) != '\0');
  808. /* If we're moving past a string to some other token, it must be the
  809. quote was terminated. */
  810. if (parser->completion_quote_char)
  811. {
  812. gdb_assert (parser->lexer.current.type == LSTOKEN_STRING);
  813. /* If the string was the last (non-EOI) token, we're past the
  814. quote, but remember that for later. */
  815. if (*PARSER_STREAM (parser) != '\0')
  816. {
  817. parser->completion_quote_char = '\0';
  818. parser->completion_quote_end = NULL;;
  819. }
  820. }
  821. parser->lexer.current.type = LSTOKEN_CONSUMED;
  822. linespec_lexer_lex_one (parser);
  823. if (parser->lexer.current.type == LSTOKEN_STRING)
  824. {
  825. /* Advance the completion word past a potential initial
  826. quote-char. */
  827. parser->completion_word = LS_TOKEN_STOKEN (parser->lexer.current).ptr;
  828. }
  829. else if (advance_word)
  830. {
  831. /* Advance the completion word past any whitespace. */
  832. parser->completion_word = PARSER_STREAM (parser);
  833. }
  834. return parser->lexer.current;
  835. }
  836. /* Return the next token without consuming the current token. */
  837. static linespec_token
  838. linespec_lexer_peek_token (linespec_parser *parser)
  839. {
  840. linespec_token next;
  841. const char *saved_stream = PARSER_STREAM (parser);
  842. linespec_token saved_token = parser->lexer.current;
  843. int saved_completion_quote_char = parser->completion_quote_char;
  844. const char *saved_completion_quote_end = parser->completion_quote_end;
  845. const char *saved_completion_word = parser->completion_word;
  846. next = linespec_lexer_consume_token (parser);
  847. PARSER_STREAM (parser) = saved_stream;
  848. parser->lexer.current = saved_token;
  849. parser->completion_quote_char = saved_completion_quote_char;
  850. parser->completion_quote_end = saved_completion_quote_end;
  851. parser->completion_word = saved_completion_word;
  852. return next;
  853. }
  854. /* Helper functions. */
  855. /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
  856. the new sal, if needed. If not NULL, SYMNAME is the name of the
  857. symbol to use when constructing the new canonical name.
  858. If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
  859. canonical name for the SAL. */
  860. static void
  861. add_sal_to_sals (struct linespec_state *self,
  862. std::vector<symtab_and_line> *sals,
  863. struct symtab_and_line *sal,
  864. const char *symname, int literal_canonical)
  865. {
  866. sals->push_back (*sal);
  867. if (self->canonical)
  868. {
  869. struct linespec_canonical_name *canonical;
  870. self->canonical_names = XRESIZEVEC (struct linespec_canonical_name,
  871. self->canonical_names,
  872. sals->size ());
  873. canonical = &self->canonical_names[sals->size () - 1];
  874. if (!literal_canonical && sal->symtab)
  875. {
  876. symtab_to_fullname (sal->symtab);
  877. /* Note that the filter doesn't have to be a valid linespec
  878. input. We only apply the ":LINE" treatment to Ada for
  879. the time being. */
  880. if (symname != NULL && sal->line != 0
  881. && self->language->la_language == language_ada)
  882. canonical->suffix = xstrprintf ("%s:%d", symname,
  883. sal->line).release ();
  884. else if (symname != NULL)
  885. canonical->suffix = xstrdup (symname);
  886. else
  887. canonical->suffix = xstrprintf ("%d", sal->line).release ();
  888. canonical->symtab = sal->symtab;
  889. }
  890. else
  891. {
  892. if (symname != NULL)
  893. canonical->suffix = xstrdup (symname);
  894. else
  895. canonical->suffix = xstrdup ("<unknown>");
  896. canonical->symtab = NULL;
  897. }
  898. }
  899. }
  900. /* A hash function for address_entry. */
  901. static hashval_t
  902. hash_address_entry (const void *p)
  903. {
  904. const struct address_entry *aep = (const struct address_entry *) p;
  905. hashval_t hash;
  906. hash = iterative_hash_object (aep->pspace, 0);
  907. return iterative_hash_object (aep->addr, hash);
  908. }
  909. /* An equality function for address_entry. */
  910. static int
  911. eq_address_entry (const void *a, const void *b)
  912. {
  913. const struct address_entry *aea = (const struct address_entry *) a;
  914. const struct address_entry *aeb = (const struct address_entry *) b;
  915. return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
  916. }
  917. /* Check whether the address, represented by PSPACE and ADDR, is
  918. already in the set. If so, return 0. Otherwise, add it and return
  919. 1. */
  920. static int
  921. maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
  922. {
  923. struct address_entry e, *p;
  924. void **slot;
  925. e.pspace = pspace;
  926. e.addr = addr;
  927. slot = htab_find_slot (set, &e, INSERT);
  928. if (*slot)
  929. return 0;
  930. p = XNEW (struct address_entry);
  931. memcpy (p, &e, sizeof (struct address_entry));
  932. *slot = p;
  933. return 1;
  934. }
  935. /* A helper that walks over all matching symtabs in all objfiles and
  936. calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
  937. not NULL, then the search is restricted to just that program
  938. space. If INCLUDE_INLINE is true then symbols representing
  939. inlined instances of functions will be included in the result. */
  940. static void
  941. iterate_over_all_matching_symtabs
  942. (struct linespec_state *state,
  943. const lookup_name_info &lookup_name,
  944. const domain_enum name_domain,
  945. enum search_domain search_domain,
  946. struct program_space *search_pspace, bool include_inline,
  947. gdb::function_view<symbol_found_callback_ftype> callback)
  948. {
  949. for (struct program_space *pspace : program_spaces)
  950. {
  951. if (search_pspace != NULL && search_pspace != pspace)
  952. continue;
  953. if (pspace->executing_startup)
  954. continue;
  955. set_current_program_space (pspace);
  956. for (objfile *objfile : current_program_space->objfiles ())
  957. {
  958. objfile->expand_symtabs_matching (NULL, &lookup_name, NULL, NULL,
  959. (SEARCH_GLOBAL_BLOCK
  960. | SEARCH_STATIC_BLOCK),
  961. UNDEF_DOMAIN,
  962. search_domain);
  963. for (compunit_symtab *cu : objfile->compunits ())
  964. {
  965. struct symtab *symtab = cu->primary_filetab ();
  966. iterate_over_file_blocks (symtab, lookup_name, name_domain,
  967. callback);
  968. if (include_inline)
  969. {
  970. const struct block *block;
  971. int i;
  972. const blockvector *bv = symtab->compunit ()->blockvector ();
  973. for (i = FIRST_LOCAL_BLOCK; i < BLOCKVECTOR_NBLOCKS (bv); i++)
  974. {
  975. block = BLOCKVECTOR_BLOCK (bv, i);
  976. state->language->iterate_over_symbols
  977. (block, lookup_name, name_domain,
  978. [&] (block_symbol *bsym)
  979. {
  980. /* Restrict calls to CALLBACK to symbols
  981. representing inline symbols only. */
  982. if (bsym->symbol->is_inlined ())
  983. return callback (bsym);
  984. return true;
  985. });
  986. }
  987. }
  988. }
  989. }
  990. }
  991. }
  992. /* Returns the block to be used for symbol searches from
  993. the current location. */
  994. static const struct block *
  995. get_current_search_block (void)
  996. {
  997. /* get_selected_block can change the current language when there is
  998. no selected frame yet. */
  999. scoped_restore_current_language save_language;
  1000. return get_selected_block (0);
  1001. }
  1002. /* Iterate over static and global blocks. */
  1003. static void
  1004. iterate_over_file_blocks
  1005. (struct symtab *symtab, const lookup_name_info &name,
  1006. domain_enum domain, gdb::function_view<symbol_found_callback_ftype> callback)
  1007. {
  1008. const struct block *block;
  1009. for (block = BLOCKVECTOR_BLOCK (symtab->compunit ()->blockvector (),
  1010. STATIC_BLOCK);
  1011. block != NULL;
  1012. block = BLOCK_SUPERBLOCK (block))
  1013. current_language->iterate_over_symbols (block, name, domain, callback);
  1014. }
  1015. /* A helper for find_method. This finds all methods in type T of
  1016. language T_LANG which match NAME. It adds matching symbol names to
  1017. RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
  1018. static void
  1019. find_methods (struct type *t, enum language t_lang, const char *name,
  1020. std::vector<const char *> *result_names,
  1021. std::vector<struct type *> *superclasses)
  1022. {
  1023. int ibase;
  1024. const char *class_name = t->name ();
  1025. /* Ignore this class if it doesn't have a name. This is ugly, but
  1026. unless we figure out how to get the physname without the name of
  1027. the class, then the loop can't do any good. */
  1028. if (class_name)
  1029. {
  1030. int method_counter;
  1031. lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
  1032. symbol_name_matcher_ftype *symbol_name_compare
  1033. = language_def (t_lang)->get_symbol_name_matcher (lookup_name);
  1034. t = check_typedef (t);
  1035. /* Loop over each method name. At this level, all overloads of a name
  1036. are counted as a single name. There is an inner loop which loops over
  1037. each overload. */
  1038. for (method_counter = TYPE_NFN_FIELDS (t) - 1;
  1039. method_counter >= 0;
  1040. --method_counter)
  1041. {
  1042. const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
  1043. if (symbol_name_compare (method_name, lookup_name, NULL))
  1044. {
  1045. int field_counter;
  1046. for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
  1047. - 1);
  1048. field_counter >= 0;
  1049. --field_counter)
  1050. {
  1051. struct fn_field *f;
  1052. const char *phys_name;
  1053. f = TYPE_FN_FIELDLIST1 (t, method_counter);
  1054. if (TYPE_FN_FIELD_STUB (f, field_counter))
  1055. continue;
  1056. phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
  1057. result_names->push_back (phys_name);
  1058. }
  1059. }
  1060. }
  1061. }
  1062. for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
  1063. superclasses->push_back (TYPE_BASECLASS (t, ibase));
  1064. }
  1065. /* The string equivalent of find_toplevel_char. Returns a pointer
  1066. to the location of NEEDLE in HAYSTACK, ignoring any occurrences
  1067. inside "()" and "<>". Returns NULL if NEEDLE was not found. */
  1068. static const char *
  1069. find_toplevel_string (const char *haystack, const char *needle)
  1070. {
  1071. const char *s = haystack;
  1072. do
  1073. {
  1074. s = find_toplevel_char (s, *needle);
  1075. if (s != NULL)
  1076. {
  1077. /* Found first char in HAYSTACK; check rest of string. */
  1078. if (startswith (s, needle))
  1079. return s;
  1080. /* Didn't find it; loop over HAYSTACK, looking for the next
  1081. instance of the first character of NEEDLE. */
  1082. ++s;
  1083. }
  1084. }
  1085. while (s != NULL && *s != '\0');
  1086. /* NEEDLE was not found in HAYSTACK. */
  1087. return NULL;
  1088. }
  1089. /* Convert CANONICAL to its string representation using
  1090. symtab_to_fullname for SYMTAB. */
  1091. static std::string
  1092. canonical_to_fullform (const struct linespec_canonical_name *canonical)
  1093. {
  1094. if (canonical->symtab == NULL)
  1095. return canonical->suffix;
  1096. else
  1097. return string_printf ("%s:%s", symtab_to_fullname (canonical->symtab),
  1098. canonical->suffix);
  1099. }
  1100. /* Given FILTERS, a list of canonical names, filter the sals in RESULT
  1101. and store the result in SELF->CANONICAL. */
  1102. static void
  1103. filter_results (struct linespec_state *self,
  1104. std::vector<symtab_and_line> *result,
  1105. const std::vector<const char *> &filters)
  1106. {
  1107. for (const char *name : filters)
  1108. {
  1109. linespec_sals lsal;
  1110. for (size_t j = 0; j < result->size (); ++j)
  1111. {
  1112. const struct linespec_canonical_name *canonical;
  1113. canonical = &self->canonical_names[j];
  1114. std::string fullform = canonical_to_fullform (canonical);
  1115. if (name == fullform)
  1116. lsal.sals.push_back ((*result)[j]);
  1117. }
  1118. if (!lsal.sals.empty ())
  1119. {
  1120. lsal.canonical = xstrdup (name);
  1121. self->canonical->lsals.push_back (std::move (lsal));
  1122. }
  1123. }
  1124. self->canonical->pre_expanded = 0;
  1125. }
  1126. /* Store RESULT into SELF->CANONICAL. */
  1127. static void
  1128. convert_results_to_lsals (struct linespec_state *self,
  1129. std::vector<symtab_and_line> *result)
  1130. {
  1131. struct linespec_sals lsal;
  1132. lsal.canonical = NULL;
  1133. lsal.sals = std::move (*result);
  1134. self->canonical->lsals.push_back (std::move (lsal));
  1135. }
  1136. /* A structure that contains two string representations of a struct
  1137. linespec_canonical_name:
  1138. - one where the symtab's fullname is used;
  1139. - one where the filename followed the "set filename-display"
  1140. setting. */
  1141. struct decode_line_2_item
  1142. {
  1143. decode_line_2_item (std::string &&fullform_, std::string &&displayform_,
  1144. bool selected_)
  1145. : fullform (std::move (fullform_)),
  1146. displayform (std::move (displayform_)),
  1147. selected (selected_)
  1148. {
  1149. }
  1150. /* The form using symtab_to_fullname. */
  1151. std::string fullform;
  1152. /* The form using symtab_to_filename_for_display. */
  1153. std::string displayform;
  1154. /* Field is initialized to zero and it is set to one if the user
  1155. requested breakpoint for this entry. */
  1156. unsigned int selected : 1;
  1157. };
  1158. /* Helper for std::sort to sort decode_line_2_item entries by
  1159. DISPLAYFORM and secondarily by FULLFORM. */
  1160. static bool
  1161. decode_line_2_compare_items (const decode_line_2_item &a,
  1162. const decode_line_2_item &b)
  1163. {
  1164. if (a.displayform != b.displayform)
  1165. return a.displayform < b.displayform;
  1166. return a.fullform < b.fullform;
  1167. }
  1168. /* Handle multiple results in RESULT depending on SELECT_MODE. This
  1169. will either return normally, throw an exception on multiple
  1170. results, or present a menu to the user. On return, the SALS vector
  1171. in SELF->CANONICAL is set up properly. */
  1172. static void
  1173. decode_line_2 (struct linespec_state *self,
  1174. std::vector<symtab_and_line> *result,
  1175. const char *select_mode)
  1176. {
  1177. const char *args;
  1178. const char *prompt;
  1179. int i;
  1180. std::vector<const char *> filters;
  1181. std::vector<struct decode_line_2_item> items;
  1182. gdb_assert (select_mode != multiple_symbols_all);
  1183. gdb_assert (self->canonical != NULL);
  1184. gdb_assert (!result->empty ());
  1185. /* Prepare ITEMS array. */
  1186. for (i = 0; i < result->size (); ++i)
  1187. {
  1188. const struct linespec_canonical_name *canonical;
  1189. std::string displayform;
  1190. canonical = &self->canonical_names[i];
  1191. gdb_assert (canonical->suffix != NULL);
  1192. std::string fullform = canonical_to_fullform (canonical);
  1193. if (canonical->symtab == NULL)
  1194. displayform = canonical->suffix;
  1195. else
  1196. {
  1197. const char *fn_for_display;
  1198. fn_for_display = symtab_to_filename_for_display (canonical->symtab);
  1199. displayform = string_printf ("%s:%s", fn_for_display,
  1200. canonical->suffix);
  1201. }
  1202. items.emplace_back (std::move (fullform), std::move (displayform),
  1203. false);
  1204. }
  1205. /* Sort the list of method names. */
  1206. std::sort (items.begin (), items.end (), decode_line_2_compare_items);
  1207. /* Remove entries with the same FULLFORM. */
  1208. items.erase (std::unique (items.begin (), items.end (),
  1209. [] (const struct decode_line_2_item &a,
  1210. const struct decode_line_2_item &b)
  1211. {
  1212. return a.fullform == b.fullform;
  1213. }),
  1214. items.end ());
  1215. if (select_mode == multiple_symbols_cancel && items.size () > 1)
  1216. error (_("canceled because the command is ambiguous\n"
  1217. "See set/show multiple-symbol."));
  1218. if (select_mode == multiple_symbols_all || items.size () == 1)
  1219. {
  1220. convert_results_to_lsals (self, result);
  1221. return;
  1222. }
  1223. printf_unfiltered (_("[0] cancel\n[1] all\n"));
  1224. for (i = 0; i < items.size (); i++)
  1225. printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform.c_str ());
  1226. prompt = getenv ("PS2");
  1227. if (prompt == NULL)
  1228. {
  1229. prompt = "> ";
  1230. }
  1231. args = command_line_input (prompt, "overload-choice");
  1232. if (args == 0 || *args == 0)
  1233. error_no_arg (_("one or more choice numbers"));
  1234. number_or_range_parser parser (args);
  1235. while (!parser.finished ())
  1236. {
  1237. int num = parser.get_number ();
  1238. if (num == 0)
  1239. error (_("canceled"));
  1240. else if (num == 1)
  1241. {
  1242. /* We intentionally make this result in a single breakpoint,
  1243. contrary to what older versions of gdb did. The
  1244. rationale is that this lets a user get the
  1245. multiple_symbols_all behavior even with the 'ask'
  1246. setting; and he can get separate breakpoints by entering
  1247. "2-57" at the query. */
  1248. convert_results_to_lsals (self, result);
  1249. return;
  1250. }
  1251. num -= 2;
  1252. if (num >= items.size ())
  1253. printf_unfiltered (_("No choice number %d.\n"), num);
  1254. else
  1255. {
  1256. struct decode_line_2_item *item = &items[num];
  1257. if (!item->selected)
  1258. {
  1259. filters.push_back (item->fullform.c_str ());
  1260. item->selected = 1;
  1261. }
  1262. else
  1263. {
  1264. printf_unfiltered (_("duplicate request for %d ignored.\n"),
  1265. num + 2);
  1266. }
  1267. }
  1268. }
  1269. filter_results (self, result, filters);
  1270. }
  1271. /* The parser of linespec itself. */
  1272. /* Throw an appropriate error when SYMBOL is not found (optionally in
  1273. FILENAME). */
  1274. static void ATTRIBUTE_NORETURN
  1275. symbol_not_found_error (const char *symbol, const char *filename)
  1276. {
  1277. if (symbol == NULL)
  1278. symbol = "";
  1279. if (!have_full_symbols ()
  1280. && !have_partial_symbols ()
  1281. && !have_minimal_symbols ())
  1282. throw_error (NOT_FOUND_ERROR,
  1283. _("No symbol table is loaded. Use the \"file\" command."));
  1284. /* If SYMBOL starts with '$', the user attempted to either lookup
  1285. a function/variable in his code starting with '$' or an internal
  1286. variable of that name. Since we do not know which, be concise and
  1287. explain both possibilities. */
  1288. if (*symbol == '$')
  1289. {
  1290. if (filename)
  1291. throw_error (NOT_FOUND_ERROR,
  1292. _("Undefined convenience variable or function \"%s\" "
  1293. "not defined in \"%s\"."), symbol, filename);
  1294. else
  1295. throw_error (NOT_FOUND_ERROR,
  1296. _("Undefined convenience variable or function \"%s\" "
  1297. "not defined."), symbol);
  1298. }
  1299. else
  1300. {
  1301. if (filename)
  1302. throw_error (NOT_FOUND_ERROR,
  1303. _("Function \"%s\" not defined in \"%s\"."),
  1304. symbol, filename);
  1305. else
  1306. throw_error (NOT_FOUND_ERROR,
  1307. _("Function \"%s\" not defined."), symbol);
  1308. }
  1309. }
  1310. /* Throw an appropriate error when an unexpected token is encountered
  1311. in the input. */
  1312. static void ATTRIBUTE_NORETURN
  1313. unexpected_linespec_error (linespec_parser *parser)
  1314. {
  1315. linespec_token token;
  1316. static const char * token_type_strings[]
  1317. = {"keyword", "colon", "string", "number", "comma", "end of input"};
  1318. /* Get the token that generated the error. */
  1319. token = linespec_lexer_lex_one (parser);
  1320. /* Finally, throw the error. */
  1321. if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
  1322. || token.type == LSTOKEN_KEYWORD)
  1323. {
  1324. gdb::unique_xmalloc_ptr<char> string = copy_token_string (token);
  1325. throw_error (GENERIC_ERROR,
  1326. _("malformed linespec error: unexpected %s, \"%s\""),
  1327. token_type_strings[token.type], string.get ());
  1328. }
  1329. else
  1330. throw_error (GENERIC_ERROR,
  1331. _("malformed linespec error: unexpected %s"),
  1332. token_type_strings[token.type]);
  1333. }
  1334. /* Throw an undefined label error. */
  1335. static void ATTRIBUTE_NORETURN
  1336. undefined_label_error (const char *function, const char *label)
  1337. {
  1338. if (function != NULL)
  1339. throw_error (NOT_FOUND_ERROR,
  1340. _("No label \"%s\" defined in function \"%s\"."),
  1341. label, function);
  1342. else
  1343. throw_error (NOT_FOUND_ERROR,
  1344. _("No label \"%s\" defined in current function."),
  1345. label);
  1346. }
  1347. /* Throw a source file not found error. */
  1348. static void ATTRIBUTE_NORETURN
  1349. source_file_not_found_error (const char *name)
  1350. {
  1351. throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
  1352. }
  1353. /* Unless at EIO, save the current stream position as completion word
  1354. point, and consume the next token. */
  1355. static linespec_token
  1356. save_stream_and_consume_token (linespec_parser *parser)
  1357. {
  1358. if (linespec_lexer_peek_token (parser).type != LSTOKEN_EOI)
  1359. parser->completion_word = PARSER_STREAM (parser);
  1360. return linespec_lexer_consume_token (parser);
  1361. }
  1362. /* See description in linespec.h. */
  1363. struct line_offset
  1364. linespec_parse_line_offset (const char *string)
  1365. {
  1366. const char *start = string;
  1367. struct line_offset line_offset = {0, LINE_OFFSET_NONE};
  1368. if (*string == '+')
  1369. {
  1370. line_offset.sign = LINE_OFFSET_PLUS;
  1371. ++string;
  1372. }
  1373. else if (*string == '-')
  1374. {
  1375. line_offset.sign = LINE_OFFSET_MINUS;
  1376. ++string;
  1377. }
  1378. if (*string != '\0' && !isdigit (*string))
  1379. error (_("malformed line offset: \"%s\""), start);
  1380. /* Right now, we only allow base 10 for offsets. */
  1381. line_offset.offset = atoi (string);
  1382. return line_offset;
  1383. }
  1384. /* In completion mode, if the user is still typing the number, there's
  1385. no possible completion to offer. But if there's already input past
  1386. the number, setup to expect NEXT. */
  1387. static void
  1388. set_completion_after_number (linespec_parser *parser,
  1389. linespec_complete_what next)
  1390. {
  1391. if (*PARSER_STREAM (parser) == ' ')
  1392. {
  1393. parser->completion_word = skip_spaces (PARSER_STREAM (parser) + 1);
  1394. parser->complete_what = next;
  1395. }
  1396. else
  1397. {
  1398. parser->completion_word = PARSER_STREAM (parser);
  1399. parser->complete_what = linespec_complete_what::NOTHING;
  1400. }
  1401. }
  1402. /* Parse the basic_spec in PARSER's input. */
  1403. static void
  1404. linespec_parse_basic (linespec_parser *parser)
  1405. {
  1406. gdb::unique_xmalloc_ptr<char> name;
  1407. linespec_token token;
  1408. /* Get the next token. */
  1409. token = linespec_lexer_lex_one (parser);
  1410. /* If it is EOI or KEYWORD, issue an error. */
  1411. if (token.type == LSTOKEN_KEYWORD)
  1412. {
  1413. parser->complete_what = linespec_complete_what::NOTHING;
  1414. unexpected_linespec_error (parser);
  1415. }
  1416. else if (token.type == LSTOKEN_EOI)
  1417. {
  1418. unexpected_linespec_error (parser);
  1419. }
  1420. /* If it is a LSTOKEN_NUMBER, we have an offset. */
  1421. else if (token.type == LSTOKEN_NUMBER)
  1422. {
  1423. set_completion_after_number (parser, linespec_complete_what::KEYWORD);
  1424. /* Record the line offset and get the next token. */
  1425. name = copy_token_string (token);
  1426. PARSER_EXPLICIT (parser)->line_offset
  1427. = linespec_parse_line_offset (name.get ());
  1428. /* Get the next token. */
  1429. token = linespec_lexer_consume_token (parser);
  1430. /* If the next token is a comma, stop parsing and return. */
  1431. if (token.type == LSTOKEN_COMMA)
  1432. {
  1433. parser->complete_what = linespec_complete_what::NOTHING;
  1434. return;
  1435. }
  1436. /* If the next token is anything but EOI or KEYWORD, issue
  1437. an error. */
  1438. if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
  1439. unexpected_linespec_error (parser);
  1440. }
  1441. if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
  1442. return;
  1443. /* Next token must be LSTOKEN_STRING. */
  1444. if (token.type != LSTOKEN_STRING)
  1445. {
  1446. parser->complete_what = linespec_complete_what::NOTHING;
  1447. unexpected_linespec_error (parser);
  1448. }
  1449. /* The current token will contain the name of a function, method,
  1450. or label. */
  1451. name = copy_token_string (token);
  1452. if (parser->completion_tracker != NULL)
  1453. {
  1454. /* If the function name ends with a ":", then this may be an
  1455. incomplete "::" scope operator instead of a label separator.
  1456. E.g.,
  1457. "b klass:<tab>"
  1458. which should expand to:
  1459. "b klass::method()"
  1460. Do a tentative completion assuming the later. If we find
  1461. completions, advance the stream past the colon token and make
  1462. it part of the function name/token. */
  1463. if (!parser->completion_quote_char
  1464. && strcmp (PARSER_STREAM (parser), ":") == 0)
  1465. {
  1466. completion_tracker tmp_tracker;
  1467. const char *source_filename
  1468. = PARSER_EXPLICIT (parser)->source_filename;
  1469. symbol_name_match_type match_type
  1470. = PARSER_EXPLICIT (parser)->func_name_match_type;
  1471. linespec_complete_function (tmp_tracker,
  1472. parser->completion_word,
  1473. match_type,
  1474. source_filename);
  1475. if (tmp_tracker.have_completions ())
  1476. {
  1477. PARSER_STREAM (parser)++;
  1478. LS_TOKEN_STOKEN (token).length++;
  1479. name.reset (savestring (parser->completion_word,
  1480. (PARSER_STREAM (parser)
  1481. - parser->completion_word)));
  1482. }
  1483. }
  1484. PARSER_EXPLICIT (parser)->function_name = name.release ();
  1485. }
  1486. else
  1487. {
  1488. std::vector<block_symbol> symbols;
  1489. std::vector<bound_minimal_symbol> minimal_symbols;
  1490. /* Try looking it up as a function/method. */
  1491. find_linespec_symbols (PARSER_STATE (parser),
  1492. PARSER_RESULT (parser)->file_symtabs, name.get (),
  1493. PARSER_EXPLICIT (parser)->func_name_match_type,
  1494. &symbols, &minimal_symbols);
  1495. if (!symbols.empty () || !minimal_symbols.empty ())
  1496. {
  1497. PARSER_RESULT (parser)->function_symbols = std::move (symbols);
  1498. PARSER_RESULT (parser)->minimal_symbols = std::move (minimal_symbols);
  1499. PARSER_EXPLICIT (parser)->function_name = name.release ();
  1500. }
  1501. else
  1502. {
  1503. /* NAME was not a function or a method. So it must be a label
  1504. name or user specified variable like "break foo.c:$zippo". */
  1505. std::vector<block_symbol> labels
  1506. = find_label_symbols (PARSER_STATE (parser), {}, &symbols,
  1507. name.get ());
  1508. if (!labels.empty ())
  1509. {
  1510. PARSER_RESULT (parser)->labels.label_symbols = std::move (labels);
  1511. PARSER_RESULT (parser)->labels.function_symbols
  1512. = std::move (symbols);
  1513. PARSER_EXPLICIT (parser)->label_name = name.release ();
  1514. }
  1515. else if (token.type == LSTOKEN_STRING
  1516. && *LS_TOKEN_STOKEN (token).ptr == '$')
  1517. {
  1518. /* User specified a convenience variable or history value. */
  1519. PARSER_EXPLICIT (parser)->line_offset
  1520. = linespec_parse_variable (PARSER_STATE (parser), name.get ());
  1521. if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
  1522. {
  1523. /* The user-specified variable was not valid. Do not
  1524. throw an error here. parse_linespec will do it for us. */
  1525. PARSER_EXPLICIT (parser)->function_name = name.release ();
  1526. return;
  1527. }
  1528. }
  1529. else
  1530. {
  1531. /* The name is also not a label. Abort parsing. Do not throw
  1532. an error here. parse_linespec will do it for us. */
  1533. /* Save a copy of the name we were trying to lookup. */
  1534. PARSER_EXPLICIT (parser)->function_name = name.release ();
  1535. return;
  1536. }
  1537. }
  1538. }
  1539. int previous_qc = parser->completion_quote_char;
  1540. /* Get the next token. */
  1541. token = linespec_lexer_consume_token (parser);
  1542. if (token.type == LSTOKEN_EOI)
  1543. {
  1544. if (previous_qc && !parser->completion_quote_char)
  1545. parser->complete_what = linespec_complete_what::KEYWORD;
  1546. }
  1547. else if (token.type == LSTOKEN_COLON)
  1548. {
  1549. /* User specified a label or a lineno. */
  1550. token = linespec_lexer_consume_token (parser);
  1551. if (token.type == LSTOKEN_NUMBER)
  1552. {
  1553. /* User specified an offset. Record the line offset and
  1554. get the next token. */
  1555. set_completion_after_number (parser, linespec_complete_what::KEYWORD);
  1556. name = copy_token_string (token);
  1557. PARSER_EXPLICIT (parser)->line_offset
  1558. = linespec_parse_line_offset (name.get ());
  1559. /* Get the next token. */
  1560. token = linespec_lexer_consume_token (parser);
  1561. }
  1562. else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
  1563. {
  1564. parser->complete_what = linespec_complete_what::LABEL;
  1565. }
  1566. else if (token.type == LSTOKEN_STRING)
  1567. {
  1568. parser->complete_what = linespec_complete_what::LABEL;
  1569. /* If we have text after the label separated by whitespace
  1570. (e.g., "b func():lab i<tab>"), don't consider it part of
  1571. the label. In completion mode that should complete to
  1572. "if", in normal mode, the 'i' should be treated as
  1573. garbage. */
  1574. if (parser->completion_quote_char == '\0')
  1575. {
  1576. const char *ptr = LS_TOKEN_STOKEN (token).ptr;
  1577. for (size_t i = 0; i < LS_TOKEN_STOKEN (token).length; i++)
  1578. {
  1579. if (ptr[i] == ' ')
  1580. {
  1581. LS_TOKEN_STOKEN (token).length = i;
  1582. PARSER_STREAM (parser) = skip_spaces (ptr + i + 1);
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. if (parser->completion_tracker != NULL)
  1588. {
  1589. if (PARSER_STREAM (parser)[-1] == ' ')
  1590. {
  1591. parser->completion_word = PARSER_STREAM (parser);
  1592. parser->complete_what = linespec_complete_what::KEYWORD;
  1593. }
  1594. }
  1595. else
  1596. {
  1597. std::vector<block_symbol> symbols;
  1598. /* Grab a copy of the label's name and look it up. */
  1599. name = copy_token_string (token);
  1600. std::vector<block_symbol> labels
  1601. = find_label_symbols (PARSER_STATE (parser),
  1602. PARSER_RESULT (parser)->function_symbols,
  1603. &symbols, name.get ());
  1604. if (!labels.empty ())
  1605. {
  1606. PARSER_RESULT (parser)->labels.label_symbols
  1607. = std::move (labels);
  1608. PARSER_RESULT (parser)->labels.function_symbols
  1609. = std::move (symbols);
  1610. PARSER_EXPLICIT (parser)->label_name = name.release ();
  1611. }
  1612. else
  1613. {
  1614. /* We don't know what it was, but it isn't a label. */
  1615. undefined_label_error
  1616. (PARSER_EXPLICIT (parser)->function_name, name.get ());
  1617. }
  1618. }
  1619. /* Check for a line offset. */
  1620. token = save_stream_and_consume_token (parser);
  1621. if (token.type == LSTOKEN_COLON)
  1622. {
  1623. /* Get the next token. */
  1624. token = linespec_lexer_consume_token (parser);
  1625. /* It must be a line offset. */
  1626. if (token.type != LSTOKEN_NUMBER)
  1627. unexpected_linespec_error (parser);
  1628. /* Record the line offset and get the next token. */
  1629. name = copy_token_string (token);
  1630. PARSER_EXPLICIT (parser)->line_offset
  1631. = linespec_parse_line_offset (name.get ());
  1632. /* Get the next token. */
  1633. token = linespec_lexer_consume_token (parser);
  1634. }
  1635. }
  1636. else
  1637. {
  1638. /* Trailing ':' in the input. Issue an error. */
  1639. unexpected_linespec_error (parser);
  1640. }
  1641. }
  1642. }
  1643. /* Canonicalize the linespec contained in LS. The result is saved into
  1644. STATE->canonical. This function handles both linespec and explicit
  1645. locations. */
  1646. static void
  1647. canonicalize_linespec (struct linespec_state *state, const linespec *ls)
  1648. {
  1649. struct event_location *canon;
  1650. struct explicit_location *explicit_loc;
  1651. /* If canonicalization was not requested, no need to do anything. */
  1652. if (!state->canonical)
  1653. return;
  1654. /* Save everything as an explicit location. */
  1655. state->canonical->location
  1656. = new_explicit_location (&ls->explicit_loc);
  1657. canon = state->canonical->location.get ();
  1658. explicit_loc = get_explicit_location (canon);
  1659. if (explicit_loc->label_name != NULL)
  1660. {
  1661. state->canonical->special_display = 1;
  1662. if (explicit_loc->function_name == NULL)
  1663. {
  1664. /* No function was specified, so add the symbol name. */
  1665. gdb_assert (ls->labels.function_symbols.size () == 1);
  1666. block_symbol s = ls->labels.function_symbols.front ();
  1667. explicit_loc->function_name = xstrdup (s.symbol->natural_name ());
  1668. }
  1669. }
  1670. /* If this location originally came from a linespec, save a string
  1671. representation of it for display and saving to file. */
  1672. if (state->is_linespec)
  1673. set_event_location_string (canon,
  1674. explicit_location_to_linespec (explicit_loc));
  1675. }
  1676. /* Given a line offset in LS, construct the relevant SALs. */
  1677. static std::vector<symtab_and_line>
  1678. create_sals_line_offset (struct linespec_state *self,
  1679. linespec *ls)
  1680. {
  1681. int use_default = 0;
  1682. /* This is where we need to make sure we have good defaults.
  1683. We must guarantee that this section of code is never executed
  1684. when we are called with just a function name, since
  1685. set_default_source_symtab_and_line uses
  1686. select_source_symtab that calls us with such an argument. */
  1687. if (ls->file_symtabs.size () == 1
  1688. && ls->file_symtabs.front () == nullptr)
  1689. {
  1690. set_current_program_space (self->program_space);
  1691. /* Make sure we have at least a default source line. */
  1692. set_default_source_symtab_and_line ();
  1693. initialize_defaults (&self->default_symtab, &self->default_line);
  1694. ls->file_symtabs
  1695. = collect_symtabs_from_filename (self->default_symtab->filename,
  1696. self->search_pspace);
  1697. use_default = 1;
  1698. }
  1699. symtab_and_line val;
  1700. val.line = ls->explicit_loc.line_offset.offset;
  1701. switch (ls->explicit_loc.line_offset.sign)
  1702. {
  1703. case LINE_OFFSET_PLUS:
  1704. if (ls->explicit_loc.line_offset.offset == 0)
  1705. val.line = 5;
  1706. if (use_default)
  1707. val.line = self->default_line + val.line;
  1708. break;
  1709. case LINE_OFFSET_MINUS:
  1710. if (ls->explicit_loc.line_offset.offset == 0)
  1711. val.line = 15;
  1712. if (use_default)
  1713. val.line = self->default_line - val.line;
  1714. else
  1715. val.line = -val.line;
  1716. break;
  1717. case LINE_OFFSET_NONE:
  1718. break; /* No need to adjust val.line. */
  1719. }
  1720. std::vector<symtab_and_line> values;
  1721. if (self->list_mode)
  1722. values = decode_digits_list_mode (self, ls, val);
  1723. else
  1724. {
  1725. struct linetable_entry *best_entry = NULL;
  1726. int i, j;
  1727. std::vector<symtab_and_line> intermediate_results
  1728. = decode_digits_ordinary (self, ls, val.line, &best_entry);
  1729. if (intermediate_results.empty () && best_entry != NULL)
  1730. intermediate_results = decode_digits_ordinary (self, ls,
  1731. best_entry->line,
  1732. &best_entry);
  1733. /* For optimized code, the compiler can scatter one source line
  1734. across disjoint ranges of PC values, even when no duplicate
  1735. functions or inline functions are involved. For example,
  1736. 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
  1737. function can result in two PC ranges. In this case, we don't
  1738. want to set a breakpoint on the first PC of each range. To filter
  1739. such cases, we use containing blocks -- for each PC found
  1740. above, we see if there are other PCs that are in the same
  1741. block. If yes, the other PCs are filtered out. */
  1742. gdb::def_vector<int> filter (intermediate_results.size ());
  1743. gdb::def_vector<const block *> blocks (intermediate_results.size ());
  1744. for (i = 0; i < intermediate_results.size (); ++i)
  1745. {
  1746. set_current_program_space (intermediate_results[i].pspace);
  1747. filter[i] = 1;
  1748. blocks[i] = block_for_pc_sect (intermediate_results[i].pc,
  1749. intermediate_results[i].section);
  1750. }
  1751. for (i = 0; i < intermediate_results.size (); ++i)
  1752. {
  1753. if (blocks[i] != NULL)
  1754. for (j = i + 1; j < intermediate_results.size (); ++j)
  1755. {
  1756. if (blocks[j] == blocks[i])
  1757. {
  1758. filter[j] = 0;
  1759. break;
  1760. }
  1761. }
  1762. }
  1763. for (i = 0; i < intermediate_results.size (); ++i)
  1764. if (filter[i])
  1765. {
  1766. struct symbol *sym = (blocks[i]
  1767. ? block_containing_function (blocks[i])
  1768. : NULL);
  1769. if (self->funfirstline)
  1770. skip_prologue_sal (&intermediate_results[i]);
  1771. intermediate_results[i].symbol = sym;
  1772. add_sal_to_sals (self, &values, &intermediate_results[i],
  1773. sym ? sym->natural_name () : NULL, 0);
  1774. }
  1775. }
  1776. if (values.empty ())
  1777. {
  1778. if (ls->explicit_loc.source_filename)
  1779. throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
  1780. val.line, ls->explicit_loc.source_filename);
  1781. else
  1782. throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
  1783. val.line);
  1784. }
  1785. return values;
  1786. }
  1787. /* Convert the given ADDRESS into SaLs. */
  1788. static std::vector<symtab_and_line>
  1789. convert_address_location_to_sals (struct linespec_state *self,
  1790. CORE_ADDR address)
  1791. {
  1792. symtab_and_line sal = find_pc_line (address, 0);
  1793. sal.pc = address;
  1794. sal.section = find_pc_overlay (address);
  1795. sal.explicit_pc = 1;
  1796. sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
  1797. std::vector<symtab_and_line> sals;
  1798. add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
  1799. return sals;
  1800. }
  1801. /* Create and return SALs from the linespec LS. */
  1802. static std::vector<symtab_and_line>
  1803. convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
  1804. {
  1805. std::vector<symtab_and_line> sals;
  1806. if (!ls->labels.label_symbols.empty ())
  1807. {
  1808. /* We have just a bunch of functions/methods or labels. */
  1809. struct symtab_and_line sal;
  1810. for (const auto &sym : ls->labels.label_symbols)
  1811. {
  1812. struct program_space *pspace
  1813. = symbol_symtab (sym.symbol)->compunit ()->objfile ()->pspace;
  1814. if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
  1815. && maybe_add_address (state->addr_set, pspace, sal.pc))
  1816. add_sal_to_sals (state, &sals, &sal,
  1817. sym.symbol->natural_name (), 0);
  1818. }
  1819. }
  1820. else if (!ls->function_symbols.empty () || !ls->minimal_symbols.empty ())
  1821. {
  1822. /* We have just a bunch of functions and/or methods. */
  1823. if (!ls->function_symbols.empty ())
  1824. {
  1825. /* Sort symbols so that symbols with the same program space are next
  1826. to each other. */
  1827. std::sort (ls->function_symbols.begin (),
  1828. ls->function_symbols.end (),
  1829. compare_symbols);
  1830. for (const auto &sym : ls->function_symbols)
  1831. {
  1832. program_space *pspace
  1833. = symbol_symtab (sym.symbol)->compunit ()->objfile ()->pspace;
  1834. set_current_program_space (pspace);
  1835. /* Don't skip to the first line of the function if we
  1836. had found an ifunc minimal symbol for this function,
  1837. because that means that this function is an ifunc
  1838. resolver with the same name as the ifunc itself. */
  1839. bool found_ifunc = false;
  1840. if (state->funfirstline
  1841. && !ls->minimal_symbols.empty ()
  1842. && sym.symbol->aclass () == LOC_BLOCK)
  1843. {
  1844. const CORE_ADDR addr
  1845. = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym.symbol));
  1846. for (const auto &elem : ls->minimal_symbols)
  1847. {
  1848. if (MSYMBOL_TYPE (elem.minsym) == mst_text_gnu_ifunc
  1849. || MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
  1850. {
  1851. CORE_ADDR msym_addr = BMSYMBOL_VALUE_ADDRESS (elem);
  1852. if (MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
  1853. {
  1854. struct gdbarch *gdbarch
  1855. = elem.objfile->arch ();
  1856. msym_addr
  1857. = (gdbarch_convert_from_func_ptr_addr
  1858. (gdbarch,
  1859. msym_addr,
  1860. current_inferior ()->top_target ()));
  1861. }
  1862. if (msym_addr == addr)
  1863. {
  1864. found_ifunc = true;
  1865. break;
  1866. }
  1867. }
  1868. }
  1869. }
  1870. if (!found_ifunc)
  1871. {
  1872. symtab_and_line sal;
  1873. if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
  1874. && maybe_add_address (state->addr_set, pspace, sal.pc))
  1875. add_sal_to_sals (state, &sals, &sal,
  1876. sym.symbol->natural_name (), 0);
  1877. }
  1878. }
  1879. }
  1880. if (!ls->minimal_symbols.empty ())
  1881. {
  1882. /* Sort minimal symbols by program space, too */
  1883. std::sort (ls->minimal_symbols.begin (),
  1884. ls->minimal_symbols.end (),
  1885. compare_msymbols);
  1886. for (const auto &elem : ls->minimal_symbols)
  1887. {
  1888. program_space *pspace = elem.objfile->pspace;
  1889. set_current_program_space (pspace);
  1890. minsym_found (state, elem.objfile, elem.minsym, &sals);
  1891. }
  1892. }
  1893. }
  1894. else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
  1895. {
  1896. /* Only an offset was specified. */
  1897. sals = create_sals_line_offset (state, ls);
  1898. /* Make sure we have a filename for canonicalization. */
  1899. if (ls->explicit_loc.source_filename == NULL)
  1900. {
  1901. const char *fullname = symtab_to_fullname (state->default_symtab);
  1902. /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
  1903. form so that displaying SOURCE_FILENAME can follow the current
  1904. FILENAME_DISPLAY_STRING setting. But as it is used only rarely
  1905. it has been kept for code simplicity only in absolute form. */
  1906. ls->explicit_loc.source_filename = xstrdup (fullname);
  1907. }
  1908. }
  1909. else
  1910. {
  1911. /* We haven't found any results... */
  1912. return sals;
  1913. }
  1914. canonicalize_linespec (state, ls);
  1915. if (!sals.empty () && state->canonical != NULL)
  1916. state->canonical->pre_expanded = 1;
  1917. return sals;
  1918. }
  1919. /* Build RESULT from the explicit location components SOURCE_FILENAME,
  1920. FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
  1921. static void
  1922. convert_explicit_location_to_linespec (struct linespec_state *self,
  1923. linespec *result,
  1924. const char *source_filename,
  1925. const char *function_name,
  1926. symbol_name_match_type fname_match_type,
  1927. const char *label_name,
  1928. struct line_offset line_offset)
  1929. {
  1930. std::vector<bound_minimal_symbol> minimal_symbols;
  1931. result->explicit_loc.func_name_match_type = fname_match_type;
  1932. if (source_filename != NULL)
  1933. {
  1934. try
  1935. {
  1936. result->file_symtabs
  1937. = symtabs_from_filename (source_filename, self->search_pspace);
  1938. }
  1939. catch (const gdb_exception_error &except)
  1940. {
  1941. source_file_not_found_error (source_filename);
  1942. }
  1943. result->explicit_loc.source_filename = xstrdup (source_filename);
  1944. }
  1945. else
  1946. {
  1947. /* A NULL entry means to use the default symtab. */
  1948. result->file_symtabs.push_back (nullptr);
  1949. }
  1950. if (function_name != NULL)
  1951. {
  1952. std::vector<block_symbol> symbols;
  1953. find_linespec_symbols (self, result->file_symtabs,
  1954. function_name, fname_match_type,
  1955. &symbols, &minimal_symbols);
  1956. if (symbols.empty () && minimal_symbols.empty ())
  1957. symbol_not_found_error (function_name,
  1958. result->explicit_loc.source_filename);
  1959. result->explicit_loc.function_name = xstrdup (function_name);
  1960. result->function_symbols = std::move (symbols);
  1961. result->minimal_symbols = std::move (minimal_symbols);
  1962. }
  1963. if (label_name != NULL)
  1964. {
  1965. std::vector<block_symbol> symbols;
  1966. std::vector<block_symbol> labels
  1967. = find_label_symbols (self, result->function_symbols,
  1968. &symbols, label_name);
  1969. if (labels.empty ())
  1970. undefined_label_error (result->explicit_loc.function_name,
  1971. label_name);
  1972. result->explicit_loc.label_name = xstrdup (label_name);
  1973. result->labels.label_symbols = labels;
  1974. result->labels.function_symbols = std::move (symbols);
  1975. }
  1976. if (line_offset.sign != LINE_OFFSET_UNKNOWN)
  1977. result->explicit_loc.line_offset = line_offset;
  1978. }
  1979. /* Convert the explicit location EXPLICIT_LOC into SaLs. */
  1980. static std::vector<symtab_and_line>
  1981. convert_explicit_location_to_sals (struct linespec_state *self,
  1982. linespec *result,
  1983. const struct explicit_location *explicit_loc)
  1984. {
  1985. convert_explicit_location_to_linespec (self, result,
  1986. explicit_loc->source_filename,
  1987. explicit_loc->function_name,
  1988. explicit_loc->func_name_match_type,
  1989. explicit_loc->label_name,
  1990. explicit_loc->line_offset);
  1991. return convert_linespec_to_sals (self, result);
  1992. }
  1993. /* Parse a string that specifies a linespec.
  1994. The basic grammar of linespecs:
  1995. linespec -> var_spec | basic_spec
  1996. var_spec -> '$' (STRING | NUMBER)
  1997. basic_spec -> file_offset_spec | function_spec | label_spec
  1998. file_offset_spec -> opt_file_spec offset_spec
  1999. function_spec -> opt_file_spec function_name_spec opt_label_spec
  2000. label_spec -> label_name_spec
  2001. opt_file_spec -> "" | file_name_spec ':'
  2002. opt_label_spec -> "" | ':' label_name_spec
  2003. file_name_spec -> STRING
  2004. function_name_spec -> STRING
  2005. label_name_spec -> STRING
  2006. function_name_spec -> STRING
  2007. offset_spec -> NUMBER
  2008. -> '+' NUMBER
  2009. -> '-' NUMBER
  2010. This may all be followed by several keywords such as "if EXPR",
  2011. which we ignore.
  2012. A comma will terminate parsing.
  2013. The function may be an undebuggable function found in minimal symbol table.
  2014. If the argument FUNFIRSTLINE is nonzero, we want the first line
  2015. of real code inside a function when a function is specified, and it is
  2016. not OK to specify a variable or type to get its line number.
  2017. DEFAULT_SYMTAB specifies the file to use if none is specified.
  2018. It defaults to current_source_symtab.
  2019. DEFAULT_LINE specifies the line number to use for relative
  2020. line numbers (that start with signs). Defaults to current_source_line.
  2021. If CANONICAL is non-NULL, store an array of strings containing the canonical
  2022. line specs there if necessary. Currently overloaded member functions and
  2023. line numbers or static functions without a filename yield a canonical
  2024. line spec. The array and the line spec strings are allocated on the heap,
  2025. it is the callers responsibility to free them.
  2026. Note that it is possible to return zero for the symtab
  2027. if no file is validly specified. Callers must check that.
  2028. Also, the line number returned may be invalid. */
  2029. /* Parse the linespec in ARG, which must not be nullptr. MATCH_TYPE
  2030. indicates how function names should be matched. */
  2031. static std::vector<symtab_and_line>
  2032. parse_linespec (linespec_parser *parser, const char *arg,
  2033. symbol_name_match_type match_type)
  2034. {
  2035. gdb_assert (arg != nullptr);
  2036. struct gdb_exception file_exception;
  2037. /* A special case to start. It has become quite popular for
  2038. IDEs to work around bugs in the previous parser by quoting
  2039. the entire linespec, so we attempt to deal with this nicely. */
  2040. parser->is_quote_enclosed = 0;
  2041. if (parser->completion_tracker == NULL
  2042. && !is_ada_operator (arg)
  2043. && *arg != '\0'
  2044. && strchr (linespec_quote_characters, *arg) != NULL)
  2045. {
  2046. const char *end = skip_quote_char (arg + 1, *arg);
  2047. if (end != NULL && is_closing_quote_enclosed (end))
  2048. {
  2049. /* Here's the special case. Skip ARG past the initial
  2050. quote. */
  2051. ++arg;
  2052. parser->is_quote_enclosed = 1;
  2053. }
  2054. }
  2055. parser->lexer.saved_arg = arg;
  2056. parser->lexer.stream = arg;
  2057. parser->completion_word = arg;
  2058. parser->complete_what = linespec_complete_what::FUNCTION;
  2059. PARSER_EXPLICIT (parser)->func_name_match_type = match_type;
  2060. /* Initialize the default symtab and line offset. */
  2061. initialize_defaults (&PARSER_STATE (parser)->default_symtab,
  2062. &PARSER_STATE (parser)->default_line);
  2063. /* Objective-C shortcut. */
  2064. if (parser->completion_tracker == NULL)
  2065. {
  2066. std::vector<symtab_and_line> values
  2067. = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
  2068. if (!values.empty ())
  2069. return values;
  2070. }
  2071. else
  2072. {
  2073. /* "-"/"+" is either an objc selector, or a number. There's
  2074. nothing to complete the latter to, so just let the caller
  2075. complete on functions, which finds objc selectors, if there's
  2076. any. */
  2077. if ((arg[0] == '-' || arg[0] == '+') && arg[1] == '\0')
  2078. return {};
  2079. }
  2080. /* Start parsing. */
  2081. /* Get the first token. */
  2082. linespec_token token = linespec_lexer_consume_token (parser);
  2083. /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
  2084. if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
  2085. {
  2086. /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
  2087. if (parser->completion_tracker == NULL)
  2088. PARSER_RESULT (parser)->file_symtabs.push_back (nullptr);
  2089. /* User specified a convenience variable or history value. */
  2090. gdb::unique_xmalloc_ptr<char> var = copy_token_string (token);
  2091. PARSER_EXPLICIT (parser)->line_offset
  2092. = linespec_parse_variable (PARSER_STATE (parser), var.get ());
  2093. /* If a line_offset wasn't found (VAR is the name of a user
  2094. variable/function), then skip to normal symbol processing. */
  2095. if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
  2096. {
  2097. /* Consume this token. */
  2098. linespec_lexer_consume_token (parser);
  2099. goto convert_to_sals;
  2100. }
  2101. }
  2102. else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
  2103. {
  2104. /* Let the default linespec_complete_what::FUNCTION kick in. */
  2105. unexpected_linespec_error (parser);
  2106. }
  2107. else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
  2108. {
  2109. parser->complete_what = linespec_complete_what::NOTHING;
  2110. unexpected_linespec_error (parser);
  2111. }
  2112. /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
  2113. this token cannot represent a filename. */
  2114. token = linespec_lexer_peek_token (parser);
  2115. if (token.type == LSTOKEN_COLON)
  2116. {
  2117. /* Get the current token again and extract the filename. */
  2118. token = linespec_lexer_lex_one (parser);
  2119. gdb::unique_xmalloc_ptr<char> user_filename = copy_token_string (token);
  2120. /* Check if the input is a filename. */
  2121. try
  2122. {
  2123. PARSER_RESULT (parser)->file_symtabs
  2124. = symtabs_from_filename (user_filename.get (),
  2125. PARSER_STATE (parser)->search_pspace);
  2126. }
  2127. catch (gdb_exception_error &ex)
  2128. {
  2129. file_exception = std::move (ex);
  2130. }
  2131. if (file_exception.reason >= 0)
  2132. {
  2133. /* Symtabs were found for the file. Record the filename. */
  2134. PARSER_EXPLICIT (parser)->source_filename = user_filename.release ();
  2135. /* Get the next token. */
  2136. token = linespec_lexer_consume_token (parser);
  2137. /* This is LSTOKEN_COLON; consume it. */
  2138. linespec_lexer_consume_token (parser);
  2139. }
  2140. else
  2141. {
  2142. /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
  2143. PARSER_RESULT (parser)->file_symtabs.push_back (nullptr);
  2144. }
  2145. }
  2146. /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
  2147. else if (parser->completion_tracker == NULL
  2148. && (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
  2149. && token.type != LSTOKEN_COMMA))
  2150. {
  2151. /* TOKEN is the _next_ token, not the one currently in the parser.
  2152. Consuming the token will give the correct error message. */
  2153. linespec_lexer_consume_token (parser);
  2154. unexpected_linespec_error (parser);
  2155. }
  2156. else
  2157. {
  2158. /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
  2159. PARSER_RESULT (parser)->file_symtabs.push_back (nullptr);
  2160. }
  2161. /* Parse the rest of the linespec. */
  2162. linespec_parse_basic (parser);
  2163. if (parser->completion_tracker == NULL
  2164. && PARSER_RESULT (parser)->function_symbols.empty ()
  2165. && PARSER_RESULT (parser)->labels.label_symbols.empty ()
  2166. && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
  2167. && PARSER_RESULT (parser)->minimal_symbols.empty ())
  2168. {
  2169. /* The linespec didn't parse. Re-throw the file exception if
  2170. there was one. */
  2171. if (file_exception.reason < 0)
  2172. throw_exception (std::move (file_exception));
  2173. /* Otherwise, the symbol is not found. */
  2174. symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
  2175. PARSER_EXPLICIT (parser)->source_filename);
  2176. }
  2177. convert_to_sals:
  2178. /* Get the last token and record how much of the input was parsed,
  2179. if necessary. */
  2180. token = linespec_lexer_lex_one (parser);
  2181. if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
  2182. unexpected_linespec_error (parser);
  2183. else if (token.type == LSTOKEN_KEYWORD)
  2184. {
  2185. /* Setup the completion word past the keyword. Lexing never
  2186. advances past a keyword automatically, so skip it
  2187. manually. */
  2188. parser->completion_word
  2189. = skip_spaces (skip_to_space (PARSER_STREAM (parser)));
  2190. parser->complete_what = linespec_complete_what::EXPRESSION;
  2191. }
  2192. /* Convert the data in PARSER_RESULT to SALs. */
  2193. if (parser->completion_tracker == NULL)
  2194. return convert_linespec_to_sals (PARSER_STATE (parser),
  2195. PARSER_RESULT (parser));
  2196. return {};
  2197. }
  2198. /* A constructor for linespec_state. */
  2199. static void
  2200. linespec_state_constructor (struct linespec_state *self,
  2201. int flags, const struct language_defn *language,
  2202. struct program_space *search_pspace,
  2203. struct symtab *default_symtab,
  2204. int default_line,
  2205. struct linespec_result *canonical)
  2206. {
  2207. memset (self, 0, sizeof (*self));
  2208. self->language = language;
  2209. self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
  2210. self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
  2211. self->search_pspace = search_pspace;
  2212. self->default_symtab = default_symtab;
  2213. self->default_line = default_line;
  2214. self->canonical = canonical;
  2215. self->program_space = current_program_space;
  2216. self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
  2217. xfree, xcalloc, xfree);
  2218. self->is_linespec = 0;
  2219. }
  2220. /* Initialize a new linespec parser. */
  2221. linespec_parser::linespec_parser (int flags,
  2222. const struct language_defn *language,
  2223. struct program_space *search_pspace,
  2224. struct symtab *default_symtab,
  2225. int default_line,
  2226. struct linespec_result *canonical)
  2227. {
  2228. lexer.current.type = LSTOKEN_CONSUMED;
  2229. PARSER_EXPLICIT (this)->func_name_match_type
  2230. = symbol_name_match_type::WILD;
  2231. PARSER_EXPLICIT (this)->line_offset.sign = LINE_OFFSET_UNKNOWN;
  2232. linespec_state_constructor (PARSER_STATE (this), flags, language,
  2233. search_pspace,
  2234. default_symtab, default_line, canonical);
  2235. }
  2236. /* A destructor for linespec_state. */
  2237. static void
  2238. linespec_state_destructor (struct linespec_state *self)
  2239. {
  2240. htab_delete (self->addr_set);
  2241. xfree (self->canonical_names);
  2242. }
  2243. /* Delete a linespec parser. */
  2244. linespec_parser::~linespec_parser ()
  2245. {
  2246. xfree (PARSER_EXPLICIT (this)->source_filename);
  2247. xfree (PARSER_EXPLICIT (this)->label_name);
  2248. xfree (PARSER_EXPLICIT (this)->function_name);
  2249. linespec_state_destructor (PARSER_STATE (this));
  2250. }
  2251. /* See description in linespec.h. */
  2252. void
  2253. linespec_lex_to_end (const char **stringp)
  2254. {
  2255. linespec_token token;
  2256. const char *orig;
  2257. if (stringp == NULL || *stringp == NULL)
  2258. return;
  2259. linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
  2260. parser.lexer.saved_arg = *stringp;
  2261. PARSER_STREAM (&parser) = orig = *stringp;
  2262. do
  2263. {
  2264. /* Stop before any comma tokens; we need it to keep it
  2265. as the next token in the string. */
  2266. token = linespec_lexer_peek_token (&parser);
  2267. if (token.type == LSTOKEN_COMMA)
  2268. break;
  2269. token = linespec_lexer_consume_token (&parser);
  2270. }
  2271. while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
  2272. *stringp += PARSER_STREAM (&parser) - orig;
  2273. }
  2274. /* See linespec.h. */
  2275. void
  2276. linespec_complete_function (completion_tracker &tracker,
  2277. const char *function,
  2278. symbol_name_match_type func_match_type,
  2279. const char *source_filename)
  2280. {
  2281. complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
  2282. if (source_filename != NULL)
  2283. {
  2284. collect_file_symbol_completion_matches (tracker, mode, func_match_type,
  2285. function, function, source_filename);
  2286. }
  2287. else
  2288. {
  2289. collect_symbol_completion_matches (tracker, mode, func_match_type,
  2290. function, function);
  2291. }
  2292. }
  2293. /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
  2294. only meaningful if COMPONENT is FUNCTION. */
  2295. static void
  2296. complete_linespec_component (linespec_parser *parser,
  2297. completion_tracker &tracker,
  2298. const char *text,
  2299. linespec_complete_what component,
  2300. const char *source_filename)
  2301. {
  2302. if (component == linespec_complete_what::KEYWORD)
  2303. {
  2304. complete_on_enum (tracker, linespec_keywords, text, text);
  2305. }
  2306. else if (component == linespec_complete_what::EXPRESSION)
  2307. {
  2308. const char *word
  2309. = advance_to_expression_complete_word_point (tracker, text);
  2310. complete_expression (tracker, text, word);
  2311. }
  2312. else if (component == linespec_complete_what::FUNCTION)
  2313. {
  2314. completion_list fn_list;
  2315. symbol_name_match_type match_type
  2316. = PARSER_EXPLICIT (parser)->func_name_match_type;
  2317. linespec_complete_function (tracker, text, match_type, source_filename);
  2318. if (source_filename == NULL)
  2319. {
  2320. /* Haven't seen a source component, like in "b
  2321. file.c:function[TAB]". Maybe this wasn't a function, but
  2322. a filename instead, like "b file.[TAB]". */
  2323. fn_list = complete_source_filenames (text);
  2324. }
  2325. /* If we only have a single filename completion, append a ':' for
  2326. the user, since that's the only thing that can usefully follow
  2327. the filename. */
  2328. if (fn_list.size () == 1 && !tracker.have_completions ())
  2329. {
  2330. char *fn = fn_list[0].release ();
  2331. /* If we also need to append a quote char, it needs to be
  2332. appended before the ':'. Append it now, and make ':' the
  2333. new "quote" char. */
  2334. if (tracker.quote_char ())
  2335. {
  2336. char quote_char_str[2] = { (char) tracker.quote_char () };
  2337. fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
  2338. tracker.set_quote_char (':');
  2339. }
  2340. else
  2341. fn = reconcat (fn, fn, ":", (char *) NULL);
  2342. fn_list[0].reset (fn);
  2343. /* Tell readline to skip appending a space. */
  2344. tracker.set_suppress_append_ws (true);
  2345. }
  2346. tracker.add_completions (std::move (fn_list));
  2347. }
  2348. }
  2349. /* Helper for linespec_complete_label. Find labels that match
  2350. LABEL_NAME in the function symbols listed in the PARSER, and add
  2351. them to the tracker. */
  2352. static void
  2353. complete_label (completion_tracker &tracker,
  2354. linespec_parser *parser,
  2355. const char *label_name)
  2356. {
  2357. std::vector<block_symbol> label_function_symbols;
  2358. std::vector<block_symbol> labels
  2359. = find_label_symbols (PARSER_STATE (parser),
  2360. PARSER_RESULT (parser)->function_symbols,
  2361. &label_function_symbols,
  2362. label_name, true);
  2363. for (const auto &label : labels)
  2364. {
  2365. char *match = xstrdup (label.symbol->search_name ());
  2366. tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
  2367. }
  2368. }
  2369. /* See linespec.h. */
  2370. void
  2371. linespec_complete_label (completion_tracker &tracker,
  2372. const struct language_defn *language,
  2373. const char *source_filename,
  2374. const char *function_name,
  2375. symbol_name_match_type func_name_match_type,
  2376. const char *label_name)
  2377. {
  2378. linespec_parser parser (0, language, NULL, NULL, 0, NULL);
  2379. line_offset unknown_offset = { 0, LINE_OFFSET_UNKNOWN };
  2380. try
  2381. {
  2382. convert_explicit_location_to_linespec (PARSER_STATE (&parser),
  2383. PARSER_RESULT (&parser),
  2384. source_filename,
  2385. function_name,
  2386. func_name_match_type,
  2387. NULL, unknown_offset);
  2388. }
  2389. catch (const gdb_exception_error &ex)
  2390. {
  2391. return;
  2392. }
  2393. complete_label (tracker, &parser, label_name);
  2394. }
  2395. /* See description in linespec.h. */
  2396. void
  2397. linespec_complete (completion_tracker &tracker, const char *text,
  2398. symbol_name_match_type match_type)
  2399. {
  2400. const char *orig = text;
  2401. linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
  2402. parser.lexer.saved_arg = text;
  2403. PARSER_EXPLICIT (&parser)->func_name_match_type = match_type;
  2404. PARSER_STREAM (&parser) = text;
  2405. parser.completion_tracker = &tracker;
  2406. PARSER_STATE (&parser)->is_linespec = 1;
  2407. /* Parse as much as possible. parser.completion_word will hold
  2408. furthest completion point we managed to parse to. */
  2409. try
  2410. {
  2411. parse_linespec (&parser, text, match_type);
  2412. }
  2413. catch (const gdb_exception_error &except)
  2414. {
  2415. }
  2416. if (parser.completion_quote_char != '\0'
  2417. && parser.completion_quote_end != NULL
  2418. && parser.completion_quote_end[1] == '\0')
  2419. {
  2420. /* If completing a quoted string with the cursor right at
  2421. terminating quote char, complete the completion word without
  2422. interpretation, so that readline advances the cursor one
  2423. whitespace past the quote, even if there's no match. This
  2424. makes these cases behave the same:
  2425. before: "b function()"
  2426. after: "b function() "
  2427. before: "b 'function()'"
  2428. after: "b 'function()' "
  2429. and trusts the user in this case:
  2430. before: "b 'not_loaded_function_yet()'"
  2431. after: "b 'not_loaded_function_yet()' "
  2432. */
  2433. parser.complete_what = linespec_complete_what::NOTHING;
  2434. parser.completion_quote_char = '\0';
  2435. gdb::unique_xmalloc_ptr<char> text_copy
  2436. (xstrdup (parser.completion_word));
  2437. tracker.add_completion (std::move (text_copy));
  2438. }
  2439. tracker.set_quote_char (parser.completion_quote_char);
  2440. if (parser.complete_what == linespec_complete_what::LABEL)
  2441. {
  2442. parser.complete_what = linespec_complete_what::NOTHING;
  2443. const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
  2444. std::vector<block_symbol> function_symbols;
  2445. std::vector<bound_minimal_symbol> minimal_symbols;
  2446. find_linespec_symbols (PARSER_STATE (&parser),
  2447. PARSER_RESULT (&parser)->file_symtabs,
  2448. func_name, match_type,
  2449. &function_symbols, &minimal_symbols);
  2450. PARSER_RESULT (&parser)->function_symbols = std::move (function_symbols);
  2451. PARSER_RESULT (&parser)->minimal_symbols = std::move (minimal_symbols);
  2452. complete_label (tracker, &parser, parser.completion_word);
  2453. }
  2454. else if (parser.complete_what == linespec_complete_what::FUNCTION)
  2455. {
  2456. /* While parsing/lexing, we didn't know whether the completion
  2457. word completes to a unique function/source name already or
  2458. not.
  2459. E.g.:
  2460. "b function() <tab>"
  2461. may need to complete either to:
  2462. "b function() const"
  2463. or to:
  2464. "b function() if/thread/task"
  2465. Or, this:
  2466. "b foo t"
  2467. may need to complete either to:
  2468. "b foo template_fun<T>()"
  2469. with "foo" being the template function's return type, or to:
  2470. "b foo thread/task"
  2471. Or, this:
  2472. "b file<TAB>"
  2473. may need to complete either to a source file name:
  2474. "b file.c"
  2475. or this, also a filename, but a unique completion:
  2476. "b file.c:"
  2477. or to a function name:
  2478. "b file_function"
  2479. Address that by completing assuming source or function, and
  2480. seeing if we find a completion that matches exactly the
  2481. completion word. If so, then it must be a function (see note
  2482. below) and we advance the completion word to the end of input
  2483. and switch to KEYWORD completion mode.
  2484. Note: if we find a unique completion for a source filename,
  2485. then it won't match the completion word, because the LCD will
  2486. contain a trailing ':'. And if we're completing at or after
  2487. the ':', then complete_linespec_component won't try to
  2488. complete on source filenames. */
  2489. const char *word = parser.completion_word;
  2490. complete_linespec_component (&parser, tracker,
  2491. parser.completion_word,
  2492. linespec_complete_what::FUNCTION,
  2493. PARSER_EXPLICIT (&parser)->source_filename);
  2494. parser.complete_what = linespec_complete_what::NOTHING;
  2495. if (tracker.quote_char ())
  2496. {
  2497. /* The function/file name was not close-quoted, so this
  2498. can't be a keyword. Note: complete_linespec_component
  2499. may have swapped the original quote char for ':' when we
  2500. get here, but that still indicates the same. */
  2501. }
  2502. else if (!tracker.have_completions ())
  2503. {
  2504. size_t key_start;
  2505. size_t wordlen = strlen (parser.completion_word);
  2506. key_start
  2507. = string_find_incomplete_keyword_at_end (linespec_keywords,
  2508. parser.completion_word,
  2509. wordlen);
  2510. if (key_start != -1
  2511. || (wordlen > 0
  2512. && parser.completion_word[wordlen - 1] == ' '))
  2513. {
  2514. parser.completion_word += key_start;
  2515. parser.complete_what = linespec_complete_what::KEYWORD;
  2516. }
  2517. }
  2518. else if (tracker.completes_to_completion_word (word))
  2519. {
  2520. /* Skip the function and complete on keywords. */
  2521. parser.completion_word += strlen (word);
  2522. parser.complete_what = linespec_complete_what::KEYWORD;
  2523. tracker.discard_completions ();
  2524. }
  2525. }
  2526. tracker.advance_custom_word_point_by (parser.completion_word - orig);
  2527. complete_linespec_component (&parser, tracker,
  2528. parser.completion_word,
  2529. parser.complete_what,
  2530. PARSER_EXPLICIT (&parser)->source_filename);
  2531. /* If we're past the "filename:function:label:offset" linespec, and
  2532. didn't find any match, then assume the user might want to create
  2533. a pending breakpoint anyway and offer the keyword
  2534. completions. */
  2535. if (!parser.completion_quote_char
  2536. && (parser.complete_what == linespec_complete_what::FUNCTION
  2537. || parser.complete_what == linespec_complete_what::LABEL
  2538. || parser.complete_what == linespec_complete_what::NOTHING)
  2539. && !tracker.have_completions ())
  2540. {
  2541. const char *end
  2542. = parser.completion_word + strlen (parser.completion_word);
  2543. if (end > orig && end[-1] == ' ')
  2544. {
  2545. tracker.advance_custom_word_point_by (end - parser.completion_word);
  2546. complete_linespec_component (&parser, tracker, end,
  2547. linespec_complete_what::KEYWORD,
  2548. NULL);
  2549. }
  2550. }
  2551. }
  2552. /* A helper function for decode_line_full and decode_line_1 to
  2553. turn LOCATION into std::vector<symtab_and_line>. */
  2554. static std::vector<symtab_and_line>
  2555. event_location_to_sals (linespec_parser *parser,
  2556. const struct event_location *location)
  2557. {
  2558. std::vector<symtab_and_line> result;
  2559. switch (event_location_type (location))
  2560. {
  2561. case LINESPEC_LOCATION:
  2562. {
  2563. PARSER_STATE (parser)->is_linespec = 1;
  2564. try
  2565. {
  2566. const linespec_location *ls = get_linespec_location (location);
  2567. result = parse_linespec (parser,
  2568. ls->spec_string, ls->match_type);
  2569. }
  2570. catch (const gdb_exception_error &except)
  2571. {
  2572. throw;
  2573. }
  2574. }
  2575. break;
  2576. case ADDRESS_LOCATION:
  2577. {
  2578. const char *addr_string = get_address_string_location (location);
  2579. CORE_ADDR addr = get_address_location (location);
  2580. if (addr_string != NULL)
  2581. {
  2582. addr = linespec_expression_to_pc (&addr_string);
  2583. if (PARSER_STATE (parser)->canonical != NULL)
  2584. PARSER_STATE (parser)->canonical->location
  2585. = copy_event_location (location);
  2586. }
  2587. result = convert_address_location_to_sals (PARSER_STATE (parser),
  2588. addr);
  2589. }
  2590. break;
  2591. case EXPLICIT_LOCATION:
  2592. {
  2593. const struct explicit_location *explicit_loc;
  2594. explicit_loc = get_explicit_location_const (location);
  2595. result = convert_explicit_location_to_sals (PARSER_STATE (parser),
  2596. PARSER_RESULT (parser),
  2597. explicit_loc);
  2598. }
  2599. break;
  2600. case PROBE_LOCATION:
  2601. /* Probes are handled by their own decoders. */
  2602. gdb_assert_not_reached ("attempt to decode probe location");
  2603. break;
  2604. default:
  2605. gdb_assert_not_reached ("unhandled event location type");
  2606. }
  2607. return result;
  2608. }
  2609. /* See linespec.h. */
  2610. void
  2611. decode_line_full (struct event_location *location, int flags,
  2612. struct program_space *search_pspace,
  2613. struct symtab *default_symtab,
  2614. int default_line, struct linespec_result *canonical,
  2615. const char *select_mode,
  2616. const char *filter)
  2617. {
  2618. std::vector<const char *> filters;
  2619. struct linespec_state *state;
  2620. gdb_assert (canonical != NULL);
  2621. /* The filter only makes sense for 'all'. */
  2622. gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
  2623. gdb_assert (select_mode == NULL
  2624. || select_mode == multiple_symbols_all
  2625. || select_mode == multiple_symbols_ask
  2626. || select_mode == multiple_symbols_cancel);
  2627. gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
  2628. linespec_parser parser (flags, current_language,
  2629. search_pspace, default_symtab,
  2630. default_line, canonical);
  2631. scoped_restore_current_program_space restore_pspace;
  2632. std::vector<symtab_and_line> result = event_location_to_sals (&parser,
  2633. location);
  2634. state = PARSER_STATE (&parser);
  2635. if (result.size () == 0)
  2636. throw_error (NOT_SUPPORTED_ERROR, _("Location %s not available"),
  2637. event_location_to_string (location));
  2638. gdb_assert (result.size () == 1 || canonical->pre_expanded);
  2639. canonical->pre_expanded = 1;
  2640. /* Arrange for allocated canonical names to be freed. */
  2641. std::vector<gdb::unique_xmalloc_ptr<char>> hold_names;
  2642. for (int i = 0; i < result.size (); ++i)
  2643. {
  2644. gdb_assert (state->canonical_names[i].suffix != NULL);
  2645. hold_names.emplace_back (state->canonical_names[i].suffix);
  2646. }
  2647. if (select_mode == NULL)
  2648. {
  2649. if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
  2650. select_mode = multiple_symbols_all;
  2651. else
  2652. select_mode = multiple_symbols_select_mode ();
  2653. }
  2654. if (select_mode == multiple_symbols_all)
  2655. {
  2656. if (filter != NULL)
  2657. {
  2658. filters.push_back (filter);
  2659. filter_results (state, &result, filters);
  2660. }
  2661. else
  2662. convert_results_to_lsals (state, &result);
  2663. }
  2664. else
  2665. decode_line_2 (state, &result, select_mode);
  2666. }
  2667. /* See linespec.h. */
  2668. std::vector<symtab_and_line>
  2669. decode_line_1 (const struct event_location *location, int flags,
  2670. struct program_space *search_pspace,
  2671. struct symtab *default_symtab,
  2672. int default_line)
  2673. {
  2674. linespec_parser parser (flags, current_language,
  2675. search_pspace, default_symtab,
  2676. default_line, NULL);
  2677. scoped_restore_current_program_space restore_pspace;
  2678. return event_location_to_sals (&parser, location);
  2679. }
  2680. /* See linespec.h. */
  2681. std::vector<symtab_and_line>
  2682. decode_line_with_current_source (const char *string, int flags)
  2683. {
  2684. if (string == 0)
  2685. error (_("Empty line specification."));
  2686. /* We use whatever is set as the current source line. We do not try
  2687. and get a default source symtab+line or it will recursively call us! */
  2688. symtab_and_line cursal = get_current_source_symtab_and_line ();
  2689. event_location_up location = string_to_event_location (&string,
  2690. current_language);
  2691. std::vector<symtab_and_line> sals
  2692. = decode_line_1 (location.get (), flags, NULL, cursal.symtab, cursal.line);
  2693. if (*string)
  2694. error (_("Junk at end of line specification: %s"), string);
  2695. return sals;
  2696. }
  2697. /* See linespec.h. */
  2698. std::vector<symtab_and_line>
  2699. decode_line_with_last_displayed (const char *string, int flags)
  2700. {
  2701. if (string == 0)
  2702. error (_("Empty line specification."));
  2703. event_location_up location = string_to_event_location (&string,
  2704. current_language);
  2705. std::vector<symtab_and_line> sals
  2706. = (last_displayed_sal_is_valid ()
  2707. ? decode_line_1 (location.get (), flags, NULL,
  2708. get_last_displayed_symtab (),
  2709. get_last_displayed_line ())
  2710. : decode_line_1 (location.get (), flags, NULL, NULL, 0));
  2711. if (*string)
  2712. error (_("Junk at end of line specification: %s"), string);
  2713. return sals;
  2714. }
  2715. /* First, some functions to initialize stuff at the beginning of the
  2716. function. */
  2717. static void
  2718. initialize_defaults (struct symtab **default_symtab, int *default_line)
  2719. {
  2720. if (*default_symtab == 0)
  2721. {
  2722. /* Use whatever we have for the default source line. We don't use
  2723. get_current_or_default_symtab_and_line as it can recurse and call
  2724. us back! */
  2725. struct symtab_and_line cursal =
  2726. get_current_source_symtab_and_line ();
  2727. *default_symtab = cursal.symtab;
  2728. *default_line = cursal.line;
  2729. }
  2730. }
  2731. /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
  2732. advancing EXP_PTR past any parsed text. */
  2733. CORE_ADDR
  2734. linespec_expression_to_pc (const char **exp_ptr)
  2735. {
  2736. if (current_program_space->executing_startup)
  2737. /* The error message doesn't really matter, because this case
  2738. should only hit during breakpoint reset. */
  2739. throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
  2740. "program space is in startup"));
  2741. (*exp_ptr)++;
  2742. return value_as_address (parse_to_comma_and_eval (exp_ptr));
  2743. }
  2744. /* Here's where we recognise an Objective-C Selector. An Objective C
  2745. selector may be implemented by more than one class, therefore it
  2746. may represent more than one method/function. This gives us a
  2747. situation somewhat analogous to C++ overloading. If there's more
  2748. than one method that could represent the selector, then use some of
  2749. the existing C++ code to let the user choose one. */
  2750. static std::vector<symtab_and_line>
  2751. decode_objc (struct linespec_state *self, linespec *ls, const char *arg)
  2752. {
  2753. struct collect_info info;
  2754. std::vector<const char *> symbol_names;
  2755. const char *new_argptr;
  2756. info.state = self;
  2757. std::vector<symtab *> symtabs;
  2758. symtabs.push_back (nullptr);
  2759. info.file_symtabs = &symtabs;
  2760. std::vector<block_symbol> symbols;
  2761. info.result.symbols = &symbols;
  2762. std::vector<bound_minimal_symbol> minimal_symbols;
  2763. info.result.minimal_symbols = &minimal_symbols;
  2764. new_argptr = find_imps (arg, &symbol_names);
  2765. if (symbol_names.empty ())
  2766. return {};
  2767. add_all_symbol_names_from_pspace (&info, NULL, symbol_names,
  2768. FUNCTIONS_DOMAIN);
  2769. std::vector<symtab_and_line> values;
  2770. if (!symbols.empty () || !minimal_symbols.empty ())
  2771. {
  2772. char *saved_arg;
  2773. saved_arg = (char *) alloca (new_argptr - arg + 1);
  2774. memcpy (saved_arg, arg, new_argptr - arg);
  2775. saved_arg[new_argptr - arg] = '\0';
  2776. ls->explicit_loc.function_name = xstrdup (saved_arg);
  2777. ls->function_symbols = std::move (symbols);
  2778. ls->minimal_symbols = std::move (minimal_symbols);
  2779. values = convert_linespec_to_sals (self, ls);
  2780. if (self->canonical)
  2781. {
  2782. std::string holder;
  2783. const char *str;
  2784. self->canonical->pre_expanded = 1;
  2785. if (ls->explicit_loc.source_filename)
  2786. {
  2787. holder = string_printf ("%s:%s",
  2788. ls->explicit_loc.source_filename,
  2789. saved_arg);
  2790. str = holder.c_str ();
  2791. }
  2792. else
  2793. str = saved_arg;
  2794. self->canonical->location
  2795. = new_linespec_location (&str, symbol_name_match_type::FULL);
  2796. }
  2797. }
  2798. return values;
  2799. }
  2800. namespace {
  2801. /* A function object that serves as symbol_found_callback_ftype
  2802. callback for iterate_over_symbols. This is used by
  2803. lookup_prefix_sym to collect type symbols. */
  2804. class decode_compound_collector
  2805. {
  2806. public:
  2807. decode_compound_collector ()
  2808. : m_unique_syms (htab_create_alloc (1, htab_hash_pointer,
  2809. htab_eq_pointer, NULL,
  2810. xcalloc, xfree))
  2811. {
  2812. }
  2813. /* Return all symbols collected. */
  2814. std::vector<block_symbol> release_symbols ()
  2815. {
  2816. return std::move (m_symbols);
  2817. }
  2818. /* Callable as a symbol_found_callback_ftype callback. */
  2819. bool operator () (block_symbol *bsym);
  2820. private:
  2821. /* A hash table of all symbols we found. We use this to avoid
  2822. adding any symbol more than once. */
  2823. htab_up m_unique_syms;
  2824. /* The result vector. */
  2825. std::vector<block_symbol> m_symbols;
  2826. };
  2827. bool
  2828. decode_compound_collector::operator () (block_symbol *bsym)
  2829. {
  2830. void **slot;
  2831. struct type *t;
  2832. struct symbol *sym = bsym->symbol;
  2833. if (sym->aclass () != LOC_TYPEDEF)
  2834. return true; /* Continue iterating. */
  2835. t = sym->type ();
  2836. t = check_typedef (t);
  2837. if (t->code () != TYPE_CODE_STRUCT
  2838. && t->code () != TYPE_CODE_UNION
  2839. && t->code () != TYPE_CODE_NAMESPACE)
  2840. return true; /* Continue iterating. */
  2841. slot = htab_find_slot (m_unique_syms.get (), sym, INSERT);
  2842. if (!*slot)
  2843. {
  2844. *slot = sym;
  2845. m_symbols.push_back (*bsym);
  2846. }
  2847. return true; /* Continue iterating. */
  2848. }
  2849. } // namespace
  2850. /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
  2851. static std::vector<block_symbol>
  2852. lookup_prefix_sym (struct linespec_state *state,
  2853. const std::vector<symtab *> &file_symtabs,
  2854. const char *class_name)
  2855. {
  2856. decode_compound_collector collector;
  2857. lookup_name_info lookup_name (class_name, symbol_name_match_type::FULL);
  2858. for (const auto &elt : file_symtabs)
  2859. {
  2860. if (elt == nullptr)
  2861. {
  2862. iterate_over_all_matching_symtabs (state, lookup_name,
  2863. STRUCT_DOMAIN, ALL_DOMAIN,
  2864. NULL, false, collector);
  2865. iterate_over_all_matching_symtabs (state, lookup_name,
  2866. VAR_DOMAIN, ALL_DOMAIN,
  2867. NULL, false, collector);
  2868. }
  2869. else
  2870. {
  2871. /* Program spaces that are executing startup should have
  2872. been filtered out earlier. */
  2873. program_space *pspace = elt->compunit ()->objfile ()->pspace;
  2874. gdb_assert (!pspace->executing_startup);
  2875. set_current_program_space (pspace);
  2876. iterate_over_file_blocks (elt, lookup_name, STRUCT_DOMAIN, collector);
  2877. iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN, collector);
  2878. }
  2879. }
  2880. return collector.release_symbols ();
  2881. }
  2882. /* A std::sort comparison function for symbols. The resulting order does
  2883. not actually matter; we just need to be able to sort them so that
  2884. symbols with the same program space end up next to each other. */
  2885. static bool
  2886. compare_symbols (const block_symbol &a, const block_symbol &b)
  2887. {
  2888. uintptr_t uia, uib;
  2889. uia = (uintptr_t) symbol_symtab (a.symbol)->compunit ()->objfile ()->pspace;
  2890. uib = (uintptr_t) symbol_symtab (b.symbol)->compunit ()->objfile ()->pspace;
  2891. if (uia < uib)
  2892. return true;
  2893. if (uia > uib)
  2894. return false;
  2895. uia = (uintptr_t) a.symbol;
  2896. uib = (uintptr_t) b.symbol;
  2897. if (uia < uib)
  2898. return true;
  2899. return false;
  2900. }
  2901. /* Like compare_symbols but for minimal symbols. */
  2902. static bool
  2903. compare_msymbols (const bound_minimal_symbol &a, const bound_minimal_symbol &b)
  2904. {
  2905. uintptr_t uia, uib;
  2906. uia = (uintptr_t) a.objfile->pspace;
  2907. uib = (uintptr_t) a.objfile->pspace;
  2908. if (uia < uib)
  2909. return true;
  2910. if (uia > uib)
  2911. return false;
  2912. uia = (uintptr_t) a.minsym;
  2913. uib = (uintptr_t) b.minsym;
  2914. if (uia < uib)
  2915. return true;
  2916. return false;
  2917. }
  2918. /* Look for all the matching instances of each symbol in NAMES. Only
  2919. instances from PSPACE are considered; other program spaces are
  2920. handled by our caller. If PSPACE is NULL, then all program spaces
  2921. are considered. Results are stored into INFO. */
  2922. static void
  2923. add_all_symbol_names_from_pspace (struct collect_info *info,
  2924. struct program_space *pspace,
  2925. const std::vector<const char *> &names,
  2926. enum search_domain search_domain)
  2927. {
  2928. for (const char *iter : names)
  2929. add_matching_symbols_to_info (iter,
  2930. symbol_name_match_type::FULL,
  2931. search_domain, info, pspace);
  2932. }
  2933. static void
  2934. find_superclass_methods (std::vector<struct type *> &&superclasses,
  2935. const char *name, enum language name_lang,
  2936. std::vector<const char *> *result_names)
  2937. {
  2938. size_t old_len = result_names->size ();
  2939. while (1)
  2940. {
  2941. std::vector<struct type *> new_supers;
  2942. for (type *t : superclasses)
  2943. find_methods (t, name_lang, name, result_names, &new_supers);
  2944. if (result_names->size () != old_len || new_supers.empty ())
  2945. break;
  2946. superclasses = std::move (new_supers);
  2947. }
  2948. }
  2949. /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
  2950. given by one of the symbols in SYM_CLASSES. Matches are returned
  2951. in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
  2952. static void
  2953. find_method (struct linespec_state *self,
  2954. const std::vector<symtab *> &file_symtabs,
  2955. const char *class_name, const char *method_name,
  2956. std::vector<block_symbol> *sym_classes,
  2957. std::vector<block_symbol> *symbols,
  2958. std::vector<bound_minimal_symbol> *minsyms)
  2959. {
  2960. size_t last_result_len;
  2961. std::vector<struct type *> superclass_vec;
  2962. std::vector<const char *> result_names;
  2963. struct collect_info info;
  2964. /* Sort symbols so that symbols with the same program space are next
  2965. to each other. */
  2966. std::sort (sym_classes->begin (), sym_classes->end (),
  2967. compare_symbols);
  2968. info.state = self;
  2969. info.file_symtabs = &file_symtabs;
  2970. info.result.symbols = symbols;
  2971. info.result.minimal_symbols = minsyms;
  2972. /* Iterate over all the types, looking for the names of existing
  2973. methods matching METHOD_NAME. If we cannot find a direct method in a
  2974. given program space, then we consider inherited methods; this is
  2975. not ideal (ideal would be to respect C++ hiding rules), but it
  2976. seems good enough and is what GDB has historically done. We only
  2977. need to collect the names because later we find all symbols with
  2978. those names. This loop is written in a somewhat funny way
  2979. because we collect data across the program space before deciding
  2980. what to do. */
  2981. last_result_len = 0;
  2982. for (const auto &elt : *sym_classes)
  2983. {
  2984. struct type *t;
  2985. struct program_space *pspace;
  2986. struct symbol *sym = elt.symbol;
  2987. unsigned int ix = &elt - &*sym_classes->begin ();
  2988. /* Program spaces that are executing startup should have
  2989. been filtered out earlier. */
  2990. pspace = symbol_symtab (sym)->compunit ()->objfile ()->pspace;
  2991. gdb_assert (!pspace->executing_startup);
  2992. set_current_program_space (pspace);
  2993. t = check_typedef (sym->type ());
  2994. find_methods (t, sym->language (),
  2995. method_name, &result_names, &superclass_vec);
  2996. /* Handle all items from a single program space at once; and be
  2997. sure not to miss the last batch. */
  2998. if (ix == sym_classes->size () - 1
  2999. || (pspace
  3000. != (symbol_symtab (sym_classes->at (ix + 1).symbol)
  3001. ->compunit ()->objfile ()->pspace)))
  3002. {
  3003. /* If we did not find a direct implementation anywhere in
  3004. this program space, consider superclasses. */
  3005. if (result_names.size () == last_result_len)
  3006. find_superclass_methods (std::move (superclass_vec), method_name,
  3007. sym->language (), &result_names);
  3008. /* We have a list of candidate symbol names, so now we
  3009. iterate over the symbol tables looking for all
  3010. matches in this pspace. */
  3011. add_all_symbol_names_from_pspace (&info, pspace, result_names,
  3012. FUNCTIONS_DOMAIN);
  3013. superclass_vec.clear ();
  3014. last_result_len = result_names.size ();
  3015. }
  3016. }
  3017. if (!symbols->empty () || !minsyms->empty ())
  3018. return;
  3019. /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
  3020. and other attempts to locate the symbol will be made. */
  3021. throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
  3022. }
  3023. namespace {
  3024. /* This function object is a callback for iterate_over_symtabs, used
  3025. when collecting all matching symtabs. */
  3026. class symtab_collector
  3027. {
  3028. public:
  3029. symtab_collector ()
  3030. : m_symtab_table (htab_create (1, htab_hash_pointer, htab_eq_pointer,
  3031. NULL))
  3032. {
  3033. }
  3034. /* Callable as a symbol_found_callback_ftype callback. */
  3035. bool operator () (symtab *sym);
  3036. /* Return an rvalue reference to the collected symtabs. */
  3037. std::vector<symtab *> &&release_symtabs ()
  3038. {
  3039. return std::move (m_symtabs);
  3040. }
  3041. private:
  3042. /* The result vector of symtabs. */
  3043. std::vector<symtab *> m_symtabs;
  3044. /* This is used to ensure the symtabs are unique. */
  3045. htab_up m_symtab_table;
  3046. };
  3047. bool
  3048. symtab_collector::operator () (struct symtab *symtab)
  3049. {
  3050. void **slot;
  3051. slot = htab_find_slot (m_symtab_table.get (), symtab, INSERT);
  3052. if (!*slot)
  3053. {
  3054. *slot = symtab;
  3055. m_symtabs.push_back (symtab);
  3056. }
  3057. return false;
  3058. }
  3059. } // namespace
  3060. /* Given a file name, return a list of all matching symtabs. If
  3061. SEARCH_PSPACE is not NULL, the search is restricted to just that
  3062. program space. */
  3063. static std::vector<symtab *>
  3064. collect_symtabs_from_filename (const char *file,
  3065. struct program_space *search_pspace)
  3066. {
  3067. symtab_collector collector;
  3068. /* Find that file's data. */
  3069. if (search_pspace == NULL)
  3070. {
  3071. for (struct program_space *pspace : program_spaces)
  3072. {
  3073. if (pspace->executing_startup)
  3074. continue;
  3075. set_current_program_space (pspace);
  3076. iterate_over_symtabs (file, collector);
  3077. }
  3078. }
  3079. else
  3080. {
  3081. set_current_program_space (search_pspace);
  3082. iterate_over_symtabs (file, collector);
  3083. }
  3084. return collector.release_symtabs ();
  3085. }
  3086. /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
  3087. not NULL, the search is restricted to just that program space. */
  3088. static std::vector<symtab *>
  3089. symtabs_from_filename (const char *filename,
  3090. struct program_space *search_pspace)
  3091. {
  3092. std::vector<symtab *> result
  3093. = collect_symtabs_from_filename (filename, search_pspace);
  3094. if (result.empty ())
  3095. {
  3096. if (!have_full_symbols () && !have_partial_symbols ())
  3097. throw_error (NOT_FOUND_ERROR,
  3098. _("No symbol table is loaded. "
  3099. "Use the \"file\" command."));
  3100. source_file_not_found_error (filename);
  3101. }
  3102. return result;
  3103. }
  3104. /* See symtab.h. */
  3105. void
  3106. symbol_searcher::find_all_symbols (const std::string &name,
  3107. const struct language_defn *language,
  3108. enum search_domain search_domain,
  3109. std::vector<symtab *> *search_symtabs,
  3110. struct program_space *search_pspace)
  3111. {
  3112. symbol_searcher_collect_info info;
  3113. struct linespec_state state;
  3114. memset (&state, 0, sizeof (state));
  3115. state.language = language;
  3116. info.state = &state;
  3117. info.result.symbols = &m_symbols;
  3118. info.result.minimal_symbols = &m_minimal_symbols;
  3119. std::vector<symtab *> all_symtabs;
  3120. if (search_symtabs == nullptr)
  3121. {
  3122. all_symtabs.push_back (nullptr);
  3123. search_symtabs = &all_symtabs;
  3124. }
  3125. info.file_symtabs = search_symtabs;
  3126. add_matching_symbols_to_info (name.c_str (), symbol_name_match_type::WILD,
  3127. search_domain, &info, search_pspace);
  3128. }
  3129. /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
  3130. debug symbols are returned in SYMBOLS. Matching minimal symbols are
  3131. returned in MINSYMS. */
  3132. static void
  3133. find_function_symbols (struct linespec_state *state,
  3134. const std::vector<symtab *> &file_symtabs, const char *name,
  3135. symbol_name_match_type name_match_type,
  3136. std::vector<block_symbol> *symbols,
  3137. std::vector<bound_minimal_symbol> *minsyms)
  3138. {
  3139. struct collect_info info;
  3140. std::vector<const char *> symbol_names;
  3141. info.state = state;
  3142. info.result.symbols = symbols;
  3143. info.result.minimal_symbols = minsyms;
  3144. info.file_symtabs = &file_symtabs;
  3145. /* Try NAME as an Objective-C selector. */
  3146. find_imps (name, &symbol_names);
  3147. if (!symbol_names.empty ())
  3148. add_all_symbol_names_from_pspace (&info, state->search_pspace,
  3149. symbol_names, FUNCTIONS_DOMAIN);
  3150. else
  3151. add_matching_symbols_to_info (name, name_match_type, FUNCTIONS_DOMAIN,
  3152. &info, state->search_pspace);
  3153. }
  3154. /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
  3155. in SYMBOLS and minimal symbols in MINSYMS. */
  3156. static void
  3157. find_linespec_symbols (struct linespec_state *state,
  3158. const std::vector<symtab *> &file_symtabs,
  3159. const char *lookup_name,
  3160. symbol_name_match_type name_match_type,
  3161. std::vector <block_symbol> *symbols,
  3162. std::vector<bound_minimal_symbol> *minsyms)
  3163. {
  3164. gdb::unique_xmalloc_ptr<char> canon
  3165. = cp_canonicalize_string_no_typedefs (lookup_name);
  3166. if (canon != nullptr)
  3167. lookup_name = canon.get ();
  3168. /* It's important to not call expand_symtabs_matching unnecessarily
  3169. as it can really slow things down (by unnecessarily expanding
  3170. potentially 1000s of symtabs, which when debugging some apps can
  3171. cost 100s of seconds). Avoid this to some extent by *first* calling
  3172. find_function_symbols, and only if that doesn't find anything
  3173. *then* call find_method. This handles two important cases:
  3174. 1) break (anonymous namespace)::foo
  3175. 2) break class::method where method is in class (and not a baseclass) */
  3176. find_function_symbols (state, file_symtabs, lookup_name,
  3177. name_match_type, symbols, minsyms);
  3178. /* If we were unable to locate a symbol of the same name, try dividing
  3179. the name into class and method names and searching the class and its
  3180. baseclasses. */
  3181. if (symbols->empty () && minsyms->empty ())
  3182. {
  3183. std::string klass, method;
  3184. const char *last, *p, *scope_op;
  3185. /* See if we can find a scope operator and break this symbol
  3186. name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
  3187. scope_op = "::";
  3188. p = find_toplevel_string (lookup_name, scope_op);
  3189. last = NULL;
  3190. while (p != NULL)
  3191. {
  3192. last = p;
  3193. p = find_toplevel_string (p + strlen (scope_op), scope_op);
  3194. }
  3195. /* If no scope operator was found, there is nothing more we can do;
  3196. we already attempted to lookup the entire name as a symbol
  3197. and failed. */
  3198. if (last == NULL)
  3199. return;
  3200. /* LOOKUP_NAME points to the class name.
  3201. LAST points to the method name. */
  3202. klass = std::string (lookup_name, last - lookup_name);
  3203. /* Skip past the scope operator. */
  3204. last += strlen (scope_op);
  3205. method = last;
  3206. /* Find a list of classes named KLASS. */
  3207. std::vector<block_symbol> classes
  3208. = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
  3209. if (!classes.empty ())
  3210. {
  3211. /* Now locate a list of suitable methods named METHOD. */
  3212. try
  3213. {
  3214. find_method (state, file_symtabs,
  3215. klass.c_str (), method.c_str (),
  3216. &classes, symbols, minsyms);
  3217. }
  3218. /* If successful, we're done. If NOT_FOUND_ERROR
  3219. was not thrown, rethrow the exception that we did get. */
  3220. catch (const gdb_exception_error &except)
  3221. {
  3222. if (except.error != NOT_FOUND_ERROR)
  3223. throw;
  3224. }
  3225. }
  3226. }
  3227. }
  3228. /* Helper for find_label_symbols. Find all labels that match name
  3229. NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
  3230. Return the actual function symbol in which the label was found in
  3231. LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
  3232. interpreted as a label name prefix. Otherwise, only a label named
  3233. exactly NAME match. */
  3234. static void
  3235. find_label_symbols_in_block (const struct block *block,
  3236. const char *name, struct symbol *fn_sym,
  3237. bool completion_mode,
  3238. std::vector<block_symbol> *result,
  3239. std::vector<block_symbol> *label_funcs_ret)
  3240. {
  3241. if (completion_mode)
  3242. {
  3243. struct block_iterator iter;
  3244. struct symbol *sym;
  3245. size_t name_len = strlen (name);
  3246. int (*cmp) (const char *, const char *, size_t);
  3247. cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
  3248. ALL_BLOCK_SYMBOLS (block, iter, sym)
  3249. {
  3250. if (symbol_matches_domain (sym->language (),
  3251. sym->domain (), LABEL_DOMAIN)
  3252. && cmp (sym->search_name (), name, name_len) == 0)
  3253. {
  3254. result->push_back ({sym, block});
  3255. label_funcs_ret->push_back ({fn_sym, block});
  3256. }
  3257. }
  3258. }
  3259. else
  3260. {
  3261. struct block_symbol label_sym
  3262. = lookup_symbol (name, block, LABEL_DOMAIN, 0);
  3263. if (label_sym.symbol != NULL)
  3264. {
  3265. result->push_back (label_sym);
  3266. label_funcs_ret->push_back ({fn_sym, block});
  3267. }
  3268. }
  3269. }
  3270. /* Return all labels that match name NAME in FUNCTION_SYMBOLS.
  3271. Return the actual function symbol in which the label was found in
  3272. LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
  3273. interpreted as a label name prefix. Otherwise, only labels named
  3274. exactly NAME match. */
  3275. static std::vector<block_symbol>
  3276. find_label_symbols (struct linespec_state *self,
  3277. const std::vector<block_symbol> &function_symbols,
  3278. std::vector<block_symbol> *label_funcs_ret,
  3279. const char *name,
  3280. bool completion_mode)
  3281. {
  3282. const struct block *block;
  3283. struct symbol *fn_sym;
  3284. std::vector<block_symbol> result;
  3285. if (function_symbols.empty ())
  3286. {
  3287. set_current_program_space (self->program_space);
  3288. block = get_current_search_block ();
  3289. for (;
  3290. block && !BLOCK_FUNCTION (block);
  3291. block = BLOCK_SUPERBLOCK (block))
  3292. ;
  3293. if (!block)
  3294. return {};
  3295. fn_sym = BLOCK_FUNCTION (block);
  3296. find_label_symbols_in_block (block, name, fn_sym, completion_mode,
  3297. &result, label_funcs_ret);
  3298. }
  3299. else
  3300. {
  3301. for (const auto &elt : function_symbols)
  3302. {
  3303. fn_sym = elt.symbol;
  3304. set_current_program_space
  3305. (symbol_symtab (fn_sym)->compunit ()->objfile ()->pspace);
  3306. block = SYMBOL_BLOCK_VALUE (fn_sym);
  3307. find_label_symbols_in_block (block, name, fn_sym, completion_mode,
  3308. &result, label_funcs_ret);
  3309. }
  3310. }
  3311. return result;
  3312. }
  3313. /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
  3314. static std::vector<symtab_and_line>
  3315. decode_digits_list_mode (struct linespec_state *self,
  3316. linespec *ls,
  3317. struct symtab_and_line val)
  3318. {
  3319. gdb_assert (self->list_mode);
  3320. std::vector<symtab_and_line> values;
  3321. for (const auto &elt : ls->file_symtabs)
  3322. {
  3323. /* The logic above should ensure this. */
  3324. gdb_assert (elt != NULL);
  3325. program_space *pspace = elt->compunit ()->objfile ()->pspace;
  3326. set_current_program_space (pspace);
  3327. /* Simplistic search just for the list command. */
  3328. val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
  3329. if (val.symtab == NULL)
  3330. val.symtab = elt;
  3331. val.pspace = pspace;
  3332. val.pc = 0;
  3333. val.explicit_line = true;
  3334. add_sal_to_sals (self, &values, &val, NULL, 0);
  3335. }
  3336. return values;
  3337. }
  3338. /* A helper for create_sals_line_offset that iterates over the symtabs
  3339. associated with LS and returns a vector of corresponding symtab_and_line
  3340. structures. */
  3341. static std::vector<symtab_and_line>
  3342. decode_digits_ordinary (struct linespec_state *self,
  3343. linespec *ls,
  3344. int line,
  3345. struct linetable_entry **best_entry)
  3346. {
  3347. std::vector<symtab_and_line> sals;
  3348. for (const auto &elt : ls->file_symtabs)
  3349. {
  3350. std::vector<CORE_ADDR> pcs;
  3351. /* The logic above should ensure this. */
  3352. gdb_assert (elt != NULL);
  3353. program_space *pspace = elt->compunit ()->objfile ()->pspace;
  3354. set_current_program_space (pspace);
  3355. pcs = find_pcs_for_symtab_line (elt, line, best_entry);
  3356. for (CORE_ADDR pc : pcs)
  3357. {
  3358. symtab_and_line sal;
  3359. sal.pspace = pspace;
  3360. sal.symtab = elt;
  3361. sal.line = line;
  3362. sal.explicit_line = true;
  3363. sal.pc = pc;
  3364. sals.push_back (std::move (sal));
  3365. }
  3366. }
  3367. return sals;
  3368. }
  3369. /* Return the line offset represented by VARIABLE. */
  3370. static struct line_offset
  3371. linespec_parse_variable (struct linespec_state *self, const char *variable)
  3372. {
  3373. int index = 0;
  3374. const char *p;
  3375. struct line_offset offset = {0, LINE_OFFSET_NONE};
  3376. p = (variable[1] == '$') ? variable + 2 : variable + 1;
  3377. if (*p == '$')
  3378. ++p;
  3379. while (*p >= '0' && *p <= '9')
  3380. ++p;
  3381. if (!*p) /* Reached end of token without hitting non-digit. */
  3382. {
  3383. /* We have a value history reference. */
  3384. struct value *val_history;
  3385. sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
  3386. val_history
  3387. = access_value_history ((variable[1] == '$') ? -index : index);
  3388. if (value_type (val_history)->code () != TYPE_CODE_INT)
  3389. error (_("History values used in line "
  3390. "specs must have integer values."));
  3391. offset.offset = value_as_long (val_history);
  3392. }
  3393. else
  3394. {
  3395. /* Not all digits -- may be user variable/function or a
  3396. convenience variable. */
  3397. LONGEST valx;
  3398. struct internalvar *ivar;
  3399. /* Try it as a convenience variable. If it is not a convenience
  3400. variable, return and allow normal symbol lookup to occur. */
  3401. ivar = lookup_only_internalvar (variable + 1);
  3402. if (ivar == NULL)
  3403. /* No internal variable with that name. Mark the offset
  3404. as unknown to allow the name to be looked up as a symbol. */
  3405. offset.sign = LINE_OFFSET_UNKNOWN;
  3406. else
  3407. {
  3408. /* We found a valid variable name. If it is not an integer,
  3409. throw an error. */
  3410. if (!get_internalvar_integer (ivar, &valx))
  3411. error (_("Convenience variables used in line "
  3412. "specs must have integer values."));
  3413. else
  3414. offset.offset = valx;
  3415. }
  3416. }
  3417. return offset;
  3418. }
  3419. /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
  3420. linespec; return the SAL in RESULT. This function should return SALs
  3421. matching those from find_function_start_sal, otherwise false
  3422. multiple-locations breakpoints could be placed. */
  3423. static void
  3424. minsym_found (struct linespec_state *self, struct objfile *objfile,
  3425. struct minimal_symbol *msymbol,
  3426. std::vector<symtab_and_line> *result)
  3427. {
  3428. bool want_start_sal;
  3429. CORE_ADDR func_addr;
  3430. bool is_function = msymbol_is_function (objfile, msymbol, &func_addr);
  3431. if (is_function)
  3432. {
  3433. const char *msym_name = msymbol->linkage_name ();
  3434. if (MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
  3435. || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
  3436. want_start_sal = gnu_ifunc_resolve_name (msym_name, &func_addr);
  3437. else
  3438. want_start_sal = true;
  3439. }
  3440. symtab_and_line sal;
  3441. if (is_function && want_start_sal)
  3442. sal = find_function_start_sal (func_addr, NULL, self->funfirstline);
  3443. else
  3444. {
  3445. sal.objfile = objfile;
  3446. sal.msymbol = msymbol;
  3447. /* Store func_addr, not the minsym's address in case this was an
  3448. ifunc that hasn't been resolved yet. */
  3449. if (is_function)
  3450. sal.pc = func_addr;
  3451. else
  3452. sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
  3453. sal.pspace = current_program_space;
  3454. }
  3455. sal.section = msymbol->obj_section (objfile);
  3456. if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
  3457. add_sal_to_sals (self, result, &sal, msymbol->natural_name (), 0);
  3458. }
  3459. /* Helper for search_minsyms_for_name that adds the symbol to the
  3460. result. */
  3461. static void
  3462. add_minsym (struct minimal_symbol *minsym, struct objfile *objfile,
  3463. struct symtab *symtab, int list_mode,
  3464. std::vector<struct bound_minimal_symbol> *msyms)
  3465. {
  3466. if (symtab != NULL)
  3467. {
  3468. /* We're looking for a label for which we don't have debug
  3469. info. */
  3470. CORE_ADDR func_addr;
  3471. if (msymbol_is_function (objfile, minsym, &func_addr))
  3472. {
  3473. symtab_and_line sal = find_pc_sect_line (func_addr, NULL, 0);
  3474. if (symtab != sal.symtab)
  3475. return;
  3476. }
  3477. }
  3478. /* Exclude data symbols when looking for breakpoint locations. */
  3479. if (!list_mode && !msymbol_is_function (objfile, minsym))
  3480. return;
  3481. msyms->emplace_back (minsym, objfile);
  3482. return;
  3483. }
  3484. /* Search for minimal symbols called NAME. If SEARCH_PSPACE
  3485. is not NULL, the search is restricted to just that program
  3486. space.
  3487. If SYMTAB is NULL, search all objfiles, otherwise
  3488. restrict results to the given SYMTAB. */
  3489. static void
  3490. search_minsyms_for_name (struct collect_info *info,
  3491. const lookup_name_info &name,
  3492. struct program_space *search_pspace,
  3493. struct symtab *symtab)
  3494. {
  3495. std::vector<struct bound_minimal_symbol> minsyms;
  3496. if (symtab == NULL)
  3497. {
  3498. for (struct program_space *pspace : program_spaces)
  3499. {
  3500. if (search_pspace != NULL && search_pspace != pspace)
  3501. continue;
  3502. if (pspace->executing_startup)
  3503. continue;
  3504. set_current_program_space (pspace);
  3505. for (objfile *objfile : current_program_space->objfiles ())
  3506. {
  3507. iterate_over_minimal_symbols (objfile, name,
  3508. [&] (struct minimal_symbol *msym)
  3509. {
  3510. add_minsym (msym, objfile, nullptr,
  3511. info->state->list_mode,
  3512. &minsyms);
  3513. return false;
  3514. });
  3515. }
  3516. }
  3517. }
  3518. else
  3519. {
  3520. program_space *pspace = symtab->compunit ()->objfile ()->pspace;
  3521. if (search_pspace == NULL || pspace == search_pspace)
  3522. {
  3523. set_current_program_space (pspace);
  3524. iterate_over_minimal_symbols
  3525. (symtab->compunit ()->objfile (), name,
  3526. [&] (struct minimal_symbol *msym)
  3527. {
  3528. add_minsym (msym, symtab->compunit ()->objfile (), symtab,
  3529. info->state->list_mode, &minsyms);
  3530. return false;
  3531. });
  3532. }
  3533. }
  3534. /* Return true if TYPE is a static symbol. */
  3535. auto msymbol_type_is_static = [] (enum minimal_symbol_type type)
  3536. {
  3537. switch (type)
  3538. {
  3539. case mst_file_text:
  3540. case mst_file_data:
  3541. case mst_file_bss:
  3542. return true;
  3543. default:
  3544. return false;
  3545. }
  3546. };
  3547. /* Add minsyms to the result set, but filter out trampoline symbols
  3548. if we also found extern symbols with the same name. I.e., don't
  3549. set a breakpoint on both '<foo@plt>' and 'foo', assuming that
  3550. 'foo' is the symbol that the plt resolves to. */
  3551. for (const bound_minimal_symbol &item : minsyms)
  3552. {
  3553. bool skip = false;
  3554. if (MSYMBOL_TYPE (item.minsym) == mst_solib_trampoline)
  3555. {
  3556. for (const bound_minimal_symbol &item2 : minsyms)
  3557. {
  3558. if (&item2 == &item)
  3559. continue;
  3560. /* Trampoline symbols can only jump to exported
  3561. symbols. */
  3562. if (msymbol_type_is_static (MSYMBOL_TYPE (item2.minsym)))
  3563. continue;
  3564. if (strcmp (item.minsym->linkage_name (),
  3565. item2.minsym->linkage_name ()) != 0)
  3566. continue;
  3567. /* Found a global minsym with the same name as the
  3568. trampoline. Don't create a location for this
  3569. trampoline. */
  3570. skip = true;
  3571. break;
  3572. }
  3573. }
  3574. if (!skip)
  3575. info->result.minimal_symbols->push_back (item);
  3576. }
  3577. }
  3578. /* A helper function to add all symbols matching NAME to INFO. If
  3579. PSPACE is not NULL, the search is restricted to just that program
  3580. space. */
  3581. static void
  3582. add_matching_symbols_to_info (const char *name,
  3583. symbol_name_match_type name_match_type,
  3584. enum search_domain search_domain,
  3585. struct collect_info *info,
  3586. struct program_space *pspace)
  3587. {
  3588. lookup_name_info lookup_name (name, name_match_type);
  3589. for (const auto &elt : *info->file_symtabs)
  3590. {
  3591. if (elt == nullptr)
  3592. {
  3593. iterate_over_all_matching_symtabs (info->state, lookup_name,
  3594. VAR_DOMAIN, search_domain,
  3595. pspace, true,
  3596. [&] (block_symbol *bsym)
  3597. { return info->add_symbol (bsym); });
  3598. search_minsyms_for_name (info, lookup_name, pspace, NULL);
  3599. }
  3600. else if (pspace == NULL || pspace == elt->compunit ()->objfile ()->pspace)
  3601. {
  3602. int prev_len = info->result.symbols->size ();
  3603. /* Program spaces that are executing startup should have
  3604. been filtered out earlier. */
  3605. program_space *elt_pspace = elt->compunit ()->objfile ()->pspace;
  3606. gdb_assert (!elt_pspace->executing_startup);
  3607. set_current_program_space (elt_pspace);
  3608. iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN,
  3609. [&] (block_symbol *bsym)
  3610. { return info->add_symbol (bsym); });
  3611. /* If no new symbols were found in this iteration and this symtab
  3612. is in assembler, we might actually be looking for a label for
  3613. which we don't have debug info. Check for a minimal symbol in
  3614. this case. */
  3615. if (prev_len == info->result.symbols->size ()
  3616. && elt->language () == language_asm)
  3617. search_minsyms_for_name (info, lookup_name, pspace, elt);
  3618. }
  3619. }
  3620. }
  3621. /* Now come some functions that are called from multiple places within
  3622. decode_line_1. */
  3623. static int
  3624. symbol_to_sal (struct symtab_and_line *result,
  3625. int funfirstline, struct symbol *sym)
  3626. {
  3627. if (sym->aclass () == LOC_BLOCK)
  3628. {
  3629. *result = find_function_start_sal (sym, funfirstline);
  3630. return 1;
  3631. }
  3632. else
  3633. {
  3634. if (sym->aclass () == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
  3635. {
  3636. *result = {};
  3637. result->symtab = symbol_symtab (sym);
  3638. result->symbol = sym;
  3639. result->line = sym->line ();
  3640. result->pc = SYMBOL_VALUE_ADDRESS (sym);
  3641. result->pspace = result->symtab->compunit ()->objfile ()->pspace;
  3642. result->explicit_pc = 1;
  3643. return 1;
  3644. }
  3645. else if (funfirstline)
  3646. {
  3647. /* Nothing. */
  3648. }
  3649. else if (sym->line () != 0)
  3650. {
  3651. /* We know its line number. */
  3652. *result = {};
  3653. result->symtab = symbol_symtab (sym);
  3654. result->symbol = sym;
  3655. result->line = sym->line ();
  3656. result->pc = SYMBOL_VALUE_ADDRESS (sym);
  3657. result->pspace = result->symtab->compunit ()->objfile ()->pspace;
  3658. return 1;
  3659. }
  3660. }
  3661. return 0;
  3662. }
  3663. linespec_result::~linespec_result ()
  3664. {
  3665. for (linespec_sals &lsal : lsals)
  3666. xfree (lsal.canonical);
  3667. }
  3668. /* Return the quote characters permitted by the linespec parser. */
  3669. const char *
  3670. get_gdb_linespec_parser_quote_characters (void)
  3671. {
  3672. return linespec_quote_characters;
  3673. }