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 const char defaultmap[]="/boot/System.map";
62 static const char defaultpro[]="/proc/profile";
64 int readprofile_main(int argc, char **argv)
68 const 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 if (write(fd, &multiplier, to_write) != to_write)
143 bb_perror_msg_and_die("error writing %s", defaultpro);
150 * Use an fd for the profiling buffer, to skip stdio overhead
153 proFd = bb_xopen(proFile,O_RDONLY);
155 if (((int)(len=lseek(proFd,0,SEEK_END)) < 0)
156 || (lseek(proFd,0,SEEK_SET) < 0))
157 bb_perror_msg_and_die(proFile);
161 if (read(proFd,buf,len) != len)
162 bb_perror_msg_and_die(proFile);
167 int entries = len/sizeof(*buf);
168 int big = 0,small = 0,i;
171 for (p = buf+1; p < buf+entries; p++) {
172 if (*p & ~0U << (sizeof(*buf)*4))
174 if (*p & ((1 << (sizeof(*buf)*4))-1))
178 bb_error_msg("Assuming reversed byte order. "
179 "Use -n to force native byte order.");
180 for (p = buf; p < buf+entries; p++)
181 for (i = 0; i < sizeof(*buf)/2; i++) {
182 unsigned char *b = (unsigned char *) p;
186 b[i] = b[sizeof(*buf)-i-1];
187 b[sizeof(*buf)-i-1] = tmp;
194 printf("Sampling_step: %i\n", step);
200 map = bb_xfopen(mapFile, "r");
202 while (fgets(mapline,S_LEN,map)) {
203 if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
204 bb_error_msg_and_die("%s(%i): wrong map line",
207 if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
215 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
220 while (fgets(mapline,S_LEN,map)) {
221 unsigned int this = 0;
223 if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
224 bb_error_msg_and_die("%s(%i): wrong map line",
229 /* ignore any LEADING (before a '[tT]' symbol is found)
231 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
232 if (*mode != 'T' && *mode != 't' &&
233 *mode != 'W' && *mode != 'w')
234 break; /* only text is profiled */
236 if (indx >= len / sizeof(*buf))
237 bb_error_msg_and_die("profile address out of range. "
240 while (indx < (next_add-add0)/step) {
241 if (optBins && (buf[indx] || optAll)) {
242 if (!header_printed) {
243 printf ("%s:\n", fn_name);
246 printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
253 if (optVerbose || this > 0)
254 printf (" total\t\t\t\t%u\n", this);
255 } else if ((this || optAll) &&
256 (fn_len = next_add-fn_add) != 0) {
258 printf("%016llx %-40s %6i %8.4f\n", fn_add,
259 fn_name,this,this/(double)fn_len);
261 printf("%6i %-40s %8.4f\n",
262 this,fn_name,this/(double)fn_len);
264 unsigned long long scan;
266 for (scan = (fn_add-add0)/step + 1;
267 scan < (next_add-add0)/step; scan++) {
268 unsigned long long addr;
270 addr = (scan - 1)*step + add0;
271 printf("\t%#llx\t%s+%#llx\t%u\n",
272 addr, fn_name, addr - fn_add,
279 strcpy(fn_name,next_name);
284 /* clock ticks, out of kernel text - probably modules */
285 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
289 printf("%016x %-40s %6i %8.4f\n",
290 0,"total",total,total/(double)(fn_add-add0));
292 printf("%6i %-40s %8.4f\n",
293 total,"total",total/(double)(fn_add-add0));