blob: e25d07d2b972f76457285c1a3c44b6763efe7f21 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Paul Mundt8b2d02e2005-05-20 17:22:18 +00002/*
3 * readprofile.c - used to read /proc/profile
4 *
5 * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
6 *
Rob Landleyd921b2e2006-08-03 15:41:12 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Paul Mundt8b2d02e2005-05-20 17:22:18 +00008 */
9
10/*
Denis Vlasenko1d426652008-03-17 09:09:09 +000011 * 1999-02-22 Arkadiusz Mickiewicz <misiek@pld.ORG.PL>
Paul Mundt8b2d02e2005-05-20 17:22:18 +000012 * - added Native Language Support
13 * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
14 * - 64bit clean patch
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"
30 *
31 * Taken from util-linux and adapted for busybox by
32 * Paul Mundt <lethal@linux-sh.org>.
33 */
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000036#include <sys/utsname.h>
Paul Mundt8b2d02e2005-05-20 17:22:18 +000037
38#define S_LEN 128
39
40/* These are the defaults */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000041static const char defaultmap[] ALIGN1 = "/boot/System.map";
42static const char defaultpro[] ALIGN1 = "/proc/profile";
Paul Mundt8b2d02e2005-05-20 17:22:18 +000043
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000044int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000045int readprofile_main(int argc ATTRIBUTE_UNUSED, char **argv)
Paul Mundt8b2d02e2005-05-20 17:22:18 +000046{
47 FILE *map;
Denis Vlasenko1d426652008-03-17 09:09:09 +000048 const char *mapFile, *proFile;
Denis Vlasenko92258542006-11-01 10:25:35 +000049 unsigned long indx = 1;
Denis Vlasenkoea620772006-10-14 02:23:43 +000050 size_t len;
Denis Vlasenko92258542006-11-01 10:25:35 +000051 uint64_t add0 = 0;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000052 unsigned int step;
53 unsigned int *buf, total, fn_len;
Denis Vlasenkocad36682006-09-22 08:39:49 +000054 unsigned long long fn_add, next_add; /* current and next address */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000055 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000056 char mapline[S_LEN];
Denis Vlasenko92258542006-11-01 10:25:35 +000057 char mode[8];
Denis Vlasenko92258542006-11-01 10:25:35 +000058 int maplineno = 1;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000059 int header_printed;
Denis Vlasenko1d426652008-03-17 09:09:09 +000060 int multiplier = 0;
61 unsigned opt;
62 enum {
63 OPT_M = (1 << 0),
64 OPT_m = (1 << 1),
65 OPT_p = (1 << 2),
66 OPT_n = (1 << 3),
67 OPT_a = (1 << 4),
68 OPT_b = (1 << 5),
69 OPT_s = (1 << 6),
70 OPT_i = (1 << 7),
71 OPT_r = (1 << 8),
72 OPT_v = (1 << 9),
73 };
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)
Paul Mundt8b2d02e2005-05-20 17:22:18 +000082
83#define next (current^1)
84
85 proFile = defaultpro;
86 mapFile = defaultmap;
87
Denis Vlasenko1d426652008-03-17 09:09:09 +000088 opt_complementary = "M+"; /* -M N */
89 opt = getopt32(argv, "M:m:p:nabsirv", &multiplier, &mapFile, &proFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +000090
Denis Vlasenko1d426652008-03-17 09:09:09 +000091 if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
92 int fd, to_write;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000093
94 /*
95 * When writing the multiplier, if the length of the write is
96 * not sizeof(int), the multiplier is not changed
97 */
Denis Vlasenko1d426652008-03-17 09:09:09 +000098 to_write = sizeof(int);
99 if (!optMult)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000100 to_write = 1; /* sth different from sizeof(int) */
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000101
Denis Vlasenko92258542006-11-01 10:25:35 +0000102 fd = xopen(defaultpro, O_WRONLY);
Denis Vlasenkoa89d50f2007-11-13 17:51:40 +0000103 xwrite(fd, &multiplier, to_write);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000104 close(fd);
105 return EXIT_SUCCESS;
106 }
107
108 /*
109 * Use an fd for the profiling buffer, to skip stdio overhead
110 */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000111 len = MAXINT(ssize_t);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000112 buf = xmalloc_open_read_close(proFile, &len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000113 if (!optNative) {
114 int entries = len/sizeof(*buf);
Denis Vlasenko92258542006-11-01 10:25:35 +0000115 int big = 0, small = 0, i;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000116 unsigned *p;
117
118 for (p = buf+1; p < buf+entries; p++) {
119 if (*p & ~0U << (sizeof(*buf)*4))
120 big++;
121 if (*p & ((1 << (sizeof(*buf)*4))-1))
122 small++;
123 }
124 if (big > small) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000125 bb_error_msg("assuming reversed byte order, "
126 "use -n to force native byte order");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000127 for (p = buf; p < buf+entries; p++)
128 for (i = 0; i < sizeof(*buf)/2; i++) {
129 unsigned char *b = (unsigned char *) p;
130 unsigned char tmp;
131
132 tmp = b[i];
133 b[i] = b[sizeof(*buf)-i-1];
134 b[sizeof(*buf)-i-1] = tmp;
135 }
136 }
137 }
138
139 step = buf[0];
140 if (optInfo) {
141 printf("Sampling_step: %i\n", step);
142 return EXIT_SUCCESS;
143 }
144
145 total = 0;
146
Rob Landleyd921b2e2006-08-03 15:41:12 +0000147 map = xfopen(mapFile, "r");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000148
Denis Vlasenko92258542006-11-01 10:25:35 +0000149 while (fgets(mapline, S_LEN, map)) {
150 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000151 bb_error_msg_and_die("%s(%i): wrong map line",
152 mapFile, maplineno);
153
Denis Vlasenko92258542006-11-01 10:25:35 +0000154 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000155 add0 = fn_add;
156 break;
157 }
158 maplineno++;
159 }
160
161 if (!add0)
Rob Landleyb7285002005-09-14 15:28:15 +0000162 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000163
164 /*
165 * Main loop.
166 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000167 while (fgets(mapline, S_LEN, map)) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000168 unsigned int this = 0;
169
Denis Vlasenko92258542006-11-01 10:25:35 +0000170 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
Rob Landleyb7285002005-09-14 15:28:15 +0000171 bb_error_msg_and_die("%s(%i): wrong map line",
Denis Vlasenko92258542006-11-01 10:25:35 +0000172 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000173
174 header_printed = 0;
175
176 /* ignore any LEADING (before a '[tT]' symbol is found)
177 Absolute symbols */
178 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
179 if (*mode != 'T' && *mode != 't' &&
180 *mode != 'W' && *mode != 'w')
181 break; /* only text is profiled */
182
183 if (indx >= len / sizeof(*buf))
184 bb_error_msg_and_die("profile address out of range. "
185 "Wrong map file?");
186
187 while (indx < (next_add-add0)/step) {
188 if (optBins && (buf[indx] || optAll)) {
189 if (!header_printed) {
Denis Vlasenko92258542006-11-01 10:25:35 +0000190 printf("%s:\n", fn_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000191 header_printed = 1;
192 }
Denis Vlasenko92258542006-11-01 10:25:35 +0000193 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000194 }
195 this += buf[indx++];
196 }
197 total += this;
198
199 if (optBins) {
200 if (optVerbose || this > 0)
Denis Vlasenko92258542006-11-01 10:25:35 +0000201 printf(" total\t\t\t\t%u\n", this);
Denis Vlasenko1d426652008-03-17 09:09:09 +0000202 } else if ((this || optAll)
203 && (fn_len = next_add-fn_add) != 0
204 ) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000205 if (optVerbose)
206 printf("%016llx %-40s %6i %8.4f\n", fn_add,
Denis Vlasenko92258542006-11-01 10:25:35 +0000207 fn_name, this, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000208 else
209 printf("%6i %-40s %8.4f\n",
Denis Vlasenko92258542006-11-01 10:25:35 +0000210 this, fn_name, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000211 if (optSub) {
212 unsigned long long scan;
213
214 for (scan = (fn_add-add0)/step + 1;
215 scan < (next_add-add0)/step; scan++) {
216 unsigned long long addr;
217
218 addr = (scan - 1)*step + add0;
219 printf("\t%#llx\t%s+%#llx\t%u\n",
220 addr, fn_name, addr - fn_add,
221 buf[scan]);
222 }
223 }
224 }
225
226 fn_add = next_add;
Denis Vlasenko92258542006-11-01 10:25:35 +0000227 strcpy(fn_name, next_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000228
229 maplineno++;
230 }
231
232 /* clock ticks, out of kernel text - probably modules */
233 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
234
235 /* trailer */
236 if (optVerbose)
237 printf("%016x %-40s %6i %8.4f\n",
Denis Vlasenko92258542006-11-01 10:25:35 +0000238 0, "total", total, total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000239 else
240 printf("%6i %-40s %8.4f\n",
Denis Vlasenko92258542006-11-01 10:25:35 +0000241 total, "total", total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000242
243 fclose(map);
244 free(buf);
245
246 return EXIT_SUCCESS;
247}