1*3d8817e4Smiod /* basic_blocks.c - Basic-block level related code: reading/writing
2*3d8817e4Smiod of basic-block info to/from gmon.out; computing and formatting of
3*3d8817e4Smiod basic-block related statistics.
4*3d8817e4Smiod
5*3d8817e4Smiod Copyright 1999, 2000, 2001, 2002, 2004, 2005
6*3d8817e4Smiod Free Software Foundation, Inc.
7*3d8817e4Smiod
8*3d8817e4Smiod This file is part of GNU Binutils.
9*3d8817e4Smiod
10*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
11*3d8817e4Smiod it under the terms of the GNU General Public License as published by
12*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
13*3d8817e4Smiod (at your option) any later version.
14*3d8817e4Smiod
15*3d8817e4Smiod This program is distributed in the hope that it will be useful,
16*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
17*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*3d8817e4Smiod GNU General Public License for more details.
19*3d8817e4Smiod
20*3d8817e4Smiod You should have received a copy of the GNU General Public License
21*3d8817e4Smiod along with this program; if not, write to the Free Software
22*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23*3d8817e4Smiod 02110-1301, USA. */
24*3d8817e4Smiod
25*3d8817e4Smiod #include "libiberty.h"
26*3d8817e4Smiod #include "gprof.h"
27*3d8817e4Smiod #include "basic_blocks.h"
28*3d8817e4Smiod #include "corefile.h"
29*3d8817e4Smiod #include "gmon_io.h"
30*3d8817e4Smiod #include "gmon_out.h"
31*3d8817e4Smiod #include "search_list.h"
32*3d8817e4Smiod #include "source.h"
33*3d8817e4Smiod #include "symtab.h"
34*3d8817e4Smiod #include "sym_ids.h"
35*3d8817e4Smiod
36*3d8817e4Smiod static int cmp_bb (const PTR, const PTR);
37*3d8817e4Smiod static int cmp_ncalls (const PTR, const PTR);
38*3d8817e4Smiod static void fskip_string (FILE *);
39*3d8817e4Smiod static void annotate_with_count (char *, unsigned int, int, PTR);
40*3d8817e4Smiod
41*3d8817e4Smiod /* Default option values: */
42*3d8817e4Smiod bfd_boolean bb_annotate_all_lines = FALSE;
43*3d8817e4Smiod unsigned long bb_min_calls = 1;
44*3d8817e4Smiod int bb_table_length = 10;
45*3d8817e4Smiod
46*3d8817e4Smiod /* Variables used to compute annotated source listing stats: */
47*3d8817e4Smiod static long num_executable_lines;
48*3d8817e4Smiod static long num_lines_executed;
49*3d8817e4Smiod
50*3d8817e4Smiod
51*3d8817e4Smiod /* Helper for sorting. Compares two symbols and returns result
52*3d8817e4Smiod such that sorting will be increasing according to filename, line
53*3d8817e4Smiod number, and address (in that order). */
54*3d8817e4Smiod
55*3d8817e4Smiod static int
cmp_bb(const PTR lp,const PTR rp)56*3d8817e4Smiod cmp_bb (const PTR lp, const PTR rp)
57*3d8817e4Smiod {
58*3d8817e4Smiod int r;
59*3d8817e4Smiod const Sym *left = *(const Sym **) lp;
60*3d8817e4Smiod const Sym *right = *(const Sym **) rp;
61*3d8817e4Smiod
62*3d8817e4Smiod if (left->file && right->file)
63*3d8817e4Smiod {
64*3d8817e4Smiod r = strcmp (left->file->name, right->file->name);
65*3d8817e4Smiod
66*3d8817e4Smiod if (r)
67*3d8817e4Smiod return r;
68*3d8817e4Smiod
69*3d8817e4Smiod if (left->line_num != right->line_num)
70*3d8817e4Smiod return left->line_num - right->line_num;
71*3d8817e4Smiod }
72*3d8817e4Smiod
73*3d8817e4Smiod if (left->addr < right->addr)
74*3d8817e4Smiod return -1;
75*3d8817e4Smiod else if (left->addr > right->addr)
76*3d8817e4Smiod return 1;
77*3d8817e4Smiod else
78*3d8817e4Smiod return 0;
79*3d8817e4Smiod }
80*3d8817e4Smiod
81*3d8817e4Smiod
82*3d8817e4Smiod /* Helper for sorting. Order basic blocks in decreasing number of
83*3d8817e4Smiod calls, ties are broken in increasing order of line numbers. */
84*3d8817e4Smiod static int
cmp_ncalls(const PTR lp,const PTR rp)85*3d8817e4Smiod cmp_ncalls (const PTR lp, const PTR rp)
86*3d8817e4Smiod {
87*3d8817e4Smiod const Sym *left = *(const Sym **) lp;
88*3d8817e4Smiod const Sym *right = *(const Sym **) rp;
89*3d8817e4Smiod
90*3d8817e4Smiod if (!left)
91*3d8817e4Smiod return 1;
92*3d8817e4Smiod else if (!right)
93*3d8817e4Smiod return -1;
94*3d8817e4Smiod
95*3d8817e4Smiod if (left->ncalls < right->ncalls)
96*3d8817e4Smiod return 1;
97*3d8817e4Smiod else if (left->ncalls > right->ncalls)
98*3d8817e4Smiod return -1;
99*3d8817e4Smiod
100*3d8817e4Smiod return left->line_num - right->line_num;
101*3d8817e4Smiod }
102*3d8817e4Smiod
103*3d8817e4Smiod /* Skip over variable length string. */
104*3d8817e4Smiod static void
fskip_string(FILE * fp)105*3d8817e4Smiod fskip_string (FILE *fp)
106*3d8817e4Smiod {
107*3d8817e4Smiod int ch;
108*3d8817e4Smiod
109*3d8817e4Smiod while ((ch = fgetc (fp)) != EOF)
110*3d8817e4Smiod {
111*3d8817e4Smiod if (ch == '\0')
112*3d8817e4Smiod break;
113*3d8817e4Smiod }
114*3d8817e4Smiod }
115*3d8817e4Smiod
116*3d8817e4Smiod /* Read a basic-block record from file IFP. FILENAME is the name
117*3d8817e4Smiod of file IFP and is provided for formatting error-messages only. */
118*3d8817e4Smiod
119*3d8817e4Smiod void
bb_read_rec(FILE * ifp,const char * filename)120*3d8817e4Smiod bb_read_rec (FILE *ifp, const char *filename)
121*3d8817e4Smiod {
122*3d8817e4Smiod unsigned int nblocks, b;
123*3d8817e4Smiod bfd_vma addr, ncalls;
124*3d8817e4Smiod Sym *sym;
125*3d8817e4Smiod
126*3d8817e4Smiod if (gmon_io_read_32 (ifp, &nblocks))
127*3d8817e4Smiod {
128*3d8817e4Smiod fprintf (stderr, _("%s: %s: unexpected end of file\n"),
129*3d8817e4Smiod whoami, filename);
130*3d8817e4Smiod done (1);
131*3d8817e4Smiod }
132*3d8817e4Smiod
133*3d8817e4Smiod nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks);
134*3d8817e4Smiod if (gmon_file_version == 0)
135*3d8817e4Smiod fskip_string (ifp);
136*3d8817e4Smiod
137*3d8817e4Smiod for (b = 0; b < nblocks; ++b)
138*3d8817e4Smiod {
139*3d8817e4Smiod if (gmon_file_version == 0)
140*3d8817e4Smiod {
141*3d8817e4Smiod int line_num;
142*3d8817e4Smiod
143*3d8817e4Smiod /* Version 0 had lots of extra stuff that we don't
144*3d8817e4Smiod care about anymore. */
145*3d8817e4Smiod if ((fread (&ncalls, sizeof (ncalls), 1, ifp) != 1)
146*3d8817e4Smiod || (fread (&addr, sizeof (addr), 1, ifp) != 1)
147*3d8817e4Smiod || (fskip_string (ifp), FALSE)
148*3d8817e4Smiod || (fskip_string (ifp), FALSE)
149*3d8817e4Smiod || (fread (&line_num, sizeof (line_num), 1, ifp) != 1))
150*3d8817e4Smiod {
151*3d8817e4Smiod perror (filename);
152*3d8817e4Smiod done (1);
153*3d8817e4Smiod }
154*3d8817e4Smiod }
155*3d8817e4Smiod else if (gmon_io_read_vma (ifp, &addr)
156*3d8817e4Smiod || gmon_io_read_vma (ifp, &ncalls))
157*3d8817e4Smiod {
158*3d8817e4Smiod perror (filename);
159*3d8817e4Smiod done (1);
160*3d8817e4Smiod }
161*3d8817e4Smiod
162*3d8817e4Smiod /* Basic-block execution counts are meaningful only if we're
163*3d8817e4Smiod profiling at the line-by-line level: */
164*3d8817e4Smiod if (line_granularity)
165*3d8817e4Smiod {
166*3d8817e4Smiod sym = sym_lookup (&symtab, addr);
167*3d8817e4Smiod
168*3d8817e4Smiod if (sym)
169*3d8817e4Smiod {
170*3d8817e4Smiod int i;
171*3d8817e4Smiod
172*3d8817e4Smiod DBG (BBDEBUG,
173*3d8817e4Smiod printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n",
174*3d8817e4Smiod (unsigned long) addr, (unsigned long) sym->addr,
175*3d8817e4Smiod sym->name, sym->line_num, (unsigned long) ncalls));
176*3d8817e4Smiod
177*3d8817e4Smiod for (i = 0; i < NBBS; i++)
178*3d8817e4Smiod {
179*3d8817e4Smiod if (! sym->bb_addr[i] || sym->bb_addr[i] == addr)
180*3d8817e4Smiod {
181*3d8817e4Smiod sym->bb_addr[i] = addr;
182*3d8817e4Smiod sym->bb_calls[i] += ncalls;
183*3d8817e4Smiod break;
184*3d8817e4Smiod }
185*3d8817e4Smiod }
186*3d8817e4Smiod }
187*3d8817e4Smiod }
188*3d8817e4Smiod else
189*3d8817e4Smiod {
190*3d8817e4Smiod static bfd_boolean user_warned = FALSE;
191*3d8817e4Smiod
192*3d8817e4Smiod if (!user_warned)
193*3d8817e4Smiod {
194*3d8817e4Smiod user_warned = TRUE;
195*3d8817e4Smiod fprintf (stderr,
196*3d8817e4Smiod _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"),
197*3d8817e4Smiod whoami);
198*3d8817e4Smiod }
199*3d8817e4Smiod }
200*3d8817e4Smiod }
201*3d8817e4Smiod return;
202*3d8817e4Smiod }
203*3d8817e4Smiod
204*3d8817e4Smiod /* Write all basic-blocks with non-zero counts to file OFP. FILENAME
205*3d8817e4Smiod is the name of OFP and is provided for producing error-messages
206*3d8817e4Smiod only. */
207*3d8817e4Smiod void
bb_write_blocks(FILE * ofp,const char * filename)208*3d8817e4Smiod bb_write_blocks (FILE *ofp, const char *filename)
209*3d8817e4Smiod {
210*3d8817e4Smiod unsigned int nblocks = 0;
211*3d8817e4Smiod Sym *sym;
212*3d8817e4Smiod int i;
213*3d8817e4Smiod
214*3d8817e4Smiod /* Count how many non-zero blocks with have: */
215*3d8817e4Smiod for (sym = symtab.base; sym < symtab.limit; ++sym)
216*3d8817e4Smiod {
217*3d8817e4Smiod for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
218*3d8817e4Smiod ;
219*3d8817e4Smiod nblocks += i;
220*3d8817e4Smiod }
221*3d8817e4Smiod
222*3d8817e4Smiod /* Write header: */
223*3d8817e4Smiod if (gmon_io_write_8 (ofp, GMON_TAG_BB_COUNT)
224*3d8817e4Smiod || gmon_io_write_32 (ofp, nblocks))
225*3d8817e4Smiod {
226*3d8817e4Smiod perror (filename);
227*3d8817e4Smiod done (1);
228*3d8817e4Smiod }
229*3d8817e4Smiod
230*3d8817e4Smiod /* Write counts: */
231*3d8817e4Smiod for (sym = symtab.base; sym < symtab.limit; ++sym)
232*3d8817e4Smiod {
233*3d8817e4Smiod for (i = 0; i < NBBS && sym->bb_addr[i]; i++)
234*3d8817e4Smiod {
235*3d8817e4Smiod if (gmon_io_write_vma (ofp, sym->bb_addr[i])
236*3d8817e4Smiod || gmon_io_write_vma (ofp, (bfd_vma) sym->bb_calls[i]))
237*3d8817e4Smiod {
238*3d8817e4Smiod perror (filename);
239*3d8817e4Smiod done (1);
240*3d8817e4Smiod }
241*3d8817e4Smiod }
242*3d8817e4Smiod }
243*3d8817e4Smiod }
244*3d8817e4Smiod
245*3d8817e4Smiod /* Output basic-block statistics in a format that is easily parseable.
246*3d8817e4Smiod Current the format is:
247*3d8817e4Smiod
248*3d8817e4Smiod <filename>:<line-number>: (<function-name>:<bb-addr): <ncalls> */
249*3d8817e4Smiod
250*3d8817e4Smiod void
print_exec_counts()251*3d8817e4Smiod print_exec_counts ()
252*3d8817e4Smiod {
253*3d8817e4Smiod Sym **sorted_bbs, *sym;
254*3d8817e4Smiod unsigned int i, j, len;
255*3d8817e4Smiod
256*3d8817e4Smiod if (first_output)
257*3d8817e4Smiod first_output = FALSE;
258*3d8817e4Smiod else
259*3d8817e4Smiod printf ("\f\n");
260*3d8817e4Smiod
261*3d8817e4Smiod /* Sort basic-blocks according to function name and line number: */
262*3d8817e4Smiod sorted_bbs = (Sym **) xmalloc (symtab.len * sizeof (sorted_bbs[0]));
263*3d8817e4Smiod len = 0;
264*3d8817e4Smiod
265*3d8817e4Smiod for (sym = symtab.base; sym < symtab.limit; ++sym)
266*3d8817e4Smiod {
267*3d8817e4Smiod /* Accept symbol if it's in the INCL_EXEC table
268*3d8817e4Smiod or there is no INCL_EXEC table
269*3d8817e4Smiod and it does not appear in the EXCL_EXEC table. */
270*3d8817e4Smiod if (sym_lookup (&syms[INCL_EXEC], sym->addr)
271*3d8817e4Smiod || (syms[INCL_EXEC].len == 0
272*3d8817e4Smiod && !sym_lookup (&syms[EXCL_EXEC], sym->addr)))
273*3d8817e4Smiod {
274*3d8817e4Smiod sorted_bbs[len++] = sym;
275*3d8817e4Smiod }
276*3d8817e4Smiod }
277*3d8817e4Smiod
278*3d8817e4Smiod qsort (sorted_bbs, len, sizeof (sorted_bbs[0]), cmp_bb);
279*3d8817e4Smiod
280*3d8817e4Smiod /* Output basic-blocks: */
281*3d8817e4Smiod
282*3d8817e4Smiod for (i = 0; i < len; ++i)
283*3d8817e4Smiod {
284*3d8817e4Smiod sym = sorted_bbs [i];
285*3d8817e4Smiod
286*3d8817e4Smiod if (sym->ncalls > 0 || ! ignore_zeros)
287*3d8817e4Smiod {
288*3d8817e4Smiod /* FIXME: This only works if bfd_vma is unsigned long. */
289*3d8817e4Smiod printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
290*3d8817e4Smiod sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
291*3d8817e4Smiod sym->name, (unsigned long) sym->addr, sym->ncalls);
292*3d8817e4Smiod }
293*3d8817e4Smiod
294*3d8817e4Smiod for (j = 0; j < NBBS && sym->bb_addr[j]; j ++)
295*3d8817e4Smiod {
296*3d8817e4Smiod if (sym->bb_calls[j] > 0 || ! ignore_zeros)
297*3d8817e4Smiod {
298*3d8817e4Smiod /* FIXME: This only works if bfd_vma is unsigned long. */
299*3d8817e4Smiod printf (_("%s:%d: (%s:0x%lx) %lu executions\n"),
300*3d8817e4Smiod sym->file ? sym->file->name : _("<unknown>"), sym->line_num,
301*3d8817e4Smiod sym->name, (unsigned long) sym->bb_addr[j],
302*3d8817e4Smiod sym->bb_calls[j]);
303*3d8817e4Smiod }
304*3d8817e4Smiod }
305*3d8817e4Smiod }
306*3d8817e4Smiod free (sorted_bbs);
307*3d8817e4Smiod }
308*3d8817e4Smiod
309*3d8817e4Smiod /* Helper for bb_annotated_source: format annotation containing
310*3d8817e4Smiod number of line executions. Depends on being called on each
311*3d8817e4Smiod line of a file in sequential order.
312*3d8817e4Smiod
313*3d8817e4Smiod Global variable bb_annotate_all_lines enables execution count
314*3d8817e4Smiod compression (counts are supressed if identical to the last one)
315*3d8817e4Smiod and prints counts on all executed lines. Otherwise, print
316*3d8817e4Smiod all basic-block execution counts exactly once on the line
317*3d8817e4Smiod that starts the basic-block. */
318*3d8817e4Smiod
319*3d8817e4Smiod static void
annotate_with_count(char * buf,unsigned int width,int line_num,PTR arg)320*3d8817e4Smiod annotate_with_count (char *buf, unsigned int width, int line_num, PTR arg)
321*3d8817e4Smiod {
322*3d8817e4Smiod Source_File *sf = arg;
323*3d8817e4Smiod Sym *b;
324*3d8817e4Smiod unsigned int i;
325*3d8817e4Smiod static unsigned long last_count;
326*3d8817e4Smiod unsigned long last_print = (unsigned long) -1;
327*3d8817e4Smiod
328*3d8817e4Smiod b = NULL;
329*3d8817e4Smiod
330*3d8817e4Smiod if (line_num <= sf->num_lines)
331*3d8817e4Smiod b = sf->line[line_num - 1];
332*3d8817e4Smiod
333*3d8817e4Smiod if (!b)
334*3d8817e4Smiod {
335*3d8817e4Smiod for (i = 0; i < width; i++)
336*3d8817e4Smiod buf[i] = ' ';
337*3d8817e4Smiod buf[width] = '\0';
338*3d8817e4Smiod }
339*3d8817e4Smiod else
340*3d8817e4Smiod {
341*3d8817e4Smiod char tmpbuf[NBBS * 30];
342*3d8817e4Smiod char *p;
343*3d8817e4Smiod unsigned long ncalls;
344*3d8817e4Smiod int ncalls_set;
345*3d8817e4Smiod unsigned int len;
346*3d8817e4Smiod
347*3d8817e4Smiod ++num_executable_lines;
348*3d8817e4Smiod
349*3d8817e4Smiod p = tmpbuf;
350*3d8817e4Smiod *p = '\0';
351*3d8817e4Smiod
352*3d8817e4Smiod ncalls = 0;
353*3d8817e4Smiod ncalls_set = 0;
354*3d8817e4Smiod
355*3d8817e4Smiod /* If this is a function entry point, label the line no matter what.
356*3d8817e4Smiod Otherwise, we're in the middle of a function, so check to see
357*3d8817e4Smiod if the first basic-block address is larger than the starting
358*3d8817e4Smiod address of the line. If so, then this line begins with a
359*3d8817e4Smiod a portion of the previous basic-block, so print that prior
360*3d8817e4Smiod execution count (if bb_annotate_all_lines is set). */
361*3d8817e4Smiod if (b->is_func)
362*3d8817e4Smiod {
363*3d8817e4Smiod sprintf (p, "%lu", b->ncalls);
364*3d8817e4Smiod p += strlen (p);
365*3d8817e4Smiod last_count = b->ncalls;
366*3d8817e4Smiod last_print = last_count;
367*3d8817e4Smiod ncalls = b->ncalls;
368*3d8817e4Smiod ncalls_set = 1;
369*3d8817e4Smiod }
370*3d8817e4Smiod else if (bb_annotate_all_lines
371*3d8817e4Smiod && b->bb_addr[0] && b->bb_addr[0] > b->addr)
372*3d8817e4Smiod {
373*3d8817e4Smiod sprintf (p, "%lu", last_count);
374*3d8817e4Smiod p += strlen (p);
375*3d8817e4Smiod last_print = last_count;
376*3d8817e4Smiod ncalls = last_count;
377*3d8817e4Smiod ncalls_set = 1;
378*3d8817e4Smiod }
379*3d8817e4Smiod
380*3d8817e4Smiod /* Loop through all of this line's basic-blocks. For each one,
381*3d8817e4Smiod update last_count, then compress sequential identical counts
382*3d8817e4Smiod (if bb_annotate_all_lines) and print the execution count. */
383*3d8817e4Smiod
384*3d8817e4Smiod for (i = 0; i < NBBS && b->bb_addr[i]; i++)
385*3d8817e4Smiod {
386*3d8817e4Smiod last_count = b->bb_calls[i];
387*3d8817e4Smiod if (! ncalls_set)
388*3d8817e4Smiod {
389*3d8817e4Smiod ncalls = 0;
390*3d8817e4Smiod ncalls_set = 1;
391*3d8817e4Smiod }
392*3d8817e4Smiod ncalls += last_count;
393*3d8817e4Smiod
394*3d8817e4Smiod if (bb_annotate_all_lines && last_count == last_print)
395*3d8817e4Smiod continue;
396*3d8817e4Smiod
397*3d8817e4Smiod if (p > tmpbuf)
398*3d8817e4Smiod *p++ = ',';
399*3d8817e4Smiod sprintf (p, "%lu", last_count);
400*3d8817e4Smiod p += strlen (p);
401*3d8817e4Smiod
402*3d8817e4Smiod last_print = last_count;
403*3d8817e4Smiod }
404*3d8817e4Smiod
405*3d8817e4Smiod /* We're done. If nothing has been printed on this line,
406*3d8817e4Smiod print the last execution count (bb_annotate_all_lines),
407*3d8817e4Smiod which could be from either a previous line (if there were
408*3d8817e4Smiod no BBs on this line), or from this line (if all our BB
409*3d8817e4Smiod counts were compressed out because they were identical). */
410*3d8817e4Smiod
411*3d8817e4Smiod if (bb_annotate_all_lines && p == tmpbuf)
412*3d8817e4Smiod {
413*3d8817e4Smiod sprintf (p, "%lu", last_count);
414*3d8817e4Smiod p += strlen (p);
415*3d8817e4Smiod ncalls = last_count;
416*3d8817e4Smiod ncalls_set = 1;
417*3d8817e4Smiod }
418*3d8817e4Smiod
419*3d8817e4Smiod if (! ncalls_set)
420*3d8817e4Smiod {
421*3d8817e4Smiod unsigned int c;
422*3d8817e4Smiod
423*3d8817e4Smiod for (c = 0; c < width; c++)
424*3d8817e4Smiod buf[c] = ' ';
425*3d8817e4Smiod buf[width] = '\0';
426*3d8817e4Smiod return;
427*3d8817e4Smiod }
428*3d8817e4Smiod
429*3d8817e4Smiod ++num_lines_executed;
430*3d8817e4Smiod
431*3d8817e4Smiod if (ncalls < bb_min_calls)
432*3d8817e4Smiod {
433*3d8817e4Smiod strcpy (tmpbuf, "#####");
434*3d8817e4Smiod p = tmpbuf + 5;
435*3d8817e4Smiod }
436*3d8817e4Smiod
437*3d8817e4Smiod strcpy (p, " -> ");
438*3d8817e4Smiod p += 4;
439*3d8817e4Smiod
440*3d8817e4Smiod len = p - tmpbuf;
441*3d8817e4Smiod if (len >= width)
442*3d8817e4Smiod {
443*3d8817e4Smiod strncpy (buf, tmpbuf, width);
444*3d8817e4Smiod buf[width] = '\0';
445*3d8817e4Smiod }
446*3d8817e4Smiod else
447*3d8817e4Smiod {
448*3d8817e4Smiod unsigned int c;
449*3d8817e4Smiod
450*3d8817e4Smiod strcpy (buf + width - len, tmpbuf);
451*3d8817e4Smiod for (c = 0; c < width - len; ++c)
452*3d8817e4Smiod buf[c] = ' ';
453*3d8817e4Smiod }
454*3d8817e4Smiod }
455*3d8817e4Smiod }
456*3d8817e4Smiod
457*3d8817e4Smiod /* Annotate the files named in SOURCE_FILES with basic-block statistics
458*3d8817e4Smiod (execution counts). After each source files, a few statistics
459*3d8817e4Smiod regarding that source file are printed. */
460*3d8817e4Smiod
461*3d8817e4Smiod void
print_annotated_source()462*3d8817e4Smiod print_annotated_source ()
463*3d8817e4Smiod {
464*3d8817e4Smiod Sym *sym, *line_stats, *new_line;
465*3d8817e4Smiod Source_File *sf;
466*3d8817e4Smiod int i, table_len;
467*3d8817e4Smiod FILE *ofp;
468*3d8817e4Smiod
469*3d8817e4Smiod /* Find maximum line number for each source file that user is
470*3d8817e4Smiod interested in: */
471*3d8817e4Smiod for (sym = symtab.base; sym < symtab.limit; ++sym)
472*3d8817e4Smiod {
473*3d8817e4Smiod /* Accept symbol if it's file is known, its line number is
474*3d8817e4Smiod bigger than anything we have seen for that file so far and
475*3d8817e4Smiod if it's in the INCL_ANNO table or there is no INCL_ANNO
476*3d8817e4Smiod table and it does not appear in the EXCL_ANNO table. */
477*3d8817e4Smiod if (sym->file && sym->line_num > sym->file->num_lines
478*3d8817e4Smiod && (sym_lookup (&syms[INCL_ANNO], sym->addr)
479*3d8817e4Smiod || (syms[INCL_ANNO].len == 0
480*3d8817e4Smiod && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
481*3d8817e4Smiod {
482*3d8817e4Smiod sym->file->num_lines = sym->line_num;
483*3d8817e4Smiod }
484*3d8817e4Smiod }
485*3d8817e4Smiod
486*3d8817e4Smiod /* Allocate line descriptors: */
487*3d8817e4Smiod for (sf = first_src_file; sf; sf = sf->next)
488*3d8817e4Smiod {
489*3d8817e4Smiod if (sf->num_lines > 0)
490*3d8817e4Smiod {
491*3d8817e4Smiod sf->line = (void *) xmalloc (sf->num_lines * sizeof (sf->line[0]));
492*3d8817e4Smiod memset (sf->line, 0, sf->num_lines * sizeof (sf->line[0]));
493*3d8817e4Smiod }
494*3d8817e4Smiod }
495*3d8817e4Smiod
496*3d8817e4Smiod /* Count executions per line: */
497*3d8817e4Smiod for (sym = symtab.base; sym < symtab.limit; ++sym)
498*3d8817e4Smiod {
499*3d8817e4Smiod if (sym->file && sym->file->num_lines
500*3d8817e4Smiod && (sym_lookup (&syms[INCL_ANNO], sym->addr)
501*3d8817e4Smiod || (syms[INCL_ANNO].len == 0
502*3d8817e4Smiod && !sym_lookup (&syms[EXCL_ANNO], sym->addr))))
503*3d8817e4Smiod {
504*3d8817e4Smiod sym->file->ncalls += sym->ncalls;
505*3d8817e4Smiod line_stats = sym->file->line[sym->line_num - 1];
506*3d8817e4Smiod
507*3d8817e4Smiod if (!line_stats)
508*3d8817e4Smiod {
509*3d8817e4Smiod /* Common case has at most one basic-block per source line: */
510*3d8817e4Smiod sym->file->line[sym->line_num - 1] = sym;
511*3d8817e4Smiod }
512*3d8817e4Smiod else if (!line_stats->addr)
513*3d8817e4Smiod {
514*3d8817e4Smiod /* sym is the 3rd .. nth basic block for this line: */
515*3d8817e4Smiod line_stats->ncalls += sym->ncalls;
516*3d8817e4Smiod }
517*3d8817e4Smiod else
518*3d8817e4Smiod {
519*3d8817e4Smiod /* sym is the second basic block for this line. */
520*3d8817e4Smiod new_line = (Sym *) xmalloc (sizeof (*new_line));
521*3d8817e4Smiod *new_line = *line_stats;
522*3d8817e4Smiod new_line->addr = 0;
523*3d8817e4Smiod new_line->ncalls += sym->ncalls;
524*3d8817e4Smiod sym->file->line[sym->line_num - 1] = new_line;
525*3d8817e4Smiod }
526*3d8817e4Smiod }
527*3d8817e4Smiod }
528*3d8817e4Smiod
529*3d8817e4Smiod /* Plod over source files, annotating them: */
530*3d8817e4Smiod for (sf = first_src_file; sf; sf = sf->next)
531*3d8817e4Smiod {
532*3d8817e4Smiod if (!sf->num_lines || (ignore_zeros && sf->ncalls == 0))
533*3d8817e4Smiod continue;
534*3d8817e4Smiod
535*3d8817e4Smiod num_executable_lines = num_lines_executed = 0;
536*3d8817e4Smiod
537*3d8817e4Smiod ofp = annotate_source (sf, 16, annotate_with_count, sf);
538*3d8817e4Smiod if (!ofp)
539*3d8817e4Smiod continue;
540*3d8817e4Smiod
541*3d8817e4Smiod if (bb_table_length > 0)
542*3d8817e4Smiod {
543*3d8817e4Smiod fprintf (ofp, _("\n\nTop %d Lines:\n\n Line Count\n\n"),
544*3d8817e4Smiod bb_table_length);
545*3d8817e4Smiod
546*3d8817e4Smiod /* Abuse line arrays---it's not needed anymore: */
547*3d8817e4Smiod qsort (sf->line, sf->num_lines, sizeof (sf->line[0]), cmp_ncalls);
548*3d8817e4Smiod table_len = bb_table_length;
549*3d8817e4Smiod
550*3d8817e4Smiod if (table_len > sf->num_lines)
551*3d8817e4Smiod table_len = sf->num_lines;
552*3d8817e4Smiod
553*3d8817e4Smiod for (i = 0; i < table_len; ++i)
554*3d8817e4Smiod {
555*3d8817e4Smiod sym = sf->line[i];
556*3d8817e4Smiod
557*3d8817e4Smiod if (!sym || sym->ncalls == 0)
558*3d8817e4Smiod break;
559*3d8817e4Smiod
560*3d8817e4Smiod fprintf (ofp, "%9d %10lu\n", sym->line_num, sym->ncalls);
561*3d8817e4Smiod }
562*3d8817e4Smiod }
563*3d8817e4Smiod
564*3d8817e4Smiod free (sf->line);
565*3d8817e4Smiod sf->line = 0;
566*3d8817e4Smiod
567*3d8817e4Smiod fprintf (ofp, _("\nExecution Summary:\n\n"));
568*3d8817e4Smiod fprintf (ofp, _("%9ld Executable lines in this file\n"),
569*3d8817e4Smiod num_executable_lines);
570*3d8817e4Smiod fprintf (ofp, _("%9ld Lines executed\n"), num_lines_executed);
571*3d8817e4Smiod fprintf (ofp, _("%9.2f Percent of the file executed\n"),
572*3d8817e4Smiod num_executable_lines
573*3d8817e4Smiod ? 100.0 * num_lines_executed / (double) num_executable_lines
574*3d8817e4Smiod : 100.0);
575*3d8817e4Smiod fprintf (ofp, _("\n%9lu Total number of line executions\n"),
576*3d8817e4Smiod sf->ncalls);
577*3d8817e4Smiod fprintf (ofp, _("%9.2f Average executions per line\n"),
578*3d8817e4Smiod num_executable_lines
579*3d8817e4Smiod ? (double) sf->ncalls / (double) num_executable_lines
580*3d8817e4Smiod : 0.0);
581*3d8817e4Smiod
582*3d8817e4Smiod if (ofp != stdout)
583*3d8817e4Smiod fclose (ofp);
584*3d8817e4Smiod }
585*3d8817e4Smiod }
586