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 Mi¶kiewicz <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[] = "/boot/System.map";
42 static const char defaultpro[] = "/proc/profile";
44 int readprofile_main(int argc, char **argv)
47 const char *mapFile, *proFile, *mult = 0;
48 unsigned long indx = 1;
52 unsigned int *buf, total, fn_len;
53 unsigned long long fn_add, next_add; /* current and next address */
54 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
57 int optAll = 0, optInfo = 0, optReset = 0;
58 int optVerbose = 0, optNative = 0;
59 int optBins = 0, optSub = 0;
63 #define next (current^1)
68 opt_complementary = "nn:aa:bb:ss:ii:rr:vv";
69 getopt32(argc, argv, "M:m:p:nabsirv",
70 &mult, &mapFile, &proFile,
71 &optNative, &optAll, &optBins, &optSub,
72 &optInfo, &optReset, &optVerbose);
74 if (optReset || mult) {
75 int multiplier, fd, to_write;
78 * When writing the multiplier, if the length of the write is
79 * not sizeof(int), the multiplier is not changed
82 multiplier = xatoi_u(mult);
83 to_write = sizeof(int);
86 to_write = 1; /* sth different from sizeof(int) */
89 fd = xopen(defaultpro, O_WRONLY);
91 if (full_write(fd, &multiplier, to_write) != to_write)
92 bb_perror_msg_and_die("error writing %s", defaultpro);
99 * Use an fd for the profiling buffer, to skip stdio overhead
102 buf = xmalloc_open_read_close(proFile, &len);
104 int entries = len/sizeof(*buf);
105 int big = 0, small = 0, i;
108 for (p = buf+1; p < buf+entries; p++) {
109 if (*p & ~0U << (sizeof(*buf)*4))
111 if (*p & ((1 << (sizeof(*buf)*4))-1))
115 bb_error_msg("assuming reversed byte order, "
116 "use -n to force native byte order");
117 for (p = buf; p < buf+entries; p++)
118 for (i = 0; i < sizeof(*buf)/2; i++) {
119 unsigned char *b = (unsigned char *) p;
123 b[i] = b[sizeof(*buf)-i-1];
124 b[sizeof(*buf)-i-1] = tmp;
131 printf("Sampling_step: %i\n", step);
137 map = xfopen(mapFile, "r");
139 while (fgets(mapline, S_LEN, map)) {
140 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
141 bb_error_msg_and_die("%s(%i): wrong map line",
144 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
152 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
157 while (fgets(mapline, S_LEN, map)) {
158 unsigned int this = 0;
160 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
161 bb_error_msg_and_die("%s(%i): wrong map line",
166 /* ignore any LEADING (before a '[tT]' symbol is found)
168 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
169 if (*mode != 'T' && *mode != 't' &&
170 *mode != 'W' && *mode != 'w')
171 break; /* only text is profiled */
173 if (indx >= len / sizeof(*buf))
174 bb_error_msg_and_die("profile address out of range. "
177 while (indx < (next_add-add0)/step) {
178 if (optBins && (buf[indx] || optAll)) {
179 if (!header_printed) {
180 printf("%s:\n", fn_name);
183 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
190 if (optVerbose || this > 0)
191 printf(" total\t\t\t\t%u\n", this);
192 } else if ((this || optAll) &&
193 (fn_len = next_add-fn_add) != 0) {
195 printf("%016llx %-40s %6i %8.4f\n", fn_add,
196 fn_name, this, this/(double)fn_len);
198 printf("%6i %-40s %8.4f\n",
199 this, fn_name, this/(double)fn_len);
201 unsigned long long scan;
203 for (scan = (fn_add-add0)/step + 1;
204 scan < (next_add-add0)/step; scan++) {
205 unsigned long long addr;
207 addr = (scan - 1)*step + add0;
208 printf("\t%#llx\t%s+%#llx\t%u\n",
209 addr, fn_name, addr - fn_add,
216 strcpy(fn_name, next_name);
221 /* clock ticks, out of kernel text - probably modules */
222 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
226 printf("%016x %-40s %6i %8.4f\n",
227 0, "total", total, total/(double)(fn_add-add0));
229 printf("%6i %-40s %8.4f\n",
230 total, "total", total/(double)(fn_add-add0));