]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/readprofile.c
Merge branch 'mbsencode' of https://github.com/yontalcar/util-linux
[thirdparty/util-linux.git] / sys-utils / readprofile.c
CommitLineData
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 61static char defaultmap[]="/boot/System.map";
6dbe3af9 62static char defaultpro[]="/proc/profile";
6dbe3af9 63
a3f0ea51
SK
64static 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
85static 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 100static 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
130int 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;
a3f0ea51
SK
139 unsigned long long fn_add, next_add; /* current and next address */
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);
efb8854f 172 atexit(close_stdout);
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;
209 case 'V':
443f7313
SK
210 printf(UTIL_LINUX_VERSION);
211 return EXIT_SUCCESS;
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);
c07ebfa1 249
2ab428f6 250 buf = xmalloc(len);
c07ebfa1 251
a3f0ea51 252 rc = read(proFd, buf, len);
c416814a 253 if (rc < 0 || (size_t) rc != len)
a3f0ea51 254 err(EXIT_FAILURE, "%s", proFile);
c07ebfa1
KZ
255 close(proFd);
256
612721db 257 if (!optNative) {
a3f0ea51
SK
258 int entries = len / sizeof(*buf);
259 int big = 0, small = 0;
612721db 260 unsigned *p;
33d18dc4 261 size_t i;
612721db 262
a3f0ea51
SK
263 for (p = buf + 1; p < buf + entries; p++) {
264 if (*p & ~0U << (sizeof(*buf) * 4))
df1dddf9 265 big++;
a3f0ea51 266 if (*p & ((1 << (sizeof(*buf) * 4)) - 1))
df1dddf9
KZ
267 small++;
268 }
612721db 269 if (big > small) {
c416814a
SK
270 warnx(_("Assuming reversed byte order. "
271 "Use -n to force native byte order."));
a3f0ea51
SK
272 for (p = buf; p < buf + entries; p++)
273 for (i = 0; i < sizeof(*buf) / 2; i++) {
274 unsigned char *b = (unsigned char *)p;
612721db 275 unsigned char tmp;
612721db 276 tmp = b[i];
a3f0ea51
SK
277 b[i] = b[sizeof(*buf) - i - 1];
278 b[sizeof(*buf) - i - 1] = tmp;
612721db
KZ
279 }
280 }
281 }
282
df1dddf9 283 step = buf[0];
c07ebfa1 284 if (optInfo) {
e3ca1312 285 printf(_("Sampling_step: %u\n"), step);
c416814a 286 exit(EXIT_SUCCESS);
a3f0ea51 287 }
c07ebfa1 288
df1dddf9 289 total = 0;
c07ebfa1 290
df1dddf9
KZ
291 map = myopen(mapFile, "r", &popenMap);
292 if (map == NULL && mapFile == defaultmap) {
293 mapFile = boot_uname_r_str();
294 map = myopen(mapFile, "r", &popenMap);
295 }
c416814a
SK
296 if (map == NULL)
297 err(EXIT_FAILURE, "%s", mapFile);
c07ebfa1 298
a3f0ea51 299 while (fgets(mapline, S_LEN, map)) {
01b63fcc 300 if (sscanf(mapline, "%llx %7[^\n ] %127[^\n ]", &fn_add, mode, fn_name) != 3)
c416814a
SK
301 errx(EXIT_FAILURE, _("%s(%i): wrong map line"), mapFile,
302 maplineno);
db94975b 303 /* only elf works like this */
a3f0ea51 304 if (!strcmp(fn_name, "_stext") || !strcmp(fn_name, "__stext")) {
1d4ad1de 305 add0 = fn_add;
c07ebfa1
KZ
306 break;
307 }
1d4ad1de 308 maplineno++;
c07ebfa1
KZ
309 }
310
c416814a
SK
311 if (!add0)
312 errx(EXIT_FAILURE, _("can't find \"_stext\" in %s"), mapFile);
c07ebfa1
KZ
313
314 /*
315 * Main loop.
316 */
a3f0ea51
SK
317 while (fgets(mapline, S_LEN, map)) {
318 unsigned int this = 0;
5a3009ca 319 int done = 0;
c07ebfa1 320
01b63fcc 321 if (sscanf(mapline, "%llx %7[^\n ] %127[^\n ]", &next_add, mode, next_name) != 3)
c416814a
SK
322 errx(EXIT_FAILURE, _("%s(%i): wrong map line"), mapFile,
323 maplineno);
f0c8eda1 324 header_printed = 0;
c07ebfa1 325
5a3009ca
MK
326 /* the kernel only profiles up to _etext */
327 if (!strcmp(next_name, "_etext") ||
328 !strcmp(next_name, "__etext"))
329 done = 1;
330 else {
a3f0ea51
SK
331 /* ignore any LEADING (before a '[tT]' symbol
332 * is found) Absolute symbols and __init_end
333 * because some architectures place it before
334 * .text section */
236faa7e
PYC
335 if ((*mode == 'A' || *mode == '?')
336 && (total == 0 || !strcmp(next_name, "__init_end")))
5a3009ca
MK
337 continue;
338 if (*mode != 'T' && *mode != 't' &&
339 *mode != 'W' && *mode != 'w')
340 break; /* only text is profiled */
341 }
c07ebfa1 342
c416814a
SK
343 if (indx >= len / sizeof(*buf))
344 errx(EXIT_FAILURE,
345 _("profile address out of range. Wrong map file?"));
364cda48 346
a3f0ea51 347 while (indx < (next_add - add0) / step) {
f0c8eda1
KZ
348 if (optBins && (buf[indx] || optAll)) {
349 if (!header_printed) {
a3f0ea51 350 printf("%s:\n", fn_name);
f0c8eda1
KZ
351 header_printed = 1;
352 }
a3f0ea51
SK
353 printf("\t%llx\t%u\n", (indx - 1) * step + add0,
354 buf[indx]);
f0c8eda1 355 }
c07ebfa1 356 this += buf[indx++];
f0c8eda1 357 }
c07ebfa1
KZ
358 total += this;
359
f0c8eda1
KZ
360 if (optBins) {
361 if (optVerbose || this > 0)
a3f0ea51 362 printf(" total\t\t\t\t%u\n", this);
c129767e 363 } else if ((this || optAll) &&
a3f0ea51 364 (fn_len = next_add - fn_add) != 0) {
c129767e 365 if (optVerbose)
e3ca1312 366 printf("%016llx %-40s %6u %8.4f\n", fn_add,
a3f0ea51 367 fn_name, this, this / (double)fn_len);
c129767e 368 else
e3ca1312 369 printf("%6u %-40s %8.4f\n",
a3f0ea51 370 this, fn_name, this / (double)fn_len);
c129767e
KZ
371 if (optSub) {
372 unsigned long long scan;
373
a3f0ea51
SK
374 for (scan = (fn_add - add0) / step + 1;
375 scan < (next_add - add0) / step;
376 scan++) {
c129767e 377 unsigned long long addr;
a3f0ea51 378 addr = (scan - 1) * step + add0;
c129767e
KZ
379 printf("\t%#llx\t%s+%#llx\t%u\n",
380 addr, fn_name, addr - fn_add,
381 buf[scan]);
382 }
f0c8eda1 383 }
c07ebfa1 384 }
c129767e
KZ
385
386 fn_add = next_add;
a3f0ea51 387 strcpy(fn_name, next_name);
1d4ad1de
KZ
388
389 maplineno++;
5a3009ca
MK
390 if (done)
391 break;
c07ebfa1 392 }
d162fcb5
KZ
393
394 /* clock ticks, out of kernel text - probably modules */
e3ca1312 395 printf("%6u %s\n", buf[len / sizeof(*buf) - 1], "*unknown*");
d162fcb5 396
c07ebfa1
KZ
397 /* trailer */
398 if (optVerbose)
e3ca1312 399 printf("%016x %-40s %6u %8.4f\n",
a3f0ea51 400 0, "total", total, total / (double)(fn_add - add0));
c07ebfa1 401 else
e3ca1312 402 printf("%6u %-40s %8.4f\n",
a3f0ea51
SK
403 total, _("total"), total / (double)(fn_add - add0));
404
c07ebfa1 405 popenMap ? pclose(map) : fclose(map);
c416814a 406 exit(EXIT_SUCCESS);
6dbe3af9 407}