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