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