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