3 Copyright 1999, 2000, 2001, 2002, 2004, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
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.
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.
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
23 #include "libiberty.h"
24 #include "safe-ctype.h"
25 #include "search_list.h"
35 char *spec; /* Parsing modifies this. */
37 bfd_boolean has_right;
41 int prev_index; /* Index of prev match. */
42 Sym *prev_match; /* Previous match. */
43 Sym *first_match; /* Chain of all matches. */
50 static void parse_spec
54 static bfd_boolean match
56 static void extend_match
57 (struct match *, Sym *, Sym_Table *, bfd_boolean);
60 Sym_Table syms[NUM_TABLES];
63 static const char *table_name[] =
65 "INCL_GRAPH", "EXCL_GRAPH",
66 "INCL_ARCS", "EXCL_ARCS",
67 "INCL_FLAT", "EXCL_FLAT",
68 "INCL_TIME", "EXCL_TIME",
69 "INCL_ANNO", "EXCL_ANNO",
70 "INCL_EXEC", "EXCL_EXEC"
74 /* This is the table in which we keep all the syms that match
75 the right half of an arc id. It is NOT sorted according
76 to the addresses, because it is accessed only through
77 the left half's CHILDREN pointers (so it's crucial not
78 to reorder this table once pointers into it exist). */
79 static Sym_Table right_ids;
81 static Source_File non_existent_file =
83 0, "<non-existent-file>", 0, 0, 0, NULL
88 sym_id_add (const char *spec, Table_Id which_table)
91 int len = strlen (spec);
93 id = (struct sym_id *) xmalloc (sizeof (*id) + len + 1);
94 memset (id, 0, sizeof (*id));
96 id->spec = (char *) id + sizeof (*id);
97 strcpy (id->spec, spec);
98 id->which_table = which_table;
105 /* A spec has the syntax FILENAME:(FUNCNAME|LINENUM). As a convenience
106 to the user, a spec without a colon is interpreted as:
108 (i) a FILENAME if it contains a dot
109 (ii) a FUNCNAME if it starts with a non-digit character
110 (iii) a LINENUM if it starts with a digit
112 A FUNCNAME containing a dot can be specified by :FUNCNAME, a
113 FILENAME not containing a dot can be specified by FILENAME. */
116 parse_spec (char *spec, Sym *sym)
121 colon = strrchr (spec, ':');
129 sym->file = source_file_lookup_name (spec);
132 sym->file = &non_existent_file;
139 if (ISDIGIT (spec[0]))
140 sym->line_num = atoi (spec);
145 else if (strlen (spec))
147 /* No colon: spec is a filename if it contains a dot. */
148 if (strchr (spec, '.'))
150 sym->file = source_file_lookup_name (spec);
153 sym->file = &non_existent_file;
155 else if (ISDIGIT (*spec))
157 sym->line_num = atoi (spec);
159 else if (strlen (spec))
167 /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
171 parse_id (struct sym_id *id)
175 DBG (IDDEBUG, printf ("[parse_id] %s -> ", id->spec));
177 slash = strchr (id->spec, '/');
180 parse_spec (slash + 1, &id->right.sym);
182 id->has_right = TRUE;
184 parse_spec (id->spec, &id->left.sym);
187 if (debug_level & IDDEBUG)
189 printf ("%s:", id->left.sym.file ? id->left.sym.file->name : "*");
191 if (id->left.sym.name)
192 printf ("%s", id->left.sym.name);
193 else if (id->left.sym.line_num)
194 printf ("%d", id->left.sym.line_num);
201 id->right.sym.file ? id->right.sym.file->name : "*");
203 if (id->right.sym.name)
204 printf ("%s", id->right.sym.name);
205 else if (id->right.sym.line_num)
206 printf ("%d", id->right.sym.line_num);
217 /* Return TRUE iff PATTERN matches SYM. */
220 match (Sym *pattern, Sym *sym)
222 if (pattern->file && pattern->file != sym->file)
224 if (pattern->line_num && pattern->line_num != sym->line_num)
228 const char *sym_name = sym->name;
229 if (*sym_name && bfd_get_symbol_leading_char (core_bfd) == *sym_name)
231 if (strcmp (pattern->name, sym_name) != 0)
239 extend_match (struct match *m, Sym *sym, Sym_Table *tab, bfd_boolean second_pass)
241 if (m->prev_match != sym - 1)
243 /* Discontinuity: add new match to table. */
246 tab->base[tab->len] = *sym;
247 m->prev_index = tab->len;
249 /* Link match into match's chain. */
250 tab->base[tab->len].next = m->first_match;
251 m->first_match = &tab->base[tab->len];
257 /* Extend match to include this symbol. */
259 tab->base[m->prev_index].end_addr = sym->end_addr;
265 /* Go through sym_id list produced by option processing and fill
266 in the various symbol tables indicating what symbols should
267 be displayed or suppressed for the various kinds of outputs.
269 This can potentially produce huge tables and in particulars
270 tons of arcs, but this happens only if the user makes silly
271 requests---you get what you ask for! */
276 Sym *sym, *left, *right;
280 /* Convert symbol ids into Syms, so we can deal with them more easily. */
281 for (id = id_list; id; id = id->next)
284 /* First determine size of each table. */
285 for (sym = symtab.base; sym < symtab.limit; ++sym)
287 for (id = id_list; id; id = id->next)
289 if (match (&id->left.sym, sym))
290 extend_match (&id->left, sym, &syms[id->which_table], FALSE);
292 if (id->has_right && match (&id->right.sym, sym))
293 extend_match (&id->right, sym, &right_ids, FALSE);
297 /* Create tables of appropriate size and reset lengths. */
298 for (tab = syms; tab < &syms[NUM_TABLES]; ++tab)
302 tab->base = (Sym *) xmalloc (tab->len * sizeof (Sym));
303 tab->limit = tab->base + tab->len;
310 right_ids.base = (Sym *) xmalloc (right_ids.len * sizeof (Sym));
311 right_ids.limit = right_ids.base + right_ids.len;
315 /* Make a second pass through symtab, creating syms as necessary. */
316 for (sym = symtab.base; sym < symtab.limit; ++sym)
318 for (id = id_list; id; id = id->next)
320 if (match (&id->left.sym, sym))
321 extend_match (&id->left, sym, &syms[id->which_table], TRUE);
323 if (id->has_right && match (&id->right.sym, sym))
324 extend_match (&id->right, sym, &right_ids, TRUE);
328 /* Go through ids creating arcs as needed. */
329 for (id = id_list; id; id = id->next)
333 for (left = id->left.first_match; left; left = left->next)
335 for (right = id->right.first_match; right; right = right->next)
339 "[sym_id_parse]: arc %s:%s(%lx-%lx) -> %s:%s(%lx-%lx) to %s\n",
340 left->file ? left->file->name : "*",
341 left->name ? left->name : "*",
342 (unsigned long) left->addr,
343 (unsigned long) left->end_addr,
344 right->file ? right->file->name : "*",
345 right->name ? right->name : "*",
346 (unsigned long) right->addr,
347 (unsigned long) right->end_addr,
348 table_name[id->which_table]));
350 arc_add (left, right, (unsigned long) 0);
356 /* Finally, we can sort the tables and we're done. */
357 for (tab = &syms[0]; tab < &syms[NUM_TABLES]; ++tab)
359 DBG (IDDEBUG, printf ("[sym_id_parse] syms[%s]:\n",
360 table_name[tab - &syms[0]]));
361 symtab_finalize (tab);
366 /* Symbol tables storing the FROM symbols of arcs do not necessarily
367 have distinct address ranges. For example, somebody might request
368 -k /_mcount to suppress any arcs into _mcount, while at the same
369 time requesting -k a/b. Fortunately, those symbol tables don't get
370 very big (the user has to type them!), so a linear search is probably
373 sym_id_arc_is_present (Sym_Table *sym_tab, Sym *from, Sym *to)
377 for (sym = sym_tab->base; sym < sym_tab->limit; ++sym)
379 if (from->addr >= sym->addr && from->addr <= sym->end_addr
380 && arc_lookup (sym, to))