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