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