readprofile.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * readprofile.c - used to read /proc/profile
  4. *
  5. * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /*
  10. * 1999-02-22 Arkadiusz Mickiewicz <misiek@pld.ORG.PL>
  11. * - added Native Language Support
  12. * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
  13. * - 64bit clean patch
  14. * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
  15. * - -M option to write profile multiplier.
  16. * 2001-11-07 Werner Almesberger <wa@almesberger.net>
  17. * - byte order auto-detection and -n option
  18. * 2001-11-09 Werner Almesberger <wa@almesberger.net>
  19. * - skip step size (index 0)
  20. * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
  21. * - make maplineno do something
  22. * 2002-11-28 Mads Martin Joergensen +
  23. * - also try /boot/System.map-`uname -r`
  24. * 2003-04-09 Werner Almesberger <wa@almesberger.net>
  25. * - fixed off-by eight error and improved heuristics in byte order detection
  26. * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM>
  27. * - added -s option; example of use:
  28. * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
  29. *
  30. * Taken from util-linux and adapted for busybox by
  31. * Paul Mundt <lethal@linux-sh.org>.
  32. */
  33. //config:config READPROFILE
  34. //config: bool "readprofile (7.1 kb)"
  35. //config: default y
  36. //config: help
  37. //config: This allows you to parse /proc/profile for basic profiling.
  38. //applet:IF_READPROFILE(APPLET(readprofile, BB_DIR_USR_SBIN, BB_SUID_DROP))
  39. //kbuild:lib-$(CONFIG_READPROFILE) += readprofile.o
  40. //usage:#define readprofile_trivial_usage
  41. //usage: "[OPTIONS]"
  42. //usage:#define readprofile_full_usage "\n\n"
  43. //usage: " -m MAPFILE (Default: /boot/System.map)"
  44. //usage: "\n -p PROFILE (Default: /proc/profile)"
  45. //usage: "\n -M NUM Set the profiling multiplier to NUM"
  46. //usage: "\n -i Print only info about the sampling step"
  47. //usage: "\n -v Verbose"
  48. //usage: "\n -a Print all symbols, even if count is 0"
  49. //usage: "\n -b Print individual histogram-bin counts"
  50. //usage: "\n -s Print individual counters within functions"
  51. //usage: "\n -r Reset all the counters (root only)"
  52. //usage: "\n -n Disable byte order auto-detection"
  53. #include "libbb.h"
  54. #include <sys/utsname.h>
  55. #define S_LEN 128
  56. int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  57. int readprofile_main(int argc UNUSED_PARAM, char **argv)
  58. {
  59. FILE *map;
  60. const char *mapFile, *proFile;
  61. unsigned long indx;
  62. size_t len;
  63. uint64_t add0;
  64. unsigned int step;
  65. unsigned int *buf, total, fn_len;
  66. unsigned long long fn_add, next_add; /* current and next address */
  67. char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
  68. char mapline[S_LEN];
  69. char mode[8];
  70. int maplineno;
  71. int multiplier;
  72. unsigned opt;
  73. enum {
  74. OPT_M = (1 << 0),
  75. OPT_m = (1 << 1),
  76. OPT_p = (1 << 2),
  77. OPT_n = (1 << 3),
  78. OPT_a = (1 << 4),
  79. OPT_b = (1 << 5),
  80. OPT_s = (1 << 6),
  81. OPT_i = (1 << 7),
  82. OPT_r = (1 << 8),
  83. OPT_v = (1 << 9),
  84. };
  85. #define optMult (opt & OPT_M)
  86. #define optNative (opt & OPT_n)
  87. #define optAll (opt & OPT_a)
  88. #define optBins (opt & OPT_b)
  89. #define optSub (opt & OPT_s)
  90. #define optInfo (opt & OPT_i)
  91. #define optReset (opt & OPT_r)
  92. #define optVerbose (opt & OPT_v)
  93. #define next (current^1)
  94. proFile = "/proc/profile";
  95. mapFile = "/boot/System.map";
  96. multiplier = 0;
  97. opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
  98. if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
  99. int fd, to_write;
  100. /*
  101. * When writing the multiplier, if the length of the write is
  102. * not sizeof(int), the multiplier is not changed
  103. */
  104. to_write = sizeof(int);
  105. if (!optMult)
  106. to_write = 1; /* sth different from sizeof(int) */
  107. fd = xopen("/proc/profile", O_WRONLY);
  108. xwrite(fd, &multiplier, to_write);
  109. close(fd);
  110. return EXIT_SUCCESS;
  111. }
  112. /*
  113. * Use an fd for the profiling buffer, to skip stdio overhead
  114. */
  115. len = MAXINT(ssize_t);
  116. buf = xmalloc_xopen_read_close(proFile, &len);
  117. len /= sizeof(*buf);
  118. if (!optNative) {
  119. int big = 0, small = 0;
  120. unsigned *p;
  121. for (p = buf+1; p < buf+len; p++) {
  122. if (*p & ~0U << (sizeof(*buf)*4))
  123. big++;
  124. if (*p & ((1 << (sizeof(*buf)*4))-1))
  125. small++;
  126. }
  127. if (big > small) {
  128. bb_simple_error_msg("assuming reversed byte order, "
  129. "use -n to force native byte order");
  130. BUILD_BUG_ON(sizeof(*p) > 8);
  131. for (p = buf; p < buf+len; p++) {
  132. if (sizeof(*p) == 2)
  133. *p = bswap_16(*p);
  134. if (sizeof(*p) == 4)
  135. *p = bswap_32(*p);
  136. if (sizeof(*p) == 8)
  137. *p = bb_bswap_64(*p);
  138. }
  139. }
  140. }
  141. step = buf[0];
  142. if (optInfo) {
  143. printf("Sampling_step: %u\n", step);
  144. return EXIT_SUCCESS;
  145. }
  146. map = xfopen_for_read(mapFile);
  147. add0 = 0;
  148. maplineno = 1;
  149. while (fgets(mapline, S_LEN, map)) {
  150. if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
  151. bb_error_msg_and_die("%s(%i): wrong map line",
  152. mapFile, maplineno);
  153. if (strcmp(fn_name, "_stext") == 0) /* only elf works like this */ {
  154. add0 = fn_add;
  155. break;
  156. }
  157. maplineno++;
  158. }
  159. if (!add0)
  160. bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
  161. /*
  162. * Main loop.
  163. */
  164. total = 0;
  165. indx = 1;
  166. while (fgets(mapline, S_LEN, map)) {
  167. bool header_printed;
  168. unsigned int this;
  169. if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
  170. bb_error_msg_and_die("%s(%i): wrong map line",
  171. mapFile, maplineno);
  172. header_printed = 0;
  173. /* ignore any LEADING (before a '[tT]' symbol is found)
  174. Absolute symbols */
  175. if ((mode[0] == 'A' || mode[0] == '?') && total == 0)
  176. continue;
  177. if ((mode[0]|0x20) != 't' && (mode[0]|0x20) != 'w') {
  178. break; /* only text is profiled */
  179. }
  180. if (indx >= len)
  181. bb_simple_error_msg_and_die("profile address out of range. "
  182. "Wrong map file?");
  183. this = 0;
  184. while (indx < (next_add-add0)/step) {
  185. if (optBins && (buf[indx] || optAll)) {
  186. if (!header_printed) {
  187. printf("%s:\n", fn_name);
  188. header_printed = 1;
  189. }
  190. printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
  191. }
  192. this += buf[indx++];
  193. }
  194. total += this;
  195. if (optBins) {
  196. if (optVerbose || this > 0)
  197. printf(" total\t\t\t\t%u\n", this);
  198. } else
  199. if ((this || optAll)
  200. && (fn_len = next_add-fn_add) != 0
  201. ) {
  202. if (optVerbose)
  203. printf("%016llx %-40s %6u %8.4f\n", fn_add,
  204. fn_name, this, this/(double)fn_len);
  205. else
  206. printf("%6u %-40s %8.4f\n",
  207. this, fn_name, this/(double)fn_len);
  208. if (optSub) {
  209. unsigned long long scan;
  210. for (scan = (fn_add-add0)/step + 1;
  211. scan < (next_add-add0)/step; scan++) {
  212. unsigned long long addr;
  213. addr = (scan - 1)*step + add0;
  214. printf("\t%#llx\t%s+%#llx\t%u\n",
  215. addr, fn_name, addr - fn_add,
  216. buf[scan]);
  217. }
  218. }
  219. }
  220. fn_add = next_add;
  221. strcpy(fn_name, next_name);
  222. maplineno++;
  223. }
  224. /* clock ticks, out of kernel text - probably modules */
  225. printf("%6u *unknown*\n", buf[len-1]);
  226. /* trailer */
  227. if (optVerbose)
  228. printf("%016x %-40s %6u %8.4f\n",
  229. 0, "total", total, total/(double)(fn_add-add0));
  230. else
  231. printf("%6u %-40s %8.4f\n",
  232. total, "total", total/(double)(fn_add-add0));
  233. if (ENABLE_FEATURE_CLEAN_UP) {
  234. fclose(map);
  235. free(buf);
  236. }
  237. return EXIT_SUCCESS;
  238. }