gmon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*-
  2. * Copyright (c) 1983, 1992, 1993, 2011
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/time.h>
  31. #include <sys/gmon.h>
  32. #include <sys/gmon_out.h>
  33. #include <sys/uio.h>
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include <fcntl.h>
  37. #include <unistd.h>
  38. #include <wchar.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <stddef.h>
  43. #include <unistd.h>
  44. #include <libc-internal.h>
  45. #include <not-cancel.h>
  46. #define TUNABLE_NAMESPACE gmon
  47. #include <elf/dl-tunables.h>
  48. #ifdef PIC
  49. # include <link.h>
  50. static int
  51. callback (struct dl_phdr_info *info, size_t size, void *data)
  52. {
  53. if (info->dlpi_name[0] == '\0')
  54. {
  55. /* The link map for the executable is created by calling
  56. _dl_new_object with "" as filename. dl_iterate_phdr
  57. calls the callback function with filename from the
  58. link map as dlpi_name. */
  59. u_long *load_address = data;
  60. *load_address = (u_long) info->dlpi_addr;
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. #endif
  66. /* Head of basic-block list or NULL. */
  67. struct __bb *__bb_head attribute_hidden;
  68. struct gmonparam _gmonparam attribute_hidden = { GMON_PROF_OFF };
  69. /*
  70. * See profil(2) where this is described:
  71. */
  72. static int s_scale;
  73. #define SCALE_1_TO_1 0x10000L
  74. #define ERR(s) __write_nocancel (STDERR_FILENO, s, sizeof (s) - 1)
  75. void moncontrol (int mode);
  76. void __moncontrol (int mode);
  77. libc_hidden_proto (__moncontrol)
  78. static void write_hist (int fd, u_long load_address);
  79. static void write_call_graph (int fd, u_long load_address);
  80. static void write_bb_counts (int fd);
  81. /*
  82. * Control profiling
  83. * profiling is what mcount checks to see if
  84. * all the data structures are ready.
  85. */
  86. void
  87. __moncontrol (int mode)
  88. {
  89. struct gmonparam *p = &_gmonparam;
  90. /* Treat start request as stop if error or gmon not initialized. */
  91. if (mode && p->state != GMON_PROF_ERROR && p->tos != NULL)
  92. {
  93. /* start */
  94. __profil((void *) p->kcount, p->kcountsize, p->lowpc, s_scale);
  95. p->state = GMON_PROF_ON;
  96. }
  97. else
  98. {
  99. /* stop */
  100. __profil(NULL, 0, 0, 0);
  101. /* Don't change the state if we ran into an error. */
  102. if (p->state != GMON_PROF_ERROR)
  103. p->state = GMON_PROF_OFF;
  104. }
  105. }
  106. libc_hidden_def (__moncontrol)
  107. weak_alias (__moncontrol, moncontrol)
  108. void
  109. __monstartup (u_long lowpc, u_long highpc)
  110. {
  111. int o;
  112. char *cp;
  113. struct gmonparam *p = &_gmonparam;
  114. long int minarcs, maxarcs;
  115. /* Read minarcs/maxarcs tunables. */
  116. minarcs = TUNABLE_GET (minarcs, int32_t, NULL);
  117. maxarcs = TUNABLE_GET (maxarcs, int32_t, NULL);
  118. if (maxarcs < minarcs)
  119. {
  120. ERR("monstartup: maxarcs < minarcs, setting maxarcs = minarcs\n");
  121. maxarcs = minarcs;
  122. }
  123. /*
  124. * If we are incorrectly called twice in a row (without an
  125. * intervening call to _mcleanup), ignore the second call to
  126. * prevent leaking memory.
  127. */
  128. if (p->tos != NULL)
  129. return;
  130. /*
  131. * round lowpc and highpc to multiples of the density we're using
  132. * so the rest of the scaling (here and in gprof) stays in ints.
  133. */
  134. p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
  135. p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
  136. p->textsize = p->highpc - p->lowpc;
  137. /* This looks like a typo, but it's here to align the p->froms
  138. section. */
  139. p->kcountsize = ROUNDUP(p->textsize / HISTFRACTION, sizeof(*p->froms));
  140. p->hashfraction = HASHFRACTION;
  141. p->log_hashfraction = -1;
  142. /* The following test must be kept in sync with the corresponding
  143. test in mcount.c. */
  144. if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) {
  145. /* if HASHFRACTION is a power of two, mcount can use shifting
  146. instead of integer division. Precompute shift amount. */
  147. p->log_hashfraction = ffs(p->hashfraction * sizeof(*p->froms)) - 1;
  148. }
  149. p->fromssize = ROUNDUP(p->textsize / HASHFRACTION, sizeof(*p->froms));
  150. p->tolimit = p->textsize * ARCDENSITY / 100;
  151. if (p->tolimit < minarcs)
  152. p->tolimit = minarcs;
  153. else if (p->tolimit > maxarcs)
  154. p->tolimit = maxarcs;
  155. p->tossize = p->tolimit * sizeof(struct tostruct);
  156. cp = calloc (p->kcountsize + p->fromssize + p->tossize, 1);
  157. if (! cp)
  158. {
  159. ERR("monstartup: out of memory\n");
  160. p->tos = NULL;
  161. p->state = GMON_PROF_ERROR;
  162. return;
  163. }
  164. p->tos = (struct tostruct *)cp;
  165. cp += p->tossize;
  166. p->kcount = (HISTCOUNTER *)cp;
  167. cp += p->kcountsize;
  168. p->froms = (ARCINDEX *)cp;
  169. p->tos[0].link = 0;
  170. o = p->highpc - p->lowpc;
  171. if (p->kcountsize < (u_long) o)
  172. {
  173. #ifndef hp300
  174. s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
  175. #else
  176. /* avoid floating point operations */
  177. int quot = o / p->kcountsize;
  178. if (quot >= 0x10000)
  179. s_scale = 1;
  180. else if (quot >= 0x100)
  181. s_scale = 0x10000 / quot;
  182. else if (o >= 0x800000)
  183. s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
  184. else
  185. s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
  186. #endif
  187. } else
  188. s_scale = SCALE_1_TO_1;
  189. __moncontrol(1);
  190. }
  191. weak_alias (__monstartup, monstartup)
  192. static void
  193. write_hist (int fd, u_long load_address)
  194. {
  195. u_char tag = GMON_TAG_TIME_HIST;
  196. if (_gmonparam.kcountsize > 0)
  197. {
  198. struct real_gmon_hist_hdr
  199. {
  200. char *low_pc;
  201. char *high_pc;
  202. int32_t hist_size;
  203. int32_t prof_rate;
  204. char dimen[15];
  205. char dimen_abbrev;
  206. } thdr;
  207. struct iovec iov[3] =
  208. {
  209. { &tag, sizeof (tag) },
  210. { &thdr, sizeof (struct gmon_hist_hdr) },
  211. { _gmonparam.kcount, _gmonparam.kcountsize }
  212. };
  213. if (sizeof (thdr) != sizeof (struct gmon_hist_hdr)
  214. || (offsetof (struct real_gmon_hist_hdr, low_pc)
  215. != offsetof (struct gmon_hist_hdr, low_pc))
  216. || (offsetof (struct real_gmon_hist_hdr, high_pc)
  217. != offsetof (struct gmon_hist_hdr, high_pc))
  218. || (offsetof (struct real_gmon_hist_hdr, hist_size)
  219. != offsetof (struct gmon_hist_hdr, hist_size))
  220. || (offsetof (struct real_gmon_hist_hdr, prof_rate)
  221. != offsetof (struct gmon_hist_hdr, prof_rate))
  222. || (offsetof (struct real_gmon_hist_hdr, dimen)
  223. != offsetof (struct gmon_hist_hdr, dimen))
  224. || (offsetof (struct real_gmon_hist_hdr, dimen_abbrev)
  225. != offsetof (struct gmon_hist_hdr, dimen_abbrev)))
  226. abort ();
  227. thdr.low_pc = (char *) _gmonparam.lowpc - load_address;
  228. thdr.high_pc = (char *) _gmonparam.highpc - load_address;
  229. thdr.hist_size = _gmonparam.kcountsize / sizeof (HISTCOUNTER);
  230. thdr.prof_rate = __profile_frequency ();
  231. strncpy (thdr.dimen, "seconds", sizeof (thdr.dimen));
  232. thdr.dimen_abbrev = 's';
  233. __writev_nocancel_nostatus (fd, iov, 3);
  234. }
  235. }
  236. static void
  237. write_call_graph (int fd, u_long load_address)
  238. {
  239. #define NARCS_PER_WRITEV 32
  240. u_char tag = GMON_TAG_CG_ARC;
  241. struct gmon_cg_arc_record raw_arc[NARCS_PER_WRITEV]
  242. __attribute__ ((aligned (__alignof__ (char*))));
  243. ARCINDEX from_index, to_index;
  244. u_long from_len;
  245. u_long frompc;
  246. struct iovec iov[2 * NARCS_PER_WRITEV];
  247. int nfilled;
  248. for (nfilled = 0; nfilled < NARCS_PER_WRITEV; ++nfilled)
  249. {
  250. iov[2 * nfilled].iov_base = &tag;
  251. iov[2 * nfilled].iov_len = sizeof (tag);
  252. iov[2 * nfilled + 1].iov_base = &raw_arc[nfilled];
  253. iov[2 * nfilled + 1].iov_len = sizeof (struct gmon_cg_arc_record);
  254. }
  255. nfilled = 0;
  256. from_len = _gmonparam.fromssize / sizeof (*_gmonparam.froms);
  257. for (from_index = 0; from_index < from_len; ++from_index)
  258. {
  259. if (_gmonparam.froms[from_index] == 0)
  260. continue;
  261. frompc = _gmonparam.lowpc;
  262. frompc += (from_index * _gmonparam.hashfraction
  263. * sizeof (*_gmonparam.froms));
  264. for (to_index = _gmonparam.froms[from_index];
  265. to_index != 0;
  266. to_index = _gmonparam.tos[to_index].link)
  267. {
  268. struct arc
  269. {
  270. char *frompc;
  271. char *selfpc;
  272. int32_t count;
  273. }
  274. arc;
  275. arc.frompc = (char *) frompc - load_address;
  276. arc.selfpc = ((char *) _gmonparam.tos[to_index].selfpc
  277. - load_address);
  278. arc.count = _gmonparam.tos[to_index].count;
  279. memcpy (raw_arc + nfilled, &arc, sizeof (raw_arc [0]));
  280. if (++nfilled == NARCS_PER_WRITEV)
  281. {
  282. __writev_nocancel_nostatus (fd, iov, 2 * nfilled);
  283. nfilled = 0;
  284. }
  285. }
  286. }
  287. if (nfilled > 0)
  288. __writev_nocancel_nostatus (fd, iov, 2 * nfilled);
  289. }
  290. static void
  291. write_bb_counts (int fd)
  292. {
  293. struct __bb *grp;
  294. u_char tag = GMON_TAG_BB_COUNT;
  295. size_t ncounts;
  296. size_t i;
  297. struct iovec bbhead[2] =
  298. {
  299. { &tag, sizeof (tag) },
  300. { &ncounts, sizeof (ncounts) }
  301. };
  302. struct iovec bbbody[8];
  303. size_t nfilled;
  304. for (i = 0; i < (sizeof (bbbody) / sizeof (bbbody[0])); i += 2)
  305. {
  306. bbbody[i].iov_len = sizeof (grp->addresses[0]);
  307. bbbody[i + 1].iov_len = sizeof (grp->counts[0]);
  308. }
  309. /* Write each group of basic-block info (all basic-blocks in a
  310. compilation unit form a single group). */
  311. for (grp = __bb_head; grp; grp = grp->next)
  312. {
  313. ncounts = grp->ncounts;
  314. __writev_nocancel_nostatus (fd, bbhead, 2);
  315. for (nfilled = i = 0; i < ncounts; ++i)
  316. {
  317. if (nfilled > (sizeof (bbbody) / sizeof (bbbody[0])) - 2)
  318. {
  319. __writev_nocancel_nostatus (fd, bbbody, nfilled);
  320. nfilled = 0;
  321. }
  322. bbbody[nfilled++].iov_base = (char *) &grp->addresses[i];
  323. bbbody[nfilled++].iov_base = &grp->counts[i];
  324. }
  325. if (nfilled > 0)
  326. __writev_nocancel_nostatus (fd, bbbody, nfilled);
  327. }
  328. }
  329. static void
  330. write_gmon (void)
  331. {
  332. int fd = -1;
  333. char *env;
  334. env = getenv ("GMON_OUT_PREFIX");
  335. if (env != NULL && !__libc_enable_secure)
  336. {
  337. size_t len = strlen (env);
  338. char buf[len + 20];
  339. __snprintf (buf, sizeof (buf), "%s.%u", env, __getpid ());
  340. fd = __open_nocancel (buf, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW
  341. | O_CLOEXEC, 0666);
  342. }
  343. if (fd == -1)
  344. {
  345. fd = __open_nocancel ("gmon.out", O_CREAT | O_TRUNC | O_WRONLY
  346. | O_NOFOLLOW | O_CLOEXEC, 0666);
  347. if (fd < 0)
  348. {
  349. char buf[300];
  350. int errnum = errno;
  351. __fxprintf (NULL, "_mcleanup: gmon.out: %s\n",
  352. __strerror_r (errnum, buf, sizeof buf));
  353. return;
  354. }
  355. }
  356. /* write gmon.out header: */
  357. struct real_gmon_hdr
  358. {
  359. char cookie[4];
  360. int32_t version;
  361. char spare[3 * 4];
  362. } ghdr;
  363. if (sizeof (ghdr) != sizeof (struct gmon_hdr)
  364. || (offsetof (struct real_gmon_hdr, cookie)
  365. != offsetof (struct gmon_hdr, cookie))
  366. || (offsetof (struct real_gmon_hdr, version)
  367. != offsetof (struct gmon_hdr, version)))
  368. abort ();
  369. memcpy (&ghdr.cookie[0], GMON_MAGIC, sizeof (ghdr.cookie));
  370. ghdr.version = GMON_VERSION;
  371. memset (ghdr.spare, '\0', sizeof (ghdr.spare));
  372. __write_nocancel (fd, &ghdr, sizeof (struct gmon_hdr));
  373. /* Get load_address to profile PIE. */
  374. u_long load_address = 0;
  375. #ifdef PIC
  376. __dl_iterate_phdr (callback, &load_address);
  377. #endif
  378. /* write PC histogram: */
  379. write_hist (fd, load_address);
  380. /* write call-graph: */
  381. write_call_graph (fd, load_address);
  382. /* write basic-block execution counts: */
  383. write_bb_counts (fd);
  384. __close_nocancel_nostatus (fd);
  385. }
  386. #ifdef PROF
  387. void
  388. __write_profiling (void)
  389. {
  390. int save = _gmonparam.state;
  391. _gmonparam.state = GMON_PROF_OFF;
  392. if (save == GMON_PROF_ON)
  393. write_gmon ();
  394. _gmonparam.state = save;
  395. }
  396. /* This symbol isn't used anywhere in the DSO and it is not exported.
  397. This would normally mean it should be removed to get the same API
  398. in static libraries. But since profiling is special in static libs
  399. anyway we keep it. But not when building the DSO since some
  400. quality assurance tests will otherwise trigger. */
  401. weak_alias (__write_profiling, write_profiling)
  402. #endif
  403. void
  404. _mcleanup (void)
  405. {
  406. __moncontrol (0);
  407. if (_gmonparam.state != GMON_PROF_ERROR && _gmonparam.tos != NULL)
  408. write_gmon ();
  409. /* free the memory. */
  410. free (_gmonparam.tos);
  411. /* reset buffer to initial state for safety */
  412. memset(&_gmonparam, 0, sizeof _gmonparam);
  413. /* somewhat confusingly, ON=0, OFF=3 */
  414. _gmonparam.state = GMON_PROF_OFF;
  415. }