blob: d97c12786e2b34cc57f67e8d0110c5fa3a54b393 [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
6# define VERSION "???"
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') */
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 Cespedese3eb9aa1999-04-03 03:21:52 +0200119 static char pathname[1024];
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 Cespedese3eb9aa1999-04-03 03:21:52 +0200133 strncpy(pathname, path, n); /* Possible buffer overrun */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100134 if (n && pathname[n - 1] != '/') {
135 pathname[n++] = '/';
136 }
137 strcpy(pathname + n, filename);
138 if (!access(pathname, X_OK)) {
139 return pathname;
140 }
141 }
142 return filename;
143}
144
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100145char **
146process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200147 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100148 output = stderr;
149
Juan Cespedesac3db291998-04-25 14:31:58 +0200150#if HAVE_GETOPT || HAVE_GETOPT_LONG
Juan Cespedes5e01f651998-03-08 22:31:44 +0100151 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200152 int c;
153#if HAVE_GETOPT_LONG
154 int option_index = 0;
155 static struct option long_options[] = {
156 { "align", 1, 0, 'a'},
157 { "debug", 0, 0, 'd'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200158# if HAVE_LIBIBERTY
Juan Cespedesac3db291998-04-25 14:31:58 +0200159 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200160#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200161 { "indent", 0, 0, 'n'},
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200162 { "library", 0, 0, 'l'},
Juan Cespedesac3db291998-04-25 14:31:58 +0200163 { "help", 0, 0, 'h'},
164 { "output", 1, 0, 'o'},
165 { "version", 0, 0, 'V'},
166 { 0, 0, 0, 0}
167 };
Juan Cespedesf666d191998-09-20 23:04:34 +0200168 c = getopt_long(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200169# if HAVE_LIBIBERTY
170 "C"
171# endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200172 "a:s:o:u:p:e:n:l:", long_options, &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200173#else
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200174 c = getopt(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200175# if HAVE_LIBIBERTY
176 "C"
177# endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200178 "a:s:o:u:p:e:n:l:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200179#endif
180 if (c==-1) {
181 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100182 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200183 switch(c) {
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200184 case 'l': if (library_num == MAX_LIBRARY) {
185 fprintf(stderr, "Too many libraries. Maximum is %i.\n", MAX_LIBRARY);
186 exit(1);
187 }
188 library[library_num++] = optarg;
189 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200190 case 'd': opt_d++;
191 break;
192 case 'f': opt_f = 1;
193 break;
194 case 'i': opt_i++;
195 break;
196 case 'L': opt_L = 0;
197 break;
198 case 'S': opt_S = 1;
199 break;
Juan Cespedesf666d191998-09-20 23:04:34 +0200200 case 'r': opt_r++;
201 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200202 case 't': opt_t++;
203 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200204#if HAVE_LIBIBERTY
205 case 'C': opt_C++;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100206 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200207#endif
208 case 'a': opt_a = atoi(optarg);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100209 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200210 case 's': opt_s = atoi(optarg);
211 break;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200212 case 'n': opt_n = atoi(optarg);
213 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200214 case 'h': usage();
215 exit(0);
216 case 'o': output = fopen(optarg, "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100217 if (!output) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200218 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100219 exit(1);
220 }
Juan Cespedes64c6dfb1998-07-14 13:49:47 +0200221 setvbuf(output, (char *)NULL, _IOLBF, 0);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200222 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100223 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200224 case 'u': opt_u = optarg;
Juan Cespedese1887051998-03-10 00:08:41 +0100225 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200226 case 'p':
Juan Cespedes5e01f651998-03-08 22:31:44 +0100227 {
228 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
229 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200230 perror("ltrace: malloc");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100231 exit(1);
232 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200233 tmp->pid = atoi(optarg);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100234 tmp->next = opt_p;
235 opt_p = tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100236 break;
237 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200238 case 'e':
239 {
240 char * str_e = strdup(optarg);
241 if (!str_e) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200242 perror("ltrace: strdup");
Juan Cespedesac3db291998-04-25 14:31:58 +0200243 exit(1);
244 }
245 if (str_e[0]=='!') {
246 opt_e_enable=0;
247 str_e++;
248 }
249 while(*str_e) {
250 struct opt_e_t * tmp;
251 char *str2 = strchr(str_e,',');
252 if (str2) {
253 *str2 = '\0';
254 }
255 tmp = malloc(sizeof(struct opt_e_t));
256 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200257 perror("ltrace: malloc");
Juan Cespedesac3db291998-04-25 14:31:58 +0200258 exit(1);
259 }
260 tmp->name = str_e;
261 tmp->next = opt_e;
262 opt_e = tmp;
263 if (str2) {
264 str_e = str2+1;
265 } else {
266 break;
267 }
268 }
269 break;
270 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200271 case 'V': printf("ltrace version " VERSION ".\n"
Juan Cespedesb1dd77d2002-03-03 00:22:06 +0100272"Copyright (C) 1997-2002 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200273"This is free software; see the GNU General Public Licence\n"
274"version 2 or later for copying conditions. There is NO warranty.\n");
275 exit(0);
276
277 default:
278#if HAVE_GETOPT_LONG
279 fprintf(stderr, "Try `%s --help' for more information\n", progname);
280#else
281 fprintf(stderr, "Try `%s -h' for more information\n", progname);
282#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283 exit(1);
284 }
285 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200286 argc -= optind; argv += optind;
287#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100288
Juan Cespedesac3db291998-04-25 14:31:58 +0200289 if (!opt_p && argc<1) {
290 fprintf(stderr, "%s: too few arguments\n", progname);
291#if HAVE_GETOPT_LONG
292 fprintf(stderr, "Try `%s --help' for more information\n", progname);
293#elif HAVE_GETOPT
294 fprintf(stderr, "Try `%s -h' for more information\n", progname);
295#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100296 exit(1);
297 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200298 if (opt_r && opt_t) {
299 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
300 exit(1);
301 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200302 if (argc>0) {
303 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100304 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200305 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100306}