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