2 * readprofile.c - used to read /proc/profile
4 * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
23 * - added Native Language Support
24 * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
26 * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
27 * - -M option to write profile multiplier.
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)
32 * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
33 * - make maplineno do something
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
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"
49 #include <sys/types.h>
51 #include <sys/utsname.h>
58 /* These are the defaults */
59 static char defaultmap[]="/boot/System.map";
60 static char defaultpro[]="/proc/profile";
61 static char optstring[]="M:m:np:itvarVbs";
64 xmalloc (size_t size) {
72 fprintf(stderr, _("out of memory"));
80 myopen(char *name, char *mode, int *flag) {
81 int len = strlen(name);
83 if (!strcmp(name+len-3,".gz")) {
85 char *cmdline = xmalloc(len+6);
86 sprintf(cmdline, "zcat %s", name);
87 res = popen(cmdline,mode);
93 return fopen(name,mode);
96 #ifndef BOOT_SYSTEM_MAP
97 #define BOOT_SYSTEM_MAP "/boot/System.map-"
101 boot_uname_r_str(void) {
102 struct utsname uname_info;
106 if (uname(&uname_info))
108 len = strlen(BOOT_SYSTEM_MAP) + strlen(uname_info.release) + 1;
110 strcpy(s, BOOT_SYSTEM_MAP);
111 strcat(s, uname_info.release);
118 "%s: Usage: \"%s [options]\n"
119 "\t -m <mapfile> (defaults: \"%s\" and\n\t\t\t\t \"%s\")\n"
120 "\t -p <pro-file> (default: \"%s\")\n"
121 "\t -M <mult> set the profiling multiplier to <mult>\n"
122 "\t -i print only info about the sampling step\n"
123 "\t -v print verbose data\n"
124 "\t -a print all symbols, even if count is 0\n"
125 "\t -b print individual histogram-bin counts\n"
126 "\t -s print individual counters within functions\n"
127 "\t -r reset all the counters (root only)\n"
128 "\t -n disable byte order auto-detection\n"
129 "\t -V print version and exit\n"),
130 prgname, prgname, defaultmap, boot_uname_r_str(), defaultpro);
135 main(int argc, char **argv) {
138 char *mapFile, *proFile, *mult=0;
139 unsigned long len=0, indx=1;
140 unsigned long long add0=0;
142 unsigned int *buf, total, fn_len;
143 unsigned long long fn_add, next_add; /* current and next address */
144 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */
147 int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
148 int optBins=0, optSub=0;
151 int popenMap; /* flag to tell if popen() has been used */
154 #define next (current^1)
156 setlocale(LC_ALL, "");
157 bindtextdomain(PACKAGE, LOCALEDIR);
161 proFile = defaultpro;
162 mapFile = defaultmap;
164 while ((c = getopt(argc, argv, optstring)) != -1) {
197 printf(_("%s (%s)\n"), prgname,
205 if (optReset || mult) {
206 int multiplier, fd, to_write;
209 * When writing the multiplier, if the length of the write is
210 * not sizeof(int), the multiplier is not changed
213 multiplier = strtoul(mult, 0, 10);
214 to_write = sizeof(int);
217 to_write = 1; /* sth different from sizeof(int) */
219 /* try to become root, just in case */
221 fd = open(defaultpro,O_WRONLY);
226 if (write(fd, &multiplier, to_write) != to_write) {
227 fprintf(stderr, _("readprofile: error writing %s: %s\n"),
228 defaultpro, strerror(errno));
236 * Use an fd for the profiling buffer, to skip stdio overhead
238 if (((proFd=open(proFile,O_RDONLY)) < 0)
239 || ((int)(len=lseek(proFd,0,SEEK_END)) < 0)
240 || (lseek(proFd,0,SEEK_SET) < 0)) {
241 fprintf(stderr,"%s: %s: %s\n",prgname,proFile,strerror(errno));
245 if (!(buf=malloc(len))) {
246 fprintf(stderr,"%s: malloc(): %s\n", prgname, strerror(errno));
250 if (read(proFd,buf,len) != len) {
251 fprintf(stderr,"%s: %s: %s\n",prgname,proFile,strerror(errno));
257 int entries = len/sizeof(*buf);
258 int big = 0,small = 0,i;
261 for (p = buf+1; p < buf+entries; p++) {
262 if (*p & ~0U << (sizeof(*buf)*4))
264 if (*p & ((1 << (sizeof(*buf)*4))-1))
268 fprintf(stderr,_("Assuming reversed byte order. "
269 "Use -n to force native byte order.\n"));
270 for (p = buf; p < buf+entries; p++)
271 for (i = 0; i < sizeof(*buf)/2; i++) {
272 unsigned char *b = (unsigned char *) p;
276 b[i] = b[sizeof(*buf)-i-1];
277 b[sizeof(*buf)-i-1] = tmp;
284 printf(_("Sampling_step: %i\n"), step);
290 map = myopen(mapFile, "r", &popenMap);
291 if (map == NULL && mapFile == defaultmap) {
292 mapFile = boot_uname_r_str();
293 map = myopen(mapFile, "r", &popenMap);
297 fprintf(stderr, "%s: ", prgname);
303 while (fgets(mapline,S_LEN,map)) {
304 if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3) {
305 fprintf(stderr,_("%s: %s(%i): wrong map line\n"),
306 prgname, mapFile, maplineno);
309 /* only elf works like this */
310 if (!strcmp(fn_name,"_stext") || !strcmp(fn_name,"__stext")) {
318 fprintf(stderr,_("%s: can't find \"_stext\" in %s\n"),
326 while (fgets(mapline,S_LEN,map)) {
330 if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name)!=3) {
331 fprintf(stderr,_("%s: %s(%i): wrong map line\n"),
332 prgname,mapFile, maplineno);
337 /* the kernel only profiles up to _etext */
338 if (!strcmp(next_name, "_etext") ||
339 !strcmp(next_name, "__etext"))
342 /* ignore any LEADING (before a '[tT]' symbol is found)
344 if ((*mode == 'A' || *mode == '?') && total == 0)
346 if (*mode != 'T' && *mode != 't' &&
347 *mode != 'W' && *mode != 'w')
348 break; /* only text is profiled */
351 if (indx >= len / sizeof(*buf)) {
352 fprintf(stderr, _("%s: profile address out of range. "
353 "Wrong map file?\n"), prgname);
357 while (indx < (next_add-add0)/step) {
358 if (optBins && (buf[indx] || optAll)) {
359 if (!header_printed) {
360 printf ("%s:\n", fn_name);
363 printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
370 if (optVerbose || this > 0)
371 printf (" total\t\t\t\t%u\n", this);
372 } else if ((this || optAll) &&
373 (fn_len = next_add-fn_add) != 0) {
375 printf("%016llx %-40s %6i %8.4f\n", fn_add,
376 fn_name,this,this/(double)fn_len);
378 printf("%6i %-40s %8.4f\n",
379 this,fn_name,this/(double)fn_len);
381 unsigned long long scan;
383 for (scan = (fn_add-add0)/step + 1;
384 scan < (next_add-add0)/step; scan++) {
385 unsigned long long addr;
387 addr = (scan - 1)*step + add0;
388 printf("\t%#llx\t%s+%#llx\t%u\n",
389 addr, fn_name, addr - fn_add,
396 strcpy(fn_name,next_name);
403 /* clock ticks, out of kernel text - probably modules */
404 printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
408 printf("%016x %-40s %6i %8.4f\n",
409 0,"total",total,total/(double)(fn_add-add0));
411 printf("%6i %-40s %8.4f\n",
412 total,_("total"),total/(double)(fn_add-add0));
414 popenMap ? pclose(map) : fclose(map);