Convert all util-linux/* applets to "new style" applet definitions
[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  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 /*
11  * 1999-02-22 Arkadiusz Mickiewicz <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 //config:config READPROFILE
35 //config:       bool "readprofile"
36 //config:       default y
37 //config:       #select PLATFORM_LINUX
38 //config:       help
39 //config:         This allows you to parse /proc/profile for basic profiling.
40
41 //applet:IF_READPROFILE(APPLET(readprofile, BB_DIR_USR_SBIN, BB_SUID_DROP))
42
43 //kbuild:lib-$(CONFIG_READPROFILE) += readprofile.o
44
45 //usage:#define readprofile_trivial_usage
46 //usage:       "[OPTIONS]"
47 //usage:#define readprofile_full_usage "\n\n"
48 //usage:       "        -m mapfile      (Default: /boot/System.map)"
49 //usage:     "\n        -p profile      (Default: /proc/profile)"
50 //usage:     "\n        -M NUM          Set the profiling multiplier to NUM"
51 //usage:     "\n        -i              Print only info about the sampling step"
52 //usage:     "\n        -v              Verbose"
53 //usage:     "\n        -a              Print all symbols, even if count is 0"
54 //usage:     "\n        -b              Print individual histogram-bin counts"
55 //usage:     "\n        -s              Print individual counters within functions"
56 //usage:     "\n        -r              Reset all the counters (root only)"
57 //usage:     "\n        -n              Disable byte order auto-detection"
58
59 #include "libbb.h"
60 #include <sys/utsname.h>
61
62 #define S_LEN 128
63
64 /* These are the defaults */
65 static const char defaultmap[] ALIGN1 = "/boot/System.map";
66 static const char defaultpro[] ALIGN1 = "/proc/profile";
67
68 int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
69 int readprofile_main(int argc UNUSED_PARAM, char **argv)
70 {
71         FILE *map;
72         const char *mapFile, *proFile;
73         unsigned long indx = 1;
74         size_t len;
75         uint64_t add0 = 0;
76         unsigned int step;
77         unsigned int *buf, total, fn_len;
78         unsigned long long fn_add, next_add;     /* current and next address */
79         char fn_name[S_LEN], next_name[S_LEN];   /* current and next name */
80         char mapline[S_LEN];
81         char mode[8];
82         int maplineno = 1;
83         int header_printed;
84         int multiplier = 0;
85         unsigned opt;
86         enum {
87                 OPT_M = (1 << 0),
88                 OPT_m = (1 << 1),
89                 OPT_p = (1 << 2),
90                 OPT_n = (1 << 3),
91                 OPT_a = (1 << 4),
92                 OPT_b = (1 << 5),
93                 OPT_s = (1 << 6),
94                 OPT_i = (1 << 7),
95                 OPT_r = (1 << 8),
96                 OPT_v = (1 << 9),
97         };
98 #define optMult    (opt & OPT_M)
99 #define optNative  (opt & OPT_n)
100 #define optAll     (opt & OPT_a)
101 #define optBins    (opt & OPT_b)
102 #define optSub     (opt & OPT_s)
103 #define optInfo    (opt & OPT_i)
104 #define optReset   (opt & OPT_r)
105 #define optVerbose (opt & OPT_v)
106
107 #define next (current^1)
108
109         proFile = defaultpro;
110         mapFile = defaultmap;
111
112         opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
113
114         if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */
115                 int fd, to_write;
116
117                 /*
118                  * When writing the multiplier, if the length of the write is
119                  * not sizeof(int), the multiplier is not changed
120                  */
121                 to_write = sizeof(int);
122                 if (!optMult)
123                         to_write = 1;  /* sth different from sizeof(int) */
124
125                 fd = xopen(defaultpro, O_WRONLY);
126                 xwrite(fd, &multiplier, to_write);
127                 close(fd);
128                 return EXIT_SUCCESS;
129         }
130
131         /*
132          * Use an fd for the profiling buffer, to skip stdio overhead
133          */
134         len = MAXINT(ssize_t);
135         buf = xmalloc_xopen_read_close(proFile, &len);
136         if (!optNative) {
137                 int entries = len / sizeof(*buf);
138                 int big = 0, small = 0, i;
139                 unsigned *p;
140
141                 for (p = buf+1; p < buf+entries; p++) {
142                         if (*p & ~0U << (sizeof(*buf)*4))
143                                 big++;
144                         if (*p & ((1 << (sizeof(*buf)*4))-1))
145                                 small++;
146                 }
147                 if (big > small) {
148                         bb_error_msg("assuming reversed byte order, "
149                                 "use -n to force native byte order");
150                         for (p = buf; p < buf+entries; p++)
151                                 for (i = 0; i < sizeof(*buf)/2; i++) {
152                                         unsigned char *b = (unsigned char *) p;
153                                         unsigned char tmp;
154
155                                         tmp = b[i];
156                                         b[i] = b[sizeof(*buf)-i-1];
157                                         b[sizeof(*buf)-i-1] = tmp;
158                                 }
159                 }
160         }
161
162         step = buf[0];
163         if (optInfo) {
164                 printf("Sampling_step: %u\n", step);
165                 return EXIT_SUCCESS;
166         }
167
168         total = 0;
169
170         map = xfopen_for_read(mapFile);
171
172         while (fgets(mapline, S_LEN, map)) {
173                 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
174                         bb_error_msg_and_die("%s(%i): wrong map line",
175                                         mapFile, maplineno);
176
177                 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {
178                         add0 = fn_add;
179                         break;
180                 }
181                 maplineno++;
182         }
183
184         if (!add0)
185                 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
186
187         /*
188          * Main loop.
189          */
190         while (fgets(mapline, S_LEN, map)) {
191                 unsigned int this = 0;
192
193                 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
194                         bb_error_msg_and_die("%s(%i): wrong map line",
195                                         mapFile, maplineno);
196
197                 header_printed = 0;
198
199                 /* ignore any LEADING (before a '[tT]' symbol is found)
200                    Absolute symbols */
201                 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
202                 if (*mode != 'T' && *mode != 't'
203                  && *mode != 'W' && *mode != 'w'
204                 ) {
205                         break;  /* only text is profiled */
206                 }
207
208                 if (indx >= len / sizeof(*buf))
209                         bb_error_msg_and_die("profile address out of range. "
210                                         "Wrong map file?");
211
212                 while (indx < (next_add-add0)/step) {
213                         if (optBins && (buf[indx] || optAll)) {
214                                 if (!header_printed) {
215                                         printf("%s:\n", fn_name);
216                                         header_printed = 1;
217                                 }
218                                 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
219                         }
220                         this += buf[indx++];
221                 }
222                 total += this;
223
224                 if (optBins) {
225                         if (optVerbose || this > 0)
226                                 printf("  total\t\t\t\t%u\n", this);
227                 } else if ((this || optAll)
228                         && (fn_len = next_add-fn_add) != 0
229                 ) {
230                         if (optVerbose)
231                                 printf("%016llx %-40s %6u %8.4f\n", fn_add,
232                                         fn_name, this, this/(double)fn_len);
233                         else
234                                 printf("%6u %-40s %8.4f\n",
235                                         this, fn_name, this/(double)fn_len);
236                         if (optSub) {
237                                 unsigned long long scan;
238
239                                 for (scan = (fn_add-add0)/step + 1;
240                                      scan < (next_add-add0)/step; scan++) {
241                                         unsigned long long addr;
242
243                                         addr = (scan - 1)*step + add0;
244                                         printf("\t%#llx\t%s+%#llx\t%u\n",
245                                                 addr, fn_name, addr - fn_add,
246                                                 buf[scan]);
247                                 }
248                         }
249                 }
250
251                 fn_add = next_add;
252                 strcpy(fn_name, next_name);
253
254                 maplineno++;
255         }
256
257         /* clock ticks, out of kernel text - probably modules */
258         printf("%6u %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
259
260         /* trailer */
261         if (optVerbose)
262                 printf("%016x %-40s %6u %8.4f\n",
263                         0, "total", total, total/(double)(fn_add-add0));
264         else
265                 printf("%6u %-40s %8.4f\n",
266                         total, "total", total/(double)(fn_add-add0));
267
268         fclose(map);
269         free(buf);
270
271         return EXIT_SUCCESS;
272 }