blob: 99dfa6ae178962089c1871bde2d562464a1b1305 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include <string.h>
6#include <stdlib.h>
7#include <unistd.h>
Juan Cespedes1b9cfd61999-08-30 19:34:50 +02008#include <fcntl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <errno.h>
10#include <limits.h>
11
Juan Cespedesac3db291998-04-25 14:31:58 +020012#if HAVE_GETOPT_H
13#include <getopt.h>
14#endif
15
Juan Cespedes5e01f651998-03-08 22:31:44 +010016#include "ltrace.h"
17#include "options.h"
18#include "defs.h"
19
Juan Cespedesac3db291998-04-25 14:31:58 +020020static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010021FILE * output;
22int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
23int opt_d = 0; /* debug */
24int opt_i = 0; /* instruction pointer */
25int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
26int opt_S = 0; /* display syscalls */
27int opt_L = 1; /* display library calls */
28int opt_f = 0; /* trace child processes as they are created */
Juan Cespedese1887051998-03-10 00:08:41 +010029char * opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020030int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020031int opt_t = 0; /* print absolute timestamp */
Juan Cespedesac3db291998-04-25 14:31:58 +020032#if HAVE_LIBIBERTY
33int opt_C = 0; /* Demangle low-level symbol names into user-level names */
34#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020035int opt_n = 0; /* indent trace output according to program flow */
Juan Cespedes5e01f651998-03-08 22:31:44 +010036
37/* List of pids given to option -p: */
38struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */
39
Juan Cespedesac3db291998-04-25 14:31:58 +020040/* List of function names given to option -e: */
41struct opt_e_t * opt_e = NULL;
42int opt_e_enable=1;
43
Juan Cespedes5e01f651998-03-08 22:31:44 +010044static void usage(void)
45{
Juan Cespedesac3db291998-04-25 14:31:58 +020046#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
47 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
48"Trace library calls of a given program.\n\n", progname);
49#else
50 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
51"Trace library calls of a given program.\n\n"
52
53# if HAVE_GETOPT_LONG
54" -d, --debug print debugging info.\n"
55# else
56" -d print debugging info.\n"
57# endif
58" -f follow forks.\n"
59" -i print instruction pointer at time of library call.\n"
60" -L do NOT display library calls.\n"
61" -S display system calls.\n"
Juan Cespedesf666d191998-09-20 23:04:34 +020062" -r print relative timestamps.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020063" -t, -tt, -ttt print absolute timestamps.\n"
64# if HAVE_LIBIBERTY
65# if HAVE_GETOPT_LONG
66" -C, --demangle decode low-level symbol names into user-level names.\n"
67# else
68" -C decode low-level symbol names into user-level names.\n"
69# endif
70# endif
71# if HAVE_GETOPT_LONG
72" -a, --align=COLUMN align return values in a secific column.\n"
73# else
74" -a COLUMN align return values in a secific column.\n"
75# endif
76" -s STRLEN specify the maximum string size to print.\n"
77# if HAVE_GETOPT_LONG
78" -o, --output=FILE write the trace output to that file.\n"
79# else
80" -o FILE write the trace output to that file.\n"
81# endif
82" -u USERNAME run command with the userid, groupid of username.\n"
83" -p PID attach to the process with the process ID pid.\n"
84" -e expr modify which events to trace.\n"
85# if HAVE_GETOPT_LONG
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020086" -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
87# else
88" -n NR indent output by NR spaces for each call level nesting.\n"
89# endif
90# if HAVE_GETOPT_LONG
Juan Cespedesac3db291998-04-25 14:31:58 +020091" -h, --help display this help and exit.\n"
92# else
93" -h display this help and exit.\n"
94# endif
95# if HAVE_GETOPT_LONG
96" -V, --version output version information and exit.\n"
97# else
98" -V output version information and exit.\n"
99# endif
100"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
101 , progname);
102#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100103}
104
105static char * search_for_command(char * filename)
106{
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200107 static char pathname[1024];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100108 char *path;
109 int m, n;
110
111 if (strchr(filename, '/')) {
112 return filename;
113 }
114 for (path = getenv("PATH"); path && *path; path += m) {
115 if (strchr(path, ':')) {
116 n = strchr(path, ':') - path;
117 m = n + 1;
118 } else {
119 m = n = strlen(path);
120 }
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200121 strncpy(pathname, path, n); /* Possible buffer overrun */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100122 if (n && pathname[n - 1] != '/') {
123 pathname[n++] = '/';
124 }
125 strcpy(pathname + n, filename);
126 if (!access(pathname, X_OK)) {
127 return pathname;
128 }
129 }
130 return filename;
131}
132
133char ** process_options(int argc, char **argv)
134{
Juan Cespedesac3db291998-04-25 14:31:58 +0200135 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 output = stderr;
137
Juan Cespedesac3db291998-04-25 14:31:58 +0200138#if HAVE_GETOPT || HAVE_GETOPT_LONG
Juan Cespedes5e01f651998-03-08 22:31:44 +0100139 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200140 int c;
141#if HAVE_GETOPT_LONG
142 int option_index = 0;
143 static struct option long_options[] = {
144 { "align", 1, 0, 'a'},
145 { "debug", 0, 0, 'd'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200146# if HAVE_LIBIBERTY
Juan Cespedesac3db291998-04-25 14:31:58 +0200147 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200148#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200149 { "indent", 0, 0, 'n'},
Juan Cespedesac3db291998-04-25 14:31:58 +0200150 { "help", 0, 0, 'h'},
151 { "output", 1, 0, 'o'},
152 { "version", 0, 0, 'V'},
153 { 0, 0, 0, 0}
154 };
Juan Cespedesf666d191998-09-20 23:04:34 +0200155 c = getopt_long(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200156# if HAVE_LIBIBERTY
157 "C"
158# endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200159 "a:s:o:u:p:e:n:", long_options, &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200160#else
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200161 c = getopt(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200162# if HAVE_LIBIBERTY
163 "C"
164# endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200165 "a:s:o:u:p:e:n:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200166#endif
167 if (c==-1) {
168 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100169 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200170 switch(c) {
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200171 case 'd': opt_d++;
172 break;
173 case 'f': opt_f = 1;
174 break;
175 case 'i': opt_i++;
176 break;
177 case 'L': opt_L = 0;
178 break;
179 case 'S': opt_S = 1;
180 break;
Juan Cespedesf666d191998-09-20 23:04:34 +0200181 case 'r': opt_r++;
182 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200183 case 't': opt_t++;
184 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200185#if HAVE_LIBIBERTY
186 case 'C': opt_C++;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100187 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200188#endif
189 case 'a': opt_a = atoi(optarg);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200191 case 's': opt_s = atoi(optarg);
192 break;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200193 case 'n': opt_n = atoi(optarg);
194 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200195 case 'h': usage();
196 exit(0);
197 case 'o': output = fopen(optarg, "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100198 if (!output) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200199 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 exit(1);
201 }
Juan Cespedes64c6dfb1998-07-14 13:49:47 +0200202 setvbuf(output, (char *)NULL, _IOLBF, 0);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200203 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200205 case 'u': opt_u = optarg;
Juan Cespedese1887051998-03-10 00:08:41 +0100206 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200207 case 'p':
Juan Cespedes5e01f651998-03-08 22:31:44 +0100208 {
209 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
210 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200211 perror("ltrace: malloc");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100212 exit(1);
213 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200214 tmp->pid = atoi(optarg);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100215 tmp->next = opt_p;
216 opt_p = tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100217 break;
218 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200219 case 'e':
220 {
221 char * str_e = strdup(optarg);
222 if (!str_e) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200223 perror("ltrace: strdup");
Juan Cespedesac3db291998-04-25 14:31:58 +0200224 exit(1);
225 }
226 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));
237 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200238 perror("ltrace: malloc");
Juan Cespedesac3db291998-04-25 14:31:58 +0200239 exit(1);
240 }
241 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 }
249 }
250 break;
251 }
252 case 'V': printf("ltrace version "
253#ifdef VERSION
254 VERSION
255#else
256 "???"
257#endif
258 ".\n"
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200259"Copyright (C) 1997-1999 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200260"This is free software; see the GNU General Public Licence\n"
261"version 2 or later for copying conditions. There is NO warranty.\n");
262 exit(0);
263
264 default:
265#if HAVE_GETOPT_LONG
266 fprintf(stderr, "Try `%s --help' for more information\n", progname);
267#else
268 fprintf(stderr, "Try `%s -h' for more information\n", progname);
269#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270 exit(1);
271 }
272 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200273 argc -= optind; argv += optind;
274#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100275
Juan Cespedesac3db291998-04-25 14:31:58 +0200276 if (!opt_p && argc<1) {
277 fprintf(stderr, "%s: too few arguments\n", progname);
278#if HAVE_GETOPT_LONG
279 fprintf(stderr, "Try `%s --help' for more information\n", progname);
280#elif HAVE_GETOPT
281 fprintf(stderr, "Try `%s -h' for more information\n", progname);
282#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283 exit(1);
284 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200285 if (opt_r && opt_t) {
286 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
287 exit(1);
288 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200289 if (argc>0) {
290 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100291 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200292 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100293}