readprofile.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.2 kb)"
  35. //config: default y
  36. //config: #select PLATFORM_LINUX
  37. //config: help
  38. //config: This allows you to parse /proc/profile for basic profiling.
  39. //applet:IF_READPROFILE(APPLET(readprofile, BB_DIR_USR_SBIN, BB_SUID_DROP))
  40. //kbuild:lib-$(CONFIG_READPROFILE) += readprofile.o
  41. //usage:#define readprofile_trivial_usage
  42. //usage: "[OPTIONS]"
  43. //usage:#define readprofile_full_usage "\n\n"
  44. //usage: " -m mapfile (Default: /boot/System.map)"
  45. //usage: "\n -p profile (Default: /proc/profile)"
  46. //usage: "\n -M NUM Set the profiling multiplier to NUM"
  47. //usage: "\n -i Print only info about the sampling step"
  48. //usage: "\n -v Verbose"
  49. //usage: "\n -a Print all symbols, even if count is 0"
  50. //usage: "\n -b Print individual histogram-bin counts"
  51. //usage: "\n -s Print individual counters within functions"
  52. //usage: "\n -r Reset all the counters (root only)"
  53. //usage: "\n -n Disable byte order auto-detection"
  54. #include "libbb.h"
  55. #include <sys/utsname.h>
  56. #define S_LEN 128
  57. int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  58. int readprofile_main(int argc UNUSED_PARAM, char **argv)
  59. {
  60. FILE *map;
  61. const char *mapFile, *proFile;
  62. unsigned long indx;
  63. size_t len;
  64. uint64_t add0;
  65. unsigned int step;
  66. unsigned int *buf, total, fn_len;
  67. unsigned long long fn_add, next_add; /* current and next address */
  68. char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
  69. char mapline[S_LEN];
  70. char mode[8];
  71. int maplineno;
  72. int multiplier;
  73. unsigned opt;
  74. enum {
  75. OPT_M = (1 << 0),
  76. OPT_m = (1 << 1),
  77. OPT_p = (1 << 2),
  78. OPT_n = (1 << 3),
  79. OPT_a = (1 << 4),
  80. OPT_b = (1 << 5),
  81. OPT_s = (1 << 6),
  82. OPT_i = (1 << 7),
  83. OPT_r = (1 << 8),
  84. OPT_v = (1 << 9),
  85. };
  86. #define optMult (opt & OPT_M)
  87. #define optNative (opt & OPT_n)
  88. #define optAll (opt & OPT_a)
  89. #define optBins (opt & OPT_b)
  90. #define optSub (opt & OPT_s)
  91. #define optInfo (opt & OPT_i)
  92. #define optReset (opt & OPT_r)
  93. #define optVerbose (opt & OPT_v)
  94. #define next (current^1)
  95. proFile = "/proc/profile";
  96. mapFile = "/boot/System.map";
  97. multiplier = 0;
  98. opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
  99. if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
  100. int fd, to_write;
  101. /*
  102. * When writing the multiplier, if the length of the write is
  103. * not sizeof(int), the multiplier is not changed
  104. */
  105. to_write = sizeof(int);
  106. if (!optMult)
  107. to_write = 1; /* sth different from sizeof(int) */
  108. fd = xopen("/proc/profile", O_WRONLY);
  109. xwrite(fd, &multiplier, to_write);
  110. close(fd);
  111. return EXIT_SUCCESS;
  112. }
  113. /*
  114. * Use an fd for the profiling buffer, to skip stdio overhead
  115. */
  116. len = MAXINT(ssize_t);
  117. buf = xmalloc_xopen_read_close(proFile, &len);
  118. len /= sizeof(*buf);
  119. if (!optNative) {
  120. int big = 0, small = 0;
  121. unsigned *p;
  122. for (p = buf+1; p < buf+len; p++) {
  123. if (*p & ~0U << (sizeof(*buf)*4))
  124. big++;
  125. if (*p & ((1 << (sizeof(*buf)*4))-1))
  126. small++;
  127. }
  128. if (big > small) {
  129. bb_error_msg("assuming reversed byte order, "
  130. "use -n to force native byte order");
  131. BUILD_BUG_ON(sizeof(*p) > 8);
  132. for (p = buf; p < buf+len; p++) {
  133. if (sizeof(*p) == 2)
  134. *p = bswap_16(*p);
  135. if (sizeof(*p) == 4)
  136. *p = bswap_32(*p);
  137. if (sizeof(*p) == 8)
  138. *p = bb_bswap_64(*p);
  139. }
  140. }
  141. }
  142. step = buf[0];
  143. if (optInfo) {
  144. printf("Sampling_step: %u\n", step);
  145. return EXIT_SUCCESS;
  146. }
  147. map = xfopen_for_read(mapFile);
  148. add0 = 0;
  149. maplineno = 1;
  150. while (fgets(mapline, S_LEN, map)) {
  151. if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
  152. bb_error_msg_and_die("%s(%i): wrong map line",
  153. mapFile, maplineno);
  154. if (strcmp(fn_name, "_stext") == 0) /* only elf works like this */ {
  155. add0 = fn_add;
  156. break;
  157. }
  158. maplineno++;
  159. }
  160. if (!add0)
  161. bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
  162. /*
  163. * Main loop.
  164. */
  165. total = 0;
  166. indx = 1;
  167. while (fgets(mapline, S_LEN, map)) {
  168. bool header_printed;
  169. unsigned int this;
  170. if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
  171. bb_error_msg_and_die("%s(%i): wrong map line",
  172. mapFile, maplineno);
  173. header_printed = 0;
  174. /* ignore any LEADING (before a '[tT]' symbol is found)
  175. Absolute symbols */
  176. if ((mode[0] == 'A' || mode[0] == '?') && total == 0)
  177. continue;
  178. if ((mode[0]|0x20) != 't' && (mode[0]|0x20) != 'w') {
  179. break; /* only text is profiled */
  180. }
  181. if (indx >= len)
  182. bb_error_msg_and_die("profile address out of range. "
  183. "Wrong map file?");
  184. this = 0;
  185. while (indx < (next_add-add0)/step) {
  186. if (optBins && (buf[indx] || optAll)) {
  187. if (!header_printed) {
  188. printf("%s:\n", fn_name);
  189. header_printed = 1;
  190. }
  191. printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
  192. }
  193. this += buf[indx++];
  194. }
  195. total += this;
  196. if (optBins) {
  197. if (optVerbose || this > 0)
  198. printf(" total\t\t\t\t%u\n", this);
  199. } else
  200. if ((this || optAll)
  201. && (fn_len = next_add-fn_add) != 0
  202. ) {
  203. if (optVerbose)
  204. printf("%016llx %-40s %6u %8.4f\n", fn_add,
  205. fn_name, this, this/(double)fn_len);
  206. else
  207. printf("%6u %-40s %8.4f\n",
  208. this, fn_name, this/(double)fn_len);
  209. if (optSub) {
  210. unsigned long long scan;
  211. for (scan = (fn_add-add0)/step + 1;
  212. scan < (next_add-add0)/step; scan++) {
  213. unsigned long long addr;
  214. addr = (scan - 1)*step + add0;
  215. printf("\t%#llx\t%s+%#llx\t%u\n",
  216. addr, fn_name, addr - fn_add,
  217. buf[scan]);
  218. }
  219. }
  220. }
  221. fn_add = next_add;
  222. strcpy(fn_name, next_name);
  223. maplineno++;
  224. }
  225. /* clock ticks, out of kernel text - probably modules */
  226. printf("%6u *unknown*\n", buf[len-1]);
  227. /* trailer */
  228. if (optVerbose)
  229. printf("%016x %-40s %6u %8.4f\n",
  230. 0, "total", total, total/(double)(fn_add-add0));
  231. else
  232. printf("%6u %-40s %8.4f\n",
  233. total, "total", total/(double)(fn_add-add0));
  234. if (ENABLE_FEATURE_CLEAN_UP) {
  235. fclose(map);
  236. free(buf);
  237. }
  238. return EXIT_SUCCESS;
  239. }