blob: 63f2ff34a4dfc3a70dfd10f0463972777c935f6f [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 Wienand9a2ad352006-02-20 22:44:45 +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 Wienand9a2ad352006-02-20 22:44:45 +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 Wienand9a2ad352006-02-20 22:44:45 +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 Wienand9a2ad352006-02-20 22:44:45 +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: */
54struct opt_e_t * opt_x = NULL;
55
56/* Set a break on the routine named here in order to re-initialize breakpoints
57 after all the PLTs have been initialzed */
58char * PLTs_initialized_by_here = PLTs_INIT_BY_HERE;
59
60static void
61usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020062#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
63 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand9a2ad352006-02-20 22:44:45 +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 Wienand9a2ad352006-02-20 22:44:45 +010067"Trace library calls of a given program.\n\n"
68
Juan Cespedesac3db291998-04-25 14:31:58 +020069# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +010070" -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020071# else
Ian Wienand9a2ad352006-02-20 22:44:45 +010072" -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020073# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010074" -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010075# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020076# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +010077" -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020078# else
Ian Wienand9a2ad352006-02-20 22:44:45 +010079" -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020080# endif
81# endif
82# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +010083" -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020084# else
Ian Wienand9a2ad352006-02-20 22:44:45 +010085" -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020086# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010087" -e expr modify which events to trace.\n"
88" -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010089# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +010090" -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010091# else
Ian Wienand9a2ad352006-02-20 22:44:45 +010092" -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010093# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010094" -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010095# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +010096" -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010097# else
Ian Wienand9a2ad352006-02-20 22:44:45 +010098" -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010099# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100100" -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200101# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +0100102" -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200103# else
Ian Wienand9a2ad352006-02-20 22:44:45 +0100104" -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200105# endif
106# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +0100107" -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200108# else
Ian Wienand9a2ad352006-02-20 22:44:45 +0100109" -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200110# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100111" -p PID attach to the process with the process ID pid.\n"
112" -r print relative timestamps.\n"
113" -s STRLEN specify the maximum string size to print.\n"
114" -S display system calls.\n"
115" -t, -tt, -ttt print absolute timestamps.\n"
116" -T show the time spent inside each call.\n"
117" -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200118# if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +0100119" -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200120# else
Ian Wienand9a2ad352006-02-20 22:44:45 +0100121" -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200122# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100123" -x NAME treat the global NAME like a library subroutine.\n"
124" -X NAME same as -x; and PLT's will be initialized by here.\n"
125"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
126 , progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200127#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100128}
129
Ian Wienand9a2ad352006-02-20 22:44:45 +0100130static char *
131search_for_command(char * filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100132 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100133 char *path;
134 int m, n;
135
136 if (strchr(filename, '/')) {
137 return filename;
138 }
139 for (path = getenv("PATH"); path && *path; path += m) {
140 if (strchr(path, ':')) {
141 n = strchr(path, ':') - path;
142 m = n + 1;
143 } else {
144 m = n = strlen(path);
145 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100146 if (n + strlen(filename) + 1 >= PATH_MAX) {
147 fprintf(stderr, "Error: filename too long\n");
148 exit(1);
149 }
150 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100151 if (n && pathname[n - 1] != '/') {
152 pathname[n++] = '/';
153 }
154 strcpy(pathname + n, filename);
155 if (!access(pathname, X_OK)) {
156 return pathname;
157 }
158 }
159 return filename;
160}
161
Ian Wienand9a2ad352006-02-20 22:44:45 +0100162char **
163process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200164 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100165 output = stderr;
166
Juan Cespedesac3db291998-04-25 14:31:58 +0200167#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +0100168 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200169 int c;
170#if HAVE_GETOPT_LONG
171 int option_index = 0;
172 static struct option long_options[] = {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100173 { "align", 1, 0, 'a'},
174 { "debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100175# ifdef USE_DEMANGLE
Ian Wienand9a2ad352006-02-20 22:44:45 +0100176 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200177#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100178 { "indent", 1, 0, 'n'},
179 { "help", 0, 0, 'h'},
180 { "library", 1, 0, 'l'},
181 { "output", 1, 0, 'o'},
182 { "version", 0, 0, 'V'},
183 { 0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200184 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100185 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100186# ifdef USE_DEMANGLE
Ian Wienand9a2ad352006-02-20 22:44:45 +0100187 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200188# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100189 "a:e:l:n:o:p:s:u:x:X:", long_options, &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 Wienand9a2ad352006-02-20 22:44:45 +0100193 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200194# endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100195 "a:e:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200196#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100197 if (c==-1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200198 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100200 switch(c) {
201 case 'a': opt_a = atoi(optarg);
202 break;
203 case 'c': opt_c++;
204 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100205#ifdef USE_DEMANGLE
Ian Wienand9a2ad352006-02-20 22:44:45 +0100206 case 'C': opt_C++;
207 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200208#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100209 case 'd': opt_d++;
210 break;
211 case 'e':
212 {
213 char * str_e = strdup(optarg);
214 if (!str_e) {
215 perror("ltrace: strdup");
216 exit(1);
Ian Wienand3219f322006-02-16 06:00:00 +0100217 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100218 if (str_e[0]=='!') {
219 opt_e_enable=0;
220 str_e++;
221 }
222 while(*str_e) {
223 struct opt_e_t * tmp;
224 char *str2 = strchr(str_e,',');
225 if (str2) {
226 *str2 = '\0';
227 }
228 tmp = malloc(sizeof(struct opt_e_t));
229 if (!tmp) {
230 perror("ltrace: malloc");
231 exit(1);
232 }
233 tmp->name = str_e;
234 tmp->next = opt_e;
235 opt_e = tmp;
236 if (str2) {
237 str_e = str2+1;
238 } else {
239 break;
240 }
241 }
242 break;
243 }
244 case 'f': opt_f = 1;
245 break;
246 case 'h': usage();
247 exit(0);
248 case 'i': opt_i++;
249 break;
250 case 'l': if (library_num == MAX_LIBRARY) {
251 fprintf(stderr, "Too many libraries. Maximum is %i.\n", MAX_LIBRARY);
252 exit(1);
253 }
254 library[library_num++] = optarg;
255 break;
256 case 'L': opt_L = 0;
257 break;
258 case 'n': opt_n = atoi(optarg);
259 break;
260 case 'o': output = fopen(optarg, "w");
261 if (!output) {
262 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
263 exit(1);
264 }
265 setvbuf(output, (char *)NULL, _IOLBF, 0);
266 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
267 break;
268 case 'p':
269 {
270 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100271 if (!tmp) {
272 perror("ltrace: malloc");
273 exit(1);
274 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100275 tmp->pid = atoi(optarg);
276 tmp->next = opt_p;
277 opt_p = tmp;
278 break;
279 }
280 case 'r': opt_r++;
Ian Wienand3219f322006-02-16 06:00:00 +0100281 break;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100282 case 's': opt_s = atoi(optarg);
283 break;
284 case 'S': opt_S = 1;
285 break;
286 case 't': opt_t++;
287 break;
288 case 'T': opt_T++;
289 break;
290 case 'u': opt_u = optarg;
291 break;
292 case 'V': printf("ltrace version " PACKAGE_VERSION ".\n"
293"Copyright (C) 1997-2006 Juan Cespedes <cespedes@debian.org>.\n"
294"This is free software; see the GNU General Public Licence\n"
295"version 2 or later for copying conditions. There is NO warranty.\n");
296 exit(0);
297 case 'X': PLTs_initialized_by_here = optarg;
298 /* Fall Thru */
299
300 case 'x':
301 {
302 struct opt_e_t * p = opt_x;
Juan Cespedesac3db291998-04-25 14:31:58 +0200303
Ian Wienand9a2ad352006-02-20 22:44:45 +0100304 /* First, check for duplicate. */
305 while (p && strcmp(p->name, optarg)) {
306 p = p->next;
307 }
308 if (p) { break; }
309
310 /* If not duplicate, add to list. */
311 p = malloc(sizeof(struct opt_e_t));
312 if (!p) {
313 perror("ltrace: malloc");
314 exit(1);
315 }
316 p->name = optarg;
317 p->next = opt_x;
318 opt_x = p;
319 break;
320 }
321
322
323 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200324#if HAVE_GETOPT_LONG
Ian Wienand9a2ad352006-02-20 22:44:45 +0100325 fprintf(stderr, "Try `%s --help' for more information\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200326#else
Ian Wienand9a2ad352006-02-20 22:44:45 +0100327 fprintf(stderr, "Try `%s -h' for more information\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200328#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100329 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100330 }
331 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100332 argc -= optind; argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200333#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100334
Ian Wienand9a2ad352006-02-20 22:44:45 +0100335 if (!opt_p && argc<1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200336 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200337 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100338 exit(1);
339 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200340 if (opt_r && opt_t) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100341 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200342 exit(1);
343 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100344 if (argc>0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200345 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100346 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200347 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100348}