readprofile.c 6.1 KB

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