blob: 219c0c18c02f7b95af600a18124f21089b27a62f [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/*
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02003 * readprofile.c - used to read /proc/profile
Paul Mundt8b2d02e2005-05-20 17:22:18 +00004 *
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02005 * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
Paul Mundt8b2d02e2005-05-20 17:22:18 +00006 *
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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020035//config: bool "readprofile (7.2 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010036//config: default y
37//config: #select PLATFORM_LINUX
38//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020039//config: This allows you to parse /proc/profile for basic profiling.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010040
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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000064int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000065int readprofile_main(int argc UNUSED_PARAM, char **argv)
Paul Mundt8b2d02e2005-05-20 17:22:18 +000066{
67 FILE *map;
Denis Vlasenko1d426652008-03-17 09:09:09 +000068 const char *mapFile, *proFile;
Denys Vlasenko0e09ded2018-03-11 19:35:58 +010069 unsigned long indx;
Denis Vlasenkoea620772006-10-14 02:23:43 +000070 size_t len;
Denys Vlasenko0e09ded2018-03-11 19:35:58 +010071 uint64_t add0;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000072 unsigned int step;
73 unsigned int *buf, total, fn_len;
Denis Vlasenkocad36682006-09-22 08:39:49 +000074 unsigned long long fn_add, next_add; /* current and next address */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000075 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000076 char mapline[S_LEN];
Denis Vlasenko92258542006-11-01 10:25:35 +000077 char mode[8];
Denys Vlasenko0e09ded2018-03-11 19:35:58 +010078 int maplineno;
79 int multiplier;
Denis Vlasenko1d426652008-03-17 09:09:09 +000080 unsigned opt;
81 enum {
82 OPT_M = (1 << 0),
83 OPT_m = (1 << 1),
84 OPT_p = (1 << 2),
85 OPT_n = (1 << 3),
86 OPT_a = (1 << 4),
87 OPT_b = (1 << 5),
88 OPT_s = (1 << 6),
89 OPT_i = (1 << 7),
90 OPT_r = (1 << 8),
91 OPT_v = (1 << 9),
92 };
93#define optMult (opt & OPT_M)
94#define optNative (opt & OPT_n)
95#define optAll (opt & OPT_a)
96#define optBins (opt & OPT_b)
97#define optSub (opt & OPT_s)
98#define optInfo (opt & OPT_i)
99#define optReset (opt & OPT_r)
100#define optVerbose (opt & OPT_v)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000101
102#define next (current^1)
103
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100104 proFile = "/proc/profile";
105 mapFile = "/boot/System.map";
106 multiplier = 0;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000107
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200108 opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000109
Denis Vlasenko1d426652008-03-17 09:09:09 +0000110 if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
111 int fd, to_write;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000112
113 /*
114 * When writing the multiplier, if the length of the write is
115 * not sizeof(int), the multiplier is not changed
116 */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000117 to_write = sizeof(int);
118 if (!optMult)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200119 to_write = 1; /* sth different from sizeof(int) */
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000120
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100121 fd = xopen("/proc/profile", O_WRONLY);
Denis Vlasenkoa89d50f2007-11-13 17:51:40 +0000122 xwrite(fd, &multiplier, to_write);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000123 close(fd);
124 return EXIT_SUCCESS;
125 }
126
127 /*
128 * Use an fd for the profiling buffer, to skip stdio overhead
129 */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000130 len = MAXINT(ssize_t);
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000131 buf = xmalloc_xopen_read_close(proFile, &len);
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100132 len /= sizeof(*buf);
133
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000134 if (!optNative) {
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100135 int big = 0, small = 0;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000136 unsigned *p;
137
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100138 for (p = buf+1; p < buf+len; p++) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000139 if (*p & ~0U << (sizeof(*buf)*4))
140 big++;
141 if (*p & ((1 << (sizeof(*buf)*4))-1))
142 small++;
143 }
144 if (big > small) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000145 bb_error_msg("assuming reversed byte order, "
146 "use -n to force native byte order");
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100147 BUILD_BUG_ON(sizeof(*p) > 8);
148 for (p = buf; p < buf+len; p++) {
149 if (sizeof(*p) == 2)
150 *p = bswap_16(*p);
151 if (sizeof(*p) == 4)
152 *p = bswap_32(*p);
153 if (sizeof(*p) == 8)
154 *p = bb_bswap_64(*p);
155 }
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000156 }
157 }
158
159 step = buf[0];
160 if (optInfo) {
Denys Vlasenko327f5502013-11-29 16:45:45 +0100161 printf("Sampling_step: %u\n", step);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000162 return EXIT_SUCCESS;
163 }
164
Denis Vlasenko5415c852008-07-21 23:05:26 +0000165 map = xfopen_for_read(mapFile);
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100166 add0 = 0;
167 maplineno = 1;
Denis Vlasenko92258542006-11-01 10:25:35 +0000168 while (fgets(mapline, S_LEN, map)) {
169 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000170 bb_error_msg_and_die("%s(%i): wrong map line",
Denys Vlasenko69675782013-01-14 01:34:48 +0100171 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000172
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100173 if (strcmp(fn_name, "_stext") == 0) /* only elf works like this */ {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000174 add0 = fn_add;
175 break;
176 }
177 maplineno++;
178 }
179
180 if (!add0)
Rob Landleyb7285002005-09-14 15:28:15 +0000181 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000182
183 /*
184 * Main loop.
185 */
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100186 total = 0;
187 indx = 1;
Denis Vlasenko92258542006-11-01 10:25:35 +0000188 while (fgets(mapline, S_LEN, map)) {
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100189 bool header_printed;
190 unsigned int this;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000191
Denis Vlasenko92258542006-11-01 10:25:35 +0000192 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
Rob Landleyb7285002005-09-14 15:28:15 +0000193 bb_error_msg_and_die("%s(%i): wrong map line",
Denis Vlasenko92258542006-11-01 10:25:35 +0000194 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000195
196 header_printed = 0;
197
198 /* ignore any LEADING (before a '[tT]' symbol is found)
199 Absolute symbols */
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100200 if ((mode[0] == 'A' || mode[0] == '?') && total == 0)
201 continue;
202 if ((mode[0]|0x20) != 't' && (mode[0]|0x20) != 'w') {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200203 break; /* only text is profiled */
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100204 }
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000205
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100206 if (indx >= len)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000207 bb_error_msg_and_die("profile address out of range. "
Denys Vlasenko69675782013-01-14 01:34:48 +0100208 "Wrong map file?");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000209
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100210 this = 0;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000211 while (indx < (next_add-add0)/step) {
212 if (optBins && (buf[indx] || optAll)) {
213 if (!header_printed) {
Denis Vlasenko92258542006-11-01 10:25:35 +0000214 printf("%s:\n", fn_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000215 header_printed = 1;
216 }
Denis Vlasenko92258542006-11-01 10:25:35 +0000217 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000218 }
219 this += buf[indx++];
220 }
221 total += this;
222
223 if (optBins) {
224 if (optVerbose || this > 0)
Denis Vlasenko92258542006-11-01 10:25:35 +0000225 printf(" total\t\t\t\t%u\n", this);
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100226 } else
227 if ((this || optAll)
228 && (fn_len = next_add-fn_add) != 0
Denis Vlasenko1d426652008-03-17 09:09:09 +0000229 ) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000230 if (optVerbose)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100231 printf("%016llx %-40s %6u %8.4f\n", fn_add,
Denys Vlasenko69675782013-01-14 01:34:48 +0100232 fn_name, this, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000233 else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100234 printf("%6u %-40s %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100235 this, fn_name, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000236 if (optSub) {
237 unsigned long long scan;
238
239 for (scan = (fn_add-add0)/step + 1;
240 scan < (next_add-add0)/step; scan++) {
241 unsigned long long addr;
242
243 addr = (scan - 1)*step + add0;
244 printf("\t%#llx\t%s+%#llx\t%u\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100245 addr, fn_name, addr - fn_add,
246 buf[scan]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000247 }
248 }
249 }
250
251 fn_add = next_add;
Denis Vlasenko92258542006-11-01 10:25:35 +0000252 strcpy(fn_name, next_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000253
254 maplineno++;
255 }
256
257 /* clock ticks, out of kernel text - probably modules */
Denys Vlasenko0e09ded2018-03-11 19:35:58 +0100258 printf("%6u *unknown*\n", buf[len-1]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000259
260 /* trailer */
261 if (optVerbose)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100262 printf("%016x %-40s %6u %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100263 0, "total", total, total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000264 else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100265 printf("%6u %-40s %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100266 total, "total", total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000267
Denys Vlasenko035e7152017-08-06 20:39:27 +0200268 if (ENABLE_FEATURE_CLEAN_UP) {
269 fclose(map);
270 free(buf);
271 }
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000272
273 return EXIT_SUCCESS;
274}