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