blob: a8c1354544f5bb6225d061a2e124ddf06c1e7549 [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>
8#include <errno.h>
9#include <limits.h>
10
Juan Cespedesac3db291998-04-25 14:31:58 +020011#if HAVE_GETOPT_H
12#include <getopt.h>
13#endif
14
Juan Cespedes5e01f651998-03-08 22:31:44 +010015#include "ltrace.h"
16#include "options.h"
17#include "defs.h"
18
Juan Cespedesac3db291998-04-25 14:31:58 +020019static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010020FILE * output;
21int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
22int opt_d = 0; /* debug */
23int opt_i = 0; /* instruction pointer */
24int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
25int opt_S = 0; /* display syscalls */
26int opt_L = 1; /* display library calls */
27int opt_f = 0; /* trace child processes as they are created */
Juan Cespedese1887051998-03-10 00:08:41 +010028char * opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020029int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020030int opt_t = 0; /* print absolute timestamp */
Juan Cespedesac3db291998-04-25 14:31:58 +020031#if HAVE_LIBIBERTY
32int opt_C = 0; /* Demangle low-level symbol names into user-level names */
33#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +010034
35/* List of pids given to option -p: */
36struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */
37
Juan Cespedesac3db291998-04-25 14:31:58 +020038/* List of function names given to option -e: */
39struct opt_e_t * opt_e = NULL;
40int opt_e_enable=1;
41
Juan Cespedes5e01f651998-03-08 22:31:44 +010042static void usage(void)
43{
Juan Cespedesac3db291998-04-25 14:31:58 +020044#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
45 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
46"Trace library calls of a given program.\n\n", progname);
47#else
48 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
49"Trace library calls of a given program.\n\n"
50
51# if HAVE_GETOPT_LONG
52" -d, --debug print debugging info.\n"
53# else
54" -d print debugging info.\n"
55# endif
56" -f follow forks.\n"
57" -i print instruction pointer at time of library call.\n"
58" -L do NOT display library calls.\n"
59" -S display system calls.\n"
Juan Cespedesf666d191998-09-20 23:04:34 +020060" -r print relative timestamps.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020061" -t, -tt, -ttt print absolute timestamps.\n"
62# if HAVE_LIBIBERTY
63# if HAVE_GETOPT_LONG
64" -C, --demangle decode low-level symbol names into user-level names.\n"
65# else
66" -C decode low-level symbol names into user-level names.\n"
67# endif
68# endif
69# if HAVE_GETOPT_LONG
70" -a, --align=COLUMN align return values in a secific column.\n"
71# else
72" -a COLUMN align return values in a secific column.\n"
73# endif
74" -s STRLEN specify the maximum string size to print.\n"
75# if HAVE_GETOPT_LONG
76" -o, --output=FILE write the trace output to that file.\n"
77# else
78" -o FILE write the trace output to that file.\n"
79# endif
80" -u USERNAME run command with the userid, groupid of username.\n"
81" -p PID attach to the process with the process ID pid.\n"
82" -e expr modify which events to trace.\n"
83# if HAVE_GETOPT_LONG
84" -h, --help display this help and exit.\n"
85# else
86" -h display this help and exit.\n"
87# endif
88# if HAVE_GETOPT_LONG
89" -V, --version output version information and exit.\n"
90# else
91" -V output version information and exit.\n"
92# endif
93"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
94 , progname);
95#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +010096}
97
98static char * search_for_command(char * filename)
99{
100 static char pathname[PATH_MAX];
101 char *path;
102 int m, n;
103
104 if (strchr(filename, '/')) {
105 return filename;
106 }
107 for (path = getenv("PATH"); path && *path; path += m) {
108 if (strchr(path, ':')) {
109 n = strchr(path, ':') - path;
110 m = n + 1;
111 } else {
112 m = n = strlen(path);
113 }
114 strncpy(pathname, path, n);
115 if (n && pathname[n - 1] != '/') {
116 pathname[n++] = '/';
117 }
118 strcpy(pathname + n, filename);
119 if (!access(pathname, X_OK)) {
120 return pathname;
121 }
122 }
123 return filename;
124}
125
126char ** process_options(int argc, char **argv)
127{
Juan Cespedesac3db291998-04-25 14:31:58 +0200128 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100129 output = stderr;
130
Juan Cespedesac3db291998-04-25 14:31:58 +0200131#if HAVE_GETOPT || HAVE_GETOPT_LONG
Juan Cespedes5e01f651998-03-08 22:31:44 +0100132 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200133 int c;
134#if HAVE_GETOPT_LONG
135 int option_index = 0;
136 static struct option long_options[] = {
137 { "align", 1, 0, 'a'},
138 { "debug", 0, 0, 'd'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200139# if HAVE_LIBIBERTY
Juan Cespedesac3db291998-04-25 14:31:58 +0200140 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200141#endif
Juan Cespedesac3db291998-04-25 14:31:58 +0200142 { "help", 0, 0, 'h'},
143 { "output", 1, 0, 'o'},
144 { "version", 0, 0, 'V'},
145 { 0, 0, 0, 0}
146 };
Juan Cespedesf666d191998-09-20 23:04:34 +0200147 c = getopt_long(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200148# if HAVE_LIBIBERTY
149 "C"
150# endif
151 "a:s:o:u:p:e:", long_options, &option_index);
152#else
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200153 c = getopt(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200154# if HAVE_LIBIBERTY
155 "C"
156# endif
157 "a:s:o:u:p:e:");
158#endif
159 if (c==-1) {
160 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100161 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200162 switch(c) {
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200163 case 'd': opt_d++;
164 break;
165 case 'f': opt_f = 1;
166 break;
167 case 'i': opt_i++;
168 break;
169 case 'L': opt_L = 0;
170 break;
171 case 'S': opt_S = 1;
172 break;
Juan Cespedesf666d191998-09-20 23:04:34 +0200173 case 'r': opt_r++;
174 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200175 case 't': opt_t++;
176 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200177#if HAVE_LIBIBERTY
178 case 'C': opt_C++;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100179 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200180#endif
181 case 'a': opt_a = atoi(optarg);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100182 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200183 case 's': opt_s = atoi(optarg);
184 break;
185 case 'h': usage();
186 exit(0);
187 case 'o': output = fopen(optarg, "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100188 if (!output) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200189 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 exit(1);
191 }
Juan Cespedes64c6dfb1998-07-14 13:49:47 +0200192 setvbuf(output, (char *)NULL, _IOLBF, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100193 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200194 case 'u': opt_u = optarg;
Juan Cespedese1887051998-03-10 00:08:41 +0100195 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200196 case 'p':
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197 {
198 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
199 if (!tmp) {
200 perror("malloc");
201 exit(1);
202 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200203 tmp->pid = atoi(optarg);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100204 tmp->next = opt_p;
205 opt_p = tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100206 break;
207 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200208 case 'e':
209 {
210 char * str_e = strdup(optarg);
211 if (!str_e) {
212 perror("strdup");
213 exit(1);
214 }
215 if (str_e[0]=='!') {
216 opt_e_enable=0;
217 str_e++;
218 }
219 while(*str_e) {
220 struct opt_e_t * tmp;
221 char *str2 = strchr(str_e,',');
222 if (str2) {
223 *str2 = '\0';
224 }
225 tmp = malloc(sizeof(struct opt_e_t));
226 if (!tmp) {
227 perror("malloc");
228 exit(1);
229 }
230 tmp->name = str_e;
231 tmp->next = opt_e;
232 opt_e = tmp;
233 if (str2) {
234 str_e = str2+1;
235 } else {
236 break;
237 }
238 }
239 break;
240 }
241 case 'V': printf("ltrace version "
242#ifdef VERSION
243 VERSION
244#else
245 "???"
246#endif
247 ".\n"
248"Copyright (C) 1997-1998 Juan Cespedes <cespedes@debian.org>.\n"
249"This is free software; see the GNU General Public Licence\n"
250"version 2 or later for copying conditions. There is NO warranty.\n");
251 exit(0);
252
253 default:
254#if HAVE_GETOPT_LONG
255 fprintf(stderr, "Try `%s --help' for more information\n", progname);
256#else
257 fprintf(stderr, "Try `%s -h' for more information\n", progname);
258#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100259 exit(1);
260 }
261 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200262 argc -= optind; argv += optind;
263#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100264
Juan Cespedesac3db291998-04-25 14:31:58 +0200265 if (!opt_p && argc<1) {
266 fprintf(stderr, "%s: too few arguments\n", progname);
267#if HAVE_GETOPT_LONG
268 fprintf(stderr, "Try `%s --help' for more information\n", progname);
269#elif HAVE_GETOPT
270 fprintf(stderr, "Try `%s -h' for more information\n", progname);
271#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100272 exit(1);
273 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200274 if (opt_r && opt_t) {
275 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
276 exit(1);
277 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200278 if (argc>0) {
279 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100280 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200281 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100282}