options.c revision f670eea50e959eeb9da53d70cad8d43c19494ef0
1#include "config.h" 2 3#include <string.h> 4#include <stdlib.h> 5#include <unistd.h> 6#include <fcntl.h> 7#include <errno.h> 8#include <limits.h> 9#include <sys/ioctl.h> 10 11#include <getopt.h> 12 13#include "common.h" 14 15#ifndef SYSCONFDIR 16#define SYSCONFDIR "/etc" 17#endif 18 19#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf" 20#define USER_CONFIG_FILE "~/.ltrace.conf" 21 22struct options_t options = { 23 .align = DEFAULT_ALIGN, /* alignment column for results */ 24 .user = NULL, /* username to run command as */ 25 .syscalls = 0, /* display syscalls */ 26 .libcalls = 1, /* display library calls */ 27#ifdef USE_DEMANGLE 28 .demangle = 0, /* Demangle low-level symbol names */ 29#endif 30 .indent = 0, /* indent output according to program flow */ 31 .output = NULL, /* output to a specific file */ 32 .summary = 0, /* Report a summary on program exit */ 33 .debug = 0, /* debug */ 34 .arraylen = DEFAULT_ARRAYLEN, /* maximum # array elements to print */ 35 .strlen = DEFAULT_STRLEN, /* maximum # of bytes printed in strings */ 36 .follow = 0, /* trace child processes */ 37}; 38 39char *library[MAX_LIBRARIES]; 40int library_num = 0; 41static char *progname; /* Program name (`ltrace') */ 42int opt_i = 0; /* instruction pointer */ 43int opt_r = 0; /* print relative timestamp */ 44int opt_t = 0; /* print absolute timestamp */ 45int opt_T = 0; /* show the time spent inside each call */ 46 47/* List of pids given to option -p: */ 48struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */ 49 50/* List of function names given to option -e: */ 51struct opt_e_t *opt_e = NULL; 52int opt_e_enable = 1; 53 54/* List of global function names given to -x: */ 55struct opt_x_t *opt_x = NULL; 56 57/* List of filenames give to option -F: */ 58struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */ 59 60#ifdef PLT_REINITALISATION_BP 61/* Set a break on the routine named here in order to re-initialize breakpoints 62 after all the PLTs have been initialzed */ 63char *PLTs_initialized_by_here = PLT_REINITALISATION_BP; 64#endif 65 66static void 67err_usage(void) { 68 fprintf(stderr, "Try `%s --help' for more information\n", progname); 69 exit(1); 70} 71 72static void 73usage(void) { 74 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" 75 "Trace library calls of a given program.\n\n" 76 " -a, --align=COLUMN align return values in a secific column.\n" 77 " -A ARRAYLEN maximum number of array elements to print.\n" 78 " -c count time and calls, and report a summary on exit.\n" 79# ifdef USE_DEMANGLE 80 " -C, --demangle decode low-level symbol names into user-level names.\n" 81# endif 82 " -D, --debug=LEVEL enable debugging (see -Dh or --debug=help).\n" 83 " -Dh, --debug=help show help on debugging.\n" 84 " -e expr modify which events to trace.\n" 85 " -f trace children (fork() and clone()).\n" 86 " -F, --config=FILE load alternate configuration file (may be repeated).\n" 87 " -h, --help display this help and exit.\n" 88 " -i print instruction pointer at time of library call.\n" 89 " -l, --library=FILE print library calls from this library only.\n" 90 " -L do NOT display library calls.\n" 91 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n" 92 " -o, --output=FILE write the trace output to that file.\n" 93 " -p PID attach to the process with the process ID pid.\n" 94 " -r print relative timestamps.\n" 95 " -s STRLEN specify the maximum string size to print.\n" 96 " -S display system calls.\n" 97 " -t, -tt, -ttt print absolute timestamps.\n" 98 " -T show the time spent inside each call.\n" 99 " -u USERNAME run command with the userid, groupid of username.\n" 100 " -V, --version output version information and exit.\n" 101 " -x NAME treat the global NAME like a library subroutine.\n" 102#ifdef PLT_REINITALISATION_BP 103 " -X NAME same as -x; and PLT's will be initialized by here.\n" 104#endif 105 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n", 106 progname); 107} 108 109static void 110usage_debug(void) { 111 fprintf(stdout, "%s debugging option, --debug=<octal> or -D<octal>:\n", progname); 112 fprintf(stdout, 113 "\n" 114 " number ref. in source description\n" 115 " 1 general Generally helpful progress information\n" 116 " 10 event Shows every event received by a traced process\n" 117 " 20 process Shows actions carried upon a traced processes\n" 118 " 40 function Shows every entry to internal functions\n" 119 "\n" 120 "Debugging options are mixed using bitwise-or.\n" 121 "Note that the meanings and values are subject to change.\n" 122 ); 123} 124 125static char * 126search_for_command(char *filename) { 127 static char pathname[PATH_MAX]; 128 char *path; 129 int m, n; 130 131 if (strchr(filename, '/')) { 132 return filename; 133 } 134 for (path = getenv("PATH"); path && *path; path += m) { 135 if (strchr(path, ':')) { 136 n = strchr(path, ':') - path; 137 m = n + 1; 138 } else { 139 m = n = strlen(path); 140 } 141 if (n + strlen(filename) + 1 >= PATH_MAX) { 142 fprintf(stderr, "Error: filename too long\n"); 143 exit(1); 144 } 145 strncpy(pathname, path, n); 146 if (n && pathname[n - 1] != '/') { 147 pathname[n++] = '/'; 148 } 149 strcpy(pathname + n, filename); 150 if (!access(pathname, X_OK)) { 151 return pathname; 152 } 153 } 154 return filename; 155} 156 157static void 158guess_cols(void) { 159 struct winsize ws; 160 char *c; 161 162 options.align = DEFAULT_ALIGN; 163 c = getenv("COLUMNS"); 164 if (c && *c) { 165 char *endptr; 166 int cols; 167 cols = strtol(c, &endptr, 0); 168 if (cols > 0 && !*endptr) { 169 options.align = cols * 5 / 8; 170 } 171 } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { 172 options.align = ws.ws_col * 5 / 8; 173 } else if (ioctl(2, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) { 174 options.align = ws.ws_col * 5 / 8; 175 } 176} 177 178char ** 179process_options(int argc, char **argv) { 180 progname = argv[0]; 181 options.output = stderr; 182 183 guess_cols(); 184 185 while (1) { 186 int c; 187 char *p; 188 int option_index = 0; 189 static struct option long_options[] = { 190 {"align", 1, 0, 'a'}, 191 {"config", 1, 0, 'F'}, 192 {"debug", 1, 0, 'D'}, 193# ifdef USE_DEMANGLE 194 {"demangle", 0, 0, 'C'}, 195#endif 196 {"indent", 1, 0, 'n'}, 197 {"help", 0, 0, 'h'}, 198 {"library", 1, 0, 'l'}, 199 {"output", 1, 0, 'o'}, 200 {"version", 0, 0, 'V'}, 201 {0, 0, 0, 0} 202 }; 203 c = getopt_long(argc, argv, "+cfhiLrStTV" 204# ifdef USE_DEMANGLE 205 "C" 206# endif 207 "a:A:D:e:F:l:n:o:p:s:u:x:X:", long_options, 208 &option_index); 209 if (c == -1) { 210 break; 211 } 212 switch (c) { 213 case 'a': 214 options.align = atoi(optarg); 215 break; 216 case 'A': 217 options.arraylen = atoi(optarg); 218 break; 219 case 'c': 220 options.summary++; 221 break; 222#ifdef USE_DEMANGLE 223 case 'C': 224 options.demangle++; 225 break; 226#endif 227 case 'D': 228 if (optarg[0]=='h') { 229 usage_debug(); 230 exit(0); 231 } 232 options.debug = strtoul(optarg,&p,8); 233 if (*p) { 234 fprintf(stderr, "%s: --debug requires an octal argument\n", progname); 235 err_usage(); 236 } 237 break; 238 case 'e': 239 { 240 char *str_e = strdup(optarg); 241 if (!str_e) { 242 perror("ltrace: strdup"); 243 exit(1); 244 } 245 if (str_e[0] == '!') { 246 opt_e_enable = 0; 247 str_e++; 248 } 249 while (*str_e) { 250 struct opt_e_t *tmp; 251 char *str2 = strchr(str_e, ','); 252 if (str2) { 253 *str2 = '\0'; 254 } 255 tmp = malloc(sizeof(struct opt_e_t)); 256 if (!tmp) { 257 perror("ltrace: malloc"); 258 exit(1); 259 } 260 tmp->name = str_e; 261 tmp->next = opt_e; 262 opt_e = tmp; 263 if (str2) { 264 str_e = str2 + 1; 265 } else { 266 break; 267 } 268 } 269 break; 270 } 271 case 'f': 272 options.follow = 1; 273 break; 274 case 'F': 275 { 276 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t)); 277 if (!tmp) { 278 perror("ltrace: malloc"); 279 exit(1); 280 } 281 tmp->filename = strdup(optarg); 282 tmp->next = opt_F; 283 opt_F = tmp; 284 break; 285 } 286 case 'h': 287 usage(); 288 exit(0); 289 case 'i': 290 opt_i++; 291 break; 292 case 'l': 293 if (library_num == MAX_LIBRARIES) { 294 fprintf(stderr, 295 "Too many libraries. Maximum is %i.\n", 296 MAX_LIBRARIES); 297 exit(1); 298 } 299 library[library_num++] = optarg; 300 break; 301 case 'L': 302 options.libcalls = 0; 303 break; 304 case 'n': 305 options.indent = atoi(optarg); 306 break; 307 case 'o': 308 options.output = fopen(optarg, "w"); 309 if (!options.output) { 310 fprintf(stderr, 311 "Can't open %s for output: %s\n", 312 optarg, strerror(errno)); 313 exit(1); 314 } 315 setvbuf(options.output, (char *)NULL, _IOLBF, 0); 316 fcntl(fileno(options.output), F_SETFD, FD_CLOEXEC); 317 break; 318 case 'p': 319 { 320 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t)); 321 if (!tmp) { 322 perror("ltrace: malloc"); 323 exit(1); 324 } 325 tmp->pid = atoi(optarg); 326 tmp->next = opt_p; 327 opt_p = tmp; 328 break; 329 } 330 case 'r': 331 opt_r++; 332 break; 333 case 's': 334 options.strlen = atoi(optarg); 335 break; 336 case 'S': 337 options.syscalls = 1; 338 break; 339 case 't': 340 opt_t++; 341 break; 342 case 'T': 343 opt_T++; 344 break; 345 case 'u': 346 options.user = optarg; 347 break; 348 case 'V': 349 printf("ltrace version " PACKAGE_VERSION ".\n" 350 "Copyright (C) 1997-2009 Juan Cespedes <cespedes@debian.org>.\n" 351 "This is free software; see the GNU General Public Licence\n" 352 "version 2 or later for copying conditions. There is NO warranty.\n"); 353 exit(0); 354 case 'X': 355#ifdef PLT_REINITALISATION_BP 356 PLTs_initialized_by_here = optarg; 357#else 358 fprintf(stderr, "WARNING: \"-X\" not used for this " 359 "architecture: assuming you meant \"-x\"\n"); 360#endif 361 /* Fall Thru */ 362 363 case 'x': 364 { 365 struct opt_x_t *p = opt_x; 366 367 /* First, check for duplicate. */ 368 while (p && strcmp(p->name, optarg)) { 369 p = p->next; 370 } 371 if (p) { 372 break; 373 } 374 375 /* If not duplicate, add to list. */ 376 p = malloc(sizeof(struct opt_x_t)); 377 if (!p) { 378 perror("ltrace: malloc"); 379 exit(1); 380 } 381 p->name = optarg; 382 p->found = 0; 383 p->next = opt_x; 384 opt_x = p; 385 break; 386 } 387 388 default: 389 err_usage(); 390 } 391 } 392 argc -= optind; 393 argv += optind; 394 395 if (!opt_F) { 396 opt_F = malloc(sizeof(struct opt_F_t)); 397 opt_F->next = malloc(sizeof(struct opt_F_t)); 398 opt_F->next->next = NULL; 399 opt_F->filename = USER_CONFIG_FILE; 400 opt_F->next->filename = SYSTEM_CONFIG_FILE; 401 } 402 /* Reverse the config file list since it was built by 403 * prepending, and it would make more sense to process the 404 * files in the order they were given. Probably it would make 405 * more sense to keep a tail pointer instead? */ 406 { 407 struct opt_F_t *egg = NULL; 408 struct opt_F_t *chicken; 409 while (opt_F) { 410 chicken = opt_F->next; 411 opt_F->next = egg; 412 egg = opt_F; 413 opt_F = chicken; 414 } 415 opt_F = egg; 416 } 417 418 if (!opt_p && argc < 1) { 419 fprintf(stderr, "%s: too few arguments\n", progname); 420 err_usage(); 421 } 422 if (opt_r && opt_t) { 423 fprintf(stderr, "%s: Incompatible options -r and -t\n", 424 progname); 425 err_usage(); 426 } 427 if (argc > 0) { 428 command = search_for_command(argv[0]); 429 } 430 return &argv[0]; 431} 432