basic_blocks.c 15 KB

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