Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * readprofile.c - used to read /proc/profile |
| 3 | * |
| 4 | * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL> |
| 23 | * - added Native Language Support |
| 24 | * 1999-09-01 Stephane Eranian <eranian@cello.hpl.hp.com> |
| 25 | * - 64bit clean patch |
| 26 | * 3Feb2001 Andrew Morton <andrewm@uow.edu.au> |
| 27 | * - -M option to write profile multiplier. |
| 28 | * 2001-11-07 Werner Almesberger <wa@almesberger.net> |
| 29 | * - byte order auto-detection and -n option |
| 30 | * 2001-11-09 Werner Almesberger <wa@almesberger.net> |
| 31 | * - skip step size (index 0) |
| 32 | * 2002-03-09 John Levon <moz@compsoc.man.ac.uk> |
| 33 | * - make maplineno do something |
| 34 | * 2002-11-28 Mads Martin Joergensen + |
| 35 | * - also try /boot/System.map-`uname -r` |
| 36 | * 2003-04-09 Werner Almesberger <wa@almesberger.net> |
| 37 | * - fixed off-by eight error and improved heuristics in byte order detection |
| 38 | * 2003-08-12 Nikita Danilov <Nikita@Namesys.COM> |
| 39 | * - added -s option; example of use: |
| 40 | * "readprofile -s -m /boot/System.map-test | grep __d_lookup | sort -n -k3" |
| 41 | * |
| 42 | * Taken from util-linux and adapted for busybox by |
| 43 | * Paul Mundt <lethal@linux-sh.org>. |
| 44 | */ |
| 45 | |
| 46 | #include <errno.h> |
| 47 | #include <stdio.h> |
| 48 | #include <fcntl.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <unistd.h> |
| 51 | #include <string.h> |
| 52 | #include <sys/types.h> |
| 53 | #include <sys/stat.h> |
| 54 | #include <sys/utsname.h> |
| 55 | |
| 56 | #include "busybox.h" |
| 57 | |
| 58 | #define S_LEN 128 |
| 59 | |
| 60 | /* These are the defaults */ |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 61 | static const char defaultmap[]="/boot/System.map"; |
| 62 | static const char defaultpro[]="/proc/profile"; |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 63 | |
| 64 | int readprofile_main(int argc, char **argv) |
| 65 | { |
| 66 | FILE *map; |
| 67 | int proFd; |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 68 | const char *mapFile, *proFile, *mult=0; |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 69 | unsigned long len=0, indx=1; |
| 70 | unsigned long long add0=0; |
| 71 | unsigned int step; |
| 72 | unsigned int *buf, total, fn_len; |
| 73 | unsigned long long fn_add, next_add; /* current and next address */ |
| 74 | char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ |
| 75 | char mode[8]; |
| 76 | int c; |
| 77 | int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0; |
| 78 | int optBins=0, optSub=0; |
| 79 | char mapline[S_LEN]; |
| 80 | int maplineno=1; |
| 81 | int header_printed; |
| 82 | |
| 83 | #define next (current^1) |
| 84 | |
| 85 | proFile = defaultpro; |
| 86 | mapFile = defaultmap; |
| 87 | |
| 88 | while ((c = getopt(argc, argv, "M:m:np:itvarVbs")) != -1) { |
| 89 | switch(c) { |
| 90 | case 'm': |
| 91 | mapFile = optarg; |
| 92 | break; |
| 93 | case 'n': |
| 94 | optNative++; |
| 95 | break; |
| 96 | case 'p': |
| 97 | proFile = optarg; |
| 98 | break; |
| 99 | case 'a': |
| 100 | optAll++; |
| 101 | break; |
| 102 | case 'b': |
| 103 | optBins++; |
| 104 | break; |
| 105 | case 's': |
| 106 | optSub++; |
| 107 | break; |
| 108 | case 'i': |
| 109 | optInfo++; |
| 110 | break; |
| 111 | case 'M': |
| 112 | mult = optarg; |
| 113 | break; |
| 114 | case 'r': |
| 115 | optReset++; |
| 116 | break; |
| 117 | case 'v': |
| 118 | optVerbose++; |
| 119 | break; |
| 120 | default: |
| 121 | bb_show_usage(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (optReset || mult) { |
| 126 | int multiplier, fd, to_write; |
| 127 | |
| 128 | /* |
| 129 | * When writing the multiplier, if the length of the write is |
| 130 | * not sizeof(int), the multiplier is not changed |
| 131 | */ |
| 132 | if (mult) { |
| 133 | multiplier = strtoul(mult, 0, 10); |
| 134 | to_write = sizeof(int); |
| 135 | } else { |
| 136 | multiplier = 0; |
| 137 | to_write = 1; /* sth different from sizeof(int) */ |
| 138 | } |
| 139 | |
| 140 | fd = bb_xopen(defaultpro,O_WRONLY); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 141 | |
| 142 | if (write(fd, &multiplier, to_write) != to_write) |
| 143 | bb_perror_msg_and_die("error writing %s", defaultpro); |
| 144 | |
| 145 | close(fd); |
| 146 | return EXIT_SUCCESS; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Use an fd for the profiling buffer, to skip stdio overhead |
| 151 | */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 152 | |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 153 | proFd = bb_xopen(proFile,O_RDONLY); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 154 | |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 155 | if (((int)(len=lseek(proFd,0,SEEK_END)) < 0) |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 156 | || (lseek(proFd,0,SEEK_SET) < 0)) |
| 157 | bb_perror_msg_and_die(proFile); |
| 158 | |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 159 | buf = xmalloc(len); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 160 | |
| 161 | if (read(proFd,buf,len) != len) |
| 162 | bb_perror_msg_and_die(proFile); |
| 163 | |
| 164 | close(proFd); |
| 165 | |
| 166 | if (!optNative) { |
| 167 | int entries = len/sizeof(*buf); |
| 168 | int big = 0,small = 0,i; |
| 169 | unsigned *p; |
| 170 | |
| 171 | for (p = buf+1; p < buf+entries; p++) { |
| 172 | if (*p & ~0U << (sizeof(*buf)*4)) |
| 173 | big++; |
| 174 | if (*p & ((1 << (sizeof(*buf)*4))-1)) |
| 175 | small++; |
| 176 | } |
| 177 | if (big > small) { |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 178 | bb_error_msg("Assuming reversed byte order. " |
| 179 | "Use -n to force native byte order."); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 180 | for (p = buf; p < buf+entries; p++) |
| 181 | for (i = 0; i < sizeof(*buf)/2; i++) { |
| 182 | unsigned char *b = (unsigned char *) p; |
| 183 | unsigned char tmp; |
| 184 | |
| 185 | tmp = b[i]; |
| 186 | b[i] = b[sizeof(*buf)-i-1]; |
| 187 | b[sizeof(*buf)-i-1] = tmp; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | step = buf[0]; |
| 193 | if (optInfo) { |
| 194 | printf("Sampling_step: %i\n", step); |
| 195 | return EXIT_SUCCESS; |
| 196 | } |
| 197 | |
| 198 | total = 0; |
| 199 | |
| 200 | map = bb_xfopen(mapFile, "r"); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 201 | |
| 202 | while (fgets(mapline,S_LEN,map)) { |
| 203 | if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3) |
| 204 | bb_error_msg_and_die("%s(%i): wrong map line", |
| 205 | mapFile, maplineno); |
| 206 | |
| 207 | if (!strcmp(fn_name,"_stext")) /* only elf works like this */ { |
| 208 | add0 = fn_add; |
| 209 | break; |
| 210 | } |
| 211 | maplineno++; |
| 212 | } |
| 213 | |
| 214 | if (!add0) |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 215 | bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile); |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Main loop. |
| 219 | */ |
| 220 | while (fgets(mapline,S_LEN,map)) { |
| 221 | unsigned int this = 0; |
| 222 | |
| 223 | if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3) |
Rob Landley | b728500 | 2005-09-14 15:28:15 +0000 | [diff] [blame] | 224 | bb_error_msg_and_die("%s(%i): wrong map line", |
Paul Mundt | 8b2d02e | 2005-05-20 17:22:18 +0000 | [diff] [blame] | 225 | mapFile, maplineno); |
| 226 | |
| 227 | header_printed = 0; |
| 228 | |
| 229 | /* ignore any LEADING (before a '[tT]' symbol is found) |
| 230 | Absolute symbols */ |
| 231 | if ((*mode == 'A' || *mode == '?') && total == 0) continue; |
| 232 | if (*mode != 'T' && *mode != 't' && |
| 233 | *mode != 'W' && *mode != 'w') |
| 234 | break; /* only text is profiled */ |
| 235 | |
| 236 | if (indx >= len / sizeof(*buf)) |
| 237 | bb_error_msg_and_die("profile address out of range. " |
| 238 | "Wrong map file?"); |
| 239 | |
| 240 | while (indx < (next_add-add0)/step) { |
| 241 | if (optBins && (buf[indx] || optAll)) { |
| 242 | if (!header_printed) { |
| 243 | printf ("%s:\n", fn_name); |
| 244 | header_printed = 1; |
| 245 | } |
| 246 | printf ("\t%llx\t%u\n", (indx - 1)*step + add0, buf[indx]); |
| 247 | } |
| 248 | this += buf[indx++]; |
| 249 | } |
| 250 | total += this; |
| 251 | |
| 252 | if (optBins) { |
| 253 | if (optVerbose || this > 0) |
| 254 | printf (" total\t\t\t\t%u\n", this); |
| 255 | } else if ((this || optAll) && |
| 256 | (fn_len = next_add-fn_add) != 0) { |
| 257 | if (optVerbose) |
| 258 | printf("%016llx %-40s %6i %8.4f\n", fn_add, |
| 259 | fn_name,this,this/(double)fn_len); |
| 260 | else |
| 261 | printf("%6i %-40s %8.4f\n", |
| 262 | this,fn_name,this/(double)fn_len); |
| 263 | if (optSub) { |
| 264 | unsigned long long scan; |
| 265 | |
| 266 | for (scan = (fn_add-add0)/step + 1; |
| 267 | scan < (next_add-add0)/step; scan++) { |
| 268 | unsigned long long addr; |
| 269 | |
| 270 | addr = (scan - 1)*step + add0; |
| 271 | printf("\t%#llx\t%s+%#llx\t%u\n", |
| 272 | addr, fn_name, addr - fn_add, |
| 273 | buf[scan]); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | fn_add = next_add; |
| 279 | strcpy(fn_name,next_name); |
| 280 | |
| 281 | maplineno++; |
| 282 | } |
| 283 | |
| 284 | /* clock ticks, out of kernel text - probably modules */ |
| 285 | printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*"); |
| 286 | |
| 287 | /* trailer */ |
| 288 | if (optVerbose) |
| 289 | printf("%016x %-40s %6i %8.4f\n", |
| 290 | 0,"total",total,total/(double)(fn_add-add0)); |
| 291 | else |
| 292 | printf("%6i %-40s %8.4f\n", |
| 293 | total,"total",total/(double)(fn_add-add0)); |
| 294 | |
| 295 | fclose(map); |
| 296 | free(buf); |
| 297 | |
| 298 | return EXIT_SUCCESS; |
| 299 | } |