3
0

readprofile.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 char defaultmap[]="/boot/System.map";
  57. static char defaultpro[]="/proc/profile";
  58. int readprofile_main(int argc, char **argv)
  59. {
  60. FILE *map;
  61. int proFd;
  62. 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 (fd < 0)
  130. bb_perror_msg_and_die(defaultpro);
  131. if (write(fd, &multiplier, to_write) != to_write)
  132. bb_perror_msg_and_die("error writing %s", defaultpro);
  133. close(fd);
  134. return EXIT_SUCCESS;
  135. }
  136. /*
  137. * Use an fd for the profiling buffer, to skip stdio overhead
  138. */
  139. if (((proFd = bb_xopen(proFile,O_RDONLY)) < 0)
  140. || ((int)(len=lseek(proFd,0,SEEK_END)) < 0)
  141. || (lseek(proFd,0,SEEK_SET) < 0))
  142. bb_perror_msg_and_die(proFile);
  143. if (!(buf = xmalloc(len)))
  144. bb_perror_nomsg_and_die();
  145. if (read(proFd,buf,len) != len)
  146. bb_perror_msg_and_die(proFile);
  147. close(proFd);
  148. if (!optNative) {
  149. int entries = len/sizeof(*buf);
  150. int big = 0,small = 0,i;
  151. unsigned *p;
  152. for (p = buf+1; p < buf+entries; p++) {
  153. if (*p & ~0U << (sizeof(*buf)*4))
  154. big++;
  155. if (*p & ((1 << (sizeof(*buf)*4))-1))
  156. small++;
  157. }
  158. if (big > small) {
  159. fprintf(stderr,"Assuming reversed byte order. "
  160. "Use -n to force native byte order.\n");
  161. for (p = buf; p < buf+entries; p++)
  162. for (i = 0; i < sizeof(*buf)/2; i++) {
  163. unsigned char *b = (unsigned char *) p;
  164. unsigned char tmp;
  165. tmp = b[i];
  166. b[i] = b[sizeof(*buf)-i-1];
  167. b[sizeof(*buf)-i-1] = tmp;
  168. }
  169. }
  170. }
  171. step = buf[0];
  172. if (optInfo) {
  173. printf("Sampling_step: %i\n", step);
  174. return EXIT_SUCCESS;
  175. }
  176. total = 0;
  177. map = bb_xfopen(mapFile, "r");
  178. if (map == NULL)
  179. bb_perror_msg_and_die(mapFile);
  180. while (fgets(mapline,S_LEN,map)) {
  181. if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
  182. bb_error_msg_and_die("%s(%i): wrong map line",
  183. mapFile, maplineno);
  184. if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
  185. add0 = fn_add;
  186. break;
  187. }
  188. maplineno++;
  189. }
  190. if (!add0)
  191. bb_error_msg_and_die("can't find \"_stext\" in %s\n", mapFile);
  192. /*
  193. * Main loop.
  194. */
  195. while (fgets(mapline,S_LEN,map)) {
  196. unsigned int this = 0;
  197. if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
  198. bb_error_msg_and_die("%s(%i): wrong map line\n",
  199. mapFile, maplineno);
  200. header_printed = 0;
  201. /* ignore any LEADING (before a '[tT]' symbol is found)
  202. Absolute symbols */
  203. if ((*mode == 'A' || *mode == '?') && total == 0) continue;
  204. if (*mode != 'T' && *mode != 't' &&
  205. *mode != 'W' && *mode != 'w')
  206. break; /* only text is profiled */
  207. if (indx >= len / sizeof(*buf))
  208. bb_error_msg_and_die("profile address out of range. "
  209. "Wrong map file?");
  210. while (indx < (next_add-add0)/step) {
  211. if (optBins && (buf[indx] || optAll)) {
  212. if (!header_printed) {
  213. printf ("%s:\n", fn_name);
  214. header_printed = 1;
  215. }
  216. printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
  217. }
  218. this += buf[indx++];
  219. }
  220. total += this;
  221. if (optBins) {
  222. if (optVerbose || this > 0)
  223. printf (" total\t\t\t\t%u\n", this);
  224. } else if ((this || optAll) &&
  225. (fn_len = next_add-fn_add) != 0) {
  226. if (optVerbose)
  227. printf("%016llx %-40s %6i %8.4f\n", fn_add,
  228. fn_name,this,this/(double)fn_len);
  229. else
  230. printf("%6i %-40s %8.4f\n",
  231. this,fn_name,this/(double)fn_len);
  232. if (optSub) {
  233. unsigned long long scan;
  234. for (scan = (fn_add-add0)/step + 1;
  235. scan < (next_add-add0)/step; scan++) {
  236. unsigned long long addr;
  237. addr = (scan - 1)*step + add0;
  238. printf("\t%#llx\t%s+%#llx\t%u\n",
  239. addr, fn_name, addr - fn_add,
  240. buf[scan]);
  241. }
  242. }
  243. }
  244. fn_add = next_add;
  245. strcpy(fn_name,next_name);
  246. maplineno++;
  247. }
  248. /* clock ticks, out of kernel text - probably modules */
  249. printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
  250. /* trailer */
  251. if (optVerbose)
  252. printf("%016x %-40s %6i %8.4f\n",
  253. 0,"total",total,total/(double)(fn_add-add0));
  254. else
  255. printf("%6i %-40s %8.4f\n",
  256. total,"total",total/(double)(fn_add-add0));
  257. fclose(map);
  258. free(buf);
  259. return EXIT_SUCCESS;
  260. }