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