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 source tree.
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>.
35 //usage:#define readprofile_trivial_usage
37 //usage:#define readprofile_full_usage "\n\n"
38 //usage: " -m mapfile (Default: /boot/System.map)"
39 //usage: "\n -p profile (Default: /proc/profile)"
40 //usage: "\n -M NUM Set the profiling multiplier to NUM"
41 //usage: "\n -i Print only info about the sampling step"
42 //usage: "\n -v Verbose"
43 //usage: "\n -a Print all symbols, even if count is 0"
44 //usage: "\n -b Print individual histogram-bin counts"
45 //usage: "\n -s Print individual counters within functions"
46 //usage: "\n -r Reset all the counters (root only)"
47 //usage: "\n -n Disable byte order auto-detection"
50 #include <sys/utsname.h>
54 /* These are the defaults */
55 static const char defaultmap[] ALIGN1 = "/boot/System.map";
56 static const char defaultpro[] ALIGN1 = "/proc/profile";
58 int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59 int readprofile_main(int argc UNUSED_PARAM, char **argv)
62 const char *mapFile, *proFile;
63 unsigned long indx = 1;
67 unsigned int *buf, total, fn_len;
68 unsigned long long fn_add, next_add; /* current and next address */
69 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
88 #define optMult (opt & OPT_M)
89 #define optNative (opt & OPT_n)
90 #define optAll (opt & OPT_a)
91 #define optBins (opt & OPT_b)
92 #define optSub (opt & OPT_s)
93 #define optInfo (opt & OPT_i)
94 #define optReset (opt & OPT_r)
95 #define optVerbose (opt & OPT_v)
97 #define next (current^1)
100 mapFile = defaultmap;
102 opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
104 if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
108 * When writing the multiplier, if the length of the write is
109 * not sizeof(int), the multiplier is not changed
111 to_write = sizeof(int);
113 to_write = 1; /* sth different from sizeof(int) */
115 fd = xopen(defaultpro, O_WRONLY);
116 xwrite(fd, &multiplier, to_write);
122 * Use an fd for the profiling buffer, to skip stdio overhead
124 len = MAXINT(ssize_t);
125 buf = xmalloc_xopen_read_close(proFile, &len);
127 int entries = len / sizeof(*buf);
128 int big = 0, small = 0, i;
131 for (p = buf+1; p < buf+entries; p++) {
132 if (*p & ~0U << (sizeof(*buf)*4))
134 if (*p & ((1 << (sizeof(*buf)*4))-1))
138 bb_error_msg("assuming reversed byte order, "
139 "use -n to force native byte order");
140 for (p = buf; p < buf+entries; p++)
141 for (i = 0; i < sizeof(*buf)/2; i++) {
142 unsigned char *b = (unsigned char *) p;
146 b[i] = b[sizeof(*buf)-i-1];
147 b[sizeof(*buf)-i-1] = tmp;
154 printf("Sampling_step: %u\n", step);
160 map = xfopen_for_read(mapFile);
162 while (fgets(mapline, S_LEN, map)) {
163 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
164 bb_error_msg_and_die("%s(%i): wrong map line",
167 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
175 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
180 while (fgets(mapline, S_LEN, map)) {
181 unsigned int this = 0;
183 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
184 bb_error_msg_and_die("%s(%i): wrong map line",
189 /* ignore any LEADING (before a '[tT]' symbol is found)
191 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
192 if (*mode != 'T' && *mode != 't'
193 && *mode != 'W' && *mode != 'w'
195 break; /* only text is profiled */
198 if (indx >= len / sizeof(*buf))
199 bb_error_msg_and_die("profile address out of range. "
202 while (indx < (next_add-add0)/step) {
203 if (optBins && (buf[indx] || optAll)) {
204 if (!header_printed) {
205 printf("%s:\n", fn_name);
208 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
215 if (optVerbose || this > 0)
216 printf(" total\t\t\t\t%u\n", this);
217 } else if ((this || optAll)
218 && (fn_len = next_add-fn_add) != 0
221 printf("%016llx %-40s %6u %8.4f\n", fn_add,
222 fn_name, this, this/(double)fn_len);
224 printf("%6u %-40s %8.4f\n",
225 this, fn_name, this/(double)fn_len);
227 unsigned long long scan;
229 for (scan = (fn_add-add0)/step + 1;
230 scan < (next_add-add0)/step; scan++) {
231 unsigned long long addr;
233 addr = (scan - 1)*step + add0;
234 printf("\t%#llx\t%s+%#llx\t%u\n",
235 addr, fn_name, addr - fn_add,
242 strcpy(fn_name, next_name);
247 /* clock ticks, out of kernel text - probably modules */
248 printf("%6u %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
252 printf("%016x %-40s %6u %8.4f\n",
253 0, "total", total, total/(double)(fn_add-add0));
255 printf("%6u %-40s %8.4f\n",
256 total, "total", total/(double)(fn_add-add0));