readprofile.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 tarball for details.
  8. */
  9. /*
  10. * 1999-02-22 Arkadiusz Mi¶kiewicz <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. #include "busybox.h"
  34. #include <sys/utsname.h>
  35. #define S_LEN 128
  36. /* These are the defaults */
  37. static const char defaultmap[] = "/boot/System.map";
  38. static const char defaultpro[] = "/proc/profile";
  39. int readprofile_main(int argc, char **argv)
  40. {
  41. FILE *map;
  42. const char *mapFile, *proFile, *mult = 0;
  43. unsigned long indx = 1;
  44. size_t len;
  45. uint64_t add0 = 0;
  46. unsigned int step;
  47. unsigned int *buf, total, fn_len;
  48. unsigned long long fn_add, next_add; /* current and next address */
  49. char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
  50. char mapline[S_LEN];
  51. char mode[8];
  52. int optAll = 0, optInfo = 0, optReset = 0;
  53. int optVerbose = 0, optNative = 0;
  54. int optBins = 0, optSub = 0;
  55. int maplineno = 1;
  56. int header_printed;
  57. #define next (current^1)
  58. proFile = defaultpro;
  59. mapFile = defaultmap;
  60. opt_complementary = "nn:aa:bb:ss:ii:rr:vv";
  61. getopt32(argc, argv, "M:m:p:nabsirv",
  62. &mult, &mapFile, &proFile,
  63. &optNative, &optAll, &optBins, &optSub,
  64. &optInfo, &optReset, &optVerbose);
  65. if (optReset || mult) {
  66. int multiplier, fd, to_write;
  67. /*
  68. * When writing the multiplier, if the length of the write is
  69. * not sizeof(int), the multiplier is not changed
  70. */
  71. if (mult) {
  72. multiplier = xatoi_u(mult);
  73. to_write = sizeof(int);
  74. } else {
  75. multiplier = 0;
  76. to_write = 1; /* sth different from sizeof(int) */
  77. }
  78. fd = xopen(defaultpro, O_WRONLY);
  79. if (full_write(fd, &multiplier, to_write) != to_write)
  80. bb_perror_msg_and_die("error writing %s", defaultpro);
  81. close(fd);
  82. return EXIT_SUCCESS;
  83. }
  84. /*
  85. * Use an fd for the profiling buffer, to skip stdio overhead
  86. */
  87. len = INT_MAX;
  88. buf = xmalloc_open_read_close(proFile, &len);
  89. if (!optNative) {
  90. int entries = len/sizeof(*buf);
  91. int big = 0, small = 0, i;
  92. unsigned *p;
  93. for (p = buf+1; p < buf+entries; p++) {
  94. if (*p & ~0U << (sizeof(*buf)*4))
  95. big++;
  96. if (*p & ((1 << (sizeof(*buf)*4))-1))
  97. small++;
  98. }
  99. if (big > small) {
  100. bb_error_msg("assuming reversed byte order, "
  101. "use -n to force native byte order");
  102. for (p = buf; p < buf+entries; p++)
  103. for (i = 0; i < sizeof(*buf)/2; i++) {
  104. unsigned char *b = (unsigned char *) p;
  105. unsigned char tmp;
  106. tmp = b[i];
  107. b[i] = b[sizeof(*buf)-i-1];
  108. b[sizeof(*buf)-i-1] = tmp;
  109. }
  110. }
  111. }
  112. step = buf[0];
  113. if (optInfo) {
  114. printf("Sampling_step: %i\n", step);
  115. return EXIT_SUCCESS;
  116. }
  117. total = 0;
  118. map = xfopen(mapFile, "r");
  119. while (fgets(mapline, S_LEN, map)) {
  120. if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
  121. bb_error_msg_and_die("%s(%i): wrong map line",
  122. mapFile, maplineno);
  123. if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
  124. add0 = fn_add;
  125. break;
  126. }
  127. maplineno++;
  128. }
  129. if (!add0)
  130. bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
  131. /*
  132. * Main loop.
  133. */
  134. while (fgets(mapline, S_LEN, map)) {
  135. unsigned int this = 0;
  136. if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
  137. bb_error_msg_and_die("%s(%i): wrong map line",
  138. mapFile, maplineno);
  139. header_printed = 0;
  140. /* ignore any LEADING (before a '[tT]' symbol is found)
  141. Absolute symbols */
  142. if ((*mode == 'A' || *mode == '?') && total == 0) continue;
  143. if (*mode != 'T' && *mode != 't' &&
  144. *mode != 'W' && *mode != 'w')
  145. break; /* only text is profiled */
  146. if (indx >= len / sizeof(*buf))
  147. bb_error_msg_and_die("profile address out of range. "
  148. "Wrong map file?");
  149. while (indx < (next_add-add0)/step) {
  150. if (optBins && (buf[indx] || optAll)) {
  151. if (!header_printed) {
  152. printf("%s:\n", fn_name);
  153. header_printed = 1;
  154. }
  155. printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
  156. }
  157. this += buf[indx++];
  158. }
  159. total += this;
  160. if (optBins) {
  161. if (optVerbose || this > 0)
  162. printf(" total\t\t\t\t%u\n", this);
  163. } else if ((this || optAll) &&
  164. (fn_len = next_add-fn_add) != 0) {
  165. if (optVerbose)
  166. printf("%016llx %-40s %6i %8.4f\n", fn_add,
  167. fn_name, this, this/(double)fn_len);
  168. else
  169. printf("%6i %-40s %8.4f\n",
  170. this, fn_name, this/(double)fn_len);
  171. if (optSub) {
  172. unsigned long long scan;
  173. for (scan = (fn_add-add0)/step + 1;
  174. scan < (next_add-add0)/step; scan++) {
  175. unsigned long long addr;
  176. addr = (scan - 1)*step + add0;
  177. printf("\t%#llx\t%s+%#llx\t%u\n",
  178. addr, fn_name, addr - fn_add,
  179. buf[scan]);
  180. }
  181. }
  182. }
  183. fn_add = next_add;
  184. strcpy(fn_name, next_name);
  185. maplineno++;
  186. }
  187. /* clock ticks, out of kernel text - probably modules */
  188. printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
  189. /* trailer */
  190. if (optVerbose)
  191. printf("%016x %-40s %6i %8.4f\n",
  192. 0, "total", total, total/(double)(fn_add-add0));
  193. else
  194. printf("%6i %-40s %8.4f\n",
  195. total, "total", total/(double)(fn_add-add0));
  196. fclose(map);
  197. free(buf);
  198. return EXIT_SUCCESS;
  199. }