readprofile.c 6.3 KB

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