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