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