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