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