sym_ids.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* sym_ids.c
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  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, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "gprof.h"
  17. #include "libiberty.h"
  18. #include "safe-ctype.h"
  19. #include "search_list.h"
  20. #include "source.h"
  21. #include "symtab.h"
  22. #include "cg_arcs.h"
  23. #include "sym_ids.h"
  24. #include "corefile.h"
  25. struct match
  26. {
  27. int prev_index; /* Index of prev match. */
  28. Sym *prev_match; /* Previous match. */
  29. Sym *first_match; /* Chain of all matches. */
  30. Sym sym;
  31. };
  32. struct sym_id
  33. {
  34. struct sym_id *next;
  35. char *spec; /* Parsing modifies this. */
  36. Table_Id which_table;
  37. bool has_right;
  38. struct match left, right;
  39. };
  40. static struct sym_id *id_list;
  41. static void parse_spec
  42. (char *, Sym *);
  43. static void parse_id
  44. (struct sym_id *);
  45. static bool match
  46. (Sym *, Sym *);
  47. static void extend_match
  48. (struct match *, Sym *, Sym_Table *, bool);
  49. Sym_Table syms[NUM_TABLES];
  50. #ifdef DEBUG
  51. static const char *table_name[] =
  52. {
  53. "INCL_GRAPH", "EXCL_GRAPH",
  54. "INCL_ARCS", "EXCL_ARCS",
  55. "INCL_FLAT", "EXCL_FLAT",
  56. "INCL_TIME", "EXCL_TIME",
  57. "INCL_ANNO", "EXCL_ANNO",
  58. "INCL_EXEC", "EXCL_EXEC"
  59. };
  60. #endif /* DEBUG */
  61. /* This is the table in which we keep all the syms that match
  62. the right half of an arc id. It is NOT sorted according
  63. to the addresses, because it is accessed only through
  64. the left half's CHILDREN pointers (so it's crucial not
  65. to reorder this table once pointers into it exist). */
  66. static Sym_Table right_ids;
  67. static Source_File non_existent_file =
  68. {
  69. 0, "<non-existent-file>", 0, 0, 0, NULL
  70. };
  71. void
  72. sym_id_add (const char *spec, Table_Id which_table)
  73. {
  74. struct sym_id *id;
  75. int len = strlen (spec);
  76. id = (struct sym_id *) xmalloc (sizeof (*id) + len + 1);
  77. memset (id, 0, sizeof (*id));
  78. id->spec = (char *) id + sizeof (*id);
  79. strcpy (id->spec, spec);
  80. id->which_table = which_table;
  81. id->next = id_list;
  82. id_list = id;
  83. }
  84. /* A spec has the syntax FILENAME:(FUNCNAME|LINENUM). As a convenience
  85. to the user, a spec without a colon is interpreted as:
  86. (i) a FILENAME if it contains a dot
  87. (ii) a FUNCNAME if it starts with a non-digit character
  88. (iii) a LINENUM if it starts with a digit
  89. A FUNCNAME containing a dot can be specified by :FUNCNAME, a
  90. FILENAME not containing a dot can be specified by FILENAME. */
  91. static void
  92. parse_spec (char *spec, Sym *sym)
  93. {
  94. char *colon;
  95. sym_init (sym);
  96. colon = strrchr (spec, ':');
  97. if (colon)
  98. {
  99. *colon = '\0';
  100. if (colon > spec)
  101. {
  102. sym->file = source_file_lookup_name (spec);
  103. if (!sym->file)
  104. sym->file = &non_existent_file;
  105. }
  106. spec = colon + 1;
  107. if (strlen (spec))
  108. {
  109. if (ISDIGIT (spec[0]))
  110. sym->line_num = atoi (spec);
  111. else
  112. sym->name = spec;
  113. }
  114. }
  115. else if (strlen (spec))
  116. {
  117. /* No colon: spec is a filename if it contains a dot. */
  118. if (strchr (spec, '.'))
  119. {
  120. sym->file = source_file_lookup_name (spec);
  121. if (!sym->file)
  122. sym->file = &non_existent_file;
  123. }
  124. else if (ISDIGIT (*spec))
  125. {
  126. sym->line_num = atoi (spec);
  127. }
  128. else if (strlen (spec))
  129. {
  130. sym->name = spec;
  131. }
  132. }
  133. }
  134. /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
  135. by parse_spec(). */
  136. static void
  137. parse_id (struct sym_id *id)
  138. {
  139. char *slash;
  140. DBG (IDDEBUG, printf ("[parse_id] %s -> ", id->spec));
  141. slash = strchr (id->spec, '/');
  142. if (slash)
  143. {
  144. parse_spec (slash + 1, &id->right.sym);
  145. *slash = '\0';
  146. id->has_right = true;
  147. }
  148. parse_spec (id->spec, &id->left.sym);
  149. #ifdef DEBUG
  150. if (debug_level & IDDEBUG)
  151. {
  152. printf ("%s:", id->left.sym.file ? id->left.sym.file->name : "*");
  153. if (id->left.sym.name)
  154. printf ("%s", id->left.sym.name);
  155. else if (id->left.sym.line_num)
  156. printf ("%d", id->left.sym.line_num);
  157. else
  158. printf ("*");
  159. if (id->has_right)
  160. {
  161. printf ("/%s:",
  162. id->right.sym.file ? id->right.sym.file->name : "*");
  163. if (id->right.sym.name)
  164. printf ("%s", id->right.sym.name);
  165. else if (id->right.sym.line_num)
  166. printf ("%d", id->right.sym.line_num);
  167. else
  168. printf ("*");
  169. }
  170. printf ("\n");
  171. }
  172. #endif
  173. }
  174. /* Return TRUE iff PATTERN matches SYM. */
  175. static bool
  176. match (Sym *pattern, Sym *sym)
  177. {
  178. if (pattern->file && pattern->file != sym->file)
  179. return false;
  180. if (pattern->line_num && pattern->line_num != sym->line_num)
  181. return false;
  182. if (pattern->name)
  183. {
  184. const char *sym_name = sym->name;
  185. if (*sym_name && bfd_get_symbol_leading_char (core_bfd) == *sym_name)
  186. sym_name++;
  187. if (strcmp (pattern->name, sym_name) != 0)
  188. return false;
  189. }
  190. return true;
  191. }
  192. static void
  193. extend_match (struct match *m, Sym *sym, Sym_Table *tab, bool second_pass)
  194. {
  195. if (m->prev_match != sym - 1)
  196. {
  197. /* Discontinuity: add new match to table. */
  198. if (second_pass)
  199. {
  200. tab->base[tab->len] = *sym;
  201. m->prev_index = tab->len;
  202. /* Link match into match's chain. */
  203. tab->base[tab->len].next = m->first_match;
  204. m->first_match = &tab->base[tab->len];
  205. }
  206. ++tab->len;
  207. }
  208. /* Extend match to include this symbol. */
  209. if (second_pass)
  210. tab->base[m->prev_index].end_addr = sym->end_addr;
  211. m->prev_match = sym;
  212. }
  213. /* Go through sym_id list produced by option processing and fill
  214. in the various symbol tables indicating what symbols should
  215. be displayed or suppressed for the various kinds of outputs.
  216. This can potentially produce huge tables and in particulars
  217. tons of arcs, but this happens only if the user makes silly
  218. requests---you get what you ask for! */
  219. void
  220. sym_id_parse (void)
  221. {
  222. Sym *sym, *left, *right;
  223. struct sym_id *id;
  224. Sym_Table *tab;
  225. /* Convert symbol ids into Syms, so we can deal with them more easily. */
  226. for (id = id_list; id; id = id->next)
  227. parse_id (id);
  228. /* First determine size of each table. */
  229. for (sym = symtab.base; sym < symtab.limit; ++sym)
  230. {
  231. for (id = id_list; id; id = id->next)
  232. {
  233. if (match (&id->left.sym, sym))
  234. extend_match (&id->left, sym, &syms[id->which_table], false);
  235. if (id->has_right && match (&id->right.sym, sym))
  236. extend_match (&id->right, sym, &right_ids, false);
  237. }
  238. }
  239. /* Create tables of appropriate size and reset lengths. */
  240. for (tab = syms; tab < &syms[NUM_TABLES]; ++tab)
  241. {
  242. if (tab->len)
  243. {
  244. tab->base = (Sym *) xmalloc (tab->len * sizeof (Sym));
  245. tab->limit = tab->base + tab->len;
  246. tab->len = 0;
  247. }
  248. }
  249. if (right_ids.len)
  250. {
  251. right_ids.base = (Sym *) xmalloc (right_ids.len * sizeof (Sym));
  252. right_ids.limit = right_ids.base + right_ids.len;
  253. right_ids.len = 0;
  254. }
  255. /* Make a second pass through symtab, creating syms as necessary. */
  256. for (sym = symtab.base; sym < symtab.limit; ++sym)
  257. {
  258. for (id = id_list; id; id = id->next)
  259. {
  260. if (match (&id->left.sym, sym))
  261. extend_match (&id->left, sym, &syms[id->which_table], true);
  262. if (id->has_right && match (&id->right.sym, sym))
  263. extend_match (&id->right, sym, &right_ids, true);
  264. }
  265. }
  266. /* Go through ids creating arcs as needed. */
  267. for (id = id_list; id; id = id->next)
  268. {
  269. if (id->has_right)
  270. {
  271. for (left = id->left.first_match; left; left = left->next)
  272. {
  273. for (right = id->right.first_match; right; right = right->next)
  274. {
  275. DBG (IDDEBUG,
  276. printf (
  277. "[sym_id_parse]: arc %s:%s(%lx-%lx) -> %s:%s(%lx-%lx) to %s\n",
  278. left->file ? left->file->name : "*",
  279. left->name ? left->name : "*",
  280. (unsigned long) left->addr,
  281. (unsigned long) left->end_addr,
  282. right->file ? right->file->name : "*",
  283. right->name ? right->name : "*",
  284. (unsigned long) right->addr,
  285. (unsigned long) right->end_addr,
  286. table_name[id->which_table]));
  287. arc_add (left, right, (unsigned long) 0);
  288. }
  289. }
  290. }
  291. }
  292. /* Finally, we can sort the tables and we're done. */
  293. for (tab = &syms[0]; tab < &syms[NUM_TABLES]; ++tab)
  294. {
  295. DBG (IDDEBUG, printf ("[sym_id_parse] syms[%s]:\n",
  296. table_name[tab - &syms[0]]));
  297. symtab_finalize (tab);
  298. }
  299. }
  300. /* Symbol tables storing the FROM symbols of arcs do not necessarily
  301. have distinct address ranges. For example, somebody might request
  302. -k /_mcount to suppress any arcs into _mcount, while at the same
  303. time requesting -k a/b. Fortunately, those symbol tables don't get
  304. very big (the user has to type them!), so a linear search is probably
  305. tolerable. */
  306. bool
  307. sym_id_arc_is_present (Sym_Table *sym_tab, Sym *from, Sym *to)
  308. {
  309. Sym *sym;
  310. for (sym = sym_tab->base; sym < sym_tab->limit; ++sym)
  311. {
  312. if (from->addr >= sym->addr && from->addr <= sym->end_addr
  313. && arc_lookup (sym, to))
  314. return true;
  315. }
  316. return false;
  317. }