readprofile.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * readprofile.c - used to read /proc/profile
  3. *
  4. * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
  22. * - added Native Language Support
  23. * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
  24. * - 64bit clean patch
  25. * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
  26. * - -M option to write profile multiplier.
  27. * 2001-11-07 Werner Almesberger <wa@almesberger.net>
  28. * - byte order auto-detection and -n option
  29. * 2001-11-09 Werner Almesberger <wa@almesberger.net>
  30. * - skip step size (index 0)
  31. * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
  32. * - make maplineno do something
  33. * 2002-11-28 Mads Martin Joergensen +
  34. * - also try /boot/System.map-`uname -r`
  35. * 2003-04-09 Werner Almesberger <wa@almesberger.net>
  36. * - fixed off-by eight error and improved heuristics in byte order detection
  37. * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM>
  38. * - added -s option; example of use:
  39. * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
  40. *
  41. * Taken from util-linux and adapted for busybox by
  42. * Paul Mundt <lethal@linux-sh.org>.
  43. */
  44. #include <errno.h>
  45. #include <stdio.h>
  46. #include <fcntl.h>
  47. #include <stdlib.h>
  48. #include <unistd.h>
  49. #include <string.h>
  50. #include <sys/types.h>
  51. #include <sys/stat.h>
  52. #include <sys/utsname.h>
  53. #include "busybox.h"
  54. #define S_LEN 128
  55. /* These are the defaults */
  56. static const char defaultmap[]="/boot/System.map";
  57. static const char defaultpro[]="/proc/profile";
  58. int readprofile_main(int argc, char **argv)
  59. {
  60. FILE *map;
  61. int proFd;
  62. const char *mapFile, *proFile, *mult=0;
  63. unsigned long len=0, indx=1;
  64. unsigned long long add0=0;
  65. unsigned int step;
  66. unsigned int *buf, total, fn_len;
  67. unsigned long long fn_add, next_add; /* current and next address */
  68. char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
  69. char mode[8];
  70. int c;
  71. int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
  72. int optBins=0, optSub=0;
  73. char mapline[S_LEN];
  74. int maplineno=1;
  75. int header_printed;
  76. #define next (current^1)
  77. proFile = defaultpro;
  78. mapFile = defaultmap;
  79. while ((c = getopt(argc, argv, "M:m:np:itvarVbs")) != -1) {
  80. switch(c) {
  81. case 'm':
  82. mapFile = optarg;
  83. break;
  84. case 'n':
  85. optNative++;
  86. break;
  87. case 'p':
  88. proFile = optarg;
  89. break;
  90. case 'a':
  91. optAll++;
  92. break;
  93. case 'b':
  94. optBins++;
  95. break;
  96. case 's':
  97. optSub++;
  98. break;
  99. case 'i':
  100. optInfo++;
  101. break;
  102. case 'M':
  103. mult = optarg;
  104. break;
  105. case 'r':
  106. optReset++;
  107. break;
  108. case 'v':
  109. optVerbose++;
  110. break;
  111. default:
  112. bb_show_usage();
  113. }
  114. }
  115. if (optReset || mult) {
  116. int multiplier, fd, to_write;
  117. /*
  118. * When writing the multiplier, if the length of the write is
  119. * not sizeof(int), the multiplier is not changed
  120. */
  121. if (mult) {
  122. multiplier = strtoul(mult, 0, 10);
  123. to_write = sizeof(int);
  124. } else {
  125. multiplier = 0;
  126. to_write = 1; /* sth different from sizeof(int) */
  127. }
  128. fd = bb_xopen(defaultpro,O_WRONLY);
  129. if (write(fd, &multiplier, to_write) != to_write)
  130. bb_perror_msg_and_die("error writing %s", defaultpro);
  131. close(fd);
  132. return EXIT_SUCCESS;
  133. }
  134. /*
  135. * Use an fd for the profiling buffer, to skip stdio overhead
  136. */
  137. proFd = bb_xopen(proFile,O_RDONLY);
  138. if (((int)(len=lseek(proFd,0,SEEK_END)) < 0)
  139. || (lseek(proFd,0,SEEK_SET) < 0))
  140. bb_perror_msg_and_die(proFile);
  141. buf = xmalloc(len);
  142. if (read(proFd,buf,len) != len)
  143. bb_perror_msg_and_die(proFile);
  144. close(proFd);
  145. if (!optNative) {
  146. int entries = len/sizeof(*buf);
  147. int big = 0,small = 0,i;
  148. unsigned *p;
  149. for (p = buf+1; p < buf+entries; p++) {
  150. if (*p & ~0U << (sizeof(*buf)*4))
  151. big++;
  152. if (*p & ((1 << (sizeof(*buf)*4))-1))
  153. small++;
  154. }
  155. if (big > small) {
  156. bb_error_msg("Assuming reversed byte order. "
  157. "Use -n to force native byte order.");
  158. for (p = buf; p < buf+entries; p++)
  159. for (i = 0; i < sizeof(*buf)/2; i++) {
  160. unsigned char *b = (unsigned char *) p;
  161. unsigned char tmp;
  162. tmp = b[i];
  163. b[i] = b[sizeof(*buf)-i-1];
  164. b[sizeof(*buf)-i-1] = tmp;
  165. }
  166. }
  167. }
  168. step = buf[0];
  169. if (optInfo) {
  170. printf("Sampling_step: %i\n", step);
  171. return EXIT_SUCCESS;
  172. }
  173. total = 0;
  174. map = bb_xfopen(mapFile, "r");
  175. while (fgets(mapline,S_LEN,map)) {
  176. if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
  177. bb_error_msg_and_die("%s(%i): wrong map line",
  178. mapFile, maplineno);
  179. if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
  180. add0 = fn_add;
  181. break;
  182. }
  183. maplineno++;
  184. }
  185. if (!add0)
  186. bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
  187. /*
  188. * Main loop.
  189. */
  190. while (fgets(mapline,S_LEN,map)) {
  191. unsigned int this = 0;
  192. if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
  193. bb_error_msg_and_die("%s(%i): wrong map line",
  194. mapFile, maplineno);
  195. header_printed = 0;
  196. /* ignore any LEADING (before a '[tT]' symbol is found)
  197. Absolute symbols */
  198. if ((*mode == 'A' || *mode == '?') && total == 0) continue;
  199. if (*mode != 'T' && *mode != 't' &&
  200. *mode != 'W' && *mode != 'w')
  201. break; /* only text is profiled */
  202. if (indx >= len / sizeof(*buf))
  203. bb_error_msg_and_die("profile address out of range. "
  204. "Wrong map file?");
  205. while (indx < (next_add-add0)/step) {
  206. if (optBins && (buf[indx] || optAll)) {
  207. if (!header_printed) {
  208. printf ("%s:\n", fn_name);
  209. header_printed = 1;
  210. }
  211. printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
  212. }
  213. this += buf[indx++];
  214. }
  215. total += this;
  216. if (optBins) {
  217. if (optVerbose || this > 0)
  218. printf (" total\t\t\t\t%u\n", this);
  219. } else if ((this || optAll) &&
  220. (fn_len = next_add-fn_add) != 0) {
  221. if (optVerbose)
  222. printf("%016llx %-40s %6i %8.4f\n", fn_add,
  223. fn_name,this,this/(double)fn_len);
  224. else
  225. printf("%6i %-40s %8.4f\n",
  226. this,fn_name,this/(double)fn_len);
  227. if (optSub) {
  228. unsigned long long scan;
  229. for (scan = (fn_add-add0)/step + 1;
  230. scan < (next_add-add0)/step; scan++) {
  231. unsigned long long addr;
  232. addr = (scan - 1)*step + add0;
  233. printf("\t%#llx\t%s+%#llx\t%u\n",
  234. addr, fn_name, addr - fn_add,
  235. buf[scan]);
  236. }
  237. }
  238. }
  239. fn_add = next_add;
  240. strcpy(fn_name,next_name);
  241. maplineno++;
  242. }
  243. /* clock ticks, out of kernel text - probably modules */
  244. printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
  245. /* trailer */
  246. if (optVerbose)
  247. printf("%016x %-40s %6i %8.4f\n",
  248. 0,"total",total,total/(double)(fn_add-add0));
  249. else
  250. printf("%6i %-40s %8.4f\n",
  251. total,"total",total/(double)(fn_add-add0));
  252. fclose(map);
  253. free(buf);
  254. return EXIT_SUCCESS;
  255. }