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