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