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