2003-07-18 Michael Snyder <msnyder@redhat.com>
[binutils.git] / gprof / sym_ids.c
blobf75088a30ea855cd2ec164a952e5ed398a33eb44
1 /* sym_ids.c
3 Copyright 2000, 2001, 2002 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 2 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., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "libiberty.h"
23 #include "safe-ctype.h"
24 #include "gprof.h"
25 #include "search_list.h"
26 #include "source.h"
27 #include "symtab.h"
28 #include "cg_arcs.h"
29 #include "sym_ids.h"
31 struct sym_id
33 struct sym_id *next;
34 char *spec; /* Parsing modifies this. */
35 Table_Id which_table;
36 bfd_boolean has_right;
38 struct match
40 int prev_index; /* Index of prev match. */
41 Sym *prev_match; /* Previous match. */
42 Sym *first_match; /* Chain of all matches. */
43 Sym sym;
45 left, right;
47 *id_list;
49 static void parse_spec
50 PARAMS ((char *, Sym *));
51 static void parse_id
52 PARAMS ((struct sym_id *));
53 static bfd_boolean match
54 PARAMS ((Sym *, Sym *));
55 static void extend_match
56 PARAMS ((struct match *, Sym *, Sym_Table *, bfd_boolean));
59 Sym_Table syms[NUM_TABLES];
61 #ifdef DEBUG
62 const char *table_name[] =
64 "INCL_GRAPH", "EXCL_GRAPH",
65 "INCL_ARCS", "EXCL_ARCS",
66 "INCL_FLAT", "EXCL_FLAT",
67 "INCL_TIME", "EXCL_TIME",
68 "INCL_ANNO", "EXCL_ANNO",
69 "INCL_EXEC", "EXCL_EXEC"
71 #endif /* DEBUG */
73 /* This is the table in which we keep all the syms that match
74 the right half of an arc id. It is NOT sorted according
75 to the addresses, because it is accessed only through
76 the left half's CHILDREN pointers (so it's crucial not
77 to reorder this table once pointers into it exist). */
78 static Sym_Table right_ids;
80 static Source_File non_existent_file =
82 0, "<non-existent-file>", 0, 0, 0, NULL
86 void
87 sym_id_add (spec, which_table)
88 const char *spec;
89 Table_Id which_table;
91 struct sym_id *id;
92 int len = strlen (spec);
94 id = (struct sym_id *) xmalloc (sizeof (*id) + len + 1);
95 memset (id, 0, sizeof (*id));
97 id->spec = (char *) id + sizeof (*id);
98 strcpy (id->spec, spec);
99 id->which_table = which_table;
101 id->next = id_list;
102 id_list = id;
106 /* A spec has the syntax FILENAME:(FUNCNAME|LINENUM). As a convenience
107 to the user, a spec without a colon is interpreted as:
109 (i) a FILENAME if it contains a dot
110 (ii) a FUNCNAME if it starts with a non-digit character
111 (iii) a LINENUM if it starts with a digit
113 A FUNCNAME containing a dot can be specified by :FUNCNAME, a
114 FILENAME not containing a dot can be specified by FILENAME. */
116 static void
117 parse_spec (spec, sym)
118 char *spec;
119 Sym *sym;
121 char *colon;
123 sym_init (sym);
124 colon = strrchr (spec, ':');
126 if (colon)
128 *colon = '\0';
130 if (colon > spec)
132 sym->file = source_file_lookup_name (spec);
134 if (!sym->file)
135 sym->file = &non_existent_file;
138 spec = colon + 1;
140 if (strlen (spec))
142 if (ISDIGIT (spec[0]))
143 sym->line_num = atoi (spec);
144 else
145 sym->name = spec;
148 else if (strlen (spec))
150 /* No colon: spec is a filename if it contains a dot. */
151 if (strchr (spec, '.'))
153 sym->file = source_file_lookup_name (spec);
155 if (!sym->file)
156 sym->file = &non_existent_file;
158 else if (ISDIGIT (*spec))
160 sym->line_num = atoi (spec);
162 else if (strlen (spec))
164 sym->name = spec;
170 /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
171 by parse_spec(). */
173 static void
174 parse_id (id)
175 struct sym_id *id;
177 char *slash;
179 DBG (IDDEBUG, printf ("[parse_id] %s -> ", id->spec));
181 slash = strchr (id->spec, '/');
182 if (slash)
184 parse_spec (slash + 1, &id->right.sym);
185 *slash = '\0';
186 id->has_right = TRUE;
188 parse_spec (id->spec, &id->left.sym);
190 #ifdef DEBUG
191 if (debug_level & IDDEBUG)
193 printf ("%s:", id->left.sym.file ? id->left.sym.file->name : "*");
195 if (id->left.sym.name)
196 printf ("%s", id->left.sym.name);
197 else if (id->left.sym.line_num)
198 printf ("%d", id->left.sym.line_num);
199 else
200 printf ("*");
202 if (id->has_right)
204 printf ("/%s:",
205 id->right.sym.file ? id->right.sym.file->name : "*");
207 if (id->right.sym.name)
208 printf ("%s", id->right.sym.name);
209 else if (id->right.sym.line_num)
210 printf ("%d", id->right.sym.line_num);
211 else
212 printf ("*");
215 printf ("\n");
217 #endif
221 /* Return TRUE iff PATTERN matches SYM. */
223 static bfd_boolean
224 match (pattern, sym)
225 Sym *pattern;
226 Sym *sym;
228 return (pattern->file ? pattern->file == sym->file : TRUE)
229 && (pattern->line_num ? pattern->line_num == sym->line_num : TRUE)
230 && (pattern->name
231 ? strcmp (pattern->name,
232 sym->name+(discard_underscores && sym->name[0] == '_')) == 0
233 : TRUE);
237 static void
238 extend_match (m, sym, tab, second_pass)
239 struct match *m;
240 Sym *sym;
241 Sym_Table *tab;
242 bfd_boolean second_pass;
244 if (m->prev_match != sym - 1)
246 /* Discontinuity: add new match to table. */
247 if (second_pass)
249 tab->base[tab->len] = *sym;
250 m->prev_index = tab->len;
252 /* Link match into match's chain. */
253 tab->base[tab->len].next = m->first_match;
254 m->first_match = &tab->base[tab->len];
257 ++tab->len;
260 /* Extend match to include this symbol. */
261 if (second_pass)
262 tab->base[m->prev_index].end_addr = sym->end_addr;
264 m->prev_match = sym;
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.
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! */
276 void
277 sym_id_parse ()
279 Sym *sym, *left, *right;
280 struct sym_id *id;
281 Sym_Table *tab;
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);
287 /* First determine size of each table. */
288 for (sym = symtab.base; sym < symtab.limit; ++sym)
290 for (id = id_list; id; id = id->next)
292 if (match (&id->left.sym, sym))
293 extend_match (&id->left, sym, &syms[id->which_table], FALSE);
295 if (id->has_right && match (&id->right.sym, sym))
296 extend_match (&id->right, sym, &right_ids, FALSE);
300 /* Create tables of appropriate size and reset lengths. */
301 for (tab = syms; tab < &syms[NUM_TABLES]; ++tab)
303 if (tab->len)
305 tab->base = (Sym *) xmalloc (tab->len * sizeof (Sym));
306 tab->limit = tab->base + tab->len;
307 tab->len = 0;
311 if (right_ids.len)
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;
318 /* Make a second pass through symtab, creating syms as necessary. */
319 for (sym = symtab.base; sym < symtab.limit; ++sym)
321 for (id = id_list; id; id = id->next)
323 if (match (&id->left.sym, sym))
324 extend_match (&id->left, sym, &syms[id->which_table], TRUE);
326 if (id->has_right && match (&id->right.sym, sym))
327 extend_match (&id->right, sym, &right_ids, TRUE);
331 /* Go through ids creating arcs as needed. */
332 for (id = id_list; id; id = id->next)
334 if (id->has_right)
336 for (left = id->left.first_match; left; left = left->next)
338 for (right = id->right.first_match; right; right = right->next)
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]));
353 arc_add (left, right, (unsigned long) 0);
359 /* Finally, we can sort the tables and we're done. */
360 for (tab = &syms[0]; tab < &syms[NUM_TABLES]; ++tab)
362 DBG (IDDEBUG, printf ("[sym_id_parse] syms[%s]:\n",
363 table_name[tab - &syms[0]]));
364 symtab_finalize (tab);
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 bfd_boolean
376 sym_id_arc_is_present (sym_tab, from, to)
377 Sym_Table *sym_tab;
378 Sym *from;
379 Sym *to;
381 Sym *sym;
383 for (sym = sym_tab->base; sym < sym_tab->limit; ++sym)
385 if (from->addr >= sym->addr && from->addr <= sym->end_addr
386 && arc_lookup (sym, to))
387 return TRUE;
390 return FALSE;