blob: 025e909834742a17ff818e50adaf2c60942cdef6 [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'},
139 { "demangle", 0, 0, 'C'},
140 { "help", 0, 0, 'h'},
141 { "output", 1, 0, 'o'},
142 { "version", 0, 0, 'V'},
143 { 0, 0, 0, 0}
144 };
Juan Cespedesf666d191998-09-20 23:04:34 +0200145 c = getopt_long(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200146# if HAVE_LIBIBERTY
147 "C"
148# endif
149 "a:s:o:u:p:e:", long_options, &option_index);
150#else
151 c = getopt(argc, argv, "+dfiLSth"
152# if HAVE_LIBIBERTY
153 "C"
154# endif
155 "a:s:o:u:p:e:");
156#endif
157 if (c==-1) {
158 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100159 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200160 switch(c) {
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200161 case 'd': opt_d++;
162 break;
163 case 'f': opt_f = 1;
164 break;
165 case 'i': opt_i++;
166 break;
167 case 'L': opt_L = 0;
168 break;
169 case 'S': opt_S = 1;
170 break;
Juan Cespedesf666d191998-09-20 23:04:34 +0200171 case 'r': opt_r++;
172 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200173 case 't': opt_t++;
174 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200175#if HAVE_LIBIBERTY
176 case 'C': opt_C++;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100177 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200178#endif
179 case 'a': opt_a = atoi(optarg);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200181 case 's': opt_s = atoi(optarg);
182 break;
183 case 'h': usage();
184 exit(0);
185 case 'o': output = fopen(optarg, "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100186 if (!output) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200187 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100188 exit(1);
189 }
Juan Cespedes64c6dfb1998-07-14 13:49:47 +0200190 setvbuf(output, (char *)NULL, _IOLBF, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100191 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200192 case 'u': opt_u = optarg;
Juan Cespedese1887051998-03-10 00:08:41 +0100193 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200194 case 'p':
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195 {
196 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
197 if (!tmp) {
198 perror("malloc");
199 exit(1);
200 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200201 tmp->pid = atoi(optarg);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100202 tmp->next = opt_p;
203 opt_p = tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204 break;
205 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200206 case 'e':
207 {
208 char * str_e = strdup(optarg);
209 if (!str_e) {
210 perror("strdup");
211 exit(1);
212 }
213 if (str_e[0]=='!') {
214 opt_e_enable=0;
215 str_e++;
216 }
217 while(*str_e) {
218 struct opt_e_t * tmp;
219 char *str2 = strchr(str_e,',');
220 if (str2) {
221 *str2 = '\0';
222 }
223 tmp = malloc(sizeof(struct opt_e_t));
224 if (!tmp) {
225 perror("malloc");
226 exit(1);
227 }
228 tmp->name = str_e;
229 tmp->next = opt_e;
230 opt_e = tmp;
231 if (str2) {
232 str_e = str2+1;
233 } else {
234 break;
235 }
236 }
237 break;
238 }
239 case 'V': printf("ltrace version "
240#ifdef VERSION
241 VERSION
242#else
243 "???"
244#endif
245 ".\n"
246"Copyright (C) 1997-1998 Juan Cespedes <cespedes@debian.org>.\n"
247"This is free software; see the GNU General Public Licence\n"
248"version 2 or later for copying conditions. There is NO warranty.\n");
249 exit(0);
250
251 default:
252#if HAVE_GETOPT_LONG
253 fprintf(stderr, "Try `%s --help' for more information\n", progname);
254#else
255 fprintf(stderr, "Try `%s -h' for more information\n", progname);
256#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100257 exit(1);
258 }
259 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200260 argc -= optind; argv += optind;
261#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100262
Juan Cespedesac3db291998-04-25 14:31:58 +0200263 if (!opt_p && argc<1) {
264 fprintf(stderr, "%s: too few arguments\n", progname);
265#if HAVE_GETOPT_LONG
266 fprintf(stderr, "Try `%s --help' for more information\n", progname);
267#elif HAVE_GETOPT
268 fprintf(stderr, "Try `%s -h' for more information\n", progname);
269#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270 exit(1);
271 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200272 if (opt_r && opt_t) {
273 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
274 exit(1);
275 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200276 if (argc>0) {
277 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100278 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200279 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100280}