diff options
Diffstat (limited to 'util-linux/readprofile.c')
-rw-r--r-- | util-linux/readprofile.c | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/util-linux/readprofile.c b/util-linux/readprofile.c new file mode 100644 index 000000000..dd810f021 --- /dev/null +++ b/util-linux/readprofile.c | |||
@@ -0,0 +1,236 @@ | |||
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 | const char *mapFile, *proFile, *mult = 0; | ||
48 | unsigned long indx = 1; | ||
49 | size_t len; | ||
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 mapline[S_LEN]; | ||
56 | char mode[8]; | ||
57 | int optAll = 0, optInfo = 0, optReset = 0; | ||
58 | int optVerbose = 0, optNative = 0; | ||
59 | int optBins = 0, optSub = 0; | ||
60 | int maplineno = 1; | ||
61 | int header_printed; | ||
62 | |||
63 | #define next (current^1) | ||
64 | |||
65 | proFile = defaultpro; | ||
66 | mapFile = defaultmap; | ||
67 | |||
68 | opt_complementary = "nn:aa:bb:ss:ii:rr:vv"; | ||
69 | getopt32(argc, argv, "M:m:p:nabsirv", | ||
70 | &mult, &mapFile, &proFile, | ||
71 | &optNative, &optAll, &optBins, &optSub, | ||
72 | &optInfo, &optReset, &optVerbose); | ||
73 | |||
74 | if (optReset || mult) { | ||
75 | int multiplier, fd, to_write; | ||
76 | |||
77 | /* | ||
78 | * When writing the multiplier, if the length of the write is | ||
79 | * not sizeof(int), the multiplier is not changed | ||
80 | */ | ||
81 | if (mult) { | ||
82 | multiplier = xatoi_u(mult); | ||
83 | to_write = sizeof(int); | ||
84 | } else { | ||
85 | multiplier = 0; | ||
86 | to_write = 1; /* sth different from sizeof(int) */ | ||
87 | } | ||
88 | |||
89 | fd = xopen(defaultpro, O_WRONLY); | ||
90 | |||
91 | if (full_write(fd, &multiplier, to_write) != to_write) | ||
92 | bb_perror_msg_and_die("error writing %s", defaultpro); | ||
93 | |||
94 | close(fd); | ||
95 | return EXIT_SUCCESS; | ||
96 | } | ||
97 | |||
98 | /* | ||
99 | * Use an fd for the profiling buffer, to skip stdio overhead | ||
100 | */ | ||
101 | len = INT_MAX; | ||
102 | buf = xmalloc_open_read_close(proFile, &len); | ||
103 | if (!optNative) { | ||
104 | int entries = len/sizeof(*buf); | ||
105 | int big = 0, small = 0, i; | ||
106 | unsigned *p; | ||
107 | |||
108 | for (p = buf+1; p < buf+entries; p++) { | ||
109 | if (*p & ~0U << (sizeof(*buf)*4)) | ||
110 | big++; | ||
111 | if (*p & ((1 << (sizeof(*buf)*4))-1)) | ||
112 | small++; | ||
113 | } | ||
114 | if (big > small) { | ||
115 | bb_error_msg("assuming reversed byte order, " | ||
116 | "use -n to force native byte order"); | ||
117 | for (p = buf; p < buf+entries; p++) | ||
118 | for (i = 0; i < sizeof(*buf)/2; i++) { | ||
119 | unsigned char *b = (unsigned char *) p; | ||
120 | unsigned char tmp; | ||
121 | |||
122 | tmp = b[i]; | ||
123 | b[i] = b[sizeof(*buf)-i-1]; | ||
124 | b[sizeof(*buf)-i-1] = tmp; | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | step = buf[0]; | ||
130 | if (optInfo) { | ||
131 | printf("Sampling_step: %i\n", step); | ||
132 | return EXIT_SUCCESS; | ||
133 | } | ||
134 | |||
135 | total = 0; | ||
136 | |||
137 | map = xfopen(mapFile, "r"); | ||
138 | |||
139 | while (fgets(mapline, S_LEN, map)) { | ||
140 | if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) | ||
141 | bb_error_msg_and_die("%s(%i): wrong map line", | ||
142 | mapFile, maplineno); | ||
143 | |||
144 | if (!strcmp(fn_name, "_stext")) /* only elf works like this */ { | ||
145 | add0 = fn_add; | ||
146 | break; | ||
147 | } | ||
148 | maplineno++; | ||
149 | } | ||
150 | |||
151 | if (!add0) | ||
152 | bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile); | ||
153 | |||
154 | /* | ||
155 | * Main loop. | ||
156 | */ | ||
157 | while (fgets(mapline, S_LEN, map)) { | ||
158 | unsigned int this = 0; | ||
159 | |||
160 | if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3) | ||
161 | bb_error_msg_and_die("%s(%i): wrong map line", | ||
162 | mapFile, maplineno); | ||
163 | |||
164 | header_printed = 0; | ||
165 | |||
166 | /* ignore any LEADING (before a '[tT]' symbol is found) | ||
167 | Absolute symbols */ | ||
168 | if ((*mode == 'A' || *mode == '?') && total == 0) continue; | ||
169 | if (*mode != 'T' && *mode != 't' && | ||
170 | *mode != 'W' && *mode != 'w') | ||
171 | break; /* only text is profiled */ | ||
172 | |||
173 | if (indx >= len / sizeof(*buf)) | ||
174 | bb_error_msg_and_die("profile address out of range. " | ||
175 | "Wrong map file?"); | ||
176 | |||
177 | while (indx < (next_add-add0)/step) { | ||
178 | if (optBins && (buf[indx] || optAll)) { | ||
179 | if (!header_printed) { | ||
180 | printf("%s:\n", fn_name); | ||
181 | header_printed = 1; | ||
182 | } | ||
183 | printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]); | ||
184 | } | ||
185 | this += buf[indx++]; | ||
186 | } | ||
187 | total += this; | ||
188 | |||
189 | if (optBins) { | ||
190 | if (optVerbose || this > 0) | ||
191 | printf(" total\t\t\t\t%u\n", this); | ||
192 | } else if ((this || optAll) && | ||
193 | (fn_len = next_add-fn_add) != 0) { | ||
194 | if (optVerbose) | ||
195 | printf("%016llx %-40s %6i %8.4f\n", fn_add, | ||
196 | fn_name, this, this/(double)fn_len); | ||
197 | else | ||
198 | printf("%6i %-40s %8.4f\n", | ||
199 | this, fn_name, this/(double)fn_len); | ||
200 | if (optSub) { | ||
201 | unsigned long long scan; | ||
202 | |||
203 | for (scan = (fn_add-add0)/step + 1; | ||
204 | scan < (next_add-add0)/step; scan++) { | ||
205 | unsigned long long addr; | ||
206 | |||
207 | addr = (scan - 1)*step + add0; | ||
208 | printf("\t%#llx\t%s+%#llx\t%u\n", | ||
209 | addr, fn_name, addr - fn_add, | ||
210 | buf[scan]); | ||
211 | } | ||
212 | } | ||
213 | } | ||
214 | |||
215 | fn_add = next_add; | ||
216 | strcpy(fn_name, next_name); | ||
217 | |||
218 | maplineno++; | ||
219 | } | ||
220 | |||
221 | /* clock ticks, out of kernel text - probably modules */ | ||
222 | printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*"); | ||
223 | |||
224 | /* trailer */ | ||
225 | if (optVerbose) | ||
226 | printf("%016x %-40s %6i %8.4f\n", | ||
227 | 0, "total", total, total/(double)(fn_add-add0)); | ||
228 | else | ||
229 | printf("%6i %-40s %8.4f\n", | ||
230 | total, "total", total/(double)(fn_add-add0)); | ||
231 | |||
232 | fclose(map); | ||
233 | free(buf); | ||
234 | |||
235 | return EXIT_SUCCESS; | ||
236 | } | ||