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 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 * 1999-02-22 Arkadiusz Mickiewicz <misiek@pld.ORG.PL>
12 * - added Native Language Support
13 * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
15 * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
16 * - -M option to write profile multiplier.
17 * 2001-11-07 Werner Almesberger <wa@almesberger.net>
18 * - byte order auto-detection and -n option
19 * 2001-11-09 Werner Almesberger <wa@almesberger.net>
20 * - skip step size (index 0)
21 * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
22 * - make maplineno do something
23 * 2002-11-28 Mads Martin Joergensen +
24 * - also try /boot/System.map-`uname -r`
25 * 2003-04-09 Werner Almesberger <wa@almesberger.net>
26 * - fixed off-by eight error and improved heuristics in byte order detection
27 * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM>
28 * - added -s option; example of use:
29 * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
31 * Taken from util-linux and adapted for busybox by
32 * Paul Mundt <lethal@linux-sh.org>.
36 #include <sys/utsname.h>
40 /* These are the defaults */
41 static const char defaultmap[] ALIGN1 = "/boot/System.map";
42 static const char defaultpro[] ALIGN1 = "/proc/profile";
44 int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45 int readprofile_main(int argc UNUSED_PARAM, char **argv)
48 const char *mapFile, *proFile;
49 unsigned long indx = 1;
53 unsigned int *buf, total, fn_len;
54 unsigned long long fn_add, next_add; /* current and next address */
55 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
74 #define optMult (opt & OPT_M)
75 #define optNative (opt & OPT_n)
76 #define optAll (opt & OPT_a)
77 #define optBins (opt & OPT_b)
78 #define optSub (opt & OPT_s)
79 #define optInfo (opt & OPT_i)
80 #define optReset (opt & OPT_r)
81 #define optVerbose (opt & OPT_v)
83 #define next (current^1)
88 opt_complementary = "M+"; /* -M N */
89 opt = getopt32(argv, "M:m:p:nabsirv", &multiplier, &mapFile, &proFile);
91 if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
95 * When writing the multiplier, if the length of the write is
96 * not sizeof(int), the multiplier is not changed
98 to_write = sizeof(int);
100 to_write = 1; /* sth different from sizeof(int) */
102 fd = xopen(defaultpro, O_WRONLY);
103 xwrite(fd, &multiplier, to_write);
109 * Use an fd for the profiling buffer, to skip stdio overhead
111 len = MAXINT(ssize_t);
112 buf = xmalloc_xopen_read_close(proFile, &len);
114 int entries = len / sizeof(*buf);
115 int big = 0, small = 0, i;
118 for (p = buf+1; p < buf+entries; p++) {
119 if (*p & ~0U << (sizeof(*buf)*4))
121 if (*p & ((1 << (sizeof(*buf)*4))-1))
125 bb_error_msg("assuming reversed byte order, "
126 "use -n to force native byte order");
127 for (p = buf; p < buf+entries; p++)
128 for (i = 0; i < sizeof(*buf)/2; i++) {
129 unsigned char *b = (unsigned char *) p;
133 b[i] = b[sizeof(*buf)-i-1];
134 b[sizeof(*buf)-i-1] = tmp;
141 printf("Sampling_step: %i\n", step);
147 map = xfopen_for_read(mapFile);
149 while (fgets(mapline, S_LEN, map)) {
150 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
151 bb_error_msg_and_die("%s(%i): wrong map line",
154 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
162 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
167 while (fgets(mapline, S_LEN, map)) {
168 unsigned int this = 0;
170 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
171 bb_error_msg_and_die("%s(%i): wrong map line",
176 /* ignore any LEADING (before a '[tT]' symbol is found)
178 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
179 if (*mode != 'T' && *mode != 't'
180 && *mode != 'W' && *mode != 'w'
182 break; /* only text is profiled */
185 if (indx >= len / sizeof(*buf))
186 bb_error_msg_and_die("profile address out of range. "
189 while (indx < (next_add-add0)/step) {
190 if (optBins && (buf[indx] || optAll)) {
191 if (!header_printed) {
192 printf("%s:\n", fn_name);
195 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
202 if (optVerbose || this > 0)
203 printf(" total\t\t\t\t%u\n", this);
204 } else if ((this || optAll)
205 && (fn_len = next_add-fn_add) != 0
208 printf("%016llx %-40s %6i %8.4f\n", fn_add,
209 fn_name, this, this/(double)fn_len);
211 printf("%6i %-40s %8.4f\n",
212 this, fn_name, this/(double)fn_len);
214 unsigned long long scan;
216 for (scan = (fn_add-add0)/step + 1;
217 scan < (next_add-add0)/step; scan++) {
218 unsigned long long addr;
220 addr = (scan - 1)*step + add0;
221 printf("\t%#llx\t%s+%#llx\t%u\n",
222 addr, fn_name, addr - fn_add,
229 strcpy(fn_name, next_name);
234 /* clock ticks, out of kernel text - probably modules */
235 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
239 printf("%016x %-40s %6i %8.4f\n",
240 0, "total", total, total/(double)(fn_add-add0));
242 printf("%6i %-40s %8.4f\n",
243 total, "total", total/(double)(fn_add-add0));