]>
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); | |
f45f3ec3 RM |
125 | printf(USAGE_HELP_OPTIONS(27)); |
126 | printf(USAGE_MAN_TAIL("readprofile(8)")); | |
86be6a32 | 127 | exit(EXIT_SUCCESS); |
df1dddf9 KZ |
128 | } |
129 | ||
a3f0ea51 SK |
130 | int main(int argc, char **argv) |
131 | { | |
c07ebfa1 KZ |
132 | FILE *map; |
133 | int proFd; | |
87918040 | 134 | char *mapFile, *proFile, *mult = NULL; |
a3f0ea51 SK |
135 | size_t len = 0, indx = 1; |
136 | unsigned long long add0 = 0; | |
c07ebfa1 KZ |
137 | unsigned int step; |
138 | unsigned int *buf, total, fn_len; | |
4813a521 | 139 | unsigned long long fn_add = 0, next_add; /* current and next address */ |
a3f0ea51 | 140 | char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ |
c07ebfa1 KZ |
141 | char mode[8]; |
142 | int c; | |
33d18dc4 | 143 | ssize_t rc; |
a3f0ea51 SK |
144 | int optAll = 0, optInfo = 0, optReset = 0, optVerbose = 0, optNative = 0; |
145 | int optBins = 0, optSub = 0; | |
c07ebfa1 | 146 | char mapline[S_LEN]; |
a3f0ea51 SK |
147 | int maplineno = 1; |
148 | int popenMap; /* flag to tell if popen() has been used */ | |
f0c8eda1 | 149 | int header_printed; |
6dbe3af9 | 150 | |
443f7313 SK |
151 | static const struct option longopts[] = { |
152 | {"mapfile", required_argument, NULL, 'm'}, | |
153 | {"profile", required_argument, NULL, 'p'}, | |
154 | {"multiplier", required_argument, NULL, 'M'}, | |
155 | {"info", no_argument, NULL, 'i'}, | |
156 | {"verbose", no_argument, NULL, 'v'}, | |
157 | {"all", no_argument, NULL, 'a'}, | |
158 | {"histbin", no_argument, NULL, 'b'}, | |
159 | {"counters", no_argument, NULL, 's'}, | |
9b80bef6 | 160 | {"reset", no_argument, NULL, 'r'}, |
443f7313 SK |
161 | {"no-auto", no_argument, NULL, 'n'}, |
162 | {"version", no_argument, NULL, 'V'}, | |
163 | {"help", no_argument, NULL, 'h'}, | |
87918040 | 164 | {NULL, 0, NULL, 0} |
443f7313 SK |
165 | }; |
166 | ||
6dbe3af9 KZ |
167 | #define next (current^1) |
168 | ||
c07ebfa1 KZ |
169 | setlocale(LC_ALL, ""); |
170 | bindtextdomain(PACKAGE, LOCALEDIR); | |
171 | textdomain(PACKAGE); | |
2c308875 | 172 | close_stdout_atexit(); |
c07ebfa1 | 173 | |
df1dddf9 KZ |
174 | proFile = defaultpro; |
175 | mapFile = defaultmap; | |
c07ebfa1 | 176 | |
cda9acbe | 177 | while ((c = getopt_long(argc, argv, "m:p:M:ivabsrnVh", longopts, NULL)) != -1) { |
a3f0ea51 | 178 | switch (c) { |
df1dddf9 KZ |
179 | case 'm': |
180 | mapFile = optarg; | |
181 | break; | |
182 | case 'n': | |
183 | optNative++; | |
184 | break; | |
185 | case 'p': | |
186 | proFile = optarg; | |
187 | break; | |
188 | case 'a': | |
189 | optAll++; | |
190 | break; | |
191 | case 'b': | |
192 | optBins++; | |
193 | break; | |
c129767e KZ |
194 | case 's': |
195 | optSub++; | |
196 | break; | |
df1dddf9 KZ |
197 | case 'i': |
198 | optInfo++; | |
199 | break; | |
200 | case 'M': | |
201 | mult = optarg; | |
202 | break; | |
203 | case 'r': | |
204 | optReset++; | |
205 | break; | |
206 | case 'v': | |
207 | optVerbose++; | |
208 | break; | |
2c308875 | 209 | |
df1dddf9 | 210 | case 'V': |
2c308875 | 211 | print_version(EXIT_SUCCESS); |
443f7313 | 212 | case 'h': |
86be6a32 | 213 | usage(); |
df1dddf9 | 214 | default: |
677ec86c | 215 | errtryhelp(EXIT_FAILURE); |
c07ebfa1 KZ |
216 | } |
217 | } | |
218 | ||
219 | if (optReset || mult) { | |
220 | int multiplier, fd, to_write; | |
221 | ||
a3f0ea51 SK |
222 | /* When writing the multiplier, if the length of the |
223 | * write is not sizeof(int), the multiplier is not | |
224 | * changed. */ | |
c07ebfa1 | 225 | if (mult) { |
87918040 | 226 | multiplier = strtoul(mult, NULL, 10); |
c07ebfa1 KZ |
227 | to_write = sizeof(int); |
228 | } else { | |
229 | multiplier = 0; | |
a3f0ea51 SK |
230 | /* sth different from sizeof(int) */ |
231 | to_write = 1; | |
c07ebfa1 KZ |
232 | } |
233 | /* try to become root, just in case */ | |
8f3b568c | 234 | ignore_result( setuid(0) ); |
a3f0ea51 | 235 | fd = open(defaultpro, O_WRONLY); |
c416814a SK |
236 | if (fd < 0) |
237 | err(EXIT_FAILURE, "%s", defaultpro); | |
238 | if (write(fd, &multiplier, to_write) != to_write) | |
239 | err(EXIT_FAILURE, _("error writing %s"), defaultpro); | |
c07ebfa1 | 240 | close(fd); |
c416814a | 241 | exit(EXIT_SUCCESS); |
c07ebfa1 KZ |
242 | } |
243 | ||
a3f0ea51 SK |
244 | /* Use an fd for the profiling buffer, to skip stdio overhead */ |
245 | if (((proFd = open(proFile, O_RDONLY)) < 0) | |
246 | || ((int)(len = lseek(proFd, 0, SEEK_END)) < 0) | |
247 | || (lseek(proFd, 0, SEEK_SET) < 0)) | |
c416814a | 248 | err(EXIT_FAILURE, "%s", proFile); |
cae9283a SK |
249 | if (!len) |
250 | errx(EXIT_FAILURE, "%s: %s", proFile, _("input file is empty")); | |
c07ebfa1 | 251 | |
2ab428f6 | 252 | buf = xmalloc(len); |
c07ebfa1 | 253 | |
a3f0ea51 | 254 | rc = read(proFd, buf, len); |
c416814a | 255 | if (rc < 0 || (size_t) rc != len) |
a3f0ea51 | 256 | err(EXIT_FAILURE, "%s", proFile); |
c07ebfa1 KZ |
257 | close(proFd); |
258 | ||
612721db | 259 | if (!optNative) { |
a3f0ea51 SK |
260 | int entries = len / sizeof(*buf); |
261 | int big = 0, small = 0; | |
612721db | 262 | unsigned *p; |
33d18dc4 | 263 | size_t i; |
612721db | 264 | |
a3f0ea51 | 265 | for (p = buf + 1; p < buf + entries; p++) { |
517debc3 | 266 | if (*p & ~0U << ((unsigned) sizeof(*buf) * 4U)) |
df1dddf9 | 267 | big++; |
517debc3 | 268 | if (*p & ((1U << ((unsigned) sizeof(*buf) * 4U)) - 1U)) |
df1dddf9 KZ |
269 | small++; |
270 | } | |
612721db | 271 | if (big > small) { |
c416814a SK |
272 | warnx(_("Assuming reversed byte order. " |
273 | "Use -n to force native byte order.")); | |
a3f0ea51 SK |
274 | for (p = buf; p < buf + entries; p++) |
275 | for (i = 0; i < sizeof(*buf) / 2; i++) { | |
276 | unsigned char *b = (unsigned char *)p; | |
612721db | 277 | unsigned char tmp; |
612721db | 278 | tmp = b[i]; |
a3f0ea51 SK |
279 | b[i] = b[sizeof(*buf) - i - 1]; |
280 | b[sizeof(*buf) - i - 1] = tmp; | |
612721db KZ |
281 | } |
282 | } | |
283 | } | |
284 | ||
df1dddf9 | 285 | step = buf[0]; |
c07ebfa1 | 286 | if (optInfo) { |
e3ca1312 | 287 | printf(_("Sampling_step: %u\n"), step); |
c416814a | 288 | exit(EXIT_SUCCESS); |
a3f0ea51 | 289 | } |
c07ebfa1 | 290 | |
df1dddf9 | 291 | total = 0; |
c07ebfa1 | 292 | |
df1dddf9 KZ |
293 | map = myopen(mapFile, "r", &popenMap); |
294 | if (map == NULL && mapFile == defaultmap) { | |
295 | mapFile = boot_uname_r_str(); | |
296 | map = myopen(mapFile, "r", &popenMap); | |
297 | } | |
c416814a SK |
298 | if (map == NULL) |
299 | err(EXIT_FAILURE, "%s", mapFile); | |
c07ebfa1 | 300 | |
a3f0ea51 | 301 | while (fgets(mapline, S_LEN, map)) { |
01b63fcc | 302 | if (sscanf(mapline, "%llx %7[^\n ] %127[^\n ]", &fn_add, mode, fn_name) != 3) |
c416814a SK |
303 | errx(EXIT_FAILURE, _("%s(%i): wrong map line"), mapFile, |
304 | maplineno); | |
db94975b | 305 | /* only elf works like this */ |
a3f0ea51 | 306 | if (!strcmp(fn_name, "_stext") || !strcmp(fn_name, "__stext")) { |
1d4ad1de | 307 | add0 = fn_add; |
c07ebfa1 KZ |
308 | break; |
309 | } | |
1d4ad1de | 310 | maplineno++; |
c07ebfa1 KZ |
311 | } |
312 | ||
c416814a SK |
313 | if (!add0) |
314 | errx(EXIT_FAILURE, _("can't find \"_stext\" in %s"), mapFile); | |
c07ebfa1 KZ |
315 | |
316 | /* | |
317 | * Main loop. | |
318 | */ | |
a3f0ea51 SK |
319 | while (fgets(mapline, S_LEN, map)) { |
320 | unsigned int this = 0; | |
5a3009ca | 321 | int done = 0; |
c07ebfa1 | 322 | |
01b63fcc | 323 | if (sscanf(mapline, "%llx %7[^\n ] %127[^\n ]", &next_add, mode, next_name) != 3) |
c416814a SK |
324 | errx(EXIT_FAILURE, _("%s(%i): wrong map line"), mapFile, |
325 | maplineno); | |
f0c8eda1 | 326 | header_printed = 0; |
c07ebfa1 | 327 | |
5a3009ca MK |
328 | /* the kernel only profiles up to _etext */ |
329 | if (!strcmp(next_name, "_etext") || | |
330 | !strcmp(next_name, "__etext")) | |
331 | done = 1; | |
332 | else { | |
a3f0ea51 SK |
333 | /* ignore any LEADING (before a '[tT]' symbol |
334 | * is found) Absolute symbols and __init_end | |
335 | * because some architectures place it before | |
336 | * .text section */ | |
236faa7e PYC |
337 | if ((*mode == 'A' || *mode == '?') |
338 | && (total == 0 || !strcmp(next_name, "__init_end"))) | |
5a3009ca MK |
339 | continue; |
340 | if (*mode != 'T' && *mode != 't' && | |
341 | *mode != 'W' && *mode != 'w') | |
342 | break; /* only text is profiled */ | |
343 | } | |
c07ebfa1 | 344 | |
c416814a SK |
345 | if (indx >= len / sizeof(*buf)) |
346 | errx(EXIT_FAILURE, | |
347 | _("profile address out of range. Wrong map file?")); | |
364cda48 | 348 | |
a3f0ea51 | 349 | while (indx < (next_add - add0) / step) { |
f0c8eda1 KZ |
350 | if (optBins && (buf[indx] || optAll)) { |
351 | if (!header_printed) { | |
a3f0ea51 | 352 | printf("%s:\n", fn_name); |
f0c8eda1 KZ |
353 | header_printed = 1; |
354 | } | |
a3f0ea51 SK |
355 | printf("\t%llx\t%u\n", (indx - 1) * step + add0, |
356 | buf[indx]); | |
f0c8eda1 | 357 | } |
c07ebfa1 | 358 | this += buf[indx++]; |
f0c8eda1 | 359 | } |
c07ebfa1 KZ |
360 | total += this; |
361 | ||
f0c8eda1 KZ |
362 | if (optBins) { |
363 | if (optVerbose || this > 0) | |
a3f0ea51 | 364 | printf(" total\t\t\t\t%u\n", this); |
c129767e | 365 | } else if ((this || optAll) && |
a3f0ea51 | 366 | (fn_len = next_add - fn_add) != 0) { |
c129767e | 367 | if (optVerbose) |
e3ca1312 | 368 | printf("%016llx %-40s %6u %8.4f\n", fn_add, |
a3f0ea51 | 369 | fn_name, this, this / (double)fn_len); |
c129767e | 370 | else |
e3ca1312 | 371 | printf("%6u %-40s %8.4f\n", |
a3f0ea51 | 372 | this, fn_name, this / (double)fn_len); |
c129767e KZ |
373 | if (optSub) { |
374 | unsigned long long scan; | |
375 | ||
a3f0ea51 SK |
376 | for (scan = (fn_add - add0) / step + 1; |
377 | scan < (next_add - add0) / step; | |
378 | scan++) { | |
c129767e | 379 | unsigned long long addr; |
a3f0ea51 | 380 | addr = (scan - 1) * step + add0; |
c129767e KZ |
381 | printf("\t%#llx\t%s+%#llx\t%u\n", |
382 | addr, fn_name, addr - fn_add, | |
383 | buf[scan]); | |
384 | } | |
f0c8eda1 | 385 | } |
c07ebfa1 | 386 | } |
c129767e KZ |
387 | |
388 | fn_add = next_add; | |
a3f0ea51 | 389 | strcpy(fn_name, next_name); |
1d4ad1de KZ |
390 | |
391 | maplineno++; | |
5a3009ca MK |
392 | if (done) |
393 | break; | |
c07ebfa1 | 394 | } |
d162fcb5 KZ |
395 | |
396 | /* clock ticks, out of kernel text - probably modules */ | |
e3ca1312 | 397 | printf("%6u %s\n", buf[len / sizeof(*buf) - 1], "*unknown*"); |
d162fcb5 | 398 | |
c07ebfa1 KZ |
399 | /* trailer */ |
400 | if (optVerbose) | |
e3ca1312 | 401 | printf("%016x %-40s %6u %8.4f\n", |
a3f0ea51 | 402 | 0, "total", total, total / (double)(fn_add - add0)); |
c07ebfa1 | 403 | else |
e3ca1312 | 404 | printf("%6u %-40s %8.4f\n", |
a3f0ea51 SK |
405 | total, _("total"), total / (double)(fn_add - add0)); |
406 | ||
c07ebfa1 | 407 | popenMap ? pclose(map) : fclose(map); |
c416814a | 408 | exit(EXIT_SUCCESS); |
6dbe3af9 | 409 | } |