blob: a5b10a6aca430914da2bb0043898878bfed91b0f [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 Cespedes5e01f651998-03-08 22:31:44 +010035
36/* List of pids given to option -p: */
37struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */
38
Juan Cespedesac3db291998-04-25 14:31:58 +020039/* List of function names given to option -e: */
40struct opt_e_t * opt_e = NULL;
41int opt_e_enable=1;
42
Juan Cespedes5e01f651998-03-08 22:31:44 +010043static void usage(void)
44{
Juan Cespedesac3db291998-04-25 14:31:58 +020045#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
46 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
47"Trace library calls of a given program.\n\n", progname);
48#else
49 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
50"Trace library calls of a given program.\n\n"
51
52# if HAVE_GETOPT_LONG
53" -d, --debug print debugging info.\n"
54# else
55" -d print debugging info.\n"
56# endif
57" -f follow forks.\n"
58" -i print instruction pointer at time of library call.\n"
59" -L do NOT display library calls.\n"
60" -S display system calls.\n"
Juan Cespedesf666d191998-09-20 23:04:34 +020061" -r print relative timestamps.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020062" -t, -tt, -ttt print absolute timestamps.\n"
63# if HAVE_LIBIBERTY
64# if HAVE_GETOPT_LONG
65" -C, --demangle decode low-level symbol names into user-level names.\n"
66# else
67" -C decode low-level symbol names into user-level names.\n"
68# endif
69# endif
70# if HAVE_GETOPT_LONG
71" -a, --align=COLUMN align return values in a secific column.\n"
72# else
73" -a COLUMN align return values in a secific column.\n"
74# endif
75" -s STRLEN specify the maximum string size to print.\n"
76# if HAVE_GETOPT_LONG
77" -o, --output=FILE write the trace output to that file.\n"
78# else
79" -o FILE write the trace output to that file.\n"
80# endif
81" -u USERNAME run command with the userid, groupid of username.\n"
82" -p PID attach to the process with the process ID pid.\n"
83" -e expr modify which events to trace.\n"
84# if HAVE_GETOPT_LONG
85" -h, --help display this help and exit.\n"
86# else
87" -h display this help and exit.\n"
88# endif
89# if HAVE_GETOPT_LONG
90" -V, --version output version information and exit.\n"
91# else
92" -V output version information and exit.\n"
93# endif
94"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
95 , progname);
96#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +010097}
98
99static char * search_for_command(char * filename)
100{
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200101 static char pathname[1024];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100102 char *path;
103 int m, n;
104
105 if (strchr(filename, '/')) {
106 return filename;
107 }
108 for (path = getenv("PATH"); path && *path; path += m) {
109 if (strchr(path, ':')) {
110 n = strchr(path, ':') - path;
111 m = n + 1;
112 } else {
113 m = n = strlen(path);
114 }
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200115 strncpy(pathname, path, n); /* Possible buffer overrun */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100116 if (n && pathname[n - 1] != '/') {
117 pathname[n++] = '/';
118 }
119 strcpy(pathname + n, filename);
120 if (!access(pathname, X_OK)) {
121 return pathname;
122 }
123 }
124 return filename;
125}
126
127char ** process_options(int argc, char **argv)
128{
Juan Cespedesac3db291998-04-25 14:31:58 +0200129 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100130 output = stderr;
131
Juan Cespedesac3db291998-04-25 14:31:58 +0200132#if HAVE_GETOPT || HAVE_GETOPT_LONG
Juan Cespedes5e01f651998-03-08 22:31:44 +0100133 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200134 int c;
135#if HAVE_GETOPT_LONG
136 int option_index = 0;
137 static struct option long_options[] = {
138 { "align", 1, 0, 'a'},
139 { "debug", 0, 0, 'd'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200140# if HAVE_LIBIBERTY
Juan Cespedesac3db291998-04-25 14:31:58 +0200141 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200142#endif
Juan Cespedesac3db291998-04-25 14:31:58 +0200143 { "help", 0, 0, 'h'},
144 { "output", 1, 0, 'o'},
145 { "version", 0, 0, 'V'},
146 { 0, 0, 0, 0}
147 };
Juan Cespedesf666d191998-09-20 23:04:34 +0200148 c = getopt_long(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200149# if HAVE_LIBIBERTY
150 "C"
151# endif
152 "a:s:o:u:p:e:", long_options, &option_index);
153#else
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200154 c = getopt(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200155# if HAVE_LIBIBERTY
156 "C"
157# endif
158 "a:s:o:u:p:e:");
159#endif
160 if (c==-1) {
161 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100162 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200163 switch(c) {
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200164 case 'd': opt_d++;
165 break;
166 case 'f': opt_f = 1;
167 break;
168 case 'i': opt_i++;
169 break;
170 case 'L': opt_L = 0;
171 break;
172 case 'S': opt_S = 1;
173 break;
Juan Cespedesf666d191998-09-20 23:04:34 +0200174 case 'r': opt_r++;
175 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200176 case 't': opt_t++;
177 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200178#if HAVE_LIBIBERTY
179 case 'C': opt_C++;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200181#endif
182 case 'a': opt_a = atoi(optarg);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100183 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200184 case 's': opt_s = atoi(optarg);
185 break;
186 case 'h': usage();
187 exit(0);
188 case 'o': output = fopen(optarg, "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100189 if (!output) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200190 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100191 exit(1);
192 }
Juan Cespedes64c6dfb1998-07-14 13:49:47 +0200193 setvbuf(output, (char *)NULL, _IOLBF, 0);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200194 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200196 case 'u': opt_u = optarg;
Juan Cespedese1887051998-03-10 00:08:41 +0100197 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200198 case 'p':
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 {
200 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
201 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200202 perror("ltrace: malloc");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100203 exit(1);
204 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200205 tmp->pid = atoi(optarg);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100206 tmp->next = opt_p;
207 opt_p = tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100208 break;
209 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200210 case 'e':
211 {
212 char * str_e = strdup(optarg);
213 if (!str_e) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200214 perror("ltrace: strdup");
Juan Cespedesac3db291998-04-25 14:31:58 +0200215 exit(1);
216 }
217 if (str_e[0]=='!') {
218 opt_e_enable=0;
219 str_e++;
220 }
221 while(*str_e) {
222 struct opt_e_t * tmp;
223 char *str2 = strchr(str_e,',');
224 if (str2) {
225 *str2 = '\0';
226 }
227 tmp = malloc(sizeof(struct opt_e_t));
228 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200229 perror("ltrace: malloc");
Juan Cespedesac3db291998-04-25 14:31:58 +0200230 exit(1);
231 }
232 tmp->name = str_e;
233 tmp->next = opt_e;
234 opt_e = tmp;
235 if (str2) {
236 str_e = str2+1;
237 } else {
238 break;
239 }
240 }
241 break;
242 }
243 case 'V': printf("ltrace version "
244#ifdef VERSION
245 VERSION
246#else
247 "???"
248#endif
249 ".\n"
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200250"Copyright (C) 1997-1999 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200251"This is free software; see the GNU General Public Licence\n"
252"version 2 or later for copying conditions. There is NO warranty.\n");
253 exit(0);
254
255 default:
256#if HAVE_GETOPT_LONG
257 fprintf(stderr, "Try `%s --help' for more information\n", progname);
258#else
259 fprintf(stderr, "Try `%s -h' for more information\n", progname);
260#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100261 exit(1);
262 }
263 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200264 argc -= optind; argv += optind;
265#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100266
Juan Cespedesac3db291998-04-25 14:31:58 +0200267 if (!opt_p && argc<1) {
268 fprintf(stderr, "%s: too few arguments\n", progname);
269#if HAVE_GETOPT_LONG
270 fprintf(stderr, "Try `%s --help' for more information\n", progname);
271#elif HAVE_GETOPT
272 fprintf(stderr, "Try `%s -h' for more information\n", progname);
273#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100274 exit(1);
275 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200276 if (opt_r && opt_t) {
277 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
278 exit(1);
279 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200280 if (argc>0) {
281 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100282 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200283 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100284}