options.c revision de5a7eb873c05a698e4267b554e25124dc92e7f4
1#if HAVE_CONFIG_H 2#include "config.h" 3#endif 4 5#ifndef VERSION 6# define VERSION "0.3.26" 7#endif 8 9#include <string.h> 10#include <stdlib.h> 11#include <unistd.h> 12#include <fcntl.h> 13#include <errno.h> 14#include <limits.h> 15 16#if HAVE_GETOPT_H 17#include <getopt.h> 18#endif 19 20#include "ltrace.h" 21#include "options.h" 22#include "defs.h" 23 24#define MAX_LIBRARY 30 25char *library[MAX_LIBRARY]; 26int library_num = 0; 27static char *progname; /* Program name (`ltrace') */ 28FILE * output; 29int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */ 30int opt_d = 0; /* debug */ 31int opt_i = 0; /* instruction pointer */ 32int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */ 33int opt_S = 0; /* display syscalls */ 34int opt_L = 1; /* display library calls */ 35int opt_f = 0; /* trace child processes as they are created */ 36char * opt_u = NULL; /* username to run command as */ 37int opt_r = 0; /* print relative timestamp */ 38int opt_t = 0; /* print absolute timestamp */ 39#if HAVE_LIBIBERTY 40int opt_C = 0; /* Demangle low-level symbol names into user-level names */ 41#endif 42int opt_n = 0; /* indent trace output according to program flow */ 43 44/* List of pids given to option -p: */ 45struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */ 46 47/* List of function names given to option -e: */ 48struct opt_e_t * opt_e = NULL; 49int opt_e_enable=1; 50 51static void 52usage(void) { 53#if !(HAVE_GETOPT || HAVE_GETOPT_LONG) 54 fprintf(stdout, "Usage: %s [command [arg ...]]\n" 55"Trace library calls of a given program.\n\n", progname); 56#else 57 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n" 58"Trace library calls of a given program.\n\n" 59 60# if HAVE_GETOPT_LONG 61" -d, --debug print debugging info.\n" 62# else 63" -d print debugging info.\n" 64# endif 65" -f follow forks.\n" 66" -i print instruction pointer at time of library call.\n" 67" -L do NOT display library calls.\n" 68" -S display system calls.\n" 69" -r print relative timestamps.\n" 70" -t, -tt, -ttt print absolute timestamps.\n" 71# if HAVE_GETOPT_LONG 72" -l, --library=FILE print library calls from this library only.\n" 73# else 74" -l FILE print library calls from this library only.\n" 75# endif 76# if HAVE_LIBIBERTY 77# if HAVE_GETOPT_LONG 78" -C, --demangle decode low-level symbol names into user-level names.\n" 79# else 80" -C decode low-level symbol names into user-level names.\n" 81# endif 82# endif 83# if HAVE_GETOPT_LONG 84" -a, --align=COLUMN align return values in a secific column.\n" 85# else 86" -a COLUMN align return values in a secific column.\n" 87# endif 88" -s STRLEN specify the maximum string size to print.\n" 89# if HAVE_GETOPT_LONG 90" -o, --output=FILE write the trace output to that file.\n" 91# else 92" -o FILE write the trace output to that file.\n" 93# endif 94" -u USERNAME run command with the userid, groupid of username.\n" 95" -p PID attach to the process with the process ID pid.\n" 96" -e expr modify which events to trace.\n" 97# if HAVE_GETOPT_LONG 98" -n, --indent=NR indent output by NR spaces for each call level nesting.\n" 99# else 100" -n NR indent output by NR spaces for each call level nesting.\n" 101# endif 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# if HAVE_GETOPT_LONG 108" -V, --version output version information and exit.\n" 109# else 110" -V output version information and exit.\n" 111# endif 112"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n" 113 , progname); 114#endif 115} 116 117static char * 118search_for_command(char * filename) { 119 static char pathname[PATH_MAX]; 120 char *path; 121 int m, n; 122 123 if (strchr(filename, '/')) { 124 return filename; 125 } 126 for (path = getenv("PATH"); path && *path; path += m) { 127 if (strchr(path, ':')) { 128 n = strchr(path, ':') - path; 129 m = n + 1; 130 } else { 131 m = n = strlen(path); 132 } 133 if (n + strlen(filename) + 1 >= PATH_MAX) { 134 fprintf(stderr, "Error: filename too long\n"); 135 exit(1); 136 } 137 strncpy(pathname, path, n); 138 if (n && pathname[n - 1] != '/') { 139 pathname[n++] = '/'; 140 } 141 strcpy(pathname + n, filename); 142 if (!access(pathname, X_OK)) { 143 return pathname; 144 } 145 } 146 return filename; 147} 148 149char ** 150process_options(int argc, char **argv) { 151 progname = argv[0]; 152 output = stderr; 153 154#if HAVE_GETOPT || HAVE_GETOPT_LONG 155 while(1) { 156 int c; 157#if HAVE_GETOPT_LONG 158 int option_index = 0; 159 static struct option long_options[] = { 160 { "align", 1, 0, 'a'}, 161 { "debug", 0, 0, 'd'}, 162# if HAVE_LIBIBERTY 163 { "demangle", 0, 0, 'C'}, 164#endif 165 { "indent", 0, 0, 'n'}, 166 { "library", 0, 0, 'l'}, 167 { "help", 0, 0, 'h'}, 168 { "output", 1, 0, 'o'}, 169 { "version", 0, 0, 'V'}, 170 { 0, 0, 0, 0} 171 }; 172 c = getopt_long(argc, argv, "+dfiLSrthV" 173# if HAVE_LIBIBERTY 174 "C" 175# endif 176 "a:s:o:u:p:e:n:l:", long_options, &option_index); 177#else 178 c = getopt(argc, argv, "+dfiLSrthV" 179# if HAVE_LIBIBERTY 180 "C" 181# endif 182 "a:s:o:u:p:e:n:l:"); 183#endif 184 if (c==-1) { 185 break; 186 } 187 switch(c) { 188 case 'l': if (library_num == MAX_LIBRARY) { 189 fprintf(stderr, "Too many libraries. Maximum is %i.\n", MAX_LIBRARY); 190 exit(1); 191 } 192 library[library_num++] = optarg; 193 break; 194 case 'd': opt_d++; 195 break; 196 case 'f': opt_f = 1; 197 break; 198 case 'i': opt_i++; 199 break; 200 case 'L': opt_L = 0; 201 break; 202 case 'S': opt_S = 1; 203 break; 204 case 'r': opt_r++; 205 break; 206 case 't': opt_t++; 207 break; 208#if HAVE_LIBIBERTY 209 case 'C': opt_C++; 210 break; 211#endif 212 case 'a': opt_a = atoi(optarg); 213 break; 214 case 's': opt_s = atoi(optarg); 215 break; 216 case 'n': opt_n = atoi(optarg); 217 break; 218 case 'h': usage(); 219 exit(0); 220 case 'o': output = fopen(optarg, "w"); 221 if (!output) { 222 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno)); 223 exit(1); 224 } 225 setvbuf(output, (char *)NULL, _IOLBF, 0); 226 fcntl(fileno(output), F_SETFD, FD_CLOEXEC); 227 break; 228 case 'u': opt_u = optarg; 229 break; 230 case 'p': 231 { 232 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t)); 233 if (!tmp) { 234 perror("ltrace: malloc"); 235 exit(1); 236 } 237 tmp->pid = atoi(optarg); 238 tmp->next = opt_p; 239 opt_p = tmp; 240 break; 241 } 242 case 'e': 243 { 244 char * str_e = strdup(optarg); 245 if (!str_e) { 246 perror("ltrace: strdup"); 247 exit(1); 248 } 249 if (str_e[0]=='!') { 250 opt_e_enable=0; 251 str_e++; 252 } 253 while(*str_e) { 254 struct opt_e_t * tmp; 255 char *str2 = strchr(str_e,','); 256 if (str2) { 257 *str2 = '\0'; 258 } 259 tmp = malloc(sizeof(struct opt_e_t)); 260 if (!tmp) { 261 perror("ltrace: malloc"); 262 exit(1); 263 } 264 tmp->name = str_e; 265 tmp->next = opt_e; 266 opt_e = tmp; 267 if (str2) { 268 str_e = str2+1; 269 } else { 270 break; 271 } 272 } 273 break; 274 } 275 case 'V': printf("ltrace version " VERSION ".\n" 276"Copyright (C) 1997-2002 Juan Cespedes <cespedes@debian.org>.\n" 277"This is free software; see the GNU General Public Licence\n" 278"version 2 or later for copying conditions. There is NO warranty.\n"); 279 exit(0); 280 281 default: 282#if HAVE_GETOPT_LONG 283 fprintf(stderr, "Try `%s --help' for more information\n", progname); 284#else 285 fprintf(stderr, "Try `%s -h' for more information\n", progname); 286#endif 287 exit(1); 288 } 289 } 290 argc -= optind; argv += optind; 291#endif 292 293 if (!opt_p && argc<1) { 294 fprintf(stderr, "%s: too few arguments\n", progname); 295#if HAVE_GETOPT_LONG 296 fprintf(stderr, "Try `%s --help' for more information\n", progname); 297#elif HAVE_GETOPT 298 fprintf(stderr, "Try `%s -h' for more information\n", progname); 299#endif 300 exit(1); 301 } 302 if (opt_r && opt_t) { 303 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname); 304 exit(1); 305 } 306 if (argc>0) { 307 command = search_for_command(argv[0]); 308 } 309 return &argv[0]; 310} 311