blob: 57a0f8e11721fe3e55b22e14fce6c4813ffa4124 [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
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020012#define _GNU_SOURCE
Juan Cespedesac3db291998-04-25 14:31:58 +020013#include <getopt.h>
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020014#undef _GNU_SOURCE
Juan Cespedesac3db291998-04-25 14:31:58 +020015#endif
16
Juan Cespedes5e01f651998-03-08 22:31:44 +010017#include "ltrace.h"
18#include "options.h"
19#include "defs.h"
20
Juan Cespedesac3db291998-04-25 14:31:58 +020021static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010022FILE * output;
23int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
24int opt_d = 0; /* debug */
25int opt_i = 0; /* instruction pointer */
26int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
27int opt_S = 0; /* display syscalls */
28int opt_L = 1; /* display library calls */
29int opt_f = 0; /* trace child processes as they are created */
Juan Cespedese1887051998-03-10 00:08:41 +010030char * opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020031int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020032int opt_t = 0; /* print absolute timestamp */
Juan Cespedesac3db291998-04-25 14:31:58 +020033#if HAVE_LIBIBERTY
34int opt_C = 0; /* Demangle low-level symbol names into user-level names */
35#endif
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
86" -h, --help display this help and exit.\n"
87# else
88" -h display this help and exit.\n"
89# endif
90# if HAVE_GETOPT_LONG
91" -V, --version output version information and exit.\n"
92# else
93" -V output version information and exit.\n"
94# endif
95"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
96 , progname);
97#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +010098}
99
100static char * search_for_command(char * filename)
101{
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200102 static char pathname[1024];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100103 char *path;
104 int m, n;
105
106 if (strchr(filename, '/')) {
107 return filename;
108 }
109 for (path = getenv("PATH"); path && *path; path += m) {
110 if (strchr(path, ':')) {
111 n = strchr(path, ':') - path;
112 m = n + 1;
113 } else {
114 m = n = strlen(path);
115 }
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200116 strncpy(pathname, path, n); /* Possible buffer overrun */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100117 if (n && pathname[n - 1] != '/') {
118 pathname[n++] = '/';
119 }
120 strcpy(pathname + n, filename);
121 if (!access(pathname, X_OK)) {
122 return pathname;
123 }
124 }
125 return filename;
126}
127
128char ** process_options(int argc, char **argv)
129{
Juan Cespedesac3db291998-04-25 14:31:58 +0200130 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100131 output = stderr;
132
Juan Cespedesac3db291998-04-25 14:31:58 +0200133#if HAVE_GETOPT || HAVE_GETOPT_LONG
Juan Cespedes5e01f651998-03-08 22:31:44 +0100134 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200135 int c;
136#if HAVE_GETOPT_LONG
137 int option_index = 0;
138 static struct option long_options[] = {
139 { "align", 1, 0, 'a'},
140 { "debug", 0, 0, 'd'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200141# if HAVE_LIBIBERTY
Juan Cespedesac3db291998-04-25 14:31:58 +0200142 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200143#endif
Juan Cespedesac3db291998-04-25 14:31:58 +0200144 { "help", 0, 0, 'h'},
145 { "output", 1, 0, 'o'},
146 { "version", 0, 0, 'V'},
147 { 0, 0, 0, 0}
148 };
Juan Cespedesf666d191998-09-20 23:04:34 +0200149 c = getopt_long(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200150# if HAVE_LIBIBERTY
151 "C"
152# endif
153 "a:s:o:u:p:e:", long_options, &option_index);
154#else
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200155 c = getopt(argc, argv, "+dfiLSrthV"
Juan Cespedesac3db291998-04-25 14:31:58 +0200156# if HAVE_LIBIBERTY
157 "C"
158# endif
159 "a:s:o:u:p:e:");
160#endif
161 if (c==-1) {
162 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100163 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200164 switch(c) {
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200165 case 'd': opt_d++;
166 break;
167 case 'f': opt_f = 1;
168 break;
169 case 'i': opt_i++;
170 break;
171 case 'L': opt_L = 0;
172 break;
173 case 'S': opt_S = 1;
174 break;
Juan Cespedesf666d191998-09-20 23:04:34 +0200175 case 'r': opt_r++;
176 break;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200177 case 't': opt_t++;
178 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200179#if HAVE_LIBIBERTY
180 case 'C': opt_C++;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100181 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200182#endif
183 case 'a': opt_a = atoi(optarg);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100184 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200185 case 's': opt_s = atoi(optarg);
186 break;
187 case 'h': usage();
188 exit(0);
189 case 'o': output = fopen(optarg, "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 if (!output) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200191 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
Juan Cespedes5e01f651998-03-08 22:31:44 +0100192 exit(1);
193 }
Juan Cespedes64c6dfb1998-07-14 13:49:47 +0200194 setvbuf(output, (char *)NULL, _IOLBF, 0);
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}