readprofile.c 7.2 KB

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