Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / util-linux / readprofile.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  readprofile.c - used to read /proc/profile
4  *
5  *  Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it)
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 /*
23  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
24  * - added Native Language Support
25  * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
26  * - 64bit clean patch
27  * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
28  * - -M option to write profile multiplier.
29  * 2001-11-07 Werner Almesberger <wa@almesberger.net>
30  * - byte order auto-detection and -n option
31  * 2001-11-09 Werner Almesberger <wa@almesberger.net>
32  * - skip step size (index 0)
33  * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
34  * - make maplineno do something
35  * 2002-11-28 Mads Martin Joergensen +
36  * - also try /boot/System.map-`uname -r`
37  * 2003-04-09 Werner Almesberger <wa@almesberger.net>
38  * - fixed off-by eight error and improved heuristics in byte order detection
39  * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM>
40  * - added -s option; example of use:
41  * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
42  *
43  * Taken from util-linux and adapted for busybox by
44  * Paul Mundt <lethal@linux-sh.org>.
45  */
46
47 #include <errno.h>
48 #include <stdio.h>
49 #include <fcntl.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <sys/utsname.h>
56
57 #include "busybox.h"
58
59 #define S_LEN 128
60
61 /* These are the defaults */
62 static const char defaultmap[]="/boot/System.map";
63 static const char defaultpro[]="/proc/profile";
64
65 int readprofile_main(int argc, char **argv)
66 {
67         FILE *map;
68         int proFd;
69         const char *mapFile, *proFile, *mult=0;
70         unsigned long len=0, indx=1;
71         unsigned long long add0=0;
72         unsigned int step;
73         unsigned int *buf, total, fn_len;
74         unsigned long long fn_add, next_add;          /* current and next address */
75         char fn_name[S_LEN], next_name[S_LEN];   /* current and next name */
76         char mode[8];
77         int c;
78         int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
79         int optBins=0, optSub=0;
80         char mapline[S_LEN];
81         int maplineno=1;
82         int header_printed;
83
84 #define next (current^1)
85
86         proFile = defaultpro;
87         mapFile = defaultmap;
88
89         while ((c = getopt(argc, argv, "M:m:np:itvarVbs")) != -1) {
90                 switch(c) {
91                 case 'm':
92                         mapFile = optarg;
93                         break;
94                 case 'n':
95                         optNative++;
96                         break;
97                 case 'p':
98                         proFile = optarg;
99                         break;
100                 case 'a':
101                         optAll++;
102                         break;
103                 case 'b':
104                         optBins++;
105                         break;
106                 case 's':
107                         optSub++;
108                         break;
109                 case 'i':
110                         optInfo++;
111                         break;
112                 case 'M':
113                         mult = optarg;
114                         break;
115                 case 'r':
116                         optReset++;
117                         break;
118                 case 'v':
119                         optVerbose++;
120                         break;
121                 default:
122                         bb_show_usage();
123                 }
124         }
125
126         if (optReset || mult) {
127                 int multiplier, fd, to_write;
128
129                 /*
130                  * When writing the multiplier, if the length of the write is
131                  * not sizeof(int), the multiplier is not changed
132                  */
133                 if (mult) {
134                         multiplier = strtoul(mult, 0, 10);
135                         to_write = sizeof(int);
136                 } else {
137                         multiplier = 0;
138                         to_write = 1;   /* sth different from sizeof(int) */
139                 }
140
141                 fd = bb_xopen(defaultpro,O_WRONLY);
142
143                 if (write(fd, &multiplier, to_write) != to_write)
144                         bb_perror_msg_and_die("error writing %s", defaultpro);
145
146                 close(fd);
147                 return EXIT_SUCCESS;
148         }
149
150         /*
151          * Use an fd for the profiling buffer, to skip stdio overhead
152          */
153
154         proFd = bb_xopen(proFile,O_RDONLY);
155
156         if (((int)(len=lseek(proFd,0,SEEK_END)) < 0)
157             || (lseek(proFd,0,SEEK_SET) < 0))
158                 bb_perror_msg_and_die(proFile);
159
160         buf = xmalloc(len);
161
162         if (read(proFd,buf,len) != len)
163                 bb_perror_msg_and_die(proFile);
164
165         close(proFd);
166
167         if (!optNative) {
168                 int entries = len/sizeof(*buf);
169                 int big = 0,small = 0,i;
170                 unsigned *p;
171
172                 for (p = buf+1; p < buf+entries; p++) {
173                         if (*p & ~0U << (sizeof(*buf)*4))
174                                 big++;
175                         if (*p & ((1 << (sizeof(*buf)*4))-1))
176                                 small++;
177                 }
178                 if (big > small) {
179                         bb_error_msg("Assuming reversed byte order. "
180                                 "Use -n to force native byte order.");
181                         for (p = buf; p < buf+entries; p++)
182                                 for (i = 0; i < sizeof(*buf)/2; i++) {
183                                         unsigned char *b = (unsigned char *) p;
184                                         unsigned char tmp;
185
186                                         tmp = b[i];
187                                         b[i] = b[sizeof(*buf)-i-1];
188                                         b[sizeof(*buf)-i-1] = tmp;
189                                 }
190                 }
191         }
192
193         step = buf[0];
194         if (optInfo) {
195                 printf("Sampling_step: %i\n", step);
196                 return EXIT_SUCCESS;
197         }
198
199         total = 0;
200
201         map = bb_xfopen(mapFile, "r");
202
203         while (fgets(mapline,S_LEN,map)) {
204                 if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
205                         bb_error_msg_and_die("%s(%i): wrong map line",
206                                              mapFile, maplineno);
207
208                 if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
209                         add0 = fn_add;
210                         break;
211                 }
212                 maplineno++;
213         }
214
215         if (!add0)
216                 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
217
218         /*
219          * Main loop.
220          */
221         while (fgets(mapline,S_LEN,map)) {
222                 unsigned int this = 0;
223
224                 if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
225                         bb_error_msg_and_die("%s(%i): wrong map line",
226                                              mapFile, maplineno);
227
228                 header_printed = 0;
229
230                 /* ignore any LEADING (before a '[tT]' symbol is found)
231                    Absolute symbols */
232                 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
233                 if (*mode != 'T' && *mode != 't' &&
234                     *mode != 'W' && *mode != 'w')
235                         break;  /* only text is profiled */
236
237                 if (indx >= len / sizeof(*buf))
238                         bb_error_msg_and_die("profile address out of range. "
239                                              "Wrong map file?");
240
241                 while (indx < (next_add-add0)/step) {
242                         if (optBins && (buf[indx] || optAll)) {
243                                 if (!header_printed) {
244                                         printf ("%s:\n", fn_name);
245                                         header_printed = 1;
246                                 }
247                                 printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]);
248                         }
249                         this += buf[indx++];
250                 }
251                 total += this;
252
253                 if (optBins) {
254                         if (optVerbose || this > 0)
255                                 printf ("  total\t\t\t\t%u\n", this);
256                 } else if ((this || optAll) &&
257                            (fn_len = next_add-fn_add) != 0) {
258                         if (optVerbose)
259                                 printf("%016llx %-40s %6i %8.4f\n", fn_add,
260                                        fn_name,this,this/(double)fn_len);
261                         else
262                                 printf("%6i %-40s %8.4f\n",
263                                        this,fn_name,this/(double)fn_len);
264                         if (optSub) {
265                                 unsigned long long scan;
266
267                                 for (scan = (fn_add-add0)/step + 1;
268                                      scan < (next_add-add0)/step; scan++) {
269                                         unsigned long long addr;
270
271                                         addr = (scan - 1)*step + add0;
272                                         printf("\t%#llx\t%s+%#llx\t%u\n",
273                                                addr, fn_name, addr - fn_add,
274                                                buf[scan]);
275                                 }
276                         }
277                 }
278
279                 fn_add = next_add;
280                 strcpy(fn_name,next_name);
281
282                 maplineno++;
283         }
284
285         /* clock ticks, out of kernel text - probably modules */
286         printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
287
288         /* trailer */
289         if (optVerbose)
290                 printf("%016x %-40s %6i %8.4f\n",
291                        0,"total",total,total/(double)(fn_add-add0));
292         else
293                 printf("%6i %-40s %8.4f\n",
294                        total,"total",total/(double)(fn_add-add0));
295
296         fclose(map);
297         free(buf);
298
299         return EXIT_SUCCESS;
300 }