1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58#include "libbb.h"
59#include <sys/utsname.h>
60
61#define S_LEN 128
62
63int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
64int readprofile_main(int argc UNUSED_PARAM, char **argv)
65{
66 FILE *map;
67 const char *mapFile, *proFile;
68 unsigned long indx;
69 size_t len;
70 uint64_t add0;
71 unsigned int step;
72 unsigned int *buf, total, fn_len;
73 unsigned long long fn_add, next_add;
74 char fn_name[S_LEN], next_name[S_LEN];
75 char mapline[S_LEN];
76 char mode[8];
77 int maplineno;
78 int multiplier;
79 unsigned opt;
80 enum {
81 OPT_M = (1 << 0),
82 OPT_m = (1 << 1),
83 OPT_p = (1 << 2),
84 OPT_n = (1 << 3),
85 OPT_a = (1 << 4),
86 OPT_b = (1 << 5),
87 OPT_s = (1 << 6),
88 OPT_i = (1 << 7),
89 OPT_r = (1 << 8),
90 OPT_v = (1 << 9),
91 };
92#define optMult (opt & OPT_M)
93#define optNative (opt & OPT_n)
94#define optAll (opt & OPT_a)
95#define optBins (opt & OPT_b)
96#define optSub (opt & OPT_s)
97#define optInfo (opt & OPT_i)
98#define optReset (opt & OPT_r)
99#define optVerbose (opt & OPT_v)
100
101#define next (current^1)
102
103 proFile = "/proc/profile";
104 mapFile = "/boot/System.map";
105 multiplier = 0;
106
107 opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
108
109 if (opt & (OPT_M|OPT_r)) {
110 int fd, to_write;
111
112
113
114
115
116 to_write = sizeof(int);
117 if (!optMult)
118 to_write = 1;
119
120 fd = xopen("/proc/profile", O_WRONLY);
121 xwrite(fd, &multiplier, to_write);
122 close(fd);
123 return EXIT_SUCCESS;
124 }
125
126
127
128
129 len = MAXINT(ssize_t);
130 buf = xmalloc_xopen_read_close(proFile, &len);
131 len /= sizeof(*buf);
132
133 if (!optNative) {
134 int big = 0, small = 0;
135 unsigned *p;
136
137 for (p = buf+1; p < buf+len; p++) {
138 if (*p & ~0U << (sizeof(*buf)*4))
139 big++;
140 if (*p & ((1 << (sizeof(*buf)*4))-1))
141 small++;
142 }
143 if (big > small) {
144 bb_simple_error_msg("assuming reversed byte order, "
145 "use -n to force native byte order");
146 BUILD_BUG_ON(sizeof(*p) > 8);
147 for (p = buf; p < buf+len; p++) {
148 if (sizeof(*p) == 2)
149 *p = bswap_16(*p);
150 if (sizeof(*p) == 4)
151 *p = bswap_32(*p);
152 if (sizeof(*p) == 8)
153 *p = bb_bswap_64(*p);
154 }
155 }
156 }
157
158 step = buf[0];
159 if (optInfo) {
160 printf("Sampling_step: %u\n", step);
161 return EXIT_SUCCESS;
162 }
163
164 map = xfopen_for_read(mapFile);
165 add0 = 0;
166 maplineno = 1;
167 while (fgets(mapline, S_LEN, map)) {
168 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
169 bb_error_msg_and_die("%s(%i): wrong map line",
170 mapFile, maplineno);
171
172 if (strcmp(fn_name, "_stext") == 0) {
173 add0 = fn_add;
174 break;
175 }
176 maplineno++;
177 }
178
179 if (!add0)
180 bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
181
182
183
184
185 total = 0;
186 indx = 1;
187 while (fgets(mapline, S_LEN, map)) {
188 bool header_printed;
189 unsigned int this;
190
191 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
192 bb_error_msg_and_die("%s(%i): wrong map line",
193 mapFile, maplineno);
194
195 header_printed = 0;
196
197
198
199 if ((mode[0] == 'A' || mode[0] == '?') && total == 0)
200 continue;
201 if ((mode[0]|0x20) != 't' && (mode[0]|0x20) != 'w') {
202 break;
203 }
204
205 if (indx >= len)
206 bb_simple_error_msg_and_die("profile address out of range. "
207 "Wrong map file?");
208
209 this = 0;
210 while (indx < (next_add-add0)/step) {
211 if (optBins && (buf[indx] || optAll)) {
212 if (!header_printed) {
213 printf("%s:\n", fn_name);
214 header_printed = 1;
215 }
216 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
217 }
218 this += buf[indx++];
219 }
220 total += this;
221
222 if (optBins) {
223 if (optVerbose || this > 0)
224 printf(" total\t\t\t\t%u\n", this);
225 } else
226 if ((this || optAll)
227 && (fn_len = next_add-fn_add) != 0
228 ) {
229 if (optVerbose)
230 printf("%016llx %-40s %6u %8.4f\n", fn_add,
231 fn_name, this, this/(double)fn_len);
232 else
233 printf("%6u %-40s %8.4f\n",
234 this, fn_name, this/(double)fn_len);
235 if (optSub) {
236 unsigned long long scan;
237
238 for (scan = (fn_add-add0)/step + 1;
239 scan < (next_add-add0)/step; scan++) {
240 unsigned long long addr;
241
242 addr = (scan - 1)*step + add0;
243 printf("\t%#llx\t%s+%#llx\t%u\n",
244 addr, fn_name, addr - fn_add,
245 buf[scan]);
246 }
247 }
248 }
249
250 fn_add = next_add;
251 strcpy(fn_name, next_name);
252
253 maplineno++;
254 }
255
256
257 printf("%6u *unknown*\n", buf[len-1]);
258
259
260 if (optVerbose)
261 printf("%016x %-40s %6u %8.4f\n",
262 0, "total", total, total/(double)(fn_add-add0));
263 else
264 printf("%6u %-40s %8.4f\n",
265 total, "total", total/(double)(fn_add-add0));
266
267 if (ENABLE_FEATURE_CLEAN_UP) {
268 fclose(map);
269 free(buf);
270 }
271
272 return EXIT_SUCCESS;
273}
274