Replace 'return false' with a return of a bfd_reloc_ error code.
[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"
252b5132
RH
24#include "cg_arcs.h"
25#include "sym_ids.h"
26
27struct sym_id
28 {
29 struct sym_id *next;
ef368dac 30 char *spec; /* Parsing modifies this. */
252b5132
RH
31 Table_Id which_table;
32 bool has_right;
0eee5820 33
252b5132
RH
34 struct match
35 {
ef368dac
NC
36 int prev_index; /* Index of prev match. */
37 Sym *prev_match; /* Previous match. */
38 Sym *first_match; /* Chain of all matches. */
252b5132
RH
39 Sym sym;
40 }
41 left, right;
42 }
43 *id_list;
44
45Sym_Table syms[NUM_TABLES];
46
47#ifdef DEBUG
48const char *table_name[] =
49{
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"
56};
57#endif /* DEBUG */
58
ef368dac
NC
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). */
252b5132
RH
64static Sym_Table right_ids;
65
66static Source_File non_existent_file =
67{
8622e41b 68 0, "<non-existent-file>", 0, 0, 0, NULL
252b5132
RH
69};
70
71
72void
73DEFUN (sym_id_add, (spec, which_table),
74 const char *spec AND Table_Id which_table)
75{
76 struct sym_id *id;
77 int len = strlen (spec);
78
79 id = (struct sym_id *) xmalloc (sizeof (*id) + len + 1);
80 memset (id, 0, sizeof (*id));
81
82 id->spec = (char *) id + sizeof (*id);
83 strcpy (id->spec, spec);
84 id->which_table = which_table;
85
86 id->next = id_list;
87 id_list = id;
88}
89
90
ef368dac
NC
91/* A spec has the syntax FILENAME:(FUNCNAME|LINENUM). As a convenience
92 to the user, a spec without a colon is interpreted as:
0eee5820
AM
93
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
97
ef368dac
NC
98 A FUNCNAME containing a dot can be specified by :FUNCNAME, a
99 FILENAME not containing a dot can be specified by FILENAME. */
100
252b5132
RH
101static void
102DEFUN (parse_spec, (spec, sym), char *spec AND Sym * sym)
103{
104 char *colon;
105
106 sym_init (sym);
107 colon = strrchr (spec, ':');
0eee5820 108
252b5132
RH
109 if (colon)
110 {
111 *colon = '\0';
0eee5820 112
252b5132
RH
113 if (colon > spec)
114 {
115 sym->file = source_file_lookup_name (spec);
0eee5820 116
252b5132 117 if (!sym->file)
ef368dac 118 sym->file = &non_existent_file;
252b5132 119 }
0eee5820 120
252b5132 121 spec = colon + 1;
0eee5820 122
252b5132
RH
123 if (strlen (spec))
124 {
3882b010 125 if (ISDIGIT (spec[0]))
ef368dac 126 sym->line_num = atoi (spec);
252b5132 127 else
ef368dac 128 sym->name = spec;
252b5132
RH
129 }
130 }
131 else if (strlen (spec))
132 {
ef368dac 133 /* No colon: spec is a filename if it contains a dot. */
252b5132
RH
134 if (strchr (spec, '.'))
135 {
136 sym->file = source_file_lookup_name (spec);
0eee5820 137
252b5132 138 if (!sym->file)
ef368dac 139 sym->file = &non_existent_file;
252b5132 140 }
3882b010 141 else if (ISDIGIT (*spec))
252b5132
RH
142 {
143 sym->line_num = atoi (spec);
144 }
145 else if (strlen (spec))
146 {
147 sym->name = spec;
148 }
149 }
150}
151
152
ef368dac
NC
153/* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
154 by parse_spec(). */
155
252b5132
RH
156static void
157DEFUN (parse_id, (id), struct sym_id *id)
158{
159 char *slash;
160
161 DBG (IDDEBUG, printf ("[parse_id] %s -> ", id->spec));
162
163 slash = strchr (id->spec, '/');
164 if (slash)
165 {
166 parse_spec (slash + 1, &id->right.sym);
167 *slash = '\0';
168 id->has_right = TRUE;
169 }
170 parse_spec (id->spec, &id->left.sym);
171
172#ifdef DEBUG
173 if (debug_level & IDDEBUG)
174 {
175 printf ("%s:", id->left.sym.file ? id->left.sym.file->name : "*");
0eee5820 176
252b5132 177 if (id->left.sym.name)
ef368dac 178 printf ("%s", id->left.sym.name);
252b5132 179 else if (id->left.sym.line_num)
ef368dac 180 printf ("%d", id->left.sym.line_num);
252b5132 181 else
ef368dac 182 printf ("*");
0eee5820 183
252b5132
RH
184 if (id->has_right)
185 {
186 printf ("/%s:",
187 id->right.sym.file ? id->right.sym.file->name : "*");
0eee5820 188
252b5132 189 if (id->right.sym.name)
ef368dac 190 printf ("%s", id->right.sym.name);
252b5132 191 else if (id->right.sym.line_num)
ef368dac 192 printf ("%d", id->right.sym.line_num);
252b5132 193 else
ef368dac 194 printf ("*");
252b5132 195 }
0eee5820 196
252b5132
RH
197 printf ("\n");
198 }
199#endif
200}
201
202
ef368dac
NC
203/* Return TRUE iff PATTERN matches SYM. */
204
252b5132
RH
205static bool
206DEFUN (match, (pattern, sym), Sym * pattern AND Sym * sym)
207{
208 return (pattern->file ? pattern->file == sym->file : TRUE)
209 && (pattern->line_num ? pattern->line_num == sym->line_num : TRUE)
5af11cab
AM
210 && (pattern->name
211 ? strcmp (pattern->name,
212 sym->name+(discard_underscores && sym->name[0] == '_')) == 0
213 : TRUE);
252b5132
RH
214}
215
216
217static void
218DEFUN (extend_match, (m, sym, tab, second_pass),
219 struct match *m AND Sym * sym AND Sym_Table * tab AND bool second_pass)
220{
221 if (m->prev_match != sym - 1)
222 {
ef368dac 223 /* Discontinuity: add new match to table. */
252b5132
RH
224 if (second_pass)
225 {
226 tab->base[tab->len] = *sym;
227 m->prev_index = tab->len;
228
ef368dac 229 /* Link match into match's chain. */
252b5132
RH
230 tab->base[tab->len].next = m->first_match;
231 m->first_match = &tab->base[tab->len];
232 }
0eee5820 233
252b5132
RH
234 ++tab->len;
235 }
236
ef368dac 237 /* Extend match to include this symbol. */
252b5132 238 if (second_pass)
ef368dac
NC
239 tab->base[m->prev_index].end_addr = sym->end_addr;
240
252b5132
RH
241 m->prev_match = sym;
242}
243
244
ef368dac
NC
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.
0eee5820 248
ef368dac
NC
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! */
252
252b5132
RH
253void
254DEFUN_VOID (sym_id_parse)
255{
256 Sym *sym, *left, *right;
257 struct sym_id *id;
258 Sym_Table *tab;
259
ef368dac 260 /* Convert symbol ids into Syms, so we can deal with them more easily. */
252b5132 261 for (id = id_list; id; id = id->next)
ef368dac 262 parse_id (id);
252b5132 263
ef368dac 264 /* First determine size of each table. */
252b5132
RH
265 for (sym = symtab.base; sym < symtab.limit; ++sym)
266 {
267 for (id = id_list; id; id = id->next)
268 {
269 if (match (&id->left.sym, sym))
ef368dac
NC
270 extend_match (&id->left, sym, &syms[id->which_table], FALSE);
271
252b5132 272 if (id->has_right && match (&id->right.sym, sym))
ef368dac 273 extend_match (&id->right, sym, &right_ids, FALSE);
252b5132
RH
274 }
275 }
276
ef368dac 277 /* Create tables of appropriate size and reset lengths. */
252b5132
RH
278 for (tab = syms; tab < &syms[NUM_TABLES]; ++tab)
279 {
280 if (tab->len)
281 {
282 tab->base = (Sym *) xmalloc (tab->len * sizeof (Sym));
283 tab->limit = tab->base + tab->len;
284 tab->len = 0;
285 }
286 }
0eee5820 287
252b5132
RH
288 if (right_ids.len)
289 {
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;
293 }
294
ef368dac 295 /* Make a second pass through symtab, creating syms as necessary. */
252b5132
RH
296 for (sym = symtab.base; sym < symtab.limit; ++sym)
297 {
298 for (id = id_list; id; id = id->next)
299 {
300 if (match (&id->left.sym, sym))
ef368dac
NC
301 extend_match (&id->left, sym, &syms[id->which_table], TRUE);
302
252b5132 303 if (id->has_right && match (&id->right.sym, sym))
ef368dac 304 extend_match (&id->right, sym, &right_ids, TRUE);
252b5132
RH
305 }
306 }
307
ef368dac 308 /* Go through ids creating arcs as needed. */
252b5132
RH
309 for (id = id_list; id; id = id->next)
310 {
311 if (id->has_right)
312 {
313 for (left = id->left.first_match; left; left = left->next)
314 {
315 for (right = id->right.first_match; right; right = right->next)
316 {
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 : "*",
fdcf7d43
ILT
321 left->name ? left->name : "*",
322 (unsigned long) left->addr,
323 (unsigned long) left->end_addr,
252b5132 324 right->file ? right->file->name : "*",
fdcf7d43
ILT
325 right->name ? right->name : "*",
326 (unsigned long) right->addr,
327 (unsigned long) right->end_addr,
252b5132 328 table_name[id->which_table]));
0eee5820 329
252b5132
RH
330 arc_add (left, right, (unsigned long) 0);
331 }
332 }
333 }
334 }
335
ef368dac 336 /* Finally, we can sort the tables and we're done. */
252b5132
RH
337 for (tab = &syms[0]; tab < &syms[NUM_TABLES]; ++tab)
338 {
339 DBG (IDDEBUG, printf ("[sym_id_parse] syms[%s]:\n",
340 table_name[tab - &syms[0]]));
341 symtab_finalize (tab);
342 }
343}
344
345
ef368dac
NC
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. */
252b5132
RH
352bool
353DEFUN (sym_id_arc_is_present, (symtab, from, to),
354 Sym_Table * symtab AND Sym * from AND Sym * to)
355{
356 Sym *sym;
357
358 for (sym = symtab->base; sym < symtab->limit; ++sym)
359 {
360 if (from->addr >= sym->addr && from->addr <= sym->end_addr
361 && arc_lookup (sym, to))
ef368dac 362 return TRUE;
252b5132 363 }
0eee5820 364
252b5132
RH
365 return FALSE;
366}
This page took 0.106663 seconds and 4 git commands to generate.