]>
Commit | Line | Data |
---|---|---|
ef368dac NC |
1 | /* sym_ids.c |
2 | ||
1355568a | 3 | Copyright 2000, 2001, 2002 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 | ||
31 | struct sym_id | |
32 | { | |
33 | struct sym_id *next; | |
ef368dac | 34 | char *spec; /* Parsing modifies this. */ |
252b5132 | 35 | Table_Id which_table; |
b34976b6 | 36 | bfd_boolean 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 | ||
b34976b6 AM |
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)); | |
1355568a AM |
57 | |
58 | ||
252b5132 RH |
59 | Sym_Table syms[NUM_TABLES]; |
60 | ||
61 | #ifdef DEBUG | |
62 | const char *table_name[] = | |
63 | { | |
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" | |
70 | }; | |
71 | #endif /* DEBUG */ | |
72 | ||
ef368dac NC |
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). */ | |
252b5132 RH |
78 | static Sym_Table right_ids; |
79 | ||
80 | static Source_File non_existent_file = | |
81 | { | |
8622e41b | 82 | 0, "<non-existent-file>", 0, 0, 0, NULL |
252b5132 RH |
83 | }; |
84 | ||
85 | ||
86 | void | |
1355568a AM |
87 | sym_id_add (spec, which_table) |
88 | const char *spec; | |
89 | Table_Id which_table; | |
252b5132 RH |
90 | { |
91 | struct sym_id *id; | |
92 | int len = strlen (spec); | |
93 | ||
94 | id = (struct sym_id *) xmalloc (sizeof (*id) + len + 1); | |
95 | memset (id, 0, sizeof (*id)); | |
96 | ||
97 | id->spec = (char *) id + sizeof (*id); | |
98 | strcpy (id->spec, spec); | |
99 | id->which_table = which_table; | |
100 | ||
101 | id->next = id_list; | |
102 | id_list = id; | |
103 | } | |
104 | ||
105 | ||
ef368dac NC |
106 | /* A spec has the syntax FILENAME:(FUNCNAME|LINENUM). As a convenience |
107 | to the user, a spec without a colon is interpreted as: | |
0eee5820 AM |
108 | |
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 | |
112 | ||
ef368dac NC |
113 | A FUNCNAME containing a dot can be specified by :FUNCNAME, a |
114 | FILENAME not containing a dot can be specified by FILENAME. */ | |
115 | ||
252b5132 | 116 | static void |
1355568a AM |
117 | parse_spec (spec, sym) |
118 | char *spec; | |
119 | Sym *sym; | |
252b5132 RH |
120 | { |
121 | char *colon; | |
122 | ||
123 | sym_init (sym); | |
124 | colon = strrchr (spec, ':'); | |
0eee5820 | 125 | |
252b5132 RH |
126 | if (colon) |
127 | { | |
128 | *colon = '\0'; | |
0eee5820 | 129 | |
252b5132 RH |
130 | if (colon > spec) |
131 | { | |
132 | sym->file = source_file_lookup_name (spec); | |
0eee5820 | 133 | |
252b5132 | 134 | if (!sym->file) |
ef368dac | 135 | sym->file = &non_existent_file; |
252b5132 | 136 | } |
0eee5820 | 137 | |
252b5132 | 138 | spec = colon + 1; |
0eee5820 | 139 | |
252b5132 RH |
140 | if (strlen (spec)) |
141 | { | |
3882b010 | 142 | if (ISDIGIT (spec[0])) |
ef368dac | 143 | sym->line_num = atoi (spec); |
252b5132 | 144 | else |
ef368dac | 145 | sym->name = spec; |
252b5132 RH |
146 | } |
147 | } | |
148 | else if (strlen (spec)) | |
149 | { | |
ef368dac | 150 | /* No colon: spec is a filename if it contains a dot. */ |
252b5132 RH |
151 | if (strchr (spec, '.')) |
152 | { | |
153 | sym->file = source_file_lookup_name (spec); | |
0eee5820 | 154 | |
252b5132 | 155 | if (!sym->file) |
ef368dac | 156 | sym->file = &non_existent_file; |
252b5132 | 157 | } |
3882b010 | 158 | else if (ISDIGIT (*spec)) |
252b5132 RH |
159 | { |
160 | sym->line_num = atoi (spec); | |
161 | } | |
162 | else if (strlen (spec)) | |
163 | { | |
164 | sym->name = spec; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | ||
ef368dac NC |
170 | /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined |
171 | by parse_spec(). */ | |
172 | ||
252b5132 | 173 | static void |
1355568a AM |
174 | parse_id (id) |
175 | struct sym_id *id; | |
252b5132 RH |
176 | { |
177 | char *slash; | |
178 | ||
179 | DBG (IDDEBUG, printf ("[parse_id] %s -> ", id->spec)); | |
180 | ||
181 | slash = strchr (id->spec, '/'); | |
182 | if (slash) | |
183 | { | |
184 | parse_spec (slash + 1, &id->right.sym); | |
185 | *slash = '\0'; | |
b34976b6 | 186 | id->has_right = TRUE; |
252b5132 RH |
187 | } |
188 | parse_spec (id->spec, &id->left.sym); | |
189 | ||
190 | #ifdef DEBUG | |
191 | if (debug_level & IDDEBUG) | |
192 | { | |
193 | printf ("%s:", id->left.sym.file ? id->left.sym.file->name : "*"); | |
0eee5820 | 194 | |
252b5132 | 195 | if (id->left.sym.name) |
ef368dac | 196 | printf ("%s", id->left.sym.name); |
252b5132 | 197 | else if (id->left.sym.line_num) |
ef368dac | 198 | printf ("%d", id->left.sym.line_num); |
252b5132 | 199 | else |
ef368dac | 200 | printf ("*"); |
0eee5820 | 201 | |
252b5132 RH |
202 | if (id->has_right) |
203 | { | |
204 | printf ("/%s:", | |
205 | id->right.sym.file ? id->right.sym.file->name : "*"); | |
0eee5820 | 206 | |
252b5132 | 207 | if (id->right.sym.name) |
ef368dac | 208 | printf ("%s", id->right.sym.name); |
252b5132 | 209 | else if (id->right.sym.line_num) |
ef368dac | 210 | printf ("%d", id->right.sym.line_num); |
252b5132 | 211 | else |
ef368dac | 212 | printf ("*"); |
252b5132 | 213 | } |
0eee5820 | 214 | |
252b5132 RH |
215 | printf ("\n"); |
216 | } | |
217 | #endif | |
218 | } | |
219 | ||
220 | ||
ef368dac NC |
221 | /* Return TRUE iff PATTERN matches SYM. */ |
222 | ||
b34976b6 | 223 | static bfd_boolean |
1355568a AM |
224 | match (pattern, sym) |
225 | Sym *pattern; | |
226 | Sym *sym; | |
252b5132 | 227 | { |
b34976b6 AM |
228 | return (pattern->file ? pattern->file == sym->file : TRUE) |
229 | && (pattern->line_num ? pattern->line_num == sym->line_num : TRUE) | |
5af11cab AM |
230 | && (pattern->name |
231 | ? strcmp (pattern->name, | |
232 | sym->name+(discard_underscores && sym->name[0] == '_')) == 0 | |
b34976b6 | 233 | : TRUE); |
252b5132 RH |
234 | } |
235 | ||
236 | ||
237 | static void | |
1355568a AM |
238 | extend_match (m, sym, tab, second_pass) |
239 | struct match *m; | |
240 | Sym *sym; | |
241 | Sym_Table *tab; | |
b34976b6 | 242 | bfd_boolean second_pass; |
252b5132 RH |
243 | { |
244 | if (m->prev_match != sym - 1) | |
245 | { | |
ef368dac | 246 | /* Discontinuity: add new match to table. */ |
252b5132 RH |
247 | if (second_pass) |
248 | { | |
249 | tab->base[tab->len] = *sym; | |
250 | m->prev_index = tab->len; | |
251 | ||
ef368dac | 252 | /* Link match into match's chain. */ |
252b5132 RH |
253 | tab->base[tab->len].next = m->first_match; |
254 | m->first_match = &tab->base[tab->len]; | |
255 | } | |
0eee5820 | 256 | |
252b5132 RH |
257 | ++tab->len; |
258 | } | |
259 | ||
ef368dac | 260 | /* Extend match to include this symbol. */ |
252b5132 | 261 | if (second_pass) |
ef368dac NC |
262 | tab->base[m->prev_index].end_addr = sym->end_addr; |
263 | ||
252b5132 RH |
264 | m->prev_match = sym; |
265 | } | |
266 | ||
267 | ||
ef368dac NC |
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. | |
0eee5820 | 271 | |
ef368dac NC |
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! */ | |
275 | ||
252b5132 | 276 | void |
1355568a | 277 | sym_id_parse () |
252b5132 RH |
278 | { |
279 | Sym *sym, *left, *right; | |
280 | struct sym_id *id; | |
281 | Sym_Table *tab; | |
282 | ||
ef368dac | 283 | /* Convert symbol ids into Syms, so we can deal with them more easily. */ |
252b5132 | 284 | for (id = id_list; id; id = id->next) |
ef368dac | 285 | parse_id (id); |
252b5132 | 286 | |
ef368dac | 287 | /* First determine size of each table. */ |
252b5132 RH |
288 | for (sym = symtab.base; sym < symtab.limit; ++sym) |
289 | { | |
290 | for (id = id_list; id; id = id->next) | |
291 | { | |
292 | if (match (&id->left.sym, sym)) | |
b34976b6 | 293 | extend_match (&id->left, sym, &syms[id->which_table], FALSE); |
ef368dac | 294 | |
252b5132 | 295 | if (id->has_right && match (&id->right.sym, sym)) |
b34976b6 | 296 | extend_match (&id->right, sym, &right_ids, FALSE); |
252b5132 RH |
297 | } |
298 | } | |
299 | ||
ef368dac | 300 | /* Create tables of appropriate size and reset lengths. */ |
252b5132 RH |
301 | for (tab = syms; tab < &syms[NUM_TABLES]; ++tab) |
302 | { | |
303 | if (tab->len) | |
304 | { | |
305 | tab->base = (Sym *) xmalloc (tab->len * sizeof (Sym)); | |
306 | tab->limit = tab->base + tab->len; | |
307 | tab->len = 0; | |
308 | } | |
309 | } | |
0eee5820 | 310 | |
252b5132 RH |
311 | if (right_ids.len) |
312 | { | |
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; | |
316 | } | |
317 | ||
ef368dac | 318 | /* Make a second pass through symtab, creating syms as necessary. */ |
252b5132 RH |
319 | for (sym = symtab.base; sym < symtab.limit; ++sym) |
320 | { | |
321 | for (id = id_list; id; id = id->next) | |
322 | { | |
323 | if (match (&id->left.sym, sym)) | |
b34976b6 | 324 | extend_match (&id->left, sym, &syms[id->which_table], TRUE); |
ef368dac | 325 | |
252b5132 | 326 | if (id->has_right && match (&id->right.sym, sym)) |
b34976b6 | 327 | extend_match (&id->right, sym, &right_ids, TRUE); |
252b5132 RH |
328 | } |
329 | } | |
330 | ||
ef368dac | 331 | /* Go through ids creating arcs as needed. */ |
252b5132 RH |
332 | for (id = id_list; id; id = id->next) |
333 | { | |
334 | if (id->has_right) | |
335 | { | |
336 | for (left = id->left.first_match; left; left = left->next) | |
337 | { | |
338 | for (right = id->right.first_match; right; right = right->next) | |
339 | { | |
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 : "*", | |
fdcf7d43 ILT |
344 | left->name ? left->name : "*", |
345 | (unsigned long) left->addr, | |
346 | (unsigned long) left->end_addr, | |
252b5132 | 347 | right->file ? right->file->name : "*", |
fdcf7d43 ILT |
348 | right->name ? right->name : "*", |
349 | (unsigned long) right->addr, | |
350 | (unsigned long) right->end_addr, | |
252b5132 | 351 | table_name[id->which_table])); |
0eee5820 | 352 | |
252b5132 RH |
353 | arc_add (left, right, (unsigned long) 0); |
354 | } | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
ef368dac | 359 | /* Finally, we can sort the tables and we're done. */ |
252b5132 RH |
360 | for (tab = &syms[0]; tab < &syms[NUM_TABLES]; ++tab) |
361 | { | |
362 | DBG (IDDEBUG, printf ("[sym_id_parse] syms[%s]:\n", | |
363 | table_name[tab - &syms[0]])); | |
364 | symtab_finalize (tab); | |
365 | } | |
366 | } | |
367 | ||
368 | ||
ef368dac NC |
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. */ | |
b34976b6 | 375 | bfd_boolean |
1355568a AM |
376 | sym_id_arc_is_present (sym_tab, from, to) |
377 | Sym_Table *sym_tab; | |
378 | Sym *from; | |
379 | Sym *to; | |
252b5132 RH |
380 | { |
381 | Sym *sym; | |
382 | ||
1355568a | 383 | for (sym = sym_tab->base; sym < sym_tab->limit; ++sym) |
252b5132 RH |
384 | { |
385 | if (from->addr >= sym->addr && from->addr <= sym->end_addr | |
386 | && arc_lookup (sym, to)) | |
b34976b6 | 387 | return TRUE; |
252b5132 | 388 | } |
0eee5820 | 389 | |
b34976b6 | 390 | return FALSE; |
252b5132 | 391 | } |