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