daily update
[binutils.git] / gprof / corefile.c
blobe02d95833f4cdca8bbd8b63abb960b1152b2d3f9
1 /* corefile.c
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "libiberty.h"
24 #include "gprof.h"
25 #include "search_list.h"
26 #include "source.h"
27 #include "symtab.h"
28 #include "corefile.h"
30 bfd *core_bfd;
31 static int core_num_syms;
32 static asymbol **core_syms;
33 asection *core_text_sect;
34 PTR core_text_space;
36 static int min_insn_size;
37 int offset_to_code;
39 /* For mapping symbols to specific .o files during file ordering. */
40 struct function_map *symbol_map;
41 unsigned int symbol_map_count;
43 static void read_function_mappings (const char *);
44 static int core_sym_class (asymbol *);
45 static bfd_boolean get_src_info
46 (bfd_vma, const char **, const char **, int *);
48 extern void i386_find_call (Sym *, bfd_vma, bfd_vma);
49 extern void alpha_find_call (Sym *, bfd_vma, bfd_vma);
50 extern void vax_find_call (Sym *, bfd_vma, bfd_vma);
51 extern void tahoe_find_call (Sym *, bfd_vma, bfd_vma);
52 extern void sparc_find_call (Sym *, bfd_vma, bfd_vma);
53 extern void mips_find_call (Sym *, bfd_vma, bfd_vma);
55 static void
56 read_function_mappings (const char *filename)
58 FILE *file = fopen (filename, "r");
59 char dummy[1024];
60 int count = 0;
62 if (!file)
64 fprintf (stderr, _("%s: could not open %s.\n"), whoami, filename);
65 done (1);
68 /* First parse the mapping file so we know how big we need to
69 make our tables. We also do some sanity checks at this
70 time. */
71 while (!feof (file))
73 int matches;
75 matches = fscanf (file, "%[^\n:]", dummy);
76 if (!matches)
78 fprintf (stderr, _("%s: unable to parse mapping file %s.\n"),
79 whoami, filename);
80 done (1);
83 /* Just skip messages about files with no symbols. */
84 if (!strncmp (dummy, "No symbols in ", 14))
86 fscanf (file, "\n");
87 continue;
90 /* Don't care what else is on this line at this point. */
91 fscanf (file, "%[^\n]\n", dummy);
92 count++;
95 /* Now we know how big we need to make our table. */
96 symbol_map = ((struct function_map *)
97 xmalloc (count * sizeof (struct function_map)));
99 /* Rewind the input file so we can read it again. */
100 rewind (file);
102 /* Read each entry and put it into the table. */
103 count = 0;
104 while (!feof (file))
106 int matches;
107 char *tmp;
109 matches = fscanf (file, "%[^\n:]", dummy);
110 if (!matches)
112 fprintf (stderr, _("%s: unable to parse mapping file %s.\n"),
113 whoami, filename);
114 done (1);
117 /* Just skip messages about files with no symbols. */
118 if (!strncmp (dummy, "No symbols in ", 14))
120 fscanf (file, "\n");
121 continue;
124 /* dummy has the filename, go ahead and copy it. */
125 symbol_map[count].file_name = xmalloc (strlen (dummy) + 1);
126 strcpy (symbol_map[count].file_name, dummy);
128 /* Now we need the function name. */
129 fscanf (file, "%[^\n]\n", dummy);
130 tmp = strrchr (dummy, ' ') + 1;
131 symbol_map[count].function_name = xmalloc (strlen (tmp) + 1);
132 strcpy (symbol_map[count].function_name, tmp);
133 count++;
136 /* Record the size of the map table for future reference. */
137 symbol_map_count = count;
141 void
142 core_init (const char *aout_name)
144 int core_sym_bytes;
145 asymbol *synthsyms;
146 long synth_count;
148 core_bfd = bfd_openr (aout_name, 0);
150 if (!core_bfd)
152 perror (aout_name);
153 done (1);
156 if (!bfd_check_format (core_bfd, bfd_object))
158 fprintf (stderr, _("%s: %s: not in a.out format\n"), whoami, aout_name);
159 done (1);
162 /* Get core's text section. */
163 core_text_sect = bfd_get_section_by_name (core_bfd, ".text");
164 if (!core_text_sect)
166 core_text_sect = bfd_get_section_by_name (core_bfd, "$CODE$");
167 if (!core_text_sect)
169 fprintf (stderr, _("%s: can't find .text section in %s\n"),
170 whoami, aout_name);
171 done (1);
175 /* Read core's symbol table. */
177 /* This will probably give us more than we need, but that's ok. */
178 core_sym_bytes = bfd_get_symtab_upper_bound (core_bfd);
179 if (core_sym_bytes < 0)
181 fprintf (stderr, "%s: %s: %s\n", whoami, aout_name,
182 bfd_errmsg (bfd_get_error ()));
183 done (1);
186 core_syms = (asymbol **) xmalloc (core_sym_bytes);
187 core_num_syms = bfd_canonicalize_symtab (core_bfd, core_syms);
189 if (core_num_syms < 0)
191 fprintf (stderr, "%s: %s: %s\n", whoami, aout_name,
192 bfd_errmsg (bfd_get_error ()));
193 done (1);
196 synth_count = bfd_get_synthetic_symtab (core_bfd, core_num_syms, core_syms,
197 0, NULL, &synthsyms);
198 if (synth_count > 0)
200 asymbol **symp;
201 long new_size;
202 long i;
204 new_size = (core_num_syms + synth_count + 1) * sizeof (*core_syms);
205 core_syms = xrealloc (core_syms, new_size);
206 symp = core_syms + core_num_syms;
207 core_num_syms += synth_count;
208 for (i = 0; i < synth_count; i++)
209 *symp++ = synthsyms + i;
210 *symp = 0;
213 min_insn_size = 1;
214 offset_to_code = 0;
216 switch (bfd_get_arch (core_bfd))
218 case bfd_arch_vax:
219 case bfd_arch_tahoe:
220 offset_to_code = 2;
221 break;
223 case bfd_arch_alpha:
224 min_insn_size = 4;
225 break;
227 default:
228 break;
231 if (function_mapping_file)
232 read_function_mappings (function_mapping_file);
235 /* Read in the text space of an a.out file. */
237 void
238 core_get_text_space (bfd *cbfd)
240 core_text_space = malloc (bfd_get_section_size (core_text_sect));
242 if (!core_text_space)
244 fprintf (stderr, _("%s: ran out room for %lu bytes of text space\n"),
245 whoami, (unsigned long) bfd_get_section_size (core_text_sect));
246 done (1);
249 if (!bfd_get_section_contents (cbfd, core_text_sect, core_text_space,
250 0, bfd_get_section_size (core_text_sect)))
252 bfd_perror ("bfd_get_section_contents");
253 free (core_text_space);
254 core_text_space = 0;
257 if (!core_text_space)
258 fprintf (stderr, _("%s: can't do -c\n"), whoami);
262 void
263 find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
265 switch (bfd_get_arch (core_bfd))
267 case bfd_arch_i386:
268 i386_find_call (parent, p_lowpc, p_highpc);
269 break;
271 case bfd_arch_alpha:
272 alpha_find_call (parent, p_lowpc, p_highpc);
273 break;
275 case bfd_arch_vax:
276 vax_find_call (parent, p_lowpc, p_highpc);
277 break;
279 case bfd_arch_sparc:
280 sparc_find_call (parent, p_lowpc, p_highpc);
281 break;
283 case bfd_arch_tahoe:
284 tahoe_find_call (parent, p_lowpc, p_highpc);
285 break;
287 case bfd_arch_mips:
288 mips_find_call (parent, p_lowpc, p_highpc);
289 break;
291 default:
292 fprintf (stderr, _("%s: -c not supported on architecture %s\n"),
293 whoami, bfd_printable_name(core_bfd));
295 /* Don't give the error more than once. */
296 ignore_direct_calls = FALSE;
300 /* Return class of symbol SYM. The returned class can be any of:
301 0 -> symbol is not interesting to us
302 'T' -> symbol is a global name
303 't' -> symbol is a local (static) name. */
305 static int
306 core_sym_class (asymbol *sym)
308 symbol_info syminfo;
309 const char *name;
310 char sym_prefix;
311 int i;
313 if (sym->section == NULL || (sym->flags & BSF_DEBUGGING) != 0)
314 return 0;
316 /* Must be a text symbol, and static text symbols
317 don't qualify if ignore_static_funcs set. */
318 if (ignore_static_funcs && (sym->flags & BSF_LOCAL))
320 DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n",
321 sym->name));
322 return 0;
325 bfd_get_symbol_info (core_bfd, sym, &syminfo);
326 i = syminfo.type;
328 if (i == 'T')
329 return i; /* It's a global symbol. */
331 if (i == 'W')
332 /* Treat weak symbols as text symbols. FIXME: a weak symbol may
333 also be a data symbol. */
334 return 'T';
336 if (i != 't')
338 /* Not a static text symbol. */
339 DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n",
340 sym->name, i));
341 return 0;
344 /* Do some more filtering on static function-names. */
345 if (ignore_static_funcs)
346 return 0;
348 /* Can't zero-length name or funny characters in name, where
349 `funny' includes: `.' (.o file names) and `$' (Pascal labels). */
350 if (!sym->name || sym->name[0] == '\0')
351 return 0;
353 for (name = sym->name; *name; ++name)
355 if (*name == '.' || *name == '$')
356 return 0;
359 /* On systems where the C compiler adds an underscore to all
360 names, static names without underscores seem usually to be
361 labels in hand written assembler in the library. We don't want
362 these names. This is certainly necessary on a Sparc running
363 SunOS 4.1 (try profiling a program that does a lot of
364 division). I don't know whether it has harmful side effects on
365 other systems. Perhaps it should be made configurable. */
366 sym_prefix = bfd_get_symbol_leading_char (core_bfd);
368 if ((sym_prefix && sym_prefix != sym->name[0])
369 /* GCC may add special symbols to help gdb figure out the file
370 language. We want to ignore these, since sometimes they mask
371 the real function. (dj@ctron) */
372 || !strncmp (sym->name, "__gnu_compiled", 14)
373 || !strncmp (sym->name, "___gnu_compiled", 15))
375 return 0;
378 /* If the object file supports marking of function symbols, then
379 we can zap anything that doesn't have BSF_FUNCTION set. */
380 if (ignore_non_functions && (sym->flags & BSF_FUNCTION) == 0)
381 return 0;
383 return 't'; /* It's a static text symbol. */
386 /* Get whatever source info we can get regarding address ADDR. */
388 static bfd_boolean
389 get_src_info (bfd_vma addr, const char **filename, const char **name, int *line_num)
391 const char *fname = 0, *func_name = 0;
392 int l = 0;
394 if (bfd_find_nearest_line (core_bfd, core_text_sect, core_syms,
395 addr - core_text_sect->vma,
396 &fname, &func_name, (unsigned int *) &l)
397 && fname && func_name && l)
399 DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
400 (unsigned long) addr, fname, l, func_name));
401 *filename = fname;
402 *name = func_name;
403 *line_num = l;
404 return TRUE;
406 else
408 DBG (AOUTDEBUG, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
409 (long) addr, fname ? fname : "<unknown>", l,
410 func_name ? func_name : "<unknown>"));
411 return FALSE;
415 /* Read in symbol table from core.
416 One symbol per function is entered. */
418 void
419 core_create_function_syms ()
421 bfd_vma min_vma = ~(bfd_vma) 0;
422 bfd_vma max_vma = 0;
423 int class;
424 long i, found, skip;
425 unsigned int j;
427 /* Pass 1 - determine upper bound on number of function names. */
428 symtab.len = 0;
430 for (i = 0; i < core_num_syms; ++i)
432 if (!core_sym_class (core_syms[i]))
433 continue;
435 /* This should be replaced with a binary search or hashed
436 search. Gross.
438 Don't create a symtab entry for a function that has
439 a mapping to a file, unless it's the first function
440 in the file. */
441 skip = 0;
442 for (j = 0; j < symbol_map_count; j++)
443 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
445 if (j > 0 && ! strcmp (symbol_map [j].file_name,
446 symbol_map [j - 1].file_name))
447 skip = 1;
448 break;
451 if (!skip)
452 ++symtab.len;
455 if (symtab.len == 0)
457 fprintf (stderr, _("%s: file `%s' has no symbols\n"), whoami, a_out_name);
458 done (1);
461 /* The "+ 2" is for the sentinels. */
462 symtab.base = (Sym *) xmalloc ((symtab.len + 2) * sizeof (Sym));
464 /* Pass 2 - create symbols. */
465 symtab.limit = symtab.base;
467 for (i = 0; i < core_num_syms; ++i)
469 asection *sym_sec;
471 class = core_sym_class (core_syms[i]);
473 if (!class)
475 DBG (AOUTDEBUG,
476 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
477 (unsigned long) core_syms[i]->value,
478 core_syms[i]->name));
479 continue;
482 /* This should be replaced with a binary search or hashed
483 search. Gross. */
484 skip = 0;
485 found = 0;
487 for (j = 0; j < symbol_map_count; j++)
488 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
490 if (j > 0 && ! strcmp (symbol_map [j].file_name,
491 symbol_map [j - 1].file_name))
492 skip = 1;
493 else
494 found = j;
495 break;
498 if (skip)
499 continue;
501 sym_init (symtab.limit);
503 /* Symbol offsets are always section-relative. */
504 sym_sec = core_syms[i]->section;
505 symtab.limit->addr = core_syms[i]->value;
506 if (sym_sec)
507 symtab.limit->addr += bfd_get_section_vma (sym_sec->owner, sym_sec);
509 if (symbol_map_count
510 && !strcmp (core_syms[i]->name, symbol_map[found].function_name))
512 symtab.limit->name = symbol_map[found].file_name;
513 symtab.limit->mapped = 1;
515 else
517 symtab.limit->name = core_syms[i]->name;
518 symtab.limit->mapped = 0;
521 /* Lookup filename and line number, if we can. */
523 const char *filename, *func_name;
525 if (get_src_info (symtab.limit->addr, &filename, &func_name,
526 &symtab.limit->line_num))
528 symtab.limit->file = source_file_lookup_path (filename);
530 /* FIXME: Checking __osf__ here does not work with a cross
531 gprof. */
532 #ifdef __osf__
533 /* Suppress symbols that are not function names. This is
534 useful to suppress code-labels and aliases.
536 This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
537 labels do not appear in the symbol table info, so this isn't
538 necessary. */
540 if (strcmp (symtab.limit->name, func_name) != 0)
542 /* The symbol's address maps to a different name, so
543 it can't be a function-entry point. This happens
544 for labels, for example. */
545 DBG (AOUTDEBUG,
546 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
547 symtab.limit->name, func_name));
548 continue;
550 #endif
554 symtab.limit->is_func = TRUE;
555 symtab.limit->is_bb_head = TRUE;
557 if (class == 't')
558 symtab.limit->is_static = TRUE;
560 /* Keep track of the minimum and maximum vma addresses used by all
561 symbols. When computing the max_vma, use the ending address of the
562 section containing the symbol, if available. */
563 min_vma = MIN (symtab.limit->addr, min_vma);
564 if (sym_sec)
565 max_vma = MAX (bfd_get_section_vma (sym_sec->owner, sym_sec)
566 + bfd_section_size (sym_sec->owner, sym_sec) - 1,
567 max_vma);
568 else
569 max_vma = MAX (symtab.limit->addr, max_vma);
571 /* If we see "main" without an initial '_', we assume names
572 are *not* prefixed by '_'. */
573 if (symtab.limit->name[0] == 'm' && discard_underscores
574 && strcmp (symtab.limit->name, "main") == 0)
575 discard_underscores = 0;
577 DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
578 (long) (symtab.limit - symtab.base),
579 symtab.limit->name,
580 (unsigned long) symtab.limit->addr));
581 ++symtab.limit;
584 /* Create sentinels. */
585 sym_init (symtab.limit);
586 symtab.limit->name = "<locore>";
587 symtab.limit->addr = 0;
588 symtab.limit->end_addr = min_vma - 1;
589 ++symtab.limit;
591 sym_init (symtab.limit);
592 symtab.limit->name = "<hicore>";
593 symtab.limit->addr = max_vma + 1;
594 symtab.limit->end_addr = ~(bfd_vma) 0;
595 ++symtab.limit;
597 symtab.len = symtab.limit - symtab.base;
598 symtab_finalize (&symtab);
601 /* Read in symbol table from core.
602 One symbol per line of source code is entered. */
604 void
605 core_create_line_syms ()
607 char *prev_name, *prev_filename;
608 unsigned int prev_name_len, prev_filename_len;
609 bfd_vma vma, min_vma = ~(bfd_vma) 0, max_vma = 0;
610 Sym *prev, dummy, *sentinel, *sym;
611 const char *filename;
612 int prev_line_num;
613 Sym_Table ltab;
614 bfd_vma vma_high;
616 /* Create symbols for functions as usual. This is necessary in
617 cases where parts of a program were not compiled with -g. For
618 those parts we still want to get info at the function level. */
619 core_create_function_syms ();
621 /* Pass 1: count the number of symbols. */
623 /* To find all line information, walk through all possible
624 text-space addresses (one by one!) and get the debugging
625 info for each address. When the debugging info changes,
626 it is time to create a new symbol.
628 Of course, this is rather slow and it would be better if
629 BFD would provide an iterator for enumerating all line infos. */
630 prev_name_len = PATH_MAX;
631 prev_filename_len = PATH_MAX;
632 prev_name = xmalloc (prev_name_len);
633 prev_filename = xmalloc (prev_filename_len);
634 ltab.len = 0;
635 prev_line_num = 0;
637 vma_high = core_text_sect->vma + bfd_get_section_size (core_text_sect);
638 for (vma = core_text_sect->vma; vma < vma_high; vma += min_insn_size)
640 unsigned int len;
642 if (!get_src_info (vma, &filename, &dummy.name, &dummy.line_num)
643 || (prev_line_num == dummy.line_num
644 && prev_name != NULL
645 && strcmp (prev_name, dummy.name) == 0
646 && strcmp (prev_filename, filename) == 0))
647 continue;
649 ++ltab.len;
650 prev_line_num = dummy.line_num;
652 len = strlen (dummy.name);
653 if (len >= prev_name_len)
655 prev_name_len = len + 1024;
656 free (prev_name);
657 prev_name = xmalloc (prev_name_len);
660 strcpy (prev_name, dummy.name);
661 len = strlen (filename);
663 if (len >= prev_filename_len)
665 prev_filename_len = len + 1024;
666 free (prev_filename);
667 prev_filename = xmalloc (prev_filename_len);
670 strcpy (prev_filename, filename);
672 min_vma = MIN (vma, min_vma);
673 max_vma = MAX (vma, max_vma);
676 free (prev_name);
677 free (prev_filename);
679 /* Make room for function symbols, too. */
680 ltab.len += symtab.len;
681 ltab.base = (Sym *) xmalloc (ltab.len * sizeof (Sym));
682 ltab.limit = ltab.base;
684 /* Pass 2 - create symbols. */
686 /* We now set is_static as we go along, rather than by running
687 through the symbol table at the end.
689 The old way called symtab_finalize before the is_static pass,
690 causing a problem since symtab_finalize uses is_static as part of
691 its address conflict resolution algorithm. Since global symbols
692 were prefered over static symbols, and all line symbols were
693 global at that point, static function names that conflicted with
694 their own line numbers (static, but labeled as global) were
695 rejected in favor of the line num.
697 This was not the desired functionality. We always want to keep
698 our function symbols and discard any conflicting line symbols.
699 Perhaps symtab_finalize should be modified to make this
700 distinction as well, but the current fix works and the code is a
701 lot cleaner now. */
702 prev = 0;
704 for (vma = core_text_sect->vma; vma < vma_high; vma += min_insn_size)
706 sym_init (ltab.limit);
708 if (!get_src_info (vma, &filename, &ltab.limit->name, &ltab.limit->line_num)
709 || (prev && prev->line_num == ltab.limit->line_num
710 && strcmp (prev->name, ltab.limit->name) == 0
711 && strcmp (prev->file->name, filename) == 0))
712 continue;
714 /* Make name pointer a malloc'ed string. */
715 ltab.limit->name = xstrdup (ltab.limit->name);
716 ltab.limit->file = source_file_lookup_path (filename);
718 ltab.limit->addr = vma;
720 /* Set is_static based on the enclosing function, using either:
721 1) the previous symbol, if it's from the same function, or
722 2) a symtab lookup. */
723 if (prev && ltab.limit->file == prev->file &&
724 strcmp (ltab.limit->name, prev->name) == 0)
726 ltab.limit->is_static = prev->is_static;
728 else
730 sym = sym_lookup(&symtab, ltab.limit->addr);
731 ltab.limit->is_static = sym->is_static;
734 prev = ltab.limit;
736 /* If we see "main" without an initial '_', we assume names
737 are *not* prefixed by '_'. */
738 if (ltab.limit->name[0] == 'm' && discard_underscores
739 && strcmp (ltab.limit->name, "main") == 0)
740 discard_underscores = 0;
742 DBG (AOUTDEBUG, printf ("[core_create_line_syms] %lu %s 0x%lx\n",
743 (unsigned long) (ltab.limit - ltab.base),
744 ltab.limit->name,
745 (unsigned long) ltab.limit->addr));
746 ++ltab.limit;
749 /* Update sentinels. */
750 sentinel = sym_lookup (&symtab, (bfd_vma) 0);
752 if (sentinel
753 && strcmp (sentinel->name, "<locore>") == 0
754 && min_vma <= sentinel->end_addr)
755 sentinel->end_addr = min_vma - 1;
757 sentinel = sym_lookup (&symtab, ~(bfd_vma) 0);
759 if (sentinel
760 && strcmp (sentinel->name, "<hicore>") == 0
761 && max_vma >= sentinel->addr)
762 sentinel->addr = max_vma + 1;
764 /* Copy in function symbols. */
765 memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym));
766 ltab.limit += symtab.len;
768 if ((unsigned int) (ltab.limit - ltab.base) != ltab.len)
770 fprintf (stderr,
771 _("%s: somebody miscounted: ltab.len=%d instead of %ld\n"),
772 whoami, ltab.len, (long) (ltab.limit - ltab.base));
773 done (1);
776 /* Finalize ltab and make it symbol table. */
777 symtab_finalize (&ltab);
778 free (symtab.base);
779 symtab = ltab;