blob: 24e548e9f9463e475d90679b5af71edb7aca4fe4 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes1cd999a2001-07-03 00:46:04 +02005#ifndef VERSION
Juan Cespedesefe85f02004-04-04 01:31:38 +02006# define VERSION "0.3.32"
Juan Cespedes1cd999a2001-07-03 00:46:04 +02007#endif
8
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <string.h>
10#include <stdlib.h>
11#include <unistd.h>
Juan Cespedes1b9cfd61999-08-30 19:34:50 +020012#include <fcntl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010013#include <errno.h>
14#include <limits.h>
15
Juan Cespedesac3db291998-04-25 14:31:58 +020016#if HAVE_GETOPT_H
17#include <getopt.h>
18#endif
19
Juan Cespedes5e01f651998-03-08 22:31:44 +010020#include "ltrace.h"
21#include "options.h"
22#include "defs.h"
23
Juan Cespedes1cd999a2001-07-03 00:46:04 +020024#define MAX_LIBRARY 30
25char *library[MAX_LIBRARY];
26int library_num = 0;
Juan Cespedesac3db291998-04-25 14:31:58 +020027static char *progname; /* Program name (`ltrace') */
Ian Wienand3219f322006-02-16 06:00:00 +010028FILE *output;
Juan Cespedes5e01f651998-03-08 22:31:44 +010029int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
Juan Cespedesd65efa32003-02-03 00:22:30 +010030int opt_c = 0; /* Count time, calls, and report a summary on program exit */
Juan Cespedes5e01f651998-03-08 22:31:44 +010031int opt_d = 0; /* debug */
32int opt_i = 0; /* instruction pointer */
33int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
34int opt_S = 0; /* display syscalls */
35int opt_L = 1; /* display library calls */
36int opt_f = 0; /* trace child processes as they are created */
Ian Wienand3219f322006-02-16 06:00:00 +010037char *opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020038int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020039int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd914a202004-11-10 00:15:33 +010040#ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020041int opt_C = 0; /* Demangle low-level symbol names into user-level names */
42#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020043int opt_n = 0; /* indent trace output according to program flow */
Juan Cespedesd65efa32003-02-03 00:22:30 +010044int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010045
46/* List of pids given to option -p: */
Ian Wienand3219f322006-02-16 06:00:00 +010047struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010048
Juan Cespedesac3db291998-04-25 14:31:58 +020049/* List of function names given to option -e: */
Ian Wienand3219f322006-02-16 06:00:00 +010050struct opt_e_t *opt_e = NULL;
51int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020052
Ian Wienand3219f322006-02-16 06:00:00 +010053static void usage(void)
54{
Juan Cespedesac3db291998-04-25 14:31:58 +020055#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
56 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand3219f322006-02-16 06:00:00 +010057 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020058#else
59 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand3219f322006-02-16 06:00:00 +010060 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020061# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010062 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020063# else
Ian Wienand3219f322006-02-16 06:00:00 +010064 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020065# endif
Ian Wienand3219f322006-02-16 06:00:00 +010066 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010067# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020068# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010069 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020070# else
Ian Wienand3219f322006-02-16 06:00:00 +010071 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020072# endif
73# endif
74# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010075 " -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020076# else
Ian Wienand3219f322006-02-16 06:00:00 +010077 " -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020078# endif
Ian Wienand3219f322006-02-16 06:00:00 +010079 " -e expr modify which events to trace.\n"
80 " -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010081# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010082 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010083# else
Ian Wienand3219f322006-02-16 06:00:00 +010084 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010085# endif
Ian Wienand3219f322006-02-16 06:00:00 +010086 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010087# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010088 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010089# else
Ian Wienand3219f322006-02-16 06:00:00 +010090 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010091# endif
Ian Wienand3219f322006-02-16 06:00:00 +010092 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020093# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010094 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020095# else
Ian Wienand3219f322006-02-16 06:00:00 +010096 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020097# endif
98# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +010099 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200100# else
Ian Wienand3219f322006-02-16 06:00:00 +0100101 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200102# endif
Ian Wienand3219f322006-02-16 06:00:00 +0100103 " -p PID attach to the process with the process ID pid.\n"
104 " -r print relative timestamps.\n"
105 " -s STRLEN specify the maximum string size to print.\n"
106 " -S display system calls.\n"
107 " -t, -tt, -ttt print absolute timestamps.\n"
108 " -T show the time spent inside each call.\n"
109 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200110# if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +0100111 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200112# else
Ian Wienand3219f322006-02-16 06:00:00 +0100113 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200114# endif
Ian Wienand3219f322006-02-16 06:00:00 +0100115 "\nReport bugs to Juan Cespedes <cespedes@debian.org>\n",
116 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200117#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100118}
119
Ian Wienand3219f322006-02-16 06:00:00 +0100120static char *search_for_command(char *filename)
121{
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100122 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100123 char *path;
124 int m, n;
125
126 if (strchr(filename, '/')) {
127 return filename;
128 }
129 for (path = getenv("PATH"); path && *path; path += m) {
130 if (strchr(path, ':')) {
131 n = strchr(path, ':') - path;
132 m = n + 1;
133 } else {
134 m = n = strlen(path);
135 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100136 if (n + strlen(filename) + 1 >= PATH_MAX) {
137 fprintf(stderr, "Error: filename too long\n");
138 exit(1);
139 }
140 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100141 if (n && pathname[n - 1] != '/') {
142 pathname[n++] = '/';
143 }
144 strcpy(pathname + n, filename);
145 if (!access(pathname, X_OK)) {
146 return pathname;
147 }
148 }
149 return filename;
150}
151
Ian Wienand3219f322006-02-16 06:00:00 +0100152char **process_options(int argc, char **argv)
153{
Juan Cespedesac3db291998-04-25 14:31:58 +0200154 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100155 output = stderr;
156
Juan Cespedesac3db291998-04-25 14:31:58 +0200157#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +0100158 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200159 int c;
160#if HAVE_GETOPT_LONG
161 int option_index = 0;
162 static struct option long_options[] = {
Ian Wienand3219f322006-02-16 06:00:00 +0100163 {"align", 1, 0, 'a'},
164 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100165# ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100166 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200167#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100168 {"indent", 1, 0, 'n'},
169 {"help", 0, 0, 'h'},
170 {"library", 1, 0, 'l'},
171 {"output", 1, 0, 'o'},
172 {"version", 0, 0, 'V'},
173 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200174 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100175 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100176# ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100177 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200178# endif
Ian Wienand3219f322006-02-16 06:00:00 +0100179 "a:e:l:n:o:p:s:u:", long_options,
180 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200181#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100182 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100183# ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100184 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200185# endif
Ian Wienand3219f322006-02-16 06:00:00 +0100186 "a:e:l:n:o:p:s:u:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200187#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100188 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200189 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 }
Ian Wienand3219f322006-02-16 06:00:00 +0100191 switch (c) {
192 case 'a':
193 opt_a = atoi(optarg);
194 break;
195 case 'c':
196 opt_c++;
197 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100198#ifdef USE_DEMANGLE
Ian Wienand3219f322006-02-16 06:00:00 +0100199 case 'C':
200 opt_C++;
201 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200202#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100203 case 'd':
204 opt_d++;
205 break;
206 case 'e':
207 {
208 char *str_e = strdup(optarg);
209 if (!str_e) {
210 perror("ltrace: strdup");
211 exit(1);
Juan Cespedesac3db291998-04-25 14:31:58 +0200212 }
Ian Wienand3219f322006-02-16 06:00:00 +0100213 if (str_e[0] == '!') {
214 opt_e_enable = 0;
215 str_e++;
216 }
217 while (*str_e) {
218 struct opt_e_t *tmp;
219 char *str2 = strchr(str_e, ',');
220 if (str2) {
221 *str2 = '\0';
222 }
223 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100224 if (!tmp) {
225 perror("ltrace: malloc");
226 exit(1);
227 }
Ian Wienand3219f322006-02-16 06:00:00 +0100228 tmp->name = str_e;
229 tmp->next = opt_e;
230 opt_e = tmp;
231 if (str2) {
232 str_e = str2 + 1;
233 } else {
234 break;
235 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100236 }
Ian Wienand3219f322006-02-16 06:00:00 +0100237 break;
238 }
239 case 'f':
240 opt_f = 1;
241 break;
242 case 'h':
243 usage();
244 exit(0);
245 case 'i':
246 opt_i++;
247 break;
248 case 'l':
249 if (library_num == MAX_LIBRARY) {
250 fprintf(stderr,
251 "Too many libraries. Maximum is %i.\n",
252 MAX_LIBRARY);
253 exit(1);
254 }
255 library[library_num++] = optarg;
256 break;
257 case 'L':
258 opt_L = 0;
259 break;
260 case 'n':
261 opt_n = atoi(optarg);
262 break;
263 case 'o':
264 output = fopen(optarg, "w");
265 if (!output) {
266 fprintf(stderr,
267 "Can't open %s for output: %s\n",
268 optarg, strerror(errno));
269 exit(1);
270 }
271 setvbuf(output, (char *)NULL, _IOLBF, 0);
272 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
273 break;
274 case 'p':
275 {
276 struct opt_p_t *tmp =
277 malloc(sizeof(struct opt_p_t));
278 if (!tmp) {
279 perror("ltrace: malloc");
280 exit(1);
281 }
282 tmp->pid = atoi(optarg);
283 tmp->next = opt_p;
284 opt_p = tmp;
285 break;
286 }
287 case 'r':
288 opt_r++;
289 break;
290 case 's':
291 opt_s = atoi(optarg);
292 break;
293 case 'S':
294 opt_S = 1;
295 break;
296 case 't':
297 opt_t++;
298 break;
299 case 'T':
300 opt_T++;
301 break;
302 case 'u':
303 opt_u = optarg;
304 break;
305 case 'V':
306 printf("ltrace version " VERSION ".\n"
307 "Copyright (C) 1997-2004 Juan Cespedes <cespedes@debian.org>.\n"
308 "This is free software; see the GNU General Public Licence\n"
309 "version 2 or later for copying conditions. There is NO warranty.\n");
310 exit(0);
Juan Cespedesac3db291998-04-25 14:31:58 +0200311
Ian Wienand3219f322006-02-16 06:00:00 +0100312 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200313#if HAVE_GETOPT_LONG
Ian Wienand3219f322006-02-16 06:00:00 +0100314 fprintf(stderr,
315 "Try `%s --help' for more information\n",
316 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200317#else
Ian Wienand3219f322006-02-16 06:00:00 +0100318 fprintf(stderr, "Try `%s -h' for more information\n",
319 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200320#endif
Ian Wienand3219f322006-02-16 06:00:00 +0100321 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100322 }
323 }
Ian Wienand3219f322006-02-16 06:00:00 +0100324 argc -= optind;
325 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200326#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100327
Ian Wienand3219f322006-02-16 06:00:00 +0100328 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200329 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200330 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100331 exit(1);
332 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200333 if (opt_r && opt_t) {
Ian Wienand3219f322006-02-16 06:00:00 +0100334 fprintf(stderr, "%s: Incompatible options -r and -t\n",
335 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200336 exit(1);
337 }
Ian Wienand3219f322006-02-16 06:00:00 +0100338 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200339 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100340 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200341 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100342}