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