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