1 /* vi: set sw=4 ts=4: */
3 * readprofile.c - used to read /proc/profile
5 * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
24 * - added Native Language Support
25 * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
27 * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
28 * - -M option to write profile multiplier.
29 * 2001-11-07 Werner Almesberger <wa@almesberger.net>
30 * - byte order auto-detection and -n option
31 * 2001-11-09 Werner Almesberger <wa@almesberger.net>
32 * - skip step size (index 0)
33 * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
34 * - make maplineno do something
35 * 2002-11-28 Mads Martin Joergensen +
36 * - also try /boot/System.map-`uname -r`
37 * 2003-04-09 Werner Almesberger <wa@almesberger.net>
38 * - fixed off-by eight error and improved heuristics in byte order detection
39 * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM>
40 * - added -s option; example of use:
41 * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
43 * Taken from util-linux and adapted for busybox by
44 * Paul Mundt <lethal@linux-sh.org>.
53 #include <sys/types.h>
55 #include <sys/utsname.h>
61 /* These are the defaults */
62 static const char defaultmap[]="/boot/System.map";
63 static const char defaultpro[]="/proc/profile";
65 int readprofile_main(int argc, char **argv)
69 const char *mapFile, *proFile, *mult=0;
70 unsigned long len=0, indx=1;
71 unsigned long long add0=0;
73 unsigned int *buf, total, fn_len;
74 unsigned long long fn_add, next_add; /* current and next address */
75 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
78 int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
79 int optBins=0, optSub=0;
84 #define next (current^1)
89 while ((c = getopt(argc, argv, "M:m:np:itvarVbs")) != -1) {
126 if (optReset || mult) {
127 int multiplier, fd, to_write;
130 * When writing the multiplier, if the length of the write is
131 * not sizeof(int), the multiplier is not changed
134 multiplier = strtoul(mult, 0, 10);
135 to_write = sizeof(int);
138 to_write = 1; /* sth different from sizeof(int) */
141 fd = bb_xopen(defaultpro,O_WRONLY);
143 if (write(fd, &multiplier, to_write) != to_write)
144 bb_perror_msg_and_die("error writing %s", defaultpro);
151 * Use an fd for the profiling buffer, to skip stdio overhead
154 proFd = bb_xopen(proFile,O_RDONLY);
156 if (((int)(len=lseek(proFd,0,SEEK_END)) < 0)
157 || (lseek(proFd,0,SEEK_SET) < 0))
158 bb_perror_msg_and_die(proFile);
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 bb_error_msg("Assuming reversed byte order. "
180 "Use -n to force native byte order.");
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 while (fgets(mapline,S_LEN,map)) {
204 if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
205 bb_error_msg_and_die("%s(%i): wrong map line",
208 if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
216 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
221 while (fgets(mapline,S_LEN,map)) {
222 unsigned int this = 0;
224 if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
225 bb_error_msg_and_die("%s(%i): wrong map line",
230 /* ignore any LEADING (before a '[tT]' symbol is found)
232 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
233 if (*mode != 'T' && *mode != 't' &&
234 *mode != 'W' && *mode != 'w')
235 break; /* only text is profiled */
237 if (indx >= len / sizeof(*buf))
238 bb_error_msg_and_die("profile address out of range. "
241 while (indx < (next_add-add0)/step) {
242 if (optBins && (buf[indx] || optAll)) {
243 if (!header_printed) {
244 printf ("%s:\n", fn_name);
247 printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
254 if (optVerbose || this > 0)
255 printf (" total\t\t\t\t%u\n", this);
256 } else if ((this || optAll) &&
257 (fn_len = next_add-fn_add) != 0) {
259 printf("%016llx %-40s %6i %8.4f\n", fn_add,
260 fn_name,this,this/(double)fn_len);
262 printf("%6i %-40s %8.4f\n",
263 this,fn_name,this/(double)fn_len);
265 unsigned long long scan;
267 for (scan = (fn_add-add0)/step + 1;
268 scan < (next_add-add0)/step; scan++) {
269 unsigned long long addr;
271 addr = (scan - 1)*step + add0;
272 printf("\t%#llx\t%s+%#llx\t%u\n",
273 addr, fn_name, addr - fn_add,
280 strcpy(fn_name,next_name);
285 /* clock ticks, out of kernel text - probably modules */
286 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
290 printf("%016x %-40s %6i %8.4f\n",
291 0,"total",total,total/(double)(fn_add-add0));
293 printf("%6i %-40s %8.4f\n",
294 total,"total",total/(double)(fn_add-add0));