1 /* sym_ids.c
2 
3    Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
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 
22 #include "libiberty.h"
23 #include "safe-ctype.h"
24 #include "gprof.h"
25 #include "search_list.h"
26 #include "source.h"
27 #include "symtab.h"
28 #include "cg_arcs.h"
29 #include "sym_ids.h"
30 
31 struct sym_id
32   {
33     struct sym_id *next;
34     char *spec;			/* Parsing modifies this.  */
35     Table_Id which_table;
36     bfd_boolean has_right;
37 
38     struct match
39       {
40 	int prev_index;		/* Index of prev match.  */
41 	Sym *prev_match;	/* Previous match.  */
42 	Sym *first_match;	/* Chain of all matches.  */
43 	Sym sym;
44       }
45     left, right;
46   }
47  *id_list;
48 
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));
57 
58 
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 
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).  */
78 static Sym_Table right_ids;
79 
80 static Source_File non_existent_file =
81 {
82   0, "<non-existent-file>", 0, 0, 0, NULL
83 };
84 
85 
86 void
sym_id_add(spec,which_table)87 sym_id_add (spec, which_table)
88      const char *spec;
89      Table_Id which_table;
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 
106 /* A spec has the syntax FILENAME:(FUNCNAME|LINENUM).  As a convenience
107    to the user, a spec without a colon is interpreted as:
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 
113    A FUNCNAME containing a dot can be specified by :FUNCNAME, a
114    FILENAME not containing a dot can be specified by FILENAME.  */
115 
116 static void
parse_spec(spec,sym)117 parse_spec (spec, sym)
118      char *spec;
119      Sym *sym;
120 {
121   char *colon;
122 
123   sym_init (sym);
124   colon = strrchr (spec, ':');
125 
126   if (colon)
127     {
128       *colon = '\0';
129 
130       if (colon > spec)
131 	{
132 	  sym->file = source_file_lookup_name (spec);
133 
134 	  if (!sym->file)
135 	    sym->file = &non_existent_file;
136 	}
137 
138       spec = colon + 1;
139 
140       if (strlen (spec))
141 	{
142 	  if (ISDIGIT (spec[0]))
143 	    sym->line_num = atoi (spec);
144 	  else
145 	    sym->name = spec;
146 	}
147     }
148   else if (strlen (spec))
149     {
150       /* No colon: spec is a filename if it contains a dot.  */
151       if (strchr (spec, '.'))
152 	{
153 	  sym->file = source_file_lookup_name (spec);
154 
155 	  if (!sym->file)
156 	    sym->file = &non_existent_file;
157 	}
158       else if (ISDIGIT (*spec))
159 	{
160 	  sym->line_num = atoi (spec);
161 	}
162       else if (strlen (spec))
163 	{
164 	  sym->name = spec;
165 	}
166     }
167 }
168 
169 
170 /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
171    by parse_spec().  */
172 
173 static void
parse_id(id)174 parse_id (id)
175      struct sym_id *id;
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';
186       id->has_right = TRUE;
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 : "*");
194 
195       if (id->left.sym.name)
196 	printf ("%s", id->left.sym.name);
197       else if (id->left.sym.line_num)
198 	printf ("%d", id->left.sym.line_num);
199       else
200 	printf ("*");
201 
202       if (id->has_right)
203 	{
204 	  printf ("/%s:",
205 		  id->right.sym.file ? id->right.sym.file->name : "*");
206 
207 	  if (id->right.sym.name)
208 	    printf ("%s", id->right.sym.name);
209 	  else if (id->right.sym.line_num)
210 	    printf ("%d", id->right.sym.line_num);
211 	  else
212 	    printf ("*");
213 	}
214 
215       printf ("\n");
216     }
217 #endif
218 }
219 
220 
221 /* Return TRUE iff PATTERN matches SYM.  */
222 
223 static bfd_boolean
match(pattern,sym)224 match (pattern, sym)
225      Sym *pattern;
226      Sym *sym;
227 {
228   return (pattern->file ? pattern->file == sym->file : TRUE)
229     && (pattern->line_num ? pattern->line_num == sym->line_num : TRUE)
230     && (pattern->name
231 	? strcmp (pattern->name,
232 		  sym->name+(discard_underscores && sym->name[0] == '_')) == 0
233 	: TRUE);
234 }
235 
236 
237 static void
extend_match(m,sym,tab,second_pass)238 extend_match (m, sym, tab, second_pass)
239      struct match *m;
240      Sym *sym;
241      Sym_Table *tab;
242      bfd_boolean second_pass;
243 {
244   if (m->prev_match != sym - 1)
245     {
246       /* Discontinuity: add new match to table.  */
247       if (second_pass)
248 	{
249 	  tab->base[tab->len] = *sym;
250 	  m->prev_index = tab->len;
251 
252 	  /* Link match into match's chain.  */
253 	  tab->base[tab->len].next = m->first_match;
254 	  m->first_match = &tab->base[tab->len];
255 	}
256 
257       ++tab->len;
258     }
259 
260   /* Extend match to include this symbol.  */
261   if (second_pass)
262     tab->base[m->prev_index].end_addr = sym->end_addr;
263 
264   m->prev_match = sym;
265 }
266 
267 
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.
271 
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 
276 void
sym_id_parse()277 sym_id_parse ()
278 {
279   Sym *sym, *left, *right;
280   struct sym_id *id;
281   Sym_Table *tab;
282 
283   /* Convert symbol ids into Syms, so we can deal with them more easily.  */
284   for (id = id_list; id; id = id->next)
285     parse_id (id);
286 
287   /* First determine size of each table.  */
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))
293 	    extend_match (&id->left, sym, &syms[id->which_table], FALSE);
294 
295 	  if (id->has_right && match (&id->right.sym, sym))
296 	    extend_match (&id->right, sym, &right_ids, FALSE);
297 	}
298     }
299 
300   /* Create tables of appropriate size and reset lengths.  */
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     }
310 
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 
318   /* Make a second pass through symtab, creating syms as necessary.  */
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))
324 	    extend_match (&id->left, sym, &syms[id->which_table], TRUE);
325 
326 	  if (id->has_right && match (&id->right.sym, sym))
327 	    extend_match (&id->right, sym, &right_ids, TRUE);
328 	}
329     }
330 
331   /* Go through ids creating arcs as needed.  */
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 : "*",
344 				left->name ? left->name : "*",
345 				(unsigned long) left->addr,
346 				(unsigned long) left->end_addr,
347 				right->file ? right->file->name : "*",
348 				right->name ? right->name : "*",
349 				(unsigned long) right->addr,
350 				(unsigned long) right->end_addr,
351 				table_name[id->which_table]));
352 
353 		  arc_add (left, right, (unsigned long) 0);
354 		}
355 	    }
356 	}
357     }
358 
359   /* Finally, we can sort the tables and we're done.  */
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 
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.  */
375 bfd_boolean
sym_id_arc_is_present(sym_tab,from,to)376 sym_id_arc_is_present (sym_tab, from, to)
377      Sym_Table *sym_tab;
378      Sym *from;
379      Sym *to;
380 {
381   Sym *sym;
382 
383   for (sym = sym_tab->base; sym < sym_tab->limit; ++sym)
384     {
385       if (from->addr >= sym->addr && from->addr <= sym->end_addr
386 	  && arc_lookup (sym, to))
387 	return TRUE;
388     }
389 
390   return FALSE;
391 }
392