blob: c4182d1ec18d8276868f81dea0f4c13e195da593 [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 Cespedesf1bfe202002-03-27 00:22:23 +01006# define VERSION "0.3.24"
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') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010028FILE * output;
29int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
30int opt_d = 0; /* debug */
31int opt_i = 0; /* instruction pointer */
32int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
33int opt_S = 0; /* display syscalls */
34int opt_L = 1; /* display library calls */
35int opt_f = 0; /* trace child processes as they are created */
Juan Cespedese1887051998-03-10 00:08:41 +010036char * opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020037int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020038int opt_t = 0; /* print absolute timestamp */
Juan Cespedesac3db291998-04-25 14:31:58 +020039#if HAVE_LIBIBERTY
40int opt_C = 0; /* Demangle low-level symbol names into user-level names */
41#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020042int opt_n = 0; /* indent trace output according to program flow */
Juan Cespedes5e01f651998-03-08 22:31:44 +010043
44/* List of pids given to option -p: */
45struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */
46
Juan Cespedesac3db291998-04-25 14:31:58 +020047/* List of function names given to option -e: */
48struct opt_e_t * opt_e = NULL;
49int opt_e_enable=1;
50
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010051static void
52usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020053#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
54 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
55"Trace library calls of a given program.\n\n", progname);
56#else
57 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
58"Trace library calls of a given program.\n\n"
59
60# if HAVE_GETOPT_LONG
61" -d, --debug print debugging info.\n"
62# else
63" -d print debugging info.\n"
64# endif
65" -f follow forks.\n"
66" -i print instruction pointer at time of library call.\n"
67" -L do NOT display library calls.\n"
68" -S display system calls.\n"
Juan Cespedesf666d191998-09-20 23:04:34 +020069" -r print relative timestamps.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020070" -t, -tt, -ttt print absolute timestamps.\n"
Juan Cespedes1cd999a2001-07-03 00:46:04 +020071# if HAVE_GETOPT_LONG
72" -l, --library=FILE print library calls from this library only.\n"
73# else
74" -l FILE print library calls from this library only.\n"
75# endif
Juan Cespedesac3db291998-04-25 14:31:58 +020076# if HAVE_LIBIBERTY
77# if HAVE_GETOPT_LONG
78" -C, --demangle decode low-level symbol names into user-level names.\n"
79# else
80" -C decode low-level symbol names into user-level names.\n"
81# endif
82# endif
83# if HAVE_GETOPT_LONG
84" -a, --align=COLUMN align return values in a secific column.\n"
85# else
86" -a COLUMN align return values in a secific column.\n"
87# endif
88" -s STRLEN specify the maximum string size to print.\n"
89# if HAVE_GETOPT_LONG
90" -o, --output=FILE write the trace output to that file.\n"
91# else
92" -o FILE write the trace output to that file.\n"
93# endif
94" -u USERNAME run command with the userid, groupid of username.\n"
95" -p PID attach to the process with the process ID pid.\n"
96" -e expr modify which events to trace.\n"
97# if HAVE_GETOPT_LONG
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020098" -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
99# else
100" -n NR indent output by NR spaces for each call level nesting.\n"
101# endif
102# if HAVE_GETOPT_LONG
Juan Cespedesac3db291998-04-25 14:31:58 +0200103" -h, --help display this help and exit.\n"
104# else
105" -h display this help and exit.\n"
106# endif
107# if HAVE_GETOPT_LONG
108" -V, --version output version information and exit.\n"
109# else
110" -V output version information and exit.\n"
111# endif
112"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
113 , progname);
114#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100115}
116
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100117static char *
118search_for_command(char * filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100119 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100120 char *path;
121 int m, n;
122
123 if (strchr(filename, '/')) {
124 return filename;
125 }
126 for (path = getenv("PATH"); path && *path; path += m) {
127 if (strchr(path, ':')) {
128 n = strchr(path, ':') - path;
129 m = n + 1;
130 } else {
131 m = n = strlen(path);
132 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100133 if (n + strlen(filename) + 1 >= PATH_MAX) {
134 fprintf(stderr, "Error: filename too long\n");
135 exit(1);
136 }
137 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100138 if (n && pathname[n - 1] != '/') {
139 pathname[n++] = '/';
140 }
141 strcpy(pathname + n, filename);
142 if (!access(pathname, X_OK)) {
143 return pathname;
144 }
145 }
146 return filename;
147}
148
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100149char **
150process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200151 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100152 output = stderr;
153
Juan Cespedesac3db291998-04-25 14:31:58 +0200154#if HAVE_GETOPT || HAVE_GETOPT_LONG
Juan Cespedes5e01f651998-03-08 22:31:44 +0100155 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200156 int c;
157#if HAVE_GETOPT_LONG
158 int option_index = 0;
159 static struct option long_options[] = {
160 { "align", 1, 0, 'a'},
161 { "debug", 0, 0, 'd'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200162# if HAVE_LIBIBERTY
Juan Cespedesac3db291998-04-25 14:31:58 +0200163 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200164#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200165 { "indent", 0, 0, 'n'},
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200166 { "library", 0, 0, 'l'},
Juan Cespedesac3db291998-04-25 14:31:58 +0200167 { "help", 0, 0, 'h'},
168 { "output", 1, 0, 'o'},
169 { "version", 0, 0, 'V'},
170 { 0, 0, 0, 0}
171 };
Juan Cespedesf666d191998-09-20 23:04:34 +0200172 c = getopt_long(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200173# if HAVE_LIBIBERTY
174 "C"
175# endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200176 "a:s:o:u:p:e:n:l:", long_options, &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200177#else
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200178 c = getopt(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200179# if HAVE_LIBIBERTY
180 "C"
181# endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200182 "a:s:o:u:p:e:n:l:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200183#endif
184 if (c==-1) {
185 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100186 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200187 switch(c) {
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200188 case 'l': if (library_num == MAX_LIBRARY) {
189 fprintf(stderr, "Too many libraries. Maximum is %i.\n", MAX_LIBRARY);
190 exit(1);
191 }
192 library[library_num++] = optarg;
193 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200194 case 'd': opt_d++;
195 break;
196 case 'f': opt_f = 1;
197 break;
198 case 'i': opt_i++;
199 break;
200 case 'L': opt_L = 0;
201 break;
202 case 'S': opt_S = 1;
203 break;
Juan Cespedesf666d191998-09-20 23:04:34 +0200204 case 'r': opt_r++;
205 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200206 case 't': opt_t++;
207 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200208#if HAVE_LIBIBERTY
209 case 'C': opt_C++;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100210 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200211#endif
212 case 'a': opt_a = atoi(optarg);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100213 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200214 case 's': opt_s = atoi(optarg);
215 break;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200216 case 'n': opt_n = atoi(optarg);
217 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200218 case 'h': usage();
219 exit(0);
220 case 'o': output = fopen(optarg, "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100221 if (!output) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200222 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100223 exit(1);
224 }
Juan Cespedes64c6dfb1998-07-14 13:49:47 +0200225 setvbuf(output, (char *)NULL, _IOLBF, 0);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200226 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100227 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200228 case 'u': opt_u = optarg;
Juan Cespedese1887051998-03-10 00:08:41 +0100229 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200230 case 'p':
Juan Cespedes5e01f651998-03-08 22:31:44 +0100231 {
232 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
233 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200234 perror("ltrace: malloc");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100235 exit(1);
236 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200237 tmp->pid = atoi(optarg);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100238 tmp->next = opt_p;
239 opt_p = tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100240 break;
241 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200242 case 'e':
243 {
244 char * str_e = strdup(optarg);
245 if (!str_e) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200246 perror("ltrace: strdup");
Juan Cespedesac3db291998-04-25 14:31:58 +0200247 exit(1);
248 }
249 if (str_e[0]=='!') {
250 opt_e_enable=0;
251 str_e++;
252 }
253 while(*str_e) {
254 struct opt_e_t * tmp;
255 char *str2 = strchr(str_e,',');
256 if (str2) {
257 *str2 = '\0';
258 }
259 tmp = malloc(sizeof(struct opt_e_t));
260 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200261 perror("ltrace: malloc");
Juan Cespedesac3db291998-04-25 14:31:58 +0200262 exit(1);
263 }
264 tmp->name = str_e;
265 tmp->next = opt_e;
266 opt_e = tmp;
267 if (str2) {
268 str_e = str2+1;
269 } else {
270 break;
271 }
272 }
273 break;
274 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200275 case 'V': printf("ltrace version " VERSION ".\n"
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100276"Copyright (C) 1997-2002 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200277"This is free software; see the GNU General Public Licence\n"
278"version 2 or later for copying conditions. There is NO warranty.\n");
279 exit(0);
280
281 default:
282#if HAVE_GETOPT_LONG
283 fprintf(stderr, "Try `%s --help' for more information\n", progname);
284#else
285 fprintf(stderr, "Try `%s -h' for more information\n", progname);
286#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100287 exit(1);
288 }
289 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200290 argc -= optind; argv += optind;
291#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100292
Juan Cespedesac3db291998-04-25 14:31:58 +0200293 if (!opt_p && argc<1) {
294 fprintf(stderr, "%s: too few arguments\n", progname);
295#if HAVE_GETOPT_LONG
296 fprintf(stderr, "Try `%s --help' for more information\n", progname);
297#elif HAVE_GETOPT
298 fprintf(stderr, "Try `%s -h' for more information\n", progname);
299#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100300 exit(1);
301 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200302 if (opt_r && opt_t) {
303 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
304 exit(1);
305 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200306 if (argc>0) {
307 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100308 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200309 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100310}