daily update
[binutils.git] / gprof / sym_ids.c
blobbf6ffcd054b1e424a5811c584d19a73677d01d44
1 /* sym_ids.c
3 Copyright 2000, 2001 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 "cg_arcs.h"
25 #include "sym_ids.h"
27 struct sym_id
29 struct sym_id *next;
30 char *spec; /* Parsing modifies this. */
31 Table_Id which_table;
32 bool has_right;
34 struct match
36 int prev_index; /* Index of prev match. */
37 Sym *prev_match; /* Previous match. */
38 Sym *first_match; /* Chain of all matches. */
39 Sym sym;
41 left, right;
43 *id_list;
45 Sym_Table syms[NUM_TABLES];
47 #ifdef DEBUG
48 const char *table_name[] =
50 "INCL_GRAPH", "EXCL_GRAPH",
51 "INCL_ARCS", "EXCL_ARCS",
52 "INCL_FLAT", "EXCL_FLAT",
53 "INCL_TIME", "EXCL_TIME",
54 "INCL_ANNO", "EXCL_ANNO",
55 "INCL_EXEC", "EXCL_EXEC"
57 #endif /* DEBUG */
59 /* This is the table in which we keep all the syms that match
60 the right half of an arc id. It is NOT sorted according
61 to the addresses, because it is accessed only through
62 the left half's CHILDREN pointers (so it's crucial not
63 to reorder this table once pointers into it exist). */
64 static Sym_Table right_ids;
66 static Source_File non_existent_file =
68 0, "<non-existent-file>", 0, 0, 0, NULL
72 void
73 DEFUN (sym_id_add, (spec, which_table),
74 const char *spec AND Table_Id which_table)
76 struct sym_id *id;
77 int len = strlen (spec);
79 id = (struct sym_id *) xmalloc (sizeof (*id) + len + 1);
80 memset (id, 0, sizeof (*id));
82 id->spec = (char *) id + sizeof (*id);
83 strcpy (id->spec, spec);
84 id->which_table = which_table;
86 id->next = id_list;
87 id_list = id;
91 /* A spec has the syntax FILENAME:(FUNCNAME|LINENUM). As a convenience
92 to the user, a spec without a colon is interpreted as:
94 (i) a FILENAME if it contains a dot
95 (ii) a FUNCNAME if it starts with a non-digit character
96 (iii) a LINENUM if it starts with a digit
98 A FUNCNAME containing a dot can be specified by :FUNCNAME, a
99 FILENAME not containing a dot can be specified by FILENAME. */
101 static void
102 DEFUN (parse_spec, (spec, sym), char *spec AND Sym * sym)
104 char *colon;
106 sym_init (sym);
107 colon = strrchr (spec, ':');
109 if (colon)
111 *colon = '\0';
113 if (colon > spec)
115 sym->file = source_file_lookup_name (spec);
117 if (!sym->file)
118 sym->file = &non_existent_file;
121 spec = colon + 1;
123 if (strlen (spec))
125 if (ISDIGIT (spec[0]))
126 sym->line_num = atoi (spec);
127 else
128 sym->name = spec;
131 else if (strlen (spec))
133 /* No colon: spec is a filename if it contains a dot. */
134 if (strchr (spec, '.'))
136 sym->file = source_file_lookup_name (spec);
138 if (!sym->file)
139 sym->file = &non_existent_file;
141 else if (ISDIGIT (*spec))
143 sym->line_num = atoi (spec);
145 else if (strlen (spec))
147 sym->name = spec;
153 /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
154 by parse_spec(). */
156 static void
157 DEFUN (parse_id, (id), struct sym_id *id)
159 char *slash;
161 DBG (IDDEBUG, printf ("[parse_id] %s -> ", id->spec));
163 slash = strchr (id->spec, '/');
164 if (slash)
166 parse_spec (slash + 1, &id->right.sym);
167 *slash = '\0';
168 id->has_right = TRUE;
170 parse_spec (id->spec, &id->left.sym);
172 #ifdef DEBUG
173 if (debug_level & IDDEBUG)
175 printf ("%s:", id->left.sym.file ? id->left.sym.file->name : "*");
177 if (id->left.sym.name)
178 printf ("%s", id->left.sym.name);
179 else if (id->left.sym.line_num)
180 printf ("%d", id->left.sym.line_num);
181 else
182 printf ("*");
184 if (id->has_right)
186 printf ("/%s:",
187 id->right.sym.file ? id->right.sym.file->name : "*");
189 if (id->right.sym.name)
190 printf ("%s", id->right.sym.name);
191 else if (id->right.sym.line_num)
192 printf ("%d", id->right.sym.line_num);
193 else
194 printf ("*");
197 printf ("\n");
199 #endif
203 /* Return TRUE iff PATTERN matches SYM. */
205 static bool
206 DEFUN (match, (pattern, sym), Sym * pattern AND Sym * sym)
208 return (pattern->file ? pattern->file == sym->file : TRUE)
209 && (pattern->line_num ? pattern->line_num == sym->line_num : TRUE)
210 && (pattern->name
211 ? strcmp (pattern->name,
212 sym->name+(discard_underscores && sym->name[0] == '_')) == 0
213 : TRUE);
217 static void
218 DEFUN (extend_match, (m, sym, tab, second_pass),
219 struct match *m AND Sym * sym AND Sym_Table * tab AND bool second_pass)
221 if (m->prev_match != sym - 1)
223 /* Discontinuity: add new match to table. */
224 if (second_pass)
226 tab->base[tab->len] = *sym;
227 m->prev_index = tab->len;
229 /* Link match into match's chain. */
230 tab->base[tab->len].next = m->first_match;
231 m->first_match = &tab->base[tab->len];
234 ++tab->len;
237 /* Extend match to include this symbol. */
238 if (second_pass)
239 tab->base[m->prev_index].end_addr = sym->end_addr;
241 m->prev_match = sym;
245 /* Go through sym_id list produced by option processing and fill
246 in the various symbol tables indicating what symbols should
247 be displayed or suppressed for the various kinds of outputs.
249 This can potentially produce huge tables and in particulars
250 tons of arcs, but this happens only if the user makes silly
251 requests---you get what you ask for! */
253 void
254 DEFUN_VOID (sym_id_parse)
256 Sym *sym, *left, *right;
257 struct sym_id *id;
258 Sym_Table *tab;
260 /* Convert symbol ids into Syms, so we can deal with them more easily. */
261 for (id = id_list; id; id = id->next)
262 parse_id (id);
264 /* First determine size of each table. */
265 for (sym = symtab.base; sym < symtab.limit; ++sym)
267 for (id = id_list; id; id = id->next)
269 if (match (&id->left.sym, sym))
270 extend_match (&id->left, sym, &syms[id->which_table], FALSE);
272 if (id->has_right && match (&id->right.sym, sym))
273 extend_match (&id->right, sym, &right_ids, FALSE);
277 /* Create tables of appropriate size and reset lengths. */
278 for (tab = syms; tab < &syms[NUM_TABLES]; ++tab)
280 if (tab->len)
282 tab->base = (Sym *) xmalloc (tab->len * sizeof (Sym));
283 tab->limit = tab->base + tab->len;
284 tab->len = 0;
288 if (right_ids.len)
290 right_ids.base = (Sym *) xmalloc (right_ids.len * sizeof (Sym));
291 right_ids.limit = right_ids.base + right_ids.len;
292 right_ids.len = 0;
295 /* Make a second pass through symtab, creating syms as necessary. */
296 for (sym = symtab.base; sym < symtab.limit; ++sym)
298 for (id = id_list; id; id = id->next)
300 if (match (&id->left.sym, sym))
301 extend_match (&id->left, sym, &syms[id->which_table], TRUE);
303 if (id->has_right && match (&id->right.sym, sym))
304 extend_match (&id->right, sym, &right_ids, TRUE);
308 /* Go through ids creating arcs as needed. */
309 for (id = id_list; id; id = id->next)
311 if (id->has_right)
313 for (left = id->left.first_match; left; left = left->next)
315 for (right = id->right.first_match; right; right = right->next)
317 DBG (IDDEBUG,
318 printf (
319 "[sym_id_parse]: arc %s:%s(%lx-%lx) -> %s:%s(%lx-%lx) to %s\n",
320 left->file ? left->file->name : "*",
321 left->name ? left->name : "*",
322 (unsigned long) left->addr,
323 (unsigned long) left->end_addr,
324 right->file ? right->file->name : "*",
325 right->name ? right->name : "*",
326 (unsigned long) right->addr,
327 (unsigned long) right->end_addr,
328 table_name[id->which_table]));
330 arc_add (left, right, (unsigned long) 0);
336 /* Finally, we can sort the tables and we're done. */
337 for (tab = &syms[0]; tab < &syms[NUM_TABLES]; ++tab)
339 DBG (IDDEBUG, printf ("[sym_id_parse] syms[%s]:\n",
340 table_name[tab - &syms[0]]));
341 symtab_finalize (tab);
346 /* Symbol tables storing the FROM symbols of arcs do not necessarily
347 have distinct address ranges. For example, somebody might request
348 -k /_mcount to suppress any arcs into _mcount, while at the same
349 time requesting -k a/b. Fortunately, those symbol tables don't get
350 very big (the user has to type them!), so a linear search is probably
351 tolerable. */
352 bool
353 DEFUN (sym_id_arc_is_present, (symtab, from, to),
354 Sym_Table * symtab AND Sym * from AND Sym * to)
356 Sym *sym;
358 for (sym = symtab->base; sym < symtab->limit; ++sym)
360 if (from->addr >= sym->addr && from->addr <= sym->end_addr
361 && arc_lookup (sym, to))
362 return TRUE;
365 return FALSE;