"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 2 | /* |
| 3 | * readprofile.c - used to read /proc/profile |
| 4 | * |
| 5 | * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it) |
| 6 | * |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 36 | #include <sys/utsname.h> |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 37 | |
| 38 | #define S_LEN 128 |
| 39 | |
| 40 | /* These are the defaults */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 41 | static const char defaultmap[] ALIGN1 = "/boot/System.map"; |
| 42 | static const char defaultpro[] ALIGN1 = "/proc/profile"; |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 44 | int readprofile_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 45 | int readprofile_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 46 | { |
| 47 | FILE *map; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 48 | const char *mapFile, *proFile, *mult = 0; |
| 49 | unsigned long indx = 1; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 50 | size_t len; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 51 | uint64_t add0 = 0; |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 52 | unsigned int step; |
| 53 | unsigned int *buf, total, fn_len; |
Denis Vlasenko | cad3668 | 2006-09-22 08:39:49 +0000 | [diff] [blame] | 54 | unsigned long long fn_add, next_add; /* current and next address */ |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 55 | char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 56 | char mapline[S_LEN]; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 57 | char mode[8]; |
| 58 | int optAll = 0, optInfo = 0, optReset = 0; |
| 59 | int optVerbose = 0, optNative = 0; |
| 60 | int optBins = 0, optSub = 0; |
| 61 | int maplineno = 1; |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 62 | int header_printed; |
| 63 | |
| 64 | #define next (current^1) |
| 65 | |
| 66 | proFile = defaultpro; |
| 67 | mapFile = defaultmap; |
| 68 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 69 | opt_complementary = "nn:aa:bb:ss:ii:rr:vv"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 70 | getopt32(argv, "M:m:p:nabsirv", |
Denis Vlasenko | cad3668 | 2006-09-22 08:39:49 +0000 | [diff] [blame] | 71 | &mult, &mapFile, &proFile, |
| 72 | &optNative, &optAll, &optBins, &optSub, |
| 73 | &optInfo, &optReset, &optVerbose); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 74 | |
| 75 | if (optReset || mult) { |
| 76 | int multiplier, fd, to_write; |
| 77 | |
| 78 | /* |
| 79 | * When writing the multiplier, if the length of the write is |
| 80 | * not sizeof(int), the multiplier is not changed |
| 81 | */ |
| 82 | if (mult) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 83 | multiplier = xatoi_u(mult); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 84 | to_write = sizeof(int); |
| 85 | } else { |
| 86 | multiplier = 0; |
| 87 | to_write = 1; /* sth different from sizeof(int) */ |
| 88 | } |
| 89 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 90 | fd = xopen(defaultpro, O_WRONLY); |
Denis Vlasenko | a89d50f | 2007-11-13 17:51:40 +0000 | [diff] [blame] | 91 | xwrite(fd, &multiplier, to_write); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 92 | close(fd); |
| 93 | return EXIT_SUCCESS; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Use an fd for the profiling buffer, to skip stdio overhead |
| 98 | */ |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 99 | len = MAXINT(ssize_t); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 100 | buf = xmalloc_open_read_close(proFile, &len); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 101 | if (!optNative) { |
| 102 | int entries = len/sizeof(*buf); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 103 | int big = 0, small = 0, i; |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 104 | unsigned *p; |
| 105 | |
| 106 | for (p = buf+1; p < buf+entries; p++) { |
| 107 | if (*p & ~0U << (sizeof(*buf)*4)) |
| 108 | big++; |
| 109 | if (*p & ((1 << (sizeof(*buf)*4))-1)) |
| 110 | small++; |
| 111 | } |
| 112 | if (big > small) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 113 | bb_error_msg("assuming reversed byte order, " |
| 114 | "use -n to force native byte order"); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 115 | for (p = buf; p < buf+entries; p++) |
| 116 | for (i = 0; i < sizeof(*buf)/2; i++) { |
| 117 | unsigned char *b = (unsigned char *) p; |
| 118 | unsigned char tmp; |
| 119 | |
| 120 | tmp = b[i]; |
| 121 | b[i] = b[sizeof(*buf)-i-1]; |
| 122 | b[sizeof(*buf)-i-1] = tmp; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | step = buf[0]; |
| 128 | if (optInfo) { |
| 129 | printf("Sampling_step: %i\n", step); |
| 130 | return EXIT_SUCCESS; |
| 131 | } |
| 132 | |
| 133 | total = 0; |
| 134 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 135 | map = xfopen(mapFile, "r"); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 137 | while (fgets(mapline, S_LEN, map)) { |
| 138 | if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 139 | bb_error_msg_and_die("%s(%i): wrong map line", |
| 140 | mapFile, maplineno); |
| 141 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 142 | if (!strcmp(fn_name, "_stext")) /* only elf works like this */ { |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 143 | add0 = fn_add; |
| 144 | break; |
| 145 | } |
| 146 | maplineno++; |
| 147 | } |
| 148 | |
| 149 | if (!add0) |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 150 | bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Main loop. |
| 154 | */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 155 | while (fgets(mapline, S_LEN, map)) { |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 156 | unsigned int this = 0; |
| 157 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 158 | if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3) |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 159 | bb_error_msg_and_die("%s(%i): wrong map line", |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 160 | mapFile, maplineno); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 161 | |
| 162 | header_printed = 0; |
| 163 | |
| 164 | /* ignore any LEADING (before a '[tT]' symbol is found) |
| 165 | Absolute symbols */ |
| 166 | if ((*mode == 'A' || *mode == '?') && total == 0) continue; |
| 167 | if (*mode != 'T' && *mode != 't' && |
| 168 | *mode != 'W' && *mode != 'w') |
| 169 | break; /* only text is profiled */ |
| 170 | |
| 171 | if (indx >= len / sizeof(*buf)) |
| 172 | bb_error_msg_and_die("profile address out of range. " |
| 173 | "Wrong map file?"); |
| 174 | |
| 175 | while (indx < (next_add-add0)/step) { |
| 176 | if (optBins && (buf[indx] || optAll)) { |
| 177 | if (!header_printed) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 178 | printf("%s:\n", fn_name); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 179 | header_printed = 1; |
| 180 | } |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 181 | printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 182 | } |
| 183 | this += buf[indx++]; |
| 184 | } |
| 185 | total += this; |
| 186 | |
| 187 | if (optBins) { |
| 188 | if (optVerbose || this > 0) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 189 | printf(" total\t\t\t\t%u\n", this); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 190 | } else if ((this || optAll) && |
| 191 | (fn_len = next_add-fn_add) != 0) { |
| 192 | if (optVerbose) |
| 193 | printf("%016llx %-40s %6i %8.4f\n", fn_add, |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 194 | fn_name, this, this/(double)fn_len); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 195 | else |
| 196 | printf("%6i %-40s %8.4f\n", |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 197 | this, fn_name, this/(double)fn_len); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 198 | if (optSub) { |
| 199 | unsigned long long scan; |
| 200 | |
| 201 | for (scan = (fn_add-add0)/step + 1; |
| 202 | scan < (next_add-add0)/step; scan++) { |
| 203 | unsigned long long addr; |
| 204 | |
| 205 | addr = (scan - 1)*step + add0; |
| 206 | printf("\t%#llx\t%s+%#llx\t%u\n", |
| 207 | addr, fn_name, addr - fn_add, |
| 208 | buf[scan]); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fn_add = next_add; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 214 | strcpy(fn_name, next_name); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 215 | |
| 216 | maplineno++; |
| 217 | } |
| 218 | |
| 219 | /* clock ticks, out of kernel text - probably modules */ |
| 220 | printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*"); |
| 221 | |
| 222 | /* trailer */ |
| 223 | if (optVerbose) |
| 224 | printf("%016x %-40s %6i %8.4f\n", |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 225 | 0, "total", total, total/(double)(fn_add-add0)); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 226 | else |
| 227 | printf("%6i %-40s %8.4f\n", |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 228 | total, "total", total/(double)(fn_add-add0)); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 229 | |
| 230 | fclose(map); |
| 231 | free(buf); |
| 232 | |
| 233 | return EXIT_SUCCESS; |
| 234 | } |