readprofile: getopt_ulflags'isation
[platform/upstream/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  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /*
11  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
12  * - added Native Language Support
13  * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com>
14  * - 64bit clean patch
15  * 3Feb2001 Andrew Morton <andrewm@uow.edu.au>
16  * - -M option to write profile multiplier.
17  * 2001-11-07 Werner Almesberger <wa@almesberger.net>
18  * - byte order auto-detection and -n option
19  * 2001-11-09 Werner Almesberger <wa@almesberger.net>
20  * - skip step size (index 0)
21  * 2002-03-09 John Levon <moz@compsoc.man.ac.uk>
22  * - make maplineno do something
23  * 2002-11-28 Mads Martin Joergensen +
24  * - also try /boot/System.map-`uname -r`
25  * 2003-04-09 Werner Almesberger <wa@almesberger.net>
26  * - fixed off-by eight error and improved heuristics in byte order detection
27  * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM>
28  * - added -s option; example of use:
29  * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3"
30  *
31  * Taken from util-linux and adapted for busybox by
32  * Paul Mundt <lethal@linux-sh.org>.
33  */
34
35 #include "busybox.h"
36 #include <sys/utsname.h>
37
38 #define S_LEN 128
39
40 /* These are the defaults */
41 static const char defaultmap[]="/boot/System.map";
42 static const char defaultpro[]="/proc/profile";
43
44 int readprofile_main(int argc, char **argv)
45 {
46         FILE *map;
47         int proFd;
48         const char *mapFile, *proFile, *mult=0;
49         unsigned long len=0, indx=1;
50         uint64_t add0=0;
51         unsigned int step;
52         unsigned int *buf, total, fn_len;
53         unsigned long long fn_add, next_add;     /* current and next address */
54         char fn_name[S_LEN], next_name[S_LEN];   /* current and next name */
55         char mode[8];
56         int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
57         int optBins=0, optSub=0;
58         char mapline[S_LEN];
59         int maplineno=1;
60         int header_printed;
61
62 #define next (current^1)
63
64         proFile = defaultpro;
65         mapFile = defaultmap;
66
67         bb_opt_complementally = "nn:aa:bb:ss:ii:rr:vv";
68         bb_getopt_ulflags(argc, argv, "M:m:p:nabsirv",
69                         &mult, &mapFile, &proFile,
70                         &optNative, &optAll, &optBins, &optSub,
71                         &optInfo, &optReset, &optVerbose);
72
73         if (optReset || mult) {
74                 int multiplier, fd, to_write;
75
76                 /*
77                  * When writing the multiplier, if the length of the write is
78                  * not sizeof(int), the multiplier is not changed
79                  */
80                 if (mult) {
81                         multiplier = strtoul(mult, 0, 10);
82                         to_write = sizeof(int);
83                 } else {
84                         multiplier = 0;
85                         to_write = 1;   /* sth different from sizeof(int) */
86                 }
87
88                 fd = xopen(defaultpro,O_WRONLY);
89
90                 if (write(fd, &multiplier, to_write) != to_write)
91                         bb_perror_msg_and_die("error writing %s", defaultpro);
92
93                 close(fd);
94                 return EXIT_SUCCESS;
95         }
96
97         /*
98          * Use an fd for the profiling buffer, to skip stdio overhead
99          */
100
101         proFd = xopen(proFile,O_RDONLY);
102
103         if (((int)(len=lseek(proFd,0,SEEK_END)) < 0)
104             || (lseek(proFd,0,SEEK_SET) < 0))
105                 bb_perror_msg_and_die(proFile);
106
107         buf = xmalloc(len);
108
109         if (read(proFd,buf,len) != len)
110                 bb_perror_msg_and_die(proFile);
111
112         close(proFd);
113
114         if (!optNative) {
115                 int entries = len/sizeof(*buf);
116                 int big = 0,small = 0,i;
117                 unsigned *p;
118
119                 for (p = buf+1; p < buf+entries; p++) {
120                         if (*p & ~0U << (sizeof(*buf)*4))
121                                 big++;
122                         if (*p & ((1 << (sizeof(*buf)*4))-1))
123                                 small++;
124                 }
125                 if (big > small) {
126                         bb_error_msg("Assuming reversed byte order. "
127                                 "Use -n to force native byte order.");
128                         for (p = buf; p < buf+entries; p++)
129                                 for (i = 0; i < sizeof(*buf)/2; i++) {
130                                         unsigned char *b = (unsigned char *) p;
131                                         unsigned char tmp;
132
133                                         tmp = b[i];
134                                         b[i] = b[sizeof(*buf)-i-1];
135                                         b[sizeof(*buf)-i-1] = tmp;
136                                 }
137                 }
138         }
139
140         step = buf[0];
141         if (optInfo) {
142                 printf("Sampling_step: %i\n", step);
143                 return EXIT_SUCCESS;
144         }
145
146         total = 0;
147
148         map = xfopen(mapFile, "r");
149
150         while (fgets(mapline,S_LEN,map)) {
151                 if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
152                         bb_error_msg_and_die("%s(%i): wrong map line",
153                                              mapFile, maplineno);
154
155                 if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
156                         add0 = fn_add;
157                         break;
158                 }
159                 maplineno++;
160         }
161
162         if (!add0)
163                 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
164
165         /*
166          * Main loop.
167          */
168         while (fgets(mapline,S_LEN,map)) {
169                 unsigned int this = 0;
170
171                 if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
172                         bb_error_msg_and_die("%s(%i): wrong map line",
173                                              mapFile, maplineno);
174
175                 header_printed = 0;
176
177                 /* ignore any LEADING (before a '[tT]' symbol is found)
178                    Absolute symbols */
179                 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
180                 if (*mode != 'T' && *mode != 't' &&
181                     *mode != 'W' && *mode != 'w')
182                         break;  /* only text is profiled */
183
184                 if (indx >= len / sizeof(*buf))
185                         bb_error_msg_and_die("profile address out of range. "
186                                              "Wrong map file?");
187
188                 while (indx < (next_add-add0)/step) {
189                         if (optBins && (buf[indx] || optAll)) {
190                                 if (!header_printed) {
191                                         printf ("%s:\n", fn_name);
192                                         header_printed = 1;
193                                 }
194                                 printf ("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
195                         }
196                         this += buf[indx++];
197                 }
198                 total += this;
199
200                 if (optBins) {
201                         if (optVerbose || this > 0)
202                                 printf ("  total\t\t\t\t%u\n", this);
203                 } else if ((this || optAll) &&
204                            (fn_len = next_add-fn_add) != 0) {
205                         if (optVerbose)
206                                 printf("%016llx %-40s %6i %8.4f\n", fn_add,
207                                        fn_name,this,this/(double)fn_len);
208                         else
209                                 printf("%6i %-40s %8.4f\n",
210                                        this,fn_name,this/(double)fn_len);
211                         if (optSub) {
212                                 unsigned long long scan;
213
214                                 for (scan = (fn_add-add0)/step + 1;
215                                      scan < (next_add-add0)/step; scan++) {
216                                         unsigned long long addr;
217
218                                         addr = (scan - 1)*step + add0;
219                                         printf("\t%#llx\t%s+%#llx\t%u\n",
220                                                addr, fn_name, addr - fn_add,
221                                                buf[scan]);
222                                 }
223                         }
224                 }
225
226                 fn_add = next_add;
227                 strcpy(fn_name,next_name);
228
229                 maplineno++;
230         }
231
232         /* clock ticks, out of kernel text - probably modules */
233         printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
234
235         /* trailer */
236         if (optVerbose)
237                 printf("%016x %-40s %6i %8.4f\n",
238                        0,"total",total,total/(double)(fn_add-add0));
239         else
240                 printf("%6i %-40s %8.4f\n",
241                        total,"total",total/(double)(fn_add-add0));
242
243         fclose(map);
244         free(buf);
245
246         return EXIT_SUCCESS;
247 }