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