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