Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/util-linux/readprofile.c
- Timestamp:
- Nov 4, 2007, 3:16:40 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.5/mindi-busybox/util-linux/readprofile.c
r902 r1765 1 /* vi: set sw=4 ts=4: */ 1 2 /* 2 3 * readprofile.c - used to read /proc/profile … … 4 5 * Copyright (C) 1994,1996 Alessandro Rubini (rubini@ipvvis.unipv.it) 5 6 * 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. 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 19 8 */ 20 9 … … 44 33 */ 45 34 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> 35 #include "libbb.h" 54 36 #include <sys/utsname.h> 55 37 56 #include "busybox.h"57 58 38 #define S_LEN 128 59 39 60 40 /* These are the defaults */ 61 static const char defaultmap[]="/boot/System.map"; 62 static const char defaultpro[]="/proc/profile"; 63 41 static const char defaultmap[] ALIGN1 = "/boot/System.map"; 42 static const char defaultpro[] ALIGN1 = "/proc/profile"; 43 44 int readprofile_main(int argc, char **argv); 64 45 int readprofile_main(int argc, char **argv) 65 46 { 66 47 FILE *map; 67 int proFd;68 const char *mapFile, *proFile, *mult=0;69 unsigned long len=0, indx=1;70 uint64_t add0 =0;48 const char *mapFile, *proFile, *mult = 0; 49 unsigned long indx = 1; 50 size_t len; 51 uint64_t add0 = 0; 71 52 unsigned int step; 72 53 unsigned int *buf, total, fn_len; 73 unsigned long long fn_add, next_add; 54 unsigned long long fn_add, next_add; /* current and next address */ 74 55 char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ 56 char mapline[S_LEN]; 75 57 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; 58 int optAll = 0, optInfo = 0, optReset = 0; 59 int optVerbose = 0, optNative = 0; 60 int optBins = 0, optSub = 0; 61 int maplineno = 1; 81 62 int header_printed; 82 63 … … 86 67 mapFile = defaultmap; 87 68 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 } 69 opt_complementary = "nn:aa:bb:ss:ii:rr:vv"; 70 getopt32(argv, "M:m:p:nabsirv", 71 &mult, &mapFile, &proFile, 72 &optNative, &optAll, &optBins, &optSub, 73 &optInfo, &optReset, &optVerbose); 124 74 125 75 if (optReset || mult) { … … 131 81 */ 132 82 if (mult) { 133 multiplier = strtoul(mult, 0, 10);83 multiplier = xatoi_u(mult); 134 84 to_write = sizeof(int); 135 85 } else { … … 138 88 } 139 89 140 fd = bb_xopen(defaultpro,O_WRONLY);141 142 if ( write(fd, &multiplier, to_write) != to_write)90 fd = xopen(defaultpro, O_WRONLY); 91 92 if (full_write(fd, &multiplier, to_write) != to_write) 143 93 bb_perror_msg_and_die("error writing %s", defaultpro); 144 94 … … 150 100 * Use an fd for the profiling buffer, to skip stdio overhead 151 101 */ 152 153 proFd = bb_xopen(proFile,O_RDONLY); 154 155 if (((int)(len=lseek(proFd,0,SEEK_END)) < 0) 156 || (lseek(proFd,0,SEEK_SET) < 0)) 157 bb_perror_msg_and_die(proFile); 158 159 buf = xmalloc(len); 160 161 if (read(proFd,buf,len) != len) 162 bb_perror_msg_and_die(proFile); 163 164 close(proFd); 165 102 len = MAXINT(ssize_t); 103 buf = xmalloc_open_read_close(proFile, &len); 166 104 if (!optNative) { 167 105 int entries = len/sizeof(*buf); 168 int big = 0, small = 0,i;106 int big = 0, small = 0, i; 169 107 unsigned *p; 170 108 … … 176 114 } 177 115 if (big > small) { 178 bb_error_msg(" Assuming reversed byte order."179 " Use -n to force native byte order.");116 bb_error_msg("assuming reversed byte order, " 117 "use -n to force native byte order"); 180 118 for (p = buf; p < buf+entries; p++) 181 119 for (i = 0; i < sizeof(*buf)/2; i++) { … … 198 136 total = 0; 199 137 200 map = bb_xfopen(mapFile, "r");201 202 while (fgets(mapline, S_LEN,map)) {203 if (sscanf(mapline, "%llx %s %s",&fn_add,mode,fn_name) != 3)138 map = xfopen(mapFile, "r"); 139 140 while (fgets(mapline, S_LEN, map)) { 141 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) 204 142 bb_error_msg_and_die("%s(%i): wrong map line", 205 143 mapFile, maplineno); 206 144 207 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ {145 if (!strcmp(fn_name, "_stext")) /* only elf works like this */ { 208 146 add0 = fn_add; 209 147 break; … … 218 156 * Main loop. 219 157 */ 220 while (fgets(mapline, S_LEN,map)) {158 while (fgets(mapline, S_LEN, map)) { 221 159 unsigned int this = 0; 222 160 223 if (sscanf(mapline, "%llx %s %s",&next_add,mode,next_name) != 3)161 if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3) 224 162 bb_error_msg_and_die("%s(%i): wrong map line", 225 163 mapFile, maplineno); 226 164 227 165 header_printed = 0; … … 241 179 if (optBins && (buf[indx] || optAll)) { 242 180 if (!header_printed) { 243 printf 181 printf("%s:\n", fn_name); 244 182 header_printed = 1; 245 183 } 246 printf 184 printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]); 247 185 } 248 186 this += buf[indx++]; … … 252 190 if (optBins) { 253 191 if (optVerbose || this > 0) 254 printf 192 printf(" total\t\t\t\t%u\n", this); 255 193 } else if ((this || optAll) && 256 194 (fn_len = next_add-fn_add) != 0) { 257 195 if (optVerbose) 258 196 printf("%016llx %-40s %6i %8.4f\n", fn_add, 259 fn_name, this,this/(double)fn_len);197 fn_name, this, this/(double)fn_len); 260 198 else 261 199 printf("%6i %-40s %8.4f\n", 262 this, fn_name,this/(double)fn_len);200 this, fn_name, this/(double)fn_len); 263 201 if (optSub) { 264 202 unsigned long long scan; … … 277 215 278 216 fn_add = next_add; 279 strcpy(fn_name, next_name);217 strcpy(fn_name, next_name); 280 218 281 219 maplineno++; … … 288 226 if (optVerbose) 289 227 printf("%016x %-40s %6i %8.4f\n", 290 0, "total",total,total/(double)(fn_add-add0));228 0, "total", total, total/(double)(fn_add-add0)); 291 229 else 292 230 printf("%6i %-40s %8.4f\n", 293 total, "total",total/(double)(fn_add-add0));231 total, "total", total/(double)(fn_add-add0)); 294 232 295 233 fclose(map);
Note:
See TracChangeset
for help on using the changeset viewer.