blob: 31abb6bb25976028cc8b703db4bc6850840b57e2 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010034//config:config READPROFILE
35//config: bool "readprofile"
36//config: default y
37//config: #select PLATFORM_LINUX
38//config: help
39//config: This allows you to parse /proc/profile for basic profiling.
40
41//applet:IF_READPROFILE(APPLET(readprofile, BB_DIR_USR_SBIN, BB_SUID_DROP))
42
43//kbuild:lib-$(CONFIG_READPROFILE) += readprofile.o
Paul Mundt8b2d02e2005-05-20 17:22:18 +000044
Pere Orga5bc8c002011-04-11 03:29:49 +020045//usage:#define readprofile_trivial_usage
46//usage: "[OPTIONS]"
47//usage:#define readprofile_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020048//usage: " -m mapfile (Default: /boot/System.map)"
Pere Orga5bc8c002011-04-11 03:29:49 +020049//usage: "\n -p profile (Default: /proc/profile)"
50//usage: "\n -M NUM Set the profiling multiplier to NUM"
51//usage: "\n -i Print only info about the sampling step"
52//usage: "\n -v Verbose"
53//usage: "\n -a Print all symbols, even if count is 0"
54//usage: "\n -b Print individual histogram-bin counts"
55//usage: "\n -s Print individual counters within functions"
56//usage: "\n -r Reset all the counters (root only)"
57//usage: "\n -n Disable byte order auto-detection"
58
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000059#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000060#include <sys/utsname.h>
Paul Mundt8b2d02e2005-05-20 17:22:18 +000061
62#define S_LEN 128
63
64/* These are the defaults */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000065static const char defaultmap[] ALIGN1 = "/boot/System.map";
66static const char defaultpro[] ALIGN1 = "/proc/profile";
Paul Mundt8b2d02e2005-05-20 17:22:18 +000067
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000068int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000069int readprofile_main(int argc UNUSED_PARAM, char **argv)
Paul Mundt8b2d02e2005-05-20 17:22:18 +000070{
71 FILE *map;
Denis Vlasenko1d426652008-03-17 09:09:09 +000072 const char *mapFile, *proFile;
Denis Vlasenko92258542006-11-01 10:25:35 +000073 unsigned long indx = 1;
Denis Vlasenkoea620772006-10-14 02:23:43 +000074 size_t len;
Denis Vlasenko92258542006-11-01 10:25:35 +000075 uint64_t add0 = 0;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000076 unsigned int step;
77 unsigned int *buf, total, fn_len;
Denis Vlasenkocad36682006-09-22 08:39:49 +000078 unsigned long long fn_add, next_add; /* current and next address */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000079 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000080 char mapline[S_LEN];
Denis Vlasenko92258542006-11-01 10:25:35 +000081 char mode[8];
Denis Vlasenko92258542006-11-01 10:25:35 +000082 int maplineno = 1;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000083 int header_printed;
Denis Vlasenko1d426652008-03-17 09:09:09 +000084 int multiplier = 0;
85 unsigned opt;
86 enum {
87 OPT_M = (1 << 0),
88 OPT_m = (1 << 1),
89 OPT_p = (1 << 2),
90 OPT_n = (1 << 3),
91 OPT_a = (1 << 4),
92 OPT_b = (1 << 5),
93 OPT_s = (1 << 6),
94 OPT_i = (1 << 7),
95 OPT_r = (1 << 8),
96 OPT_v = (1 << 9),
97 };
98#define optMult (opt & OPT_M)
99#define optNative (opt & OPT_n)
100#define optAll (opt & OPT_a)
101#define optBins (opt & OPT_b)
102#define optSub (opt & OPT_s)
103#define optInfo (opt & OPT_i)
104#define optReset (opt & OPT_r)
105#define optVerbose (opt & OPT_v)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000106
107#define next (current^1)
108
109 proFile = defaultpro;
110 mapFile = defaultmap;
111
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200112 opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000113
Denis Vlasenko1d426652008-03-17 09:09:09 +0000114 if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
115 int fd, to_write;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000116
117 /*
118 * When writing the multiplier, if the length of the write is
119 * not sizeof(int), the multiplier is not changed
120 */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000121 to_write = sizeof(int);
122 if (!optMult)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200123 to_write = 1; /* sth different from sizeof(int) */
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000124
Denis Vlasenko92258542006-11-01 10:25:35 +0000125 fd = xopen(defaultpro, O_WRONLY);
Denis Vlasenkoa89d50f2007-11-13 17:51:40 +0000126 xwrite(fd, &multiplier, to_write);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000127 close(fd);
128 return EXIT_SUCCESS;
129 }
130
131 /*
132 * Use an fd for the profiling buffer, to skip stdio overhead
133 */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000134 len = MAXINT(ssize_t);
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000135 buf = xmalloc_xopen_read_close(proFile, &len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000136 if (!optNative) {
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000137 int entries = len / sizeof(*buf);
Denis Vlasenko92258542006-11-01 10:25:35 +0000138 int big = 0, small = 0, i;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000139 unsigned *p;
140
141 for (p = buf+1; p < buf+entries; p++) {
142 if (*p & ~0U << (sizeof(*buf)*4))
143 big++;
144 if (*p & ((1 << (sizeof(*buf)*4))-1))
145 small++;
146 }
147 if (big > small) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000148 bb_error_msg("assuming reversed byte order, "
149 "use -n to force native byte order");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000150 for (p = buf; p < buf+entries; p++)
151 for (i = 0; i < sizeof(*buf)/2; i++) {
152 unsigned char *b = (unsigned char *) p;
153 unsigned char tmp;
154
155 tmp = b[i];
156 b[i] = b[sizeof(*buf)-i-1];
157 b[sizeof(*buf)-i-1] = tmp;
158 }
159 }
160 }
161
162 step = buf[0];
163 if (optInfo) {
Denys Vlasenko327f5502013-11-29 16:45:45 +0100164 printf("Sampling_step: %u\n", step);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000165 return EXIT_SUCCESS;
166 }
167
168 total = 0;
169
Denis Vlasenko5415c852008-07-21 23:05:26 +0000170 map = xfopen_for_read(mapFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000171
Denis Vlasenko92258542006-11-01 10:25:35 +0000172 while (fgets(mapline, S_LEN, map)) {
173 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000174 bb_error_msg_and_die("%s(%i): wrong map line",
Denys Vlasenko69675782013-01-14 01:34:48 +0100175 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000176
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100177 if (strcmp(fn_name, "_stext") == 0) /* only elf works like this */ {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000178 add0 = fn_add;
179 break;
180 }
181 maplineno++;
182 }
183
184 if (!add0)
Rob Landleyb7285002005-09-14 15:28:15 +0000185 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000186
187 /*
188 * Main loop.
189 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000190 while (fgets(mapline, S_LEN, map)) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000191 unsigned int this = 0;
192
Denis Vlasenko92258542006-11-01 10:25:35 +0000193 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
Rob Landleyb7285002005-09-14 15:28:15 +0000194 bb_error_msg_and_die("%s(%i): wrong map line",
Denis Vlasenko92258542006-11-01 10:25:35 +0000195 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000196
197 header_printed = 0;
198
199 /* ignore any LEADING (before a '[tT]' symbol is found)
200 Absolute symbols */
201 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100202 if (*mode != 'T' && *mode != 't'
203 && *mode != 'W' && *mode != 'w'
204 ) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200205 break; /* only text is profiled */
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100206 }
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000207
208 if (indx >= len / sizeof(*buf))
209 bb_error_msg_and_die("profile address out of range. "
Denys Vlasenko69675782013-01-14 01:34:48 +0100210 "Wrong map file?");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000211
212 while (indx < (next_add-add0)/step) {
213 if (optBins && (buf[indx] || optAll)) {
214 if (!header_printed) {
Denis Vlasenko92258542006-11-01 10:25:35 +0000215 printf("%s:\n", fn_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000216 header_printed = 1;
217 }
Denis Vlasenko92258542006-11-01 10:25:35 +0000218 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000219 }
220 this += buf[indx++];
221 }
222 total += this;
223
224 if (optBins) {
225 if (optVerbose || this > 0)
Denis Vlasenko92258542006-11-01 10:25:35 +0000226 printf(" total\t\t\t\t%u\n", this);
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100227 } else
228 if ((this || optAll)
229 && (fn_len = next_add-fn_add) != 0
Denis Vlasenko1d426652008-03-17 09:09:09 +0000230 ) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000231 if (optVerbose)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100232 printf("%016llx %-40s %6u %8.4f\n", fn_add,
Denys Vlasenko69675782013-01-14 01:34:48 +0100233 fn_name, this, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000234 else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100235 printf("%6u %-40s %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100236 this, fn_name, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000237 if (optSub) {
238 unsigned long long scan;
239
240 for (scan = (fn_add-add0)/step + 1;
241 scan < (next_add-add0)/step; scan++) {
242 unsigned long long addr;
243
244 addr = (scan - 1)*step + add0;
245 printf("\t%#llx\t%s+%#llx\t%u\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100246 addr, fn_name, addr - fn_add,
247 buf[scan]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000248 }
249 }
250 }
251
252 fn_add = next_add;
Denis Vlasenko92258542006-11-01 10:25:35 +0000253 strcpy(fn_name, next_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000254
255 maplineno++;
256 }
257
258 /* clock ticks, out of kernel text - probably modules */
Denys Vlasenko327f5502013-11-29 16:45:45 +0100259 printf("%6u %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000260
261 /* trailer */
262 if (optVerbose)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100263 printf("%016x %-40s %6u %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100264 0, "total", total, total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000265 else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100266 printf("%6u %-40s %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100267 total, "total", total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000268
269 fclose(map);
270 free(buf);
271
272 return EXIT_SUCCESS;
273}