* Makefile.def: configure-target-boehm-gc depends on
[binutils.git] / gprof / sym_ids.c
blob657957e8c8a34df0a04897b76a41885450112bec
1 /* sym_ids.c
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
20 02110-1301, USA. */
22 #include "gprof.h"
23 #include "libiberty.h"
24 #include "safe-ctype.h"
25 #include "search_list.h"
26 #include "source.h"
27 #include "symtab.h"
28 #include "cg_arcs.h"
29 #include "sym_ids.h"
30 #include "corefile.h"
32 static struct sym_id
34 struct sym_id *next;
35 char *spec; /* Parsing modifies this. */
36 Table_Id which_table;
37 bfd_boolean has_right;
39 struct match
41 int prev_index; /* Index of prev match. */
42 Sym *prev_match; /* Previous match. */
43 Sym *first_match; /* Chain of all matches. */
44 Sym sym;
46 left, right;
48 *id_list;
50 static void parse_spec
51 (char *, Sym *);
52 static void parse_id
53 (struct sym_id *);
54 static bfd_boolean match
55 (Sym *, Sym *);
56 static void extend_match
57 (struct match *, Sym *, Sym_Table *, bfd_boolean);
60 Sym_Table syms[NUM_TABLES];
62 #ifdef DEBUG
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"
72 #endif /* DEBUG */
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
87 void
88 sym_id_add (const char *spec, Table_Id which_table)
90 struct sym_id *id;
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;
100 id->next = id_list;
101 id_list = id;
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. */
115 static void
116 parse_spec (char *spec, Sym *sym)
118 char *colon;
120 sym_init (sym);
121 colon = strrchr (spec, ':');
123 if (colon)
125 *colon = '\0';
127 if (colon > spec)
129 sym->file = source_file_lookup_name (spec);
131 if (!sym->file)
132 sym->file = &non_existent_file;
135 spec = colon + 1;
137 if (strlen (spec))
139 if (ISDIGIT (spec[0]))
140 sym->line_num = atoi (spec);
141 else
142 sym->name = 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);
152 if (!sym->file)
153 sym->file = &non_existent_file;
155 else if (ISDIGIT (*spec))
157 sym->line_num = atoi (spec);
159 else if (strlen (spec))
161 sym->name = spec;
167 /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
168 by parse_spec(). */
170 static void
171 parse_id (struct sym_id *id)
173 char *slash;
175 DBG (IDDEBUG, printf ("[parse_id] %s -> ", id->spec));
177 slash = strchr (id->spec, '/');
178 if (slash)
180 parse_spec (slash + 1, &id->right.sym);
181 *slash = '\0';
182 id->has_right = TRUE;
184 parse_spec (id->spec, &id->left.sym);
186 #ifdef DEBUG
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);
195 else
196 printf ("*");
198 if (id->has_right)
200 printf ("/%s:",
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);
207 else
208 printf ("*");
211 printf ("\n");
213 #endif
217 /* Return TRUE iff PATTERN matches SYM. */
219 static bfd_boolean
220 match (Sym *pattern, Sym *sym)
222 if (pattern->file && pattern->file != sym->file)
223 return FALSE;
224 if (pattern->line_num && pattern->line_num != sym->line_num)
225 return FALSE;
226 if (pattern->name)
228 const char *sym_name = sym->name;
229 if (*sym_name && bfd_get_symbol_leading_char (core_bfd) == *sym_name)
230 sym_name++;
231 if (strcmp (pattern->name, sym_name) != 0)
232 return FALSE;
234 return TRUE;
238 static void
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. */
244 if (second_pass)
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];
254 ++tab->len;
257 /* Extend match to include this symbol. */
258 if (second_pass)
259 tab->base[m->prev_index].end_addr = sym->end_addr;
261 m->prev_match = sym;
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! */
273 void
274 sym_id_parse ()
276 Sym *sym, *left, *right;
277 struct sym_id *id;
278 Sym_Table *tab;
280 /* Convert symbol ids into Syms, so we can deal with them more easily. */
281 for (id = id_list; id; id = id->next)
282 parse_id (id);
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)
300 if (tab->len)
302 tab->base = (Sym *) xmalloc (tab->len * sizeof (Sym));
303 tab->limit = tab->base + tab->len;
304 tab->len = 0;
308 if (right_ids.len)
310 right_ids.base = (Sym *) xmalloc (right_ids.len * sizeof (Sym));
311 right_ids.limit = right_ids.base + right_ids.len;
312 right_ids.len = 0;
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)
331 if (id->has_right)
333 for (left = id->left.first_match; left; left = left->next)
335 for (right = id->right.first_match; right; right = right->next)
337 DBG (IDDEBUG,
338 printf (
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
371 tolerable. */
372 bfd_boolean
373 sym_id_arc_is_present (Sym_Table *sym_tab, Sym *from, Sym *to)
375 Sym *sym;
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))
381 return TRUE;
384 return FALSE;