blob: 02afedcf01edc18c597c92cac1b47ce8741af559 [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/*
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>
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 Vlasenko13858992006-10-08 12:49:22 +000041static const char defaultmap[] = "/boot/System.map";
42static const char defaultpro[] = "/proc/profile";
Paul Mundt8b2d02e2005-05-20 17:22:18 +000043
Denis Vlasenko06af2162007-02-03 17:28:39 +000044int readprofile_main(int argc, char **argv);
Paul Mundt8b2d02e2005-05-20 17:22:18 +000045int readprofile_main(int argc, char **argv)
46{
47 FILE *map;
Denis Vlasenko92258542006-11-01 10:25:35 +000048 const char *mapFile, *proFile, *mult = 0;
49 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];
58 int optAll = 0, optInfo = 0, optReset = 0;
59 int optVerbose = 0, optNative = 0;
60 int optBins = 0, optSub = 0;
61 int maplineno = 1;
Paul Mundt8b2d02e2005-05-20 17:22:18 +000062 int header_printed;
63
64#define next (current^1)
65
66 proFile = defaultpro;
67 mapFile = defaultmap;
68
Denis Vlasenko67b23e62006-10-03 21:00:06 +000069 opt_complementary = "nn:aa:bb:ss:ii:rr:vv";
70 getopt32(argc, argv, "M:m:p:nabsirv",
Denis Vlasenkocad36682006-09-22 08:39:49 +000071 &mult, &mapFile, &proFile,
72 &optNative, &optAll, &optBins, &optSub,
73 &optInfo, &optReset, &optVerbose);
Paul Mundt8b2d02e2005-05-20 17:22:18 +000074
75 if (optReset || mult) {
76 int multiplier, fd, to_write;
77
78 /*
79 * When writing the multiplier, if the length of the write is
80 * not sizeof(int), the multiplier is not changed
81 */
82 if (mult) {
Denis Vlasenko13858992006-10-08 12:49:22 +000083 multiplier = xatoi_u(mult);
Paul Mundt8b2d02e2005-05-20 17:22:18 +000084 to_write = sizeof(int);
85 } else {
86 multiplier = 0;
87 to_write = 1; /* sth different from sizeof(int) */
88 }
89
Denis Vlasenko92258542006-11-01 10:25:35 +000090 fd = xopen(defaultpro, O_WRONLY);
Paul Mundt8b2d02e2005-05-20 17:22:18 +000091
Denis Vlasenko92258542006-11-01 10:25:35 +000092 if (full_write(fd, &multiplier, to_write) != to_write)
Paul Mundt8b2d02e2005-05-20 17:22:18 +000093 bb_perror_msg_and_die("error writing %s", defaultpro);
94
95 close(fd);
96 return EXIT_SUCCESS;
97 }
98
99 /*
100 * Use an fd for the profiling buffer, to skip stdio overhead
101 */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000102 len = MAXINT(ssize_t);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000103 buf = xmalloc_open_read_close(proFile, &len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000104 if (!optNative) {
105 int entries = len/sizeof(*buf);
Denis Vlasenko92258542006-11-01 10:25:35 +0000106 int big = 0, small = 0, i;
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000107 unsigned *p;
108
109 for (p = buf+1; p < buf+entries; p++) {
110 if (*p & ~0U << (sizeof(*buf)*4))
111 big++;
112 if (*p & ((1 << (sizeof(*buf)*4))-1))
113 small++;
114 }
115 if (big > small) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000116 bb_error_msg("assuming reversed byte order, "
117 "use -n to force native byte order");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000118 for (p = buf; p < buf+entries; p++)
119 for (i = 0; i < sizeof(*buf)/2; i++) {
120 unsigned char *b = (unsigned char *) p;
121 unsigned char tmp;
122
123 tmp = b[i];
124 b[i] = b[sizeof(*buf)-i-1];
125 b[sizeof(*buf)-i-1] = tmp;
126 }
127 }
128 }
129
130 step = buf[0];
131 if (optInfo) {
132 printf("Sampling_step: %i\n", step);
133 return EXIT_SUCCESS;
134 }
135
136 total = 0;
137
Rob Landleyd921b2e2006-08-03 15:41:12 +0000138 map = xfopen(mapFile, "r");
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000139
Denis Vlasenko92258542006-11-01 10:25:35 +0000140 while (fgets(mapline, S_LEN, map)) {
141 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000142 bb_error_msg_and_die("%s(%i): wrong map line",
143 mapFile, maplineno);
144
Denis Vlasenko92258542006-11-01 10:25:35 +0000145 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000146 add0 = fn_add;
147 break;
148 }
149 maplineno++;
150 }
151
152 if (!add0)
Rob Landleyb7285002005-09-14 15:28:15 +0000153 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000154
155 /*
156 * Main loop.
157 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000158 while (fgets(mapline, S_LEN, map)) {
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000159 unsigned int this = 0;
160
Denis Vlasenko92258542006-11-01 10:25:35 +0000161 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
Rob Landleyb7285002005-09-14 15:28:15 +0000162 bb_error_msg_and_die("%s(%i): wrong map line",
Denis Vlasenko92258542006-11-01 10:25:35 +0000163 mapFile, maplineno);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000164
165 header_printed = 0;
166
167 /* ignore any LEADING (before a '[tT]' symbol is found)
168 Absolute symbols */
169 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
170 if (*mode != 'T' && *mode != 't' &&
171 *mode != 'W' && *mode != 'w')
172 break; /* only text is profiled */
173
174 if (indx >= len / sizeof(*buf))
175 bb_error_msg_and_die("profile address out of range. "
176 "Wrong map file?");
177
178 while (indx < (next_add-add0)/step) {
179 if (optBins && (buf[indx] || optAll)) {
180 if (!header_printed) {
Denis Vlasenko92258542006-11-01 10:25:35 +0000181 printf("%s:\n", fn_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000182 header_printed = 1;
183 }
Denis Vlasenko92258542006-11-01 10:25:35 +0000184 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000185 }
186 this += buf[indx++];
187 }
188 total += this;
189
190 if (optBins) {
191 if (optVerbose || this > 0)
Denis Vlasenko92258542006-11-01 10:25:35 +0000192 printf(" total\t\t\t\t%u\n", this);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000193 } else if ((this || optAll) &&
194 (fn_len = next_add-fn_add) != 0) {
195 if (optVerbose)
196 printf("%016llx %-40s %6i %8.4f\n", fn_add,
Denis Vlasenko92258542006-11-01 10:25:35 +0000197 fn_name, this, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000198 else
199 printf("%6i %-40s %8.4f\n",
Denis Vlasenko92258542006-11-01 10:25:35 +0000200 this, fn_name, this/(double)fn_len);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000201 if (optSub) {
202 unsigned long long scan;
203
204 for (scan = (fn_add-add0)/step + 1;
205 scan < (next_add-add0)/step; scan++) {
206 unsigned long long addr;
207
208 addr = (scan - 1)*step + add0;
209 printf("\t%#llx\t%s+%#llx\t%u\n",
210 addr, fn_name, addr - fn_add,
211 buf[scan]);
212 }
213 }
214 }
215
216 fn_add = next_add;
Denis Vlasenko92258542006-11-01 10:25:35 +0000217 strcpy(fn_name, next_name);
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000218
219 maplineno++;
220 }
221
222 /* clock ticks, out of kernel text - probably modules */
223 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
224
225 /* trailer */
226 if (optVerbose)
227 printf("%016x %-40s %6i %8.4f\n",
Denis Vlasenko92258542006-11-01 10:25:35 +0000228 0, "total", total, total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000229 else
230 printf("%6i %-40s %8.4f\n",
Denis Vlasenko92258542006-11-01 10:25:35 +0000231 total, "total", total/(double)(fn_add-add0));
Paul Mundt8b2d02e2005-05-20 17:22:18 +0000232
233 fclose(map);
234 free(buf);
235
236 return EXIT_SUCCESS;
237}