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