]>
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
24 * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
33 #include <sys/types.h>
38 #define RELEASE "2.0, May 1996"
44 /* These are the defaults */
45 static char defaultmap
[]="/usr/src/linux/System.map";
46 static char defaultpro
[]="/proc/profile";
47 static char optstring
[]="m:p:itvarV";
52 _("%s: Usage: \"%s [options]\n"
53 "\t -m <mapfile> (default = \"%s\")\n"
54 "\t -p <pro-file> (default = \"%s\")\n"
55 "\t -i print only info about the sampling step\n"
56 "\t -v print verbose data\n"
57 "\t -a print all symbols, even if count is 0\n"
58 "\t -r reset all the counters (root only)\n"
59 "\t -V print version and exit\n")
60 ,prgname
,prgname
,defaultmap
,defaultpro
);
65 myopen(char *name
, char *mode
, int *flag
) {
66 static char cmdline
[S_LEN
];
68 if (!strcmp(name
+strlen(name
)-3,".gz"))
71 sprintf(cmdline
,"zcat %s", name
);
72 return popen(cmdline
,mode
);
75 return fopen(name
,mode
);
79 main (int argc
, char **argv
) {
83 char *mapFile
, *proFile
;
84 unsigned long len
=0, add0
=0, indx
=0;
86 unsigned int *buf
, total
, fn_len
;
87 unsigned long fn_add
, next_add
; /* current and next address */
88 char fn_name
[S_LEN
], next_name
[S_LEN
]; /* current and next name */
91 int optAll
=0, optInfo
=0, optReset
=0, optVerbose
=0;
94 int popenMap
; /* flag to tell if popen() has been used */
96 #define next (current^1)
98 setlocale(LC_ALL
, "");
99 bindtextdomain(PACKAGE
, LOCALEDIR
);
106 while ((c
=getopt(argc
,argv
,optstring
))!=-1)
110 case 'm': mapFile
=optarg
; break;
111 case 'p': proFile
=optarg
; break;
112 case 'a': optAll
++; break;
113 case 'i': optInfo
++; break;
114 case 'r': optReset
++; break;
115 case 'v': optVerbose
++; break;
116 case 'V': printf(_("%s Version %s\n"),prgname
,RELEASE
); exit(0);
123 /* try to become root, just in case */
125 pro
=fopen(defaultpro
,"w");
127 {perror(proFile
); exit(1);}
128 fprintf(pro
,_("anything\n"));
134 * Use an fd for the profiling buffer, to skip stdio overhead
136 if ( ((proFd
=open(proFile
,O_RDONLY
)) < 0)
137 || ((int)(len
=lseek(proFd
,0,SEEK_END
)) < 0)
138 || (lseek(proFd
,0,SEEK_SET
)<0) )
140 fprintf(stderr
,"%s: %s: %s\n",prgname
,proFile
,strerror(errno
));
144 if ( !(buf
=malloc(len
)) )
145 { fprintf(stderr
,"%s: malloc(): %s\n",prgname
, strerror(errno
)); exit(1); }
147 if (read(proFd
,buf
,len
) != len
)
149 fprintf(stderr
,"%s: %s: %s\n",prgname
,proFile
,strerror(errno
));
157 printf(_("Sampling_step: %i\n"),step
);
163 if (!(map
=myopen(mapFile
,"r",&popenMap
)))
164 {fprintf(stderr
,"%s: ",prgname
);perror(mapFile
);exit(1);}
166 while(fgets(mapline
,S_LEN
,map
))
168 if (sscanf(mapline
,"%lx %s %s",&fn_add
,mode
,fn_name
)!=3)
170 fprintf(stderr
,_("%s: %s(%i): wrong map line\n"),
171 prgname
,mapFile
, maplineno
);
174 if (!strcmp(fn_name
,"_stext")) /* only elf works like this */
183 fprintf(stderr
,_("%s: can't find \"_stext\" in %s\n"),prgname
, mapFile
);
190 while(fgets(mapline
,S_LEN
,map
))
194 if (sscanf(mapline
,"%lx %s %s",&next_add
,mode
,next_name
)!=3)
196 fprintf(stderr
,_("%s: %s(%i): wrong map line\n"),
197 prgname
,mapFile
, maplineno
);
200 /* ignore any LEADING (before a '[tT]' symbol is found) Absolute symbols */
201 if (*mode
== 'A' && total
== 0) continue;
202 if (*mode
!='T' && *mode
!='t') break; /* only text is profiled */
204 while (indx
< (next_add
-add0
)/step
)
208 fn_len
= next_add
-fn_add
;
209 if (fn_len
&& (this || optAll
))
212 printf("%08lx %-40s %6i %8.4f\n",
213 fn_add
,fn_name
,this,this/(double)fn_len
);
215 printf("%6i %-40s %8.4f\n",
216 this,fn_name
,this/(double)fn_len
);
218 fn_add
=next_add
; strcpy(fn_name
,next_name
);
222 printf("%08x %-40s %6i %8.4f\n",
223 0,"total",total
,total
/(double)(fn_add
-add0
));
225 printf("%6i %-40s %8.4f\n",
226 total
,_("total"),total
/(double)(fn_add
-add0
));
228 popenMap
? pclose(map
) : fclose(map
);