2 * readprofile.c - used to read /proc/profile
4 * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
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.
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.
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.
22 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
23 * - added Native Language Support
24 * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
26 * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
27 * - -M option to write profile multiplier.
28 * 2001-11-07 Werner Almesberger <wa@almesberger.net>
29 * - byte order auto-detection and -n option
30 * 2001-11-09 Werner Almesberger <wa@almesberger.net>
31 * - skip step size (index 0)
32 * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
33 * - make maplineno do something
34 * 2002-11-28 Mads Martin Joergensen +
35 * - also try /boot/System.map-`uname -r`
36 * 2003-04-09 Werner Almesberger <wa@almesberger.net>
37 * - fixed off-by eight error and improved heuristics in byte order detection
38 * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM>
39 * - added -s option; example of use:
40 * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
42 * Taken from util-linux and adapted for busybox by
43 * Paul Mundt <lethal@linux-sh.org>.
52 #include <sys/types.h>
54 #include <sys/utsname.h>
60 /* These are the defaults */
61 static char defaultmap[]="/boot/System.map";
62 static char defaultpro[]="/proc/profile";
64 int readprofile_main(int argc, char **argv)
68 char *mapFile, *proFile, *mult=0;
69 unsigned long len=0, indx=1;
70 unsigned long long add0=0;
72 unsigned int *buf, total, fn_len;
73 unsigned long long fn_add, next_add; /* current and next address */
74 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
77 int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
78 int optBins=0, optSub=0;
83 #define next (current^1)
88 while ((c = getopt(argc, argv, "M:m:np:itvarVbs")) != -1) {
125 if (optReset || mult) {
126 int multiplier, fd, to_write;
129 * When writing the multiplier, if the length of the write is
130 * not sizeof(int), the multiplier is not changed
133 multiplier = strtoul(mult, 0, 10);
134 to_write = sizeof(int);
137 to_write = 1; /* sth different from sizeof(int) */
140 fd = bb_xopen(defaultpro,O_WRONLY);
142 bb_perror_msg_and_die(defaultpro);
144 if (write(fd, &multiplier, to_write) != to_write)
145 bb_perror_msg_and_die("error writing %s", defaultpro);
152 * Use an fd for the profiling buffer, to skip stdio overhead
154 if (((proFd = bb_xopen(proFile,O_RDONLY)) < 0)
155 || ((int)(len=lseek(proFd,0,SEEK_END)) < 0)
156 || (lseek(proFd,0,SEEK_SET) < 0))
157 bb_perror_msg_and_die(proFile);
159 if (!(buf = xmalloc(len)))
160 bb_perror_nomsg_and_die();
162 if (read(proFd,buf,len) != len)
163 bb_perror_msg_and_die(proFile);
168 int entries = len/sizeof(*buf);
169 int big = 0,small = 0,i;
172 for (p = buf+1; p < buf+entries; p++) {
173 if (*p & ~0U << (sizeof(*buf)*4))
175 if (*p & ((1 << (sizeof(*buf)*4))-1))
179 fprintf(stderr,"Assuming reversed byte order. "
180 "Use -n to force native byte order.\n");
181 for (p = buf; p < buf+entries; p++)
182 for (i = 0; i < sizeof(*buf)/2; i++) {
183 unsigned char *b = (unsigned char *) p;
187 b[i] = b[sizeof(*buf)-i-1];
188 b[sizeof(*buf)-i-1] = tmp;
195 printf("Sampling_step: %i\n", step);
201 map = bb_xfopen(mapFile, "r");
203 bb_perror_msg_and_die(mapFile);
205 while (fgets(mapline,S_LEN,map)) {
206 if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
207 bb_error_msg_and_die("%s(%i): wrong map line",
210 if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
218 bb_error_msg_and_die("can't find \"_stext\" in %s\n", mapFile);
223 while (fgets(mapline,S_LEN,map)) {
224 unsigned int this = 0;
226 if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
227 bb_error_msg_and_die("%s(%i): wrong map line\n",
232 /* ignore any LEADING (before a '[tT]' symbol is found)
234 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
235 if (*mode != 'T' && *mode != 't' &&
236 *mode != 'W' && *mode != 'w')
237 break; /* only text is profiled */
239 if (indx >= len / sizeof(*buf))
240 bb_error_msg_and_die("profile address out of range. "
243 while (indx < (next_add-add0)/step) {
244 if (optBins && (buf[indx] || optAll)) {
245 if (!header_printed) {
246 printf ("%s:\n", fn_name);
249 printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
256 if (optVerbose || this > 0)
257 printf (" total\t\t\t\t%u\n", this);
258 } else if ((this || optAll) &&
259 (fn_len = next_add-fn_add) != 0) {
261 printf("%016llx %-40s %6i %8.4f\n", fn_add,
262 fn_name,this,this/(double)fn_len);
264 printf("%6i %-40s %8.4f\n",
265 this,fn_name,this/(double)fn_len);
267 unsigned long long scan;
269 for (scan = (fn_add-add0)/step + 1;
270 scan < (next_add-add0)/step; scan++) {
271 unsigned long long addr;
273 addr = (scan - 1)*step + add0;
274 printf("\t%#llx\t%s+%#llx\t%u\n",
275 addr, fn_name, addr - fn_add,
282 strcpy(fn_name,next_name);
287 /* clock ticks, out of kernel text - probably modules */
288 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
292 printf("%016x %-40s %6i %8.4f\n",
293 0,"total",total,total/(double)(fn_add-add0));
295 printf("%6i %-40s %8.4f\n",
296 total,"total",total/(double)(fn_add-add0));