psymtab.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. /* Partial symbol tables.
  2. Copyright (C) 2009-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 "objfiles.h"
  17. #include "psympriv.h"
  18. #include "block.h"
  19. #include "filenames.h"
  20. #include "source.h"
  21. #include "addrmap.h"
  22. #include "gdbtypes.h"
  23. #include "ui-out.h"
  24. #include "command.h"
  25. #include "readline/tilde.h"
  26. #include "gdbsupport/gdb_regex.h"
  27. #include "dictionary.h"
  28. #include "language.h"
  29. #include "cp-support.h"
  30. #include "gdbcmd.h"
  31. #include <algorithm>
  32. #include <set>
  33. #include "gdbsupport/buildargv.h"
  34. static struct partial_symbol *lookup_partial_symbol (struct objfile *,
  35. struct partial_symtab *,
  36. const lookup_name_info &,
  37. int,
  38. domain_enum);
  39. static const char *psymtab_to_fullname (struct partial_symtab *ps);
  40. static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
  41. struct partial_symtab *,
  42. CORE_ADDR,
  43. struct obj_section *);
  44. static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
  45. struct partial_symtab *pst);
  46. psymtab_storage::~psymtab_storage ()
  47. {
  48. partial_symtab *iter = psymtabs;
  49. while (iter != nullptr)
  50. {
  51. partial_symtab *next = iter->next;
  52. delete iter;
  53. iter = next;
  54. }
  55. }
  56. /* See psymtab.h. */
  57. void
  58. psymtab_storage::install_psymtab (partial_symtab *pst)
  59. {
  60. pst->next = psymtabs;
  61. psymtabs = pst;
  62. }
  63. /* Ensure that the partial symbols for OBJFILE have been loaded. This
  64. will print a message when symbols are loaded. This function
  65. returns a range adapter suitable for iterating over the psymtabs of
  66. OBJFILE. */
  67. psymtab_storage::partial_symtab_range
  68. psymbol_functions::require_partial_symbols (struct objfile *objfile)
  69. {
  70. objfile->require_partial_symbols (true);
  71. return m_partial_symtabs->range ();
  72. }
  73. /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
  74. We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
  75. static struct partial_symtab *
  76. find_pc_sect_psymtab_closer (struct objfile *objfile,
  77. CORE_ADDR pc, struct obj_section *section,
  78. struct partial_symtab *pst,
  79. struct bound_minimal_symbol msymbol)
  80. {
  81. struct partial_symtab *tpst;
  82. struct partial_symtab *best_pst = pst;
  83. CORE_ADDR best_addr = pst->text_low (objfile);
  84. gdb_assert (!pst->psymtabs_addrmap_supported);
  85. /* An objfile that has its functions reordered might have
  86. many partial symbol tables containing the PC, but
  87. we want the partial symbol table that contains the
  88. function containing the PC. */
  89. if (!(objfile->flags & OBJF_REORDERED)
  90. && section == NULL) /* Can't validate section this way. */
  91. return pst;
  92. if (msymbol.minsym == NULL)
  93. return pst;
  94. /* The code range of partial symtabs sometimes overlap, so, in
  95. the loop below, we need to check all partial symtabs and
  96. find the one that fits better for the given PC address. We
  97. select the partial symtab that contains a symbol whose
  98. address is closest to the PC address. By closest we mean
  99. that find_pc_sect_symbol returns the symbol with address
  100. that is closest and still less than the given PC. */
  101. for (tpst = pst; tpst != NULL; tpst = tpst->next)
  102. {
  103. if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
  104. {
  105. struct partial_symbol *p;
  106. CORE_ADDR this_addr;
  107. /* NOTE: This assumes that every psymbol has a
  108. corresponding msymbol, which is not necessarily
  109. true; the debug info might be much richer than the
  110. object's symbol table. */
  111. p = find_pc_sect_psymbol (objfile, tpst, pc, section);
  112. if (p != NULL
  113. && (p->address (objfile) == BMSYMBOL_VALUE_ADDRESS (msymbol)))
  114. return tpst;
  115. /* Also accept the textlow value of a psymtab as a
  116. "symbol", to provide some support for partial
  117. symbol tables with line information but no debug
  118. symbols (e.g. those produced by an assembler). */
  119. if (p != NULL)
  120. this_addr = p->address (objfile);
  121. else
  122. this_addr = tpst->text_low (objfile);
  123. /* Check whether it is closer than our current
  124. BEST_ADDR. Since this symbol address is
  125. necessarily lower or equal to PC, the symbol closer
  126. to PC is the symbol which address is the highest.
  127. This way we return the psymtab which contains such
  128. best match symbol. This can help in cases where the
  129. symbol information/debuginfo is not complete, like
  130. for instance on IRIX6 with gcc, where no debug info
  131. is emitted for statics. (See also the nodebug.exp
  132. testcase.) */
  133. if (this_addr > best_addr)
  134. {
  135. best_addr = this_addr;
  136. best_pst = tpst;
  137. }
  138. }
  139. }
  140. return best_pst;
  141. }
  142. /* See psympriv.h. */
  143. struct partial_symtab *
  144. psymbol_functions::find_pc_sect_psymtab (struct objfile *objfile,
  145. CORE_ADDR pc,
  146. struct obj_section *section,
  147. struct bound_minimal_symbol msymbol)
  148. {
  149. /* Try just the PSYMTABS_ADDRMAP mapping first as it has better
  150. granularity than the later used TEXTLOW/TEXTHIGH one. However, we need
  151. to take care as the PSYMTABS_ADDRMAP can hold things other than partial
  152. symtabs in some cases.
  153. This function should only be called for objfiles that are using partial
  154. symtabs, not for objfiles that are using indexes (.gdb_index or
  155. .debug_names), however 'maintenance print psymbols' calls this function
  156. directly for all objfiles. If we assume that PSYMTABS_ADDRMAP contains
  157. partial symtabs then we will end up returning a pointer to an object
  158. that is not a partial_symtab, which doesn't end well. */
  159. if (m_partial_symtabs->psymtabs != NULL
  160. && m_partial_symtabs->psymtabs_addrmap != NULL)
  161. {
  162. CORE_ADDR baseaddr = objfile->text_section_offset ();
  163. struct partial_symtab *pst
  164. = ((struct partial_symtab *)
  165. addrmap_find (m_partial_symtabs->psymtabs_addrmap,
  166. pc - baseaddr));
  167. if (pst != NULL)
  168. {
  169. /* FIXME: addrmaps currently do not handle overlayed sections,
  170. so fall back to the non-addrmap case if we're debugging
  171. overlays and the addrmap returned the wrong section. */
  172. if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
  173. {
  174. struct partial_symbol *p;
  175. /* NOTE: This assumes that every psymbol has a
  176. corresponding msymbol, which is not necessarily
  177. true; the debug info might be much richer than the
  178. object's symbol table. */
  179. p = find_pc_sect_psymbol (objfile, pst, pc, section);
  180. if (p == NULL
  181. || (p->address (objfile)
  182. != BMSYMBOL_VALUE_ADDRESS (msymbol)))
  183. goto next;
  184. }
  185. /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
  186. PSYMTABS_ADDRMAP we used has already the best 1-byte
  187. granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
  188. a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
  189. overlap. */
  190. return pst;
  191. }
  192. }
  193. next:
  194. /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
  195. which still have no corresponding full SYMTABs read. But it is not
  196. present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
  197. so far. */
  198. /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
  199. its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
  200. debug info type in single OBJFILE. */
  201. for (partial_symtab *pst : require_partial_symbols (objfile))
  202. if (!pst->psymtabs_addrmap_supported
  203. && pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
  204. {
  205. struct partial_symtab *best_pst;
  206. best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
  207. msymbol);
  208. if (best_pst != NULL)
  209. return best_pst;
  210. }
  211. return NULL;
  212. }
  213. /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
  214. the definition of quick_symbol_functions in symfile.h. */
  215. struct compunit_symtab *
  216. psymbol_functions::find_pc_sect_compunit_symtab
  217. (struct objfile *objfile,
  218. struct bound_minimal_symbol msymbol,
  219. CORE_ADDR pc,
  220. struct obj_section *section,
  221. int warn_if_readin)
  222. {
  223. struct partial_symtab *ps = find_pc_sect_psymtab (objfile,
  224. pc, section,
  225. msymbol);
  226. if (ps != NULL)
  227. {
  228. if (warn_if_readin && ps->readin_p (objfile))
  229. /* Might want to error() here (in case symtab is corrupt and
  230. will cause a core dump), but maybe we can successfully
  231. continue, so let's not. */
  232. warning (_("\
  233. (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
  234. paddress (objfile->arch (), pc));
  235. psymtab_to_symtab (objfile, ps);
  236. return ps->get_compunit_symtab (objfile);
  237. }
  238. return NULL;
  239. }
  240. /* Find which partial symbol within a psymtab matches PC and SECTION.
  241. Return NULL if none. */
  242. static struct partial_symbol *
  243. find_pc_sect_psymbol (struct objfile *objfile,
  244. struct partial_symtab *psymtab, CORE_ADDR pc,
  245. struct obj_section *section)
  246. {
  247. struct partial_symbol *best = NULL;
  248. CORE_ADDR best_pc;
  249. const CORE_ADDR textlow = psymtab->text_low (objfile);
  250. gdb_assert (psymtab != NULL);
  251. /* Cope with programs that start at address 0. */
  252. best_pc = (textlow != 0) ? textlow - 1 : 0;
  253. /* Search the global symbols as well as the static symbols, so that
  254. find_pc_partial_function doesn't use a minimal symbol and thus
  255. cache a bad endaddr. */
  256. for (partial_symbol *p : psymtab->global_psymbols)
  257. {
  258. if (p->domain == VAR_DOMAIN
  259. && p->aclass == LOC_BLOCK
  260. && pc >= p->address (objfile)
  261. && (p->address (objfile) > best_pc
  262. || (psymtab->text_low (objfile) == 0
  263. && best_pc == 0 && p->address (objfile) == 0)))
  264. {
  265. if (section != NULL) /* Match on a specific section. */
  266. {
  267. if (!matching_obj_sections (p->obj_section (objfile),
  268. section))
  269. continue;
  270. }
  271. best_pc = p->address (objfile);
  272. best = p;
  273. }
  274. }
  275. for (partial_symbol *p : psymtab->static_psymbols)
  276. {
  277. if (p->domain == VAR_DOMAIN
  278. && p->aclass == LOC_BLOCK
  279. && pc >= p->address (objfile)
  280. && (p->address (objfile) > best_pc
  281. || (psymtab->text_low (objfile) == 0
  282. && best_pc == 0 && p->address (objfile) == 0)))
  283. {
  284. if (section != NULL) /* Match on a specific section. */
  285. {
  286. if (!matching_obj_sections (p->obj_section (objfile),
  287. section))
  288. continue;
  289. }
  290. best_pc = p->address (objfile);
  291. best = p;
  292. }
  293. }
  294. return best;
  295. }
  296. /* Psymtab version of lookup_global_symbol_language. See its definition in
  297. the definition of quick_symbol_functions in symfile.h. */
  298. enum language
  299. psymbol_functions::lookup_global_symbol_language (struct objfile *objfile,
  300. const char *name,
  301. domain_enum domain,
  302. bool *symbol_found_p)
  303. {
  304. *symbol_found_p = false;
  305. if (objfile->sf == NULL)
  306. return language_unknown;
  307. lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
  308. for (partial_symtab *ps : require_partial_symbols (objfile))
  309. {
  310. struct partial_symbol *psym;
  311. if (ps->readin_p (objfile))
  312. continue;
  313. psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
  314. if (psym)
  315. {
  316. *symbol_found_p = true;
  317. return psym->ginfo.language ();
  318. }
  319. }
  320. return language_unknown;
  321. }
  322. /* Returns true if PSYM matches LOOKUP_NAME. */
  323. static bool
  324. psymbol_name_matches (partial_symbol *psym,
  325. const lookup_name_info &lookup_name)
  326. {
  327. const language_defn *lang = language_def (psym->ginfo.language ());
  328. symbol_name_matcher_ftype *name_match
  329. = lang->get_symbol_name_matcher (lookup_name);
  330. return name_match (psym->ginfo.search_name (), lookup_name, NULL);
  331. }
  332. /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
  333. the global block of PST if GLOBAL, and otherwise the static block.
  334. MATCH is the comparison operation that returns true iff MATCH (s,
  335. NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
  336. non-null, the symbols in the block are assumed to be ordered
  337. according to it (allowing binary search). It must be compatible
  338. with MATCH. Returns the symbol, if found, and otherwise NULL. */
  339. static struct partial_symbol *
  340. match_partial_symbol (struct objfile *objfile,
  341. struct partial_symtab *pst, int global,
  342. const lookup_name_info &name, domain_enum domain,
  343. symbol_compare_ftype *ordered_compare)
  344. {
  345. struct partial_symbol **start, **psym;
  346. struct partial_symbol **top, **real_top, **bottom, **center;
  347. int length = (global
  348. ? pst->global_psymbols.size ()
  349. : pst->static_psymbols.size ());
  350. int do_linear_search = 1;
  351. if (length == 0)
  352. return NULL;
  353. start = (global ?
  354. &pst->global_psymbols[0] :
  355. &pst->static_psymbols[0]);
  356. if (global && ordered_compare) /* Can use a binary search. */
  357. {
  358. do_linear_search = 0;
  359. /* Binary search. This search is guaranteed to end with center
  360. pointing at the earliest partial symbol whose name might be
  361. correct. At that point *all* partial symbols with an
  362. appropriate name will be checked against the correct
  363. domain. */
  364. bottom = start;
  365. top = start + length - 1;
  366. real_top = top;
  367. while (top > bottom)
  368. {
  369. center = bottom + (top - bottom) / 2;
  370. gdb_assert (center < top);
  371. enum language lang = (*center)->ginfo.language ();
  372. const char *lang_ln = name.language_lookup_name (lang);
  373. if (ordered_compare ((*center)->ginfo.search_name (),
  374. lang_ln) >= 0)
  375. top = center;
  376. else
  377. bottom = center + 1;
  378. }
  379. gdb_assert (top == bottom);
  380. while (top <= real_top
  381. && psymbol_name_matches (*top, name))
  382. {
  383. if (symbol_matches_domain ((*top)->ginfo.language (),
  384. (*top)->domain, domain))
  385. return *top;
  386. top++;
  387. }
  388. }
  389. /* Can't use a binary search or else we found during the binary search that
  390. we should also do a linear search. */
  391. if (do_linear_search)
  392. {
  393. for (psym = start; psym < start + length; psym++)
  394. {
  395. if (symbol_matches_domain ((*psym)->ginfo.language (),
  396. (*psym)->domain, domain)
  397. && psymbol_name_matches (*psym, name))
  398. return *psym;
  399. }
  400. }
  401. return NULL;
  402. }
  403. /* Look, in partial_symtab PST, for symbol whose natural name is
  404. LOOKUP_NAME. Check the global symbols if GLOBAL, the static
  405. symbols if not. */
  406. static struct partial_symbol *
  407. lookup_partial_symbol (struct objfile *objfile,
  408. struct partial_symtab *pst,
  409. const lookup_name_info &lookup_name,
  410. int global, domain_enum domain)
  411. {
  412. struct partial_symbol **start, **psym;
  413. struct partial_symbol **top, **real_top, **bottom, **center;
  414. int length = (global
  415. ? pst->global_psymbols.size ()
  416. : pst->static_psymbols.size ());
  417. int do_linear_search = 1;
  418. if (length == 0)
  419. return NULL;
  420. start = (global ?
  421. &pst->global_psymbols[0] :
  422. &pst->static_psymbols[0]);
  423. if (global) /* This means we can use a binary search. */
  424. {
  425. do_linear_search = 0;
  426. /* Binary search. This search is guaranteed to end with center
  427. pointing at the earliest partial symbol whose name might be
  428. correct. At that point *all* partial symbols with an
  429. appropriate name will be checked against the correct
  430. domain. */
  431. bottom = start;
  432. top = start + length - 1;
  433. real_top = top;
  434. while (top > bottom)
  435. {
  436. center = bottom + (top - bottom) / 2;
  437. gdb_assert (center < top);
  438. if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
  439. lookup_name.c_str ()) >= 0)
  440. {
  441. top = center;
  442. }
  443. else
  444. {
  445. bottom = center + 1;
  446. }
  447. }
  448. gdb_assert (top == bottom);
  449. /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
  450. search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
  451. while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
  452. lookup_name))
  453. top--;
  454. /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
  455. top++;
  456. while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
  457. lookup_name))
  458. {
  459. if (symbol_matches_domain ((*top)->ginfo.language (),
  460. (*top)->domain, domain))
  461. return *top;
  462. top++;
  463. }
  464. }
  465. /* Can't use a binary search or else we found during the binary search that
  466. we should also do a linear search. */
  467. if (do_linear_search)
  468. {
  469. for (psym = start; psym < start + length; psym++)
  470. {
  471. if (symbol_matches_domain ((*psym)->ginfo.language (),
  472. (*psym)->domain, domain)
  473. && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
  474. return *psym;
  475. }
  476. }
  477. return NULL;
  478. }
  479. /* Get the symbol table that corresponds to a partial_symtab.
  480. This is fast after the first time you do it.
  481. The result will be NULL if the primary symtab has no symbols,
  482. which can happen. Otherwise the result is the primary symtab
  483. that contains PST. */
  484. static struct compunit_symtab *
  485. psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
  486. {
  487. /* If it is a shared psymtab, find an unshared psymtab that includes
  488. it. Any such psymtab will do. */
  489. while (pst->user != NULL)
  490. pst = pst->user;
  491. /* If it's been looked up before, return it. */
  492. if (pst->get_compunit_symtab (objfile))
  493. return pst->get_compunit_symtab (objfile);
  494. /* If it has not yet been read in, read it. */
  495. if (!pst->readin_p (objfile))
  496. {
  497. scoped_restore decrementer = increment_reading_symtab ();
  498. if (info_verbose)
  499. {
  500. gdb_printf (_("Reading in symbols for %s...\n"),
  501. pst->filename);
  502. gdb_flush (gdb_stdout);
  503. }
  504. pst->read_symtab (objfile);
  505. }
  506. return pst->get_compunit_symtab (objfile);
  507. }
  508. /* Psymtab version of find_last_source_symtab. See its definition in
  509. the definition of quick_symbol_functions in symfile.h. */
  510. struct symtab *
  511. psymbol_functions::find_last_source_symtab (struct objfile *ofp)
  512. {
  513. struct partial_symtab *cs_pst = NULL;
  514. for (partial_symtab *ps : require_partial_symbols (ofp))
  515. {
  516. const char *name = ps->filename;
  517. int len = strlen (name);
  518. if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
  519. || strcmp (name, "<<C++-namespaces>>") == 0)))
  520. cs_pst = ps;
  521. }
  522. if (cs_pst)
  523. {
  524. if (cs_pst->readin_p (ofp))
  525. {
  526. internal_error (__FILE__, __LINE__,
  527. _("select_source_symtab: "
  528. "readin pst found and no symtabs."));
  529. }
  530. else
  531. {
  532. struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
  533. if (cust == NULL)
  534. return NULL;
  535. return cust->primary_filetab ();
  536. }
  537. }
  538. return NULL;
  539. }
  540. /* Psymtab version of forget_cached_source_info. See its definition in
  541. the definition of quick_symbol_functions in symfile.h. */
  542. void
  543. psymbol_functions::forget_cached_source_info (struct objfile *objfile)
  544. {
  545. for (partial_symtab *pst : require_partial_symbols (objfile))
  546. {
  547. if (pst->fullname != NULL)
  548. {
  549. xfree (pst->fullname);
  550. pst->fullname = NULL;
  551. }
  552. }
  553. }
  554. static void
  555. print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
  556. const std::vector<partial_symbol *> &symbols,
  557. const char *what, struct ui_file *outfile)
  558. {
  559. gdb_printf (outfile, " %s partial symbols:\n", what);
  560. for (partial_symbol *p : symbols)
  561. {
  562. QUIT;
  563. gdb_printf (outfile, " `%s'", p->ginfo.linkage_name ());
  564. if (p->ginfo.demangled_name () != NULL)
  565. {
  566. gdb_printf (outfile, " `%s'",
  567. p->ginfo.demangled_name ());
  568. }
  569. gdb_puts (", ", outfile);
  570. switch (p->domain)
  571. {
  572. case UNDEF_DOMAIN:
  573. gdb_puts ("undefined domain, ", outfile);
  574. break;
  575. case VAR_DOMAIN:
  576. /* This is the usual thing -- don't print it. */
  577. break;
  578. case STRUCT_DOMAIN:
  579. gdb_puts ("struct domain, ", outfile);
  580. break;
  581. case MODULE_DOMAIN:
  582. gdb_puts ("module domain, ", outfile);
  583. break;
  584. case LABEL_DOMAIN:
  585. gdb_puts ("label domain, ", outfile);
  586. break;
  587. case COMMON_BLOCK_DOMAIN:
  588. gdb_puts ("common block domain, ", outfile);
  589. break;
  590. default:
  591. gdb_puts ("<invalid domain>, ", outfile);
  592. break;
  593. }
  594. switch (p->aclass)
  595. {
  596. case LOC_UNDEF:
  597. gdb_puts ("undefined", outfile);
  598. break;
  599. case LOC_CONST:
  600. gdb_puts ("constant int", outfile);
  601. break;
  602. case LOC_STATIC:
  603. gdb_puts ("static", outfile);
  604. break;
  605. case LOC_REGISTER:
  606. gdb_puts ("register", outfile);
  607. break;
  608. case LOC_ARG:
  609. gdb_puts ("pass by value", outfile);
  610. break;
  611. case LOC_REF_ARG:
  612. gdb_puts ("pass by reference", outfile);
  613. break;
  614. case LOC_REGPARM_ADDR:
  615. gdb_puts ("register address parameter", outfile);
  616. break;
  617. case LOC_LOCAL:
  618. gdb_puts ("stack parameter", outfile);
  619. break;
  620. case LOC_TYPEDEF:
  621. gdb_puts ("type", outfile);
  622. break;
  623. case LOC_LABEL:
  624. gdb_puts ("label", outfile);
  625. break;
  626. case LOC_BLOCK:
  627. gdb_puts ("function", outfile);
  628. break;
  629. case LOC_CONST_BYTES:
  630. gdb_puts ("constant bytes", outfile);
  631. break;
  632. case LOC_UNRESOLVED:
  633. gdb_puts ("unresolved", outfile);
  634. break;
  635. case LOC_OPTIMIZED_OUT:
  636. gdb_puts ("optimized out", outfile);
  637. break;
  638. case LOC_COMPUTED:
  639. gdb_puts ("computed at runtime", outfile);
  640. break;
  641. default:
  642. gdb_puts ("<invalid location>", outfile);
  643. break;
  644. }
  645. gdb_puts (", ", outfile);
  646. gdb_puts (paddress (gdbarch, p->unrelocated_address ()), outfile);
  647. gdb_printf (outfile, "\n");
  648. }
  649. }
  650. static void
  651. dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
  652. struct ui_file *outfile)
  653. {
  654. struct gdbarch *gdbarch = objfile->arch ();
  655. int i;
  656. if (psymtab->anonymous)
  657. {
  658. gdb_printf (outfile, "\nAnonymous partial symtab (%s) ",
  659. psymtab->filename);
  660. }
  661. else
  662. {
  663. gdb_printf (outfile, "\nPartial symtab for source file %s ",
  664. psymtab->filename);
  665. }
  666. gdb_printf (outfile, "(object %s)\n\n",
  667. host_address_to_string (psymtab));
  668. gdb_printf (outfile, " Read from object file %s (%s)\n",
  669. objfile_name (objfile),
  670. host_address_to_string (objfile));
  671. if (psymtab->readin_p (objfile))
  672. gdb_printf
  673. (outfile,
  674. " Full symtab was read (at %s)\n",
  675. host_address_to_string (psymtab->get_compunit_symtab (objfile)));
  676. gdb_printf (outfile, " Symbols cover text addresses ");
  677. gdb_puts (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
  678. gdb_printf (outfile, "-");
  679. gdb_puts (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
  680. gdb_printf (outfile, "\n");
  681. gdb_printf (outfile, " Address map supported - %s.\n",
  682. psymtab->psymtabs_addrmap_supported ? "yes" : "no");
  683. gdb_printf (outfile, " Depends on %d other partial symtabs.\n",
  684. psymtab->number_of_dependencies);
  685. for (i = 0; i < psymtab->number_of_dependencies; i++)
  686. gdb_printf (outfile, " %d %s\n", i,
  687. host_address_to_string (psymtab->dependencies[i]));
  688. if (psymtab->user != NULL)
  689. gdb_printf (outfile, " Shared partial symtab with user %s\n",
  690. host_address_to_string (psymtab->user));
  691. if (!psymtab->global_psymbols.empty ())
  692. {
  693. print_partial_symbols
  694. (gdbarch, objfile, psymtab->global_psymbols,
  695. "Global", outfile);
  696. }
  697. if (!psymtab->static_psymbols.empty ())
  698. {
  699. print_partial_symbols
  700. (gdbarch, objfile, psymtab->static_psymbols,
  701. "Static", outfile);
  702. }
  703. gdb_printf (outfile, "\n");
  704. }
  705. /* Count the number of partial symbols in OBJFILE. */
  706. int
  707. psymbol_functions::count_psyms ()
  708. {
  709. int count = 0;
  710. for (partial_symtab *pst : m_partial_symtabs->range ())
  711. {
  712. count += pst->global_psymbols.size ();
  713. count += pst->static_psymbols.size ();
  714. }
  715. return count;
  716. }
  717. /* Psymtab version of print_stats. See its definition in
  718. the definition of quick_symbol_functions in symfile.h. */
  719. void
  720. psymbol_functions::print_stats (struct objfile *objfile, bool print_bcache)
  721. {
  722. int i;
  723. if (!print_bcache)
  724. {
  725. int n_psyms = count_psyms ();
  726. if (n_psyms > 0)
  727. gdb_printf (_(" Number of \"partial\" symbols read: %d\n"),
  728. n_psyms);
  729. i = 0;
  730. for (partial_symtab *ps : require_partial_symbols (objfile))
  731. {
  732. if (!ps->readin_p (objfile))
  733. i++;
  734. }
  735. gdb_printf (_(" Number of psym tables (not yet expanded): %d\n"),
  736. i);
  737. gdb_printf (_(" Total memory used for psymbol cache: %d\n"),
  738. m_partial_symtabs->psymbol_cache.memory_used ());
  739. }
  740. else
  741. {
  742. gdb_printf (_("Psymbol byte cache statistics:\n"));
  743. m_partial_symtabs->psymbol_cache.print_statistics
  744. ("partial symbol cache");
  745. }
  746. }
  747. /* Psymtab version of dump. See its definition in
  748. the definition of quick_symbol_functions in symfile.h. */
  749. void
  750. psymbol_functions::dump (struct objfile *objfile)
  751. {
  752. struct partial_symtab *psymtab;
  753. if (m_partial_symtabs->psymtabs)
  754. {
  755. gdb_printf ("Psymtabs:\n");
  756. for (psymtab = m_partial_symtabs->psymtabs;
  757. psymtab != NULL;
  758. psymtab = psymtab->next)
  759. gdb_printf ("%s at %s\n",
  760. psymtab->filename,
  761. host_address_to_string (psymtab));
  762. gdb_printf ("\n\n");
  763. }
  764. }
  765. /* Psymtab version of expand_all_symtabs. See its definition in
  766. the definition of quick_symbol_functions in symfile.h. */
  767. void
  768. psymbol_functions::expand_all_symtabs (struct objfile *objfile)
  769. {
  770. for (partial_symtab *psymtab : require_partial_symbols (objfile))
  771. psymtab_to_symtab (objfile, psymtab);
  772. }
  773. /* Psymtab version of map_symbol_filenames. See its definition in
  774. the definition of quick_symbol_functions in symfile.h. */
  775. void
  776. psymbol_functions::map_symbol_filenames
  777. (struct objfile *objfile,
  778. gdb::function_view<symbol_filename_ftype> fun,
  779. bool need_fullname)
  780. {
  781. for (partial_symtab *ps : require_partial_symbols (objfile))
  782. {
  783. const char *fullname;
  784. if (ps->readin_p (objfile))
  785. continue;
  786. /* We can skip shared psymtabs here, because any file name will be
  787. attached to the unshared psymtab. */
  788. if (ps->user != NULL)
  789. continue;
  790. /* Anonymous psymtabs don't have a file name. */
  791. if (ps->anonymous)
  792. continue;
  793. QUIT;
  794. if (need_fullname)
  795. fullname = psymtab_to_fullname (ps);
  796. else
  797. fullname = NULL;
  798. fun (ps->filename, fullname);
  799. }
  800. }
  801. /* Finds the fullname that a partial_symtab represents.
  802. If this functions finds the fullname, it will save it in ps->fullname
  803. and it will also return the value.
  804. If this function fails to find the file that this partial_symtab represents,
  805. NULL will be returned and ps->fullname will be set to NULL. */
  806. static const char *
  807. psymtab_to_fullname (struct partial_symtab *ps)
  808. {
  809. gdb_assert (!ps->anonymous);
  810. /* Use cached copy if we have it.
  811. We rely on forget_cached_source_info being called appropriately
  812. to handle cases like the file being moved. */
  813. if (ps->fullname == NULL)
  814. {
  815. gdb::unique_xmalloc_ptr<char> fullname
  816. = find_source_or_rewrite (ps->filename, ps->dirname);
  817. ps->fullname = fullname.release ();
  818. }
  819. return ps->fullname;
  820. }
  821. /* Psymtab version of expand_matching_symbols. See its definition in
  822. the definition of quick_symbol_functions in symfile.h. */
  823. void
  824. psymbol_functions::expand_matching_symbols
  825. (struct objfile *objfile,
  826. const lookup_name_info &name, domain_enum domain,
  827. int global,
  828. symbol_compare_ftype *ordered_compare)
  829. {
  830. for (partial_symtab *ps : require_partial_symbols (objfile))
  831. {
  832. QUIT;
  833. if (!ps->readin_p (objfile)
  834. && match_partial_symbol (objfile, ps, global, name, domain,
  835. ordered_compare))
  836. psymtab_to_symtab (objfile, ps);
  837. }
  838. }
  839. /* A helper for psym_expand_symtabs_matching that handles searching
  840. included psymtabs. This returns true if a symbol is found, and
  841. false otherwise. It also updates the 'searched_flag' on the
  842. various psymtabs that it searches. */
  843. static bool
  844. recursively_search_psymtabs
  845. (struct partial_symtab *ps,
  846. struct objfile *objfile,
  847. block_search_flags search_flags,
  848. domain_enum domain,
  849. enum search_domain search,
  850. const lookup_name_info &lookup_name,
  851. gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
  852. {
  853. int keep_going = 1;
  854. enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
  855. int i;
  856. if (ps->searched_flag != PST_NOT_SEARCHED)
  857. return ps->searched_flag == PST_SEARCHED_AND_FOUND;
  858. /* Recurse into shared psymtabs first, because they may have already
  859. been searched, and this could save some time. */
  860. for (i = 0; i < ps->number_of_dependencies; ++i)
  861. {
  862. int r;
  863. /* Skip non-shared dependencies, these are handled elsewhere. */
  864. if (ps->dependencies[i]->user == NULL)
  865. continue;
  866. r = recursively_search_psymtabs (ps->dependencies[i],
  867. objfile, search_flags, domain, search,
  868. lookup_name, sym_matcher);
  869. if (r != 0)
  870. {
  871. ps->searched_flag = PST_SEARCHED_AND_FOUND;
  872. return true;
  873. }
  874. }
  875. partial_symbol **gbound = (ps->global_psymbols.data ()
  876. + ps->global_psymbols.size ());
  877. partial_symbol **sbound = (ps->static_psymbols.data ()
  878. + ps->static_psymbols.size ());
  879. partial_symbol **bound = gbound;
  880. /* Go through all of the symbols stored in a partial
  881. symtab in one loop. */
  882. partial_symbol **psym = ps->global_psymbols.data ();
  883. if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
  884. {
  885. if (ps->static_psymbols.empty ())
  886. keep_going = 0;
  887. else
  888. {
  889. psym = ps->static_psymbols.data ();
  890. bound = sbound;
  891. }
  892. }
  893. while (keep_going)
  894. {
  895. if (psym >= bound)
  896. {
  897. if (bound == gbound && !ps->static_psymbols.empty ()
  898. && (search_flags & SEARCH_STATIC_BLOCK) != 0)
  899. {
  900. psym = ps->static_psymbols.data ();
  901. bound = sbound;
  902. }
  903. else
  904. keep_going = 0;
  905. continue;
  906. }
  907. else
  908. {
  909. QUIT;
  910. if ((domain == UNDEF_DOMAIN
  911. || symbol_matches_domain ((*psym)->ginfo.language (),
  912. (*psym)->domain, domain))
  913. && (search == ALL_DOMAIN
  914. || (search == MODULES_DOMAIN
  915. && (*psym)->domain == MODULE_DOMAIN)
  916. || (search == VARIABLES_DOMAIN
  917. && (*psym)->aclass != LOC_TYPEDEF
  918. && (*psym)->aclass != LOC_BLOCK)
  919. || (search == FUNCTIONS_DOMAIN
  920. && (*psym)->aclass == LOC_BLOCK)
  921. || (search == TYPES_DOMAIN
  922. && (*psym)->aclass == LOC_TYPEDEF))
  923. && psymbol_name_matches (*psym, lookup_name)
  924. && (sym_matcher == NULL
  925. || sym_matcher ((*psym)->ginfo.search_name ())))
  926. {
  927. /* Found a match, so notify our caller. */
  928. result = PST_SEARCHED_AND_FOUND;
  929. keep_going = 0;
  930. }
  931. }
  932. psym++;
  933. }
  934. ps->searched_flag = result;
  935. return result == PST_SEARCHED_AND_FOUND;
  936. }
  937. /* Psymtab version of expand_symtabs_matching. See its definition in
  938. the definition of quick_symbol_functions in symfile.h. */
  939. bool
  940. psymbol_functions::expand_symtabs_matching
  941. (struct objfile *objfile,
  942. gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
  943. const lookup_name_info *lookup_name,
  944. gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
  945. gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
  946. block_search_flags search_flags,
  947. domain_enum domain,
  948. enum search_domain search)
  949. {
  950. /* Clear the search flags. */
  951. for (partial_symtab *ps : require_partial_symbols (objfile))
  952. ps->searched_flag = PST_NOT_SEARCHED;
  953. gdb::optional<lookup_name_info> psym_lookup_name;
  954. if (lookup_name != nullptr)
  955. psym_lookup_name = lookup_name->make_ignore_params ();
  956. /* This invariant is documented in quick-functions.h. */
  957. gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
  958. for (partial_symtab *ps : m_partial_symtabs->range ())
  959. {
  960. QUIT;
  961. if (ps->readin_p (objfile))
  962. continue;
  963. if (file_matcher)
  964. {
  965. bool match;
  966. if (ps->anonymous)
  967. continue;
  968. match = file_matcher (ps->filename, false);
  969. if (!match)
  970. {
  971. /* Before we invoke realpath, which can get expensive when many
  972. files are involved, do a quick comparison of the basenames. */
  973. if (basenames_may_differ
  974. || file_matcher (lbasename (ps->filename), true))
  975. match = file_matcher (psymtab_to_fullname (ps), false);
  976. }
  977. if (!match)
  978. continue;
  979. }
  980. if (lookup_name == nullptr
  981. || recursively_search_psymtabs (ps, objfile, search_flags,
  982. domain, search,
  983. *psym_lookup_name,
  984. symbol_matcher))
  985. {
  986. compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
  987. if (cust != nullptr && expansion_notify != nullptr)
  988. if (!expansion_notify (cust))
  989. return false;
  990. }
  991. }
  992. return true;
  993. }
  994. /* Psymtab version of has_symbols. See its definition in
  995. the definition of quick_symbol_functions in symfile.h. */
  996. bool
  997. psymbol_functions::has_symbols (struct objfile *objfile)
  998. {
  999. return m_partial_symtabs->psymtabs != NULL;
  1000. }
  1001. /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
  1002. bool
  1003. psymbol_functions::has_unexpanded_symtabs (struct objfile *objfile)
  1004. {
  1005. for (partial_symtab *psymtab : require_partial_symbols (objfile))
  1006. {
  1007. /* Is this already expanded? */
  1008. if (psymtab->readin_p (objfile))
  1009. continue;
  1010. /* It has not yet been expanded. */
  1011. return true;
  1012. }
  1013. return false;
  1014. }
  1015. /* Helper function for psym_find_compunit_symtab_by_address that fills
  1016. in m_psymbol_map for a given range of psymbols. */
  1017. void
  1018. psymbol_functions::fill_psymbol_map
  1019. (struct objfile *objfile,
  1020. struct partial_symtab *psymtab,
  1021. std::set<CORE_ADDR> *seen_addrs,
  1022. const std::vector<partial_symbol *> &symbols)
  1023. {
  1024. for (partial_symbol *psym : symbols)
  1025. {
  1026. if (psym->aclass == LOC_STATIC)
  1027. {
  1028. CORE_ADDR addr = psym->address (objfile);
  1029. if (seen_addrs->find (addr) == seen_addrs->end ())
  1030. {
  1031. seen_addrs->insert (addr);
  1032. m_psymbol_map.emplace_back (addr, psymtab);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. /* See find_compunit_symtab_by_address in quick_symbol_functions, in
  1038. symfile.h. */
  1039. compunit_symtab *
  1040. psymbol_functions::find_compunit_symtab_by_address (struct objfile *objfile,
  1041. CORE_ADDR address)
  1042. {
  1043. if (m_psymbol_map.empty ())
  1044. {
  1045. std::set<CORE_ADDR> seen_addrs;
  1046. for (partial_symtab *pst : require_partial_symbols (objfile))
  1047. {
  1048. fill_psymbol_map (objfile, pst,
  1049. &seen_addrs,
  1050. pst->global_psymbols);
  1051. fill_psymbol_map (objfile, pst,
  1052. &seen_addrs,
  1053. pst->static_psymbols);
  1054. }
  1055. m_psymbol_map.shrink_to_fit ();
  1056. std::sort (m_psymbol_map.begin (), m_psymbol_map.end (),
  1057. [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
  1058. const std::pair<CORE_ADDR, partial_symtab *> &b)
  1059. {
  1060. return a.first < b.first;
  1061. });
  1062. }
  1063. auto iter = std::lower_bound
  1064. (m_psymbol_map.begin (), m_psymbol_map.end (), address,
  1065. [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
  1066. CORE_ADDR b)
  1067. {
  1068. return a.first < b;
  1069. });
  1070. if (iter == m_psymbol_map.end () || iter->first != address)
  1071. return NULL;
  1072. return psymtab_to_symtab (objfile, iter->second);
  1073. }
  1074. /* Partially fill a partial symtab. It will be completely filled at
  1075. the end of the symbol list. */
  1076. partial_symtab::partial_symtab (const char *filename,
  1077. psymtab_storage *partial_symtabs,
  1078. objfile_per_bfd_storage *objfile_per_bfd,
  1079. CORE_ADDR textlow)
  1080. : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
  1081. {
  1082. set_text_low (textlow);
  1083. set_text_high (raw_text_low ()); /* default */
  1084. }
  1085. /* Perform "finishing up" operations of a partial symtab. */
  1086. void
  1087. partial_symtab::end ()
  1088. {
  1089. global_psymbols.shrink_to_fit ();
  1090. static_psymbols.shrink_to_fit ();
  1091. /* Sort the global list; don't sort the static list. */
  1092. std::sort (global_psymbols.begin (),
  1093. global_psymbols.end (),
  1094. [] (partial_symbol *s1, partial_symbol *s2)
  1095. {
  1096. return strcmp_iw_ordered (s1->ginfo.search_name (),
  1097. s2->ginfo.search_name ()) < 0;
  1098. });
  1099. }
  1100. /* See psymtab.h. */
  1101. unsigned long
  1102. psymbol_bcache::hash (const void *addr, int length)
  1103. {
  1104. unsigned long h = 0;
  1105. struct partial_symbol *psymbol = (struct partial_symbol *) addr;
  1106. unsigned int lang = psymbol->ginfo.language ();
  1107. unsigned int domain = psymbol->domain;
  1108. unsigned int theclass = psymbol->aclass;
  1109. h = fast_hash (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
  1110. h = fast_hash (&lang, sizeof (unsigned int), h);
  1111. h = fast_hash (&domain, sizeof (unsigned int), h);
  1112. h = fast_hash (&theclass, sizeof (unsigned int), h);
  1113. /* Note that psymbol names are interned via compute_and_set_names, so
  1114. there's no need to hash the contents of the name here. */
  1115. h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
  1116. return h;
  1117. }
  1118. /* See psymtab.h. */
  1119. int
  1120. psymbol_bcache::compare (const void *addr1, const void *addr2, int length)
  1121. {
  1122. struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
  1123. struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
  1124. return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
  1125. sizeof (sym1->ginfo.value)) == 0
  1126. && sym1->ginfo.language () == sym2->ginfo.language ()
  1127. && sym1->domain == sym2->domain
  1128. && sym1->aclass == sym2->aclass
  1129. /* Note that psymbol names are interned via
  1130. compute_and_set_names, so there's no need to compare the
  1131. contents of the name here. */
  1132. && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
  1133. }
  1134. /* See psympriv.h. */
  1135. void
  1136. partial_symtab::add_psymbol (const partial_symbol &psymbol,
  1137. psymbol_placement where,
  1138. psymtab_storage *partial_symtabs,
  1139. struct objfile *objfile)
  1140. {
  1141. bool added;
  1142. /* Stash the partial symbol away in the cache. */
  1143. partial_symbol *psym
  1144. = ((struct partial_symbol *)
  1145. partial_symtabs->psymbol_cache.insert
  1146. (&psymbol, sizeof (struct partial_symbol), &added));
  1147. /* Do not duplicate global partial symbols. */
  1148. if (where == psymbol_placement::GLOBAL && !added)
  1149. return;
  1150. /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
  1151. std::vector<partial_symbol *> &list
  1152. = (where == psymbol_placement::STATIC
  1153. ? static_psymbols
  1154. : global_psymbols);
  1155. list.push_back (psym);
  1156. }
  1157. /* See psympriv.h. */
  1158. void
  1159. partial_symtab::add_psymbol (gdb::string_view name, bool copy_name,
  1160. domain_enum domain,
  1161. enum address_class theclass,
  1162. short section,
  1163. psymbol_placement where,
  1164. CORE_ADDR coreaddr,
  1165. enum language language,
  1166. psymtab_storage *partial_symtabs,
  1167. struct objfile *objfile)
  1168. {
  1169. struct partial_symbol psymbol;
  1170. memset (&psymbol, 0, sizeof (psymbol));
  1171. psymbol.set_unrelocated_address (coreaddr);
  1172. psymbol.ginfo.set_section_index (section);
  1173. psymbol.domain = domain;
  1174. psymbol.aclass = theclass;
  1175. psymbol.ginfo.set_language (language, partial_symtabs->obstack ());
  1176. psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
  1177. add_psymbol (psymbol, where, partial_symtabs, objfile);
  1178. }
  1179. /* See psympriv.h. */
  1180. partial_symtab::partial_symtab (const char *filename_,
  1181. psymtab_storage *partial_symtabs,
  1182. objfile_per_bfd_storage *objfile_per_bfd)
  1183. : searched_flag (PST_NOT_SEARCHED),
  1184. text_low_valid (0),
  1185. text_high_valid (0)
  1186. {
  1187. partial_symtabs->install_psymtab (this);
  1188. filename = objfile_per_bfd->intern (filename_);
  1189. if (symtab_create_debug)
  1190. {
  1191. /* Be a bit clever with debugging messages, and don't print objfile
  1192. every time, only when it changes. */
  1193. static std::string last_bfd_name;
  1194. const char *this_bfd_name
  1195. = bfd_get_filename (objfile_per_bfd->get_bfd ());
  1196. if (last_bfd_name.empty () || last_bfd_name != this_bfd_name)
  1197. {
  1198. last_bfd_name = this_bfd_name;
  1199. gdb_printf (gdb_stdlog,
  1200. "Creating one or more psymtabs for %s ...\n",
  1201. this_bfd_name);
  1202. }
  1203. gdb_printf (gdb_stdlog,
  1204. "Created psymtab %s for module %s.\n",
  1205. host_address_to_string (this), filename);
  1206. }
  1207. }
  1208. /* See psympriv.h. */
  1209. void
  1210. partial_symtab::expand_dependencies (struct objfile *objfile)
  1211. {
  1212. for (int i = 0; i < number_of_dependencies; ++i)
  1213. {
  1214. if (!dependencies[i]->readin_p (objfile)
  1215. && dependencies[i]->user == NULL)
  1216. {
  1217. /* Inform about additional files to be read in. */
  1218. if (info_verbose)
  1219. {
  1220. gdb_puts (" ");
  1221. gdb_stdout->wrap_here (0);
  1222. gdb_puts ("and ");
  1223. gdb_stdout->wrap_here (0);
  1224. gdb_printf ("%s...", dependencies[i]->filename);
  1225. gdb_flush (gdb_stdout);
  1226. }
  1227. dependencies[i]->expand_psymtab (objfile);
  1228. }
  1229. }
  1230. }
  1231. void
  1232. psymtab_storage::discard_psymtab (struct partial_symtab *pst)
  1233. {
  1234. struct partial_symtab **prev_pst;
  1235. /* From dbxread.c:
  1236. Empty psymtabs happen as a result of header files which don't
  1237. have any symbols in them. There can be a lot of them. But this
  1238. check is wrong, in that a psymtab with N_SLINE entries but
  1239. nothing else is not empty, but we don't realize that. Fixing
  1240. that without slowing things down might be tricky. */
  1241. /* First, snip it out of the psymtab chain. */
  1242. prev_pst = &psymtabs;
  1243. while ((*prev_pst) != pst)
  1244. prev_pst = &((*prev_pst)->next);
  1245. (*prev_pst) = pst->next;
  1246. delete pst;
  1247. }
  1248. /* Helper function for maintenance_print_psymbols to print the addrmap
  1249. of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
  1250. static void
  1251. dump_psymtab_addrmap (struct objfile *objfile,
  1252. psymtab_storage *partial_symtabs,
  1253. struct partial_symtab *psymtab,
  1254. struct ui_file *outfile)
  1255. {
  1256. if ((psymtab == NULL
  1257. || psymtab->psymtabs_addrmap_supported)
  1258. && partial_symtabs->psymtabs_addrmap != NULL)
  1259. {
  1260. if (psymtab == nullptr)
  1261. gdb_printf (outfile, _("Entire address map:\n"));
  1262. else
  1263. gdb_printf (outfile, _("Address map:\n"));
  1264. addrmap_dump (partial_symtabs->psymtabs_addrmap, outfile, psymtab);
  1265. }
  1266. }
  1267. static void
  1268. maintenance_print_psymbols (const char *args, int from_tty)
  1269. {
  1270. struct ui_file *outfile = gdb_stdout;
  1271. char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
  1272. int i, outfile_idx, found;
  1273. CORE_ADDR pc = 0;
  1274. struct obj_section *section = NULL;
  1275. dont_repeat ();
  1276. gdb_argv argv (args);
  1277. for (i = 0; argv != NULL && argv[i] != NULL; ++i)
  1278. {
  1279. if (strcmp (argv[i], "-pc") == 0)
  1280. {
  1281. if (argv[i + 1] == NULL)
  1282. error (_("Missing pc value"));
  1283. address_arg = argv[++i];
  1284. }
  1285. else if (strcmp (argv[i], "-source") == 0)
  1286. {
  1287. if (argv[i + 1] == NULL)
  1288. error (_("Missing source file"));
  1289. source_arg = argv[++i];
  1290. }
  1291. else if (strcmp (argv[i], "-objfile") == 0)
  1292. {
  1293. if (argv[i + 1] == NULL)
  1294. error (_("Missing objfile name"));
  1295. objfile_arg = argv[++i];
  1296. }
  1297. else if (strcmp (argv[i], "--") == 0)
  1298. {
  1299. /* End of options. */
  1300. ++i;
  1301. break;
  1302. }
  1303. else if (argv[i][0] == '-')
  1304. {
  1305. /* Future proofing: Don't allow OUTFILE to begin with "-". */
  1306. error (_("Unknown option: %s"), argv[i]);
  1307. }
  1308. else
  1309. break;
  1310. }
  1311. outfile_idx = i;
  1312. if (address_arg != NULL && source_arg != NULL)
  1313. error (_("Must specify at most one of -pc and -source"));
  1314. stdio_file arg_outfile;
  1315. if (argv != NULL && argv[outfile_idx] != NULL)
  1316. {
  1317. if (argv[outfile_idx + 1] != NULL)
  1318. error (_("Junk at end of command"));
  1319. gdb::unique_xmalloc_ptr<char> outfile_name
  1320. (tilde_expand (argv[outfile_idx]));
  1321. if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
  1322. perror_with_name (outfile_name.get ());
  1323. outfile = &arg_outfile;
  1324. }
  1325. if (address_arg != NULL)
  1326. {
  1327. pc = parse_and_eval_address (address_arg);
  1328. /* If we fail to find a section, that's ok, try the lookup anyway. */
  1329. section = find_pc_section (pc);
  1330. }
  1331. found = 0;
  1332. for (objfile *objfile : current_program_space->objfiles ())
  1333. {
  1334. int printed_objfile_header = 0;
  1335. int print_for_objfile = 1;
  1336. QUIT;
  1337. if (objfile_arg != NULL)
  1338. print_for_objfile
  1339. = compare_filenames_for_search (objfile_name (objfile),
  1340. objfile_arg);
  1341. if (!print_for_objfile)
  1342. continue;
  1343. for (const auto &iter : objfile->qf)
  1344. {
  1345. psymbol_functions *psf
  1346. = dynamic_cast<psymbol_functions *> (iter.get ());
  1347. if (psf == nullptr)
  1348. continue;
  1349. psymtab_storage *partial_symtabs
  1350. = psf->get_partial_symtabs ().get ();
  1351. if (address_arg != NULL)
  1352. {
  1353. struct bound_minimal_symbol msymbol;
  1354. /* We don't assume each pc has a unique objfile (this is for
  1355. debugging). */
  1356. struct partial_symtab *ps
  1357. = psf->find_pc_sect_psymtab (objfile, pc, section, msymbol);
  1358. if (ps != NULL)
  1359. {
  1360. if (!printed_objfile_header)
  1361. {
  1362. outfile->printf ("\nPartial symtabs for objfile %s\n",
  1363. objfile_name (objfile));
  1364. printed_objfile_header = 1;
  1365. }
  1366. dump_psymtab (objfile, ps, outfile);
  1367. dump_psymtab_addrmap (objfile, partial_symtabs, ps, outfile);
  1368. found = 1;
  1369. }
  1370. }
  1371. else
  1372. {
  1373. for (partial_symtab *ps : psf->require_partial_symbols (objfile))
  1374. {
  1375. int print_for_source = 0;
  1376. QUIT;
  1377. if (source_arg != NULL)
  1378. {
  1379. print_for_source
  1380. = compare_filenames_for_search (ps->filename, source_arg);
  1381. found = 1;
  1382. }
  1383. if (source_arg == NULL
  1384. || print_for_source)
  1385. {
  1386. if (!printed_objfile_header)
  1387. {
  1388. outfile->printf ("\nPartial symtabs for objfile %s\n",
  1389. objfile_name (objfile));
  1390. printed_objfile_header = 1;
  1391. }
  1392. dump_psymtab (objfile, ps, outfile);
  1393. dump_psymtab_addrmap (objfile, partial_symtabs, ps,
  1394. outfile);
  1395. }
  1396. }
  1397. }
  1398. /* If we're printing all the objfile's symbols dump the full addrmap. */
  1399. if (address_arg == NULL
  1400. && source_arg == NULL
  1401. && partial_symtabs->psymtabs_addrmap != NULL)
  1402. {
  1403. outfile->puts ("\n");
  1404. dump_psymtab_addrmap (objfile, partial_symtabs, NULL, outfile);
  1405. }
  1406. }
  1407. }
  1408. if (!found)
  1409. {
  1410. if (address_arg != NULL)
  1411. error (_("No partial symtab for address: %s"), address_arg);
  1412. if (source_arg != NULL)
  1413. error (_("No partial symtab for source file: %s"), source_arg);
  1414. }
  1415. }
  1416. /* List all the partial symbol tables whose names match REGEXP (optional). */
  1417. static void
  1418. maintenance_info_psymtabs (const char *regexp, int from_tty)
  1419. {
  1420. if (regexp)
  1421. re_comp (regexp);
  1422. for (struct program_space *pspace : program_spaces)
  1423. for (objfile *objfile : pspace->objfiles ())
  1424. {
  1425. struct gdbarch *gdbarch = objfile->arch ();
  1426. /* We don't want to print anything for this objfile until we
  1427. actually find a symtab whose name matches. */
  1428. int printed_objfile_start = 0;
  1429. for (const auto &iter : objfile->qf)
  1430. {
  1431. psymbol_functions *psf
  1432. = dynamic_cast<psymbol_functions *> (iter.get ());
  1433. if (psf == nullptr)
  1434. continue;
  1435. for (partial_symtab *psymtab : psf->require_partial_symbols (objfile))
  1436. {
  1437. QUIT;
  1438. if (! regexp
  1439. || re_exec (psymtab->filename))
  1440. {
  1441. if (! printed_objfile_start)
  1442. {
  1443. gdb_printf ("{ objfile %s ", objfile_name (objfile));
  1444. gdb_stdout->wrap_here (2);
  1445. gdb_printf ("((struct objfile *) %s)\n",
  1446. host_address_to_string (objfile));
  1447. printed_objfile_start = 1;
  1448. }
  1449. gdb_printf (" { psymtab %s ", psymtab->filename);
  1450. gdb_stdout->wrap_here (4);
  1451. gdb_printf ("((struct partial_symtab *) %s)\n",
  1452. host_address_to_string (psymtab));
  1453. gdb_printf (" readin %s\n",
  1454. psymtab->readin_p (objfile) ? "yes" : "no");
  1455. gdb_printf (" fullname %s\n",
  1456. psymtab->fullname
  1457. ? psymtab->fullname : "(null)");
  1458. gdb_printf (" text addresses ");
  1459. gdb_puts (paddress (gdbarch,
  1460. psymtab->text_low (objfile)));
  1461. gdb_printf (" -- ");
  1462. gdb_puts (paddress (gdbarch,
  1463. psymtab->text_high (objfile)));
  1464. gdb_printf ("\n");
  1465. gdb_printf (" psymtabs_addrmap_supported %s\n",
  1466. (psymtab->psymtabs_addrmap_supported
  1467. ? "yes" : "no"));
  1468. gdb_printf (" globals ");
  1469. if (!psymtab->global_psymbols.empty ())
  1470. gdb_printf
  1471. ("(* (struct partial_symbol **) %s @ %d)\n",
  1472. host_address_to_string (psymtab->global_psymbols.data ()),
  1473. (int) psymtab->global_psymbols.size ());
  1474. else
  1475. gdb_printf ("(none)\n");
  1476. gdb_printf (" statics ");
  1477. if (!psymtab->static_psymbols.empty ())
  1478. gdb_printf
  1479. ("(* (struct partial_symbol **) %s @ %d)\n",
  1480. host_address_to_string (psymtab->static_psymbols.data ()),
  1481. (int) psymtab->static_psymbols.size ());
  1482. else
  1483. gdb_printf ("(none)\n");
  1484. if (psymtab->user)
  1485. gdb_printf (" user %s "
  1486. "((struct partial_symtab *) %s)\n",
  1487. psymtab->user->filename,
  1488. host_address_to_string (psymtab->user));
  1489. gdb_printf (" dependencies ");
  1490. if (psymtab->number_of_dependencies)
  1491. {
  1492. int i;
  1493. gdb_printf ("{\n");
  1494. for (i = 0; i < psymtab->number_of_dependencies; i++)
  1495. {
  1496. struct partial_symtab *dep = psymtab->dependencies[i];
  1497. /* Note the string concatenation there --- no
  1498. comma. */
  1499. gdb_printf (" psymtab %s "
  1500. "((struct partial_symtab *) %s)\n",
  1501. dep->filename,
  1502. host_address_to_string (dep));
  1503. }
  1504. gdb_printf (" }\n");
  1505. }
  1506. else
  1507. gdb_printf ("(none)\n");
  1508. gdb_printf (" }\n");
  1509. }
  1510. }
  1511. }
  1512. if (printed_objfile_start)
  1513. gdb_printf ("}\n");
  1514. }
  1515. }
  1516. /* Check consistency of currently expanded psymtabs vs symtabs. */
  1517. static void
  1518. maintenance_check_psymtabs (const char *ignore, int from_tty)
  1519. {
  1520. struct symbol *sym;
  1521. struct compunit_symtab *cust = NULL;
  1522. const struct blockvector *bv;
  1523. const struct block *b;
  1524. for (objfile *objfile : current_program_space->objfiles ())
  1525. {
  1526. for (const auto &iter : objfile->qf)
  1527. {
  1528. psymbol_functions *psf
  1529. = dynamic_cast<psymbol_functions *> (iter.get ());
  1530. if (psf == nullptr)
  1531. continue;
  1532. for (partial_symtab *ps : psf->require_partial_symbols (objfile))
  1533. {
  1534. struct gdbarch *gdbarch = objfile->arch ();
  1535. /* We don't call psymtab_to_symtab here because that may cause symtab
  1536. expansion. When debugging a problem it helps if checkers leave
  1537. things unchanged. */
  1538. cust = ps->get_compunit_symtab (objfile);
  1539. /* First do some checks that don't require the associated symtab. */
  1540. if (ps->text_high (objfile) < ps->text_low (objfile))
  1541. {
  1542. gdb_printf ("Psymtab ");
  1543. gdb_puts (ps->filename);
  1544. gdb_printf (" covers bad range ");
  1545. gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
  1546. gdb_printf (" - ");
  1547. gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
  1548. gdb_printf ("\n");
  1549. continue;
  1550. }
  1551. /* Now do checks requiring the associated symtab. */
  1552. if (cust == NULL)
  1553. continue;
  1554. bv = cust->blockvector ();
  1555. b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  1556. for (partial_symbol *psym : ps->static_psymbols)
  1557. {
  1558. /* Skip symbols for inlined functions without address. These may
  1559. or may not have a match in the full symtab. */
  1560. if (psym->aclass == LOC_BLOCK
  1561. && psym->ginfo.value.address == 0)
  1562. continue;
  1563. sym = block_lookup_symbol (b, psym->ginfo.search_name (),
  1564. symbol_name_match_type::SEARCH_NAME,
  1565. psym->domain);
  1566. if (!sym)
  1567. {
  1568. gdb_printf ("Static symbol `");
  1569. gdb_puts (psym->ginfo.linkage_name ());
  1570. gdb_printf ("' only found in ");
  1571. gdb_puts (ps->filename);
  1572. gdb_printf (" psymtab\n");
  1573. }
  1574. }
  1575. b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  1576. for (partial_symbol *psym : ps->global_psymbols)
  1577. {
  1578. sym = block_lookup_symbol (b, psym->ginfo.search_name (),
  1579. symbol_name_match_type::SEARCH_NAME,
  1580. psym->domain);
  1581. if (!sym)
  1582. {
  1583. gdb_printf ("Global symbol `");
  1584. gdb_puts (psym->ginfo.linkage_name ());
  1585. gdb_printf ("' only found in ");
  1586. gdb_puts (ps->filename);
  1587. gdb_printf (" psymtab\n");
  1588. }
  1589. }
  1590. if (ps->raw_text_high () != 0
  1591. && (ps->text_low (objfile) < BLOCK_START (b)
  1592. || ps->text_high (objfile) > BLOCK_END (b)))
  1593. {
  1594. gdb_printf ("Psymtab ");
  1595. gdb_puts (ps->filename);
  1596. gdb_printf (" covers ");
  1597. gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
  1598. gdb_printf (" - ");
  1599. gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
  1600. gdb_printf (" but symtab covers only ");
  1601. gdb_puts (paddress (gdbarch, BLOCK_START (b)));
  1602. gdb_printf (" - ");
  1603. gdb_puts (paddress (gdbarch, BLOCK_END (b)));
  1604. gdb_printf ("\n");
  1605. }
  1606. }
  1607. }
  1608. }
  1609. }
  1610. void _initialize_psymtab ();
  1611. void
  1612. _initialize_psymtab ()
  1613. {
  1614. add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
  1615. Print dump of current partial symbol definitions.\n\
  1616. Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
  1617. mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
  1618. Entries in the partial symbol table are dumped to file OUTFILE,\n\
  1619. or the terminal if OUTFILE is unspecified.\n\
  1620. If ADDRESS is provided, dump only the file for that address.\n\
  1621. If SOURCE is provided, dump only that file's symbols.\n\
  1622. If OBJFILE is provided, dump only that file's minimal symbols."),
  1623. &maintenanceprintlist);
  1624. add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
  1625. List the partial symbol tables for all object files.\n\
  1626. This does not include information about individual partial symbols,\n\
  1627. just the symbol table structures themselves."),
  1628. &maintenanceinfolist);
  1629. add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
  1630. _("\
  1631. Check consistency of currently expanded psymtabs versus symtabs."),
  1632. &maintenancelist);
  1633. }