Regenerate configure to include the gettext.m4 change. Update
[platform/upstream/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 "cg_arcs.h"
25 #include "sym_ids.h"
26
27 struct sym_id
28   {
29     struct sym_id *next;
30     char *spec;                 /* Parsing modifies this.  */
31     Table_Id which_table;
32     bool has_right;
33
34     struct match
35       {
36         int prev_index;         /* Index of prev match.  */
37         Sym *prev_match;        /* Previous match.  */
38         Sym *first_match;       /* Chain of all matches.  */
39         Sym sym;
40       }
41     left, right;
42   }
43  *id_list;
44
45 Sym_Table syms[NUM_TABLES];
46
47 #ifdef DEBUG
48 const 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
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).  */
64 static Sym_Table right_ids;
65
66 static Source_File non_existent_file =
67 {
68   0, "<non-existent-file>", 0, 0, 0, NULL
69 };
70
71
72 void
73 DEFUN (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
91 /* A spec has the syntax FILENAME:(FUNCNAME|LINENUM).  As a convenience
92    to the user, a spec without a colon is interpreted as:
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
98    A FUNCNAME containing a dot can be specified by :FUNCNAME, a
99    FILENAME not containing a dot can be specified by FILENAME.  */
100
101 static void
102 DEFUN (parse_spec, (spec, sym), char *spec AND Sym * sym)
103 {
104   char *colon;
105
106   sym_init (sym);
107   colon = strrchr (spec, ':');
108
109   if (colon)
110     {
111       *colon = '\0';
112
113       if (colon > spec)
114         {
115           sym->file = source_file_lookup_name (spec);
116
117           if (!sym->file)
118             sym->file = &non_existent_file;
119         }
120
121       spec = colon + 1;
122
123       if (strlen (spec))
124         {
125           if (ISDIGIT (spec[0]))
126             sym->line_num = atoi (spec);
127           else
128             sym->name = spec;
129         }
130     }
131   else if (strlen (spec))
132     {
133       /* No colon: spec is a filename if it contains a dot.  */
134       if (strchr (spec, '.'))
135         {
136           sym->file = source_file_lookup_name (spec);
137
138           if (!sym->file)
139             sym->file = &non_existent_file;
140         }
141       else if (ISDIGIT (*spec))
142         {
143           sym->line_num = atoi (spec);
144         }
145       else if (strlen (spec))
146         {
147           sym->name = spec;
148         }
149     }
150 }
151
152
153 /* A symbol id has the syntax SPEC[/SPEC], where SPEC is is defined
154    by parse_spec().  */
155
156 static void
157 DEFUN (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 : "*");
176
177       if (id->left.sym.name)
178         printf ("%s", id->left.sym.name);
179       else if (id->left.sym.line_num)
180         printf ("%d", id->left.sym.line_num);
181       else
182         printf ("*");
183
184       if (id->has_right)
185         {
186           printf ("/%s:",
187                   id->right.sym.file ? id->right.sym.file->name : "*");
188
189           if (id->right.sym.name)
190             printf ("%s", id->right.sym.name);
191           else if (id->right.sym.line_num)
192             printf ("%d", id->right.sym.line_num);
193           else
194             printf ("*");
195         }
196
197       printf ("\n");
198     }
199 #endif
200 }
201
202
203 /* Return TRUE iff PATTERN matches SYM.  */
204
205 static bool
206 DEFUN (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)
210     && (pattern->name
211         ? strcmp (pattern->name,
212                   sym->name+(discard_underscores && sym->name[0] == '_')) == 0
213         : TRUE);
214 }
215
216
217 static void
218 DEFUN (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     {
223       /* Discontinuity: add new match to table.  */
224       if (second_pass)
225         {
226           tab->base[tab->len] = *sym;
227           m->prev_index = tab->len;
228
229           /* Link match into match's chain.  */
230           tab->base[tab->len].next = m->first_match;
231           m->first_match = &tab->base[tab->len];
232         }
233
234       ++tab->len;
235     }
236
237   /* Extend match to include this symbol.  */
238   if (second_pass)
239     tab->base[m->prev_index].end_addr = sym->end_addr;
240
241   m->prev_match = sym;
242 }
243
244
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.
248
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
253 void
254 DEFUN_VOID (sym_id_parse)
255 {
256   Sym *sym, *left, *right;
257   struct sym_id *id;
258   Sym_Table *tab;
259
260   /* Convert symbol ids into Syms, so we can deal with them more easily.  */
261   for (id = id_list; id; id = id->next)
262     parse_id (id);
263
264   /* First determine size of each table.  */
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))
270             extend_match (&id->left, sym, &syms[id->which_table], FALSE);
271
272           if (id->has_right && match (&id->right.sym, sym))
273             extend_match (&id->right, sym, &right_ids, FALSE);
274         }
275     }
276
277   /* Create tables of appropriate size and reset lengths.  */
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     }
287
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
295   /* Make a second pass through symtab, creating syms as necessary.  */
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))
301             extend_match (&id->left, sym, &syms[id->which_table], TRUE);
302
303           if (id->has_right && match (&id->right.sym, sym))
304             extend_match (&id->right, sym, &right_ids, TRUE);
305         }
306     }
307
308   /* Go through ids creating arcs as needed.  */
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 : "*",
321                                 left->name ? left->name : "*",
322                                 (unsigned long) left->addr,
323                                 (unsigned long) left->end_addr,
324                                 right->file ? right->file->name : "*",
325                                 right->name ? right->name : "*",
326                                 (unsigned long) right->addr,
327                                 (unsigned long) right->end_addr,
328                                 table_name[id->which_table]));
329
330                   arc_add (left, right, (unsigned long) 0);
331                 }
332             }
333         }
334     }
335
336   /* Finally, we can sort the tables and we're done.  */
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
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.  */
352 bool
353 DEFUN (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))
362         return TRUE;
363     }
364
365   return FALSE;
366 }