readprofile.c 6.9 KB

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