blob: 4f02a06ec90a0f990e80b7b271544b955084127d [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include <string.h>
6#include <stdlib.h>
7#include <unistd.h>
Juan Cespedes1b9cfd61999-08-30 19:34:50 +02008#include <fcntl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <errno.h>
10#include <limits.h>
11
Juan Cespedesac3db291998-04-25 14:31:58 +020012#if HAVE_GETOPT_H
13#include <getopt.h>
14#endif
15
Juan Cespedes5e01f651998-03-08 22:31:44 +010016#include "ltrace.h"
17#include "options.h"
18#include "defs.h"
19
Juan Cespedes1cd999a2001-07-03 00:46:04 +020020#define MAX_LIBRARY 30
21char *library[MAX_LIBRARY];
22int library_num = 0;
Juan Cespedesac3db291998-04-25 14:31:58 +020023static char *progname; /* Program name (`ltrace') */
Ian Wienand3219f322006-02-16 06:00:00 +010024FILE *output;
Juan Cespedes5e01f651998-03-08 22:31:44 +010025int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
Juan Cespedesd65efa32003-02-03 00:22:30 +010026int opt_c = 0; /* Count time, calls, and report a summary on program exit */
Juan Cespedes5e01f651998-03-08 22:31:44 +010027int opt_d = 0; /* debug */
28int opt_i = 0; /* instruction pointer */
29int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
30int opt_S = 0; /* display syscalls */
31int opt_L = 1; /* display library calls */
32int opt_f = 0; /* trace child processes as they are created */
Ian Wienand3219f322006-02-16 06:00:00 +010033char *opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020034int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020035int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd914a202004-11-10 00:15:33 +010036#ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020037int opt_C = 0; /* Demangle low-level symbol names into user-level names */
38#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020039int opt_n = 0; /* indent trace output according to program flow */
Juan Cespedesd65efa32003-02-03 00:22:30 +010040int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010041
42/* List of pids given to option -p: */
Ian Wienand3219f322006-02-16 06:00:00 +010043struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010044
Juan Cespedesac3db291998-04-25 14:31:58 +020045/* List of function names given to option -e: */
Ian Wienand3219f322006-02-16 06:00:00 +010046struct opt_e_t *opt_e = NULL;
47int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020048
Ian Wienand3219f322006-02-16 06:00:00 +010049static void usage(void)
50{
Juan Cespedesac3db291998-04-25 14:31:58 +020051#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
52 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand3219f322006-02-16 06:00:00 +010053 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020054#else
55 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand3219f322006-02-16 06:00:00 +010056 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020057# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010058 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020059# else
Ian Wienand3219f322006-02-16 06:00:00 +010060 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020061# endif
Ian Wienand3219f322006-02-16 06:00:00 +010062 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010063# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020064# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010065 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020066# else
Ian Wienand3219f322006-02-16 06:00:00 +010067 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020068# endif
69# endif
70# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010071 " -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020072# else
Ian Wienand3219f322006-02-16 06:00:00 +010073 " -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020074# endif
Ian Wienand3219f322006-02-16 06:00:00 +010075 " -e expr modify which events to trace.\n"
76 " -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010077# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010078 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010079# else
Ian Wienand3219f322006-02-16 06:00:00 +010080 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010081# endif
Ian Wienand3219f322006-02-16 06:00:00 +010082 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010083# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010084 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010085# else
Ian Wienand3219f322006-02-16 06:00:00 +010086 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010087# endif
Ian Wienand3219f322006-02-16 06:00:00 +010088 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020089# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010090 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020091# else
Ian Wienand3219f322006-02-16 06:00:00 +010092 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020093# endif
94# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010095 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020096# else
Ian Wienand3219f322006-02-16 06:00:00 +010097 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020098# endif
Ian Wienand3219f322006-02-16 06:00:00 +010099 " -p PID attach to the process with the process ID pid.\n"
100 " -r print relative timestamps.\n"
101 " -s STRLEN specify the maximum string size to print.\n"
102 " -S display system calls.\n"
103 " -t, -tt, -ttt print absolute timestamps.\n"
104 " -T show the time spent inside each call.\n"
105 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200106# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +0100107 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200108# else
Ian Wienand3219f322006-02-16 06:00:00 +0100109 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200110# endif
Ian Wienand3219f322006-02-16 06:00:00 +0100111 "\nReport bugs to Juan Cespedes <cespedes@debian.org>\n",
112 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200113#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100114}
115
Ian Wienand3219f322006-02-16 06:00:00 +0100116static char *search_for_command(char *filename)
117{
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100118 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100119 char *path;
120 int m, n;
121
122 if (strchr(filename, '/')) {
123 return filename;
124 }
125 for (path = getenv("PATH"); path && *path; path += m) {
126 if (strchr(path, ':')) {
127 n = strchr(path, ':') - path;
128 m = n + 1;
129 } else {
130 m = n = strlen(path);
131 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100132 if (n + strlen(filename) + 1 >= PATH_MAX) {
133 fprintf(stderr, "Error: filename too long\n");
134 exit(1);
135 }
136 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100137 if (n && pathname[n - 1] != '/') {
138 pathname[n++] = '/';
139 }
140 strcpy(pathname + n, filename);
141 if (!access(pathname, X_OK)) {
142 return pathname;
143 }
144 }
145 return filename;
146}
147
Ian Wienand3219f322006-02-16 06:00:00 +0100148char **process_options(int argc, char **argv)
149{
Juan Cespedesac3db291998-04-25 14:31:58 +0200150 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100151 output = stderr;
152
Juan Cespedesac3db291998-04-25 14:31:58 +0200153#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +0100154 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200155 int c;
156#if HAVE_GETOPT_LONG
157 int option_index = 0;
158 static struct option long_options[] = {
Ian Wienand3219f322006-02-16 06:00:00 +0100159 {"align", 1, 0, 'a'},
160 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100161# ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100162 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200163#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100164 {"indent", 1, 0, 'n'},
165 {"help", 0, 0, 'h'},
166 {"library", 1, 0, 'l'},
167 {"output", 1, 0, 'o'},
168 {"version", 0, 0, 'V'},
169 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200170 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100171 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100172# ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100173 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200174# endif
Ian Wienand3219f322006-02-16 06:00:00 +0100175 "a:e:l:n:o:p:s:u:", long_options,
176 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200177#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100178 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100179# ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100180 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200181# endif
Ian Wienand3219f322006-02-16 06:00:00 +0100182 "a:e:l:n:o:p:s:u:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200183#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100184 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200185 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100186 }
Ian Wienand3219f322006-02-16 06:00:00 +0100187 switch (c) {
188 case 'a':
189 opt_a = atoi(optarg);
190 break;
191 case 'c':
192 opt_c++;
193 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100194#ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100195 case 'C':
196 opt_C++;
197 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200198#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100199 case 'd':
200 opt_d++;
201 break;
202 case 'e':
203 {
204 char *str_e = strdup(optarg);
205 if (!str_e) {
206 perror("ltrace: strdup");
207 exit(1);
Juan Cespedesac3db291998-04-25 14:31:58 +0200208 }
Ian Wienand3219f322006-02-16 06:00:00 +0100209 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));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100220 if (!tmp) {
221 perror("ltrace: malloc");
222 exit(1);
223 }
Ian Wienand3219f322006-02-16 06:00:00 +0100224 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 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100232 }
Ian Wienand3219f322006-02-16 06:00:00 +0100233 break;
234 }
235 case 'f':
236 opt_f = 1;
237 break;
238 case 'h':
239 usage();
240 exit(0);
241 case 'i':
242 opt_i++;
243 break;
244 case 'l':
245 if (library_num == MAX_LIBRARY) {
246 fprintf(stderr,
247 "Too many libraries. Maximum is %i.\n",
248 MAX_LIBRARY);
249 exit(1);
250 }
251 library[library_num++] = optarg;
252 break;
253 case 'L':
254 opt_L = 0;
255 break;
256 case 'n':
257 opt_n = atoi(optarg);
258 break;
259 case 'o':
260 output = fopen(optarg, "w");
261 if (!output) {
262 fprintf(stderr,
263 "Can't open %s for output: %s\n",
264 optarg, strerror(errno));
265 exit(1);
266 }
267 setvbuf(output, (char *)NULL, _IOLBF, 0);
268 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
269 break;
270 case 'p':
271 {
272 struct opt_p_t *tmp =
273 malloc(sizeof(struct opt_p_t));
274 if (!tmp) {
275 perror("ltrace: malloc");
276 exit(1);
277 }
278 tmp->pid = atoi(optarg);
279 tmp->next = opt_p;
280 opt_p = tmp;
281 break;
282 }
283 case 'r':
284 opt_r++;
285 break;
286 case 's':
287 opt_s = atoi(optarg);
288 break;
289 case 'S':
290 opt_S = 1;
291 break;
292 case 't':
293 opt_t++;
294 break;
295 case 'T':
296 opt_T++;
297 break;
298 case 'u':
299 opt_u = optarg;
300 break;
301 case 'V':
Ian Wienand5570a772006-02-17 02:00:00 +0100302 printf("ltrace version " PACKAGE_VERSION ".\n"
303 "Copyright (C) 1997-2006 Juan Cespedes <cespedes@debian.org>.\n"
Ian Wienand3219f322006-02-16 06:00:00 +0100304 "This is free software; see the GNU General Public Licence\n"
305 "version 2 or later for copying conditions. There is NO warranty.\n");
306 exit(0);
Juan Cespedesac3db291998-04-25 14:31:58 +0200307
Ian Wienand3219f322006-02-16 06:00:00 +0100308 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200309#if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +0100310 fprintf(stderr,
311 "Try `%s --help' for more information\n",
312 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200313#else
Ian Wienand3219f322006-02-16 06:00:00 +0100314 fprintf(stderr, "Try `%s -h' for more information\n",
315 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200316#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100317 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100318 }
319 }
Ian Wienand3219f322006-02-16 06:00:00 +0100320 argc -= optind;
321 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200322#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100323
Ian Wienand3219f322006-02-16 06:00:00 +0100324 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200325 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200326 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100327 exit(1);
328 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200329 if (opt_r && opt_t) {
Ian Wienand3219f322006-02-16 06:00:00 +0100330 fprintf(stderr, "%s: Incompatible options -r and -t\n",
331 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200332 exit(1);
333 }
Ian Wienand3219f322006-02-16 06:00:00 +0100334 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200335 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100336 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200337 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100338}