#include <stdio.h>
#include <unistd.h>
#include "basic_blocks.h"
-#include "core.h"
+#include "corefile.h"
#include "gmon_io.h"
#include "gmon_out.h"
#include "gprof.h"
/*
- * Helper for sorting. Compares two basic-blocks and returns result
+ * Helper for sorting. Compares two symbols and returns result
* such that sorting will be increasing according to filename, line
- * number, and basic-block address (in that order).
+ * number, and address (in that order).
*/
+
static int
DEFUN (cmp_bb, (lp, rp), const void *lp AND const void *rp)
{
if (r)
{
return r;
- } /* if */
+ }
if (left->line_num != right->line_num)
{
return left->line_num - right->line_num;
- } /* if */
- } /* if */
+ }
+ }
if (left->addr < right->addr)
{
else
{
return 0;
- } /* if */
-} /* cmp_bb */
+ }
+}
/*
else if (!right)
{
return -1;
- } /* if */
+ }
if (right->ncalls != left->ncalls)
{
return right->ncalls - left->ncalls;
- } /* if */
+ }
return left->line_num - right->line_num;
-} /* cmp_ncalls */
+}
/*
if (ch == '\0')
{
break;
- } /* if */
- } /* if */
-} /* fskip_string */
+ }
+ }
+}
/*
if (fread (&nblocks, sizeof (nblocks), 1, ifp) != 1)
{
- fprintf (stderr, "%s: %s: unexpected end of file\n", whoami, filename);
+ fprintf (stderr, _("%s: %s: unexpected end of file\n"), whoami, filename);
done (1);
- } /* if */
+ }
nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
if (gmon_file_version == 0)
{
fskip_string (ifp);
- } /* if */
+ }
for (b = 0; b < nblocks; ++b)
{
{
perror (filename);
done (1);
- } /* if */
+ }
}
else
{
{
perror (filename);
done (1);
- } /* if */
- } /* if */
+ }
+ }
/*
* Basic-block execution counts are meaningful only if we're
/* convert from target to host endianness: */
addr = get_vma (core_bfd, (bfd_byte *) & addr);
+ ncalls = bfd_get_32 (core_bfd, (bfd_byte *) &ncalls);
sym = sym_lookup (&symtab, addr);
- sym->is_bb_head = TRUE;
- sym->ncalls += bfd_get_32 (core_bfd, (bfd_byte *) & ncalls);
- DBG (BBDEBUG, printf ("[bb_read_rec] 0x%lx->0x%lx (%s) cnt=%d\n",
- addr, sym->addr, sym->name, sym->ncalls));
+ if (sym)
+ {
+ int i;
+
+ DBG (BBDEBUG,
+ printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%ld\n",
+ addr, sym->addr, sym->name, sym->line_num, ncalls));
+
+ for (i = 0; i < NBBS; i++)
+ {
+ if (! sym->bb_addr[i] || sym->bb_addr[i] == addr)
+ {
+ sym->bb_addr[i] = addr;
+ sym->bb_calls[i] += ncalls;
+ break;
+ }
+ }
+ }
}
else
{
{
user_warned = TRUE;
fprintf (stderr,
- "%s: warning: ignoring basic-block exec counts (use -l or --line)\n",
+ _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
whoami);
- } /* if */
- } /* if */
- } /* for */
+ }
+ }
+ }
return;
-} /* bb_read_rec */
+}
/*
bfd_vma addr;
long ncalls;
Sym *sym;
+ int i;
/* count how many non-zero blocks with have: */
for (sym = symtab.base; sym < symtab.limit; ++sym)
{
- if (sym->ncalls > 0)
- {
- ++nblocks;
- } /* if */
- } /* for */
+ for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
+ ;
+ nblocks += i;
+ }
/* write header: */
bfd_put_32 (core_bfd, nblocks, (bfd_byte *) & nblocks);
{
perror (filename);
done (1);
- } /* if */
+ }
/* write counts: */
for (sym = symtab.base; sym < symtab.limit; ++sym)
{
- if (sym->ncalls == 0)
+ for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
{
- continue;
- } /* if */
+ put_vma (core_bfd, sym->bb_addr[i], (bfd_byte *) & addr);
+ bfd_put_32 (core_bfd, sym->bb_calls[i], (bfd_byte *) & ncalls);
- put_vma (core_bfd, sym->addr, (bfd_byte *) & addr);
- bfd_put_32 (core_bfd, sym->ncalls, (bfd_byte *) & ncalls);
-
- if (fwrite (&addr, sizeof (addr), 1, ofp) != 1
- || fwrite (&ncalls, sizeof (ncalls), 1, ofp) != 1)
- {
- perror (filename);
- done (1);
- } /* if */
- } /* for */
-} /* bb_write_blocks */
+ if (fwrite (&addr, sizeof (addr), 1, ofp) != 1
+ || fwrite (&ncalls, sizeof (ncalls), 1, ofp) != 1)
+ {
+ perror (filename);
+ done (1);
+ }
+ }
+ }
+}
/*
DEFUN_VOID (print_exec_counts)
{
Sym **sorted_bbs, *sym;
- int i, len;
+ int i, j, len;
if (first_output)
{
else
{
printf ("\f\n");
- } /* if */
+ }
/* sort basic-blocks according to function name and line number: */
for (sym = symtab.base; sym < symtab.limit; ++sym)
{
/*
- * Accept symbol if it's the start of a basic-block and it is
- * called at least bb_min_calls times and if it's in the
- * INCL_EXEC table or there is no INCL_EXEC table and it does
- * not appear in the EXCL_EXEC table.
+ * Accept symbol if it's in the INCL_EXEC table
+ * or there is no INCL_EXEC table
+ * and it does not appear in the EXCL_EXEC table.
*/
- if (sym->is_bb_head && sym->ncalls >= bb_min_calls
- && (sym_lookup (&syms[INCL_EXEC], sym->addr)
- || (syms[INCL_EXEC].len == 0
- && !sym_lookup (&syms[EXCL_EXEC], sym->addr))))
+ if (sym_lookup (&syms[INCL_EXEC], sym->addr)
+ || (syms[INCL_EXEC].len == 0
+ && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
{
sorted_bbs[len++] = sym;
- } /* if */
- } /* for */
+ }
+ }
qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
/* output basic-blocks: */
for (i = 0; i < len; ++i)
{
- sym = sorted_bbs[i];
- printf ("%s:%d: (%s:0x%lx) %d executions\n",
- sym->file ? sym->file->name : "<unknown>", sym->line_num,
- sym->name, sym->addr, sym->ncalls);
- } /* for */
+ if (sym->ncalls > 0 || ! ignore_zeros)
+ {
+ printf (_("%s:%d: (%s:0x%lx) %d executions\n"),
+ sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
+ sym->name, sym->addr, sym->ncalls);
+ }
+ for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
+ {
+ if (sym->bb_calls[j] > 0 || ! ignore_zeros)
+ {
+ printf (_("%s:%d: (%s:0x%lx) %d executions\n"),
+ sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
+ sym->name, sym->bb_addr[j], sym->bb_calls[j]);
+ }
+ }
+ }
free (sorted_bbs);
-} /* print_exec_counts */
-
+}
/*
* Helper for bb_annotated_source: format annotation containing
- * number of line executions.
+ * number of line executions. Depends on being called on each
+ * line of a file in sequential order.
+ *
+ * Global variable bb_annotate_all_lines enables execution count
+ * compression (counts are supressed if identical to the last one)
+ * and prints counts on all executed lines. Otherwise, print
+ * all basic-block execution counts exactly once on the line
+ * that starts the basic-block.
*/
+
static void
DEFUN (annotate_with_count, (buf, width, line_num, arg),
char *buf AND int width AND int line_num AND void *arg)
{
Source_File *sf = arg;
Sym *b;
- long cnt;
- static long last_count;
+ int i;
+ static int last_count;
+ int last_print=-1;
- if (line_num == 1)
- {
- last_count = -1;
- } /* if */
-
- b = 0;
+ b = NULL;
if (line_num <= sf->num_lines)
{
b = sf->line[line_num - 1];
- } /* if */
+ }
if (!b)
{
- cnt = -1;
+ for (i = 0; i < width; i++)
+ buf[i] = ' ';
+ buf[width] = '\0';
}
else
{
+ char tmpbuf[NBBS * 30];
+ char *p;
+ int ncalls;
+ int len;
+
++num_executable_lines;
- cnt = b->ncalls;
- } /* if */
- if (cnt > 0)
- {
+
+ p = tmpbuf;
+ *p = '\0';
+
+ ncalls = -1;
+
+ /* If this is a function entry point, label the line no matter what.
+ * Otherwise, we're in the middle of a function, so check to see
+ * if the first basic-block address is larger than the starting
+ * address of the line. If so, then this line begins with a
+ * a portion of the previous basic-block, so print that prior
+ * execution count (if bb_annotate_all_lines is set).
+ */
+
+ if (b->is_func)
+ {
+ sprintf (p, "%d", b->ncalls);
+ p += strlen (p);
+ last_count = b->ncalls;
+ last_print = last_count;
+ ncalls = b->ncalls;
+ }
+ else if (bb_annotate_all_lines
+ && b->bb_addr[0] && b->bb_addr[0] > b->addr)
+ {
+ sprintf (p, "%d", last_count);
+ p += strlen (p);
+ last_print = last_count;
+ ncalls = last_count;
+ }
+
+ /* Loop through all of this line's basic-blocks. For each one,
+ * update last_count, then compress sequential identical counts
+ * (if bb_annotate_all_lines) and print the execution count.
+ */
+
+ for (i = 0; i < NBBS && b->bb_addr[i]; i++)
+ {
+ last_count = b->bb_calls[i];
+ if (ncalls < 0)
+ ncalls = 0;
+ ncalls += last_count;
+
+ if (bb_annotate_all_lines && last_count == last_print)
+ {
+ continue;
+ }
+
+ if (p > tmpbuf)
+ *p++ = ',';
+ sprintf (p, "%d", last_count);
+ p += strlen (p);
+
+ last_print = last_count;
+ }
+
+ /* We're done. If nothing has been printed on this line,
+ * print the last execution count (bb_annotate_all_lines),
+ * which could be from either a previous line (if there were
+ * no BBs on this line), or from this line (if all our BB
+ * counts were compressed out because they were identical).
+ */
+
+ if (bb_annotate_all_lines && p == tmpbuf)
+ {
+ sprintf (p, "%d", last_count);
+ p += strlen (p);
+ ncalls = last_count;
+ }
+
+ if (ncalls < 0)
+ {
+ int c;
+
+ for (c = 0; c < width; c++)
+ buf[c] = ' ';
+ buf[width] = '\0';
+ return;
+ }
+
++num_lines_executed;
- } /* if */
- if (cnt < 0 && bb_annotate_all_lines)
- {
- cnt = last_count;
- } /* if */
- if (cnt < 0)
- {
- strcpy (buf, "\t\t");
- }
- else if (cnt < bb_min_calls)
- {
- strcpy (buf, " ##### -> ");
- }
- else
- {
- sprintf (buf, "%12ld -> ", cnt);
- } /* if */
- last_count = cnt;
-} /* annotate_with_count */
+ if (ncalls < bb_min_calls)
+ {
+ strcpy (tmpbuf, "#####");
+ p = tmpbuf + 5;
+ }
+
+ strcpy (p, " -> ");
+ p += 4;
+
+ len = p - tmpbuf;
+ if (len >= width)
+ {
+ strncpy (buf, tmpbuf, width);
+ buf[width] = '\0';
+ }
+ else
+ {
+ int c;
+ strcpy (buf + width - len, tmpbuf);
+ for (c = 0; c < width - len; ++c)
+ buf[c] = ' ';
+ }
+ }
+}
/*
* Annotate the files named in SOURCE_FILES with basic-block statistics
&& !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
{
sym->file->num_lines = sym->line_num;
- } /* if */
- } /* for */
+ }
+ }
/* allocate line descriptors: */
{
sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
- } /* if */
- } /* for */
+ }
+ }
/* count executions per line: */
for (sym = symtab.base; sym < symtab.limit; ++sym)
{
- if (sym->is_bb_head && sym->file && sym->file->num_lines
+ if (sym->file && sym->file->num_lines
&& (sym_lookup (&syms[INCL_ANNO], sym->addr)
|| (syms[INCL_ANNO].len == 0
&& !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
new_line->addr = 0;
new_line->ncalls += sym->ncalls;
sym->file->line[sym->line_num - 1] = new_line;
- } /* if */
- } /* if */
- } /* for */
+ }
+ }
+ }
/* plod over source files, annotating them: */
if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
{
continue;
- } /* if */
+ }
num_executable_lines = num_lines_executed = 0;
ofp = annotate_source (sf, 16, annotate_with_count, sf);
if (!ofp)
{
continue;
- } /* if */
+ }
if (bb_table_length > 0)
{
- fprintf (ofp, "\n\nTop %d Lines:\n\n Line Count\n\n",
+ fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
bb_table_length);
/* abuse line arrays---it's not needed anymore: */
if (table_len > sf->num_lines)
{
table_len = sf->num_lines;
- } /* if */
+ }
for (i = 0; i < table_len; ++i)
{
sym = sf->line[i];
if (!sym || sym->ncalls <= 0)
{
break;
- } /* if */
+ }
fprintf (ofp, "%9d %10d\n", sym->line_num, sym->ncalls);
- } /* for */
- } /* if */
+ }
+ }
free (sf->line);
sf->line = 0;
- fprintf (ofp, "\nExecution Summary:\n\n");
- fprintf (ofp, "%9ld Executable lines in this file\n",
+ fprintf (ofp, _("\nExecution Summary:\n\n"));
+ fprintf (ofp, _("%9ld Executable lines in this file\n"),
num_executable_lines);
- fprintf (ofp, "%9ld Lines executed\n", num_lines_executed);
- fprintf (ofp, "%9.2f Percent of the file executed\n",
+ fprintf (ofp, _("%9ld Lines executed\n"), num_lines_executed);
+ fprintf (ofp, _("%9.2f Percent of the file executed\n"),
num_executable_lines
? 100.0 * num_lines_executed / (double) num_executable_lines
: 100.0);
- fprintf (ofp, "\n%9d Total number of line executions\n", sf->ncalls);
- fprintf (ofp, "%9.2f Average executions per line\n",
+ fprintf (ofp, _("\n%9d Total number of line executions\n"), sf->ncalls);
+ fprintf (ofp, _("%9.2f Average executions per line\n"),
num_executable_lines
? sf->ncalls / (double) num_executable_lines
: 0.0);
if (ofp != stdout)
{
fclose (ofp);
- } /* if */
- } /* for */
-} /* print_annotated_source */
-
-/*** end of basic_block.c ***/
+ }
+ }
+}