mount: fix bugs: free(mp->mnt_fsname) of non-malloced ptr;
[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 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 c;
57         int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0;
58         int optBins=0, optSub=0;
59         char mapline[S_LEN];
60         int maplineno=1;
61         int header_printed;
62
63 #define next (current^1)
64
65         proFile = defaultpro;
66         mapFile = defaultmap;
67
68         while ((c = getopt(argc, argv, "M:m:np:itvarVbs")) != -1) {
69                 switch(c) {
70                 case 'm':
71                         mapFile = optarg;
72                         break;
73                 case 'n':
74                         optNative++;
75                         break;
76                 case 'p':
77                         proFile = optarg;
78                         break;
79                 case 'a':
80                         optAll++;
81                         break;
82                 case 'b':
83                         optBins++;
84                         break;
85                 case 's':
86                         optSub++;
87                         break;
88                 case 'i':
89                         optInfo++;
90                         break;
91                 case 'M':
92                         mult = optarg;
93                         break;
94                 case 'r':
95                         optReset++;
96                         break;
97                 case 'v':
98                         optVerbose++;
99                         break;
100                 default:
101                         bb_show_usage();
102                 }
103         }
104
105         if (optReset || mult) {
106                 int multiplier, fd, to_write;
107
108                 /*
109                  * When writing the multiplier, if the length of the write is
110                  * not sizeof(int), the multiplier is not changed
111                  */
112                 if (mult) {
113                         multiplier = strtoul(mult, 0, 10);
114                         to_write = sizeof(int);
115                 } else {
116                         multiplier = 0;
117                         to_write = 1;   /* sth different from sizeof(int) */
118                 }
119
120                 fd = xopen(defaultpro,O_WRONLY);
121
122                 if (write(fd, &multiplier, to_write) != to_write)
123                         bb_perror_msg_and_die("error writing %s", defaultpro);
124
125                 close(fd);
126                 return EXIT_SUCCESS;
127         }
128
129         /*
130          * Use an fd for the profiling buffer, to skip stdio overhead
131          */
132
133         proFd = xopen(proFile,O_RDONLY);
134
135         if (((int)(len=lseek(proFd,0,SEEK_END)) < 0)
136             || (lseek(proFd,0,SEEK_SET) < 0))
137                 bb_perror_msg_and_die(proFile);
138
139         buf = xmalloc(len);
140
141         if (read(proFd,buf,len) != len)
142                 bb_perror_msg_and_die(proFile);
143
144         close(proFd);
145
146         if (!optNative) {
147                 int entries = len/sizeof(*buf);
148                 int big = 0,small = 0,i;
149                 unsigned *p;
150
151                 for (p = buf+1; p < buf+entries; p++) {
152                         if (*p & ~0U << (sizeof(*buf)*4))
153                                 big++;
154                         if (*p & ((1 << (sizeof(*buf)*4))-1))
155                                 small++;
156                 }
157                 if (big > small) {
158                         bb_error_msg("Assuming reversed byte order. "
159                                 "Use -n to force native byte order.");
160                         for (p = buf; p < buf+entries; p++)
161                                 for (i = 0; i < sizeof(*buf)/2; i++) {
162                                         unsigned char *b = (unsigned char *) p;
163                                         unsigned char tmp;
164
165                                         tmp = b[i];
166                                         b[i] = b[sizeof(*buf)-i-1];
167                                         b[sizeof(*buf)-i-1] = tmp;
168                                 }
169                 }
170         }
171
172         step = buf[0];
173         if (optInfo) {
174                 printf("Sampling_step: %i\n", step);
175                 return EXIT_SUCCESS;
176         }
177
178         total = 0;
179
180         map = xfopen(mapFile, "r");
181
182         while (fgets(mapline,S_LEN,map)) {
183                 if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3)
184                         bb_error_msg_and_die("%s(%i): wrong map line",
185                                              mapFile, maplineno);
186
187                 if (!strcmp(fn_name,"_stext")) /* only elf works like this */ {
188                         add0 = fn_add;
189                         break;
190                 }
191                 maplineno++;
192         }
193
194         if (!add0)
195                 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
196
197         /*
198          * Main loop.
199          */
200         while (fgets(mapline,S_LEN,map)) {
201                 unsigned int this = 0;
202
203                 if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3)
204                         bb_error_msg_and_die("%s(%i): wrong map line",
205                                              mapFile, maplineno);
206
207                 header_printed = 0;
208
209                 /* ignore any LEADING (before a '[tT]' symbol is found)
210                    Absolute symbols */
211                 if ((*mode == 'A' || *mode == '?') && total == 0) continue;
212                 if (*mode != 'T' && *mode != 't' &&
213                     *mode != 'W' && *mode != 'w')
214                         break;  /* only text is profiled */
215
216                 if (indx >= len / sizeof(*buf))
217                         bb_error_msg_and_die("profile address out of range. "
218                                              "Wrong map file?");
219
220                 while (indx < (next_add-add0)/step) {
221                         if (optBins && (buf[indx] || optAll)) {
222                                 if (!header_printed) {
223                                         printf ("%s:\n", fn_name);
224                                         header_printed = 1;
225                                 }
226                                 printf ("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
227                         }
228                         this += buf[indx++];
229                 }
230                 total += this;
231
232                 if (optBins) {
233                         if (optVerbose || this > 0)
234                                 printf ("  total\t\t\t\t%u\n", this);
235                 } else if ((this || optAll) &&
236                            (fn_len = next_add-fn_add) != 0) {
237                         if (optVerbose)
238                                 printf("%016llx %-40s %6i %8.4f\n", fn_add,
239                                        fn_name,this,this/(double)fn_len);
240                         else
241                                 printf("%6i %-40s %8.4f\n",
242                                        this,fn_name,this/(double)fn_len);
243                         if (optSub) {
244                                 unsigned long long scan;
245
246                                 for (scan = (fn_add-add0)/step + 1;
247                                      scan < (next_add-add0)/step; scan++) {
248                                         unsigned long long addr;
249
250                                         addr = (scan - 1)*step + add0;
251                                         printf("\t%#llx\t%s+%#llx\t%u\n",
252                                                addr, fn_name, addr - fn_add,
253                                                buf[scan]);
254                                 }
255                         }
256                 }
257
258                 fn_add = next_add;
259                 strcpy(fn_name,next_name);
260
261                 maplineno++;
262         }
263
264         /* clock ticks, out of kernel text - probably modules */
265         printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*");
266
267         /* trailer */
268         if (optVerbose)
269                 printf("%016x %-40s %6i %8.4f\n",
270                        0,"total",total,total/(double)(fn_add-add0));
271         else
272                 printf("%6i %-40s %8.4f\n",
273                        total,"total",total/(double)(fn_add-add0));
274
275         fclose(map);
276         free(buf);
277
278         return EXIT_SUCCESS;
279 }