blob: 9a1e298690d747321335dd5fd0501aae691fc859 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Ian Wienand9a2ad352006-02-20 22:44:45 +01005#ifndef PACKAGE_VERSION
6# define PACKAGE_VERSION "0.3.32"
7#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 Wienand2d45b1a2006-02-20 22:48:07 +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 Wienand2d45b1a2006-02-20 22:48:07 +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 Wienand2d45b1a2006-02-20 22:48:07 +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 Wienand2d45b1a2006-02-20 22:48:07 +010050struct opt_e_t *opt_e = NULL;
51int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020052
Ian Wienand9a2ad352006-02-20 22:44:45 +010053/* List of global function names given to -x: */
Paul Gilliam24e643a2006-03-13 18:43:13 +010054struct opt_x_t *opt_x = NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010055
56/* Set a break on the routine named here in order to re-initialize breakpoints
57 after all the PLTs have been initialzed */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058char *PLTs_initialized_by_here = PLTs_INIT_BY_HERE;
Ian Wienand9a2ad352006-02-20 22:44:45 +010059
Ian Wienand2d45b1a2006-02-20 22:48:07 +010060static void usage(void)
61{
Juan Cespedesac3db291998-04-25 14:31:58 +020062#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
63 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010064 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020065#else
66 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010067 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020068# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020070# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010071 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020072# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010074# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020075# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020077# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020079# endif
80# endif
81# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 " -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020083# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 " -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020085# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 " -e expr modify which events to trace.\n"
87 " -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010088# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010090# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010092# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010094# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010096# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010098# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200100# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200102# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200104# endif
105# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200107# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200109# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 " -p PID attach to the process with the process ID pid.\n"
111 " -r print relative timestamps.\n"
112 " -s STRLEN specify the maximum string size to print.\n"
113 " -S display system calls.\n"
114 " -t, -tt, -ttt print absolute timestamps.\n"
115 " -T show the time spent inside each call.\n"
116 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200117# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100118 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200119# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200121# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 " -x NAME treat the global NAME like a library subroutine.\n"
123 " -X NAME same as -x; and PLT's will be initialized by here.\n"
124 "\nReport bugs to Juan Cespedes <cespedes@debian.org>\n",
125 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200126#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100127}
128
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100129static char *search_for_command(char *filename)
130{
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100131 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100132 char *path;
133 int m, n;
134
135 if (strchr(filename, '/')) {
136 return filename;
137 }
138 for (path = getenv("PATH"); path && *path; path += m) {
139 if (strchr(path, ':')) {
140 n = strchr(path, ':') - path;
141 m = n + 1;
142 } else {
143 m = n = strlen(path);
144 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100145 if (n + strlen(filename) + 1 >= PATH_MAX) {
146 fprintf(stderr, "Error: filename too long\n");
147 exit(1);
148 }
149 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100150 if (n && pathname[n - 1] != '/') {
151 pathname[n++] = '/';
152 }
153 strcpy(pathname + n, filename);
154 if (!access(pathname, X_OK)) {
155 return pathname;
156 }
157 }
158 return filename;
159}
160
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100161char **process_options(int argc, char **argv)
162{
Juan Cespedesac3db291998-04-25 14:31:58 +0200163 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100164 output = stderr;
165
Juan Cespedesac3db291998-04-25 14:31:58 +0200166#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100167 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200168 int c;
169#if HAVE_GETOPT_LONG
170 int option_index = 0;
171 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100172 {"align", 1, 0, 'a'},
173 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100174# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100175 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200176#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100177 {"indent", 1, 0, 'n'},
178 {"help", 0, 0, 'h'},
179 {"library", 1, 0, 'l'},
180 {"output", 1, 0, 'o'},
181 {"version", 0, 0, 'V'},
182 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200183 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100184 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100185# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100186 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200187# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100188 "a:e:l:n:o:p:s:u:x:X:", long_options,
189 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200190#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100191 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100192# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200194# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 "a:e:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200196#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200198 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 switch (c) {
201 case 'a':
202 opt_a = atoi(optarg);
203 break;
204 case 'c':
205 opt_c++;
206 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100207#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 case 'C':
209 opt_C++;
210 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200211#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 case 'd':
213 opt_d++;
214 break;
215 case 'e':
216 {
217 char *str_e = strdup(optarg);
218 if (!str_e) {
219 perror("ltrace: strdup");
220 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100221 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100222 if (str_e[0] == '!') {
223 opt_e_enable = 0;
224 str_e++;
225 }
226 while (*str_e) {
227 struct opt_e_t *tmp;
228 char *str2 = strchr(str_e, ',');
229 if (str2) {
230 *str2 = '\0';
231 }
232 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100233 if (!tmp) {
234 perror("ltrace: malloc");
235 exit(1);
236 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100237 tmp->name = str_e;
238 tmp->next = opt_e;
239 opt_e = tmp;
240 if (str2) {
241 str_e = str2 + 1;
242 } else {
243 break;
244 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100245 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100246 break;
247 }
248 case 'f':
249 opt_f = 1;
250 break;
251 case 'h':
252 usage();
253 exit(0);
254 case 'i':
255 opt_i++;
256 break;
257 case 'l':
258 if (library_num == MAX_LIBRARY) {
259 fprintf(stderr,
260 "Too many libraries. Maximum is %i.\n",
261 MAX_LIBRARY);
262 exit(1);
263 }
264 library[library_num++] = optarg;
265 break;
266 case 'L':
267 opt_L = 0;
268 break;
269 case 'n':
270 opt_n = atoi(optarg);
271 break;
272 case 'o':
273 output = fopen(optarg, "w");
274 if (!output) {
275 fprintf(stderr,
276 "Can't open %s for output: %s\n",
277 optarg, strerror(errno));
278 exit(1);
279 }
280 setvbuf(output, (char *)NULL, _IOLBF, 0);
281 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
282 break;
283 case 'p':
284 {
285 struct opt_p_t *tmp =
286 malloc(sizeof(struct opt_p_t));
287 if (!tmp) {
288 perror("ltrace: malloc");
289 exit(1);
290 }
291 tmp->pid = atoi(optarg);
292 tmp->next = opt_p;
293 opt_p = tmp;
294 break;
295 }
296 case 'r':
297 opt_r++;
298 break;
299 case 's':
300 opt_s = atoi(optarg);
301 break;
302 case 'S':
303 opt_S = 1;
304 break;
305 case 't':
306 opt_t++;
307 break;
308 case 'T':
309 opt_T++;
310 break;
311 case 'u':
312 opt_u = optarg;
313 break;
314 case 'V':
315 printf("ltrace version " PACKAGE_VERSION ".\n"
316 "Copyright (C) 1997-2006 Juan Cespedes <cespedes@debian.org>.\n"
317 "This is free software; see the GNU General Public Licence\n"
318 "version 2 or later for copying conditions. There is NO warranty.\n");
319 exit(0);
320 case 'X':
321 PLTs_initialized_by_here = optarg;
322 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200323
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 case 'x':
325 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100326 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100327
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100328 /* First, check for duplicate. */
329 while (p && strcmp(p->name, optarg)) {
330 p = p->next;
331 }
332 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100333 break;
334 }
335
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100336 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100337 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100338 if (!p) {
339 perror("ltrace: malloc");
340 exit(1);
341 }
342 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100343 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 p->next = opt_x;
345 opt_x = p;
346 break;
347 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100348
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200350#if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100351 fprintf(stderr,
352 "Try `%s --help' for more information\n",
353 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200354#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 fprintf(stderr, "Try `%s -h' for more information\n",
356 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200357#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100359 }
360 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 argc -= optind;
362 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200363#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100364
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200366 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200367 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100368 exit(1);
369 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200370 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 fprintf(stderr, "%s: Incompatible options -r and -t\n",
372 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200373 exit(1);
374 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200376 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100377 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200378 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100379}