123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- #include "libbb.h"
- #include <sys/utsname.h>
- #define S_LEN 128
- int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int readprofile_main(int argc UNUSED_PARAM, char **argv)
- {
- FILE *map;
- const char *mapFile, *proFile;
- unsigned long indx;
- size_t len;
- uint64_t add0;
- unsigned int step;
- unsigned int *buf, total, fn_len;
- unsigned long long fn_add, next_add;
- char fn_name[S_LEN], next_name[S_LEN];
- char mapline[S_LEN];
- char mode[8];
- int maplineno;
- int multiplier;
- unsigned opt;
- enum {
- OPT_M = (1 << 0),
- OPT_m = (1 << 1),
- OPT_p = (1 << 2),
- OPT_n = (1 << 3),
- OPT_a = (1 << 4),
- OPT_b = (1 << 5),
- OPT_s = (1 << 6),
- OPT_i = (1 << 7),
- OPT_r = (1 << 8),
- OPT_v = (1 << 9),
- };
- #define optMult (opt & OPT_M)
- #define optNative (opt & OPT_n)
- #define optAll (opt & OPT_a)
- #define optBins (opt & OPT_b)
- #define optSub (opt & OPT_s)
- #define optInfo (opt & OPT_i)
- #define optReset (opt & OPT_r)
- #define optVerbose (opt & OPT_v)
- #define next (current^1)
- proFile = "/proc/profile";
- mapFile = "/boot/System.map";
- multiplier = 0;
- opt = getopt32(argv, "M:+m:p:nabsirv", &multiplier, &mapFile, &proFile);
- if (opt & (OPT_M|OPT_r)) {
- int fd, to_write;
-
- to_write = sizeof(int);
- if (!optMult)
- to_write = 1;
- fd = xopen("/proc/profile", O_WRONLY);
- xwrite(fd, &multiplier, to_write);
- close(fd);
- return EXIT_SUCCESS;
- }
-
- len = MAXINT(ssize_t);
- buf = xmalloc_xopen_read_close(proFile, &len);
- len /= sizeof(*buf);
- if (!optNative) {
- int big = 0, small = 0;
- unsigned *p;
- for (p = buf+1; p < buf+len; p++) {
- if (*p & ~0U << (sizeof(*buf)*4))
- big++;
- if (*p & ((1 << (sizeof(*buf)*4))-1))
- small++;
- }
- if (big > small) {
- bb_simple_error_msg("assuming reversed byte order, "
- "use -n to force native byte order");
- BUILD_BUG_ON(sizeof(*p) > 8);
- for (p = buf; p < buf+len; p++) {
- if (sizeof(*p) == 2)
- *p = bswap_16(*p);
- if (sizeof(*p) == 4)
- *p = bswap_32(*p);
- if (sizeof(*p) == 8)
- *p = bb_bswap_64(*p);
- }
- }
- }
- step = buf[0];
- if (optInfo) {
- printf("Sampling_step: %u\n", step);
- return EXIT_SUCCESS;
- }
- map = xfopen_for_read(mapFile);
- add0 = 0;
- maplineno = 1;
- while (fgets(mapline, S_LEN, map)) {
- if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
- bb_error_msg_and_die("%s(%i): wrong map line",
- mapFile, maplineno);
- if (strcmp(fn_name, "_stext") == 0) {
- add0 = fn_add;
- break;
- }
- maplineno++;
- }
- if (!add0)
- bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile);
-
- total = 0;
- indx = 1;
- while (fgets(mapline, S_LEN, map)) {
- bool header_printed;
- unsigned int this;
- if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3)
- bb_error_msg_and_die("%s(%i): wrong map line",
- mapFile, maplineno);
- header_printed = 0;
-
- if ((mode[0] == 'A' || mode[0] == '?') && total == 0)
- continue;
- if ((mode[0]|0x20) != 't' && (mode[0]|0x20) != 'w') {
- break;
- }
- if (indx >= len)
- bb_simple_error_msg_and_die("profile address out of range. "
- "Wrong map file?");
- this = 0;
- while (indx < (next_add-add0)/step) {
- if (optBins && (buf[indx] || optAll)) {
- if (!header_printed) {
- printf("%s:\n", fn_name);
- header_printed = 1;
- }
- printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]);
- }
- this += buf[indx++];
- }
- total += this;
- if (optBins) {
- if (optVerbose || this > 0)
- printf(" total\t\t\t\t%u\n", this);
- } else
- if ((this || optAll)
- && (fn_len = next_add-fn_add) != 0
- ) {
- if (optVerbose)
- printf("%016llx %-40s %6u %8.4f\n", fn_add,
- fn_name, this, this/(double)fn_len);
- else
- printf("%6u %-40s %8.4f\n",
- this, fn_name, this/(double)fn_len);
- if (optSub) {
- unsigned long long scan;
- for (scan = (fn_add-add0)/step + 1;
- scan < (next_add-add0)/step; scan++) {
- unsigned long long addr;
- addr = (scan - 1)*step + add0;
- printf("\t%#llx\t%s+%#llx\t%u\n",
- addr, fn_name, addr - fn_add,
- buf[scan]);
- }
- }
- }
- fn_add = next_add;
- strcpy(fn_name, next_name);
- maplineno++;
- }
-
- printf("%6u *unknown*\n", buf[len-1]);
-
- if (optVerbose)
- printf("%016x %-40s %6u %8.4f\n",
- 0, "total", total, total/(double)(fn_add-add0));
- else
- printf("%6u %-40s %8.4f\n",
- total, "total", total/(double)(fn_add-add0));
- if (ENABLE_FEATURE_CLEAN_UP) {
- fclose(map);
- free(buf);
- }
- return EXIT_SUCCESS;
- }
|