2006-12-01 Paul Brook <paul@codesourcery.com>
[binutils.git] / gprof / corefile.c
blobf2350a1f765f56cd253972b7066ecb14dd34e0a4
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 "hist.h"
29 #include "corefile.h"
31 bfd *core_bfd;
32 static int core_num_syms;
33 static asymbol **core_syms;
34 asection *core_text_sect;
35 PTR core_text_space;
37 static int min_insn_size;
38 int offset_to_code;
40 /* For mapping symbols to specific .o files during file ordering. */
41 struct function_map *symbol_map;
42 unsigned int symbol_map_count;
44 static void read_function_mappings (const char *);
45 static int core_sym_class (asymbol *);
46 static bfd_boolean get_src_info
47 (bfd_vma, const char **, const char **, int *);
49 extern void i386_find_call (Sym *, bfd_vma, bfd_vma);
50 extern void alpha_find_call (Sym *, bfd_vma, bfd_vma);
51 extern void vax_find_call (Sym *, bfd_vma, bfd_vma);
52 extern void tahoe_find_call (Sym *, bfd_vma, bfd_vma);
53 extern void sparc_find_call (Sym *, bfd_vma, bfd_vma);
54 extern void mips_find_call (Sym *, bfd_vma, bfd_vma);
56 static void
57 read_function_mappings (const char *filename)
59 FILE *file = fopen (filename, "r");
60 char dummy[1024];
61 int count = 0;
63 if (!file)
65 fprintf (stderr, _("%s: could not open %s.\n"), whoami, filename);
66 done (1);
69 /* First parse the mapping file so we know how big we need to
70 make our tables. We also do some sanity checks at this
71 time. */
72 while (!feof (file))
74 int matches;
76 matches = fscanf (file, "%[^\n:]", dummy);
77 if (!matches)
79 fprintf (stderr, _("%s: unable to parse mapping file %s.\n"),
80 whoami, filename);
81 done (1);
84 /* Just skip messages about files with no symbols. */
85 if (!strncmp (dummy, "No symbols in ", 14))
87 fscanf (file, "\n");
88 continue;
91 /* Don't care what else is on this line at this point. */
92 fscanf (file, "%[^\n]\n", dummy);
93 count++;
96 /* Now we know how big we need to make our table. */
97 symbol_map = ((struct function_map *)
98 xmalloc (count * sizeof (struct function_map)));
100 /* Rewind the input file so we can read it again. */
101 rewind (file);
103 /* Read each entry and put it into the table. */
104 count = 0;
105 while (!feof (file))
107 int matches;
108 char *tmp;
110 matches = fscanf (file, "%[^\n:]", dummy);
111 if (!matches)
113 fprintf (stderr, _("%s: unable to parse mapping file %s.\n"),
114 whoami, filename);
115 done (1);
118 /* Just skip messages about files with no symbols. */
119 if (!strncmp (dummy, "No symbols in ", 14))
121 fscanf (file, "\n");
122 continue;
125 /* dummy has the filename, go ahead and copy it. */
126 symbol_map[count].file_name = xmalloc (strlen (dummy) + 1);
127 strcpy (symbol_map[count].file_name, dummy);
129 /* Now we need the function name. */
130 fscanf (file, "%[^\n]\n", dummy);
131 tmp = strrchr (dummy, ' ') + 1;
132 symbol_map[count].function_name = xmalloc (strlen (tmp) + 1);
133 strcpy (symbol_map[count].function_name, tmp);
134 count++;
137 /* Record the size of the map table for future reference. */
138 symbol_map_count = count;
142 void
143 core_init (const char *aout_name)
145 int core_sym_bytes;
146 asymbol *synthsyms;
147 long synth_count;
149 core_bfd = bfd_openr (aout_name, 0);
151 if (!core_bfd)
153 perror (aout_name);
154 done (1);
157 if (!bfd_check_format (core_bfd, bfd_object))
159 fprintf (stderr, _("%s: %s: not in executable format\n"), whoami, aout_name);
160 done (1);
163 /* Get core's text section. */
164 core_text_sect = bfd_get_section_by_name (core_bfd, ".text");
165 if (!core_text_sect)
167 core_text_sect = bfd_get_section_by_name (core_bfd, "$CODE$");
168 if (!core_text_sect)
170 fprintf (stderr, _("%s: can't find .text section in %s\n"),
171 whoami, aout_name);
172 done (1);
176 /* Read core's symbol table. */
178 /* This will probably give us more than we need, but that's ok. */
179 core_sym_bytes = bfd_get_symtab_upper_bound (core_bfd);
180 if (core_sym_bytes < 0)
182 fprintf (stderr, "%s: %s: %s\n", whoami, aout_name,
183 bfd_errmsg (bfd_get_error ()));
184 done (1);
187 core_syms = (asymbol **) xmalloc (core_sym_bytes);
188 core_num_syms = bfd_canonicalize_symtab (core_bfd, core_syms);
190 if (core_num_syms < 0)
192 fprintf (stderr, "%s: %s: %s\n", whoami, aout_name,
193 bfd_errmsg (bfd_get_error ()));
194 done (1);
197 synth_count = bfd_get_synthetic_symtab (core_bfd, core_num_syms, core_syms,
198 0, NULL, &synthsyms);
199 if (synth_count > 0)
201 asymbol **symp;
202 long new_size;
203 long i;
205 new_size = (core_num_syms + synth_count + 1) * sizeof (*core_syms);
206 core_syms = xrealloc (core_syms, new_size);
207 symp = core_syms + core_num_syms;
208 core_num_syms += synth_count;
209 for (i = 0; i < synth_count; i++)
210 *symp++ = synthsyms + i;
211 *symp = 0;
214 min_insn_size = 1;
215 offset_to_code = 0;
217 switch (bfd_get_arch (core_bfd))
219 case bfd_arch_vax:
220 case bfd_arch_tahoe:
221 offset_to_code = 2;
222 break;
224 case bfd_arch_alpha:
225 min_insn_size = 4;
226 break;
228 default:
229 break;
232 if (function_mapping_file)
233 read_function_mappings (function_mapping_file);
236 /* Read in the text space of an a.out file. */
238 void
239 core_get_text_space (bfd *cbfd)
241 core_text_space = malloc (bfd_get_section_size (core_text_sect));
243 if (!core_text_space)
245 fprintf (stderr, _("%s: ran out room for %lu bytes of text space\n"),
246 whoami, (unsigned long) bfd_get_section_size (core_text_sect));
247 done (1);
250 if (!bfd_get_section_contents (cbfd, core_text_sect, core_text_space,
251 0, bfd_get_section_size (core_text_sect)))
253 bfd_perror ("bfd_get_section_contents");
254 free (core_text_space);
255 core_text_space = 0;
258 if (!core_text_space)
259 fprintf (stderr, _("%s: can't do -c\n"), whoami);
263 void
264 find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
266 if (core_text_space == 0)
267 return;
269 hist_clip_symbol_address (&p_lowpc, &p_highpc);
271 switch (bfd_get_arch (core_bfd))
273 case bfd_arch_i386:
274 i386_find_call (parent, p_lowpc, p_highpc);
275 break;
277 case bfd_arch_alpha:
278 alpha_find_call (parent, p_lowpc, p_highpc);
279 break;
281 case bfd_arch_vax:
282 vax_find_call (parent, p_lowpc, p_highpc);
283 break;
285 case bfd_arch_sparc:
286 sparc_find_call (parent, p_lowpc, p_highpc);
287 break;
289 case bfd_arch_tahoe:
290 tahoe_find_call (parent, p_lowpc, p_highpc);
291 break;
293 case bfd_arch_mips:
294 mips_find_call (parent, p_lowpc, p_highpc);
295 break;
297 default:
298 fprintf (stderr, _("%s: -c not supported on architecture %s\n"),
299 whoami, bfd_printable_name(core_bfd));
301 /* Don't give the error more than once. */
302 ignore_direct_calls = FALSE;
306 /* Return class of symbol SYM. The returned class can be any of:
307 0 -> symbol is not interesting to us
308 'T' -> symbol is a global name
309 't' -> symbol is a local (static) name. */
311 static int
312 core_sym_class (asymbol *sym)
314 symbol_info syminfo;
315 const char *name;
316 char sym_prefix;
317 int i;
319 if (sym->section == NULL || (sym->flags & BSF_DEBUGGING) != 0)
320 return 0;
322 /* Must be a text symbol, and static text symbols
323 don't qualify if ignore_static_funcs set. */
324 if (ignore_static_funcs && (sym->flags & BSF_LOCAL))
326 DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n",
327 sym->name));
328 return 0;
331 bfd_get_symbol_info (core_bfd, sym, &syminfo);
332 i = syminfo.type;
334 if (i == 'T')
335 return i; /* It's a global symbol. */
337 if (i == 'W')
338 /* Treat weak symbols as text symbols. FIXME: a weak symbol may
339 also be a data symbol. */
340 return 'T';
342 if (i != 't')
344 /* Not a static text symbol. */
345 DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n",
346 sym->name, i));
347 return 0;
350 /* Do some more filtering on static function-names. */
351 if (ignore_static_funcs)
352 return 0;
354 /* Can't zero-length name or funny characters in name, where
355 `funny' includes: `.' (.o file names) and `$' (Pascal labels). */
356 if (!sym->name || sym->name[0] == '\0')
357 return 0;
359 for (name = sym->name; *name; ++name)
361 if (*name == '.' || *name == '$')
362 return 0;
365 /* On systems where the C compiler adds an underscore to all
366 names, static names without underscores seem usually to be
367 labels in hand written assembler in the library. We don't want
368 these names. This is certainly necessary on a Sparc running
369 SunOS 4.1 (try profiling a program that does a lot of
370 division). I don't know whether it has harmful side effects on
371 other systems. Perhaps it should be made configurable. */
372 sym_prefix = bfd_get_symbol_leading_char (core_bfd);
374 if ((sym_prefix && sym_prefix != sym->name[0])
375 /* GCC may add special symbols to help gdb figure out the file
376 language. We want to ignore these, since sometimes they mask
377 the real function. (dj@ctron) */
378 || !strncmp (sym->name, "__gnu_compiled", 14)
379 || !strncmp (sym->name, "___gnu_compiled", 15))
381 return 0;
384 /* If the object file supports marking of function symbols, then
385 we can zap anything that doesn't have BSF_FUNCTION set. */
386 if (ignore_non_functions && (sym->flags & BSF_FUNCTION) == 0)
387 return 0;
389 return 't'; /* It's a static text symbol. */
392 /* Get whatever source info we can get regarding address ADDR. */
394 static bfd_boolean
395 get_src_info (bfd_vma addr, const char **filename, const char **name, int *line_num)
397 const char *fname = 0, *func_name = 0;
398 int l = 0;
400 if (bfd_find_nearest_line (core_bfd, core_text_sect, core_syms,
401 addr - core_text_sect->vma,
402 &fname, &func_name, (unsigned int *) &l)
403 && fname && func_name && l)
405 DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
406 (unsigned long) addr, fname, l, func_name));
407 *filename = fname;
408 *name = func_name;
409 *line_num = l;
410 return TRUE;
412 else
414 DBG (AOUTDEBUG, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
415 (long) addr, fname ? fname : "<unknown>", l,
416 func_name ? func_name : "<unknown>"));
417 return FALSE;
421 /* Read in symbol table from core.
422 One symbol per function is entered. */
424 void
425 core_create_function_syms ()
427 bfd_vma min_vma = ~(bfd_vma) 0;
428 bfd_vma max_vma = 0;
429 int class;
430 long i, found, skip;
431 unsigned int j;
433 /* Pass 1 - determine upper bound on number of function names. */
434 symtab.len = 0;
436 for (i = 0; i < core_num_syms; ++i)
438 if (!core_sym_class (core_syms[i]))
439 continue;
441 /* This should be replaced with a binary search or hashed
442 search. Gross.
444 Don't create a symtab entry for a function that has
445 a mapping to a file, unless it's the first function
446 in the file. */
447 skip = 0;
448 for (j = 0; j < symbol_map_count; j++)
449 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
451 if (j > 0 && ! strcmp (symbol_map [j].file_name,
452 symbol_map [j - 1].file_name))
453 skip = 1;
454 break;
457 if (!skip)
458 ++symtab.len;
461 if (symtab.len == 0)
463 fprintf (stderr, _("%s: file `%s' has no symbols\n"), whoami, a_out_name);
464 done (1);
467 /* The "+ 2" is for the sentinels. */
468 symtab.base = (Sym *) xmalloc ((symtab.len + 2) * sizeof (Sym));
470 /* Pass 2 - create symbols. */
471 symtab.limit = symtab.base;
473 for (i = 0; i < core_num_syms; ++i)
475 asection *sym_sec;
477 class = core_sym_class (core_syms[i]);
479 if (!class)
481 DBG (AOUTDEBUG,
482 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
483 (unsigned long) core_syms[i]->value,
484 core_syms[i]->name));
485 continue;
488 /* This should be replaced with a binary search or hashed
489 search. Gross. */
490 skip = 0;
491 found = 0;
493 for (j = 0; j < symbol_map_count; j++)
494 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
496 if (j > 0 && ! strcmp (symbol_map [j].file_name,
497 symbol_map [j - 1].file_name))
498 skip = 1;
499 else
500 found = j;
501 break;
504 if (skip)
505 continue;
507 sym_init (symtab.limit);
509 /* Symbol offsets are always section-relative. */
510 sym_sec = core_syms[i]->section;
511 symtab.limit->addr = core_syms[i]->value;
512 if (sym_sec)
513 symtab.limit->addr += bfd_get_section_vma (sym_sec->owner, sym_sec);
515 if (symbol_map_count
516 && !strcmp (core_syms[i]->name, symbol_map[found].function_name))
518 symtab.limit->name = symbol_map[found].file_name;
519 symtab.limit->mapped = 1;
521 else
523 symtab.limit->name = core_syms[i]->name;
524 symtab.limit->mapped = 0;
527 /* Lookup filename and line number, if we can. */
529 const char *filename, *func_name;
531 if (get_src_info (symtab.limit->addr, &filename, &func_name,
532 &symtab.limit->line_num))
534 symtab.limit->file = source_file_lookup_path (filename);
536 /* FIXME: Checking __osf__ here does not work with a cross
537 gprof. */
538 #ifdef __osf__
539 /* Suppress symbols that are not function names. This is
540 useful to suppress code-labels and aliases.
542 This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
543 labels do not appear in the symbol table info, so this isn't
544 necessary. */
546 if (strcmp (symtab.limit->name, func_name) != 0)
548 /* The symbol's address maps to a different name, so
549 it can't be a function-entry point. This happens
550 for labels, for example. */
551 DBG (AOUTDEBUG,
552 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
553 symtab.limit->name, func_name));
554 continue;
556 #endif
560 symtab.limit->is_func = TRUE;
561 symtab.limit->is_bb_head = TRUE;
563 if (class == 't')
564 symtab.limit->is_static = TRUE;
566 /* Keep track of the minimum and maximum vma addresses used by all
567 symbols. When computing the max_vma, use the ending address of the
568 section containing the symbol, if available. */
569 min_vma = MIN (symtab.limit->addr, min_vma);
570 if (sym_sec)
571 max_vma = MAX (bfd_get_section_vma (sym_sec->owner, sym_sec)
572 + bfd_section_size (sym_sec->owner, sym_sec) - 1,
573 max_vma);
574 else
575 max_vma = MAX (symtab.limit->addr, max_vma);
577 /* If we see "main" without an initial '_', we assume names
578 are *not* prefixed by '_'. */
579 if (symtab.limit->name[0] == 'm' && discard_underscores
580 && strcmp (symtab.limit->name, "main") == 0)
581 discard_underscores = 0;
583 DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
584 (long) (symtab.limit - symtab.base),
585 symtab.limit->name,
586 (unsigned long) symtab.limit->addr));
587 ++symtab.limit;
590 /* Create sentinels. */
591 sym_init (symtab.limit);
592 symtab.limit->name = "<locore>";
593 symtab.limit->addr = 0;
594 symtab.limit->end_addr = min_vma - 1;
595 ++symtab.limit;
597 sym_init (symtab.limit);
598 symtab.limit->name = "<hicore>";
599 symtab.limit->addr = max_vma + 1;
600 symtab.limit->end_addr = ~(bfd_vma) 0;
601 ++symtab.limit;
603 symtab.len = symtab.limit - symtab.base;
604 symtab_finalize (&symtab);
607 /* Read in symbol table from core.
608 One symbol per line of source code is entered. */
610 void
611 core_create_line_syms ()
613 char *prev_name, *prev_filename;
614 unsigned int prev_name_len, prev_filename_len;
615 bfd_vma vma, min_vma = ~(bfd_vma) 0, max_vma = 0;
616 Sym *prev, dummy, *sentinel, *sym;
617 const char *filename;
618 int prev_line_num;
619 Sym_Table ltab;
620 bfd_vma vma_high;
622 /* Create symbols for functions as usual. This is necessary in
623 cases where parts of a program were not compiled with -g. For
624 those parts we still want to get info at the function level. */
625 core_create_function_syms ();
627 /* Pass 1: count the number of symbols. */
629 /* To find all line information, walk through all possible
630 text-space addresses (one by one!) and get the debugging
631 info for each address. When the debugging info changes,
632 it is time to create a new symbol.
634 Of course, this is rather slow and it would be better if
635 BFD would provide an iterator for enumerating all line infos. */
636 prev_name_len = PATH_MAX;
637 prev_filename_len = PATH_MAX;
638 prev_name = xmalloc (prev_name_len);
639 prev_filename = xmalloc (prev_filename_len);
640 ltab.len = 0;
641 prev_line_num = 0;
643 vma_high = core_text_sect->vma + bfd_get_section_size (core_text_sect);
644 for (vma = core_text_sect->vma; vma < vma_high; vma += min_insn_size)
646 unsigned int len;
648 if (!get_src_info (vma, &filename, &dummy.name, &dummy.line_num)
649 || (prev_line_num == dummy.line_num
650 && prev_name != NULL
651 && strcmp (prev_name, dummy.name) == 0
652 && strcmp (prev_filename, filename) == 0))
653 continue;
655 ++ltab.len;
656 prev_line_num = dummy.line_num;
658 len = strlen (dummy.name);
659 if (len >= prev_name_len)
661 prev_name_len = len + 1024;
662 free (prev_name);
663 prev_name = xmalloc (prev_name_len);
666 strcpy (prev_name, dummy.name);
667 len = strlen (filename);
669 if (len >= prev_filename_len)
671 prev_filename_len = len + 1024;
672 free (prev_filename);
673 prev_filename = xmalloc (prev_filename_len);
676 strcpy (prev_filename, filename);
678 min_vma = MIN (vma, min_vma);
679 max_vma = MAX (vma, max_vma);
682 free (prev_name);
683 free (prev_filename);
685 /* Make room for function symbols, too. */
686 ltab.len += symtab.len;
687 ltab.base = (Sym *) xmalloc (ltab.len * sizeof (Sym));
688 ltab.limit = ltab.base;
690 /* Pass 2 - create symbols. */
692 /* We now set is_static as we go along, rather than by running
693 through the symbol table at the end.
695 The old way called symtab_finalize before the is_static pass,
696 causing a problem since symtab_finalize uses is_static as part of
697 its address conflict resolution algorithm. Since global symbols
698 were prefered over static symbols, and all line symbols were
699 global at that point, static function names that conflicted with
700 their own line numbers (static, but labeled as global) were
701 rejected in favor of the line num.
703 This was not the desired functionality. We always want to keep
704 our function symbols and discard any conflicting line symbols.
705 Perhaps symtab_finalize should be modified to make this
706 distinction as well, but the current fix works and the code is a
707 lot cleaner now. */
708 prev = 0;
710 for (vma = core_text_sect->vma; vma < vma_high; vma += min_insn_size)
712 sym_init (ltab.limit);
714 if (!get_src_info (vma, &filename, &ltab.limit->name, &ltab.limit->line_num)
715 || (prev && prev->line_num == ltab.limit->line_num
716 && strcmp (prev->name, ltab.limit->name) == 0
717 && strcmp (prev->file->name, filename) == 0))
718 continue;
720 /* Make name pointer a malloc'ed string. */
721 ltab.limit->name = xstrdup (ltab.limit->name);
722 ltab.limit->file = source_file_lookup_path (filename);
724 ltab.limit->addr = vma;
726 /* Set is_static based on the enclosing function, using either:
727 1) the previous symbol, if it's from the same function, or
728 2) a symtab lookup. */
729 if (prev && ltab.limit->file == prev->file &&
730 strcmp (ltab.limit->name, prev->name) == 0)
732 ltab.limit->is_static = prev->is_static;
734 else
736 sym = sym_lookup(&symtab, ltab.limit->addr);
737 ltab.limit->is_static = sym->is_static;
740 prev = ltab.limit;
742 /* If we see "main" without an initial '_', we assume names
743 are *not* prefixed by '_'. */
744 if (ltab.limit->name[0] == 'm' && discard_underscores
745 && strcmp (ltab.limit->name, "main") == 0)
746 discard_underscores = 0;
748 DBG (AOUTDEBUG, printf ("[core_create_line_syms] %lu %s 0x%lx\n",
749 (unsigned long) (ltab.limit - ltab.base),
750 ltab.limit->name,
751 (unsigned long) ltab.limit->addr));
752 ++ltab.limit;
755 /* Update sentinels. */
756 sentinel = sym_lookup (&symtab, (bfd_vma) 0);
758 if (sentinel
759 && strcmp (sentinel->name, "<locore>") == 0
760 && min_vma <= sentinel->end_addr)
761 sentinel->end_addr = min_vma - 1;
763 sentinel = sym_lookup (&symtab, ~(bfd_vma) 0);
765 if (sentinel
766 && strcmp (sentinel->name, "<hicore>") == 0
767 && max_vma >= sentinel->addr)
768 sentinel->addr = max_vma + 1;
770 /* Copy in function symbols. */
771 memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym));
772 ltab.limit += symtab.len;
774 if ((unsigned int) (ltab.limit - ltab.base) != ltab.len)
776 fprintf (stderr,
777 _("%s: somebody miscounted: ltab.len=%d instead of %ld\n"),
778 whoami, ltab.len, (long) (ltab.limit - ltab.base));
779 done (1);
782 /* Finalize ltab and make it symbol table. */
783 symtab_finalize (&ltab);
784 free (symtab.base);
785 symtab = ltab;