]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/readprofile.c
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@misiek.eu.org>
23 * - added Native Language Support
31 #include <sys/types.h>
36 #define RELEASE "2.0, May 1996"
42 /* These are the defaults */
43 static char defaultmap
[]="/usr/src/linux/System.map";
44 static char defaultpro
[]="/proc/profile";
45 static char optstring
[]="m:p:itvarV";
50 _("%s: Usage: \"%s [options]\n"
51 "\t -m <mapfile> (default = \"%s\")\n"
52 "\t -p <pro-file> (default = \"%s\")\n"
53 "\t -i print only info about the sampling step\n"
54 "\t -v print verbose data\n"
55 "\t -a print all symbols, even if count is 0\n"
56 "\t -r reset all the counters (root only)\n"
57 "\t -V print version and exit\n")
58 ,prgname
,prgname
,defaultmap
,defaultpro
);
62 FILE *myopen(char *name
, char *mode
, int *flag
)
64 static char cmdline
[S_LEN
];
66 if (!strcmp(name
+strlen(name
)-3,".gz"))
69 sprintf(cmdline
,"zcat %s", name
);
70 return popen(cmdline
,mode
);
73 return fopen(name
,mode
);
76 int main (int argc
, char **argv
)
81 char *mapFile
, *proFile
;
82 unsigned int len
=0, add0
=0, step
, index
=0;
83 unsigned int *buf
, total
, fn_len
;
84 unsigned int fn_add
, next_add
; /* current and next address */
85 char fn_name
[S_LEN
], next_name
[S_LEN
]; /* current and next name */
88 int optAll
=0, optInfo
=0, optReset
=0, optVerbose
=0;
91 int popenMap
; /* flag to tell if popen() has been used */
93 #define next (current^1)
95 setlocale(LC_ALL
, "");
96 bindtextdomain(PACKAGE
, LOCALEDIR
);
103 while ((c
=getopt(argc
,argv
,optstring
))!=-1)
107 case 'm': mapFile
=optarg
; break;
108 case 'p': proFile
=optarg
; break;
109 case 'a': optAll
++; break;
110 case 'i': optInfo
++; break;
111 case 'r': optReset
++; break;
112 case 'v': optVerbose
++; break;
113 case 'V': printf(_("%s Version %s\n"),prgname
,RELEASE
); exit(0);
120 /* try to become root, just in case */
122 pro
=fopen(defaultpro
,"w");
124 {perror(proFile
); exit(1);}
125 fprintf(pro
,_("anything\n"));
131 * Use an fd for the profiling buffer, to skip stdio overhead
133 if ( ((proFd
=open(proFile
,O_RDONLY
)) < 0)
134 || ((int)(len
=lseek(proFd
,0,SEEK_END
)) < 0)
135 || (lseek(proFd
,0,SEEK_SET
)<0) )
137 fprintf(stderr
,"%s: %s: %s\n",prgname
,proFile
,strerror(errno
));
141 if ( !(buf
=malloc(len
)) )
142 { fprintf(stderr
,"%s: malloc(): %s\n",prgname
, strerror(errno
)); exit(1); }
144 if (read(proFd
,buf
,len
) != len
)
146 fprintf(stderr
,"%s: %s: %s\n",prgname
,proFile
,strerror(errno
));
154 printf(_("Sampling_step: %i\n"),step
);
160 if (!(map
=myopen(mapFile
,"r",&popenMap
)))
161 {fprintf(stderr
,"%s: ",prgname
);perror(mapFile
);exit(1);}
163 while(fgets(mapline
,S_LEN
,map
))
165 if (sscanf(mapline
,"%x %s %s",&fn_add
,mode
,fn_name
)!=3)
167 fprintf(stderr
,_("%s: %s(%i): wrong map line\n"),
168 prgname
,mapFile
, maplineno
);
171 if (strcmp(fn_name
,"_stext")) /* only elf works like this */
180 fprintf(stderr
,_("%s: can't find \"_stext\" in %s\n"),prgname
, mapFile
);
187 while(fgets(mapline
,S_LEN
,map
))
191 if (sscanf(mapline
,"%x %s %s",&next_add
,mode
,next_name
)!=3)
193 fprintf(stderr
,_("%s: %s(%i): wrong map line\n"),
194 prgname
,mapFile
, maplineno
);
197 if (*mode
!='T' && *mode
!='t') break; /* only text is profiled */
199 while (index
< (next_add
-add0
)/step
)
200 this += buf
[index
++];
203 fn_len
= next_add
-fn_add
;
204 if (fn_len
&& (this || optAll
))
207 printf("%08x %-40s %6i %8.4f\n",
208 fn_add
,fn_name
,this,this/(double)fn_len
);
210 printf("%6i %-40s %8.4f\n",
211 this,fn_name
,this/(double)fn_len
);
213 fn_add
=next_add
; strcpy(fn_name
,next_name
);
217 printf("%08x %-40s %6i %8.4f\n",
218 0,"total",total
,total
/(double)(fn_add
-add0
));
220 printf("%6i %-40s %8.4f\n",
221 total
,_("total"),total
/(double)(fn_add
-add0
));
223 popenMap
? pclose(map
) : fclose(map
);