hist.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /* hist.c - Histogram related operations.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "gprof.h"
  17. #include "libiberty.h"
  18. #include "search_list.h"
  19. #include "source.h"
  20. #include "symtab.h"
  21. #include "corefile.h"
  22. #include "gmon_io.h"
  23. #include "gmon_out.h"
  24. #include "hist.h"
  25. #include "sym_ids.h"
  26. #include "utils.h"
  27. #include "math.h"
  28. #include "stdio.h"
  29. #include "stdlib.h"
  30. #define UNITS_TO_CODE (offset_to_code / sizeof(UNIT))
  31. static void scale_and_align_entries (void);
  32. static void print_header (int);
  33. static void print_line (Sym *, double);
  34. static int cmp_time (const PTR, const PTR);
  35. /* Declarations of automatically generated functions to output blurbs. */
  36. extern void flat_blurb (FILE * fp);
  37. static histogram *find_histogram (bfd_vma lowpc, bfd_vma highpc);
  38. static histogram *find_histogram_for_pc (bfd_vma pc);
  39. histogram * histograms;
  40. unsigned num_histograms;
  41. double hist_scale;
  42. static char hist_dimension[16] = "seconds";
  43. static char hist_dimension_abbrev = 's';
  44. static double accum_time; /* Accumulated time so far for print_line(). */
  45. static double total_time; /* Total time for all routines. */
  46. /* Table of SI prefixes for powers of 10 (used to automatically
  47. scale some of the values in the flat profile). */
  48. const struct
  49. {
  50. char prefix;
  51. double scale;
  52. }
  53. SItab[] =
  54. {
  55. { 'T', 1e-12 }, /* tera */
  56. { 'G', 1e-09 }, /* giga */
  57. { 'M', 1e-06 }, /* mega */
  58. { 'K', 1e-03 }, /* kilo */
  59. { ' ', 1e-00 },
  60. { 'm', 1e+03 }, /* milli */
  61. { 'u', 1e+06 }, /* micro */
  62. { 'n', 1e+09 }, /* nano */
  63. { 'p', 1e+12 }, /* pico */
  64. { 'f', 1e+15 }, /* femto */
  65. { 'a', 1e+18 } /* ato */
  66. };
  67. /* Reads just the header part of histogram record into
  68. *RECORD from IFP. FILENAME is the name of IFP and
  69. is provided for formatting error messages only.
  70. If FIRST is non-zero, sets global variables HZ, HIST_DIMENSION,
  71. HIST_DIMENSION_ABBREV, HIST_SCALE. If FIRST is zero, checks
  72. that the new histogram is compatible with already-set values
  73. of those variables and emits an error if that's not so. */
  74. static void
  75. read_histogram_header (histogram *record,
  76. FILE *ifp, const char *filename,
  77. int first)
  78. {
  79. unsigned int profrate;
  80. char n_hist_dimension[15];
  81. char n_hist_dimension_abbrev;
  82. double n_hist_scale;
  83. if (gmon_io_read_vma (ifp, &record->lowpc)
  84. || gmon_io_read_vma (ifp, &record->highpc)
  85. || gmon_io_read_32 (ifp, &record->num_bins)
  86. || gmon_io_read_32 (ifp, &profrate)
  87. || gmon_io_read (ifp, n_hist_dimension, 15)
  88. || gmon_io_read (ifp, &n_hist_dimension_abbrev, 1))
  89. {
  90. fprintf (stderr, _("%s: %s: unexpected end of file\n"),
  91. whoami, filename);
  92. done (1);
  93. }
  94. n_hist_scale = (double)((record->highpc - record->lowpc) / sizeof (UNIT))
  95. / record->num_bins;
  96. if (first)
  97. {
  98. /* We don't try to veryfy profrate is the same for all histogram
  99. records. If we have two histogram records for the same
  100. address range and profiling samples is done as often
  101. as possible as opposed on timer, then the actual profrate will
  102. be slightly different. Most of the time the difference does not
  103. matter and insisting that profiling rate is exactly the same
  104. will only create inconvenient. */
  105. hz = profrate;
  106. memcpy (hist_dimension, n_hist_dimension, 15);
  107. hist_dimension_abbrev = n_hist_dimension_abbrev;
  108. hist_scale = n_hist_scale;
  109. }
  110. else
  111. {
  112. if (strncmp (n_hist_dimension, hist_dimension, 15) != 0)
  113. {
  114. fprintf (stderr,
  115. _("%s: dimension unit changed between histogram records\n"
  116. "%s: from '%s'\n"
  117. "%s: to '%s'\n"),
  118. whoami, whoami, hist_dimension, whoami, n_hist_dimension);
  119. done (1);
  120. }
  121. if (n_hist_dimension_abbrev != hist_dimension_abbrev)
  122. {
  123. fprintf (stderr,
  124. _("%s: dimension abbreviation changed between histogram records\n"
  125. "%s: from '%c'\n"
  126. "%s: to '%c'\n"),
  127. whoami, whoami, hist_dimension_abbrev, whoami, n_hist_dimension_abbrev);
  128. done (1);
  129. }
  130. /* The only reason we require the same scale for histograms is that
  131. there's code (notably printing code), that prints units,
  132. and it would be very confusing to have one unit mean different
  133. things for different functions. */
  134. if (fabs (hist_scale - n_hist_scale) > 0.000001)
  135. {
  136. fprintf (stderr,
  137. _("%s: different scales in histogram records"),
  138. whoami);
  139. done (1);
  140. }
  141. }
  142. }
  143. /* Read the histogram from file IFP. FILENAME is the name of IFP and
  144. is provided for formatting error messages only. */
  145. void
  146. hist_read_rec (FILE * ifp, const char *filename)
  147. {
  148. bfd_vma lowpc, highpc;
  149. histogram n_record;
  150. histogram *record, *existing_record;
  151. unsigned i;
  152. /* 1. Read the header and see if there's existing record for the
  153. same address range and that there are no overlapping records. */
  154. read_histogram_header (&n_record, ifp, filename, num_histograms == 0);
  155. existing_record = find_histogram (n_record.lowpc, n_record.highpc);
  156. if (existing_record)
  157. {
  158. record = existing_record;
  159. }
  160. else
  161. {
  162. /* If this record overlaps, but does not completely match an existing
  163. record, it's an error. */
  164. lowpc = n_record.lowpc;
  165. highpc = n_record.highpc;
  166. hist_clip_symbol_address (&lowpc, &highpc);
  167. if (lowpc != highpc)
  168. {
  169. fprintf (stderr,
  170. _("%s: overlapping histogram records\n"),
  171. whoami);
  172. done (1);
  173. }
  174. /* This is new record. Add it to global array and allocate space for
  175. the samples. */
  176. histograms = (struct histogram *)
  177. xrealloc (histograms, sizeof (histogram) * (num_histograms + 1));
  178. memcpy (histograms + num_histograms,
  179. &n_record, sizeof (histogram));
  180. record = &histograms[num_histograms];
  181. ++num_histograms;
  182. record->sample = (int *) xmalloc (record->num_bins
  183. * sizeof (record->sample[0]));
  184. memset (record->sample, 0, record->num_bins * sizeof (record->sample[0]));
  185. }
  186. /* 2. We have either a new record (with zeroed histogram data), or an existing
  187. record with some data in the histogram already. Read new data into the
  188. record, adding hit counts. */
  189. DBG (SAMPLEDEBUG,
  190. printf ("[hist_read_rec] n_lowpc 0x%lx n_highpc 0x%lx ncnt %u\n",
  191. (unsigned long) record->lowpc, (unsigned long) record->highpc,
  192. record->num_bins));
  193. for (i = 0; i < record->num_bins; ++i)
  194. {
  195. UNIT count;
  196. if (fread (&count[0], sizeof (count), 1, ifp) != 1)
  197. {
  198. fprintf (stderr,
  199. _("%s: %s: unexpected EOF after reading %u of %u samples\n"),
  200. whoami, filename, i, record->num_bins);
  201. done (1);
  202. }
  203. record->sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) & count[0]);
  204. DBG (SAMPLEDEBUG,
  205. printf ("[hist_read_rec] 0x%lx: %u\n",
  206. (unsigned long) (record->lowpc
  207. + i * (record->highpc - record->lowpc)
  208. / record->num_bins),
  209. record->sample[i]));
  210. }
  211. }
  212. /* Write all execution histograms file OFP. FILENAME is the name
  213. of OFP and is provided for formatting error-messages only. */
  214. void
  215. hist_write_hist (FILE * ofp, const char *filename)
  216. {
  217. UNIT count;
  218. unsigned int i, r;
  219. for (r = 0; r < num_histograms; ++r)
  220. {
  221. histogram *record = &histograms[r];
  222. /* Write header. */
  223. if (gmon_io_write_8 (ofp, GMON_TAG_TIME_HIST)
  224. || gmon_io_write_vma (ofp, record->lowpc)
  225. || gmon_io_write_vma (ofp, record->highpc)
  226. || gmon_io_write_32 (ofp, record->num_bins)
  227. || gmon_io_write_32 (ofp, hz)
  228. || gmon_io_write (ofp, hist_dimension, 15)
  229. || gmon_io_write (ofp, &hist_dimension_abbrev, 1))
  230. {
  231. perror (filename);
  232. done (1);
  233. }
  234. for (i = 0; i < record->num_bins; ++i)
  235. {
  236. bfd_put_16 (core_bfd, (bfd_vma) record->sample[i], (bfd_byte *) &count[0]);
  237. if (fwrite (&count[0], sizeof (count), 1, ofp) != 1)
  238. {
  239. perror (filename);
  240. done (1);
  241. }
  242. }
  243. }
  244. }
  245. /* Calculate scaled entry point addresses (to save time in
  246. hist_assign_samples), and, on architectures that have procedure
  247. entry masks at the start of a function, possibly push the scaled
  248. entry points over the procedure entry mask, if it turns out that
  249. the entry point is in one bin and the code for a routine is in the
  250. next bin. */
  251. static void
  252. scale_and_align_entries (void)
  253. {
  254. Sym *sym;
  255. bfd_vma bin_of_entry;
  256. bfd_vma bin_of_code;
  257. for (sym = symtab.base; sym < symtab.limit; sym++)
  258. {
  259. histogram *r = find_histogram_for_pc (sym->addr);
  260. sym->hist.scaled_addr = sym->addr / sizeof (UNIT);
  261. if (r)
  262. {
  263. bin_of_entry = (sym->hist.scaled_addr - r->lowpc) / hist_scale;
  264. bin_of_code = ((sym->hist.scaled_addr + UNITS_TO_CODE - r->lowpc)
  265. / hist_scale);
  266. if (bin_of_entry < bin_of_code)
  267. {
  268. DBG (SAMPLEDEBUG,
  269. printf ("[scale_and_align_entries] pushing 0x%lx to 0x%lx\n",
  270. (unsigned long) sym->hist.scaled_addr,
  271. (unsigned long) (sym->hist.scaled_addr
  272. + UNITS_TO_CODE)));
  273. sym->hist.scaled_addr += UNITS_TO_CODE;
  274. }
  275. }
  276. }
  277. }
  278. /* Assign samples to the symbol to which they belong.
  279. Histogram bin I covers some address range [BIN_LOWPC,BIN_HIGH_PC)
  280. which may overlap one more symbol address ranges. If a symbol
  281. overlaps with the bin's address range by O percent, then O percent
  282. of the bin's count is credited to that symbol.
  283. There are three cases as to where BIN_LOW_PC and BIN_HIGH_PC can be
  284. with respect to the symbol's address range [SYM_LOW_PC,
  285. SYM_HIGH_PC) as shown in the following diagram. OVERLAP computes
  286. the distance (in UNITs) between the arrows, the fraction of the
  287. sample that is to be credited to the symbol which starts at
  288. SYM_LOW_PC.
  289. sym_low_pc sym_high_pc
  290. | |
  291. v v
  292. +-----------------------------------------------+
  293. | |
  294. | ->| |<- ->| |<- ->| |<- |
  295. | | | | | |
  296. +---------+ +---------+ +---------+
  297. ^ ^ ^ ^ ^ ^
  298. | | | | | |
  299. bin_low_pc bin_high_pc bin_low_pc bin_high_pc bin_low_pc bin_high_pc
  300. For the VAX we assert that samples will never fall in the first two
  301. bytes of any routine, since that is the entry mask, thus we call
  302. scale_and_align_entries() to adjust the entry points if the entry
  303. mask falls in one bin but the code for the routine doesn't start
  304. until the next bin. In conjunction with the alignment of routine
  305. addresses, this should allow us to have only one sample for every
  306. four bytes of text space and never have any overlap (the two end
  307. cases, above). */
  308. static void
  309. hist_assign_samples_1 (histogram *r)
  310. {
  311. bfd_vma bin_low_pc, bin_high_pc;
  312. bfd_vma sym_low_pc, sym_high_pc;
  313. bfd_vma overlap, addr;
  314. unsigned int bin_count;
  315. unsigned int i, j, k;
  316. double count_time, credit;
  317. bfd_vma lowpc = r->lowpc / sizeof (UNIT);
  318. /* Iterate over all sample bins. */
  319. for (i = 0, k = 1; i < r->num_bins; ++i)
  320. {
  321. bin_count = r->sample[i];
  322. if (! bin_count)
  323. continue;
  324. bin_low_pc = lowpc + (bfd_vma) (hist_scale * i);
  325. bin_high_pc = lowpc + (bfd_vma) (hist_scale * (i + 1));
  326. count_time = bin_count;
  327. DBG (SAMPLEDEBUG,
  328. printf (
  329. "[assign_samples] bin_low_pc=0x%lx, bin_high_pc=0x%lx, bin_count=%u\n",
  330. (unsigned long) (sizeof (UNIT) * bin_low_pc),
  331. (unsigned long) (sizeof (UNIT) * bin_high_pc),
  332. bin_count));
  333. total_time += count_time;
  334. /* Credit all symbols that are covered by bin I.
  335. PR gprof/13325: Make sure that K does not get decremented
  336. and J will never be less than 0. */
  337. for (j = k - 1; j < symtab.len; k = ++j)
  338. {
  339. sym_low_pc = symtab.base[j].hist.scaled_addr;
  340. sym_high_pc = symtab.base[j + 1].hist.scaled_addr;
  341. /* If high end of bin is below entry address,
  342. go for next bin. */
  343. if (bin_high_pc < sym_low_pc)
  344. break;
  345. /* If low end of bin is above high end of symbol,
  346. go for next symbol. */
  347. if (bin_low_pc >= sym_high_pc)
  348. continue;
  349. overlap =
  350. MIN (bin_high_pc, sym_high_pc) - MAX (bin_low_pc, sym_low_pc);
  351. if (overlap > 0)
  352. {
  353. DBG (SAMPLEDEBUG,
  354. printf (
  355. "[assign_samples] [0x%lx,0x%lx) %s gets %f ticks %ld overlap\n",
  356. (unsigned long) symtab.base[j].addr,
  357. (unsigned long) (sizeof (UNIT) * sym_high_pc),
  358. symtab.base[j].name, overlap * count_time / hist_scale,
  359. (long) overlap));
  360. addr = symtab.base[j].addr;
  361. credit = overlap * count_time / hist_scale;
  362. /* Credit symbol if it appears in INCL_FLAT or that
  363. table is empty and it does not appear it in
  364. EXCL_FLAT. */
  365. if (sym_lookup (&syms[INCL_FLAT], addr)
  366. || (syms[INCL_FLAT].len == 0
  367. && !sym_lookup (&syms[EXCL_FLAT], addr)))
  368. {
  369. symtab.base[j].hist.time += credit;
  370. }
  371. else
  372. {
  373. total_time -= credit;
  374. }
  375. }
  376. }
  377. }
  378. DBG (SAMPLEDEBUG, printf ("[assign_samples] total_time %f\n",
  379. total_time));
  380. }
  381. /* Calls 'hist_assign_sampes_1' for all histogram records read so far. */
  382. void
  383. hist_assign_samples (void)
  384. {
  385. unsigned i;
  386. scale_and_align_entries ();
  387. for (i = 0; i < num_histograms; ++i)
  388. hist_assign_samples_1 (&histograms[i]);
  389. }
  390. /* Print header for flag histogram profile. */
  391. static void
  392. print_header (int prefix)
  393. {
  394. char unit[64];
  395. sprintf (unit, _("%c%c/call"), prefix, hist_dimension_abbrev);
  396. if (bsd_style_output)
  397. {
  398. printf (_("\ngranularity: each sample hit covers %ld byte(s)"),
  399. (long) hist_scale * (long) sizeof (UNIT));
  400. if (total_time > 0.0)
  401. {
  402. printf (_(" for %.2f%% of %.2f %s\n\n"),
  403. 100.0 / total_time, total_time / hz, hist_dimension);
  404. }
  405. }
  406. else
  407. {
  408. printf (_("\nEach sample counts as %g %s.\n"), 1.0 / hz, hist_dimension);
  409. }
  410. if (total_time <= 0.0)
  411. {
  412. printf (_(" no time accumulated\n\n"));
  413. /* This doesn't hurt since all the numerators will be zero. */
  414. total_time = 1.0;
  415. }
  416. printf ("%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
  417. "% ", _("cumulative"), _("self "), "", _("self "), _("total "),
  418. "");
  419. printf ("%5.5s %9.9s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
  420. _("time"), hist_dimension, hist_dimension, _("calls"), unit, unit,
  421. _("name"));
  422. }
  423. static void
  424. print_line (Sym *sym, double scale)
  425. {
  426. if (ignore_zeros && sym->ncalls == 0 && sym->hist.time == 0)
  427. return;
  428. accum_time += sym->hist.time;
  429. if (bsd_style_output)
  430. printf ("%5.1f %10.2f %8.2f",
  431. total_time > 0.0 ? 100 * sym->hist.time / total_time : 0.0,
  432. accum_time / hz, sym->hist.time / hz);
  433. else
  434. printf ("%6.2f %9.2f %8.2f",
  435. total_time > 0.0 ? 100 * sym->hist.time / total_time : 0.0,
  436. accum_time / hz, sym->hist.time / hz);
  437. if (sym->ncalls != 0)
  438. printf (" %8lu %8.2f %8.2f ",
  439. sym->ncalls, scale * sym->hist.time / hz / sym->ncalls,
  440. scale * (sym->hist.time + sym->cg.child_time) / hz / sym->ncalls);
  441. else
  442. printf (" %8.8s %8.8s %8.8s ", "", "", "");
  443. if (bsd_style_output)
  444. print_name (sym);
  445. else
  446. print_name_only (sym);
  447. printf ("\n");
  448. }
  449. /* Compare LP and RP. The primary comparison key is execution time,
  450. the secondary is number of invocation, and the tertiary is the
  451. lexicographic order of the function names. */
  452. static int
  453. cmp_time (const PTR lp, const PTR rp)
  454. {
  455. const Sym *left = *(const Sym **) lp;
  456. const Sym *right = *(const Sym **) rp;
  457. double time_diff;
  458. time_diff = right->hist.time - left->hist.time;
  459. if (time_diff > 0.0)
  460. return 1;
  461. if (time_diff < 0.0)
  462. return -1;
  463. if (right->ncalls > left->ncalls)
  464. return 1;
  465. if (right->ncalls < left->ncalls)
  466. return -1;
  467. return strcmp (left->name, right->name);
  468. }
  469. /* Print the flat histogram profile. */
  470. void
  471. hist_print (void)
  472. {
  473. Sym **time_sorted_syms, *top_dog, *sym;
  474. unsigned int sym_index;
  475. unsigned log_scale;
  476. double top_time;
  477. bfd_vma addr;
  478. if (first_output)
  479. first_output = false;
  480. else
  481. printf ("\f\n");
  482. accum_time = 0.0;
  483. if (bsd_style_output)
  484. {
  485. if (print_descriptions)
  486. {
  487. printf (_("\n\n\nflat profile:\n"));
  488. flat_blurb (stdout);
  489. }
  490. }
  491. else
  492. {
  493. printf (_("Flat profile:\n"));
  494. }
  495. /* Sort the symbol table by time (call-count and name as secondary
  496. and tertiary keys). */
  497. time_sorted_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
  498. for (sym_index = 0; sym_index < symtab.len; ++sym_index)
  499. time_sorted_syms[sym_index] = &symtab.base[sym_index];
  500. qsort (time_sorted_syms, symtab.len, sizeof (Sym *), cmp_time);
  501. if (bsd_style_output)
  502. {
  503. log_scale = 5; /* Milli-seconds is BSD-default. */
  504. }
  505. else
  506. {
  507. /* Search for symbol with highest per-call
  508. execution time and scale accordingly. */
  509. log_scale = 0;
  510. top_dog = 0;
  511. top_time = 0.0;
  512. for (sym_index = 0; sym_index < symtab.len; ++sym_index)
  513. {
  514. sym = time_sorted_syms[sym_index];
  515. if (sym->ncalls != 0)
  516. {
  517. double call_time;
  518. call_time = (sym->hist.time + sym->cg.child_time) / sym->ncalls;
  519. if (call_time > top_time)
  520. {
  521. top_dog = sym;
  522. top_time = call_time;
  523. }
  524. }
  525. }
  526. if (top_dog && top_dog->ncalls != 0 && top_time > 0.0)
  527. {
  528. top_time /= hz;
  529. for (log_scale = 0; log_scale < ARRAY_SIZE (SItab); log_scale ++)
  530. {
  531. double scaled_value = SItab[log_scale].scale * top_time;
  532. if (scaled_value >= 1.0 && scaled_value < 1000.0)
  533. break;
  534. }
  535. }
  536. }
  537. /* For now, the dimension is always seconds. In the future, we
  538. may also want to support other (pseudo-)dimensions (such as
  539. I-cache misses etc.). */
  540. print_header (SItab[log_scale].prefix);
  541. for (sym_index = 0; sym_index < symtab.len; ++sym_index)
  542. {
  543. addr = time_sorted_syms[sym_index]->addr;
  544. /* Print symbol if its in INCL_FLAT table or that table
  545. is empty and the symbol is not in EXCL_FLAT. */
  546. if (sym_lookup (&syms[INCL_FLAT], addr)
  547. || (syms[INCL_FLAT].len == 0
  548. && !sym_lookup (&syms[EXCL_FLAT], addr)))
  549. print_line (time_sorted_syms[sym_index], SItab[log_scale].scale);
  550. }
  551. free (time_sorted_syms);
  552. if (print_descriptions && !bsd_style_output)
  553. flat_blurb (stdout);
  554. }
  555. int
  556. hist_check_address (unsigned address)
  557. {
  558. unsigned i;
  559. for (i = 0; i < num_histograms; ++i)
  560. if (histograms[i].lowpc <= address && address < histograms[i].highpc)
  561. return 1;
  562. return 0;
  563. }
  564. #if ! defined(min)
  565. #define min(a,b) (((a)<(b)) ? (a) : (b))
  566. #endif
  567. #if ! defined(max)
  568. #define max(a,b) (((a)>(b)) ? (a) : (b))
  569. #endif
  570. void
  571. hist_clip_symbol_address (bfd_vma *p_lowpc, bfd_vma *p_highpc)
  572. {
  573. unsigned i;
  574. int found = 0;
  575. if (num_histograms == 0)
  576. {
  577. *p_highpc = *p_lowpc;
  578. return;
  579. }
  580. for (i = 0; i < num_histograms; ++i)
  581. {
  582. bfd_vma common_low, common_high;
  583. common_low = max (histograms[i].lowpc, *p_lowpc);
  584. common_high = min (histograms[i].highpc, *p_highpc);
  585. if (common_low < common_high)
  586. {
  587. if (found)
  588. {
  589. fprintf (stderr,
  590. _("%s: found a symbol that covers "
  591. "several histogram records"),
  592. whoami);
  593. done (1);
  594. }
  595. found = 1;
  596. *p_lowpc = common_low;
  597. *p_highpc = common_high;
  598. }
  599. }
  600. if (!found)
  601. *p_highpc = *p_lowpc;
  602. }
  603. /* Find and return exising histogram record having the same lowpc and
  604. highpc as passed via the parameters. Return NULL if nothing is found.
  605. The return value is valid until any new histogram is read. */
  606. static histogram *
  607. find_histogram (bfd_vma lowpc, bfd_vma highpc)
  608. {
  609. unsigned i;
  610. for (i = 0; i < num_histograms; ++i)
  611. {
  612. if (histograms[i].lowpc == lowpc && histograms[i].highpc == highpc)
  613. return &histograms[i];
  614. }
  615. return 0;
  616. }
  617. /* Given a PC, return histogram record which address range include this PC.
  618. Return NULL if there's no such record. */
  619. static histogram *
  620. find_histogram_for_pc (bfd_vma pc)
  621. {
  622. unsigned i;
  623. for (i = 0; i < num_histograms; ++i)
  624. {
  625. if (histograms[i].lowpc <= pc && pc < histograms[i].highpc)
  626. return &histograms[i];
  627. }
  628. return 0;
  629. }