blob: 345b676ba1ae007bc612dd95ddfcb2579942056d [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 */
34
Pere Orga5bc8c002011-04-11 03:29:49 +020035//usage:#define readprofile_trivial_usage
36//usage: "[OPTIONS]"
37//usage:#define readprofile_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020038//usage: " -m mapfile (Default: /boot/System.map)"
Pere Orga5bc8c002011-04-11 03:29:49 +020039//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"
48
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000049#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000050#include <sys/utsname.h>
Paul Mundt8b2d02e2005-05-20 17:22:18 +000051
52#define S_LEN 128
53
54/* These are the defaults */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000055static const char defaultmap[] ALIGN1 = "/boot/System.map";
56static const char defaultpro[] ALIGN1 = "/proc/profile";
Paul Mundt8b2d02e2005-05-20 17:22:18 +000057
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000058int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000059int readprofile_main(int argc UNUSED_PARAM, char **argv)
Paul Mundt8b2d02e2005-05-20 17:22:18 +000060{
61 FILE *map;
Denis Vlasenko1d426652008-03-17 09:09:09 +000062 const char *mapFile, *proFile;
Denis Vlasenko92258542006-11-01 10:25:35 +000063 unsigned long indx = 1;
Denis Vlasenkoea620772006-10-14 02:23:43 +000064 size_t len;
Denis Vlasenko92258542006-11-01 10:25:35 +000065 uint64_t add0 = 0;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000066 unsigned int step;
67 unsigned int *buf, total, fn_len;
Denis Vlasenkocad36682006-09-22 08:39:49 +000068 unsigned long long fn_add, next_add; /* current and next address */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000069 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
Paul Mundt8b2d02e2005-05-20 17:22:18 +000070 char mapline[S_LEN];
Denis Vlasenko92258542006-11-01 10:25:35 +000071 char mode[8];
Denis Vlasenko92258542006-11-01 10:25:35 +000072 int maplineno = 1;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000073 int header_printed;
Denis Vlasenko1d426652008-03-17 09:09:09 +000074 int multiplier = 0;
75 unsigned opt;
76 enum {
77 OPT_M = (1 << 0),
78 OPT_m = (1 << 1),
79 OPT_p = (1 << 2),
80 OPT_n = (1 << 3),
81 OPT_a = (1 << 4),
82 OPT_b = (1 << 5),
83 OPT_s = (1 << 6),
84 OPT_i = (1 << 7),
85 OPT_r = (1 << 8),
86 OPT_v = (1 << 9),
87 };
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)
Paul Mundt8b2d02e2005-05-20 17:22:18 +000096
97#define next (current^1)
98
99 proFile = defaultpro;
100 mapFile = defaultmap;
101
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200102 opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000103
Denis Vlasenko1d426652008-03-17 09:09:09 +0000104 if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
105 int fd, to_write;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000106
107 /*
108 * When writing the multiplier, if the length of the write is
109 * not sizeof(int), the multiplier is not changed
110 */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000111 to_write = sizeof(int);
112 if (!optMult)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200113 to_write = 1; /* sth different from sizeof(int) */
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000114
Denis Vlasenko92258542006-11-01 10:25:35 +0000115 fd = xopen(defaultpro, O_WRONLY);
Denis Vlasenkoa89d50f2007-11-13 17:51:40 +0000116 xwrite(fd, &multiplier, to_write);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000117 close(fd);
118 return EXIT_SUCCESS;
119 }
120
121 /*
122 * Use an fd for the profiling buffer, to skip stdio overhead
123 */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000124 len = MAXINT(ssize_t);
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000125 buf = xmalloc_xopen_read_close(proFile, &len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000126 if (!optNative) {
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000127 int entries = len / sizeof(*buf);
Denis Vlasenko92258542006-11-01 10:25:35 +0000128 int big = 0, small = 0, i;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000129 unsigned *p;
130
131 for (p = buf+1; p < buf+entries; p++) {
132 if (*p & ~0U << (sizeof(*buf)*4))
133 big++;
134 if (*p & ((1 << (sizeof(*buf)*4))-1))
135 small++;
136 }
137 if (big > small) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000138 bb_error_msg("assuming reversed byte order, "
139 "use -n to force native byte order");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000140 for (p = buf; p < buf+entries; p++)
141 for (i = 0; i < sizeof(*buf)/2; i++) {
142 unsigned char *b = (unsigned char *) p;
143 unsigned char tmp;
144
145 tmp = b[i];
146 b[i] = b[sizeof(*buf)-i-1];
147 b[sizeof(*buf)-i-1] = tmp;
148 }
149 }
150 }
151
152 step = buf[0];
153 if (optInfo) {
Denys Vlasenko327f5502013-11-29 16:45:45 +0100154 printf("Sampling_step: %u\n", step);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000155 return EXIT_SUCCESS;
156 }
157
158 total = 0;
159
Denis Vlasenko5415c852008-07-21 23:05:26 +0000160 map = xfopen_for_read(mapFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000161
Denis Vlasenko92258542006-11-01 10:25:35 +0000162 while (fgets(mapline, S_LEN, map)) {
163 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000164 bb_error_msg_and_die("%s(%i): wrong map line",
Denys Vlasenko69675782013-01-14 01:34:48 +0100165 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000166
Denis Vlasenko92258542006-11-01 10:25:35 +0000167 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000168 add0 = fn_add;
169 break;
170 }
171 maplineno++;
172 }
173
174 if (!add0)
Rob Landleyb7285002005-09-14 15:28:15 +0000175 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000176
177 /*
178 * Main loop.
179 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000180 while (fgets(mapline, S_LEN, map)) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000181 unsigned int this = 0;
182
Denis Vlasenko92258542006-11-01 10:25:35 +0000183 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
Rob Landleyb7285002005-09-14 15:28:15 +0000184 bb_error_msg_and_die("%s(%i): wrong map line",
Denis Vlasenko92258542006-11-01 10:25:35 +0000185 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000186
187 header_printed = 0;
188
189 /* ignore any LEADING (before a '[tT]' symbol is found)
190 Absolute symbols */
191 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100192 if (*mode != 'T' && *mode != 't'
193 && *mode != 'W' && *mode != 'w'
194 ) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200195 break; /* only text is profiled */
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100196 }
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000197
198 if (indx >= len / sizeof(*buf))
199 bb_error_msg_and_die("profile address out of range. "
Denys Vlasenko69675782013-01-14 01:34:48 +0100200 "Wrong map file?");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000201
202 while (indx < (next_add-add0)/step) {
203 if (optBins && (buf[indx] || optAll)) {
204 if (!header_printed) {
Denis Vlasenko92258542006-11-01 10:25:35 +0000205 printf("%s:\n", fn_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000206 header_printed = 1;
207 }
Denis Vlasenko92258542006-11-01 10:25:35 +0000208 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000209 }
210 this += buf[indx++];
211 }
212 total += this;
213
214 if (optBins) {
215 if (optVerbose || this > 0)
Denis Vlasenko92258542006-11-01 10:25:35 +0000216 printf(" total\t\t\t\t%u\n", this);
Denis Vlasenko1d426652008-03-17 09:09:09 +0000217 } else if ((this || optAll)
218 && (fn_len = next_add-fn_add) != 0
219 ) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000220 if (optVerbose)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100221 printf("%016llx %-40s %6u %8.4f\n", fn_add,
Denys Vlasenko69675782013-01-14 01:34:48 +0100222 fn_name, this, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000223 else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100224 printf("%6u %-40s %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100225 this, fn_name, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000226 if (optSub) {
227 unsigned long long scan;
228
229 for (scan = (fn_add-add0)/step + 1;
230 scan < (next_add-add0)/step; scan++) {
231 unsigned long long addr;
232
233 addr = (scan - 1)*step + add0;
234 printf("\t%#llx\t%s+%#llx\t%u\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100235 addr, fn_name, addr - fn_add,
236 buf[scan]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000237 }
238 }
239 }
240
241 fn_add = next_add;
Denis Vlasenko92258542006-11-01 10:25:35 +0000242 strcpy(fn_name, next_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000243
244 maplineno++;
245 }
246
247 /* clock ticks, out of kernel text - probably modules */
Denys Vlasenko327f5502013-11-29 16:45:45 +0100248 printf("%6u %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000249
250 /* trailer */
251 if (optVerbose)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100252 printf("%016x %-40s %6u %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100253 0, "total", total, total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000254 else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100255 printf("%6u %-40s %8.4f\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100256 total, "total", total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000257
258 fclose(map);
259 free(buf);
260
261 return EXIT_SUCCESS;
262}