blob: b6dabab532080edaba22fb92d2b0aca9dcd5f2fa [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
Paul Gilliambe320772006-04-24 22:06:23 +020056#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010057/* Set a break on the routine named here in order to re-initialize breakpoints
58 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020059char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
60#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010061
Ian Wienand2d45b1a2006-02-20 22:48:07 +010062static void usage(void)
63{
Juan Cespedesac3db291998-04-25 14:31:58 +020064#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
65 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020067#else
68 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020070# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010071 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020072# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020074# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010076# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020077# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020079# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020081# endif
82# endif
83# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 " -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020085# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 " -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020087# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 " -e expr modify which events to trace.\n"
89 " -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010090# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010092# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010094# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010096# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010098# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100100# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200102# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200104# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200106# endif
107# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200109# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200111# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 " -p PID attach to the process with the process ID pid.\n"
113 " -r print relative timestamps.\n"
114 " -s STRLEN specify the maximum string size to print.\n"
115 " -S display system calls.\n"
116 " -t, -tt, -ttt print absolute timestamps.\n"
117 " -T show the time spent inside each call.\n"
118 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200119# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200121# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200123# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100124 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200125#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200127#endif
128 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100129 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200130#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100131}
132
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100133static char *search_for_command(char *filename)
134{
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100135 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 char *path;
137 int m, n;
138
139 if (strchr(filename, '/')) {
140 return filename;
141 }
142 for (path = getenv("PATH"); path && *path; path += m) {
143 if (strchr(path, ':')) {
144 n = strchr(path, ':') - path;
145 m = n + 1;
146 } else {
147 m = n = strlen(path);
148 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100149 if (n + strlen(filename) + 1 >= PATH_MAX) {
150 fprintf(stderr, "Error: filename too long\n");
151 exit(1);
152 }
153 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100154 if (n && pathname[n - 1] != '/') {
155 pathname[n++] = '/';
156 }
157 strcpy(pathname + n, filename);
158 if (!access(pathname, X_OK)) {
159 return pathname;
160 }
161 }
162 return filename;
163}
164
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100165char **process_options(int argc, char **argv)
166{
Juan Cespedesac3db291998-04-25 14:31:58 +0200167 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100168 output = stderr;
169
Juan Cespedesac3db291998-04-25 14:31:58 +0200170#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100171 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200172 int c;
173#if HAVE_GETOPT_LONG
174 int option_index = 0;
175 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100176 {"align", 1, 0, 'a'},
177 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100178# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100179 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200180#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 {"indent", 1, 0, 'n'},
182 {"help", 0, 0, 'h'},
183 {"library", 1, 0, 'l'},
184 {"output", 1, 0, 'o'},
185 {"version", 0, 0, 'V'},
186 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200187 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100188 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100189# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100190 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200191# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100192 "a:e:l:n:o:p:s:u:x:X:", long_options,
193 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200194#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100195 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100196# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200198# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100199 "a:e:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200200#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200202 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100203 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100204 switch (c) {
205 case 'a':
206 opt_a = atoi(optarg);
207 break;
208 case 'c':
209 opt_c++;
210 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100211#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 case 'C':
213 opt_C++;
214 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200215#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100216 case 'd':
217 opt_d++;
218 break;
219 case 'e':
220 {
221 char *str_e = strdup(optarg);
222 if (!str_e) {
223 perror("ltrace: strdup");
224 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100225 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 if (str_e[0] == '!') {
227 opt_e_enable = 0;
228 str_e++;
229 }
230 while (*str_e) {
231 struct opt_e_t *tmp;
232 char *str2 = strchr(str_e, ',');
233 if (str2) {
234 *str2 = '\0';
235 }
236 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100237 if (!tmp) {
238 perror("ltrace: malloc");
239 exit(1);
240 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100241 tmp->name = str_e;
242 tmp->next = opt_e;
243 opt_e = tmp;
244 if (str2) {
245 str_e = str2 + 1;
246 } else {
247 break;
248 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100249 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 break;
251 }
252 case 'f':
253 opt_f = 1;
254 break;
255 case 'h':
256 usage();
257 exit(0);
258 case 'i':
259 opt_i++;
260 break;
261 case 'l':
262 if (library_num == MAX_LIBRARY) {
263 fprintf(stderr,
264 "Too many libraries. Maximum is %i.\n",
265 MAX_LIBRARY);
266 exit(1);
267 }
268 library[library_num++] = optarg;
269 break;
270 case 'L':
271 opt_L = 0;
272 break;
273 case 'n':
274 opt_n = atoi(optarg);
275 break;
276 case 'o':
277 output = fopen(optarg, "w");
278 if (!output) {
279 fprintf(stderr,
280 "Can't open %s for output: %s\n",
281 optarg, strerror(errno));
282 exit(1);
283 }
284 setvbuf(output, (char *)NULL, _IOLBF, 0);
285 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
286 break;
287 case 'p':
288 {
289 struct opt_p_t *tmp =
290 malloc(sizeof(struct opt_p_t));
291 if (!tmp) {
292 perror("ltrace: malloc");
293 exit(1);
294 }
295 tmp->pid = atoi(optarg);
296 tmp->next = opt_p;
297 opt_p = tmp;
298 break;
299 }
300 case 'r':
301 opt_r++;
302 break;
303 case 's':
304 opt_s = atoi(optarg);
305 break;
306 case 'S':
307 opt_S = 1;
308 break;
309 case 't':
310 opt_t++;
311 break;
312 case 'T':
313 opt_T++;
314 break;
315 case 'u':
316 opt_u = optarg;
317 break;
318 case 'V':
319 printf("ltrace version " PACKAGE_VERSION ".\n"
320 "Copyright (C) 1997-2006 Juan Cespedes <cespedes@debian.org>.\n"
321 "This is free software; see the GNU General Public Licence\n"
322 "version 2 or later for copying conditions. There is NO warranty.\n");
323 exit(0);
324 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200325#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200327#else
328 fprintf(stderr, "WANRING: \"-X\" not used for this "
329 "architecture: assuming you meant \"-x\"\n");
330#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200332
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100333 case 'x':
334 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100335 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100336
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100337 /* First, check for duplicate. */
338 while (p && strcmp(p->name, optarg)) {
339 p = p->next;
340 }
341 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100342 break;
343 }
344
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100346 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100347 if (!p) {
348 perror("ltrace: malloc");
349 exit(1);
350 }
351 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100352 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100353 p->next = opt_x;
354 opt_x = p;
355 break;
356 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100357
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200359#if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 fprintf(stderr,
361 "Try `%s --help' for more information\n",
362 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200363#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 fprintf(stderr, "Try `%s -h' for more information\n",
365 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200366#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100367 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100368 }
369 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 argc -= optind;
371 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200372#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100373
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200375 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200376 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100377 exit(1);
378 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200379 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100380 fprintf(stderr, "%s: Incompatible options -r and -t\n",
381 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200382 exit(1);
383 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200385 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100386 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200387 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100388}