]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * readprofile.c - used to read /proc/profile | |
3 | * | |
fd6b7a7f | 4 | * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it) |
6dbe3af9 KZ |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
7cebf0bb SK |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
6dbe3af9 KZ |
19 | */ |
20 | ||
7eda085c | 21 | /* |
b50945d4 | 22 | * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c | 23 | * - added Native Language Support |
eb63b9b8 KZ |
24 | * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com> |
25 | * - 64bit clean patch | |
c07ebfa1 KZ |
26 | * 3Feb2001 Andrew Morton <andrewm@uow.edu.au> |
27 | * - -M option to write profile multiplier. | |
612721db KZ |
28 | * 2001-11-07 Werner Almesberger <wa@almesberger.net> |
29 | * - byte order auto-detection and -n option | |
30 | * 2001-11-09 Werner Almesberger <wa@almesberger.net> | |
31 | * - skip step size (index 0) | |
1d4ad1de KZ |
32 | * 2002-03-09 John Levon <moz@compsoc.man.ac.uk> |
33 | * - make maplineno do something | |
df1dddf9 KZ |
34 | * 2002-11-28 Mads Martin Joergensen + |
35 | * - also try /boot/System.map-`uname -r` | |
36 | * 2003-04-09 Werner Almesberger <wa@almesberger.net> | |
37 | * - fixed off-by eight error and improved heuristics in byte order detection | |
c129767e KZ |
38 | * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM> |
39 | * - added -s option; example of use: | |
40 | * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3" | |
7eda085c KZ |
41 | */ |
42 | ||
fd6b7a7f | 43 | #include <errno.h> |
df1dddf9 | 44 | #include <fcntl.h> |
a3f0ea51 SK |
45 | #include <getopt.h> |
46 | #include <stdio.h> | |
6dbe3af9 | 47 | #include <stdlib.h> |
6dbe3af9 | 48 | #include <string.h> |
fd6b7a7f | 49 | #include <sys/stat.h> |
a3f0ea51 | 50 | #include <sys/types.h> |
df1dddf9 | 51 | #include <sys/utsname.h> |
a3f0ea51 | 52 | #include <unistd.h> |
2ab428f6 | 53 | |
7eda085c | 54 | #include "nls.h" |
a3f0ea51 | 55 | #include "xalloc.h" |
efb8854f | 56 | #include "closestream.h" |
6dbe3af9 | 57 | |
6dbe3af9 KZ |
58 | #define S_LEN 128 |
59 | ||
fd6b7a7f | 60 | /* These are the defaults */ |
b22550fa | 61 | static char defaultmap[]="/boot/System.map"; |
6dbe3af9 | 62 | static char defaultpro[]="/proc/profile"; |
6dbe3af9 | 63 | |
a3f0ea51 SK |
64 | static FILE *myopen(char *name, char *mode, int *flag) |
65 | { | |
e8f26419 | 66 | int len = strlen(name); |
c07ebfa1 | 67 | |
a3f0ea51 | 68 | if (!strcmp(name + len - 3, ".gz")) { |
e8f26419 | 69 | FILE *res; |
a3f0ea51 | 70 | char *cmdline = xmalloc(len + 6); |
e8f26419 | 71 | sprintf(cmdline, "zcat %s", name); |
a3f0ea51 | 72 | res = popen(cmdline, mode); |
e8f26419 KZ |
73 | free(cmdline); |
74 | *flag = 1; | |
75 | return res; | |
c07ebfa1 | 76 | } |
e8f26419 | 77 | *flag = 0; |
a3f0ea51 | 78 | return fopen(name, mode); |
6dbe3af9 KZ |
79 | } |
80 | ||
df1dddf9 KZ |
81 | #ifndef BOOT_SYSTEM_MAP |
82 | #define BOOT_SYSTEM_MAP "/boot/System.map-" | |
83 | #endif | |
a3f0ea51 SK |
84 | |
85 | static char *boot_uname_r_str(void) | |
86 | { | |
df1dddf9 KZ |
87 | struct utsname uname_info; |
88 | char *s; | |
89 | size_t len; | |
90 | ||
91 | if (uname(&uname_info)) | |
92 | return ""; | |
93 | len = strlen(BOOT_SYSTEM_MAP) + strlen(uname_info.release) + 1; | |
94 | s = xmalloc(len); | |
95 | strcpy(s, BOOT_SYSTEM_MAP); | |
96 | strcat(s, uname_info.release); | |
97 | return s; | |
98 | } | |
99 | ||
86be6a32 | 100 | static void __attribute__((__noreturn__)) usage(void) |
a3f0ea51 | 101 | { |
86be6a32 | 102 | FILE *out = stdout; |
443f7313 SK |
103 | fputs(USAGE_HEADER, out); |
104 | fprintf(out, _(" %s [options]\n"), program_invocation_short_name); | |
443f7313 | 105 | |
451dbcfa BS |
106 | fputs(USAGE_SEPARATOR, out); |
107 | fputs(_("Display kernel profiling information.\n"), out); | |
108 | ||
109 | fputs(USAGE_OPTIONS, out); | |
443f7313 SK |
110 | fprintf(out, |
111 | _(" -m, --mapfile <mapfile> (defaults: \"%s\" and\n"), defaultmap); | |
112 | fprintf(out, | |
113 | _(" \"%s\")\n"), boot_uname_r_str()); | |
114 | fprintf(out, | |
115 | _(" -p, --profile <pro-file> (default: \"%s\")\n"), defaultpro); | |
116 | fputs(_(" -M, --multiplier <mult> set the profiling multiplier to <mult>\n"), out); | |
117 | fputs(_(" -i, --info print only info about the sampling step\n"), out); | |
118 | fputs(_(" -v, --verbose print verbose data\n"), out); | |
119 | fputs(_(" -a, --all print all symbols, even if count is 0\n"), out); | |
120 | fputs(_(" -b, --histbin print individual histogram-bin counts\n"), out); | |
121 | fputs(_(" -s, --counters print individual counters within functions\n"), out); | |
122 | fputs(_(" -r, --reset reset all the counters (root only)\n"), out); | |
123 | fputs(_(" -n, --no-auto disable byte order auto-detection\n"), out); | |
124 | fputs(USAGE_SEPARATOR, out); | |
125 | fputs(USAGE_HELP, out); | |
126 | fputs(USAGE_VERSION, out); | |
127 | fprintf(out, USAGE_MAN_TAIL("readprofile(8)")); | |
86be6a32 | 128 | exit(EXIT_SUCCESS); |
df1dddf9 KZ |
129 | } |
130 | ||
a3f0ea51 SK |
131 | int main(int argc, char **argv) |
132 | { | |
c07ebfa1 KZ |
133 | FILE *map; |
134 | int proFd; | |
87918040 | 135 | char *mapFile, *proFile, *mult = NULL; |
a3f0ea51 SK |
136 | size_t len = 0, indx = 1; |
137 | unsigned long long add0 = 0; | |
c07ebfa1 KZ |
138 | unsigned int step; |
139 | unsigned int *buf, total, fn_len; | |
a3f0ea51 SK |
140 | unsigned long long fn_add, next_add; /* current and next address */ |
141 | char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ | |
c07ebfa1 KZ |
142 | char mode[8]; |
143 | int c; | |
33d18dc4 | 144 | ssize_t rc; |
a3f0ea51 SK |
145 | int optAll = 0, optInfo = 0, optReset = 0, optVerbose = 0, optNative = 0; |
146 | int optBins = 0, optSub = 0; | |
c07ebfa1 | 147 | char mapline[S_LEN]; |
a3f0ea51 SK |
148 | int maplineno = 1; |
149 | int popenMap; /* flag to tell if popen() has been used */ | |
f0c8eda1 | 150 | int header_printed; |
6dbe3af9 | 151 | |
443f7313 SK |
152 | static const struct option longopts[] = { |
153 | {"mapfile", required_argument, NULL, 'm'}, | |
154 | {"profile", required_argument, NULL, 'p'}, | |
155 | {"multiplier", required_argument, NULL, 'M'}, | |
156 | {"info", no_argument, NULL, 'i'}, | |
157 | {"verbose", no_argument, NULL, 'v'}, | |
158 | {"all", no_argument, NULL, 'a'}, | |
159 | {"histbin", no_argument, NULL, 'b'}, | |
160 | {"counters", no_argument, NULL, 's'}, | |
9b80bef6 | 161 | {"reset", no_argument, NULL, 'r'}, |
443f7313 SK |
162 | {"no-auto", no_argument, NULL, 'n'}, |
163 | {"version", no_argument, NULL, 'V'}, | |
164 | {"help", no_argument, NULL, 'h'}, | |
87918040 | 165 | {NULL, 0, NULL, 0} |
443f7313 SK |
166 | }; |
167 | ||
6dbe3af9 KZ |
168 | #define next (current^1) |
169 | ||
c07ebfa1 KZ |
170 | setlocale(LC_ALL, ""); |
171 | bindtextdomain(PACKAGE, LOCALEDIR); | |
172 | textdomain(PACKAGE); | |
efb8854f | 173 | atexit(close_stdout); |
c07ebfa1 | 174 | |
df1dddf9 KZ |
175 | proFile = defaultpro; |
176 | mapFile = defaultmap; | |
c07ebfa1 | 177 | |
cda9acbe | 178 | while ((c = getopt_long(argc, argv, "m:p:M:ivabsrnVh", longopts, NULL)) != -1) { |
a3f0ea51 | 179 | switch (c) { |
df1dddf9 KZ |
180 | case 'm': |
181 | mapFile = optarg; | |
182 | break; | |
183 | case 'n': | |
184 | optNative++; | |
185 | break; | |
186 | case 'p': | |
187 | proFile = optarg; | |
188 | break; | |
189 | case 'a': | |
190 | optAll++; | |
191 | break; | |
192 | case 'b': | |
193 | optBins++; | |
194 | break; | |
c129767e KZ |
195 | case 's': |
196 | optSub++; | |
197 | break; | |
df1dddf9 KZ |
198 | case 'i': |
199 | optInfo++; | |
200 | break; | |
201 | case 'M': | |
202 | mult = optarg; | |
203 | break; | |
204 | case 'r': | |
205 | optReset++; | |
206 | break; | |
207 | case 'v': | |
208 | optVerbose++; | |
209 | break; | |
210 | case 'V': | |
443f7313 SK |
211 | printf(UTIL_LINUX_VERSION); |
212 | return EXIT_SUCCESS; | |
213 | case 'h': | |
86be6a32 | 214 | usage(); |
df1dddf9 | 215 | default: |
677ec86c | 216 | errtryhelp(EXIT_FAILURE); |
c07ebfa1 KZ |
217 | } |
218 | } | |
219 | ||
220 | if (optReset || mult) { | |
221 | int multiplier, fd, to_write; | |
222 | ||
a3f0ea51 SK |
223 | /* When writing the multiplier, if the length of the |
224 | * write is not sizeof(int), the multiplier is not | |
225 | * changed. */ | |
c07ebfa1 | 226 | if (mult) { |
87918040 | 227 | multiplier = strtoul(mult, NULL, 10); |
c07ebfa1 KZ |
228 | to_write = sizeof(int); |
229 | } else { | |
230 | multiplier = 0; | |
a3f0ea51 SK |
231 | /* sth different from sizeof(int) */ |
232 | to_write = 1; | |
c07ebfa1 KZ |
233 | } |
234 | /* try to become root, just in case */ | |
8f3b568c | 235 | ignore_result( setuid(0) ); |
a3f0ea51 | 236 | fd = open(defaultpro, O_WRONLY); |
c416814a SK |
237 | if (fd < 0) |
238 | err(EXIT_FAILURE, "%s", defaultpro); | |
239 | if (write(fd, &multiplier, to_write) != to_write) | |
240 | err(EXIT_FAILURE, _("error writing %s"), defaultpro); | |
c07ebfa1 | 241 | close(fd); |
c416814a | 242 | exit(EXIT_SUCCESS); |
c07ebfa1 KZ |
243 | } |
244 | ||
a3f0ea51 SK |
245 | /* Use an fd for the profiling buffer, to skip stdio overhead */ |
246 | if (((proFd = open(proFile, O_RDONLY)) < 0) | |
247 | || ((int)(len = lseek(proFd, 0, SEEK_END)) < 0) | |
248 | || (lseek(proFd, 0, SEEK_SET) < 0)) | |
c416814a | 249 | err(EXIT_FAILURE, "%s", proFile); |
c07ebfa1 | 250 | |
2ab428f6 | 251 | buf = xmalloc(len); |
c07ebfa1 | 252 | |
a3f0ea51 | 253 | rc = read(proFd, buf, len); |
c416814a | 254 | if (rc < 0 || (size_t) rc != len) |
a3f0ea51 | 255 | err(EXIT_FAILURE, "%s", proFile); |
c07ebfa1 KZ |
256 | close(proFd); |
257 | ||
612721db | 258 | if (!optNative) { |
a3f0ea51 SK |
259 | int entries = len / sizeof(*buf); |
260 | int big = 0, small = 0; | |
612721db | 261 | unsigned *p; |
33d18dc4 | 262 | size_t i; |
612721db | 263 | |
a3f0ea51 SK |
264 | for (p = buf + 1; p < buf + entries; p++) { |
265 | if (*p & ~0U << (sizeof(*buf) * 4)) | |
df1dddf9 | 266 | big++; |
a3f0ea51 | 267 | if (*p & ((1 << (sizeof(*buf) * 4)) - 1)) |
df1dddf9 KZ |
268 | small++; |
269 | } | |
612721db | 270 | if (big > small) { |
c416814a SK |
271 | warnx(_("Assuming reversed byte order. " |
272 | "Use -n to force native byte order.")); | |
a3f0ea51 SK |
273 | for (p = buf; p < buf + entries; p++) |
274 | for (i = 0; i < sizeof(*buf) / 2; i++) { | |
275 | unsigned char *b = (unsigned char *)p; | |
612721db | 276 | unsigned char tmp; |
612721db | 277 | tmp = b[i]; |
a3f0ea51 SK |
278 | b[i] = b[sizeof(*buf) - i - 1]; |
279 | b[sizeof(*buf) - i - 1] = tmp; | |
612721db KZ |
280 | } |
281 | } | |
282 | } | |
283 | ||
df1dddf9 | 284 | step = buf[0]; |
c07ebfa1 | 285 | if (optInfo) { |
e3ca1312 | 286 | printf(_("Sampling_step: %u\n"), step); |
c416814a | 287 | exit(EXIT_SUCCESS); |
a3f0ea51 | 288 | } |
c07ebfa1 | 289 | |
df1dddf9 | 290 | total = 0; |
c07ebfa1 | 291 | |
df1dddf9 KZ |
292 | map = myopen(mapFile, "r", &popenMap); |
293 | if (map == NULL && mapFile == defaultmap) { | |
294 | mapFile = boot_uname_r_str(); | |
295 | map = myopen(mapFile, "r", &popenMap); | |
296 | } | |
c416814a SK |
297 | if (map == NULL) |
298 | err(EXIT_FAILURE, "%s", mapFile); | |
c07ebfa1 | 299 | |
a3f0ea51 | 300 | while (fgets(mapline, S_LEN, map)) { |
01b63fcc | 301 | if (sscanf(mapline, "%llx %7[^\n ] %127[^\n ]", &fn_add, mode, fn_name) != 3) |
c416814a SK |
302 | errx(EXIT_FAILURE, _("%s(%i): wrong map line"), mapFile, |
303 | maplineno); | |
db94975b | 304 | /* only elf works like this */ |
a3f0ea51 | 305 | if (!strcmp(fn_name, "_stext") || !strcmp(fn_name, "__stext")) { |
1d4ad1de | 306 | add0 = fn_add; |
c07ebfa1 KZ |
307 | break; |
308 | } | |
1d4ad1de | 309 | maplineno++; |
c07ebfa1 KZ |
310 | } |
311 | ||
c416814a SK |
312 | if (!add0) |
313 | errx(EXIT_FAILURE, _("can't find \"_stext\" in %s"), mapFile); | |
c07ebfa1 KZ |
314 | |
315 | /* | |
316 | * Main loop. | |
317 | */ | |
a3f0ea51 SK |
318 | while (fgets(mapline, S_LEN, map)) { |
319 | unsigned int this = 0; | |
5a3009ca | 320 | int done = 0; |
c07ebfa1 | 321 | |
01b63fcc | 322 | if (sscanf(mapline, "%llx %7[^\n ] %127[^\n ]", &next_add, mode, next_name) != 3) |
c416814a SK |
323 | errx(EXIT_FAILURE, _("%s(%i): wrong map line"), mapFile, |
324 | maplineno); | |
f0c8eda1 | 325 | header_printed = 0; |
c07ebfa1 | 326 | |
5a3009ca MK |
327 | /* the kernel only profiles up to _etext */ |
328 | if (!strcmp(next_name, "_etext") || | |
329 | !strcmp(next_name, "__etext")) | |
330 | done = 1; | |
331 | else { | |
a3f0ea51 SK |
332 | /* ignore any LEADING (before a '[tT]' symbol |
333 | * is found) Absolute symbols and __init_end | |
334 | * because some architectures place it before | |
335 | * .text section */ | |
236faa7e PYC |
336 | if ((*mode == 'A' || *mode == '?') |
337 | && (total == 0 || !strcmp(next_name, "__init_end"))) | |
5a3009ca MK |
338 | continue; |
339 | if (*mode != 'T' && *mode != 't' && | |
340 | *mode != 'W' && *mode != 'w') | |
341 | break; /* only text is profiled */ | |
342 | } | |
c07ebfa1 | 343 | |
c416814a SK |
344 | if (indx >= len / sizeof(*buf)) |
345 | errx(EXIT_FAILURE, | |
346 | _("profile address out of range. Wrong map file?")); | |
364cda48 | 347 | |
a3f0ea51 | 348 | while (indx < (next_add - add0) / step) { |
f0c8eda1 KZ |
349 | if (optBins && (buf[indx] || optAll)) { |
350 | if (!header_printed) { | |
a3f0ea51 | 351 | printf("%s:\n", fn_name); |
f0c8eda1 KZ |
352 | header_printed = 1; |
353 | } | |
a3f0ea51 SK |
354 | printf("\t%llx\t%u\n", (indx - 1) * step + add0, |
355 | buf[indx]); | |
f0c8eda1 | 356 | } |
c07ebfa1 | 357 | this += buf[indx++]; |
f0c8eda1 | 358 | } |
c07ebfa1 KZ |
359 | total += this; |
360 | ||
f0c8eda1 KZ |
361 | if (optBins) { |
362 | if (optVerbose || this > 0) | |
a3f0ea51 | 363 | printf(" total\t\t\t\t%u\n", this); |
c129767e | 364 | } else if ((this || optAll) && |
a3f0ea51 | 365 | (fn_len = next_add - fn_add) != 0) { |
c129767e | 366 | if (optVerbose) |
e3ca1312 | 367 | printf("%016llx %-40s %6u %8.4f\n", fn_add, |
a3f0ea51 | 368 | fn_name, this, this / (double)fn_len); |
c129767e | 369 | else |
e3ca1312 | 370 | printf("%6u %-40s %8.4f\n", |
a3f0ea51 | 371 | this, fn_name, this / (double)fn_len); |
c129767e KZ |
372 | if (optSub) { |
373 | unsigned long long scan; | |
374 | ||
a3f0ea51 SK |
375 | for (scan = (fn_add - add0) / step + 1; |
376 | scan < (next_add - add0) / step; | |
377 | scan++) { | |
c129767e | 378 | unsigned long long addr; |
a3f0ea51 | 379 | addr = (scan - 1) * step + add0; |
c129767e KZ |
380 | printf("\t%#llx\t%s+%#llx\t%u\n", |
381 | addr, fn_name, addr - fn_add, | |
382 | buf[scan]); | |
383 | } | |
f0c8eda1 | 384 | } |
c07ebfa1 | 385 | } |
c129767e KZ |
386 | |
387 | fn_add = next_add; | |
a3f0ea51 | 388 | strcpy(fn_name, next_name); |
1d4ad1de KZ |
389 | |
390 | maplineno++; | |
5a3009ca MK |
391 | if (done) |
392 | break; | |
c07ebfa1 | 393 | } |
d162fcb5 KZ |
394 | |
395 | /* clock ticks, out of kernel text - probably modules */ | |
e3ca1312 | 396 | printf("%6u %s\n", buf[len / sizeof(*buf) - 1], "*unknown*"); |
d162fcb5 | 397 | |
c07ebfa1 KZ |
398 | /* trailer */ |
399 | if (optVerbose) | |
e3ca1312 | 400 | printf("%016x %-40s %6u %8.4f\n", |
a3f0ea51 | 401 | 0, "total", total, total / (double)(fn_add - add0)); |
c07ebfa1 | 402 | else |
e3ca1312 | 403 | printf("%6u %-40s %8.4f\n", |
a3f0ea51 SK |
404 | total, _("total"), total / (double)(fn_add - add0)); |
405 | ||
c07ebfa1 | 406 | popenMap ? pclose(map) : fclose(map); |
c416814a | 407 | exit(EXIT_SUCCESS); |
6dbe3af9 | 408 | } |