symtab.c 205 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147
  1. /* Symbol table lookup 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 "gdbtypes.h"
  17. #include "gdbcore.h"
  18. #include "frame.h"
  19. #include "target.h"
  20. #include "value.h"
  21. #include "symfile.h"
  22. #include "objfiles.h"
  23. #include "gdbcmd.h"
  24. #include "gdbsupport/gdb_regex.h"
  25. #include "expression.h"
  26. #include "language.h"
  27. #include "demangle.h"
  28. #include "inferior.h"
  29. #include "source.h"
  30. #include "filenames.h" /* for FILENAME_CMP */
  31. #include "objc-lang.h"
  32. #include "d-lang.h"
  33. #include "ada-lang.h"
  34. #include "go-lang.h"
  35. #include "p-lang.h"
  36. #include "addrmap.h"
  37. #include "cli/cli-utils.h"
  38. #include "cli/cli-style.h"
  39. #include "cli/cli-cmds.h"
  40. #include "fnmatch.h"
  41. #include "hashtab.h"
  42. #include "typeprint.h"
  43. #include "gdbsupport/gdb_obstack.h"
  44. #include "block.h"
  45. #include "dictionary.h"
  46. #include <sys/types.h>
  47. #include <fcntl.h>
  48. #include <sys/stat.h>
  49. #include <ctype.h>
  50. #include "cp-abi.h"
  51. #include "cp-support.h"
  52. #include "observable.h"
  53. #include "solist.h"
  54. #include "macrotab.h"
  55. #include "macroscope.h"
  56. #include "parser-defs.h"
  57. #include "completer.h"
  58. #include "progspace-and-thread.h"
  59. #include "gdbsupport/gdb_optional.h"
  60. #include "filename-seen-cache.h"
  61. #include "arch-utils.h"
  62. #include <algorithm>
  63. #include "gdbsupport/gdb_string_view.h"
  64. #include "gdbsupport/pathstuff.h"
  65. #include "gdbsupport/common-utils.h"
  66. /* Forward declarations for local functions. */
  67. static void rbreak_command (const char *, int);
  68. static int find_line_common (struct linetable *, int, int *, int);
  69. static struct block_symbol
  70. lookup_symbol_aux (const char *name,
  71. symbol_name_match_type match_type,
  72. const struct block *block,
  73. const domain_enum domain,
  74. enum language language,
  75. struct field_of_this_result *);
  76. static
  77. struct block_symbol lookup_local_symbol (const char *name,
  78. symbol_name_match_type match_type,
  79. const struct block *block,
  80. const domain_enum domain,
  81. enum language language);
  82. static struct block_symbol
  83. lookup_symbol_in_objfile (struct objfile *objfile,
  84. enum block_enum block_index,
  85. const char *name, const domain_enum domain);
  86. /* Type of the data stored on the program space. */
  87. struct main_info
  88. {
  89. main_info () = default;
  90. ~main_info ()
  91. {
  92. xfree (name_of_main);
  93. }
  94. /* Name of "main". */
  95. char *name_of_main = nullptr;
  96. /* Language of "main". */
  97. enum language language_of_main = language_unknown;
  98. };
  99. /* Program space key for finding name and language of "main". */
  100. static const program_space_key<main_info> main_progspace_key;
  101. /* The default symbol cache size.
  102. There is no extra cpu cost for large N (except when flushing the cache,
  103. which is rare). The value here is just a first attempt. A better default
  104. value may be higher or lower. A prime number can make up for a bad hash
  105. computation, so that's why the number is what it is. */
  106. #define DEFAULT_SYMBOL_CACHE_SIZE 1021
  107. /* The maximum symbol cache size.
  108. There's no method to the decision of what value to use here, other than
  109. there's no point in allowing a user typo to make gdb consume all memory. */
  110. #define MAX_SYMBOL_CACHE_SIZE (1024*1024)
  111. /* symbol_cache_lookup returns this if a previous lookup failed to find the
  112. symbol in any objfile. */
  113. #define SYMBOL_LOOKUP_FAILED \
  114. ((struct block_symbol) {(struct symbol *) 1, NULL})
  115. #define SYMBOL_LOOKUP_FAILED_P(SIB) (SIB.symbol == (struct symbol *) 1)
  116. /* Recording lookups that don't find the symbol is just as important, if not
  117. more so, than recording found symbols. */
  118. enum symbol_cache_slot_state
  119. {
  120. SYMBOL_SLOT_UNUSED,
  121. SYMBOL_SLOT_NOT_FOUND,
  122. SYMBOL_SLOT_FOUND
  123. };
  124. struct symbol_cache_slot
  125. {
  126. enum symbol_cache_slot_state state;
  127. /* The objfile that was current when the symbol was looked up.
  128. This is only needed for global blocks, but for simplicity's sake
  129. we allocate the space for both. If data shows the extra space used
  130. for static blocks is a problem, we can split things up then.
  131. Global blocks need cache lookup to include the objfile context because
  132. we need to account for gdbarch_iterate_over_objfiles_in_search_order
  133. which can traverse objfiles in, effectively, any order, depending on
  134. the current objfile, thus affecting which symbol is found. Normally,
  135. only the current objfile is searched first, and then the rest are
  136. searched in recorded order; but putting cache lookup inside
  137. gdbarch_iterate_over_objfiles_in_search_order would be awkward.
  138. Instead we just make the current objfile part of the context of
  139. cache lookup. This means we can record the same symbol multiple times,
  140. each with a different "current objfile" that was in effect when the
  141. lookup was saved in the cache, but cache space is pretty cheap. */
  142. const struct objfile *objfile_context;
  143. union
  144. {
  145. struct block_symbol found;
  146. struct
  147. {
  148. char *name;
  149. domain_enum domain;
  150. } not_found;
  151. } value;
  152. };
  153. /* Clear out SLOT. */
  154. static void
  155. symbol_cache_clear_slot (struct symbol_cache_slot *slot)
  156. {
  157. if (slot->state == SYMBOL_SLOT_NOT_FOUND)
  158. xfree (slot->value.not_found.name);
  159. slot->state = SYMBOL_SLOT_UNUSED;
  160. }
  161. /* Symbols don't specify global vs static block.
  162. So keep them in separate caches. */
  163. struct block_symbol_cache
  164. {
  165. unsigned int hits;
  166. unsigned int misses;
  167. unsigned int collisions;
  168. /* SYMBOLS is a variable length array of this size.
  169. One can imagine that in general one cache (global/static) should be a
  170. fraction of the size of the other, but there's no data at the moment
  171. on which to decide. */
  172. unsigned int size;
  173. struct symbol_cache_slot symbols[1];
  174. };
  175. /* Clear all slots of BSC and free BSC. */
  176. static void
  177. destroy_block_symbol_cache (struct block_symbol_cache *bsc)
  178. {
  179. if (bsc != nullptr)
  180. {
  181. for (unsigned int i = 0; i < bsc->size; i++)
  182. symbol_cache_clear_slot (&bsc->symbols[i]);
  183. xfree (bsc);
  184. }
  185. }
  186. /* The symbol cache.
  187. Searching for symbols in the static and global blocks over multiple objfiles
  188. again and again can be slow, as can searching very big objfiles. This is a
  189. simple cache to improve symbol lookup performance, which is critical to
  190. overall gdb performance.
  191. Symbols are hashed on the name, its domain, and block.
  192. They are also hashed on their objfile for objfile-specific lookups. */
  193. struct symbol_cache
  194. {
  195. symbol_cache () = default;
  196. ~symbol_cache ()
  197. {
  198. destroy_block_symbol_cache (global_symbols);
  199. destroy_block_symbol_cache (static_symbols);
  200. }
  201. struct block_symbol_cache *global_symbols = nullptr;
  202. struct block_symbol_cache *static_symbols = nullptr;
  203. };
  204. /* Program space key for finding its symbol cache. */
  205. static const program_space_key<symbol_cache> symbol_cache_key;
  206. /* When non-zero, print debugging messages related to symtab creation. */
  207. unsigned int symtab_create_debug = 0;
  208. /* When non-zero, print debugging messages related to symbol lookup. */
  209. unsigned int symbol_lookup_debug = 0;
  210. /* The size of the cache is staged here. */
  211. static unsigned int new_symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
  212. /* The current value of the symbol cache size.
  213. This is saved so that if the user enters a value too big we can restore
  214. the original value from here. */
  215. static unsigned int symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
  216. /* True if a file may be known by two different basenames.
  217. This is the uncommon case, and significantly slows down gdb.
  218. Default set to "off" to not slow down the common case. */
  219. bool basenames_may_differ = false;
  220. /* Allow the user to configure the debugger behavior with respect
  221. to multiple-choice menus when more than one symbol matches during
  222. a symbol lookup. */
  223. const char multiple_symbols_ask[] = "ask";
  224. const char multiple_symbols_all[] = "all";
  225. const char multiple_symbols_cancel[] = "cancel";
  226. static const char *const multiple_symbols_modes[] =
  227. {
  228. multiple_symbols_ask,
  229. multiple_symbols_all,
  230. multiple_symbols_cancel,
  231. NULL
  232. };
  233. static const char *multiple_symbols_mode = multiple_symbols_all;
  234. /* When TRUE, ignore the prologue-end flag in linetable_entry when searching
  235. for the SAL past a function prologue. */
  236. static bool ignore_prologue_end_flag = false;
  237. /* Read-only accessor to AUTO_SELECT_MODE. */
  238. const char *
  239. multiple_symbols_select_mode (void)
  240. {
  241. return multiple_symbols_mode;
  242. }
  243. /* Return the name of a domain_enum. */
  244. const char *
  245. domain_name (domain_enum e)
  246. {
  247. switch (e)
  248. {
  249. case UNDEF_DOMAIN: return "UNDEF_DOMAIN";
  250. case VAR_DOMAIN: return "VAR_DOMAIN";
  251. case STRUCT_DOMAIN: return "STRUCT_DOMAIN";
  252. case MODULE_DOMAIN: return "MODULE_DOMAIN";
  253. case LABEL_DOMAIN: return "LABEL_DOMAIN";
  254. case COMMON_BLOCK_DOMAIN: return "COMMON_BLOCK_DOMAIN";
  255. default: gdb_assert_not_reached ("bad domain_enum");
  256. }
  257. }
  258. /* Return the name of a search_domain . */
  259. const char *
  260. search_domain_name (enum search_domain e)
  261. {
  262. switch (e)
  263. {
  264. case VARIABLES_DOMAIN: return "VARIABLES_DOMAIN";
  265. case FUNCTIONS_DOMAIN: return "FUNCTIONS_DOMAIN";
  266. case TYPES_DOMAIN: return "TYPES_DOMAIN";
  267. case MODULES_DOMAIN: return "MODULES_DOMAIN";
  268. case ALL_DOMAIN: return "ALL_DOMAIN";
  269. default: gdb_assert_not_reached ("bad search_domain");
  270. }
  271. }
  272. /* See symtab.h. */
  273. call_site *
  274. compunit_symtab::find_call_site (CORE_ADDR pc) const
  275. {
  276. if (m_call_site_htab == nullptr)
  277. return nullptr;
  278. CORE_ADDR delta
  279. = this->objfile ()->section_offsets[this->block_line_section ()];
  280. CORE_ADDR unrelocated_pc = pc - delta;
  281. struct call_site call_site_local (unrelocated_pc, nullptr, nullptr);
  282. void **slot
  283. = htab_find_slot (m_call_site_htab, &call_site_local, NO_INSERT);
  284. if (slot == nullptr)
  285. return nullptr;
  286. return (call_site *) *slot;
  287. }
  288. /* See symtab.h. */
  289. void
  290. compunit_symtab::set_call_site_htab (htab_t call_site_htab)
  291. {
  292. gdb_assert (m_call_site_htab == nullptr);
  293. m_call_site_htab = call_site_htab;
  294. }
  295. /* See symtab.h. */
  296. void
  297. compunit_symtab::set_primary_filetab (symtab *primary_filetab)
  298. {
  299. symtab *prev_filetab = nullptr;
  300. /* Move PRIMARY_FILETAB to the head of the filetab list. */
  301. for (symtab *filetab : this->filetabs ())
  302. {
  303. if (filetab == primary_filetab)
  304. {
  305. if (prev_filetab != nullptr)
  306. {
  307. prev_filetab->next = primary_filetab->next;
  308. primary_filetab->next = m_filetabs;
  309. m_filetabs = primary_filetab;
  310. }
  311. break;
  312. }
  313. prev_filetab = filetab;
  314. }
  315. gdb_assert (primary_filetab == m_filetabs);
  316. }
  317. /* See symtab.h. */
  318. struct symtab *
  319. compunit_symtab::primary_filetab () const
  320. {
  321. gdb_assert (m_filetabs != nullptr);
  322. /* The primary file symtab is the first one in the list. */
  323. return m_filetabs;
  324. }
  325. /* See symtab.h. */
  326. enum language
  327. compunit_language (const struct compunit_symtab *cust)
  328. {
  329. struct symtab *symtab = cust->primary_filetab ();
  330. /* The language of the compunit symtab is the language of its primary
  331. source file. */
  332. return symtab->language ();
  333. }
  334. /* See symtab.h. */
  335. bool
  336. minimal_symbol::data_p () const
  337. {
  338. return type == mst_data
  339. || type == mst_bss
  340. || type == mst_abs
  341. || type == mst_file_data
  342. || type == mst_file_bss;
  343. }
  344. /* See symtab.h. */
  345. bool
  346. minimal_symbol::text_p () const
  347. {
  348. return type == mst_text
  349. || type == mst_text_gnu_ifunc
  350. || type == mst_data_gnu_ifunc
  351. || type == mst_slot_got_plt
  352. || type == mst_solib_trampoline
  353. || type == mst_file_text;
  354. }
  355. /* See whether FILENAME matches SEARCH_NAME using the rule that we
  356. advertise to the user. (The manual's description of linespecs
  357. describes what we advertise). Returns true if they match, false
  358. otherwise. */
  359. bool
  360. compare_filenames_for_search (const char *filename, const char *search_name)
  361. {
  362. int len = strlen (filename);
  363. size_t search_len = strlen (search_name);
  364. if (len < search_len)
  365. return false;
  366. /* The tail of FILENAME must match. */
  367. if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
  368. return false;
  369. /* Either the names must completely match, or the character
  370. preceding the trailing SEARCH_NAME segment of FILENAME must be a
  371. directory separator.
  372. The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
  373. cannot match FILENAME "/path//dir/file.c" - as user has requested
  374. absolute path. The sama applies for "c:\file.c" possibly
  375. incorrectly hypothetically matching "d:\dir\c:\file.c".
  376. The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
  377. compatible with SEARCH_NAME "file.c". In such case a compiler had
  378. to put the "c:file.c" name into debug info. Such compatibility
  379. works only on GDB built for DOS host. */
  380. return (len == search_len
  381. || (!IS_ABSOLUTE_PATH (search_name)
  382. && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
  383. || (HAS_DRIVE_SPEC (filename)
  384. && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
  385. }
  386. /* Same as compare_filenames_for_search, but for glob-style patterns.
  387. Heads up on the order of the arguments. They match the order of
  388. compare_filenames_for_search, but it's the opposite of the order of
  389. arguments to gdb_filename_fnmatch. */
  390. bool
  391. compare_glob_filenames_for_search (const char *filename,
  392. const char *search_name)
  393. {
  394. /* We rely on the property of glob-style patterns with FNM_FILE_NAME that
  395. all /s have to be explicitly specified. */
  396. int file_path_elements = count_path_elements (filename);
  397. int search_path_elements = count_path_elements (search_name);
  398. if (search_path_elements > file_path_elements)
  399. return false;
  400. if (IS_ABSOLUTE_PATH (search_name))
  401. {
  402. return (search_path_elements == file_path_elements
  403. && gdb_filename_fnmatch (search_name, filename,
  404. FNM_FILE_NAME | FNM_NOESCAPE) == 0);
  405. }
  406. {
  407. const char *file_to_compare
  408. = strip_leading_path_elements (filename,
  409. file_path_elements - search_path_elements);
  410. return gdb_filename_fnmatch (search_name, file_to_compare,
  411. FNM_FILE_NAME | FNM_NOESCAPE) == 0;
  412. }
  413. }
  414. /* Check for a symtab of a specific name by searching some symtabs.
  415. This is a helper function for callbacks of iterate_over_symtabs.
  416. If NAME is not absolute, then REAL_PATH is NULL
  417. If NAME is absolute, then REAL_PATH is the gdb_realpath form of NAME.
  418. The return value, NAME, REAL_PATH and CALLBACK are identical to the
  419. `map_symtabs_matching_filename' method of quick_symbol_functions.
  420. FIRST and AFTER_LAST indicate the range of compunit symtabs to search.
  421. Each symtab within the specified compunit symtab is also searched.
  422. AFTER_LAST is one past the last compunit symtab to search; NULL means to
  423. search until the end of the list. */
  424. bool
  425. iterate_over_some_symtabs (const char *name,
  426. const char *real_path,
  427. struct compunit_symtab *first,
  428. struct compunit_symtab *after_last,
  429. gdb::function_view<bool (symtab *)> callback)
  430. {
  431. struct compunit_symtab *cust;
  432. const char* base_name = lbasename (name);
  433. for (cust = first; cust != NULL && cust != after_last; cust = cust->next)
  434. {
  435. for (symtab *s : cust->filetabs ())
  436. {
  437. if (compare_filenames_for_search (s->filename, name))
  438. {
  439. if (callback (s))
  440. return true;
  441. continue;
  442. }
  443. /* Before we invoke realpath, which can get expensive when many
  444. files are involved, do a quick comparison of the basenames. */
  445. if (! basenames_may_differ
  446. && FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
  447. continue;
  448. if (compare_filenames_for_search (symtab_to_fullname (s), name))
  449. {
  450. if (callback (s))
  451. return true;
  452. continue;
  453. }
  454. /* If the user gave us an absolute path, try to find the file in
  455. this symtab and use its absolute path. */
  456. if (real_path != NULL)
  457. {
  458. const char *fullname = symtab_to_fullname (s);
  459. gdb_assert (IS_ABSOLUTE_PATH (real_path));
  460. gdb_assert (IS_ABSOLUTE_PATH (name));
  461. gdb::unique_xmalloc_ptr<char> fullname_real_path
  462. = gdb_realpath (fullname);
  463. fullname = fullname_real_path.get ();
  464. if (FILENAME_CMP (real_path, fullname) == 0)
  465. {
  466. if (callback (s))
  467. return true;
  468. continue;
  469. }
  470. }
  471. }
  472. }
  473. return false;
  474. }
  475. /* Check for a symtab of a specific name; first in symtabs, then in
  476. psymtabs. *If* there is no '/' in the name, a match after a '/'
  477. in the symtab filename will also work.
  478. Calls CALLBACK with each symtab that is found. If CALLBACK returns
  479. true, the search stops. */
  480. void
  481. iterate_over_symtabs (const char *name,
  482. gdb::function_view<bool (symtab *)> callback)
  483. {
  484. gdb::unique_xmalloc_ptr<char> real_path;
  485. /* Here we are interested in canonicalizing an absolute path, not
  486. absolutizing a relative path. */
  487. if (IS_ABSOLUTE_PATH (name))
  488. {
  489. real_path = gdb_realpath (name);
  490. gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
  491. }
  492. for (objfile *objfile : current_program_space->objfiles ())
  493. {
  494. if (iterate_over_some_symtabs (name, real_path.get (),
  495. objfile->compunit_symtabs, NULL,
  496. callback))
  497. return;
  498. }
  499. /* Same search rules as above apply here, but now we look thru the
  500. psymtabs. */
  501. for (objfile *objfile : current_program_space->objfiles ())
  502. {
  503. if (objfile->map_symtabs_matching_filename (name, real_path.get (),
  504. callback))
  505. return;
  506. }
  507. }
  508. /* A wrapper for iterate_over_symtabs that returns the first matching
  509. symtab, or NULL. */
  510. struct symtab *
  511. lookup_symtab (const char *name)
  512. {
  513. struct symtab *result = NULL;
  514. iterate_over_symtabs (name, [&] (symtab *symtab)
  515. {
  516. result = symtab;
  517. return true;
  518. });
  519. return result;
  520. }
  521. /* Mangle a GDB method stub type. This actually reassembles the pieces of the
  522. full method name, which consist of the class name (from T), the unadorned
  523. method name from METHOD_ID, and the signature for the specific overload,
  524. specified by SIGNATURE_ID. Note that this function is g++ specific. */
  525. char *
  526. gdb_mangle_name (struct type *type, int method_id, int signature_id)
  527. {
  528. int mangled_name_len;
  529. char *mangled_name;
  530. struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
  531. struct fn_field *method = &f[signature_id];
  532. const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
  533. const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
  534. const char *newname = type->name ();
  535. /* Does the form of physname indicate that it is the full mangled name
  536. of a constructor (not just the args)? */
  537. int is_full_physname_constructor;
  538. int is_constructor;
  539. int is_destructor = is_destructor_name (physname);
  540. /* Need a new type prefix. */
  541. const char *const_prefix = method->is_const ? "C" : "";
  542. const char *volatile_prefix = method->is_volatile ? "V" : "";
  543. char buf[20];
  544. int len = (newname == NULL ? 0 : strlen (newname));
  545. /* Nothing to do if physname already contains a fully mangled v3 abi name
  546. or an operator name. */
  547. if ((physname[0] == '_' && physname[1] == 'Z')
  548. || is_operator_name (field_name))
  549. return xstrdup (physname);
  550. is_full_physname_constructor = is_constructor_name (physname);
  551. is_constructor = is_full_physname_constructor
  552. || (newname && strcmp (field_name, newname) == 0);
  553. if (!is_destructor)
  554. is_destructor = (startswith (physname, "__dt"));
  555. if (is_destructor || is_full_physname_constructor)
  556. {
  557. mangled_name = (char *) xmalloc (strlen (physname) + 1);
  558. strcpy (mangled_name, physname);
  559. return mangled_name;
  560. }
  561. if (len == 0)
  562. {
  563. xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
  564. }
  565. else if (physname[0] == 't' || physname[0] == 'Q')
  566. {
  567. /* The physname for template and qualified methods already includes
  568. the class name. */
  569. xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
  570. newname = NULL;
  571. len = 0;
  572. }
  573. else
  574. {
  575. xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
  576. volatile_prefix, len);
  577. }
  578. mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
  579. + strlen (buf) + len + strlen (physname) + 1);
  580. mangled_name = (char *) xmalloc (mangled_name_len);
  581. if (is_constructor)
  582. mangled_name[0] = '\0';
  583. else
  584. strcpy (mangled_name, field_name);
  585. strcat (mangled_name, buf);
  586. /* If the class doesn't have a name, i.e. newname NULL, then we just
  587. mangle it using 0 for the length of the class. Thus it gets mangled
  588. as something starting with `::' rather than `classname::'. */
  589. if (newname != NULL)
  590. strcat (mangled_name, newname);
  591. strcat (mangled_name, physname);
  592. return (mangled_name);
  593. }
  594. /* See symtab.h. */
  595. void
  596. general_symbol_info::set_demangled_name (const char *name,
  597. struct obstack *obstack)
  598. {
  599. if (language () == language_ada)
  600. {
  601. if (name == NULL)
  602. {
  603. ada_mangled = 0;
  604. language_specific.obstack = obstack;
  605. }
  606. else
  607. {
  608. ada_mangled = 1;
  609. language_specific.demangled_name = name;
  610. }
  611. }
  612. else
  613. language_specific.demangled_name = name;
  614. }
  615. /* Initialize the language dependent portion of a symbol
  616. depending upon the language for the symbol. */
  617. void
  618. general_symbol_info::set_language (enum language language,
  619. struct obstack *obstack)
  620. {
  621. m_language = language;
  622. if (language == language_cplus
  623. || language == language_d
  624. || language == language_go
  625. || language == language_objc
  626. || language == language_fortran)
  627. {
  628. set_demangled_name (NULL, obstack);
  629. }
  630. else if (language == language_ada)
  631. {
  632. gdb_assert (ada_mangled == 0);
  633. language_specific.obstack = obstack;
  634. }
  635. else
  636. {
  637. memset (&language_specific, 0, sizeof (language_specific));
  638. }
  639. }
  640. /* Functions to initialize a symbol's mangled name. */
  641. /* Objects of this type are stored in the demangled name hash table. */
  642. struct demangled_name_entry
  643. {
  644. demangled_name_entry (gdb::string_view mangled_name)
  645. : mangled (mangled_name) {}
  646. gdb::string_view mangled;
  647. enum language language;
  648. gdb::unique_xmalloc_ptr<char> demangled;
  649. };
  650. /* Hash function for the demangled name hash. */
  651. static hashval_t
  652. hash_demangled_name_entry (const void *data)
  653. {
  654. const struct demangled_name_entry *e
  655. = (const struct demangled_name_entry *) data;
  656. return fast_hash (e->mangled.data (), e->mangled.length ());
  657. }
  658. /* Equality function for the demangled name hash. */
  659. static int
  660. eq_demangled_name_entry (const void *a, const void *b)
  661. {
  662. const struct demangled_name_entry *da
  663. = (const struct demangled_name_entry *) a;
  664. const struct demangled_name_entry *db
  665. = (const struct demangled_name_entry *) b;
  666. return da->mangled == db->mangled;
  667. }
  668. static void
  669. free_demangled_name_entry (void *data)
  670. {
  671. struct demangled_name_entry *e
  672. = (struct demangled_name_entry *) data;
  673. e->~demangled_name_entry();
  674. }
  675. /* Create the hash table used for demangled names. Each hash entry is
  676. a pair of strings; one for the mangled name and one for the demangled
  677. name. The entry is hashed via just the mangled name. */
  678. static void
  679. create_demangled_names_hash (struct objfile_per_bfd_storage *per_bfd)
  680. {
  681. /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
  682. The hash table code will round this up to the next prime number.
  683. Choosing a much larger table size wastes memory, and saves only about
  684. 1% in symbol reading. However, if the minsym count is already
  685. initialized (e.g. because symbol name setting was deferred to
  686. a background thread) we can initialize the hashtable with a count
  687. based on that, because we will almost certainly have at least that
  688. many entries. If we have a nonzero number but less than 256,
  689. we still stay with 256 to have some space for psymbols, etc. */
  690. /* htab will expand the table when it is 3/4th full, so we account for that
  691. here. +2 to round up. */
  692. int minsym_based_count = (per_bfd->minimal_symbol_count + 2) / 3 * 4;
  693. int count = std::max (per_bfd->minimal_symbol_count, minsym_based_count);
  694. per_bfd->demangled_names_hash.reset (htab_create_alloc
  695. (count, hash_demangled_name_entry, eq_demangled_name_entry,
  696. free_demangled_name_entry, xcalloc, xfree));
  697. }
  698. /* See symtab.h */
  699. gdb::unique_xmalloc_ptr<char>
  700. symbol_find_demangled_name (struct general_symbol_info *gsymbol,
  701. const char *mangled)
  702. {
  703. gdb::unique_xmalloc_ptr<char> demangled;
  704. int i;
  705. if (gsymbol->language () == language_unknown)
  706. gsymbol->m_language = language_auto;
  707. if (gsymbol->language () != language_auto)
  708. {
  709. const struct language_defn *lang = language_def (gsymbol->language ());
  710. lang->sniff_from_mangled_name (mangled, &demangled);
  711. return demangled;
  712. }
  713. for (i = language_unknown; i < nr_languages; ++i)
  714. {
  715. enum language l = (enum language) i;
  716. const struct language_defn *lang = language_def (l);
  717. if (lang->sniff_from_mangled_name (mangled, &demangled))
  718. {
  719. gsymbol->m_language = l;
  720. return demangled;
  721. }
  722. }
  723. return NULL;
  724. }
  725. /* Set both the mangled and demangled (if any) names for GSYMBOL based
  726. on LINKAGE_NAME and LEN. Ordinarily, NAME is copied onto the
  727. objfile's obstack; but if COPY_NAME is 0 and if NAME is
  728. NUL-terminated, then this function assumes that NAME is already
  729. correctly saved (either permanently or with a lifetime tied to the
  730. objfile), and it will not be copied.
  731. The hash table corresponding to OBJFILE is used, and the memory
  732. comes from the per-BFD storage_obstack. LINKAGE_NAME is copied,
  733. so the pointer can be discarded after calling this function. */
  734. void
  735. general_symbol_info::compute_and_set_names (gdb::string_view linkage_name,
  736. bool copy_name,
  737. objfile_per_bfd_storage *per_bfd,
  738. gdb::optional<hashval_t> hash)
  739. {
  740. struct demangled_name_entry **slot;
  741. if (language () == language_ada)
  742. {
  743. /* In Ada, we do the symbol lookups using the mangled name, so
  744. we can save some space by not storing the demangled name. */
  745. if (!copy_name)
  746. m_name = linkage_name.data ();
  747. else
  748. m_name = obstack_strndup (&per_bfd->storage_obstack,
  749. linkage_name.data (),
  750. linkage_name.length ());
  751. set_demangled_name (NULL, &per_bfd->storage_obstack);
  752. return;
  753. }
  754. if (per_bfd->demangled_names_hash == NULL)
  755. create_demangled_names_hash (per_bfd);
  756. struct demangled_name_entry entry (linkage_name);
  757. if (!hash.has_value ())
  758. hash = hash_demangled_name_entry (&entry);
  759. slot = ((struct demangled_name_entry **)
  760. htab_find_slot_with_hash (per_bfd->demangled_names_hash.get (),
  761. &entry, *hash, INSERT));
  762. /* The const_cast is safe because the only reason it is already
  763. initialized is if we purposefully set it from a background
  764. thread to avoid doing the work here. However, it is still
  765. allocated from the heap and needs to be freed by us, just
  766. like if we called symbol_find_demangled_name here. If this is
  767. nullptr, we call symbol_find_demangled_name below, but we put
  768. this smart pointer here to be sure that we don't leak this name. */
  769. gdb::unique_xmalloc_ptr<char> demangled_name
  770. (const_cast<char *> (language_specific.demangled_name));
  771. /* If this name is not in the hash table, add it. */
  772. if (*slot == NULL
  773. /* A C version of the symbol may have already snuck into the table.
  774. This happens to, e.g., main.init (__go_init_main). Cope. */
  775. || (language () == language_go && (*slot)->demangled == nullptr))
  776. {
  777. /* A 0-terminated copy of the linkage name. Callers must set COPY_NAME
  778. to true if the string might not be nullterminated. We have to make
  779. this copy because demangling needs a nullterminated string. */
  780. gdb::string_view linkage_name_copy;
  781. if (copy_name)
  782. {
  783. char *alloc_name = (char *) alloca (linkage_name.length () + 1);
  784. memcpy (alloc_name, linkage_name.data (), linkage_name.length ());
  785. alloc_name[linkage_name.length ()] = '\0';
  786. linkage_name_copy = gdb::string_view (alloc_name,
  787. linkage_name.length ());
  788. }
  789. else
  790. linkage_name_copy = linkage_name;
  791. if (demangled_name.get () == nullptr)
  792. demangled_name
  793. = symbol_find_demangled_name (this, linkage_name_copy.data ());
  794. /* Suppose we have demangled_name==NULL, copy_name==0, and
  795. linkage_name_copy==linkage_name. In this case, we already have the
  796. mangled name saved, and we don't have a demangled name. So,
  797. you might think we could save a little space by not recording
  798. this in the hash table at all.
  799. It turns out that it is actually important to still save such
  800. an entry in the hash table, because storing this name gives
  801. us better bcache hit rates for partial symbols. */
  802. if (!copy_name)
  803. {
  804. *slot
  805. = ((struct demangled_name_entry *)
  806. obstack_alloc (&per_bfd->storage_obstack,
  807. sizeof (demangled_name_entry)));
  808. new (*slot) demangled_name_entry (linkage_name);
  809. }
  810. else
  811. {
  812. /* If we must copy the mangled name, put it directly after
  813. the struct so we can have a single allocation. */
  814. *slot
  815. = ((struct demangled_name_entry *)
  816. obstack_alloc (&per_bfd->storage_obstack,
  817. sizeof (demangled_name_entry)
  818. + linkage_name.length () + 1));
  819. char *mangled_ptr = reinterpret_cast<char *> (*slot + 1);
  820. memcpy (mangled_ptr, linkage_name.data (), linkage_name.length ());
  821. mangled_ptr [linkage_name.length ()] = '\0';
  822. new (*slot) demangled_name_entry
  823. (gdb::string_view (mangled_ptr, linkage_name.length ()));
  824. }
  825. (*slot)->demangled = std::move (demangled_name);
  826. (*slot)->language = language ();
  827. }
  828. else if (language () == language_unknown || language () == language_auto)
  829. m_language = (*slot)->language;
  830. m_name = (*slot)->mangled.data ();
  831. set_demangled_name ((*slot)->demangled.get (), &per_bfd->storage_obstack);
  832. }
  833. /* See symtab.h. */
  834. const char *
  835. general_symbol_info::natural_name () const
  836. {
  837. switch (language ())
  838. {
  839. case language_cplus:
  840. case language_d:
  841. case language_go:
  842. case language_objc:
  843. case language_fortran:
  844. case language_rust:
  845. if (language_specific.demangled_name != nullptr)
  846. return language_specific.demangled_name;
  847. break;
  848. case language_ada:
  849. return ada_decode_symbol (this);
  850. default:
  851. break;
  852. }
  853. return linkage_name ();
  854. }
  855. /* See symtab.h. */
  856. const char *
  857. general_symbol_info::demangled_name () const
  858. {
  859. const char *dem_name = NULL;
  860. switch (language ())
  861. {
  862. case language_cplus:
  863. case language_d:
  864. case language_go:
  865. case language_objc:
  866. case language_fortran:
  867. case language_rust:
  868. dem_name = language_specific.demangled_name;
  869. break;
  870. case language_ada:
  871. dem_name = ada_decode_symbol (this);
  872. break;
  873. default:
  874. break;
  875. }
  876. return dem_name;
  877. }
  878. /* See symtab.h. */
  879. const char *
  880. general_symbol_info::search_name () const
  881. {
  882. if (language () == language_ada)
  883. return linkage_name ();
  884. else
  885. return natural_name ();
  886. }
  887. /* See symtab.h. */
  888. struct obj_section *
  889. general_symbol_info::obj_section (const struct objfile *objfile) const
  890. {
  891. if (section_index () >= 0)
  892. return &objfile->sections[section_index ()];
  893. return nullptr;
  894. }
  895. /* See symtab.h. */
  896. bool
  897. symbol_matches_search_name (const struct general_symbol_info *gsymbol,
  898. const lookup_name_info &name)
  899. {
  900. symbol_name_matcher_ftype *name_match
  901. = language_def (gsymbol->language ())->get_symbol_name_matcher (name);
  902. return name_match (gsymbol->search_name (), name, NULL);
  903. }
  904. /* Return true if the two sections are the same, or if they could
  905. plausibly be copies of each other, one in an original object
  906. file and another in a separated debug file. */
  907. bool
  908. matching_obj_sections (struct obj_section *obj_first,
  909. struct obj_section *obj_second)
  910. {
  911. asection *first = obj_first? obj_first->the_bfd_section : NULL;
  912. asection *second = obj_second? obj_second->the_bfd_section : NULL;
  913. /* If they're the same section, then they match. */
  914. if (first == second)
  915. return true;
  916. /* If either is NULL, give up. */
  917. if (first == NULL || second == NULL)
  918. return false;
  919. /* This doesn't apply to absolute symbols. */
  920. if (first->owner == NULL || second->owner == NULL)
  921. return false;
  922. /* If they're in the same object file, they must be different sections. */
  923. if (first->owner == second->owner)
  924. return false;
  925. /* Check whether the two sections are potentially corresponding. They must
  926. have the same size, address, and name. We can't compare section indexes,
  927. which would be more reliable, because some sections may have been
  928. stripped. */
  929. if (bfd_section_size (first) != bfd_section_size (second))
  930. return false;
  931. /* In-memory addresses may start at a different offset, relativize them. */
  932. if (bfd_section_vma (first) - bfd_get_start_address (first->owner)
  933. != bfd_section_vma (second) - bfd_get_start_address (second->owner))
  934. return false;
  935. if (bfd_section_name (first) == NULL
  936. || bfd_section_name (second) == NULL
  937. || strcmp (bfd_section_name (first), bfd_section_name (second)) != 0)
  938. return false;
  939. /* Otherwise check that they are in corresponding objfiles. */
  940. struct objfile *obj = NULL;
  941. for (objfile *objfile : current_program_space->objfiles ())
  942. if (objfile->obfd == first->owner)
  943. {
  944. obj = objfile;
  945. break;
  946. }
  947. gdb_assert (obj != NULL);
  948. if (obj->separate_debug_objfile != NULL
  949. && obj->separate_debug_objfile->obfd == second->owner)
  950. return true;
  951. if (obj->separate_debug_objfile_backlink != NULL
  952. && obj->separate_debug_objfile_backlink->obfd == second->owner)
  953. return true;
  954. return false;
  955. }
  956. /* See symtab.h. */
  957. void
  958. expand_symtab_containing_pc (CORE_ADDR pc, struct obj_section *section)
  959. {
  960. struct bound_minimal_symbol msymbol;
  961. /* If we know that this is not a text address, return failure. This is
  962. necessary because we loop based on texthigh and textlow, which do
  963. not include the data ranges. */
  964. msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
  965. if (msymbol.minsym && msymbol.minsym->data_p ())
  966. return;
  967. for (objfile *objfile : current_program_space->objfiles ())
  968. {
  969. struct compunit_symtab *cust
  970. = objfile->find_pc_sect_compunit_symtab (msymbol, pc, section, 0);
  971. if (cust)
  972. return;
  973. }
  974. }
  975. /* Hash function for the symbol cache. */
  976. static unsigned int
  977. hash_symbol_entry (const struct objfile *objfile_context,
  978. const char *name, domain_enum domain)
  979. {
  980. unsigned int hash = (uintptr_t) objfile_context;
  981. if (name != NULL)
  982. hash += htab_hash_string (name);
  983. /* Because of symbol_matches_domain we need VAR_DOMAIN and STRUCT_DOMAIN
  984. to map to the same slot. */
  985. if (domain == STRUCT_DOMAIN)
  986. hash += VAR_DOMAIN * 7;
  987. else
  988. hash += domain * 7;
  989. return hash;
  990. }
  991. /* Equality function for the symbol cache. */
  992. static int
  993. eq_symbol_entry (const struct symbol_cache_slot *slot,
  994. const struct objfile *objfile_context,
  995. const char *name, domain_enum domain)
  996. {
  997. const char *slot_name;
  998. domain_enum slot_domain;
  999. if (slot->state == SYMBOL_SLOT_UNUSED)
  1000. return 0;
  1001. if (slot->objfile_context != objfile_context)
  1002. return 0;
  1003. if (slot->state == SYMBOL_SLOT_NOT_FOUND)
  1004. {
  1005. slot_name = slot->value.not_found.name;
  1006. slot_domain = slot->value.not_found.domain;
  1007. }
  1008. else
  1009. {
  1010. slot_name = slot->value.found.symbol->search_name ();
  1011. slot_domain = slot->value.found.symbol->domain ();
  1012. }
  1013. /* NULL names match. */
  1014. if (slot_name == NULL && name == NULL)
  1015. {
  1016. /* But there's no point in calling symbol_matches_domain in the
  1017. SYMBOL_SLOT_FOUND case. */
  1018. if (slot_domain != domain)
  1019. return 0;
  1020. }
  1021. else if (slot_name != NULL && name != NULL)
  1022. {
  1023. /* It's important that we use the same comparison that was done
  1024. the first time through. If the slot records a found symbol,
  1025. then this means using the symbol name comparison function of
  1026. the symbol's language with symbol->search_name (). See
  1027. dictionary.c. It also means using symbol_matches_domain for
  1028. found symbols. See block.c.
  1029. If the slot records a not-found symbol, then require a precise match.
  1030. We could still be lax with whitespace like strcmp_iw though. */
  1031. if (slot->state == SYMBOL_SLOT_NOT_FOUND)
  1032. {
  1033. if (strcmp (slot_name, name) != 0)
  1034. return 0;
  1035. if (slot_domain != domain)
  1036. return 0;
  1037. }
  1038. else
  1039. {
  1040. struct symbol *sym = slot->value.found.symbol;
  1041. lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
  1042. if (!symbol_matches_search_name (sym, lookup_name))
  1043. return 0;
  1044. if (!symbol_matches_domain (sym->language (), slot_domain, domain))
  1045. return 0;
  1046. }
  1047. }
  1048. else
  1049. {
  1050. /* Only one name is NULL. */
  1051. return 0;
  1052. }
  1053. return 1;
  1054. }
  1055. /* Given a cache of size SIZE, return the size of the struct (with variable
  1056. length array) in bytes. */
  1057. static size_t
  1058. symbol_cache_byte_size (unsigned int size)
  1059. {
  1060. return (sizeof (struct block_symbol_cache)
  1061. + ((size - 1) * sizeof (struct symbol_cache_slot)));
  1062. }
  1063. /* Resize CACHE. */
  1064. static void
  1065. resize_symbol_cache (struct symbol_cache *cache, unsigned int new_size)
  1066. {
  1067. /* If there's no change in size, don't do anything.
  1068. All caches have the same size, so we can just compare with the size
  1069. of the global symbols cache. */
  1070. if ((cache->global_symbols != NULL
  1071. && cache->global_symbols->size == new_size)
  1072. || (cache->global_symbols == NULL
  1073. && new_size == 0))
  1074. return;
  1075. destroy_block_symbol_cache (cache->global_symbols);
  1076. destroy_block_symbol_cache (cache->static_symbols);
  1077. if (new_size == 0)
  1078. {
  1079. cache->global_symbols = NULL;
  1080. cache->static_symbols = NULL;
  1081. }
  1082. else
  1083. {
  1084. size_t total_size = symbol_cache_byte_size (new_size);
  1085. cache->global_symbols
  1086. = (struct block_symbol_cache *) xcalloc (1, total_size);
  1087. cache->static_symbols
  1088. = (struct block_symbol_cache *) xcalloc (1, total_size);
  1089. cache->global_symbols->size = new_size;
  1090. cache->static_symbols->size = new_size;
  1091. }
  1092. }
  1093. /* Return the symbol cache of PSPACE.
  1094. Create one if it doesn't exist yet. */
  1095. static struct symbol_cache *
  1096. get_symbol_cache (struct program_space *pspace)
  1097. {
  1098. struct symbol_cache *cache = symbol_cache_key.get (pspace);
  1099. if (cache == NULL)
  1100. {
  1101. cache = symbol_cache_key.emplace (pspace);
  1102. resize_symbol_cache (cache, symbol_cache_size);
  1103. }
  1104. return cache;
  1105. }
  1106. /* Set the size of the symbol cache in all program spaces. */
  1107. static void
  1108. set_symbol_cache_size (unsigned int new_size)
  1109. {
  1110. for (struct program_space *pspace : program_spaces)
  1111. {
  1112. struct symbol_cache *cache = symbol_cache_key.get (pspace);
  1113. /* The pspace could have been created but not have a cache yet. */
  1114. if (cache != NULL)
  1115. resize_symbol_cache (cache, new_size);
  1116. }
  1117. }
  1118. /* Called when symbol-cache-size is set. */
  1119. static void
  1120. set_symbol_cache_size_handler (const char *args, int from_tty,
  1121. struct cmd_list_element *c)
  1122. {
  1123. if (new_symbol_cache_size > MAX_SYMBOL_CACHE_SIZE)
  1124. {
  1125. /* Restore the previous value.
  1126. This is the value the "show" command prints. */
  1127. new_symbol_cache_size = symbol_cache_size;
  1128. error (_("Symbol cache size is too large, max is %u."),
  1129. MAX_SYMBOL_CACHE_SIZE);
  1130. }
  1131. symbol_cache_size = new_symbol_cache_size;
  1132. set_symbol_cache_size (symbol_cache_size);
  1133. }
  1134. /* Lookup symbol NAME,DOMAIN in BLOCK in the symbol cache of PSPACE.
  1135. OBJFILE_CONTEXT is the current objfile, which may be NULL.
  1136. The result is the symbol if found, SYMBOL_LOOKUP_FAILED if a previous lookup
  1137. failed (and thus this one will too), or NULL if the symbol is not present
  1138. in the cache.
  1139. *BSC_PTR and *SLOT_PTR are set to the cache and slot of the symbol, which
  1140. can be used to save the result of a full lookup attempt. */
  1141. static struct block_symbol
  1142. symbol_cache_lookup (struct symbol_cache *cache,
  1143. struct objfile *objfile_context, enum block_enum block,
  1144. const char *name, domain_enum domain,
  1145. struct block_symbol_cache **bsc_ptr,
  1146. struct symbol_cache_slot **slot_ptr)
  1147. {
  1148. struct block_symbol_cache *bsc;
  1149. unsigned int hash;
  1150. struct symbol_cache_slot *slot;
  1151. if (block == GLOBAL_BLOCK)
  1152. bsc = cache->global_symbols;
  1153. else
  1154. bsc = cache->static_symbols;
  1155. if (bsc == NULL)
  1156. {
  1157. *bsc_ptr = NULL;
  1158. *slot_ptr = NULL;
  1159. return {};
  1160. }
  1161. hash = hash_symbol_entry (objfile_context, name, domain);
  1162. slot = bsc->symbols + hash % bsc->size;
  1163. *bsc_ptr = bsc;
  1164. *slot_ptr = slot;
  1165. if (eq_symbol_entry (slot, objfile_context, name, domain))
  1166. {
  1167. if (symbol_lookup_debug)
  1168. gdb_printf (gdb_stdlog,
  1169. "%s block symbol cache hit%s for %s, %s\n",
  1170. block == GLOBAL_BLOCK ? "Global" : "Static",
  1171. slot->state == SYMBOL_SLOT_NOT_FOUND
  1172. ? " (not found)" : "",
  1173. name, domain_name (domain));
  1174. ++bsc->hits;
  1175. if (slot->state == SYMBOL_SLOT_NOT_FOUND)
  1176. return SYMBOL_LOOKUP_FAILED;
  1177. return slot->value.found;
  1178. }
  1179. /* Symbol is not present in the cache. */
  1180. if (symbol_lookup_debug)
  1181. {
  1182. gdb_printf (gdb_stdlog,
  1183. "%s block symbol cache miss for %s, %s\n",
  1184. block == GLOBAL_BLOCK ? "Global" : "Static",
  1185. name, domain_name (domain));
  1186. }
  1187. ++bsc->misses;
  1188. return {};
  1189. }
  1190. /* Mark SYMBOL as found in SLOT.
  1191. OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
  1192. if it's not needed to distinguish lookups (STATIC_BLOCK). It is *not*
  1193. necessarily the objfile the symbol was found in. */
  1194. static void
  1195. symbol_cache_mark_found (struct block_symbol_cache *bsc,
  1196. struct symbol_cache_slot *slot,
  1197. struct objfile *objfile_context,
  1198. struct symbol *symbol,
  1199. const struct block *block)
  1200. {
  1201. if (bsc == NULL)
  1202. return;
  1203. if (slot->state != SYMBOL_SLOT_UNUSED)
  1204. {
  1205. ++bsc->collisions;
  1206. symbol_cache_clear_slot (slot);
  1207. }
  1208. slot->state = SYMBOL_SLOT_FOUND;
  1209. slot->objfile_context = objfile_context;
  1210. slot->value.found.symbol = symbol;
  1211. slot->value.found.block = block;
  1212. }
  1213. /* Mark symbol NAME, DOMAIN as not found in SLOT.
  1214. OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
  1215. if it's not needed to distinguish lookups (STATIC_BLOCK). */
  1216. static void
  1217. symbol_cache_mark_not_found (struct block_symbol_cache *bsc,
  1218. struct symbol_cache_slot *slot,
  1219. struct objfile *objfile_context,
  1220. const char *name, domain_enum domain)
  1221. {
  1222. if (bsc == NULL)
  1223. return;
  1224. if (slot->state != SYMBOL_SLOT_UNUSED)
  1225. {
  1226. ++bsc->collisions;
  1227. symbol_cache_clear_slot (slot);
  1228. }
  1229. slot->state = SYMBOL_SLOT_NOT_FOUND;
  1230. slot->objfile_context = objfile_context;
  1231. slot->value.not_found.name = xstrdup (name);
  1232. slot->value.not_found.domain = domain;
  1233. }
  1234. /* Flush the symbol cache of PSPACE. */
  1235. static void
  1236. symbol_cache_flush (struct program_space *pspace)
  1237. {
  1238. struct symbol_cache *cache = symbol_cache_key.get (pspace);
  1239. int pass;
  1240. if (cache == NULL)
  1241. return;
  1242. if (cache->global_symbols == NULL)
  1243. {
  1244. gdb_assert (symbol_cache_size == 0);
  1245. gdb_assert (cache->static_symbols == NULL);
  1246. return;
  1247. }
  1248. /* If the cache is untouched since the last flush, early exit.
  1249. This is important for performance during the startup of a program linked
  1250. with 100s (or 1000s) of shared libraries. */
  1251. if (cache->global_symbols->misses == 0
  1252. && cache->static_symbols->misses == 0)
  1253. return;
  1254. gdb_assert (cache->global_symbols->size == symbol_cache_size);
  1255. gdb_assert (cache->static_symbols->size == symbol_cache_size);
  1256. for (pass = 0; pass < 2; ++pass)
  1257. {
  1258. struct block_symbol_cache *bsc
  1259. = pass == 0 ? cache->global_symbols : cache->static_symbols;
  1260. unsigned int i;
  1261. for (i = 0; i < bsc->size; ++i)
  1262. symbol_cache_clear_slot (&bsc->symbols[i]);
  1263. }
  1264. cache->global_symbols->hits = 0;
  1265. cache->global_symbols->misses = 0;
  1266. cache->global_symbols->collisions = 0;
  1267. cache->static_symbols->hits = 0;
  1268. cache->static_symbols->misses = 0;
  1269. cache->static_symbols->collisions = 0;
  1270. }
  1271. /* Dump CACHE. */
  1272. static void
  1273. symbol_cache_dump (const struct symbol_cache *cache)
  1274. {
  1275. int pass;
  1276. if (cache->global_symbols == NULL)
  1277. {
  1278. gdb_printf (" <disabled>\n");
  1279. return;
  1280. }
  1281. for (pass = 0; pass < 2; ++pass)
  1282. {
  1283. const struct block_symbol_cache *bsc
  1284. = pass == 0 ? cache->global_symbols : cache->static_symbols;
  1285. unsigned int i;
  1286. if (pass == 0)
  1287. gdb_printf ("Global symbols:\n");
  1288. else
  1289. gdb_printf ("Static symbols:\n");
  1290. for (i = 0; i < bsc->size; ++i)
  1291. {
  1292. const struct symbol_cache_slot *slot = &bsc->symbols[i];
  1293. QUIT;
  1294. switch (slot->state)
  1295. {
  1296. case SYMBOL_SLOT_UNUSED:
  1297. break;
  1298. case SYMBOL_SLOT_NOT_FOUND:
  1299. gdb_printf (" [%4u] = %s, %s %s (not found)\n", i,
  1300. host_address_to_string (slot->objfile_context),
  1301. slot->value.not_found.name,
  1302. domain_name (slot->value.not_found.domain));
  1303. break;
  1304. case SYMBOL_SLOT_FOUND:
  1305. {
  1306. struct symbol *found = slot->value.found.symbol;
  1307. const struct objfile *context = slot->objfile_context;
  1308. gdb_printf (" [%4u] = %s, %s %s\n", i,
  1309. host_address_to_string (context),
  1310. found->print_name (),
  1311. domain_name (found->domain ()));
  1312. break;
  1313. }
  1314. }
  1315. }
  1316. }
  1317. }
  1318. /* The "mt print symbol-cache" command. */
  1319. static void
  1320. maintenance_print_symbol_cache (const char *args, int from_tty)
  1321. {
  1322. for (struct program_space *pspace : program_spaces)
  1323. {
  1324. struct symbol_cache *cache;
  1325. gdb_printf (_("Symbol cache for pspace %d\n%s:\n"),
  1326. pspace->num,
  1327. pspace->symfile_object_file != NULL
  1328. ? objfile_name (pspace->symfile_object_file)
  1329. : "(no object file)");
  1330. /* If the cache hasn't been created yet, avoid creating one. */
  1331. cache = symbol_cache_key.get (pspace);
  1332. if (cache == NULL)
  1333. gdb_printf (" <empty>\n");
  1334. else
  1335. symbol_cache_dump (cache);
  1336. }
  1337. }
  1338. /* The "mt flush-symbol-cache" command. */
  1339. static void
  1340. maintenance_flush_symbol_cache (const char *args, int from_tty)
  1341. {
  1342. for (struct program_space *pspace : program_spaces)
  1343. {
  1344. symbol_cache_flush (pspace);
  1345. }
  1346. }
  1347. /* Print usage statistics of CACHE. */
  1348. static void
  1349. symbol_cache_stats (struct symbol_cache *cache)
  1350. {
  1351. int pass;
  1352. if (cache->global_symbols == NULL)
  1353. {
  1354. gdb_printf (" <disabled>\n");
  1355. return;
  1356. }
  1357. for (pass = 0; pass < 2; ++pass)
  1358. {
  1359. const struct block_symbol_cache *bsc
  1360. = pass == 0 ? cache->global_symbols : cache->static_symbols;
  1361. QUIT;
  1362. if (pass == 0)
  1363. gdb_printf ("Global block cache stats:\n");
  1364. else
  1365. gdb_printf ("Static block cache stats:\n");
  1366. gdb_printf (" size: %u\n", bsc->size);
  1367. gdb_printf (" hits: %u\n", bsc->hits);
  1368. gdb_printf (" misses: %u\n", bsc->misses);
  1369. gdb_printf (" collisions: %u\n", bsc->collisions);
  1370. }
  1371. }
  1372. /* The "mt print symbol-cache-statistics" command. */
  1373. static void
  1374. maintenance_print_symbol_cache_statistics (const char *args, int from_tty)
  1375. {
  1376. for (struct program_space *pspace : program_spaces)
  1377. {
  1378. struct symbol_cache *cache;
  1379. gdb_printf (_("Symbol cache statistics for pspace %d\n%s:\n"),
  1380. pspace->num,
  1381. pspace->symfile_object_file != NULL
  1382. ? objfile_name (pspace->symfile_object_file)
  1383. : "(no object file)");
  1384. /* If the cache hasn't been created yet, avoid creating one. */
  1385. cache = symbol_cache_key.get (pspace);
  1386. if (cache == NULL)
  1387. gdb_printf (" empty, no stats available\n");
  1388. else
  1389. symbol_cache_stats (cache);
  1390. }
  1391. }
  1392. /* This module's 'new_objfile' observer. */
  1393. static void
  1394. symtab_new_objfile_observer (struct objfile *objfile)
  1395. {
  1396. /* Ideally we'd use OBJFILE->pspace, but OBJFILE may be NULL. */
  1397. symbol_cache_flush (current_program_space);
  1398. }
  1399. /* This module's 'free_objfile' observer. */
  1400. static void
  1401. symtab_free_objfile_observer (struct objfile *objfile)
  1402. {
  1403. symbol_cache_flush (objfile->pspace);
  1404. }
  1405. /* Debug symbols usually don't have section information. We need to dig that
  1406. out of the minimal symbols and stash that in the debug symbol. */
  1407. void
  1408. fixup_section (struct general_symbol_info *ginfo,
  1409. CORE_ADDR addr, struct objfile *objfile)
  1410. {
  1411. struct minimal_symbol *msym;
  1412. /* First, check whether a minimal symbol with the same name exists
  1413. and points to the same address. The address check is required
  1414. e.g. on PowerPC64, where the minimal symbol for a function will
  1415. point to the function descriptor, while the debug symbol will
  1416. point to the actual function code. */
  1417. msym = lookup_minimal_symbol_by_pc_name (addr, ginfo->linkage_name (),
  1418. objfile);
  1419. if (msym)
  1420. ginfo->set_section_index (msym->section_index ());
  1421. else
  1422. {
  1423. /* Static, function-local variables do appear in the linker
  1424. (minimal) symbols, but are frequently given names that won't
  1425. be found via lookup_minimal_symbol(). E.g., it has been
  1426. observed in frv-uclinux (ELF) executables that a static,
  1427. function-local variable named "foo" might appear in the
  1428. linker symbols as "foo.6" or "foo.3". Thus, there is no
  1429. point in attempting to extend the lookup-by-name mechanism to
  1430. handle this case due to the fact that there can be multiple
  1431. names.
  1432. So, instead, search the section table when lookup by name has
  1433. failed. The ``addr'' and ``endaddr'' fields may have already
  1434. been relocated. If so, the relocation offset needs to be
  1435. subtracted from these values when performing the comparison.
  1436. We unconditionally subtract it, because, when no relocation
  1437. has been performed, the value will simply be zero.
  1438. The address of the symbol whose section we're fixing up HAS
  1439. NOT BEEN adjusted (relocated) yet. It can't have been since
  1440. the section isn't yet known and knowing the section is
  1441. necessary in order to add the correct relocation value. In
  1442. other words, we wouldn't even be in this function (attempting
  1443. to compute the section) if it were already known.
  1444. Note that it is possible to search the minimal symbols
  1445. (subtracting the relocation value if necessary) to find the
  1446. matching minimal symbol, but this is overkill and much less
  1447. efficient. It is not necessary to find the matching minimal
  1448. symbol, only its section.
  1449. Note that this technique (of doing a section table search)
  1450. can fail when unrelocated section addresses overlap. For
  1451. this reason, we still attempt a lookup by name prior to doing
  1452. a search of the section table. */
  1453. struct obj_section *s;
  1454. int fallback = -1;
  1455. ALL_OBJFILE_OSECTIONS (objfile, s)
  1456. {
  1457. int idx = s - objfile->sections;
  1458. CORE_ADDR offset = objfile->section_offsets[idx];
  1459. if (fallback == -1)
  1460. fallback = idx;
  1461. if (s->addr () - offset <= addr && addr < s->endaddr () - offset)
  1462. {
  1463. ginfo->set_section_index (idx);
  1464. return;
  1465. }
  1466. }
  1467. /* If we didn't find the section, assume it is in the first
  1468. section. If there is no allocated section, then it hardly
  1469. matters what we pick, so just pick zero. */
  1470. if (fallback == -1)
  1471. ginfo->set_section_index (0);
  1472. else
  1473. ginfo->set_section_index (fallback);
  1474. }
  1475. }
  1476. struct symbol *
  1477. fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
  1478. {
  1479. CORE_ADDR addr;
  1480. if (!sym)
  1481. return NULL;
  1482. if (!sym->is_objfile_owned ())
  1483. return sym;
  1484. /* We either have an OBJFILE, or we can get at it from the sym's
  1485. symtab. Anything else is a bug. */
  1486. gdb_assert (objfile || symbol_symtab (sym));
  1487. if (objfile == NULL)
  1488. objfile = symbol_objfile (sym);
  1489. if (sym->obj_section (objfile) != nullptr)
  1490. return sym;
  1491. /* We should have an objfile by now. */
  1492. gdb_assert (objfile);
  1493. switch (sym->aclass ())
  1494. {
  1495. case LOC_STATIC:
  1496. case LOC_LABEL:
  1497. addr = SYMBOL_VALUE_ADDRESS (sym);
  1498. break;
  1499. case LOC_BLOCK:
  1500. addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
  1501. break;
  1502. default:
  1503. /* Nothing else will be listed in the minsyms -- no use looking
  1504. it up. */
  1505. return sym;
  1506. }
  1507. fixup_section (sym, addr, objfile);
  1508. return sym;
  1509. }
  1510. /* See symtab.h. */
  1511. demangle_for_lookup_info::demangle_for_lookup_info
  1512. (const lookup_name_info &lookup_name, language lang)
  1513. {
  1514. demangle_result_storage storage;
  1515. if (lookup_name.ignore_parameters () && lang == language_cplus)
  1516. {
  1517. gdb::unique_xmalloc_ptr<char> without_params
  1518. = cp_remove_params_if_any (lookup_name.c_str (),
  1519. lookup_name.completion_mode ());
  1520. if (without_params != NULL)
  1521. {
  1522. if (lookup_name.match_type () != symbol_name_match_type::SEARCH_NAME)
  1523. m_demangled_name = demangle_for_lookup (without_params.get (),
  1524. lang, storage);
  1525. return;
  1526. }
  1527. }
  1528. if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
  1529. m_demangled_name = lookup_name.c_str ();
  1530. else
  1531. m_demangled_name = demangle_for_lookup (lookup_name.c_str (),
  1532. lang, storage);
  1533. }
  1534. /* See symtab.h. */
  1535. const lookup_name_info &
  1536. lookup_name_info::match_any ()
  1537. {
  1538. /* Lookup any symbol that "" would complete. I.e., this matches all
  1539. symbol names. */
  1540. static const lookup_name_info lookup_name ("", symbol_name_match_type::FULL,
  1541. true);
  1542. return lookup_name;
  1543. }
  1544. /* Compute the demangled form of NAME as used by the various symbol
  1545. lookup functions. The result can either be the input NAME
  1546. directly, or a pointer to a buffer owned by the STORAGE object.
  1547. For Ada, this function just returns NAME, unmodified.
  1548. Normally, Ada symbol lookups are performed using the encoded name
  1549. rather than the demangled name, and so it might seem to make sense
  1550. for this function to return an encoded version of NAME.
  1551. Unfortunately, we cannot do this, because this function is used in
  1552. circumstances where it is not appropriate to try to encode NAME.
  1553. For instance, when displaying the frame info, we demangle the name
  1554. of each parameter, and then perform a symbol lookup inside our
  1555. function using that demangled name. In Ada, certain functions
  1556. have internally-generated parameters whose name contain uppercase
  1557. characters. Encoding those name would result in those uppercase
  1558. characters to become lowercase, and thus cause the symbol lookup
  1559. to fail. */
  1560. const char *
  1561. demangle_for_lookup (const char *name, enum language lang,
  1562. demangle_result_storage &storage)
  1563. {
  1564. /* If we are using C++, D, or Go, demangle the name before doing a
  1565. lookup, so we can always binary search. */
  1566. if (lang == language_cplus)
  1567. {
  1568. gdb::unique_xmalloc_ptr<char> demangled_name
  1569. = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
  1570. if (demangled_name != NULL)
  1571. return storage.set_malloc_ptr (std::move (demangled_name));
  1572. /* If we were given a non-mangled name, canonicalize it
  1573. according to the language (so far only for C++). */
  1574. gdb::unique_xmalloc_ptr<char> canon = cp_canonicalize_string (name);
  1575. if (canon != nullptr)
  1576. return storage.set_malloc_ptr (std::move (canon));
  1577. }
  1578. else if (lang == language_d)
  1579. {
  1580. gdb::unique_xmalloc_ptr<char> demangled_name = d_demangle (name, 0);
  1581. if (demangled_name != NULL)
  1582. return storage.set_malloc_ptr (std::move (demangled_name));
  1583. }
  1584. else if (lang == language_go)
  1585. {
  1586. gdb::unique_xmalloc_ptr<char> demangled_name
  1587. = language_def (language_go)->demangle_symbol (name, 0);
  1588. if (demangled_name != NULL)
  1589. return storage.set_malloc_ptr (std::move (demangled_name));
  1590. }
  1591. return name;
  1592. }
  1593. /* See symtab.h. */
  1594. unsigned int
  1595. search_name_hash (enum language language, const char *search_name)
  1596. {
  1597. return language_def (language)->search_name_hash (search_name);
  1598. }
  1599. /* See symtab.h.
  1600. This function (or rather its subordinates) have a bunch of loops and
  1601. it would seem to be attractive to put in some QUIT's (though I'm not really
  1602. sure whether it can run long enough to be really important). But there
  1603. are a few calls for which it would appear to be bad news to quit
  1604. out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c. (Note
  1605. that there is C++ code below which can error(), but that probably
  1606. doesn't affect these calls since they are looking for a known
  1607. variable and thus can probably assume it will never hit the C++
  1608. code). */
  1609. struct block_symbol
  1610. lookup_symbol_in_language (const char *name, const struct block *block,
  1611. const domain_enum domain, enum language lang,
  1612. struct field_of_this_result *is_a_field_of_this)
  1613. {
  1614. demangle_result_storage storage;
  1615. const char *modified_name = demangle_for_lookup (name, lang, storage);
  1616. return lookup_symbol_aux (modified_name,
  1617. symbol_name_match_type::FULL,
  1618. block, domain, lang,
  1619. is_a_field_of_this);
  1620. }
  1621. /* See symtab.h. */
  1622. struct block_symbol
  1623. lookup_symbol (const char *name, const struct block *block,
  1624. domain_enum domain,
  1625. struct field_of_this_result *is_a_field_of_this)
  1626. {
  1627. return lookup_symbol_in_language (name, block, domain,
  1628. current_language->la_language,
  1629. is_a_field_of_this);
  1630. }
  1631. /* See symtab.h. */
  1632. struct block_symbol
  1633. lookup_symbol_search_name (const char *search_name, const struct block *block,
  1634. domain_enum domain)
  1635. {
  1636. return lookup_symbol_aux (search_name, symbol_name_match_type::SEARCH_NAME,
  1637. block, domain, language_asm, NULL);
  1638. }
  1639. /* See symtab.h. */
  1640. struct block_symbol
  1641. lookup_language_this (const struct language_defn *lang,
  1642. const struct block *block)
  1643. {
  1644. if (lang->name_of_this () == NULL || block == NULL)
  1645. return {};
  1646. if (symbol_lookup_debug > 1)
  1647. {
  1648. struct objfile *objfile = block_objfile (block);
  1649. gdb_printf (gdb_stdlog,
  1650. "lookup_language_this (%s, %s (objfile %s))",
  1651. lang->name (), host_address_to_string (block),
  1652. objfile_debug_name (objfile));
  1653. }
  1654. while (block)
  1655. {
  1656. struct symbol *sym;
  1657. sym = block_lookup_symbol (block, lang->name_of_this (),
  1658. symbol_name_match_type::SEARCH_NAME,
  1659. VAR_DOMAIN);
  1660. if (sym != NULL)
  1661. {
  1662. if (symbol_lookup_debug > 1)
  1663. {
  1664. gdb_printf (gdb_stdlog, " = %s (%s, block %s)\n",
  1665. sym->print_name (),
  1666. host_address_to_string (sym),
  1667. host_address_to_string (block));
  1668. }
  1669. return (struct block_symbol) {sym, block};
  1670. }
  1671. if (BLOCK_FUNCTION (block))
  1672. break;
  1673. block = BLOCK_SUPERBLOCK (block);
  1674. }
  1675. if (symbol_lookup_debug > 1)
  1676. gdb_printf (gdb_stdlog, " = NULL\n");
  1677. return {};
  1678. }
  1679. /* Given TYPE, a structure/union,
  1680. return 1 if the component named NAME from the ultimate target
  1681. structure/union is defined, otherwise, return 0. */
  1682. static int
  1683. check_field (struct type *type, const char *name,
  1684. struct field_of_this_result *is_a_field_of_this)
  1685. {
  1686. int i;
  1687. /* The type may be a stub. */
  1688. type = check_typedef (type);
  1689. for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
  1690. {
  1691. const char *t_field_name = type->field (i).name ();
  1692. if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
  1693. {
  1694. is_a_field_of_this->type = type;
  1695. is_a_field_of_this->field = &type->field (i);
  1696. return 1;
  1697. }
  1698. }
  1699. /* C++: If it was not found as a data field, then try to return it
  1700. as a pointer to a method. */
  1701. for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
  1702. {
  1703. if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
  1704. {
  1705. is_a_field_of_this->type = type;
  1706. is_a_field_of_this->fn_field = &TYPE_FN_FIELDLIST (type, i);
  1707. return 1;
  1708. }
  1709. }
  1710. for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1711. if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
  1712. return 1;
  1713. return 0;
  1714. }
  1715. /* Behave like lookup_symbol except that NAME is the natural name
  1716. (e.g., demangled name) of the symbol that we're looking for. */
  1717. static struct block_symbol
  1718. lookup_symbol_aux (const char *name, symbol_name_match_type match_type,
  1719. const struct block *block,
  1720. const domain_enum domain, enum language language,
  1721. struct field_of_this_result *is_a_field_of_this)
  1722. {
  1723. struct block_symbol result;
  1724. const struct language_defn *langdef;
  1725. if (symbol_lookup_debug)
  1726. {
  1727. struct objfile *objfile = (block == nullptr
  1728. ? nullptr : block_objfile (block));
  1729. gdb_printf (gdb_stdlog,
  1730. "lookup_symbol_aux (%s, %s (objfile %s), %s, %s)\n",
  1731. name, host_address_to_string (block),
  1732. objfile != NULL
  1733. ? objfile_debug_name (objfile) : "NULL",
  1734. domain_name (domain), language_str (language));
  1735. }
  1736. /* Make sure we do something sensible with is_a_field_of_this, since
  1737. the callers that set this parameter to some non-null value will
  1738. certainly use it later. If we don't set it, the contents of
  1739. is_a_field_of_this are undefined. */
  1740. if (is_a_field_of_this != NULL)
  1741. memset (is_a_field_of_this, 0, sizeof (*is_a_field_of_this));
  1742. /* Search specified block and its superiors. Don't search
  1743. STATIC_BLOCK or GLOBAL_BLOCK. */
  1744. result = lookup_local_symbol (name, match_type, block, domain, language);
  1745. if (result.symbol != NULL)
  1746. {
  1747. if (symbol_lookup_debug)
  1748. {
  1749. gdb_printf (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
  1750. host_address_to_string (result.symbol));
  1751. }
  1752. return result;
  1753. }
  1754. /* If requested to do so by the caller and if appropriate for LANGUAGE,
  1755. check to see if NAME is a field of `this'. */
  1756. langdef = language_def (language);
  1757. /* Don't do this check if we are searching for a struct. It will
  1758. not be found by check_field, but will be found by other
  1759. means. */
  1760. if (is_a_field_of_this != NULL && domain != STRUCT_DOMAIN)
  1761. {
  1762. result = lookup_language_this (langdef, block);
  1763. if (result.symbol)
  1764. {
  1765. struct type *t = result.symbol->type ();
  1766. /* I'm not really sure that type of this can ever
  1767. be typedefed; just be safe. */
  1768. t = check_typedef (t);
  1769. if (t->is_pointer_or_reference ())
  1770. t = TYPE_TARGET_TYPE (t);
  1771. if (t->code () != TYPE_CODE_STRUCT
  1772. && t->code () != TYPE_CODE_UNION)
  1773. error (_("Internal error: `%s' is not an aggregate"),
  1774. langdef->name_of_this ());
  1775. if (check_field (t, name, is_a_field_of_this))
  1776. {
  1777. if (symbol_lookup_debug)
  1778. {
  1779. gdb_printf (gdb_stdlog,
  1780. "lookup_symbol_aux (...) = NULL\n");
  1781. }
  1782. return {};
  1783. }
  1784. }
  1785. }
  1786. /* Now do whatever is appropriate for LANGUAGE to look
  1787. up static and global variables. */
  1788. result = langdef->lookup_symbol_nonlocal (name, block, domain);
  1789. if (result.symbol != NULL)
  1790. {
  1791. if (symbol_lookup_debug)
  1792. {
  1793. gdb_printf (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
  1794. host_address_to_string (result.symbol));
  1795. }
  1796. return result;
  1797. }
  1798. /* Now search all static file-level symbols. Not strictly correct,
  1799. but more useful than an error. */
  1800. result = lookup_static_symbol (name, domain);
  1801. if (symbol_lookup_debug)
  1802. {
  1803. gdb_printf (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
  1804. result.symbol != NULL
  1805. ? host_address_to_string (result.symbol)
  1806. : "NULL");
  1807. }
  1808. return result;
  1809. }
  1810. /* Check to see if the symbol is defined in BLOCK or its superiors.
  1811. Don't search STATIC_BLOCK or GLOBAL_BLOCK. */
  1812. static struct block_symbol
  1813. lookup_local_symbol (const char *name,
  1814. symbol_name_match_type match_type,
  1815. const struct block *block,
  1816. const domain_enum domain,
  1817. enum language language)
  1818. {
  1819. struct symbol *sym;
  1820. const struct block *static_block = block_static_block (block);
  1821. const char *scope = block_scope (block);
  1822. /* Check if either no block is specified or it's a global block. */
  1823. if (static_block == NULL)
  1824. return {};
  1825. while (block != static_block)
  1826. {
  1827. sym = lookup_symbol_in_block (name, match_type, block, domain);
  1828. if (sym != NULL)
  1829. return (struct block_symbol) {sym, block};
  1830. if (language == language_cplus || language == language_fortran)
  1831. {
  1832. struct block_symbol blocksym
  1833. = cp_lookup_symbol_imports_or_template (scope, name, block,
  1834. domain);
  1835. if (blocksym.symbol != NULL)
  1836. return blocksym;
  1837. }
  1838. if (BLOCK_FUNCTION (block) != NULL && block_inlined_p (block))
  1839. break;
  1840. block = BLOCK_SUPERBLOCK (block);
  1841. }
  1842. /* We've reached the end of the function without finding a result. */
  1843. return {};
  1844. }
  1845. /* See symtab.h. */
  1846. struct symbol *
  1847. lookup_symbol_in_block (const char *name, symbol_name_match_type match_type,
  1848. const struct block *block,
  1849. const domain_enum domain)
  1850. {
  1851. struct symbol *sym;
  1852. if (symbol_lookup_debug > 1)
  1853. {
  1854. struct objfile *objfile = (block == nullptr
  1855. ? nullptr : block_objfile (block));
  1856. gdb_printf (gdb_stdlog,
  1857. "lookup_symbol_in_block (%s, %s (objfile %s), %s)",
  1858. name, host_address_to_string (block),
  1859. objfile_debug_name (objfile),
  1860. domain_name (domain));
  1861. }
  1862. sym = block_lookup_symbol (block, name, match_type, domain);
  1863. if (sym)
  1864. {
  1865. if (symbol_lookup_debug > 1)
  1866. {
  1867. gdb_printf (gdb_stdlog, " = %s\n",
  1868. host_address_to_string (sym));
  1869. }
  1870. return fixup_symbol_section (sym, NULL);
  1871. }
  1872. if (symbol_lookup_debug > 1)
  1873. gdb_printf (gdb_stdlog, " = NULL\n");
  1874. return NULL;
  1875. }
  1876. /* See symtab.h. */
  1877. struct block_symbol
  1878. lookup_global_symbol_from_objfile (struct objfile *main_objfile,
  1879. enum block_enum block_index,
  1880. const char *name,
  1881. const domain_enum domain)
  1882. {
  1883. gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
  1884. for (objfile *objfile : main_objfile->separate_debug_objfiles ())
  1885. {
  1886. struct block_symbol result
  1887. = lookup_symbol_in_objfile (objfile, block_index, name, domain);
  1888. if (result.symbol != nullptr)
  1889. return result;
  1890. }
  1891. return {};
  1892. }
  1893. /* Check to see if the symbol is defined in one of the OBJFILE's
  1894. symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
  1895. depending on whether or not we want to search global symbols or
  1896. static symbols. */
  1897. static struct block_symbol
  1898. lookup_symbol_in_objfile_symtabs (struct objfile *objfile,
  1899. enum block_enum block_index, const char *name,
  1900. const domain_enum domain)
  1901. {
  1902. gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
  1903. if (symbol_lookup_debug > 1)
  1904. {
  1905. gdb_printf (gdb_stdlog,
  1906. "lookup_symbol_in_objfile_symtabs (%s, %s, %s, %s)",
  1907. objfile_debug_name (objfile),
  1908. block_index == GLOBAL_BLOCK
  1909. ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
  1910. name, domain_name (domain));
  1911. }
  1912. struct block_symbol other;
  1913. other.symbol = NULL;
  1914. for (compunit_symtab *cust : objfile->compunits ())
  1915. {
  1916. const struct blockvector *bv;
  1917. const struct block *block;
  1918. struct block_symbol result;
  1919. bv = cust->blockvector ();
  1920. block = BLOCKVECTOR_BLOCK (bv, block_index);
  1921. result.symbol = block_lookup_symbol_primary (block, name, domain);
  1922. result.block = block;
  1923. if (result.symbol == NULL)
  1924. continue;
  1925. if (best_symbol (result.symbol, domain))
  1926. {
  1927. other = result;
  1928. break;
  1929. }
  1930. if (symbol_matches_domain (result.symbol->language (),
  1931. result.symbol->domain (), domain))
  1932. {
  1933. struct symbol *better
  1934. = better_symbol (other.symbol, result.symbol, domain);
  1935. if (better != other.symbol)
  1936. {
  1937. other.symbol = better;
  1938. other.block = block;
  1939. }
  1940. }
  1941. }
  1942. if (other.symbol != NULL)
  1943. {
  1944. if (symbol_lookup_debug > 1)
  1945. {
  1946. gdb_printf (gdb_stdlog, " = %s (block %s)\n",
  1947. host_address_to_string (other.symbol),
  1948. host_address_to_string (other.block));
  1949. }
  1950. other.symbol = fixup_symbol_section (other.symbol, objfile);
  1951. return other;
  1952. }
  1953. if (symbol_lookup_debug > 1)
  1954. gdb_printf (gdb_stdlog, " = NULL\n");
  1955. return {};
  1956. }
  1957. /* Wrapper around lookup_symbol_in_objfile_symtabs for search_symbols.
  1958. Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
  1959. and all associated separate debug objfiles.
  1960. Normally we only look in OBJFILE, and not any separate debug objfiles
  1961. because the outer loop will cause them to be searched too. This case is
  1962. different. Here we're called from search_symbols where it will only
  1963. call us for the objfile that contains a matching minsym. */
  1964. static struct block_symbol
  1965. lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
  1966. const char *linkage_name,
  1967. domain_enum domain)
  1968. {
  1969. enum language lang = current_language->la_language;
  1970. struct objfile *main_objfile;
  1971. demangle_result_storage storage;
  1972. const char *modified_name = demangle_for_lookup (linkage_name, lang, storage);
  1973. if (objfile->separate_debug_objfile_backlink)
  1974. main_objfile = objfile->separate_debug_objfile_backlink;
  1975. else
  1976. main_objfile = objfile;
  1977. for (::objfile *cur_objfile : main_objfile->separate_debug_objfiles ())
  1978. {
  1979. struct block_symbol result;
  1980. result = lookup_symbol_in_objfile_symtabs (cur_objfile, GLOBAL_BLOCK,
  1981. modified_name, domain);
  1982. if (result.symbol == NULL)
  1983. result = lookup_symbol_in_objfile_symtabs (cur_objfile, STATIC_BLOCK,
  1984. modified_name, domain);
  1985. if (result.symbol != NULL)
  1986. return result;
  1987. }
  1988. return {};
  1989. }
  1990. /* A helper function that throws an exception when a symbol was found
  1991. in a psymtab but not in a symtab. */
  1992. static void ATTRIBUTE_NORETURN
  1993. error_in_psymtab_expansion (enum block_enum block_index, const char *name,
  1994. struct compunit_symtab *cust)
  1995. {
  1996. error (_("\
  1997. Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
  1998. %s may be an inlined function, or may be a template function\n \
  1999. (if a template, try specifying an instantiation: %s<type>)."),
  2000. block_index == GLOBAL_BLOCK ? "global" : "static",
  2001. name,
  2002. symtab_to_filename_for_display (cust->primary_filetab ()),
  2003. name, name);
  2004. }
  2005. /* A helper function for various lookup routines that interfaces with
  2006. the "quick" symbol table functions. */
  2007. static struct block_symbol
  2008. lookup_symbol_via_quick_fns (struct objfile *objfile,
  2009. enum block_enum block_index, const char *name,
  2010. const domain_enum domain)
  2011. {
  2012. struct compunit_symtab *cust;
  2013. const struct blockvector *bv;
  2014. const struct block *block;
  2015. struct block_symbol result;
  2016. if (symbol_lookup_debug > 1)
  2017. {
  2018. gdb_printf (gdb_stdlog,
  2019. "lookup_symbol_via_quick_fns (%s, %s, %s, %s)\n",
  2020. objfile_debug_name (objfile),
  2021. block_index == GLOBAL_BLOCK
  2022. ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
  2023. name, domain_name (domain));
  2024. }
  2025. cust = objfile->lookup_symbol (block_index, name, domain);
  2026. if (cust == NULL)
  2027. {
  2028. if (symbol_lookup_debug > 1)
  2029. {
  2030. gdb_printf (gdb_stdlog,
  2031. "lookup_symbol_via_quick_fns (...) = NULL\n");
  2032. }
  2033. return {};
  2034. }
  2035. bv = cust->blockvector ();
  2036. block = BLOCKVECTOR_BLOCK (bv, block_index);
  2037. result.symbol = block_lookup_symbol (block, name,
  2038. symbol_name_match_type::FULL, domain);
  2039. if (result.symbol == NULL)
  2040. error_in_psymtab_expansion (block_index, name, cust);
  2041. if (symbol_lookup_debug > 1)
  2042. {
  2043. gdb_printf (gdb_stdlog,
  2044. "lookup_symbol_via_quick_fns (...) = %s (block %s)\n",
  2045. host_address_to_string (result.symbol),
  2046. host_address_to_string (block));
  2047. }
  2048. result.symbol = fixup_symbol_section (result.symbol, objfile);
  2049. result.block = block;
  2050. return result;
  2051. }
  2052. /* See language.h. */
  2053. struct block_symbol
  2054. language_defn::lookup_symbol_nonlocal (const char *name,
  2055. const struct block *block,
  2056. const domain_enum domain) const
  2057. {
  2058. struct block_symbol result;
  2059. /* NOTE: dje/2014-10-26: The lookup in all objfiles search could skip
  2060. the current objfile. Searching the current objfile first is useful
  2061. for both matching user expectations as well as performance. */
  2062. result = lookup_symbol_in_static_block (name, block, domain);
  2063. if (result.symbol != NULL)
  2064. return result;
  2065. /* If we didn't find a definition for a builtin type in the static block,
  2066. search for it now. This is actually the right thing to do and can be
  2067. a massive performance win. E.g., when debugging a program with lots of
  2068. shared libraries we could search all of them only to find out the
  2069. builtin type isn't defined in any of them. This is common for types
  2070. like "void". */
  2071. if (domain == VAR_DOMAIN)
  2072. {
  2073. struct gdbarch *gdbarch;
  2074. if (block == NULL)
  2075. gdbarch = target_gdbarch ();
  2076. else
  2077. gdbarch = block_gdbarch (block);
  2078. result.symbol = language_lookup_primitive_type_as_symbol (this,
  2079. gdbarch, name);
  2080. result.block = NULL;
  2081. if (result.symbol != NULL)
  2082. return result;
  2083. }
  2084. return lookup_global_symbol (name, block, domain);
  2085. }
  2086. /* See symtab.h. */
  2087. struct block_symbol
  2088. lookup_symbol_in_static_block (const char *name,
  2089. const struct block *block,
  2090. const domain_enum domain)
  2091. {
  2092. const struct block *static_block = block_static_block (block);
  2093. struct symbol *sym;
  2094. if (static_block == NULL)
  2095. return {};
  2096. if (symbol_lookup_debug)
  2097. {
  2098. struct objfile *objfile = (block == nullptr
  2099. ? nullptr : block_objfile (block));
  2100. gdb_printf (gdb_stdlog,
  2101. "lookup_symbol_in_static_block (%s, %s (objfile %s),"
  2102. " %s)\n",
  2103. name,
  2104. host_address_to_string (block),
  2105. objfile_debug_name (objfile),
  2106. domain_name (domain));
  2107. }
  2108. sym = lookup_symbol_in_block (name,
  2109. symbol_name_match_type::FULL,
  2110. static_block, domain);
  2111. if (symbol_lookup_debug)
  2112. {
  2113. gdb_printf (gdb_stdlog,
  2114. "lookup_symbol_in_static_block (...) = %s\n",
  2115. sym != NULL ? host_address_to_string (sym) : "NULL");
  2116. }
  2117. return (struct block_symbol) {sym, static_block};
  2118. }
  2119. /* Perform the standard symbol lookup of NAME in OBJFILE:
  2120. 1) First search expanded symtabs, and if not found
  2121. 2) Search the "quick" symtabs (partial or .gdb_index).
  2122. BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */
  2123. static struct block_symbol
  2124. lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index,
  2125. const char *name, const domain_enum domain)
  2126. {
  2127. struct block_symbol result;
  2128. gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
  2129. if (symbol_lookup_debug)
  2130. {
  2131. gdb_printf (gdb_stdlog,
  2132. "lookup_symbol_in_objfile (%s, %s, %s, %s)\n",
  2133. objfile_debug_name (objfile),
  2134. block_index == GLOBAL_BLOCK
  2135. ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
  2136. name, domain_name (domain));
  2137. }
  2138. result = lookup_symbol_in_objfile_symtabs (objfile, block_index,
  2139. name, domain);
  2140. if (result.symbol != NULL)
  2141. {
  2142. if (symbol_lookup_debug)
  2143. {
  2144. gdb_printf (gdb_stdlog,
  2145. "lookup_symbol_in_objfile (...) = %s"
  2146. " (in symtabs)\n",
  2147. host_address_to_string (result.symbol));
  2148. }
  2149. return result;
  2150. }
  2151. result = lookup_symbol_via_quick_fns (objfile, block_index,
  2152. name, domain);
  2153. if (symbol_lookup_debug)
  2154. {
  2155. gdb_printf (gdb_stdlog,
  2156. "lookup_symbol_in_objfile (...) = %s%s\n",
  2157. result.symbol != NULL
  2158. ? host_address_to_string (result.symbol)
  2159. : "NULL",
  2160. result.symbol != NULL ? " (via quick fns)" : "");
  2161. }
  2162. return result;
  2163. }
  2164. /* Find the language for partial symbol with NAME. */
  2165. static enum language
  2166. find_quick_global_symbol_language (const char *name, const domain_enum domain)
  2167. {
  2168. for (objfile *objfile : current_program_space->objfiles ())
  2169. {
  2170. bool symbol_found_p;
  2171. enum language lang
  2172. = objfile->lookup_global_symbol_language (name, domain, &symbol_found_p);
  2173. if (symbol_found_p)
  2174. return lang;
  2175. }
  2176. return language_unknown;
  2177. }
  2178. /* Private data to be used with lookup_symbol_global_iterator_cb. */
  2179. struct global_or_static_sym_lookup_data
  2180. {
  2181. /* The name of the symbol we are searching for. */
  2182. const char *name;
  2183. /* The domain to use for our search. */
  2184. domain_enum domain;
  2185. /* The block index in which to search. */
  2186. enum block_enum block_index;
  2187. /* The field where the callback should store the symbol if found.
  2188. It should be initialized to {NULL, NULL} before the search is started. */
  2189. struct block_symbol result;
  2190. };
  2191. /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
  2192. It searches by name for a symbol in the block given by BLOCK_INDEX of the
  2193. given OBJFILE. The arguments for the search are passed via CB_DATA, which
  2194. in reality is a pointer to struct global_or_static_sym_lookup_data. */
  2195. static int
  2196. lookup_symbol_global_or_static_iterator_cb (struct objfile *objfile,
  2197. void *cb_data)
  2198. {
  2199. struct global_or_static_sym_lookup_data *data =
  2200. (struct global_or_static_sym_lookup_data *) cb_data;
  2201. gdb_assert (data->result.symbol == NULL
  2202. && data->result.block == NULL);
  2203. data->result = lookup_symbol_in_objfile (objfile, data->block_index,
  2204. data->name, data->domain);
  2205. /* If we found a match, tell the iterator to stop. Otherwise,
  2206. keep going. */
  2207. return (data->result.symbol != NULL);
  2208. }
  2209. /* This function contains the common code of lookup_{global,static}_symbol.
  2210. OBJFILE is only used if BLOCK_INDEX is GLOBAL_SCOPE, in which case it is
  2211. the objfile to start the lookup in. */
  2212. static struct block_symbol
  2213. lookup_global_or_static_symbol (const char *name,
  2214. enum block_enum block_index,
  2215. struct objfile *objfile,
  2216. const domain_enum domain)
  2217. {
  2218. struct symbol_cache *cache = get_symbol_cache (current_program_space);
  2219. struct block_symbol result;
  2220. struct global_or_static_sym_lookup_data lookup_data;
  2221. struct block_symbol_cache *bsc;
  2222. struct symbol_cache_slot *slot;
  2223. gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
  2224. gdb_assert (objfile == nullptr || block_index == GLOBAL_BLOCK);
  2225. /* First see if we can find the symbol in the cache.
  2226. This works because we use the current objfile to qualify the lookup. */
  2227. result = symbol_cache_lookup (cache, objfile, block_index, name, domain,
  2228. &bsc, &slot);
  2229. if (result.symbol != NULL)
  2230. {
  2231. if (SYMBOL_LOOKUP_FAILED_P (result))
  2232. return {};
  2233. return result;
  2234. }
  2235. /* Do a global search (of global blocks, heh). */
  2236. if (result.symbol == NULL)
  2237. {
  2238. memset (&lookup_data, 0, sizeof (lookup_data));
  2239. lookup_data.name = name;
  2240. lookup_data.block_index = block_index;
  2241. lookup_data.domain = domain;
  2242. gdbarch_iterate_over_objfiles_in_search_order
  2243. (objfile != NULL ? objfile->arch () : target_gdbarch (),
  2244. lookup_symbol_global_or_static_iterator_cb, &lookup_data, objfile);
  2245. result = lookup_data.result;
  2246. }
  2247. if (result.symbol != NULL)
  2248. symbol_cache_mark_found (bsc, slot, objfile, result.symbol, result.block);
  2249. else
  2250. symbol_cache_mark_not_found (bsc, slot, objfile, name, domain);
  2251. return result;
  2252. }
  2253. /* See symtab.h. */
  2254. struct block_symbol
  2255. lookup_static_symbol (const char *name, const domain_enum domain)
  2256. {
  2257. return lookup_global_or_static_symbol (name, STATIC_BLOCK, nullptr, domain);
  2258. }
  2259. /* See symtab.h. */
  2260. struct block_symbol
  2261. lookup_global_symbol (const char *name,
  2262. const struct block *block,
  2263. const domain_enum domain)
  2264. {
  2265. /* If a block was passed in, we want to search the corresponding
  2266. global block first. This yields "more expected" behavior, and is
  2267. needed to support 'FILENAME'::VARIABLE lookups. */
  2268. const struct block *global_block = block_global_block (block);
  2269. symbol *sym = NULL;
  2270. if (global_block != nullptr)
  2271. {
  2272. sym = lookup_symbol_in_block (name,
  2273. symbol_name_match_type::FULL,
  2274. global_block, domain);
  2275. if (sym != NULL && best_symbol (sym, domain))
  2276. return { sym, global_block };
  2277. }
  2278. struct objfile *objfile = nullptr;
  2279. if (block != nullptr)
  2280. {
  2281. objfile = block_objfile (block);
  2282. if (objfile->separate_debug_objfile_backlink != nullptr)
  2283. objfile = objfile->separate_debug_objfile_backlink;
  2284. }
  2285. block_symbol bs
  2286. = lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain);
  2287. if (better_symbol (sym, bs.symbol, domain) == sym)
  2288. return { sym, global_block };
  2289. else
  2290. return bs;
  2291. }
  2292. bool
  2293. symbol_matches_domain (enum language symbol_language,
  2294. domain_enum symbol_domain,
  2295. domain_enum domain)
  2296. {
  2297. /* For C++ "struct foo { ... }" also defines a typedef for "foo".
  2298. Similarly, any Ada type declaration implicitly defines a typedef. */
  2299. if (symbol_language == language_cplus
  2300. || symbol_language == language_d
  2301. || symbol_language == language_ada
  2302. || symbol_language == language_rust)
  2303. {
  2304. if ((domain == VAR_DOMAIN || domain == STRUCT_DOMAIN)
  2305. && symbol_domain == STRUCT_DOMAIN)
  2306. return true;
  2307. }
  2308. /* For all other languages, strict match is required. */
  2309. return (symbol_domain == domain);
  2310. }
  2311. /* See symtab.h. */
  2312. struct type *
  2313. lookup_transparent_type (const char *name)
  2314. {
  2315. return current_language->lookup_transparent_type (name);
  2316. }
  2317. /* A helper for basic_lookup_transparent_type that interfaces with the
  2318. "quick" symbol table functions. */
  2319. static struct type *
  2320. basic_lookup_transparent_type_quick (struct objfile *objfile,
  2321. enum block_enum block_index,
  2322. const char *name)
  2323. {
  2324. struct compunit_symtab *cust;
  2325. const struct blockvector *bv;
  2326. const struct block *block;
  2327. struct symbol *sym;
  2328. cust = objfile->lookup_symbol (block_index, name, STRUCT_DOMAIN);
  2329. if (cust == NULL)
  2330. return NULL;
  2331. bv = cust->blockvector ();
  2332. block = BLOCKVECTOR_BLOCK (bv, block_index);
  2333. sym = block_find_symbol (block, name, STRUCT_DOMAIN,
  2334. block_find_non_opaque_type, NULL);
  2335. if (sym == NULL)
  2336. error_in_psymtab_expansion (block_index, name, cust);
  2337. gdb_assert (!TYPE_IS_OPAQUE (sym->type ()));
  2338. return sym->type ();
  2339. }
  2340. /* Subroutine of basic_lookup_transparent_type to simplify it.
  2341. Look up the non-opaque definition of NAME in BLOCK_INDEX of OBJFILE.
  2342. BLOCK_INDEX is either GLOBAL_BLOCK or STATIC_BLOCK. */
  2343. static struct type *
  2344. basic_lookup_transparent_type_1 (struct objfile *objfile,
  2345. enum block_enum block_index,
  2346. const char *name)
  2347. {
  2348. const struct blockvector *bv;
  2349. const struct block *block;
  2350. const struct symbol *sym;
  2351. for (compunit_symtab *cust : objfile->compunits ())
  2352. {
  2353. bv = cust->blockvector ();
  2354. block = BLOCKVECTOR_BLOCK (bv, block_index);
  2355. sym = block_find_symbol (block, name, STRUCT_DOMAIN,
  2356. block_find_non_opaque_type, NULL);
  2357. if (sym != NULL)
  2358. {
  2359. gdb_assert (!TYPE_IS_OPAQUE (sym->type ()));
  2360. return sym->type ();
  2361. }
  2362. }
  2363. return NULL;
  2364. }
  2365. /* The standard implementation of lookup_transparent_type. This code
  2366. was modeled on lookup_symbol -- the parts not relevant to looking
  2367. up types were just left out. In particular it's assumed here that
  2368. types are available in STRUCT_DOMAIN and only in file-static or
  2369. global blocks. */
  2370. struct type *
  2371. basic_lookup_transparent_type (const char *name)
  2372. {
  2373. struct type *t;
  2374. /* Now search all the global symbols. Do the symtab's first, then
  2375. check the psymtab's. If a psymtab indicates the existence
  2376. of the desired name as a global, then do psymtab-to-symtab
  2377. conversion on the fly and return the found symbol. */
  2378. for (objfile *objfile : current_program_space->objfiles ())
  2379. {
  2380. t = basic_lookup_transparent_type_1 (objfile, GLOBAL_BLOCK, name);
  2381. if (t)
  2382. return t;
  2383. }
  2384. for (objfile *objfile : current_program_space->objfiles ())
  2385. {
  2386. t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
  2387. if (t)
  2388. return t;
  2389. }
  2390. /* Now search the static file-level symbols.
  2391. Not strictly correct, but more useful than an error.
  2392. Do the symtab's first, then
  2393. check the psymtab's. If a psymtab indicates the existence
  2394. of the desired name as a file-level static, then do psymtab-to-symtab
  2395. conversion on the fly and return the found symbol. */
  2396. for (objfile *objfile : current_program_space->objfiles ())
  2397. {
  2398. t = basic_lookup_transparent_type_1 (objfile, STATIC_BLOCK, name);
  2399. if (t)
  2400. return t;
  2401. }
  2402. for (objfile *objfile : current_program_space->objfiles ())
  2403. {
  2404. t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
  2405. if (t)
  2406. return t;
  2407. }
  2408. return (struct type *) 0;
  2409. }
  2410. /* See symtab.h. */
  2411. bool
  2412. iterate_over_symbols (const struct block *block,
  2413. const lookup_name_info &name,
  2414. const domain_enum domain,
  2415. gdb::function_view<symbol_found_callback_ftype> callback)
  2416. {
  2417. struct block_iterator iter;
  2418. struct symbol *sym;
  2419. ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
  2420. {
  2421. if (symbol_matches_domain (sym->language (), sym->domain (), domain))
  2422. {
  2423. struct block_symbol block_sym = {sym, block};
  2424. if (!callback (&block_sym))
  2425. return false;
  2426. }
  2427. }
  2428. return true;
  2429. }
  2430. /* See symtab.h. */
  2431. bool
  2432. iterate_over_symbols_terminated
  2433. (const struct block *block,
  2434. const lookup_name_info &name,
  2435. const domain_enum domain,
  2436. gdb::function_view<symbol_found_callback_ftype> callback)
  2437. {
  2438. if (!iterate_over_symbols (block, name, domain, callback))
  2439. return false;
  2440. struct block_symbol block_sym = {nullptr, block};
  2441. return callback (&block_sym);
  2442. }
  2443. /* Find the compunit symtab associated with PC and SECTION.
  2444. This will read in debug info as necessary. */
  2445. struct compunit_symtab *
  2446. find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
  2447. {
  2448. struct compunit_symtab *best_cust = NULL;
  2449. CORE_ADDR best_cust_range = 0;
  2450. struct bound_minimal_symbol msymbol;
  2451. /* If we know that this is not a text address, return failure. This is
  2452. necessary because we loop based on the block's high and low code
  2453. addresses, which do not include the data ranges, and because
  2454. we call find_pc_sect_psymtab which has a similar restriction based
  2455. on the partial_symtab's texthigh and textlow. */
  2456. msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
  2457. if (msymbol.minsym && msymbol.minsym->data_p ())
  2458. return NULL;
  2459. /* Search all symtabs for the one whose file contains our address, and which
  2460. is the smallest of all the ones containing the address. This is designed
  2461. to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
  2462. and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
  2463. 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
  2464. This happens for native ecoff format, where code from included files
  2465. gets its own symtab. The symtab for the included file should have
  2466. been read in already via the dependency mechanism.
  2467. It might be swifter to create several symtabs with the same name
  2468. like xcoff does (I'm not sure).
  2469. It also happens for objfiles that have their functions reordered.
  2470. For these, the symtab we are looking for is not necessarily read in. */
  2471. for (objfile *obj_file : current_program_space->objfiles ())
  2472. {
  2473. for (compunit_symtab *cust : obj_file->compunits ())
  2474. {
  2475. const struct blockvector *bv = cust->blockvector ();
  2476. const struct block *global_block
  2477. = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  2478. CORE_ADDR start = BLOCK_START (global_block);
  2479. CORE_ADDR end = BLOCK_END (global_block);
  2480. bool in_range_p = start <= pc && pc < end;
  2481. if (!in_range_p)
  2482. continue;
  2483. if (BLOCKVECTOR_MAP (bv))
  2484. {
  2485. if (addrmap_find (BLOCKVECTOR_MAP (bv), pc) == nullptr)
  2486. continue;
  2487. return cust;
  2488. }
  2489. CORE_ADDR range = end - start;
  2490. if (best_cust != nullptr
  2491. && range >= best_cust_range)
  2492. /* Cust doesn't have a smaller range than best_cust, skip it. */
  2493. continue;
  2494. /* For an objfile that has its functions reordered,
  2495. find_pc_psymtab will find the proper partial symbol table
  2496. and we simply return its corresponding symtab. */
  2497. /* In order to better support objfiles that contain both
  2498. stabs and coff debugging info, we continue on if a psymtab
  2499. can't be found. */
  2500. if ((obj_file->flags & OBJF_REORDERED) != 0)
  2501. {
  2502. struct compunit_symtab *result;
  2503. result
  2504. = obj_file->find_pc_sect_compunit_symtab (msymbol,
  2505. pc,
  2506. section,
  2507. 0);
  2508. if (result != NULL)
  2509. return result;
  2510. }
  2511. if (section != 0)
  2512. {
  2513. struct symbol *sym = NULL;
  2514. struct block_iterator iter;
  2515. for (int b_index = GLOBAL_BLOCK;
  2516. b_index <= STATIC_BLOCK && sym == NULL;
  2517. ++b_index)
  2518. {
  2519. const struct block *b = BLOCKVECTOR_BLOCK (bv, b_index);
  2520. ALL_BLOCK_SYMBOLS (b, iter, sym)
  2521. {
  2522. fixup_symbol_section (sym, obj_file);
  2523. if (matching_obj_sections (sym->obj_section (obj_file),
  2524. section))
  2525. break;
  2526. }
  2527. }
  2528. if (sym == NULL)
  2529. continue; /* No symbol in this symtab matches
  2530. section. */
  2531. }
  2532. /* Cust is best found sofar, save it. */
  2533. best_cust = cust;
  2534. best_cust_range = range;
  2535. }
  2536. }
  2537. if (best_cust != NULL)
  2538. return best_cust;
  2539. /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs). */
  2540. for (objfile *objf : current_program_space->objfiles ())
  2541. {
  2542. struct compunit_symtab *result
  2543. = objf->find_pc_sect_compunit_symtab (msymbol, pc, section, 1);
  2544. if (result != NULL)
  2545. return result;
  2546. }
  2547. return NULL;
  2548. }
  2549. /* Find the compunit symtab associated with PC.
  2550. This will read in debug info as necessary.
  2551. Backward compatibility, no section. */
  2552. struct compunit_symtab *
  2553. find_pc_compunit_symtab (CORE_ADDR pc)
  2554. {
  2555. return find_pc_sect_compunit_symtab (pc, find_pc_mapped_section (pc));
  2556. }
  2557. /* See symtab.h. */
  2558. struct symbol *
  2559. find_symbol_at_address (CORE_ADDR address)
  2560. {
  2561. /* A helper function to search a given symtab for a symbol matching
  2562. ADDR. */
  2563. auto search_symtab = [] (compunit_symtab *symtab, CORE_ADDR addr) -> symbol *
  2564. {
  2565. const struct blockvector *bv = symtab->blockvector ();
  2566. for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
  2567. {
  2568. const struct block *b = BLOCKVECTOR_BLOCK (bv, i);
  2569. struct block_iterator iter;
  2570. struct symbol *sym;
  2571. ALL_BLOCK_SYMBOLS (b, iter, sym)
  2572. {
  2573. if (sym->aclass () == LOC_STATIC
  2574. && SYMBOL_VALUE_ADDRESS (sym) == addr)
  2575. return sym;
  2576. }
  2577. }
  2578. return nullptr;
  2579. };
  2580. for (objfile *objfile : current_program_space->objfiles ())
  2581. {
  2582. /* If this objfile was read with -readnow, then we need to
  2583. search the symtabs directly. */
  2584. if ((objfile->flags & OBJF_READNOW) != 0)
  2585. {
  2586. for (compunit_symtab *symtab : objfile->compunits ())
  2587. {
  2588. struct symbol *sym = search_symtab (symtab, address);
  2589. if (sym != nullptr)
  2590. return sym;
  2591. }
  2592. }
  2593. else
  2594. {
  2595. struct compunit_symtab *symtab
  2596. = objfile->find_compunit_symtab_by_address (address);
  2597. if (symtab != NULL)
  2598. {
  2599. struct symbol *sym = search_symtab (symtab, address);
  2600. if (sym != nullptr)
  2601. return sym;
  2602. }
  2603. }
  2604. }
  2605. return NULL;
  2606. }
  2607. /* Find the source file and line number for a given PC value and SECTION.
  2608. Return a structure containing a symtab pointer, a line number,
  2609. and a pc range for the entire source line.
  2610. The value's .pc field is NOT the specified pc.
  2611. NOTCURRENT nonzero means, if specified pc is on a line boundary,
  2612. use the line that ends there. Otherwise, in that case, the line
  2613. that begins there is used. */
  2614. /* The big complication here is that a line may start in one file, and end just
  2615. before the start of another file. This usually occurs when you #include
  2616. code in the middle of a subroutine. To properly find the end of a line's PC
  2617. range, we must search all symtabs associated with this compilation unit, and
  2618. find the one whose first PC is closer than that of the next line in this
  2619. symtab. */
  2620. struct symtab_and_line
  2621. find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
  2622. {
  2623. struct compunit_symtab *cust;
  2624. struct linetable *l;
  2625. int len;
  2626. struct linetable_entry *item;
  2627. const struct blockvector *bv;
  2628. struct bound_minimal_symbol msymbol;
  2629. /* Info on best line seen so far, and where it starts, and its file. */
  2630. struct linetable_entry *best = NULL;
  2631. CORE_ADDR best_end = 0;
  2632. struct symtab *best_symtab = 0;
  2633. /* Store here the first line number
  2634. of a file which contains the line at the smallest pc after PC.
  2635. If we don't find a line whose range contains PC,
  2636. we will use a line one less than this,
  2637. with a range from the start of that file to the first line's pc. */
  2638. struct linetable_entry *alt = NULL;
  2639. /* Info on best line seen in this file. */
  2640. struct linetable_entry *prev;
  2641. /* If this pc is not from the current frame,
  2642. it is the address of the end of a call instruction.
  2643. Quite likely that is the start of the following statement.
  2644. But what we want is the statement containing the instruction.
  2645. Fudge the pc to make sure we get that. */
  2646. /* It's tempting to assume that, if we can't find debugging info for
  2647. any function enclosing PC, that we shouldn't search for line
  2648. number info, either. However, GAS can emit line number info for
  2649. assembly files --- very helpful when debugging hand-written
  2650. assembly code. In such a case, we'd have no debug info for the
  2651. function, but we would have line info. */
  2652. if (notcurrent)
  2653. pc -= 1;
  2654. /* elz: added this because this function returned the wrong
  2655. information if the pc belongs to a stub (import/export)
  2656. to call a shlib function. This stub would be anywhere between
  2657. two functions in the target, and the line info was erroneously
  2658. taken to be the one of the line before the pc. */
  2659. /* RT: Further explanation:
  2660. * We have stubs (trampolines) inserted between procedures.
  2661. *
  2662. * Example: "shr1" exists in a shared library, and a "shr1" stub also
  2663. * exists in the main image.
  2664. *
  2665. * In the minimal symbol table, we have a bunch of symbols
  2666. * sorted by start address. The stubs are marked as "trampoline",
  2667. * the others appear as text. E.g.:
  2668. *
  2669. * Minimal symbol table for main image
  2670. * main: code for main (text symbol)
  2671. * shr1: stub (trampoline symbol)
  2672. * foo: code for foo (text symbol)
  2673. * ...
  2674. * Minimal symbol table for "shr1" image:
  2675. * ...
  2676. * shr1: code for shr1 (text symbol)
  2677. * ...
  2678. *
  2679. * So the code below is trying to detect if we are in the stub
  2680. * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
  2681. * and if found, do the symbolization from the real-code address
  2682. * rather than the stub address.
  2683. *
  2684. * Assumptions being made about the minimal symbol table:
  2685. * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
  2686. * if we're really in the trampoline.s If we're beyond it (say
  2687. * we're in "foo" in the above example), it'll have a closer
  2688. * symbol (the "foo" text symbol for example) and will not
  2689. * return the trampoline.
  2690. * 2. lookup_minimal_symbol_text() will find a real text symbol
  2691. * corresponding to the trampoline, and whose address will
  2692. * be different than the trampoline address. I put in a sanity
  2693. * check for the address being the same, to avoid an
  2694. * infinite recursion.
  2695. */
  2696. msymbol = lookup_minimal_symbol_by_pc (pc);
  2697. if (msymbol.minsym != NULL)
  2698. if (MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
  2699. {
  2700. struct bound_minimal_symbol mfunsym
  2701. = lookup_minimal_symbol_text (msymbol.minsym->linkage_name (),
  2702. NULL);
  2703. if (mfunsym.minsym == NULL)
  2704. /* I eliminated this warning since it is coming out
  2705. * in the following situation:
  2706. * gdb shmain // test program with shared libraries
  2707. * (gdb) break shr1 // function in shared lib
  2708. * Warning: In stub for ...
  2709. * In the above situation, the shared lib is not loaded yet,
  2710. * so of course we can't find the real func/line info,
  2711. * but the "break" still works, and the warning is annoying.
  2712. * So I commented out the warning. RT */
  2713. /* warning ("In stub for %s; unable to find real function/line info",
  2714. msymbol->linkage_name ()); */
  2715. ;
  2716. /* fall through */
  2717. else if (BMSYMBOL_VALUE_ADDRESS (mfunsym)
  2718. == BMSYMBOL_VALUE_ADDRESS (msymbol))
  2719. /* Avoid infinite recursion */
  2720. /* See above comment about why warning is commented out. */
  2721. /* warning ("In stub for %s; unable to find real function/line info",
  2722. msymbol->linkage_name ()); */
  2723. ;
  2724. /* fall through */
  2725. else
  2726. {
  2727. /* Detect an obvious case of infinite recursion. If this
  2728. should occur, we'd like to know about it, so error out,
  2729. fatally. */
  2730. if (BMSYMBOL_VALUE_ADDRESS (mfunsym) == pc)
  2731. internal_error (__FILE__, __LINE__,
  2732. _("Infinite recursion detected in find_pc_sect_line;"
  2733. "please file a bug report"));
  2734. return find_pc_line (BMSYMBOL_VALUE_ADDRESS (mfunsym), 0);
  2735. }
  2736. }
  2737. symtab_and_line val;
  2738. val.pspace = current_program_space;
  2739. cust = find_pc_sect_compunit_symtab (pc, section);
  2740. if (cust == NULL)
  2741. {
  2742. /* If no symbol information, return previous pc. */
  2743. if (notcurrent)
  2744. pc++;
  2745. val.pc = pc;
  2746. return val;
  2747. }
  2748. bv = cust->blockvector ();
  2749. /* Look at all the symtabs that share this blockvector.
  2750. They all have the same apriori range, that we found was right;
  2751. but they have different line tables. */
  2752. for (symtab *iter_s : cust->filetabs ())
  2753. {
  2754. /* Find the best line in this symtab. */
  2755. l = iter_s->linetable ();
  2756. if (!l)
  2757. continue;
  2758. len = l->nitems;
  2759. if (len <= 0)
  2760. {
  2761. /* I think len can be zero if the symtab lacks line numbers
  2762. (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
  2763. I'm not sure which, and maybe it depends on the symbol
  2764. reader). */
  2765. continue;
  2766. }
  2767. prev = NULL;
  2768. item = l->item; /* Get first line info. */
  2769. /* Is this file's first line closer than the first lines of other files?
  2770. If so, record this file, and its first line, as best alternate. */
  2771. if (item->pc > pc && (!alt || item->pc < alt->pc))
  2772. alt = item;
  2773. auto pc_compare = [](const CORE_ADDR & comp_pc,
  2774. const struct linetable_entry & lhs)->bool
  2775. {
  2776. return comp_pc < lhs.pc;
  2777. };
  2778. struct linetable_entry *first = item;
  2779. struct linetable_entry *last = item + len;
  2780. item = std::upper_bound (first, last, pc, pc_compare);
  2781. if (item != first)
  2782. prev = item - 1; /* Found a matching item. */
  2783. /* At this point, prev points at the line whose start addr is <= pc, and
  2784. item points at the next line. If we ran off the end of the linetable
  2785. (pc >= start of the last line), then prev == item. If pc < start of
  2786. the first line, prev will not be set. */
  2787. /* Is this file's best line closer than the best in the other files?
  2788. If so, record this file, and its best line, as best so far. Don't
  2789. save prev if it represents the end of a function (i.e. line number
  2790. 0) instead of a real line. */
  2791. if (prev && prev->line && (!best || prev->pc > best->pc))
  2792. {
  2793. best = prev;
  2794. best_symtab = iter_s;
  2795. /* If during the binary search we land on a non-statement entry,
  2796. scan backward through entries at the same address to see if
  2797. there is an entry marked as is-statement. In theory this
  2798. duplication should have been removed from the line table
  2799. during construction, this is just a double check. If the line
  2800. table has had the duplication removed then this should be
  2801. pretty cheap. */
  2802. if (!best->is_stmt)
  2803. {
  2804. struct linetable_entry *tmp = best;
  2805. while (tmp > first && (tmp - 1)->pc == tmp->pc
  2806. && (tmp - 1)->line != 0 && !tmp->is_stmt)
  2807. --tmp;
  2808. if (tmp->is_stmt)
  2809. best = tmp;
  2810. }
  2811. /* Discard BEST_END if it's before the PC of the current BEST. */
  2812. if (best_end <= best->pc)
  2813. best_end = 0;
  2814. }
  2815. /* If another line (denoted by ITEM) is in the linetable and its
  2816. PC is after BEST's PC, but before the current BEST_END, then
  2817. use ITEM's PC as the new best_end. */
  2818. if (best && item < last && item->pc > best->pc
  2819. && (best_end == 0 || best_end > item->pc))
  2820. best_end = item->pc;
  2821. }
  2822. if (!best_symtab)
  2823. {
  2824. /* If we didn't find any line number info, just return zeros.
  2825. We used to return alt->line - 1 here, but that could be
  2826. anywhere; if we don't have line number info for this PC,
  2827. don't make some up. */
  2828. val.pc = pc;
  2829. }
  2830. else if (best->line == 0)
  2831. {
  2832. /* If our best fit is in a range of PC's for which no line
  2833. number info is available (line number is zero) then we didn't
  2834. find any valid line information. */
  2835. val.pc = pc;
  2836. }
  2837. else
  2838. {
  2839. val.is_stmt = best->is_stmt;
  2840. val.symtab = best_symtab;
  2841. val.line = best->line;
  2842. val.pc = best->pc;
  2843. if (best_end && (!alt || best_end < alt->pc))
  2844. val.end = best_end;
  2845. else if (alt)
  2846. val.end = alt->pc;
  2847. else
  2848. val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
  2849. }
  2850. val.section = section;
  2851. return val;
  2852. }
  2853. /* Backward compatibility (no section). */
  2854. struct symtab_and_line
  2855. find_pc_line (CORE_ADDR pc, int notcurrent)
  2856. {
  2857. struct obj_section *section;
  2858. section = find_pc_overlay (pc);
  2859. if (!pc_in_unmapped_range (pc, section))
  2860. return find_pc_sect_line (pc, section, notcurrent);
  2861. /* If the original PC was an unmapped address then we translate this to a
  2862. mapped address in order to lookup the sal. However, as the user
  2863. passed us an unmapped address it makes more sense to return a result
  2864. that has the pc and end fields translated to unmapped addresses. */
  2865. pc = overlay_mapped_address (pc, section);
  2866. symtab_and_line sal = find_pc_sect_line (pc, section, notcurrent);
  2867. sal.pc = overlay_unmapped_address (sal.pc, section);
  2868. sal.end = overlay_unmapped_address (sal.end, section);
  2869. return sal;
  2870. }
  2871. /* See symtab.h. */
  2872. struct symtab *
  2873. find_pc_line_symtab (CORE_ADDR pc)
  2874. {
  2875. struct symtab_and_line sal;
  2876. /* This always passes zero for NOTCURRENT to find_pc_line.
  2877. There are currently no callers that ever pass non-zero. */
  2878. sal = find_pc_line (pc, 0);
  2879. return sal.symtab;
  2880. }
  2881. /* Find line number LINE in any symtab whose name is the same as
  2882. SYMTAB.
  2883. If found, return the symtab that contains the linetable in which it was
  2884. found, set *INDEX to the index in the linetable of the best entry
  2885. found, and set *EXACT_MATCH to true if the value returned is an
  2886. exact match.
  2887. If not found, return NULL. */
  2888. struct symtab *
  2889. find_line_symtab (struct symtab *sym_tab, int line,
  2890. int *index, bool *exact_match)
  2891. {
  2892. int exact = 0; /* Initialized here to avoid a compiler warning. */
  2893. /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
  2894. so far seen. */
  2895. int best_index;
  2896. struct linetable *best_linetable;
  2897. struct symtab *best_symtab;
  2898. /* First try looking it up in the given symtab. */
  2899. best_linetable = sym_tab->linetable ();
  2900. best_symtab = sym_tab;
  2901. best_index = find_line_common (best_linetable, line, &exact, 0);
  2902. if (best_index < 0 || !exact)
  2903. {
  2904. /* Didn't find an exact match. So we better keep looking for
  2905. another symtab with the same name. In the case of xcoff,
  2906. multiple csects for one source file (produced by IBM's FORTRAN
  2907. compiler) produce multiple symtabs (this is unavoidable
  2908. assuming csects can be at arbitrary places in memory and that
  2909. the GLOBAL_BLOCK of a symtab has a begin and end address). */
  2910. /* BEST is the smallest linenumber > LINE so far seen,
  2911. or 0 if none has been seen so far.
  2912. BEST_INDEX and BEST_LINETABLE identify the item for it. */
  2913. int best;
  2914. if (best_index >= 0)
  2915. best = best_linetable->item[best_index].line;
  2916. else
  2917. best = 0;
  2918. for (objfile *objfile : current_program_space->objfiles ())
  2919. objfile->expand_symtabs_with_fullname (symtab_to_fullname (sym_tab));
  2920. for (objfile *objfile : current_program_space->objfiles ())
  2921. {
  2922. for (compunit_symtab *cu : objfile->compunits ())
  2923. {
  2924. for (symtab *s : cu->filetabs ())
  2925. {
  2926. struct linetable *l;
  2927. int ind;
  2928. if (FILENAME_CMP (sym_tab->filename, s->filename) != 0)
  2929. continue;
  2930. if (FILENAME_CMP (symtab_to_fullname (sym_tab),
  2931. symtab_to_fullname (s)) != 0)
  2932. continue;
  2933. l = s->linetable ();
  2934. ind = find_line_common (l, line, &exact, 0);
  2935. if (ind >= 0)
  2936. {
  2937. if (exact)
  2938. {
  2939. best_index = ind;
  2940. best_linetable = l;
  2941. best_symtab = s;
  2942. goto done;
  2943. }
  2944. if (best == 0 || l->item[ind].line < best)
  2945. {
  2946. best = l->item[ind].line;
  2947. best_index = ind;
  2948. best_linetable = l;
  2949. best_symtab = s;
  2950. }
  2951. }
  2952. }
  2953. }
  2954. }
  2955. }
  2956. done:
  2957. if (best_index < 0)
  2958. return NULL;
  2959. if (index)
  2960. *index = best_index;
  2961. if (exact_match)
  2962. *exact_match = (exact != 0);
  2963. return best_symtab;
  2964. }
  2965. /* Given SYMTAB, returns all the PCs function in the symtab that
  2966. exactly match LINE. Returns an empty vector if there are no exact
  2967. matches, but updates BEST_ITEM in this case. */
  2968. std::vector<CORE_ADDR>
  2969. find_pcs_for_symtab_line (struct symtab *symtab, int line,
  2970. struct linetable_entry **best_item)
  2971. {
  2972. int start = 0;
  2973. std::vector<CORE_ADDR> result;
  2974. /* First, collect all the PCs that are at this line. */
  2975. while (1)
  2976. {
  2977. int was_exact;
  2978. int idx;
  2979. idx = find_line_common (symtab->linetable (), line, &was_exact,
  2980. start);
  2981. if (idx < 0)
  2982. break;
  2983. if (!was_exact)
  2984. {
  2985. struct linetable_entry *item = &symtab->linetable ()->item[idx];
  2986. if (*best_item == NULL
  2987. || (item->line < (*best_item)->line && item->is_stmt))
  2988. *best_item = item;
  2989. break;
  2990. }
  2991. result.push_back (symtab->linetable ()->item[idx].pc);
  2992. start = idx + 1;
  2993. }
  2994. return result;
  2995. }
  2996. /* Set the PC value for a given source file and line number and return true.
  2997. Returns false for invalid line number (and sets the PC to 0).
  2998. The source file is specified with a struct symtab. */
  2999. bool
  3000. find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
  3001. {
  3002. struct linetable *l;
  3003. int ind;
  3004. *pc = 0;
  3005. if (symtab == 0)
  3006. return false;
  3007. symtab = find_line_symtab (symtab, line, &ind, NULL);
  3008. if (symtab != NULL)
  3009. {
  3010. l = symtab->linetable ();
  3011. *pc = l->item[ind].pc;
  3012. return true;
  3013. }
  3014. else
  3015. return false;
  3016. }
  3017. /* Find the range of pc values in a line.
  3018. Store the starting pc of the line into *STARTPTR
  3019. and the ending pc (start of next line) into *ENDPTR.
  3020. Returns true to indicate success.
  3021. Returns false if could not find the specified line. */
  3022. bool
  3023. find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
  3024. CORE_ADDR *endptr)
  3025. {
  3026. CORE_ADDR startaddr;
  3027. struct symtab_and_line found_sal;
  3028. startaddr = sal.pc;
  3029. if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
  3030. return false;
  3031. /* This whole function is based on address. For example, if line 10 has
  3032. two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
  3033. "info line *0x123" should say the line goes from 0x100 to 0x200
  3034. and "info line *0x355" should say the line goes from 0x300 to 0x400.
  3035. This also insures that we never give a range like "starts at 0x134
  3036. and ends at 0x12c". */
  3037. found_sal = find_pc_sect_line (startaddr, sal.section, 0);
  3038. if (found_sal.line != sal.line)
  3039. {
  3040. /* The specified line (sal) has zero bytes. */
  3041. *startptr = found_sal.pc;
  3042. *endptr = found_sal.pc;
  3043. }
  3044. else
  3045. {
  3046. *startptr = found_sal.pc;
  3047. *endptr = found_sal.end;
  3048. }
  3049. return true;
  3050. }
  3051. /* Given a line table and a line number, return the index into the line
  3052. table for the pc of the nearest line whose number is >= the specified one.
  3053. Return -1 if none is found. The value is >= 0 if it is an index.
  3054. START is the index at which to start searching the line table.
  3055. Set *EXACT_MATCH nonzero if the value returned is an exact match. */
  3056. static int
  3057. find_line_common (struct linetable *l, int lineno,
  3058. int *exact_match, int start)
  3059. {
  3060. int i;
  3061. int len;
  3062. /* BEST is the smallest linenumber > LINENO so far seen,
  3063. or 0 if none has been seen so far.
  3064. BEST_INDEX identifies the item for it. */
  3065. int best_index = -1;
  3066. int best = 0;
  3067. *exact_match = 0;
  3068. if (lineno <= 0)
  3069. return -1;
  3070. if (l == 0)
  3071. return -1;
  3072. len = l->nitems;
  3073. for (i = start; i < len; i++)
  3074. {
  3075. struct linetable_entry *item = &(l->item[i]);
  3076. /* Ignore non-statements. */
  3077. if (!item->is_stmt)
  3078. continue;
  3079. if (item->line == lineno)
  3080. {
  3081. /* Return the first (lowest address) entry which matches. */
  3082. *exact_match = 1;
  3083. return i;
  3084. }
  3085. if (item->line > lineno && (best == 0 || item->line < best))
  3086. {
  3087. best = item->line;
  3088. best_index = i;
  3089. }
  3090. }
  3091. /* If we got here, we didn't get an exact match. */
  3092. return best_index;
  3093. }
  3094. bool
  3095. find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
  3096. {
  3097. struct symtab_and_line sal;
  3098. sal = find_pc_line (pc, 0);
  3099. *startptr = sal.pc;
  3100. *endptr = sal.end;
  3101. return sal.symtab != 0;
  3102. }
  3103. /* Helper for find_function_start_sal. Does most of the work, except
  3104. setting the sal's symbol. */
  3105. static symtab_and_line
  3106. find_function_start_sal_1 (CORE_ADDR func_addr, obj_section *section,
  3107. bool funfirstline)
  3108. {
  3109. symtab_and_line sal = find_pc_sect_line (func_addr, section, 0);
  3110. if (funfirstline && sal.symtab != NULL
  3111. && (sal.symtab->compunit ()->locations_valid ()
  3112. || sal.symtab->language () == language_asm))
  3113. {
  3114. struct gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
  3115. sal.pc = func_addr;
  3116. if (gdbarch_skip_entrypoint_p (gdbarch))
  3117. sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
  3118. return sal;
  3119. }
  3120. /* We always should have a line for the function start address.
  3121. If we don't, something is odd. Create a plain SAL referring
  3122. just the PC and hope that skip_prologue_sal (if requested)
  3123. can find a line number for after the prologue. */
  3124. if (sal.pc < func_addr)
  3125. {
  3126. sal = {};
  3127. sal.pspace = current_program_space;
  3128. sal.pc = func_addr;
  3129. sal.section = section;
  3130. }
  3131. if (funfirstline)
  3132. skip_prologue_sal (&sal);
  3133. return sal;
  3134. }
  3135. /* See symtab.h. */
  3136. symtab_and_line
  3137. find_function_start_sal (CORE_ADDR func_addr, obj_section *section,
  3138. bool funfirstline)
  3139. {
  3140. symtab_and_line sal
  3141. = find_function_start_sal_1 (func_addr, section, funfirstline);
  3142. /* find_function_start_sal_1 does a linetable search, so it finds
  3143. the symtab and linenumber, but not a symbol. Fill in the
  3144. function symbol too. */
  3145. sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
  3146. return sal;
  3147. }
  3148. /* See symtab.h. */
  3149. symtab_and_line
  3150. find_function_start_sal (symbol *sym, bool funfirstline)
  3151. {
  3152. fixup_symbol_section (sym, NULL);
  3153. symtab_and_line sal
  3154. = find_function_start_sal_1 (BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)),
  3155. sym->obj_section (symbol_objfile (sym)),
  3156. funfirstline);
  3157. sal.symbol = sym;
  3158. return sal;
  3159. }
  3160. /* Given a function start address FUNC_ADDR and SYMTAB, find the first
  3161. address for that function that has an entry in SYMTAB's line info
  3162. table. If such an entry cannot be found, return FUNC_ADDR
  3163. unaltered. */
  3164. static CORE_ADDR
  3165. skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
  3166. {
  3167. CORE_ADDR func_start, func_end;
  3168. struct linetable *l;
  3169. int i;
  3170. /* Give up if this symbol has no lineinfo table. */
  3171. l = symtab->linetable ();
  3172. if (l == NULL)
  3173. return func_addr;
  3174. /* Get the range for the function's PC values, or give up if we
  3175. cannot, for some reason. */
  3176. if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
  3177. return func_addr;
  3178. /* Linetable entries are ordered by PC values, see the commentary in
  3179. symtab.h where `struct linetable' is defined. Thus, the first
  3180. entry whose PC is in the range [FUNC_START..FUNC_END[ is the
  3181. address we are looking for. */
  3182. for (i = 0; i < l->nitems; i++)
  3183. {
  3184. struct linetable_entry *item = &(l->item[i]);
  3185. /* Don't use line numbers of zero, they mark special entries in
  3186. the table. See the commentary on symtab.h before the
  3187. definition of struct linetable. */
  3188. if (item->line > 0 && func_start <= item->pc && item->pc < func_end)
  3189. return item->pc;
  3190. }
  3191. return func_addr;
  3192. }
  3193. /* Try to locate the address where a breakpoint should be placed past the
  3194. prologue of function starting at FUNC_ADDR using the line table.
  3195. Return the address associated with the first entry in the line-table for
  3196. the function starting at FUNC_ADDR which has prologue_end set to true if
  3197. such entry exist, otherwise return an empty optional. */
  3198. static gdb::optional<CORE_ADDR>
  3199. skip_prologue_using_linetable (CORE_ADDR func_addr)
  3200. {
  3201. CORE_ADDR start_pc, end_pc;
  3202. if (!find_pc_partial_function (func_addr, nullptr, &start_pc, &end_pc))
  3203. return {};
  3204. const struct symtab_and_line prologue_sal = find_pc_line (start_pc, 0);
  3205. if (prologue_sal.symtab != nullptr
  3206. && prologue_sal.symtab->language () != language_asm)
  3207. {
  3208. struct linetable *linetable = prologue_sal.symtab->linetable ();
  3209. auto it = std::lower_bound
  3210. (linetable->item, linetable->item + linetable->nitems, start_pc,
  3211. [] (const linetable_entry &lte, CORE_ADDR pc) -> bool
  3212. {
  3213. return lte.pc < pc;
  3214. });
  3215. for (;
  3216. it < linetable->item + linetable->nitems && it->pc <= end_pc;
  3217. it++)
  3218. if (it->prologue_end)
  3219. return {it->pc};
  3220. }
  3221. return {};
  3222. }
  3223. /* Adjust SAL to the first instruction past the function prologue.
  3224. If the PC was explicitly specified, the SAL is not changed.
  3225. If the line number was explicitly specified then the SAL can still be
  3226. updated, unless the language for SAL is assembler, in which case the SAL
  3227. will be left unchanged.
  3228. If SAL is already past the prologue, then do nothing. */
  3229. void
  3230. skip_prologue_sal (struct symtab_and_line *sal)
  3231. {
  3232. struct symbol *sym;
  3233. struct symtab_and_line start_sal;
  3234. CORE_ADDR pc, saved_pc;
  3235. struct obj_section *section;
  3236. const char *name;
  3237. struct objfile *objfile;
  3238. struct gdbarch *gdbarch;
  3239. const struct block *b, *function_block;
  3240. int force_skip, skip;
  3241. /* Do not change the SAL if PC was specified explicitly. */
  3242. if (sal->explicit_pc)
  3243. return;
  3244. /* In assembly code, if the user asks for a specific line then we should
  3245. not adjust the SAL. The user already has instruction level
  3246. visibility in this case, so selecting a line other than one requested
  3247. is likely to be the wrong choice. */
  3248. if (sal->symtab != nullptr
  3249. && sal->explicit_line
  3250. && sal->symtab->language () == language_asm)
  3251. return;
  3252. scoped_restore_current_pspace_and_thread restore_pspace_thread;
  3253. switch_to_program_space_and_thread (sal->pspace);
  3254. sym = find_pc_sect_function (sal->pc, sal->section);
  3255. if (sym != NULL)
  3256. {
  3257. fixup_symbol_section (sym, NULL);
  3258. objfile = symbol_objfile (sym);
  3259. pc = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
  3260. section = sym->obj_section (objfile);
  3261. name = sym->linkage_name ();
  3262. }
  3263. else
  3264. {
  3265. struct bound_minimal_symbol msymbol
  3266. = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);
  3267. if (msymbol.minsym == NULL)
  3268. return;
  3269. objfile = msymbol.objfile;
  3270. pc = BMSYMBOL_VALUE_ADDRESS (msymbol);
  3271. section = msymbol.minsym->obj_section (objfile);
  3272. name = msymbol.minsym->linkage_name ();
  3273. }
  3274. gdbarch = objfile->arch ();
  3275. /* Process the prologue in two passes. In the first pass try to skip the
  3276. prologue (SKIP is true) and verify there is a real need for it (indicated
  3277. by FORCE_SKIP). If no such reason was found run a second pass where the
  3278. prologue is not skipped (SKIP is false). */
  3279. skip = 1;
  3280. force_skip = 1;
  3281. /* Be conservative - allow direct PC (without skipping prologue) only if we
  3282. have proven the CU (Compilation Unit) supports it. sal->SYMTAB does not
  3283. have to be set by the caller so we use SYM instead. */
  3284. if (sym != NULL
  3285. && symbol_symtab (sym)->compunit ()->locations_valid ())
  3286. force_skip = 0;
  3287. saved_pc = pc;
  3288. do
  3289. {
  3290. pc = saved_pc;
  3291. /* Check if the compiler explicitly indicated where a breakpoint should
  3292. be placed to skip the prologue. */
  3293. if (!ignore_prologue_end_flag && skip)
  3294. {
  3295. gdb::optional<CORE_ADDR> linetable_pc
  3296. = skip_prologue_using_linetable (pc);
  3297. if (linetable_pc)
  3298. {
  3299. pc = *linetable_pc;
  3300. start_sal = find_pc_sect_line (pc, section, 0);
  3301. force_skip = 1;
  3302. continue;
  3303. }
  3304. }
  3305. /* If the function is in an unmapped overlay, use its unmapped LMA address,
  3306. so that gdbarch_skip_prologue has something unique to work on. */
  3307. if (section_is_overlay (section) && !section_is_mapped (section))
  3308. pc = overlay_unmapped_address (pc, section);
  3309. /* Skip "first line" of function (which is actually its prologue). */
  3310. pc += gdbarch_deprecated_function_start_offset (gdbarch);
  3311. if (gdbarch_skip_entrypoint_p (gdbarch))
  3312. pc = gdbarch_skip_entrypoint (gdbarch, pc);
  3313. if (skip)
  3314. pc = gdbarch_skip_prologue_noexcept (gdbarch, pc);
  3315. /* For overlays, map pc back into its mapped VMA range. */
  3316. pc = overlay_mapped_address (pc, section);
  3317. /* Calculate line number. */
  3318. start_sal = find_pc_sect_line (pc, section, 0);
  3319. /* Check if gdbarch_skip_prologue left us in mid-line, and the next
  3320. line is still part of the same function. */
  3321. if (skip && start_sal.pc != pc
  3322. && (sym ? (BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) <= start_sal.end
  3323. && start_sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
  3324. : (lookup_minimal_symbol_by_pc_section (start_sal.end, section).minsym
  3325. == lookup_minimal_symbol_by_pc_section (pc, section).minsym)))
  3326. {
  3327. /* First pc of next line */
  3328. pc = start_sal.end;
  3329. /* Recalculate the line number (might not be N+1). */
  3330. start_sal = find_pc_sect_line (pc, section, 0);
  3331. }
  3332. /* On targets with executable formats that don't have a concept of
  3333. constructors (ELF with .init has, PE doesn't), gcc emits a call
  3334. to `__main' in `main' between the prologue and before user
  3335. code. */
  3336. if (gdbarch_skip_main_prologue_p (gdbarch)
  3337. && name && strcmp_iw (name, "main") == 0)
  3338. {
  3339. pc = gdbarch_skip_main_prologue (gdbarch, pc);
  3340. /* Recalculate the line number (might not be N+1). */
  3341. start_sal = find_pc_sect_line (pc, section, 0);
  3342. force_skip = 1;
  3343. }
  3344. }
  3345. while (!force_skip && skip--);
  3346. /* If we still don't have a valid source line, try to find the first
  3347. PC in the lineinfo table that belongs to the same function. This
  3348. happens with COFF debug info, which does not seem to have an
  3349. entry in lineinfo table for the code after the prologue which has
  3350. no direct relation to source. For example, this was found to be
  3351. the case with the DJGPP target using "gcc -gcoff" when the
  3352. compiler inserted code after the prologue to make sure the stack
  3353. is aligned. */
  3354. if (!force_skip && sym && start_sal.symtab == NULL)
  3355. {
  3356. pc = skip_prologue_using_lineinfo (pc, symbol_symtab (sym));
  3357. /* Recalculate the line number. */
  3358. start_sal = find_pc_sect_line (pc, section, 0);
  3359. }
  3360. /* If we're already past the prologue, leave SAL unchanged. Otherwise
  3361. forward SAL to the end of the prologue. */
  3362. if (sal->pc >= pc)
  3363. return;
  3364. sal->pc = pc;
  3365. sal->section = section;
  3366. sal->symtab = start_sal.symtab;
  3367. sal->line = start_sal.line;
  3368. sal->end = start_sal.end;
  3369. /* Check if we are now inside an inlined function. If we can,
  3370. use the call site of the function instead. */
  3371. b = block_for_pc_sect (sal->pc, sal->section);
  3372. function_block = NULL;
  3373. while (b != NULL)
  3374. {
  3375. if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
  3376. function_block = b;
  3377. else if (BLOCK_FUNCTION (b) != NULL)
  3378. break;
  3379. b = BLOCK_SUPERBLOCK (b);
  3380. }
  3381. if (function_block != NULL
  3382. && BLOCK_FUNCTION (function_block)->line () != 0)
  3383. {
  3384. sal->line = BLOCK_FUNCTION (function_block)->line ();
  3385. sal->symtab = symbol_symtab (BLOCK_FUNCTION (function_block));
  3386. }
  3387. }
  3388. /* Given PC at the function's start address, attempt to find the
  3389. prologue end using SAL information. Return zero if the skip fails.
  3390. A non-optimized prologue traditionally has one SAL for the function
  3391. and a second for the function body. A single line function has
  3392. them both pointing at the same line.
  3393. An optimized prologue is similar but the prologue may contain
  3394. instructions (SALs) from the instruction body. Need to skip those
  3395. while not getting into the function body.
  3396. The functions end point and an increasing SAL line are used as
  3397. indicators of the prologue's endpoint.
  3398. This code is based on the function refine_prologue_limit
  3399. (found in ia64). */
  3400. CORE_ADDR
  3401. skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
  3402. {
  3403. struct symtab_and_line prologue_sal;
  3404. CORE_ADDR start_pc;
  3405. CORE_ADDR end_pc;
  3406. const struct block *bl;
  3407. /* Get an initial range for the function. */
  3408. find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
  3409. start_pc += gdbarch_deprecated_function_start_offset (gdbarch);
  3410. prologue_sal = find_pc_line (start_pc, 0);
  3411. if (prologue_sal.line != 0)
  3412. {
  3413. /* For languages other than assembly, treat two consecutive line
  3414. entries at the same address as a zero-instruction prologue.
  3415. The GNU assembler emits separate line notes for each instruction
  3416. in a multi-instruction macro, but compilers generally will not
  3417. do this. */
  3418. if (prologue_sal.symtab->language () != language_asm)
  3419. {
  3420. struct linetable *linetable = prologue_sal.symtab->linetable ();
  3421. int idx = 0;
  3422. /* Skip any earlier lines, and any end-of-sequence marker
  3423. from a previous function. */
  3424. while (linetable->item[idx].pc != prologue_sal.pc
  3425. || linetable->item[idx].line == 0)
  3426. idx++;
  3427. if (idx+1 < linetable->nitems
  3428. && linetable->item[idx+1].line != 0
  3429. && linetable->item[idx+1].pc == start_pc)
  3430. return start_pc;
  3431. }
  3432. /* If there is only one sal that covers the entire function,
  3433. then it is probably a single line function, like
  3434. "foo(){}". */
  3435. if (prologue_sal.end >= end_pc)
  3436. return 0;
  3437. while (prologue_sal.end < end_pc)
  3438. {
  3439. struct symtab_and_line sal;
  3440. sal = find_pc_line (prologue_sal.end, 0);
  3441. if (sal.line == 0)
  3442. break;
  3443. /* Assume that a consecutive SAL for the same (or larger)
  3444. line mark the prologue -> body transition. */
  3445. if (sal.line >= prologue_sal.line)
  3446. break;
  3447. /* Likewise if we are in a different symtab altogether
  3448. (e.g. within a file included via #include).  */
  3449. if (sal.symtab != prologue_sal.symtab)
  3450. break;
  3451. /* The line number is smaller. Check that it's from the
  3452. same function, not something inlined. If it's inlined,
  3453. then there is no point comparing the line numbers. */
  3454. bl = block_for_pc (prologue_sal.end);
  3455. while (bl)
  3456. {
  3457. if (block_inlined_p (bl))
  3458. break;
  3459. if (BLOCK_FUNCTION (bl))
  3460. {
  3461. bl = NULL;
  3462. break;
  3463. }
  3464. bl = BLOCK_SUPERBLOCK (bl);
  3465. }
  3466. if (bl != NULL)
  3467. break;
  3468. /* The case in which compiler's optimizer/scheduler has
  3469. moved instructions into the prologue. We look ahead in
  3470. the function looking for address ranges whose
  3471. corresponding line number is less the first one that we
  3472. found for the function. This is more conservative then
  3473. refine_prologue_limit which scans a large number of SALs
  3474. looking for any in the prologue. */
  3475. prologue_sal = sal;
  3476. }
  3477. }
  3478. if (prologue_sal.end < end_pc)
  3479. /* Return the end of this line, or zero if we could not find a
  3480. line. */
  3481. return prologue_sal.end;
  3482. else
  3483. /* Don't return END_PC, which is past the end of the function. */
  3484. return prologue_sal.pc;
  3485. }
  3486. /* See symtab.h. */
  3487. symbol *
  3488. find_function_alias_target (bound_minimal_symbol msymbol)
  3489. {
  3490. CORE_ADDR func_addr;
  3491. if (!msymbol_is_function (msymbol.objfile, msymbol.minsym, &func_addr))
  3492. return NULL;
  3493. symbol *sym = find_pc_function (func_addr);
  3494. if (sym != NULL
  3495. && sym->aclass () == LOC_BLOCK
  3496. && BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) == func_addr)
  3497. return sym;
  3498. return NULL;
  3499. }
  3500. /* If P is of the form "operator[ \t]+..." where `...' is
  3501. some legitimate operator text, return a pointer to the
  3502. beginning of the substring of the operator text.
  3503. Otherwise, return "". */
  3504. static const char *
  3505. operator_chars (const char *p, const char **end)
  3506. {
  3507. *end = "";
  3508. if (!startswith (p, CP_OPERATOR_STR))
  3509. return *end;
  3510. p += CP_OPERATOR_LEN;
  3511. /* Don't get faked out by `operator' being part of a longer
  3512. identifier. */
  3513. if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
  3514. return *end;
  3515. /* Allow some whitespace between `operator' and the operator symbol. */
  3516. while (*p == ' ' || *p == '\t')
  3517. p++;
  3518. /* Recognize 'operator TYPENAME'. */
  3519. if (isalpha (*p) || *p == '_' || *p == '$')
  3520. {
  3521. const char *q = p + 1;
  3522. while (isalnum (*q) || *q == '_' || *q == '$')
  3523. q++;
  3524. *end = q;
  3525. return p;
  3526. }
  3527. while (*p)
  3528. switch (*p)
  3529. {
  3530. case '\\': /* regexp quoting */
  3531. if (p[1] == '*')
  3532. {
  3533. if (p[2] == '=') /* 'operator\*=' */
  3534. *end = p + 3;
  3535. else /* 'operator\*' */
  3536. *end = p + 2;
  3537. return p;
  3538. }
  3539. else if (p[1] == '[')
  3540. {
  3541. if (p[2] == ']')
  3542. error (_("mismatched quoting on brackets, "
  3543. "try 'operator\\[\\]'"));
  3544. else if (p[2] == '\\' && p[3] == ']')
  3545. {
  3546. *end = p + 4; /* 'operator\[\]' */
  3547. return p;
  3548. }
  3549. else
  3550. error (_("nothing is allowed between '[' and ']'"));
  3551. }
  3552. else
  3553. {
  3554. /* Gratuitous quote: skip it and move on. */
  3555. p++;
  3556. continue;
  3557. }
  3558. break;
  3559. case '!':
  3560. case '=':
  3561. case '*':
  3562. case '/':
  3563. case '%':
  3564. case '^':
  3565. if (p[1] == '=')
  3566. *end = p + 2;
  3567. else
  3568. *end = p + 1;
  3569. return p;
  3570. case '<':
  3571. case '>':
  3572. case '+':
  3573. case '-':
  3574. case '&':
  3575. case '|':
  3576. if (p[0] == '-' && p[1] == '>')
  3577. {
  3578. /* Struct pointer member operator 'operator->'. */
  3579. if (p[2] == '*')
  3580. {
  3581. *end = p + 3; /* 'operator->*' */
  3582. return p;
  3583. }
  3584. else if (p[2] == '\\')
  3585. {
  3586. *end = p + 4; /* Hopefully 'operator->\*' */
  3587. return p;
  3588. }
  3589. else
  3590. {
  3591. *end = p + 2; /* 'operator->' */
  3592. return p;
  3593. }
  3594. }
  3595. if (p[1] == '=' || p[1] == p[0])
  3596. *end = p + 2;
  3597. else
  3598. *end = p + 1;
  3599. return p;
  3600. case '~':
  3601. case ',':
  3602. *end = p + 1;
  3603. return p;
  3604. case '(':
  3605. if (p[1] != ')')
  3606. error (_("`operator ()' must be specified "
  3607. "without whitespace in `()'"));
  3608. *end = p + 2;
  3609. return p;
  3610. case '?':
  3611. if (p[1] != ':')
  3612. error (_("`operator ?:' must be specified "
  3613. "without whitespace in `?:'"));
  3614. *end = p + 2;
  3615. return p;
  3616. case '[':
  3617. if (p[1] != ']')
  3618. error (_("`operator []' must be specified "
  3619. "without whitespace in `[]'"));
  3620. *end = p + 2;
  3621. return p;
  3622. default:
  3623. error (_("`operator %s' not supported"), p);
  3624. break;
  3625. }
  3626. *end = "";
  3627. return *end;
  3628. }
  3629. /* See class declaration. */
  3630. info_sources_filter::info_sources_filter (match_on match_type,
  3631. const char *regexp)
  3632. : m_match_type (match_type),
  3633. m_regexp (regexp)
  3634. {
  3635. /* Setup the compiled regular expression M_C_REGEXP based on M_REGEXP. */
  3636. if (m_regexp != nullptr && *m_regexp != '\0')
  3637. {
  3638. gdb_assert (m_regexp != nullptr);
  3639. int cflags = REG_NOSUB;
  3640. #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
  3641. cflags |= REG_ICASE;
  3642. #endif
  3643. m_c_regexp.emplace (m_regexp, cflags, _("Invalid regexp"));
  3644. }
  3645. }
  3646. /* See class declaration. */
  3647. bool
  3648. info_sources_filter::matches (const char *fullname) const
  3649. {
  3650. /* Does it match regexp? */
  3651. if (m_c_regexp.has_value ())
  3652. {
  3653. const char *to_match;
  3654. std::string dirname;
  3655. switch (m_match_type)
  3656. {
  3657. case match_on::DIRNAME:
  3658. dirname = ldirname (fullname);
  3659. to_match = dirname.c_str ();
  3660. break;
  3661. case match_on::BASENAME:
  3662. to_match = lbasename (fullname);
  3663. break;
  3664. case match_on::FULLNAME:
  3665. to_match = fullname;
  3666. break;
  3667. default:
  3668. gdb_assert_not_reached ("bad m_match_type");
  3669. }
  3670. if (m_c_regexp->exec (to_match, 0, NULL, 0) != 0)
  3671. return false;
  3672. }
  3673. return true;
  3674. }
  3675. /* Data structure to maintain the state used for printing the results of
  3676. the 'info sources' command. */
  3677. struct output_source_filename_data
  3678. {
  3679. /* Create an object for displaying the results of the 'info sources'
  3680. command to UIOUT. FILTER must remain valid and unchanged for the
  3681. lifetime of this object as this object retains a reference to FILTER. */
  3682. output_source_filename_data (struct ui_out *uiout,
  3683. const info_sources_filter &filter)
  3684. : m_filter (filter),
  3685. m_uiout (uiout)
  3686. { /* Nothing. */ }
  3687. DISABLE_COPY_AND_ASSIGN (output_source_filename_data);
  3688. /* Reset enough state of this object so we can match against a new set of
  3689. files. The existing regular expression is retained though. */
  3690. void reset_output ()
  3691. {
  3692. m_first = true;
  3693. m_filename_seen_cache.clear ();
  3694. }
  3695. /* Worker for sources_info, outputs the file name formatted for either
  3696. cli or mi (based on the current_uiout). In cli mode displays
  3697. FULLNAME with a comma separating this name from any previously
  3698. printed name (line breaks are added at the comma). In MI mode
  3699. outputs a tuple containing DISP_NAME (the files display name),
  3700. FULLNAME, and EXPANDED_P (true when this file is from a fully
  3701. expanded symtab, otherwise false). */
  3702. void output (const char *disp_name, const char *fullname, bool expanded_p);
  3703. /* An overload suitable for use as a callback to
  3704. quick_symbol_functions::map_symbol_filenames. */
  3705. void operator() (const char *filename, const char *fullname)
  3706. {
  3707. /* The false here indicates that this file is from an unexpanded
  3708. symtab. */
  3709. output (filename, fullname, false);
  3710. }
  3711. /* Return true if at least one filename has been printed (after a call to
  3712. output) since either this object was created, or the last call to
  3713. reset_output. */
  3714. bool printed_filename_p () const
  3715. {
  3716. return !m_first;
  3717. }
  3718. private:
  3719. /* Flag of whether we're printing the first one. */
  3720. bool m_first = true;
  3721. /* Cache of what we've seen so far. */
  3722. filename_seen_cache m_filename_seen_cache;
  3723. /* How source filename should be filtered. */
  3724. const info_sources_filter &m_filter;
  3725. /* The object to which output is sent. */
  3726. struct ui_out *m_uiout;
  3727. };
  3728. /* See comment in class declaration above. */
  3729. void
  3730. output_source_filename_data::output (const char *disp_name,
  3731. const char *fullname,
  3732. bool expanded_p)
  3733. {
  3734. /* Since a single source file can result in several partial symbol
  3735. tables, we need to avoid printing it more than once. Note: if
  3736. some of the psymtabs are read in and some are not, it gets
  3737. printed both under "Source files for which symbols have been
  3738. read" and "Source files for which symbols will be read in on
  3739. demand". I consider this a reasonable way to deal with the
  3740. situation. I'm not sure whether this can also happen for
  3741. symtabs; it doesn't hurt to check. */
  3742. /* Was NAME already seen? If so, then don't print it again. */
  3743. if (m_filename_seen_cache.seen (fullname))
  3744. return;
  3745. /* If the filter rejects this file then don't print it. */
  3746. if (!m_filter.matches (fullname))
  3747. return;
  3748. ui_out_emit_tuple ui_emitter (m_uiout, nullptr);
  3749. /* Print it and reset *FIRST. */
  3750. if (!m_first)
  3751. m_uiout->text (", ");
  3752. m_first = false;
  3753. m_uiout->wrap_hint (0);
  3754. if (m_uiout->is_mi_like_p ())
  3755. {
  3756. m_uiout->field_string ("file", disp_name, file_name_style.style ());
  3757. if (fullname != nullptr)
  3758. m_uiout->field_string ("fullname", fullname,
  3759. file_name_style.style ());
  3760. m_uiout->field_string ("debug-fully-read",
  3761. (expanded_p ? "true" : "false"));
  3762. }
  3763. else
  3764. {
  3765. if (fullname == nullptr)
  3766. fullname = disp_name;
  3767. m_uiout->field_string ("fullname", fullname,
  3768. file_name_style.style ());
  3769. }
  3770. }
  3771. /* For the 'info sources' command, what part of the file names should we be
  3772. matching the user supplied regular expression against? */
  3773. struct filename_partial_match_opts
  3774. {
  3775. /* Only match the directory name part. */
  3776. bool dirname = false;
  3777. /* Only match the basename part. */
  3778. bool basename = false;
  3779. };
  3780. using isrc_flag_option_def
  3781. = gdb::option::flag_option_def<filename_partial_match_opts>;
  3782. static const gdb::option::option_def info_sources_option_defs[] = {
  3783. isrc_flag_option_def {
  3784. "dirname",
  3785. [] (filename_partial_match_opts *opts) { return &opts->dirname; },
  3786. N_("Show only the files having a dirname matching REGEXP."),
  3787. },
  3788. isrc_flag_option_def {
  3789. "basename",
  3790. [] (filename_partial_match_opts *opts) { return &opts->basename; },
  3791. N_("Show only the files having a basename matching REGEXP."),
  3792. },
  3793. };
  3794. /* Create an option_def_group for the "info sources" options, with
  3795. ISRC_OPTS as context. */
  3796. static inline gdb::option::option_def_group
  3797. make_info_sources_options_def_group (filename_partial_match_opts *isrc_opts)
  3798. {
  3799. return {{info_sources_option_defs}, isrc_opts};
  3800. }
  3801. /* Completer for "info sources". */
  3802. static void
  3803. info_sources_command_completer (cmd_list_element *ignore,
  3804. completion_tracker &tracker,
  3805. const char *text, const char *word)
  3806. {
  3807. const auto group = make_info_sources_options_def_group (nullptr);
  3808. if (gdb::option::complete_options
  3809. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
  3810. return;
  3811. }
  3812. /* See symtab.h. */
  3813. void
  3814. info_sources_worker (struct ui_out *uiout,
  3815. bool group_by_objfile,
  3816. const info_sources_filter &filter)
  3817. {
  3818. output_source_filename_data data (uiout, filter);
  3819. ui_out_emit_list results_emitter (uiout, "files");
  3820. gdb::optional<ui_out_emit_tuple> output_tuple;
  3821. gdb::optional<ui_out_emit_list> sources_list;
  3822. gdb_assert (group_by_objfile || uiout->is_mi_like_p ());
  3823. for (objfile *objfile : current_program_space->objfiles ())
  3824. {
  3825. if (group_by_objfile)
  3826. {
  3827. output_tuple.emplace (uiout, nullptr);
  3828. uiout->field_string ("filename", objfile_name (objfile),
  3829. file_name_style.style ());
  3830. uiout->text (":\n");
  3831. bool debug_fully_readin = !objfile->has_unexpanded_symtabs ();
  3832. if (uiout->is_mi_like_p ())
  3833. {
  3834. const char *debug_info_state;
  3835. if (objfile_has_symbols (objfile))
  3836. {
  3837. if (debug_fully_readin)
  3838. debug_info_state = "fully-read";
  3839. else
  3840. debug_info_state = "partially-read";
  3841. }
  3842. else
  3843. debug_info_state = "none";
  3844. current_uiout->field_string ("debug-info", debug_info_state);
  3845. }
  3846. else
  3847. {
  3848. if (!debug_fully_readin)
  3849. uiout->text ("(Full debug information has not yet been read "
  3850. "for this file.)\n");
  3851. if (!objfile_has_symbols (objfile))
  3852. uiout->text ("(Objfile has no debug information.)\n");
  3853. uiout->text ("\n");
  3854. }
  3855. sources_list.emplace (uiout, "sources");
  3856. }
  3857. for (compunit_symtab *cu : objfile->compunits ())
  3858. {
  3859. for (symtab *s : cu->filetabs ())
  3860. {
  3861. const char *file = symtab_to_filename_for_display (s);
  3862. const char *fullname = symtab_to_fullname (s);
  3863. data.output (file, fullname, true);
  3864. }
  3865. }
  3866. if (group_by_objfile)
  3867. {
  3868. objfile->map_symbol_filenames (data, true /* need_fullname */);
  3869. if (data.printed_filename_p ())
  3870. uiout->text ("\n\n");
  3871. data.reset_output ();
  3872. sources_list.reset ();
  3873. output_tuple.reset ();
  3874. }
  3875. }
  3876. if (!group_by_objfile)
  3877. {
  3878. data.reset_output ();
  3879. map_symbol_filenames (data, true /*need_fullname*/);
  3880. }
  3881. }
  3882. /* Implement the 'info sources' command. */
  3883. static void
  3884. info_sources_command (const char *args, int from_tty)
  3885. {
  3886. if (!have_full_symbols () && !have_partial_symbols ())
  3887. error (_("No symbol table is loaded. Use the \"file\" command."));
  3888. filename_partial_match_opts match_opts;
  3889. auto group = make_info_sources_options_def_group (&match_opts);
  3890. gdb::option::process_options
  3891. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
  3892. if (match_opts.dirname && match_opts.basename)
  3893. error (_("You cannot give both -basename and -dirname to 'info sources'."));
  3894. const char *regex = nullptr;
  3895. if (args != NULL && *args != '\000')
  3896. regex = args;
  3897. if ((match_opts.dirname || match_opts.basename) && regex == nullptr)
  3898. error (_("Missing REGEXP for 'info sources'."));
  3899. info_sources_filter::match_on match_type;
  3900. if (match_opts.dirname)
  3901. match_type = info_sources_filter::match_on::DIRNAME;
  3902. else if (match_opts.basename)
  3903. match_type = info_sources_filter::match_on::BASENAME;
  3904. else
  3905. match_type = info_sources_filter::match_on::FULLNAME;
  3906. info_sources_filter filter (match_type, regex);
  3907. info_sources_worker (current_uiout, true, filter);
  3908. }
  3909. /* Compare FILE against all the entries of FILENAMES. If BASENAMES is
  3910. true compare only lbasename of FILENAMES. */
  3911. static bool
  3912. file_matches (const char *file, const std::vector<const char *> &filenames,
  3913. bool basenames)
  3914. {
  3915. if (filenames.empty ())
  3916. return true;
  3917. for (const char *name : filenames)
  3918. {
  3919. name = (basenames ? lbasename (name) : name);
  3920. if (compare_filenames_for_search (file, name))
  3921. return true;
  3922. }
  3923. return false;
  3924. }
  3925. /* Helper function for std::sort on symbol_search objects. Can only sort
  3926. symbols, not minimal symbols. */
  3927. int
  3928. symbol_search::compare_search_syms (const symbol_search &sym_a,
  3929. const symbol_search &sym_b)
  3930. {
  3931. int c;
  3932. c = FILENAME_CMP (symbol_symtab (sym_a.symbol)->filename,
  3933. symbol_symtab (sym_b.symbol)->filename);
  3934. if (c != 0)
  3935. return c;
  3936. if (sym_a.block != sym_b.block)
  3937. return sym_a.block - sym_b.block;
  3938. return strcmp (sym_a.symbol->print_name (), sym_b.symbol->print_name ());
  3939. }
  3940. /* Returns true if the type_name of symbol_type of SYM matches TREG.
  3941. If SYM has no symbol_type or symbol_name, returns false. */
  3942. bool
  3943. treg_matches_sym_type_name (const compiled_regex &treg,
  3944. const struct symbol *sym)
  3945. {
  3946. struct type *sym_type;
  3947. std::string printed_sym_type_name;
  3948. if (symbol_lookup_debug > 1)
  3949. {
  3950. gdb_printf (gdb_stdlog,
  3951. "treg_matches_sym_type_name\n sym %s\n",
  3952. sym->natural_name ());
  3953. }
  3954. sym_type = sym->type ();
  3955. if (sym_type == NULL)
  3956. return false;
  3957. {
  3958. scoped_switch_to_sym_language_if_auto l (sym);
  3959. printed_sym_type_name = type_to_string (sym_type);
  3960. }
  3961. if (symbol_lookup_debug > 1)
  3962. {
  3963. gdb_printf (gdb_stdlog,
  3964. " sym_type_name %s\n",
  3965. printed_sym_type_name.c_str ());
  3966. }
  3967. if (printed_sym_type_name.empty ())
  3968. return false;
  3969. return treg.exec (printed_sym_type_name.c_str (), 0, NULL, 0) == 0;
  3970. }
  3971. /* See symtab.h. */
  3972. bool
  3973. global_symbol_searcher::is_suitable_msymbol
  3974. (const enum search_domain kind, const minimal_symbol *msymbol)
  3975. {
  3976. switch (MSYMBOL_TYPE (msymbol))
  3977. {
  3978. case mst_data:
  3979. case mst_bss:
  3980. case mst_file_data:
  3981. case mst_file_bss:
  3982. return kind == VARIABLES_DOMAIN;
  3983. case mst_text:
  3984. case mst_file_text:
  3985. case mst_solib_trampoline:
  3986. case mst_text_gnu_ifunc:
  3987. return kind == FUNCTIONS_DOMAIN;
  3988. default:
  3989. return false;
  3990. }
  3991. }
  3992. /* See symtab.h. */
  3993. bool
  3994. global_symbol_searcher::expand_symtabs
  3995. (objfile *objfile, const gdb::optional<compiled_regex> &preg) const
  3996. {
  3997. enum search_domain kind = m_kind;
  3998. bool found_msymbol = false;
  3999. auto do_file_match = [&] (const char *filename, bool basenames)
  4000. {
  4001. return file_matches (filename, filenames, basenames);
  4002. };
  4003. gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher = nullptr;
  4004. if (!filenames.empty ())
  4005. file_matcher = do_file_match;
  4006. objfile->expand_symtabs_matching
  4007. (file_matcher,
  4008. &lookup_name_info::match_any (),
  4009. [&] (const char *symname)
  4010. {
  4011. return (!preg.has_value ()
  4012. || preg->exec (symname, 0, NULL, 0) == 0);
  4013. },
  4014. NULL,
  4015. SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
  4016. UNDEF_DOMAIN,
  4017. kind);
  4018. /* Here, we search through the minimal symbol tables for functions and
  4019. variables that match, and force their symbols to be read. This is in
  4020. particular necessary for demangled variable names, which are no longer
  4021. put into the partial symbol tables. The symbol will then be found
  4022. during the scan of symtabs later.
  4023. For functions, find_pc_symtab should succeed if we have debug info for
  4024. the function, for variables we have to call
  4025. lookup_symbol_in_objfile_from_linkage_name to determine if the
  4026. variable has debug info. If the lookup fails, set found_msymbol so
  4027. that we will rescan to print any matching symbols without debug info.
  4028. We only search the objfile the msymbol came from, we no longer search
  4029. all objfiles. In large programs (1000s of shared libs) searching all
  4030. objfiles is not worth the pain. */
  4031. if (filenames.empty ()
  4032. && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
  4033. {
  4034. for (minimal_symbol *msymbol : objfile->msymbols ())
  4035. {
  4036. QUIT;
  4037. if (msymbol->created_by_gdb)
  4038. continue;
  4039. if (is_suitable_msymbol (kind, msymbol))
  4040. {
  4041. if (!preg.has_value ()
  4042. || preg->exec (msymbol->natural_name (), 0,
  4043. NULL, 0) == 0)
  4044. {
  4045. /* An important side-effect of these lookup functions is
  4046. to expand the symbol table if msymbol is found, later
  4047. in the process we will add matching symbols or
  4048. msymbols to the results list, and that requires that
  4049. the symbols tables are expanded. */
  4050. if (kind == FUNCTIONS_DOMAIN
  4051. ? (find_pc_compunit_symtab
  4052. (MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
  4053. == NULL)
  4054. : (lookup_symbol_in_objfile_from_linkage_name
  4055. (objfile, msymbol->linkage_name (),
  4056. VAR_DOMAIN)
  4057. .symbol == NULL))
  4058. found_msymbol = true;
  4059. }
  4060. }
  4061. }
  4062. }
  4063. return found_msymbol;
  4064. }
  4065. /* See symtab.h. */
  4066. bool
  4067. global_symbol_searcher::add_matching_symbols
  4068. (objfile *objfile,
  4069. const gdb::optional<compiled_regex> &preg,
  4070. const gdb::optional<compiled_regex> &treg,
  4071. std::set<symbol_search> *result_set) const
  4072. {
  4073. enum search_domain kind = m_kind;
  4074. /* Add matching symbols (if not already present). */
  4075. for (compunit_symtab *cust : objfile->compunits ())
  4076. {
  4077. const struct blockvector *bv = cust->blockvector ();
  4078. for (block_enum block : { GLOBAL_BLOCK, STATIC_BLOCK })
  4079. {
  4080. struct block_iterator iter;
  4081. struct symbol *sym;
  4082. const struct block *b = BLOCKVECTOR_BLOCK (bv, block);
  4083. ALL_BLOCK_SYMBOLS (b, iter, sym)
  4084. {
  4085. struct symtab *real_symtab = symbol_symtab (sym);
  4086. QUIT;
  4087. /* Check first sole REAL_SYMTAB->FILENAME. It does
  4088. not need to be a substring of symtab_to_fullname as
  4089. it may contain "./" etc. */
  4090. if ((file_matches (real_symtab->filename, filenames, false)
  4091. || ((basenames_may_differ
  4092. || file_matches (lbasename (real_symtab->filename),
  4093. filenames, true))
  4094. && file_matches (symtab_to_fullname (real_symtab),
  4095. filenames, false)))
  4096. && ((!preg.has_value ()
  4097. || preg->exec (sym->natural_name (), 0,
  4098. NULL, 0) == 0)
  4099. && ((kind == VARIABLES_DOMAIN
  4100. && sym->aclass () != LOC_TYPEDEF
  4101. && sym->aclass () != LOC_UNRESOLVED
  4102. && sym->aclass () != LOC_BLOCK
  4103. /* LOC_CONST can be used for more than
  4104. just enums, e.g., c++ static const
  4105. members. We only want to skip enums
  4106. here. */
  4107. && !(sym->aclass () == LOC_CONST
  4108. && (sym->type ()->code ()
  4109. == TYPE_CODE_ENUM))
  4110. && (!treg.has_value ()
  4111. || treg_matches_sym_type_name (*treg, sym)))
  4112. || (kind == FUNCTIONS_DOMAIN
  4113. && sym->aclass () == LOC_BLOCK
  4114. && (!treg.has_value ()
  4115. || treg_matches_sym_type_name (*treg,
  4116. sym)))
  4117. || (kind == TYPES_DOMAIN
  4118. && sym->aclass () == LOC_TYPEDEF
  4119. && sym->domain () != MODULE_DOMAIN)
  4120. || (kind == MODULES_DOMAIN
  4121. && sym->domain () == MODULE_DOMAIN
  4122. && sym->line () != 0))))
  4123. {
  4124. if (result_set->size () < m_max_search_results)
  4125. {
  4126. /* Match, insert if not already in the results. */
  4127. symbol_search ss (block, sym);
  4128. if (result_set->find (ss) == result_set->end ())
  4129. result_set->insert (ss);
  4130. }
  4131. else
  4132. return false;
  4133. }
  4134. }
  4135. }
  4136. }
  4137. return true;
  4138. }
  4139. /* See symtab.h. */
  4140. bool
  4141. global_symbol_searcher::add_matching_msymbols
  4142. (objfile *objfile, const gdb::optional<compiled_regex> &preg,
  4143. std::vector<symbol_search> *results) const
  4144. {
  4145. enum search_domain kind = m_kind;
  4146. for (minimal_symbol *msymbol : objfile->msymbols ())
  4147. {
  4148. QUIT;
  4149. if (msymbol->created_by_gdb)
  4150. continue;
  4151. if (is_suitable_msymbol (kind, msymbol))
  4152. {
  4153. if (!preg.has_value ()
  4154. || preg->exec (msymbol->natural_name (), 0,
  4155. NULL, 0) == 0)
  4156. {
  4157. /* For functions we can do a quick check of whether the
  4158. symbol might be found via find_pc_symtab. */
  4159. if (kind != FUNCTIONS_DOMAIN
  4160. || (find_pc_compunit_symtab
  4161. (MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
  4162. == NULL))
  4163. {
  4164. if (lookup_symbol_in_objfile_from_linkage_name
  4165. (objfile, msymbol->linkage_name (),
  4166. VAR_DOMAIN).symbol == NULL)
  4167. {
  4168. /* Matching msymbol, add it to the results list. */
  4169. if (results->size () < m_max_search_results)
  4170. results->emplace_back (GLOBAL_BLOCK, msymbol, objfile);
  4171. else
  4172. return false;
  4173. }
  4174. }
  4175. }
  4176. }
  4177. }
  4178. return true;
  4179. }
  4180. /* See symtab.h. */
  4181. std::vector<symbol_search>
  4182. global_symbol_searcher::search () const
  4183. {
  4184. gdb::optional<compiled_regex> preg;
  4185. gdb::optional<compiled_regex> treg;
  4186. gdb_assert (m_kind != ALL_DOMAIN);
  4187. if (m_symbol_name_regexp != NULL)
  4188. {
  4189. const char *symbol_name_regexp = m_symbol_name_regexp;
  4190. /* Make sure spacing is right for C++ operators.
  4191. This is just a courtesy to make the matching less sensitive
  4192. to how many spaces the user leaves between 'operator'
  4193. and <TYPENAME> or <OPERATOR>. */
  4194. const char *opend;
  4195. const char *opname = operator_chars (symbol_name_regexp, &opend);
  4196. if (*opname)
  4197. {
  4198. int fix = -1; /* -1 means ok; otherwise number of
  4199. spaces needed. */
  4200. if (isalpha (*opname) || *opname == '_' || *opname == '$')
  4201. {
  4202. /* There should 1 space between 'operator' and 'TYPENAME'. */
  4203. if (opname[-1] != ' ' || opname[-2] == ' ')
  4204. fix = 1;
  4205. }
  4206. else
  4207. {
  4208. /* There should 0 spaces between 'operator' and 'OPERATOR'. */
  4209. if (opname[-1] == ' ')
  4210. fix = 0;
  4211. }
  4212. /* If wrong number of spaces, fix it. */
  4213. if (fix >= 0)
  4214. {
  4215. char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
  4216. sprintf (tmp, "operator%.*s%s", fix, " ", opname);
  4217. symbol_name_regexp = tmp;
  4218. }
  4219. }
  4220. int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
  4221. ? REG_ICASE : 0);
  4222. preg.emplace (symbol_name_regexp, cflags,
  4223. _("Invalid regexp"));
  4224. }
  4225. if (m_symbol_type_regexp != NULL)
  4226. {
  4227. int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
  4228. ? REG_ICASE : 0);
  4229. treg.emplace (m_symbol_type_regexp, cflags,
  4230. _("Invalid regexp"));
  4231. }
  4232. bool found_msymbol = false;
  4233. std::set<symbol_search> result_set;
  4234. for (objfile *objfile : current_program_space->objfiles ())
  4235. {
  4236. /* Expand symtabs within objfile that possibly contain matching
  4237. symbols. */
  4238. found_msymbol |= expand_symtabs (objfile, preg);
  4239. /* Find matching symbols within OBJFILE and add them in to the
  4240. RESULT_SET set. Use a set here so that we can easily detect
  4241. duplicates as we go, and can therefore track how many unique
  4242. matches we have found so far. */
  4243. if (!add_matching_symbols (objfile, preg, treg, &result_set))
  4244. break;
  4245. }
  4246. /* Convert the result set into a sorted result list, as std::set is
  4247. defined to be sorted then no explicit call to std::sort is needed. */
  4248. std::vector<symbol_search> result (result_set.begin (), result_set.end ());
  4249. /* If there are no debug symbols, then add matching minsyms. But if the
  4250. user wants to see symbols matching a type regexp, then never give a
  4251. minimal symbol, as we assume that a minimal symbol does not have a
  4252. type. */
  4253. if ((found_msymbol || (filenames.empty () && m_kind == VARIABLES_DOMAIN))
  4254. && !m_exclude_minsyms
  4255. && !treg.has_value ())
  4256. {
  4257. gdb_assert (m_kind == VARIABLES_DOMAIN || m_kind == FUNCTIONS_DOMAIN);
  4258. for (objfile *objfile : current_program_space->objfiles ())
  4259. if (!add_matching_msymbols (objfile, preg, &result))
  4260. break;
  4261. }
  4262. return result;
  4263. }
  4264. /* See symtab.h. */
  4265. std::string
  4266. symbol_to_info_string (struct symbol *sym, int block,
  4267. enum search_domain kind)
  4268. {
  4269. std::string str;
  4270. gdb_assert (block == GLOBAL_BLOCK || block == STATIC_BLOCK);
  4271. if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
  4272. str += "static ";
  4273. /* Typedef that is not a C++ class. */
  4274. if (kind == TYPES_DOMAIN
  4275. && sym->domain () != STRUCT_DOMAIN)
  4276. {
  4277. string_file tmp_stream;
  4278. /* FIXME: For C (and C++) we end up with a difference in output here
  4279. between how a typedef is printed, and non-typedefs are printed.
  4280. The TYPEDEF_PRINT code places a ";" at the end in an attempt to
  4281. appear C-like, while TYPE_PRINT doesn't.
  4282. For the struct printing case below, things are worse, we force
  4283. printing of the ";" in this function, which is going to be wrong
  4284. for languages that don't require a ";" between statements. */
  4285. if (sym->type ()->code () == TYPE_CODE_TYPEDEF)
  4286. typedef_print (sym->type (), sym, &tmp_stream);
  4287. else
  4288. type_print (sym->type (), "", &tmp_stream, -1);
  4289. str += tmp_stream.string ();
  4290. }
  4291. /* variable, func, or typedef-that-is-c++-class. */
  4292. else if (kind < TYPES_DOMAIN
  4293. || (kind == TYPES_DOMAIN
  4294. && sym->domain () == STRUCT_DOMAIN))
  4295. {
  4296. string_file tmp_stream;
  4297. type_print (sym->type (),
  4298. (sym->aclass () == LOC_TYPEDEF
  4299. ? "" : sym->print_name ()),
  4300. &tmp_stream, 0);
  4301. str += tmp_stream.string ();
  4302. str += ";";
  4303. }
  4304. /* Printing of modules is currently done here, maybe at some future
  4305. point we might want a language specific method to print the module
  4306. symbol so that we can customise the output more. */
  4307. else if (kind == MODULES_DOMAIN)
  4308. str += sym->print_name ();
  4309. return str;
  4310. }
  4311. /* Helper function for symbol info commands, for example 'info functions',
  4312. 'info variables', etc. KIND is the kind of symbol we searched for, and
  4313. BLOCK is the type of block the symbols was found in, either GLOBAL_BLOCK
  4314. or STATIC_BLOCK. SYM is the symbol we found. If LAST is not NULL,
  4315. print file and line number information for the symbol as well. Skip
  4316. printing the filename if it matches LAST. */
  4317. static void
  4318. print_symbol_info (enum search_domain kind,
  4319. struct symbol *sym,
  4320. int block, const char *last)
  4321. {
  4322. scoped_switch_to_sym_language_if_auto l (sym);
  4323. struct symtab *s = symbol_symtab (sym);
  4324. if (last != NULL)
  4325. {
  4326. const char *s_filename = symtab_to_filename_for_display (s);
  4327. if (filename_cmp (last, s_filename) != 0)
  4328. {
  4329. gdb_printf (_("\nFile %ps:\n"),
  4330. styled_string (file_name_style.style (),
  4331. s_filename));
  4332. }
  4333. if (sym->line () != 0)
  4334. gdb_printf ("%d:\t", sym->line ());
  4335. else
  4336. gdb_puts ("\t");
  4337. }
  4338. std::string str = symbol_to_info_string (sym, block, kind);
  4339. gdb_printf ("%s\n", str.c_str ());
  4340. }
  4341. /* This help function for symtab_symbol_info() prints information
  4342. for non-debugging symbols to gdb_stdout. */
  4343. static void
  4344. print_msymbol_info (struct bound_minimal_symbol msymbol)
  4345. {
  4346. struct gdbarch *gdbarch = msymbol.objfile->arch ();
  4347. char *tmp;
  4348. if (gdbarch_addr_bit (gdbarch) <= 32)
  4349. tmp = hex_string_custom (BMSYMBOL_VALUE_ADDRESS (msymbol)
  4350. & (CORE_ADDR) 0xffffffff,
  4351. 8);
  4352. else
  4353. tmp = hex_string_custom (BMSYMBOL_VALUE_ADDRESS (msymbol),
  4354. 16);
  4355. ui_file_style sym_style = (msymbol.minsym->text_p ()
  4356. ? function_name_style.style ()
  4357. : ui_file_style ());
  4358. gdb_printf (_("%ps %ps\n"),
  4359. styled_string (address_style.style (), tmp),
  4360. styled_string (sym_style, msymbol.minsym->print_name ()));
  4361. }
  4362. /* This is the guts of the commands "info functions", "info types", and
  4363. "info variables". It calls search_symbols to find all matches and then
  4364. print_[m]symbol_info to print out some useful information about the
  4365. matches. */
  4366. static void
  4367. symtab_symbol_info (bool quiet, bool exclude_minsyms,
  4368. const char *regexp, enum search_domain kind,
  4369. const char *t_regexp, int from_tty)
  4370. {
  4371. static const char * const classnames[] =
  4372. {"variable", "function", "type", "module"};
  4373. const char *last_filename = "";
  4374. int first = 1;
  4375. gdb_assert (kind != ALL_DOMAIN);
  4376. if (regexp != nullptr && *regexp == '\0')
  4377. regexp = nullptr;
  4378. global_symbol_searcher spec (kind, regexp);
  4379. spec.set_symbol_type_regexp (t_regexp);
  4380. spec.set_exclude_minsyms (exclude_minsyms);
  4381. std::vector<symbol_search> symbols = spec.search ();
  4382. if (!quiet)
  4383. {
  4384. if (regexp != NULL)
  4385. {
  4386. if (t_regexp != NULL)
  4387. gdb_printf
  4388. (_("All %ss matching regular expression \"%s\""
  4389. " with type matching regular expression \"%s\":\n"),
  4390. classnames[kind], regexp, t_regexp);
  4391. else
  4392. gdb_printf (_("All %ss matching regular expression \"%s\":\n"),
  4393. classnames[kind], regexp);
  4394. }
  4395. else
  4396. {
  4397. if (t_regexp != NULL)
  4398. gdb_printf
  4399. (_("All defined %ss"
  4400. " with type matching regular expression \"%s\" :\n"),
  4401. classnames[kind], t_regexp);
  4402. else
  4403. gdb_printf (_("All defined %ss:\n"), classnames[kind]);
  4404. }
  4405. }
  4406. for (const symbol_search &p : symbols)
  4407. {
  4408. QUIT;
  4409. if (p.msymbol.minsym != NULL)
  4410. {
  4411. if (first)
  4412. {
  4413. if (!quiet)
  4414. gdb_printf (_("\nNon-debugging symbols:\n"));
  4415. first = 0;
  4416. }
  4417. print_msymbol_info (p.msymbol);
  4418. }
  4419. else
  4420. {
  4421. print_symbol_info (kind,
  4422. p.symbol,
  4423. p.block,
  4424. last_filename);
  4425. last_filename
  4426. = symtab_to_filename_for_display (symbol_symtab (p.symbol));
  4427. }
  4428. }
  4429. }
  4430. /* Structure to hold the values of the options used by the 'info variables'
  4431. and 'info functions' commands. These correspond to the -q, -t, and -n
  4432. options. */
  4433. struct info_vars_funcs_options
  4434. {
  4435. bool quiet = false;
  4436. bool exclude_minsyms = false;
  4437. std::string type_regexp;
  4438. };
  4439. /* The options used by the 'info variables' and 'info functions'
  4440. commands. */
  4441. static const gdb::option::option_def info_vars_funcs_options_defs[] = {
  4442. gdb::option::boolean_option_def<info_vars_funcs_options> {
  4443. "q",
  4444. [] (info_vars_funcs_options *opt) { return &opt->quiet; },
  4445. nullptr, /* show_cmd_cb */
  4446. nullptr /* set_doc */
  4447. },
  4448. gdb::option::boolean_option_def<info_vars_funcs_options> {
  4449. "n",
  4450. [] (info_vars_funcs_options *opt) { return &opt->exclude_minsyms; },
  4451. nullptr, /* show_cmd_cb */
  4452. nullptr /* set_doc */
  4453. },
  4454. gdb::option::string_option_def<info_vars_funcs_options> {
  4455. "t",
  4456. [] (info_vars_funcs_options *opt) { return &opt->type_regexp; },
  4457. nullptr, /* show_cmd_cb */
  4458. nullptr /* set_doc */
  4459. }
  4460. };
  4461. /* Returns the option group used by 'info variables' and 'info
  4462. functions'. */
  4463. static gdb::option::option_def_group
  4464. make_info_vars_funcs_options_def_group (info_vars_funcs_options *opts)
  4465. {
  4466. return {{info_vars_funcs_options_defs}, opts};
  4467. }
  4468. /* Command completer for 'info variables' and 'info functions'. */
  4469. static void
  4470. info_vars_funcs_command_completer (struct cmd_list_element *ignore,
  4471. completion_tracker &tracker,
  4472. const char *text, const char * /* word */)
  4473. {
  4474. const auto group
  4475. = make_info_vars_funcs_options_def_group (nullptr);
  4476. if (gdb::option::complete_options
  4477. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
  4478. return;
  4479. const char *word = advance_to_expression_complete_word_point (tracker, text);
  4480. symbol_completer (ignore, tracker, text, word);
  4481. }
  4482. /* Implement the 'info variables' command. */
  4483. static void
  4484. info_variables_command (const char *args, int from_tty)
  4485. {
  4486. info_vars_funcs_options opts;
  4487. auto grp = make_info_vars_funcs_options_def_group (&opts);
  4488. gdb::option::process_options
  4489. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  4490. if (args != nullptr && *args == '\0')
  4491. args = nullptr;
  4492. symtab_symbol_info
  4493. (opts.quiet, opts.exclude_minsyms, args, VARIABLES_DOMAIN,
  4494. opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
  4495. from_tty);
  4496. }
  4497. /* Implement the 'info functions' command. */
  4498. static void
  4499. info_functions_command (const char *args, int from_tty)
  4500. {
  4501. info_vars_funcs_options opts;
  4502. auto grp = make_info_vars_funcs_options_def_group (&opts);
  4503. gdb::option::process_options
  4504. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  4505. if (args != nullptr && *args == '\0')
  4506. args = nullptr;
  4507. symtab_symbol_info
  4508. (opts.quiet, opts.exclude_minsyms, args, FUNCTIONS_DOMAIN,
  4509. opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
  4510. from_tty);
  4511. }
  4512. /* Holds the -q option for the 'info types' command. */
  4513. struct info_types_options
  4514. {
  4515. bool quiet = false;
  4516. };
  4517. /* The options used by the 'info types' command. */
  4518. static const gdb::option::option_def info_types_options_defs[] = {
  4519. gdb::option::boolean_option_def<info_types_options> {
  4520. "q",
  4521. [] (info_types_options *opt) { return &opt->quiet; },
  4522. nullptr, /* show_cmd_cb */
  4523. nullptr /* set_doc */
  4524. }
  4525. };
  4526. /* Returns the option group used by 'info types'. */
  4527. static gdb::option::option_def_group
  4528. make_info_types_options_def_group (info_types_options *opts)
  4529. {
  4530. return {{info_types_options_defs}, opts};
  4531. }
  4532. /* Implement the 'info types' command. */
  4533. static void
  4534. info_types_command (const char *args, int from_tty)
  4535. {
  4536. info_types_options opts;
  4537. auto grp = make_info_types_options_def_group (&opts);
  4538. gdb::option::process_options
  4539. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  4540. if (args != nullptr && *args == '\0')
  4541. args = nullptr;
  4542. symtab_symbol_info (opts.quiet, false, args, TYPES_DOMAIN, NULL, from_tty);
  4543. }
  4544. /* Command completer for 'info types' command. */
  4545. static void
  4546. info_types_command_completer (struct cmd_list_element *ignore,
  4547. completion_tracker &tracker,
  4548. const char *text, const char * /* word */)
  4549. {
  4550. const auto group
  4551. = make_info_types_options_def_group (nullptr);
  4552. if (gdb::option::complete_options
  4553. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
  4554. return;
  4555. const char *word = advance_to_expression_complete_word_point (tracker, text);
  4556. symbol_completer (ignore, tracker, text, word);
  4557. }
  4558. /* Implement the 'info modules' command. */
  4559. static void
  4560. info_modules_command (const char *args, int from_tty)
  4561. {
  4562. info_types_options opts;
  4563. auto grp = make_info_types_options_def_group (&opts);
  4564. gdb::option::process_options
  4565. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  4566. if (args != nullptr && *args == '\0')
  4567. args = nullptr;
  4568. symtab_symbol_info (opts.quiet, true, args, MODULES_DOMAIN, NULL,
  4569. from_tty);
  4570. }
  4571. static void
  4572. rbreak_command (const char *regexp, int from_tty)
  4573. {
  4574. std::string string;
  4575. const char *file_name = nullptr;
  4576. if (regexp != nullptr)
  4577. {
  4578. const char *colon = strchr (regexp, ':');
  4579. /* Ignore the colon if it is part of a Windows drive. */
  4580. if (HAS_DRIVE_SPEC (regexp)
  4581. && (regexp[2] == '/' || regexp[2] == '\\'))
  4582. colon = strchr (STRIP_DRIVE_SPEC (regexp), ':');
  4583. if (colon && *(colon + 1) != ':')
  4584. {
  4585. int colon_index;
  4586. char *local_name;
  4587. colon_index = colon - regexp;
  4588. local_name = (char *) alloca (colon_index + 1);
  4589. memcpy (local_name, regexp, colon_index);
  4590. local_name[colon_index--] = 0;
  4591. while (isspace (local_name[colon_index]))
  4592. local_name[colon_index--] = 0;
  4593. file_name = local_name;
  4594. regexp = skip_spaces (colon + 1);
  4595. }
  4596. }
  4597. global_symbol_searcher spec (FUNCTIONS_DOMAIN, regexp);
  4598. if (file_name != nullptr)
  4599. spec.filenames.push_back (file_name);
  4600. std::vector<symbol_search> symbols = spec.search ();
  4601. scoped_rbreak_breakpoints finalize;
  4602. for (const symbol_search &p : symbols)
  4603. {
  4604. if (p.msymbol.minsym == NULL)
  4605. {
  4606. struct symtab *symtab = symbol_symtab (p.symbol);
  4607. const char *fullname = symtab_to_fullname (symtab);
  4608. string = string_printf ("%s:'%s'", fullname,
  4609. p.symbol->linkage_name ());
  4610. break_command (&string[0], from_tty);
  4611. print_symbol_info (FUNCTIONS_DOMAIN, p.symbol, p.block, NULL);
  4612. }
  4613. else
  4614. {
  4615. string = string_printf ("'%s'",
  4616. p.msymbol.minsym->linkage_name ());
  4617. break_command (&string[0], from_tty);
  4618. gdb_printf ("<function, no debug info> %s;\n",
  4619. p.msymbol.minsym->print_name ());
  4620. }
  4621. }
  4622. }
  4623. /* Evaluate if SYMNAME matches LOOKUP_NAME. */
  4624. static int
  4625. compare_symbol_name (const char *symbol_name, language symbol_language,
  4626. const lookup_name_info &lookup_name,
  4627. completion_match_result &match_res)
  4628. {
  4629. const language_defn *lang = language_def (symbol_language);
  4630. symbol_name_matcher_ftype *name_match
  4631. = lang->get_symbol_name_matcher (lookup_name);
  4632. return name_match (symbol_name, lookup_name, &match_res);
  4633. }
  4634. /* See symtab.h. */
  4635. bool
  4636. completion_list_add_name (completion_tracker &tracker,
  4637. language symbol_language,
  4638. const char *symname,
  4639. const lookup_name_info &lookup_name,
  4640. const char *text, const char *word)
  4641. {
  4642. completion_match_result &match_res
  4643. = tracker.reset_completion_match_result ();
  4644. /* Clip symbols that cannot match. */
  4645. if (!compare_symbol_name (symname, symbol_language, lookup_name, match_res))
  4646. return false;
  4647. /* Refresh SYMNAME from the match string. It's potentially
  4648. different depending on language. (E.g., on Ada, the match may be
  4649. the encoded symbol name wrapped in "<>"). */
  4650. symname = match_res.match.match ();
  4651. gdb_assert (symname != NULL);
  4652. /* We have a match for a completion, so add SYMNAME to the current list
  4653. of matches. Note that the name is moved to freshly malloc'd space. */
  4654. {
  4655. gdb::unique_xmalloc_ptr<char> completion
  4656. = make_completion_match_str (symname, text, word);
  4657. /* Here we pass the match-for-lcd object to add_completion. Some
  4658. languages match the user text against substrings of symbol
  4659. names in some cases. E.g., in C++, "b push_ba" completes to
  4660. "std::vector::push_back", "std::string::push_back", etc., and
  4661. in this case we want the completion lowest common denominator
  4662. to be "push_back" instead of "std::". */
  4663. tracker.add_completion (std::move (completion),
  4664. &match_res.match_for_lcd, text, word);
  4665. }
  4666. return true;
  4667. }
  4668. /* completion_list_add_name wrapper for struct symbol. */
  4669. static void
  4670. completion_list_add_symbol (completion_tracker &tracker,
  4671. symbol *sym,
  4672. const lookup_name_info &lookup_name,
  4673. const char *text, const char *word)
  4674. {
  4675. if (!completion_list_add_name (tracker, sym->language (),
  4676. sym->natural_name (),
  4677. lookup_name, text, word))
  4678. return;
  4679. /* C++ function symbols include the parameters within both the msymbol
  4680. name and the symbol name. The problem is that the msymbol name will
  4681. describe the parameters in the most basic way, with typedefs stripped
  4682. out, while the symbol name will represent the types as they appear in
  4683. the program. This means we will see duplicate entries in the
  4684. completion tracker. The following converts the symbol name back to
  4685. the msymbol name and removes the msymbol name from the completion
  4686. tracker. */
  4687. if (sym->language () == language_cplus
  4688. && sym->domain () == VAR_DOMAIN
  4689. && sym->aclass () == LOC_BLOCK)
  4690. {
  4691. /* The call to canonicalize returns the empty string if the input
  4692. string is already in canonical form, thanks to this we don't
  4693. remove the symbol we just added above. */
  4694. gdb::unique_xmalloc_ptr<char> str
  4695. = cp_canonicalize_string_no_typedefs (sym->natural_name ());
  4696. if (str != nullptr)
  4697. tracker.remove_completion (str.get ());
  4698. }
  4699. }
  4700. /* completion_list_add_name wrapper for struct minimal_symbol. */
  4701. static void
  4702. completion_list_add_msymbol (completion_tracker &tracker,
  4703. minimal_symbol *sym,
  4704. const lookup_name_info &lookup_name,
  4705. const char *text, const char *word)
  4706. {
  4707. completion_list_add_name (tracker, sym->language (),
  4708. sym->natural_name (),
  4709. lookup_name, text, word);
  4710. }
  4711. /* ObjC: In case we are completing on a selector, look as the msymbol
  4712. again and feed all the selectors into the mill. */
  4713. static void
  4714. completion_list_objc_symbol (completion_tracker &tracker,
  4715. struct minimal_symbol *msymbol,
  4716. const lookup_name_info &lookup_name,
  4717. const char *text, const char *word)
  4718. {
  4719. static char *tmp = NULL;
  4720. static unsigned int tmplen = 0;
  4721. const char *method, *category, *selector;
  4722. char *tmp2 = NULL;
  4723. method = msymbol->natural_name ();
  4724. /* Is it a method? */
  4725. if ((method[0] != '-') && (method[0] != '+'))
  4726. return;
  4727. if (text[0] == '[')
  4728. /* Complete on shortened method method. */
  4729. completion_list_add_name (tracker, language_objc,
  4730. method + 1,
  4731. lookup_name,
  4732. text, word);
  4733. while ((strlen (method) + 1) >= tmplen)
  4734. {
  4735. if (tmplen == 0)
  4736. tmplen = 1024;
  4737. else
  4738. tmplen *= 2;
  4739. tmp = (char *) xrealloc (tmp, tmplen);
  4740. }
  4741. selector = strchr (method, ' ');
  4742. if (selector != NULL)
  4743. selector++;
  4744. category = strchr (method, '(');
  4745. if ((category != NULL) && (selector != NULL))
  4746. {
  4747. memcpy (tmp, method, (category - method));
  4748. tmp[category - method] = ' ';
  4749. memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
  4750. completion_list_add_name (tracker, language_objc, tmp,
  4751. lookup_name, text, word);
  4752. if (text[0] == '[')
  4753. completion_list_add_name (tracker, language_objc, tmp + 1,
  4754. lookup_name, text, word);
  4755. }
  4756. if (selector != NULL)
  4757. {
  4758. /* Complete on selector only. */
  4759. strcpy (tmp, selector);
  4760. tmp2 = strchr (tmp, ']');
  4761. if (tmp2 != NULL)
  4762. *tmp2 = '\0';
  4763. completion_list_add_name (tracker, language_objc, tmp,
  4764. lookup_name, text, word);
  4765. }
  4766. }
  4767. /* Break the non-quoted text based on the characters which are in
  4768. symbols. FIXME: This should probably be language-specific. */
  4769. static const char *
  4770. language_search_unquoted_string (const char *text, const char *p)
  4771. {
  4772. for (; p > text; --p)
  4773. {
  4774. if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
  4775. continue;
  4776. else
  4777. {
  4778. if ((current_language->la_language == language_objc))
  4779. {
  4780. if (p[-1] == ':') /* Might be part of a method name. */
  4781. continue;
  4782. else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
  4783. p -= 2; /* Beginning of a method name. */
  4784. else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
  4785. { /* Might be part of a method name. */
  4786. const char *t = p;
  4787. /* Seeing a ' ' or a '(' is not conclusive evidence
  4788. that we are in the middle of a method name. However,
  4789. finding "-[" or "+[" should be pretty un-ambiguous.
  4790. Unfortunately we have to find it now to decide. */
  4791. while (t > text)
  4792. if (isalnum (t[-1]) || t[-1] == '_' ||
  4793. t[-1] == ' ' || t[-1] == ':' ||
  4794. t[-1] == '(' || t[-1] == ')')
  4795. --t;
  4796. else
  4797. break;
  4798. if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
  4799. p = t - 2; /* Method name detected. */
  4800. /* Else we leave with p unchanged. */
  4801. }
  4802. }
  4803. break;
  4804. }
  4805. }
  4806. return p;
  4807. }
  4808. static void
  4809. completion_list_add_fields (completion_tracker &tracker,
  4810. struct symbol *sym,
  4811. const lookup_name_info &lookup_name,
  4812. const char *text, const char *word)
  4813. {
  4814. if (sym->aclass () == LOC_TYPEDEF)
  4815. {
  4816. struct type *t = sym->type ();
  4817. enum type_code c = t->code ();
  4818. int j;
  4819. if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
  4820. for (j = TYPE_N_BASECLASSES (t); j < t->num_fields (); j++)
  4821. if (t->field (j).name ())
  4822. completion_list_add_name (tracker, sym->language (),
  4823. t->field (j).name (),
  4824. lookup_name, text, word);
  4825. }
  4826. }
  4827. /* See symtab.h. */
  4828. bool
  4829. symbol_is_function_or_method (symbol *sym)
  4830. {
  4831. switch (sym->type ()->code ())
  4832. {
  4833. case TYPE_CODE_FUNC:
  4834. case TYPE_CODE_METHOD:
  4835. return true;
  4836. default:
  4837. return false;
  4838. }
  4839. }
  4840. /* See symtab.h. */
  4841. bool
  4842. symbol_is_function_or_method (minimal_symbol *msymbol)
  4843. {
  4844. switch (MSYMBOL_TYPE (msymbol))
  4845. {
  4846. case mst_text:
  4847. case mst_text_gnu_ifunc:
  4848. case mst_solib_trampoline:
  4849. case mst_file_text:
  4850. return true;
  4851. default:
  4852. return false;
  4853. }
  4854. }
  4855. /* See symtab.h. */
  4856. bound_minimal_symbol
  4857. find_gnu_ifunc (const symbol *sym)
  4858. {
  4859. if (sym->aclass () != LOC_BLOCK)
  4860. return {};
  4861. lookup_name_info lookup_name (sym->search_name (),
  4862. symbol_name_match_type::SEARCH_NAME);
  4863. struct objfile *objfile = symbol_objfile (sym);
  4864. CORE_ADDR address = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
  4865. minimal_symbol *ifunc = NULL;
  4866. iterate_over_minimal_symbols (objfile, lookup_name,
  4867. [&] (minimal_symbol *minsym)
  4868. {
  4869. if (MSYMBOL_TYPE (minsym) == mst_text_gnu_ifunc
  4870. || MSYMBOL_TYPE (minsym) == mst_data_gnu_ifunc)
  4871. {
  4872. CORE_ADDR msym_addr = MSYMBOL_VALUE_ADDRESS (objfile, minsym);
  4873. if (MSYMBOL_TYPE (minsym) == mst_data_gnu_ifunc)
  4874. {
  4875. struct gdbarch *gdbarch = objfile->arch ();
  4876. msym_addr = gdbarch_convert_from_func_ptr_addr
  4877. (gdbarch, msym_addr, current_inferior ()->top_target ());
  4878. }
  4879. if (msym_addr == address)
  4880. {
  4881. ifunc = minsym;
  4882. return true;
  4883. }
  4884. }
  4885. return false;
  4886. });
  4887. if (ifunc != NULL)
  4888. return {ifunc, objfile};
  4889. return {};
  4890. }
  4891. /* Add matching symbols from SYMTAB to the current completion list. */
  4892. static void
  4893. add_symtab_completions (struct compunit_symtab *cust,
  4894. completion_tracker &tracker,
  4895. complete_symbol_mode mode,
  4896. const lookup_name_info &lookup_name,
  4897. const char *text, const char *word,
  4898. enum type_code code)
  4899. {
  4900. struct symbol *sym;
  4901. const struct block *b;
  4902. struct block_iterator iter;
  4903. int i;
  4904. if (cust == NULL)
  4905. return;
  4906. for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
  4907. {
  4908. QUIT;
  4909. b = BLOCKVECTOR_BLOCK (cust->blockvector (), i);
  4910. ALL_BLOCK_SYMBOLS (b, iter, sym)
  4911. {
  4912. if (completion_skip_symbol (mode, sym))
  4913. continue;
  4914. if (code == TYPE_CODE_UNDEF
  4915. || (sym->domain () == STRUCT_DOMAIN
  4916. && sym->type ()->code () == code))
  4917. completion_list_add_symbol (tracker, sym,
  4918. lookup_name,
  4919. text, word);
  4920. }
  4921. }
  4922. }
  4923. void
  4924. default_collect_symbol_completion_matches_break_on
  4925. (completion_tracker &tracker, complete_symbol_mode mode,
  4926. symbol_name_match_type name_match_type,
  4927. const char *text, const char *word,
  4928. const char *break_on, enum type_code code)
  4929. {
  4930. /* Problem: All of the symbols have to be copied because readline
  4931. frees them. I'm not going to worry about this; hopefully there
  4932. won't be that many. */
  4933. struct symbol *sym;
  4934. const struct block *b;
  4935. const struct block *surrounding_static_block, *surrounding_global_block;
  4936. struct block_iterator iter;
  4937. /* The symbol we are completing on. Points in same buffer as text. */
  4938. const char *sym_text;
  4939. /* Now look for the symbol we are supposed to complete on. */
  4940. if (mode == complete_symbol_mode::LINESPEC)
  4941. sym_text = text;
  4942. else
  4943. {
  4944. const char *p;
  4945. char quote_found;
  4946. const char *quote_pos = NULL;
  4947. /* First see if this is a quoted string. */
  4948. quote_found = '\0';
  4949. for (p = text; *p != '\0'; ++p)
  4950. {
  4951. if (quote_found != '\0')
  4952. {
  4953. if (*p == quote_found)
  4954. /* Found close quote. */
  4955. quote_found = '\0';
  4956. else if (*p == '\\' && p[1] == quote_found)
  4957. /* A backslash followed by the quote character
  4958. doesn't end the string. */
  4959. ++p;
  4960. }
  4961. else if (*p == '\'' || *p == '"')
  4962. {
  4963. quote_found = *p;
  4964. quote_pos = p;
  4965. }
  4966. }
  4967. if (quote_found == '\'')
  4968. /* A string within single quotes can be a symbol, so complete on it. */
  4969. sym_text = quote_pos + 1;
  4970. else if (quote_found == '"')
  4971. /* A double-quoted string is never a symbol, nor does it make sense
  4972. to complete it any other way. */
  4973. {
  4974. return;
  4975. }
  4976. else
  4977. {
  4978. /* It is not a quoted string. Break it based on the characters
  4979. which are in symbols. */
  4980. while (p > text)
  4981. {
  4982. if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0'
  4983. || p[-1] == ':' || strchr (break_on, p[-1]) != NULL)
  4984. --p;
  4985. else
  4986. break;
  4987. }
  4988. sym_text = p;
  4989. }
  4990. }
  4991. lookup_name_info lookup_name (sym_text, name_match_type, true);
  4992. /* At this point scan through the misc symbol vectors and add each
  4993. symbol you find to the list. Eventually we want to ignore
  4994. anything that isn't a text symbol (everything else will be
  4995. handled by the psymtab code below). */
  4996. if (code == TYPE_CODE_UNDEF)
  4997. {
  4998. for (objfile *objfile : current_program_space->objfiles ())
  4999. {
  5000. for (minimal_symbol *msymbol : objfile->msymbols ())
  5001. {
  5002. QUIT;
  5003. if (completion_skip_symbol (mode, msymbol))
  5004. continue;
  5005. completion_list_add_msymbol (tracker, msymbol, lookup_name,
  5006. sym_text, word);
  5007. completion_list_objc_symbol (tracker, msymbol, lookup_name,
  5008. sym_text, word);
  5009. }
  5010. }
  5011. }
  5012. /* Add completions for all currently loaded symbol tables. */
  5013. for (objfile *objfile : current_program_space->objfiles ())
  5014. {
  5015. for (compunit_symtab *cust : objfile->compunits ())
  5016. add_symtab_completions (cust, tracker, mode, lookup_name,
  5017. sym_text, word, code);
  5018. }
  5019. /* Look through the partial symtabs for all symbols which begin by
  5020. matching SYM_TEXT. Expand all CUs that you find to the list. */
  5021. expand_symtabs_matching (NULL,
  5022. lookup_name,
  5023. NULL,
  5024. [&] (compunit_symtab *symtab) /* expansion notify */
  5025. {
  5026. add_symtab_completions (symtab,
  5027. tracker, mode, lookup_name,
  5028. sym_text, word, code);
  5029. return true;
  5030. },
  5031. SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
  5032. ALL_DOMAIN);
  5033. /* Search upwards from currently selected frame (so that we can
  5034. complete on local vars). Also catch fields of types defined in
  5035. this places which match our text string. Only complete on types
  5036. visible from current context. */
  5037. b = get_selected_block (0);
  5038. surrounding_static_block = block_static_block (b);
  5039. surrounding_global_block = block_global_block (b);
  5040. if (surrounding_static_block != NULL)
  5041. while (b != surrounding_static_block)
  5042. {
  5043. QUIT;
  5044. ALL_BLOCK_SYMBOLS (b, iter, sym)
  5045. {
  5046. if (code == TYPE_CODE_UNDEF)
  5047. {
  5048. completion_list_add_symbol (tracker, sym, lookup_name,
  5049. sym_text, word);
  5050. completion_list_add_fields (tracker, sym, lookup_name,
  5051. sym_text, word);
  5052. }
  5053. else if (sym->domain () == STRUCT_DOMAIN
  5054. && sym->type ()->code () == code)
  5055. completion_list_add_symbol (tracker, sym, lookup_name,
  5056. sym_text, word);
  5057. }
  5058. /* Stop when we encounter an enclosing function. Do not stop for
  5059. non-inlined functions - the locals of the enclosing function
  5060. are in scope for a nested function. */
  5061. if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
  5062. break;
  5063. b = BLOCK_SUPERBLOCK (b);
  5064. }
  5065. /* Add fields from the file's types; symbols will be added below. */
  5066. if (code == TYPE_CODE_UNDEF)
  5067. {
  5068. if (surrounding_static_block != NULL)
  5069. ALL_BLOCK_SYMBOLS (surrounding_static_block, iter, sym)
  5070. completion_list_add_fields (tracker, sym, lookup_name,
  5071. sym_text, word);
  5072. if (surrounding_global_block != NULL)
  5073. ALL_BLOCK_SYMBOLS (surrounding_global_block, iter, sym)
  5074. completion_list_add_fields (tracker, sym, lookup_name,
  5075. sym_text, word);
  5076. }
  5077. /* Skip macros if we are completing a struct tag -- arguable but
  5078. usually what is expected. */
  5079. if (current_language->macro_expansion () == macro_expansion_c
  5080. && code == TYPE_CODE_UNDEF)
  5081. {
  5082. gdb::unique_xmalloc_ptr<struct macro_scope> scope;
  5083. /* This adds a macro's name to the current completion list. */
  5084. auto add_macro_name = [&] (const char *macro_name,
  5085. const macro_definition *,
  5086. macro_source_file *,
  5087. int)
  5088. {
  5089. completion_list_add_name (tracker, language_c, macro_name,
  5090. lookup_name, sym_text, word);
  5091. };
  5092. /* Add any macros visible in the default scope. Note that this
  5093. may yield the occasional wrong result, because an expression
  5094. might be evaluated in a scope other than the default. For
  5095. example, if the user types "break file:line if <TAB>", the
  5096. resulting expression will be evaluated at "file:line" -- but
  5097. at there does not seem to be a way to detect this at
  5098. completion time. */
  5099. scope = default_macro_scope ();
  5100. if (scope)
  5101. macro_for_each_in_scope (scope->file, scope->line,
  5102. add_macro_name);
  5103. /* User-defined macros are always visible. */
  5104. macro_for_each (macro_user_macros, add_macro_name);
  5105. }
  5106. }
  5107. /* Collect all symbols (regardless of class) which begin by matching
  5108. TEXT. */
  5109. void
  5110. collect_symbol_completion_matches (completion_tracker &tracker,
  5111. complete_symbol_mode mode,
  5112. symbol_name_match_type name_match_type,
  5113. const char *text, const char *word)
  5114. {
  5115. current_language->collect_symbol_completion_matches (tracker, mode,
  5116. name_match_type,
  5117. text, word,
  5118. TYPE_CODE_UNDEF);
  5119. }
  5120. /* Like collect_symbol_completion_matches, but only collect
  5121. STRUCT_DOMAIN symbols whose type code is CODE. */
  5122. void
  5123. collect_symbol_completion_matches_type (completion_tracker &tracker,
  5124. const char *text, const char *word,
  5125. enum type_code code)
  5126. {
  5127. complete_symbol_mode mode = complete_symbol_mode::EXPRESSION;
  5128. symbol_name_match_type name_match_type = symbol_name_match_type::EXPRESSION;
  5129. gdb_assert (code == TYPE_CODE_UNION
  5130. || code == TYPE_CODE_STRUCT
  5131. || code == TYPE_CODE_ENUM);
  5132. current_language->collect_symbol_completion_matches (tracker, mode,
  5133. name_match_type,
  5134. text, word, code);
  5135. }
  5136. /* Like collect_symbol_completion_matches, but collects a list of
  5137. symbols defined in all source files named SRCFILE. */
  5138. void
  5139. collect_file_symbol_completion_matches (completion_tracker &tracker,
  5140. complete_symbol_mode mode,
  5141. symbol_name_match_type name_match_type,
  5142. const char *text, const char *word,
  5143. const char *srcfile)
  5144. {
  5145. /* The symbol we are completing on. Points in same buffer as text. */
  5146. const char *sym_text;
  5147. /* Now look for the symbol we are supposed to complete on.
  5148. FIXME: This should be language-specific. */
  5149. if (mode == complete_symbol_mode::LINESPEC)
  5150. sym_text = text;
  5151. else
  5152. {
  5153. const char *p;
  5154. char quote_found;
  5155. const char *quote_pos = NULL;
  5156. /* First see if this is a quoted string. */
  5157. quote_found = '\0';
  5158. for (p = text; *p != '\0'; ++p)
  5159. {
  5160. if (quote_found != '\0')
  5161. {
  5162. if (*p == quote_found)
  5163. /* Found close quote. */
  5164. quote_found = '\0';
  5165. else if (*p == '\\' && p[1] == quote_found)
  5166. /* A backslash followed by the quote character
  5167. doesn't end the string. */
  5168. ++p;
  5169. }
  5170. else if (*p == '\'' || *p == '"')
  5171. {
  5172. quote_found = *p;
  5173. quote_pos = p;
  5174. }
  5175. }
  5176. if (quote_found == '\'')
  5177. /* A string within single quotes can be a symbol, so complete on it. */
  5178. sym_text = quote_pos + 1;
  5179. else if (quote_found == '"')
  5180. /* A double-quoted string is never a symbol, nor does it make sense
  5181. to complete it any other way. */
  5182. {
  5183. return;
  5184. }
  5185. else
  5186. {
  5187. /* Not a quoted string. */
  5188. sym_text = language_search_unquoted_string (text, p);
  5189. }
  5190. }
  5191. lookup_name_info lookup_name (sym_text, name_match_type, true);
  5192. /* Go through symtabs for SRCFILE and check the externs and statics
  5193. for symbols which match. */
  5194. iterate_over_symtabs (srcfile, [&] (symtab *s)
  5195. {
  5196. add_symtab_completions (s->compunit (),
  5197. tracker, mode, lookup_name,
  5198. sym_text, word, TYPE_CODE_UNDEF);
  5199. return false;
  5200. });
  5201. }
  5202. /* A helper function for make_source_files_completion_list. It adds
  5203. another file name to a list of possible completions, growing the
  5204. list as necessary. */
  5205. static void
  5206. add_filename_to_list (const char *fname, const char *text, const char *word,
  5207. completion_list *list)
  5208. {
  5209. list->emplace_back (make_completion_match_str (fname, text, word));
  5210. }
  5211. static int
  5212. not_interesting_fname (const char *fname)
  5213. {
  5214. static const char *illegal_aliens[] = {
  5215. "_globals_", /* inserted by coff_symtab_read */
  5216. NULL
  5217. };
  5218. int i;
  5219. for (i = 0; illegal_aliens[i]; i++)
  5220. {
  5221. if (filename_cmp (fname, illegal_aliens[i]) == 0)
  5222. return 1;
  5223. }
  5224. return 0;
  5225. }
  5226. /* An object of this type is passed as the callback argument to
  5227. map_partial_symbol_filenames. */
  5228. struct add_partial_filename_data
  5229. {
  5230. struct filename_seen_cache *filename_seen_cache;
  5231. const char *text;
  5232. const char *word;
  5233. int text_len;
  5234. completion_list *list;
  5235. void operator() (const char *filename, const char *fullname);
  5236. };
  5237. /* A callback for map_partial_symbol_filenames. */
  5238. void
  5239. add_partial_filename_data::operator() (const char *filename,
  5240. const char *fullname)
  5241. {
  5242. if (not_interesting_fname (filename))
  5243. return;
  5244. if (!filename_seen_cache->seen (filename)
  5245. && filename_ncmp (filename, text, text_len) == 0)
  5246. {
  5247. /* This file matches for a completion; add it to the
  5248. current list of matches. */
  5249. add_filename_to_list (filename, text, word, list);
  5250. }
  5251. else
  5252. {
  5253. const char *base_name = lbasename (filename);
  5254. if (base_name != filename
  5255. && !filename_seen_cache->seen (base_name)
  5256. && filename_ncmp (base_name, text, text_len) == 0)
  5257. add_filename_to_list (base_name, text, word, list);
  5258. }
  5259. }
  5260. /* Return a list of all source files whose names begin with matching
  5261. TEXT. The file names are looked up in the symbol tables of this
  5262. program. */
  5263. completion_list
  5264. make_source_files_completion_list (const char *text, const char *word)
  5265. {
  5266. size_t text_len = strlen (text);
  5267. completion_list list;
  5268. const char *base_name;
  5269. struct add_partial_filename_data datum;
  5270. if (!have_full_symbols () && !have_partial_symbols ())
  5271. return list;
  5272. filename_seen_cache filenames_seen;
  5273. for (objfile *objfile : current_program_space->objfiles ())
  5274. {
  5275. for (compunit_symtab *cu : objfile->compunits ())
  5276. {
  5277. for (symtab *s : cu->filetabs ())
  5278. {
  5279. if (not_interesting_fname (s->filename))
  5280. continue;
  5281. if (!filenames_seen.seen (s->filename)
  5282. && filename_ncmp (s->filename, text, text_len) == 0)
  5283. {
  5284. /* This file matches for a completion; add it to the current
  5285. list of matches. */
  5286. add_filename_to_list (s->filename, text, word, &list);
  5287. }
  5288. else
  5289. {
  5290. /* NOTE: We allow the user to type a base name when the
  5291. debug info records leading directories, but not the other
  5292. way around. This is what subroutines of breakpoint
  5293. command do when they parse file names. */
  5294. base_name = lbasename (s->filename);
  5295. if (base_name != s->filename
  5296. && !filenames_seen.seen (base_name)
  5297. && filename_ncmp (base_name, text, text_len) == 0)
  5298. add_filename_to_list (base_name, text, word, &list);
  5299. }
  5300. }
  5301. }
  5302. }
  5303. datum.filename_seen_cache = &filenames_seen;
  5304. datum.text = text;
  5305. datum.word = word;
  5306. datum.text_len = text_len;
  5307. datum.list = &list;
  5308. map_symbol_filenames (datum, false /*need_fullname*/);
  5309. return list;
  5310. }
  5311. /* Track MAIN */
  5312. /* Return the "main_info" object for the current program space. If
  5313. the object has not yet been created, create it and fill in some
  5314. default values. */
  5315. static struct main_info *
  5316. get_main_info (void)
  5317. {
  5318. struct main_info *info = main_progspace_key.get (current_program_space);
  5319. if (info == NULL)
  5320. {
  5321. /* It may seem strange to store the main name in the progspace
  5322. and also in whatever objfile happens to see a main name in
  5323. its debug info. The reason for this is mainly historical:
  5324. gdb returned "main" as the name even if no function named
  5325. "main" was defined the program; and this approach lets us
  5326. keep compatibility. */
  5327. info = main_progspace_key.emplace (current_program_space);
  5328. }
  5329. return info;
  5330. }
  5331. static void
  5332. set_main_name (const char *name, enum language lang)
  5333. {
  5334. struct main_info *info = get_main_info ();
  5335. if (info->name_of_main != NULL)
  5336. {
  5337. xfree (info->name_of_main);
  5338. info->name_of_main = NULL;
  5339. info->language_of_main = language_unknown;
  5340. }
  5341. if (name != NULL)
  5342. {
  5343. info->name_of_main = xstrdup (name);
  5344. info->language_of_main = lang;
  5345. }
  5346. }
  5347. /* Deduce the name of the main procedure, and set NAME_OF_MAIN
  5348. accordingly. */
  5349. static void
  5350. find_main_name (void)
  5351. {
  5352. const char *new_main_name;
  5353. /* First check the objfiles to see whether a debuginfo reader has
  5354. picked up the appropriate main name. Historically the main name
  5355. was found in a more or less random way; this approach instead
  5356. relies on the order of objfile creation -- which still isn't
  5357. guaranteed to get the correct answer, but is just probably more
  5358. accurate. */
  5359. for (objfile *objfile : current_program_space->objfiles ())
  5360. {
  5361. if (objfile->per_bfd->name_of_main != NULL)
  5362. {
  5363. set_main_name (objfile->per_bfd->name_of_main,
  5364. objfile->per_bfd->language_of_main);
  5365. return;
  5366. }
  5367. }
  5368. /* Try to see if the main procedure is in Ada. */
  5369. /* FIXME: brobecker/2005-03-07: Another way of doing this would
  5370. be to add a new method in the language vector, and call this
  5371. method for each language until one of them returns a non-empty
  5372. name. This would allow us to remove this hard-coded call to
  5373. an Ada function. It is not clear that this is a better approach
  5374. at this point, because all methods need to be written in a way
  5375. such that false positives never be returned. For instance, it is
  5376. important that a method does not return a wrong name for the main
  5377. procedure if the main procedure is actually written in a different
  5378. language. It is easy to guaranty this with Ada, since we use a
  5379. special symbol generated only when the main in Ada to find the name
  5380. of the main procedure. It is difficult however to see how this can
  5381. be guarantied for languages such as C, for instance. This suggests
  5382. that order of call for these methods becomes important, which means
  5383. a more complicated approach. */
  5384. new_main_name = ada_main_name ();
  5385. if (new_main_name != NULL)
  5386. {
  5387. set_main_name (new_main_name, language_ada);
  5388. return;
  5389. }
  5390. new_main_name = d_main_name ();
  5391. if (new_main_name != NULL)
  5392. {
  5393. set_main_name (new_main_name, language_d);
  5394. return;
  5395. }
  5396. new_main_name = go_main_name ();
  5397. if (new_main_name != NULL)
  5398. {
  5399. set_main_name (new_main_name, language_go);
  5400. return;
  5401. }
  5402. new_main_name = pascal_main_name ();
  5403. if (new_main_name != NULL)
  5404. {
  5405. set_main_name (new_main_name, language_pascal);
  5406. return;
  5407. }
  5408. /* The languages above didn't identify the name of the main procedure.
  5409. Fallback to "main". */
  5410. /* Try to find language for main in psymtabs. */
  5411. enum language lang
  5412. = find_quick_global_symbol_language ("main", VAR_DOMAIN);
  5413. if (lang != language_unknown)
  5414. {
  5415. set_main_name ("main", lang);
  5416. return;
  5417. }
  5418. set_main_name ("main", language_unknown);
  5419. }
  5420. /* See symtab.h. */
  5421. const char *
  5422. main_name ()
  5423. {
  5424. struct main_info *info = get_main_info ();
  5425. if (info->name_of_main == NULL)
  5426. find_main_name ();
  5427. return info->name_of_main;
  5428. }
  5429. /* Return the language of the main function. If it is not known,
  5430. return language_unknown. */
  5431. enum language
  5432. main_language (void)
  5433. {
  5434. struct main_info *info = get_main_info ();
  5435. if (info->name_of_main == NULL)
  5436. find_main_name ();
  5437. return info->language_of_main;
  5438. }
  5439. /* Handle ``executable_changed'' events for the symtab module. */
  5440. static void
  5441. symtab_observer_executable_changed (void)
  5442. {
  5443. /* NAME_OF_MAIN may no longer be the same, so reset it for now. */
  5444. set_main_name (NULL, language_unknown);
  5445. }
  5446. /* Return 1 if the supplied producer string matches the ARM RealView
  5447. compiler (armcc). */
  5448. bool
  5449. producer_is_realview (const char *producer)
  5450. {
  5451. static const char *const arm_idents[] = {
  5452. "ARM C Compiler, ADS",
  5453. "Thumb C Compiler, ADS",
  5454. "ARM C++ Compiler, ADS",
  5455. "Thumb C++ Compiler, ADS",
  5456. "ARM/Thumb C/C++ Compiler, RVCT",
  5457. "ARM C/C++ Compiler, RVCT"
  5458. };
  5459. if (producer == NULL)
  5460. return false;
  5461. for (const char *ident : arm_idents)
  5462. if (startswith (producer, ident))
  5463. return true;
  5464. return false;
  5465. }
  5466. /* The next index to hand out in response to a registration request. */
  5467. static int next_aclass_value = LOC_FINAL_VALUE;
  5468. /* The maximum number of "aclass" registrations we support. This is
  5469. constant for convenience. */
  5470. #define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 10)
  5471. /* The objects representing the various "aclass" values. The elements
  5472. from 0 up to LOC_FINAL_VALUE-1 represent themselves, and subsequent
  5473. elements are those registered at gdb initialization time. */
  5474. static struct symbol_impl symbol_impl[MAX_SYMBOL_IMPLS];
  5475. /* The globally visible pointer. This is separate from 'symbol_impl'
  5476. so that it can be const. */
  5477. const struct symbol_impl *symbol_impls = &symbol_impl[0];
  5478. /* Make sure we saved enough room in struct symbol. */
  5479. gdb_static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_ACLASS_BITS));
  5480. /* Register a computed symbol type. ACLASS must be LOC_COMPUTED. OPS
  5481. is the ops vector associated with this index. This returns the new
  5482. index, which should be used as the aclass_index field for symbols
  5483. of this type. */
  5484. int
  5485. register_symbol_computed_impl (enum address_class aclass,
  5486. const struct symbol_computed_ops *ops)
  5487. {
  5488. int result = next_aclass_value++;
  5489. gdb_assert (aclass == LOC_COMPUTED);
  5490. gdb_assert (result < MAX_SYMBOL_IMPLS);
  5491. symbol_impl[result].aclass = aclass;
  5492. symbol_impl[result].ops_computed = ops;
  5493. /* Sanity check OPS. */
  5494. gdb_assert (ops != NULL);
  5495. gdb_assert (ops->tracepoint_var_ref != NULL);
  5496. gdb_assert (ops->describe_location != NULL);
  5497. gdb_assert (ops->get_symbol_read_needs != NULL);
  5498. gdb_assert (ops->read_variable != NULL);
  5499. return result;
  5500. }
  5501. /* Register a function with frame base type. ACLASS must be LOC_BLOCK.
  5502. OPS is the ops vector associated with this index. This returns the
  5503. new index, which should be used as the aclass_index field for symbols
  5504. of this type. */
  5505. int
  5506. register_symbol_block_impl (enum address_class aclass,
  5507. const struct symbol_block_ops *ops)
  5508. {
  5509. int result = next_aclass_value++;
  5510. gdb_assert (aclass == LOC_BLOCK);
  5511. gdb_assert (result < MAX_SYMBOL_IMPLS);
  5512. symbol_impl[result].aclass = aclass;
  5513. symbol_impl[result].ops_block = ops;
  5514. /* Sanity check OPS. */
  5515. gdb_assert (ops != NULL);
  5516. gdb_assert (ops->find_frame_base_location != NULL);
  5517. return result;
  5518. }
  5519. /* Register a register symbol type. ACLASS must be LOC_REGISTER or
  5520. LOC_REGPARM_ADDR. OPS is the register ops vector associated with
  5521. this index. This returns the new index, which should be used as
  5522. the aclass_index field for symbols of this type. */
  5523. int
  5524. register_symbol_register_impl (enum address_class aclass,
  5525. const struct symbol_register_ops *ops)
  5526. {
  5527. int result = next_aclass_value++;
  5528. gdb_assert (aclass == LOC_REGISTER || aclass == LOC_REGPARM_ADDR);
  5529. gdb_assert (result < MAX_SYMBOL_IMPLS);
  5530. symbol_impl[result].aclass = aclass;
  5531. symbol_impl[result].ops_register = ops;
  5532. return result;
  5533. }
  5534. /* Initialize elements of 'symbol_impl' for the constants in enum
  5535. address_class. */
  5536. static void
  5537. initialize_ordinary_address_classes (void)
  5538. {
  5539. int i;
  5540. for (i = 0; i < LOC_FINAL_VALUE; ++i)
  5541. symbol_impl[i].aclass = (enum address_class) i;
  5542. }
  5543. /* See symtab.h. */
  5544. struct objfile *
  5545. symbol_objfile (const struct symbol *symbol)
  5546. {
  5547. gdb_assert (symbol->is_objfile_owned ());
  5548. return symbol->owner.symtab->compunit ()->objfile ();
  5549. }
  5550. /* See symtab.h. */
  5551. struct gdbarch *
  5552. symbol_arch (const struct symbol *symbol)
  5553. {
  5554. if (!symbol->is_objfile_owned ())
  5555. return symbol->owner.arch;
  5556. return symbol->owner.symtab->compunit ()->objfile ()->arch ();
  5557. }
  5558. /* See symtab.h. */
  5559. struct symtab *
  5560. symbol_symtab (const struct symbol *symbol)
  5561. {
  5562. gdb_assert (symbol->is_objfile_owned ());
  5563. return symbol->owner.symtab;
  5564. }
  5565. /* See symtab.h. */
  5566. void
  5567. symbol_set_symtab (struct symbol *symbol, struct symtab *symtab)
  5568. {
  5569. gdb_assert (symbol->is_objfile_owned ());
  5570. symbol->owner.symtab = symtab;
  5571. }
  5572. /* See symtab.h. */
  5573. CORE_ADDR
  5574. get_symbol_address (const struct symbol *sym)
  5575. {
  5576. gdb_assert (sym->maybe_copied);
  5577. gdb_assert (sym->aclass () == LOC_STATIC);
  5578. const char *linkage_name = sym->linkage_name ();
  5579. for (objfile *objfile : current_program_space->objfiles ())
  5580. {
  5581. if (objfile->separate_debug_objfile_backlink != nullptr)
  5582. continue;
  5583. bound_minimal_symbol minsym
  5584. = lookup_minimal_symbol_linkage (linkage_name, objfile);
  5585. if (minsym.minsym != nullptr)
  5586. return BMSYMBOL_VALUE_ADDRESS (minsym);
  5587. }
  5588. return sym->value.address;
  5589. }
  5590. /* See symtab.h. */
  5591. CORE_ADDR
  5592. get_msymbol_address (struct objfile *objf, const struct minimal_symbol *minsym)
  5593. {
  5594. gdb_assert (minsym->maybe_copied);
  5595. gdb_assert ((objf->flags & OBJF_MAINLINE) == 0);
  5596. const char *linkage_name = minsym->linkage_name ();
  5597. for (objfile *objfile : current_program_space->objfiles ())
  5598. {
  5599. if (objfile->separate_debug_objfile_backlink == nullptr
  5600. && (objfile->flags & OBJF_MAINLINE) != 0)
  5601. {
  5602. bound_minimal_symbol found
  5603. = lookup_minimal_symbol_linkage (linkage_name, objfile);
  5604. if (found.minsym != nullptr)
  5605. return BMSYMBOL_VALUE_ADDRESS (found);
  5606. }
  5607. }
  5608. return (minsym->value.address
  5609. + objf->section_offsets[minsym->section_index ()]);
  5610. }
  5611. /* Hold the sub-commands of 'info module'. */
  5612. static struct cmd_list_element *info_module_cmdlist = NULL;
  5613. /* See symtab.h. */
  5614. std::vector<module_symbol_search>
  5615. search_module_symbols (const char *module_regexp, const char *regexp,
  5616. const char *type_regexp, search_domain kind)
  5617. {
  5618. std::vector<module_symbol_search> results;
  5619. /* Search for all modules matching MODULE_REGEXP. */
  5620. global_symbol_searcher spec1 (MODULES_DOMAIN, module_regexp);
  5621. spec1.set_exclude_minsyms (true);
  5622. std::vector<symbol_search> modules = spec1.search ();
  5623. /* Now search for all symbols of the required KIND matching the required
  5624. regular expressions. We figure out which ones are in which modules
  5625. below. */
  5626. global_symbol_searcher spec2 (kind, regexp);
  5627. spec2.set_symbol_type_regexp (type_regexp);
  5628. spec2.set_exclude_minsyms (true);
  5629. std::vector<symbol_search> symbols = spec2.search ();
  5630. /* Now iterate over all MODULES, checking to see which items from
  5631. SYMBOLS are in each module. */
  5632. for (const symbol_search &p : modules)
  5633. {
  5634. QUIT;
  5635. /* This is a module. */
  5636. gdb_assert (p.symbol != nullptr);
  5637. std::string prefix = p.symbol->print_name ();
  5638. prefix += "::";
  5639. for (const symbol_search &q : symbols)
  5640. {
  5641. if (q.symbol == nullptr)
  5642. continue;
  5643. if (strncmp (q.symbol->print_name (), prefix.c_str (),
  5644. prefix.size ()) != 0)
  5645. continue;
  5646. results.push_back ({p, q});
  5647. }
  5648. }
  5649. return results;
  5650. }
  5651. /* Implement the core of both 'info module functions' and 'info module
  5652. variables'. */
  5653. static void
  5654. info_module_subcommand (bool quiet, const char *module_regexp,
  5655. const char *regexp, const char *type_regexp,
  5656. search_domain kind)
  5657. {
  5658. /* Print a header line. Don't build the header line bit by bit as this
  5659. prevents internationalisation. */
  5660. if (!quiet)
  5661. {
  5662. if (module_regexp == nullptr)
  5663. {
  5664. if (type_regexp == nullptr)
  5665. {
  5666. if (regexp == nullptr)
  5667. gdb_printf ((kind == VARIABLES_DOMAIN
  5668. ? _("All variables in all modules:")
  5669. : _("All functions in all modules:")));
  5670. else
  5671. gdb_printf
  5672. ((kind == VARIABLES_DOMAIN
  5673. ? _("All variables matching regular expression"
  5674. " \"%s\" in all modules:")
  5675. : _("All functions matching regular expression"
  5676. " \"%s\" in all modules:")),
  5677. regexp);
  5678. }
  5679. else
  5680. {
  5681. if (regexp == nullptr)
  5682. gdb_printf
  5683. ((kind == VARIABLES_DOMAIN
  5684. ? _("All variables with type matching regular "
  5685. "expression \"%s\" in all modules:")
  5686. : _("All functions with type matching regular "
  5687. "expression \"%s\" in all modules:")),
  5688. type_regexp);
  5689. else
  5690. gdb_printf
  5691. ((kind == VARIABLES_DOMAIN
  5692. ? _("All variables matching regular expression "
  5693. "\"%s\",\n\twith type matching regular "
  5694. "expression \"%s\" in all modules:")
  5695. : _("All functions matching regular expression "
  5696. "\"%s\",\n\twith type matching regular "
  5697. "expression \"%s\" in all modules:")),
  5698. regexp, type_regexp);
  5699. }
  5700. }
  5701. else
  5702. {
  5703. if (type_regexp == nullptr)
  5704. {
  5705. if (regexp == nullptr)
  5706. gdb_printf
  5707. ((kind == VARIABLES_DOMAIN
  5708. ? _("All variables in all modules matching regular "
  5709. "expression \"%s\":")
  5710. : _("All functions in all modules matching regular "
  5711. "expression \"%s\":")),
  5712. module_regexp);
  5713. else
  5714. gdb_printf
  5715. ((kind == VARIABLES_DOMAIN
  5716. ? _("All variables matching regular expression "
  5717. "\"%s\",\n\tin all modules matching regular "
  5718. "expression \"%s\":")
  5719. : _("All functions matching regular expression "
  5720. "\"%s\",\n\tin all modules matching regular "
  5721. "expression \"%s\":")),
  5722. regexp, module_regexp);
  5723. }
  5724. else
  5725. {
  5726. if (regexp == nullptr)
  5727. gdb_printf
  5728. ((kind == VARIABLES_DOMAIN
  5729. ? _("All variables with type matching regular "
  5730. "expression \"%s\"\n\tin all modules matching "
  5731. "regular expression \"%s\":")
  5732. : _("All functions with type matching regular "
  5733. "expression \"%s\"\n\tin all modules matching "
  5734. "regular expression \"%s\":")),
  5735. type_regexp, module_regexp);
  5736. else
  5737. gdb_printf
  5738. ((kind == VARIABLES_DOMAIN
  5739. ? _("All variables matching regular expression "
  5740. "\"%s\",\n\twith type matching regular expression "
  5741. "\"%s\",\n\tin all modules matching regular "
  5742. "expression \"%s\":")
  5743. : _("All functions matching regular expression "
  5744. "\"%s\",\n\twith type matching regular expression "
  5745. "\"%s\",\n\tin all modules matching regular "
  5746. "expression \"%s\":")),
  5747. regexp, type_regexp, module_regexp);
  5748. }
  5749. }
  5750. gdb_printf ("\n");
  5751. }
  5752. /* Find all symbols of type KIND matching the given regular expressions
  5753. along with the symbols for the modules in which those symbols
  5754. reside. */
  5755. std::vector<module_symbol_search> module_symbols
  5756. = search_module_symbols (module_regexp, regexp, type_regexp, kind);
  5757. std::sort (module_symbols.begin (), module_symbols.end (),
  5758. [] (const module_symbol_search &a, const module_symbol_search &b)
  5759. {
  5760. if (a.first < b.first)
  5761. return true;
  5762. else if (a.first == b.first)
  5763. return a.second < b.second;
  5764. else
  5765. return false;
  5766. });
  5767. const char *last_filename = "";
  5768. const symbol *last_module_symbol = nullptr;
  5769. for (const module_symbol_search &ms : module_symbols)
  5770. {
  5771. const symbol_search &p = ms.first;
  5772. const symbol_search &q = ms.second;
  5773. gdb_assert (q.symbol != nullptr);
  5774. if (last_module_symbol != p.symbol)
  5775. {
  5776. gdb_printf ("\n");
  5777. gdb_printf (_("Module \"%s\":\n"), p.symbol->print_name ());
  5778. last_module_symbol = p.symbol;
  5779. last_filename = "";
  5780. }
  5781. print_symbol_info (FUNCTIONS_DOMAIN, q.symbol, q.block,
  5782. last_filename);
  5783. last_filename
  5784. = symtab_to_filename_for_display (symbol_symtab (q.symbol));
  5785. }
  5786. }
  5787. /* Hold the option values for the 'info module .....' sub-commands. */
  5788. struct info_modules_var_func_options
  5789. {
  5790. bool quiet = false;
  5791. std::string type_regexp;
  5792. std::string module_regexp;
  5793. };
  5794. /* The options used by 'info module variables' and 'info module functions'
  5795. commands. */
  5796. static const gdb::option::option_def info_modules_var_func_options_defs [] = {
  5797. gdb::option::boolean_option_def<info_modules_var_func_options> {
  5798. "q",
  5799. [] (info_modules_var_func_options *opt) { return &opt->quiet; },
  5800. nullptr, /* show_cmd_cb */
  5801. nullptr /* set_doc */
  5802. },
  5803. gdb::option::string_option_def<info_modules_var_func_options> {
  5804. "t",
  5805. [] (info_modules_var_func_options *opt) { return &opt->type_regexp; },
  5806. nullptr, /* show_cmd_cb */
  5807. nullptr /* set_doc */
  5808. },
  5809. gdb::option::string_option_def<info_modules_var_func_options> {
  5810. "m",
  5811. [] (info_modules_var_func_options *opt) { return &opt->module_regexp; },
  5812. nullptr, /* show_cmd_cb */
  5813. nullptr /* set_doc */
  5814. }
  5815. };
  5816. /* Return the option group used by the 'info module ...' sub-commands. */
  5817. static inline gdb::option::option_def_group
  5818. make_info_modules_var_func_options_def_group
  5819. (info_modules_var_func_options *opts)
  5820. {
  5821. return {{info_modules_var_func_options_defs}, opts};
  5822. }
  5823. /* Implements the 'info module functions' command. */
  5824. static void
  5825. info_module_functions_command (const char *args, int from_tty)
  5826. {
  5827. info_modules_var_func_options opts;
  5828. auto grp = make_info_modules_var_func_options_def_group (&opts);
  5829. gdb::option::process_options
  5830. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  5831. if (args != nullptr && *args == '\0')
  5832. args = nullptr;
  5833. info_module_subcommand
  5834. (opts.quiet,
  5835. opts.module_regexp.empty () ? nullptr : opts.module_regexp.c_str (), args,
  5836. opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
  5837. FUNCTIONS_DOMAIN);
  5838. }
  5839. /* Implements the 'info module variables' command. */
  5840. static void
  5841. info_module_variables_command (const char *args, int from_tty)
  5842. {
  5843. info_modules_var_func_options opts;
  5844. auto grp = make_info_modules_var_func_options_def_group (&opts);
  5845. gdb::option::process_options
  5846. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  5847. if (args != nullptr && *args == '\0')
  5848. args = nullptr;
  5849. info_module_subcommand
  5850. (opts.quiet,
  5851. opts.module_regexp.empty () ? nullptr : opts.module_regexp.c_str (), args,
  5852. opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
  5853. VARIABLES_DOMAIN);
  5854. }
  5855. /* Command completer for 'info module ...' sub-commands. */
  5856. static void
  5857. info_module_var_func_command_completer (struct cmd_list_element *ignore,
  5858. completion_tracker &tracker,
  5859. const char *text,
  5860. const char * /* word */)
  5861. {
  5862. const auto group = make_info_modules_var_func_options_def_group (nullptr);
  5863. if (gdb::option::complete_options
  5864. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
  5865. return;
  5866. const char *word = advance_to_expression_complete_word_point (tracker, text);
  5867. symbol_completer (ignore, tracker, text, word);
  5868. }
  5869. void _initialize_symtab ();
  5870. void
  5871. _initialize_symtab ()
  5872. {
  5873. cmd_list_element *c;
  5874. initialize_ordinary_address_classes ();
  5875. c = add_info ("variables", info_variables_command,
  5876. info_print_args_help (_("\
  5877. All global and static variable names or those matching REGEXPs.\n\
  5878. Usage: info variables [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
  5879. Prints the global and static variables.\n"),
  5880. _("global and static variables"),
  5881. true));
  5882. set_cmd_completer_handle_brkchars (c, info_vars_funcs_command_completer);
  5883. c = add_info ("functions", info_functions_command,
  5884. info_print_args_help (_("\
  5885. All function names or those matching REGEXPs.\n\
  5886. Usage: info functions [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
  5887. Prints the functions.\n"),
  5888. _("functions"),
  5889. true));
  5890. set_cmd_completer_handle_brkchars (c, info_vars_funcs_command_completer);
  5891. c = add_info ("types", info_types_command, _("\
  5892. All type names, or those matching REGEXP.\n\
  5893. Usage: info types [-q] [REGEXP]\n\
  5894. Print information about all types matching REGEXP, or all types if no\n\
  5895. REGEXP is given. The optional flag -q disables printing of headers."));
  5896. set_cmd_completer_handle_brkchars (c, info_types_command_completer);
  5897. const auto info_sources_opts
  5898. = make_info_sources_options_def_group (nullptr);
  5899. static std::string info_sources_help
  5900. = gdb::option::build_help (_("\
  5901. All source files in the program or those matching REGEXP.\n\
  5902. Usage: info sources [OPTION]... [REGEXP]\n\
  5903. By default, REGEXP is used to match anywhere in the filename.\n\
  5904. \n\
  5905. Options:\n\
  5906. %OPTIONS%"),
  5907. info_sources_opts);
  5908. c = add_info ("sources", info_sources_command, info_sources_help.c_str ());
  5909. set_cmd_completer_handle_brkchars (c, info_sources_command_completer);
  5910. c = add_info ("modules", info_modules_command,
  5911. _("All module names, or those matching REGEXP."));
  5912. set_cmd_completer_handle_brkchars (c, info_types_command_completer);
  5913. add_basic_prefix_cmd ("module", class_info, _("\
  5914. Print information about modules."),
  5915. &info_module_cmdlist, 0, &infolist);
  5916. c = add_cmd ("functions", class_info, info_module_functions_command, _("\
  5917. Display functions arranged by modules.\n\
  5918. Usage: info module functions [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
  5919. Print a summary of all functions within each Fortran module, grouped by\n\
  5920. module and file. For each function the line on which the function is\n\
  5921. defined is given along with the type signature and name of the function.\n\
  5922. \n\
  5923. If REGEXP is provided then only functions whose name matches REGEXP are\n\
  5924. listed. If MODREGEXP is provided then only functions in modules matching\n\
  5925. MODREGEXP are listed. If TYPEREGEXP is given then only functions whose\n\
  5926. type signature matches TYPEREGEXP are listed.\n\
  5927. \n\
  5928. The -q flag suppresses printing some header information."),
  5929. &info_module_cmdlist);
  5930. set_cmd_completer_handle_brkchars
  5931. (c, info_module_var_func_command_completer);
  5932. c = add_cmd ("variables", class_info, info_module_variables_command, _("\
  5933. Display variables arranged by modules.\n\
  5934. Usage: info module variables [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
  5935. Print a summary of all variables within each Fortran module, grouped by\n\
  5936. module and file. For each variable the line on which the variable is\n\
  5937. defined is given along with the type and name of the variable.\n\
  5938. \n\
  5939. If REGEXP is provided then only variables whose name matches REGEXP are\n\
  5940. listed. If MODREGEXP is provided then only variables in modules matching\n\
  5941. MODREGEXP are listed. If TYPEREGEXP is given then only variables whose\n\
  5942. type matches TYPEREGEXP are listed.\n\
  5943. \n\
  5944. The -q flag suppresses printing some header information."),
  5945. &info_module_cmdlist);
  5946. set_cmd_completer_handle_brkchars
  5947. (c, info_module_var_func_command_completer);
  5948. add_com ("rbreak", class_breakpoint, rbreak_command,
  5949. _("Set a breakpoint for all functions matching REGEXP."));
  5950. add_setshow_enum_cmd ("multiple-symbols", no_class,
  5951. multiple_symbols_modes, &multiple_symbols_mode,
  5952. _("\
  5953. Set how the debugger handles ambiguities in expressions."), _("\
  5954. Show how the debugger handles ambiguities in expressions."), _("\
  5955. Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
  5956. NULL, NULL, &setlist, &showlist);
  5957. add_setshow_boolean_cmd ("basenames-may-differ", class_obscure,
  5958. &basenames_may_differ, _("\
  5959. Set whether a source file may have multiple base names."), _("\
  5960. Show whether a source file may have multiple base names."), _("\
  5961. (A \"base name\" is the name of a file with the directory part removed.\n\
  5962. Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
  5963. If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
  5964. before comparing them. Canonicalization is an expensive operation,\n\
  5965. but it allows the same file be known by more than one base name.\n\
  5966. If not set (the default), all source files are assumed to have just\n\
  5967. one base name, and gdb will do file name comparisons more efficiently."),
  5968. NULL, NULL,
  5969. &setlist, &showlist);
  5970. add_setshow_zuinteger_cmd ("symtab-create", no_class, &symtab_create_debug,
  5971. _("Set debugging of symbol table creation."),
  5972. _("Show debugging of symbol table creation."), _("\
  5973. When enabled (non-zero), debugging messages are printed when building\n\
  5974. symbol tables. A value of 1 (one) normally provides enough information.\n\
  5975. A value greater than 1 provides more verbose information."),
  5976. NULL,
  5977. NULL,
  5978. &setdebuglist, &showdebuglist);
  5979. add_setshow_zuinteger_cmd ("symbol-lookup", no_class, &symbol_lookup_debug,
  5980. _("\
  5981. Set debugging of symbol lookup."), _("\
  5982. Show debugging of symbol lookup."), _("\
  5983. When enabled (non-zero), symbol lookups are logged."),
  5984. NULL, NULL,
  5985. &setdebuglist, &showdebuglist);
  5986. add_setshow_zuinteger_cmd ("symbol-cache-size", no_class,
  5987. &new_symbol_cache_size,
  5988. _("Set the size of the symbol cache."),
  5989. _("Show the size of the symbol cache."), _("\
  5990. The size of the symbol cache.\n\
  5991. If zero then the symbol cache is disabled."),
  5992. set_symbol_cache_size_handler, NULL,
  5993. &maintenance_set_cmdlist,
  5994. &maintenance_show_cmdlist);
  5995. add_setshow_boolean_cmd ("ignore-prologue-end-flag", no_class,
  5996. &ignore_prologue_end_flag,
  5997. _("Set if the PROLOGUE-END flag is ignored."),
  5998. _("Show if the PROLOGUE-END flag is ignored."),
  5999. _("\
  6000. The PROLOGUE-END flag from the line-table entries is used to place \
  6001. breakpoints past the prologue of functions. Disabeling its use use forces \
  6002. the use of prologue scanners."),
  6003. nullptr, nullptr,
  6004. &maintenance_set_cmdlist,
  6005. &maintenance_show_cmdlist);
  6006. add_cmd ("symbol-cache", class_maintenance, maintenance_print_symbol_cache,
  6007. _("Dump the symbol cache for each program space."),
  6008. &maintenanceprintlist);
  6009. add_cmd ("symbol-cache-statistics", class_maintenance,
  6010. maintenance_print_symbol_cache_statistics,
  6011. _("Print symbol cache statistics for each program space."),
  6012. &maintenanceprintlist);
  6013. cmd_list_element *maintenance_flush_symbol_cache_cmd
  6014. = add_cmd ("symbol-cache", class_maintenance,
  6015. maintenance_flush_symbol_cache,
  6016. _("Flush the symbol cache for each program space."),
  6017. &maintenanceflushlist);
  6018. c = add_alias_cmd ("flush-symbol-cache", maintenance_flush_symbol_cache_cmd,
  6019. class_maintenance, 0, &maintenancelist);
  6020. deprecate_cmd (c, "maintenancelist flush symbol-cache");
  6021. gdb::observers::executable_changed.attach (symtab_observer_executable_changed,
  6022. "symtab");
  6023. gdb::observers::new_objfile.attach (symtab_new_objfile_observer, "symtab");
  6024. gdb::observers::free_objfile.attach (symtab_free_objfile_observer, "symtab");
  6025. }