Thu Mar 21 17:02:02 1996 David Mosberger-Tang <davidm@azstarnet.com>
[deliverable/binutils-gdb.git] / gprof / core.c
CommitLineData
5489fcc3
KR
1#include "libiberty.h"
2#include "gprof.h"
3#include "core.h"
4#include "symtab.h"
5
0f579087
ILT
6#ifndef MIN_INSN_SIZE
7/* If not defined in MACHINE_H, assume smallest instruction is 1 byte
8 long. THis is safe but may be needlessly slow on machines where
9 all instructions are longer. */
10#define MIN_INSN_SIZE 1
11#endif
12
5489fcc3
KR
13bfd *core_bfd;
14int core_num_syms;
15asymbol **core_syms;
16asection *core_text_sect;
17PTR core_text_space;
18
64c50fc5
JL
19/* For mapping symbols to specific .o files during file ordering. */
20struct function_map {
21 char *function_name;
22 char *file_name;
23};
24
25struct function_map *symbol_map;
26int symbol_map_count;
27
28static void
29DEFUN (read_function_mappings, (filename), const char *filename)
30{
31 FILE *file = fopen (filename, "r");
32 char dummy[1024];
33 int count = 0;
34
35 if (!file)
36 {
37 fprintf (stderr, "%s: could not open %s.\n", whoami, filename);
38 done (1);
39 }
40
41 /* First parse the mapping file so we know how big we need to
42 make our tables. We also do some sanity checks at this
43 time. */
44 while (!feof (file))
45 {
46 int matches;
47
48 matches = fscanf (file, "%[^\n:]", dummy);
49 if (!matches)
50 {
51 fprintf (stderr, "%s: unable to parse mapping file %s.\n",
52 whoami, filename);
53 done (1);
54 }
55
56 /* Just skip messages about files with no symbols. */
57 if (!strncmp (dummy, "No symbols in ", 14))
58 {
59 fscanf (file, "\n");
60 continue;
61 }
62
63 /* Don't care what else is on this line at this point. */
64 fscanf (file, "%[^\n]\n", dummy);
65 count++;
66 }
67
68 /* Now we know how big we need to make our table. */
69 symbol_map = xmalloc (count * sizeof (struct function_map));
70
71 /* Rewind the input file so we can read it again. */
72 rewind (file);
73
74 /* Read each entry and put it into the table. */
75 count = 0;
76 while (!feof (file))
77 {
78 int matches;
79 char *tmp;
80
81 matches = fscanf (file, "%[^\n:]", dummy);
82 if (!matches)
83 {
84 fprintf (stderr, "%s: unable to parse mapping file %s.\n",
85 whoami, filename);
86 done (1);
87 }
88
89 /* Just skip messages about files with no symbols. */
90 if (!strncmp (dummy, "No symbols in ", 14))
91 {
92 fscanf (file, "\n");
93 continue;
94 }
95
96 /* dummy has the filename, go ahead and copy it. */
97 symbol_map[count].file_name = xmalloc (strlen (dummy) + 1);
98 strcpy (symbol_map[count].file_name, dummy);
99
100 /* Now we need the function name. */
101 fscanf (file, "%[^\n]\n", dummy);
102 tmp = strrchr (dummy, ' ') + 1;
103 symbol_map[count].function_name = xmalloc (strlen (tmp) + 1);
104 strcpy (symbol_map[count].function_name, tmp);
105 count++;
106 }
107
108 /* Record the size of the map table for future reference. */
109 symbol_map_count = count;
110}
5489fcc3
KR
111
112void
12516a37 113DEFUN (core_init, (a_out_name), const char *a_out_name)
5489fcc3 114{
12516a37
KR
115 core_bfd = bfd_openr (a_out_name, 0);
116
117 if (!core_bfd)
118 {
119 perror (a_out_name);
120 done (1);
03c35bcb 121 }
12516a37
KR
122
123 if (!bfd_check_format (core_bfd, bfd_object))
124 {
125 fprintf (stderr, "%s: %s: not in a.out format\n", whoami, a_out_name);
126 done (1);
03c35bcb 127 }
12516a37
KR
128
129 /* get core's text section: */
130 core_text_sect = bfd_get_section_by_name (core_bfd, ".text");
131 if (!core_text_sect)
132 {
133 core_text_sect = bfd_get_section_by_name (core_bfd, "$CODE$");
134 if (!core_text_sect)
135 {
136 fprintf (stderr, "%s: can't find .text section in %s\n",
137 whoami, a_out_name);
138 done (1);
03c35bcb
KR
139 }
140 }
12516a37
KR
141
142 /* read core's symbol table: */
143
144 /* this will probably give us more than we need, but that's ok: */
145 core_num_syms = bfd_get_symtab_upper_bound (core_bfd);
146 if (core_num_syms < 0)
147 {
148 fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name,
149 bfd_errmsg (bfd_get_error ()));
150 done (1);
03c35bcb 151 }
12516a37
KR
152
153 core_syms = (asymbol **) xmalloc (core_num_syms);
154 core_num_syms = bfd_canonicalize_symtab (core_bfd, core_syms);
155 if (core_num_syms < 0)
156 {
157 fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name,
158 bfd_errmsg (bfd_get_error ()));
159 done (1);
03c35bcb 160 }
64c50fc5
JL
161
162 if (function_mapping_file)
163 read_function_mappings (function_mapping_file);
03c35bcb 164}
5489fcc3
KR
165
166
167/*
168 * Read in the text space of an a.out file
169 */
170void
12516a37 171DEFUN (core_get_text_space, (core_bfd), bfd * core_bfd)
5489fcc3 172{
12516a37
KR
173 core_text_space = (PTR) malloc (core_text_sect->_raw_size);
174
175 if (!core_text_space)
5489fcc3 176 {
12516a37
KR
177 fprintf (stderr, "%s: ran out room for %ld bytes of text space\n",
178 whoami, core_text_sect->_raw_size);
179 done (1);
03c35bcb 180 }
12516a37
KR
181 if (!bfd_get_section_contents (core_bfd, core_text_sect, core_text_space,
182 0, core_text_sect->_raw_size))
183 {
184 bfd_perror ("bfd_get_section_contents");
185 free (core_text_space);
186 core_text_space = 0;
03c35bcb 187 }
12516a37
KR
188 if (!core_text_space)
189 {
190 fprintf (stderr, "%s: can't do -c\n", whoami);
03c35bcb
KR
191 }
192}
5489fcc3
KR
193
194
195/*
196 * Return class of symbol SYM. The returned class can be any of:
12516a37
KR
197 * 0 -> symbol is not interesting to us
198 * 'T' -> symbol is a global name
199 * 't' -> symbol is a local (static) name
5489fcc3
KR
200 */
201static int
12516a37 202DEFUN (core_sym_class, (sym), asymbol * sym)
5489fcc3 203{
12516a37
KR
204 symbol_info syminfo;
205 const char *name;
206 char sym_prefix;
207 int i;
208
0f579087 209 if (sym->section == NULL || (sym->flags & BSF_DEBUGGING) != 0)
12516a37
KR
210 {
211 return 0;
03c35bcb 212 }
12516a37 213
0f579087
ILT
214 /*
215 * Must be a text symbol, and static text symbols don't qualify if
216 * ignore_static_funcs set.
217 */
12516a37
KR
218 if (ignore_static_funcs && (sym->flags & BSF_LOCAL))
219 {
220 DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n",
5489fcc3 221 sym->name));
12516a37 222 return 0;
03c35bcb 223 }
5489fcc3 224
12516a37
KR
225 bfd_get_symbol_info (core_bfd, sym, &syminfo);
226 i = syminfo.type;
5489fcc3 227
12516a37
KR
228 if (i == 'T')
229 {
230 return i; /* it's a global symbol */
03c35bcb 231 }
5489fcc3 232
12516a37
KR
233 if (i != 't')
234 {
235 /* not a static text symbol */
236 DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n",
5489fcc3 237 sym->name, i));
12516a37 238 return 0;
03c35bcb 239 }
12516a37
KR
240
241 /* do some more filtering on static function-names: */
242
243 if (ignore_static_funcs)
244 {
245 return 0;
03c35bcb 246 }
12516a37
KR
247 /*
248 * Can't zero-length name or funny characters in name, where
249 * `funny' includes: `.' (.o file names) and `$' (Pascal labels).
250 */
251 if (!sym->name || sym->name[0] == '\0')
5489fcc3 252 {
12516a37 253 return 0;
03c35bcb 254 }
12516a37
KR
255
256 for (name = sym->name; *name; ++name)
257 {
258 if (*name == '.' || *name == '$')
259 {
260 return 0;
03c35bcb
KR
261 }
262 }
12516a37
KR
263 /*
264 * On systems where the C compiler adds an underscore to all
265 * names, static names without underscores seem usually to be
266 * labels in hand written assembler in the library. We don't want
267 * these names. This is certainly necessary on a Sparc running
268 * SunOS 4.1 (try profiling a program that does a lot of
269 * division). I don't know whether it has harmful side effects on
270 * other systems. Perhaps it should be made configurable.
271 */
272 sym_prefix = bfd_get_symbol_leading_char (core_bfd);
273 if (sym_prefix && sym_prefix != sym->name[0]
274 /*
275 * GCC may add special symbols to help gdb figure out the file
276 * language. We want to ignore these, since sometimes they mask
277 * the real function. (dj@ctron)
278 */
279 || !strncmp (sym->name, "__gnu_compiled", 14)
280 || !strncmp (sym->name, "___gnu_compiled", 15))
281 {
282 return 0;
03c35bcb 283 }
32843f94
JL
284
285 /* If the object file supports marking of function symbols, then we can
286 zap anything that doesn't have BSF_FUNCTION set. */
287 if (ignore_non_functions && (sym->flags & BSF_FUNCTION) == 0)
288 return 0;
289
12516a37 290 return 't'; /* it's a static text symbol */
03c35bcb 291}
5489fcc3
KR
292
293
294/*
295 * Get whatever source info we can get regarding address ADDR:
296 */
297static bool
12516a37
KR
298DEFUN (get_src_info, (addr, filename, name, line_num),
299 bfd_vma addr AND const char **filename AND const char **name
300 AND int *line_num)
5489fcc3 301{
12516a37
KR
302 const char *fname = 0, *func_name = 0;
303 int l = 0;
5489fcc3 304
12516a37
KR
305 if (bfd_find_nearest_line (core_bfd, core_text_sect, core_syms,
306 addr - core_text_sect->vma,
643f17d2 307 &fname, &func_name, (unsigned int *) &l)
12516a37 308 && fname && func_name && l)
5489fcc3 309 {
12516a37 310 DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
5489fcc3 311 addr, fname, l, func_name));
12516a37
KR
312 *filename = fname;
313 *name = func_name;
314 *line_num = l;
315 return TRUE;
316 }
317 else
318 {
319 DBG (AOUTDEBUG, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
5489fcc3
KR
320 (long) addr, fname ? fname : "<unknown>", l,
321 func_name ? func_name : "<unknown>"));
12516a37 322 return FALSE;
03c35bcb
KR
323 }
324}
5489fcc3
KR
325
326
327/*
328 * Read in symbol table from core. One symbol per function is
329 * entered.
330 */
331void
12516a37 332DEFUN (core_create_function_syms, (core_bfd), bfd * core_bfd)
5489fcc3 333{
12516a37
KR
334 bfd_vma min_vma = ~0, max_vma = 0;
335 const char *filename, *func_name;
336 int class;
64c50fc5 337 long i, j, found, skip;
12516a37
KR
338
339 /* pass 1 - determine upper bound on number of function names: */
340 symtab.len = 0;
341 for (i = 0; i < core_num_syms; ++i)
342 {
343 if (!core_sym_class (core_syms[i]))
344 {
345 continue;
03c35bcb 346 }
64c50fc5
JL
347
348 /* This should be replaced with a binary search or hashed
349 search. Gross.
350
351 Don't create a symtab entry for a function that has
352 a mapping to a file, unless it's the first function
353 in the file. */
354 skip = 0;
355 for (j = 0; j < symbol_map_count; j++)
356 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
357 {
358 if (j > 0 && ! strcmp (symbol_map [j].file_name,
359 symbol_map [j - 1].file_name))
360 skip = 1;
361 break;
362 }
363 if (!skip)
364 ++symtab.len;
03c35bcb 365 }
12516a37
KR
366
367 if (symtab.len == 0)
368 {
369 fprintf (stderr, "%s: file `%s' has no symbols\n", whoami, a_out_name);
370 done (1);
03c35bcb 371 }
12516a37
KR
372
373 /* the "+ 2" is for the sentinels: */
374 symtab.base = (Sym *) xmalloc ((symtab.len + 2) * sizeof (Sym));
375
376 /* pass 2 - create symbols: */
377
378 symtab.limit = symtab.base;
379 for (i = 0; i < core_num_syms; ++i)
380 {
381 class = core_sym_class (core_syms[i]);
382 if (!class)
383 {
384 DBG (AOUTDEBUG,
385 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
5489fcc3 386 core_syms[i]->value, core_syms[i]->name));
12516a37 387 continue;
03c35bcb 388 }
64c50fc5
JL
389 /* This should be replaced with a binary search or hashed
390 search. Gross. */
391
392 skip = 0;
393 found = 0;
394 for (j = 0; j < symbol_map_count; j++)
395 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
396 {
397 if (j > 0 && ! strcmp (symbol_map [j].file_name,
398 symbol_map [j - 1].file_name))
399 skip = 1;
400 else
401 found = j;
402 break;
403 }
404
405 if (skip)
406 continue;
5489fcc3 407
12516a37 408 sym_init (symtab.limit);
5489fcc3 409
12516a37 410 /* symbol offsets are always section-relative: */
5489fcc3 411
12516a37 412 symtab.limit->addr = core_syms[i]->value + core_syms[i]->section->vma;
64c50fc5
JL
413 if (symbol_map_count
414 && !strcmp (core_syms[i]->name, symbol_map[found].function_name))
415 {
416 symtab.limit->name = symbol_map[found].file_name;
417 symtab.limit->mapped = 1;
418 }
419 else
420 {
421 symtab.limit->name = core_syms[i]->name;
422 symtab.limit->mapped = 0;
423 }
5489fcc3
KR
424
425#ifdef __osf__
12516a37
KR
426 /*
427 * Suppress symbols that are not function names. This is
428 * useful to suppress code-labels and aliases.
429 *
430 * This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
431 * labels do not appear in the symbol table info, so this isn't
432 * necessary.
433 */
434 if (get_src_info (symtab.limit->addr, &filename, &func_name,
435 &symtab.limit->line_num))
5489fcc3 436 {
12516a37
KR
437 symtab.limit->file = source_file_lookup_path (filename);
438
439 if (strcmp (symtab.limit->name, func_name) != 0)
440 {
441 /*
442 * The symbol's address maps to a different name, so
443 * it can't be a function-entry point. This happens
444 * for labels, for example.
445 */
446 DBG (AOUTDEBUG,
447 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
448 symtab.limit->name, func_name));
449 continue;
03c35bcb
KR
450 }
451 }
5489fcc3
KR
452#endif
453
12516a37
KR
454 symtab.limit->is_func = TRUE;
455 symtab.limit->is_bb_head = TRUE;
456 if (class == 't')
457 {
458 symtab.limit->is_static = TRUE;
03c35bcb 459 }
12516a37
KR
460
461 min_vma = MIN (symtab.limit->addr, min_vma);
462 max_vma = MAX (symtab.limit->addr, max_vma);
463
464 /*
465 * If we see "main" without an initial '_', we assume names
466 * are *not* prefixed by '_'.
467 */
468 if (symtab.limit->name[0] == 'm' && discard_underscores
469 && strcmp (symtab.limit->name, "main") == 0)
5489fcc3 470 {
12516a37 471 discard_underscores = 0;
03c35bcb 472 }
5489fcc3 473
12516a37
KR
474 DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
475 (long) (symtab.limit - symtab.base),
5489fcc3 476 symtab.limit->name, symtab.limit->addr));
12516a37 477 ++symtab.limit;
03c35bcb 478 }
5489fcc3 479
12516a37 480 /* create sentinels: */
5489fcc3 481
12516a37
KR
482 sym_init (symtab.limit);
483 symtab.limit->name = "<locore>";
484 symtab.limit->addr = 0;
485 symtab.limit->end_addr = min_vma - 1;
486 ++symtab.limit;
5489fcc3 487
12516a37
KR
488 sym_init (symtab.limit);
489 symtab.limit->name = "<hicore>";
490 symtab.limit->addr = max_vma + 1;
491 symtab.limit->end_addr = ~0;
492 ++symtab.limit;
5489fcc3 493
12516a37
KR
494 symtab.len = symtab.limit - symtab.base;
495 symtab_finalize (&symtab);
03c35bcb 496}
5489fcc3
KR
497
498
499/*
500 * Read in symbol table from core. One symbol per line of source code
501 * is entered.
502 */
503void
12516a37 504DEFUN (core_create_line_syms, (core_bfd), bfd * core_bfd)
5489fcc3 505{
12516a37
KR
506 char prev_name[PATH_MAX], prev_filename[PATH_MAX];
507 bfd_vma vma, min_vma = ~0, max_vma = 0;
508 bfd_vma offset, prev_offset, min_dist;
509 Sym *prev, dummy, *sentinel, *sym;
510 const char *filename;
511 int prev_line_num, i;
512 Sym_Table ltab;
513 /*
514 * Create symbols for functions as usual. This is necessary in
515 * cases where parts of a program were not compiled with -g. For
516 * those parts we still want to get info at the function level:
517 */
518 core_create_function_syms (core_bfd);
519
520 /* pass 1 - counter number of symbols: */
521
522 /*
523 * To find all line information, walk through all possible
524 * text-space addresses (one by one!) and get the debugging
525 * info for each address. When the debugging info changes,
526 * it is time to create a new symbol.
527 *
528 * Of course, this is rather slow and it would be better if
529 * bfd would provide an iterator for enumerating all line
530 * infos, but for now, we try to speed up the second pass
531 * by determining what the minimum code distance between two
532 * lines is.
533 */
534 prev_name[0] = '\0';
535 ltab.len = 0;
536 min_dist = core_text_sect->_raw_size;
537 prev_offset = -min_dist;
538 prev_filename[0] = '\0';
539 prev_line_num = 0;
0f579087 540 for (offset = 0; offset < core_text_sect->_raw_size; offset += MIN_INSN_SIZE)
12516a37
KR
541 {
542 vma = core_text_sect->vma + offset;
543 if (!get_src_info (vma, &filename, &dummy.name, &dummy.line_num)
544 || (prev_line_num == dummy.line_num &&
545 strcmp (prev_name, dummy.name) == 0
546 && strcmp (prev_filename, filename) == 0))
5489fcc3 547 {
12516a37 548 continue;
03c35bcb 549 }
12516a37
KR
550
551 ++ltab.len;
552 prev_line_num = dummy.line_num;
553 strcpy (prev_name, dummy.name);
554 strcpy (prev_filename, filename);
555
556 if (offset - prev_offset < min_dist)
5489fcc3 557 {
12516a37 558 min_dist = offset - prev_offset;
03c35bcb 559 }
12516a37
KR
560 prev_offset = offset;
561
562 min_vma = MIN (vma, min_vma);
563 max_vma = MAX (vma, max_vma);
03c35bcb 564 }
12516a37
KR
565
566 DBG (AOUTDEBUG, printf ("[core_create_line_syms] min_dist=%lx\n", min_dist));
567
568 /* make room for function symbols, too: */
569 ltab.len += symtab.len;
570 ltab.base = (Sym *) xmalloc (ltab.len * sizeof (Sym));
571 ltab.limit = ltab.base;
572
573 /* pass 2 - create symbols: */
574
575 prev = 0;
576 for (offset = 0; offset < core_text_sect->_raw_size; offset += min_dist)
577 {
578 sym_init (ltab.limit);
579 if (!get_src_info (core_text_sect->vma + offset, &filename,
580 &ltab.limit->name, &ltab.limit->line_num)
581 || (prev && prev->line_num == ltab.limit->line_num
582 && strcmp (prev->name, ltab.limit->name) == 0
583 && strcmp (prev->file->name, filename) == 0))
5489fcc3 584 {
12516a37 585 continue;
03c35bcb 586 }
12516a37
KR
587
588 /* make name pointer a malloc'ed string: */
589 ltab.limit->name = strdup (ltab.limit->name);
590 ltab.limit->file = source_file_lookup_path (filename);
591
592 ltab.limit->addr = core_text_sect->vma + offset;
593 prev = ltab.limit;
594
595 /*
596 * If we see "main" without an initial '_', we assume names
597 * are *not* prefixed by '_'.
598 */
599 if (ltab.limit->name[0] == 'm' && discard_underscores
600 && strcmp (ltab.limit->name, "main") == 0)
601 {
602 discard_underscores = 0;
03c35bcb 603 }
5489fcc3 604
12516a37 605 DBG (AOUTDEBUG, printf ("[core_create_line_syms] %d %s 0x%lx\n",
5489fcc3
KR
606 ltab.len, ltab.limit->name,
607 ltab.limit->addr));
12516a37 608 ++ltab.limit;
03c35bcb 609 }
12516a37
KR
610
611 /* update sentinels: */
612
613 sentinel = sym_lookup (&symtab, 0);
614 if (strcmp (sentinel->name, "<locore>") == 0
615 && min_vma <= sentinel->end_addr)
616 {
617 sentinel->end_addr = min_vma - 1;
03c35bcb 618 }
12516a37
KR
619
620 sentinel = sym_lookup (&symtab, ~0);
621 if (strcmp (sentinel->name, "<hicore>") == 0 && max_vma >= sentinel->addr)
622 {
623 sentinel->addr = max_vma + 1;
03c35bcb 624 }
5489fcc3 625
12516a37
KR
626 /* copy in function symbols: */
627 memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym));
628 ltab.limit += symtab.len;
5489fcc3 629
12516a37 630 if (ltab.limit - ltab.base != ltab.len)
5489fcc3 631 {
12516a37
KR
632 fprintf (stderr,
633 "%s: somebody miscounted: ltab.len=%ld instead of %d\n",
634 whoami, (long) (ltab.limit - ltab.base), ltab.len);
635 done (1);
03c35bcb 636 }
12516a37
KR
637
638 /* finalize ltab and make it symbol table: */
639
640 symtab_finalize (&ltab);
641 free (symtab.base);
642 symtab = ltab;
643
644 /* now go through all core symbols and set is_static accordingly: */
645
646 for (i = 0; i < core_num_syms; ++i)
647 {
648 if (core_sym_class (core_syms[i]) == 't')
649 {
650 sym = sym_lookup (&symtab, core_syms[i]->value
651 + core_syms[i]->section->vma);
652 do
653 {
654 sym++->is_static = TRUE;
655 }
656 while (sym->file == sym[-1].file &&
657 strcmp (sym->name, sym[-1].name) == 0);
03c35bcb
KR
658 }
659 }
12516a37 660
03c35bcb 661}
This page took 0.078966 seconds and 4 git commands to generate.