blob: aff93aff93679c3d757683a403da8ebb4b65af28 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes1cd999a2001-07-03 00:46:04 +02005#ifndef VERSION
Juan Cespedesefe85f02004-04-04 01:31:38 +02006# define VERSION "0.3.32"
Juan Cespedes1cd999a2001-07-03 00:46:04 +02007#endif
8
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <string.h>
10#include <stdlib.h>
11#include <unistd.h>
Juan Cespedes1b9cfd61999-08-30 19:34:50 +020012#include <fcntl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010013#include <errno.h>
14#include <limits.h>
15
Juan Cespedesac3db291998-04-25 14:31:58 +020016#if HAVE_GETOPT_H
17#include <getopt.h>
18#endif
19
Juan Cespedes5e01f651998-03-08 22:31:44 +010020#include "ltrace.h"
21#include "options.h"
22#include "defs.h"
23
Juan Cespedes1cd999a2001-07-03 00:46:04 +020024#define MAX_LIBRARY 30
25char *library[MAX_LIBRARY];
26int library_num = 0;
Juan Cespedesac3db291998-04-25 14:31:58 +020027static char *progname; /* Program name (`ltrace') */
Juan Cespedes5e01f651998-03-08 22:31:44 +010028FILE * output;
29int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
Juan Cespedesd65efa32003-02-03 00:22:30 +010030int opt_c = 0; /* Count time, calls, and report a summary on program exit */
Juan Cespedes5e01f651998-03-08 22:31:44 +010031int opt_d = 0; /* debug */
32int opt_i = 0; /* instruction pointer */
33int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
34int opt_S = 0; /* display syscalls */
35int opt_L = 1; /* display library calls */
36int opt_f = 0; /* trace child processes as they are created */
Juan Cespedese1887051998-03-10 00:08:41 +010037char * opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020038int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020039int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd914a202004-11-10 00:15:33 +010040#ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020041int opt_C = 0; /* Demangle low-level symbol names into user-level names */
42#endif
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020043int opt_n = 0; /* indent trace output according to program flow */
Juan Cespedesd65efa32003-02-03 00:22:30 +010044int opt_T = 0; /* show the time spent inside each call */
Juan Cespedes5e01f651998-03-08 22:31:44 +010045
46/* List of pids given to option -p: */
47struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */
48
Juan Cespedesac3db291998-04-25 14:31:58 +020049/* List of function names given to option -e: */
50struct opt_e_t * opt_e = NULL;
51int opt_e_enable=1;
52
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010053static void
54usage(void) {
Juan Cespedesac3db291998-04-25 14:31:58 +020055#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
56 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
57"Trace library calls of a given program.\n\n", progname);
58#else
59 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
60"Trace library calls of a given program.\n\n"
61
62# if HAVE_GETOPT_LONG
Juan Cespedesd65efa32003-02-03 00:22:30 +010063" -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020064# else
Juan Cespedesd65efa32003-02-03 00:22:30 +010065" -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020066# endif
Juan Cespedesd65efa32003-02-03 00:22:30 +010067" -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010068# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020069# if HAVE_GETOPT_LONG
70" -C, --demangle decode low-level symbol names into user-level names.\n"
71# else
72" -C decode low-level symbol names into user-level names.\n"
73# endif
74# endif
75# if HAVE_GETOPT_LONG
Juan Cespedesd65efa32003-02-03 00:22:30 +010076" -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020077# else
Juan Cespedesd65efa32003-02-03 00:22:30 +010078" -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020079# endif
Juan Cespedesac3db291998-04-25 14:31:58 +020080" -e expr modify which events to trace.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010081" -f follow forks.\n"
82# if HAVE_GETOPT_LONG
83" -h, --help display this help and exit.\n"
84# else
85" -h display this help and exit.\n"
86# endif
87" -i print instruction pointer at time of library call.\n"
88# if HAVE_GETOPT_LONG
89" -l, --library=FILE print library calls from this library only.\n"
90# else
91" -l FILE print library calls from this library only.\n"
92# endif
93" -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020094# if HAVE_GETOPT_LONG
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020095" -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
96# else
97" -n NR indent output by NR spaces for each call level nesting.\n"
98# endif
99# if HAVE_GETOPT_LONG
Juan Cespedesd65efa32003-02-03 00:22:30 +0100100" -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200101# else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100102" -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200103# endif
Juan Cespedesd65efa32003-02-03 00:22:30 +0100104" -p PID attach to the process with the process ID pid.\n"
105" -r print relative timestamps.\n"
106" -s STRLEN specify the maximum string size to print.\n"
107" -S display system calls.\n"
108" -t, -tt, -ttt print absolute timestamps.\n"
109" -T show the time spent inside each call.\n"
110" -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200111# if HAVE_GETOPT_LONG
112" -V, --version output version information and exit.\n"
113# else
114" -V output version information and exit.\n"
115# endif
116"\nReport bugs to Juan Cespedes <cespedes@debian.org>\n"
117 , progname);
118#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100119}
120
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100121static char *
122search_for_command(char * filename) {
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100123 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100124 char *path;
125 int m, n;
126
127 if (strchr(filename, '/')) {
128 return filename;
129 }
130 for (path = getenv("PATH"); path && *path; path += m) {
131 if (strchr(path, ':')) {
132 n = strchr(path, ':') - path;
133 m = n + 1;
134 } else {
135 m = n = strlen(path);
136 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100137 if (n + strlen(filename) + 1 >= PATH_MAX) {
138 fprintf(stderr, "Error: filename too long\n");
139 exit(1);
140 }
141 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100142 if (n && pathname[n - 1] != '/') {
143 pathname[n++] = '/';
144 }
145 strcpy(pathname + n, filename);
146 if (!access(pathname, X_OK)) {
147 return pathname;
148 }
149 }
150 return filename;
151}
152
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100153char **
154process_options(int argc, char **argv) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200155 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100156 output = stderr;
157
Juan Cespedesac3db291998-04-25 14:31:58 +0200158#if HAVE_GETOPT || HAVE_GETOPT_LONG
Juan Cespedes5e01f651998-03-08 22:31:44 +0100159 while(1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200160 int c;
161#if HAVE_GETOPT_LONG
162 int option_index = 0;
163 static struct option long_options[] = {
164 { "align", 1, 0, 'a'},
165 { "debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100166# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +0200167 { "demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200168#endif
Juan Cespedesd914a202004-11-10 00:15:33 +0100169 { "indent", 1, 0, 'n'},
Juan Cespedesac3db291998-04-25 14:31:58 +0200170 { "help", 0, 0, 'h'},
Juan Cespedesefe85f02004-04-04 01:31:38 +0200171 { "library", 1, 0, 'l'},
Juan Cespedesac3db291998-04-25 14:31:58 +0200172 { "output", 1, 0, 'o'},
173 { "version", 0, 0, 'V'},
174 { 0, 0, 0, 0}
175 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100176 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100177# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +0200178 "C"
179# endif
Juan Cespedesd65efa32003-02-03 00:22:30 +0100180 "a:e:l:n:o:p:s:u:", long_options, &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200181#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100182 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100183# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +0200184 "C"
185# endif
Juan Cespedesd65efa32003-02-03 00:22:30 +0100186 "a:e:l:n:o:p:s:u:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200187#endif
188 if (c==-1) {
189 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200191 switch(c) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100192 case 'a': opt_a = atoi(optarg);
193 break;
194 case 'c': opt_c++;
195 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100196#ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +0200197 case 'C': opt_C++;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100198 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200199#endif
Juan Cespedesd65efa32003-02-03 00:22:30 +0100200 case 'd': opt_d++;
201 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200202 case 'e':
203 {
204 char * str_e = strdup(optarg);
205 if (!str_e) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200206 perror("ltrace: strdup");
Juan Cespedesac3db291998-04-25 14:31:58 +0200207 exit(1);
208 }
209 if (str_e[0]=='!') {
210 opt_e_enable=0;
211 str_e++;
212 }
213 while(*str_e) {
214 struct opt_e_t * tmp;
215 char *str2 = strchr(str_e,',');
216 if (str2) {
217 *str2 = '\0';
218 }
219 tmp = malloc(sizeof(struct opt_e_t));
220 if (!tmp) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200221 perror("ltrace: malloc");
Juan Cespedesac3db291998-04-25 14:31:58 +0200222 exit(1);
223 }
224 tmp->name = str_e;
225 tmp->next = opt_e;
226 opt_e = tmp;
227 if (str2) {
228 str_e = str2+1;
229 } else {
230 break;
231 }
232 }
233 break;
234 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100235 case 'f': opt_f = 1;
236 break;
237 case 'h': usage();
238 exit(0);
239 case 'i': opt_i++;
240 break;
241 case 'l': if (library_num == MAX_LIBRARY) {
242 fprintf(stderr, "Too many libraries. Maximum is %i.\n", MAX_LIBRARY);
243 exit(1);
244 }
245 library[library_num++] = optarg;
246 break;
247 case 'L': opt_L = 0;
248 break;
249 case 'n': opt_n = atoi(optarg);
250 break;
251 case 'o': output = fopen(optarg, "w");
252 if (!output) {
253 fprintf(stderr, "Can't open %s for output: %s\n", optarg, strerror(errno));
254 exit(1);
255 }
256 setvbuf(output, (char *)NULL, _IOLBF, 0);
257 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
258 break;
259 case 'p':
260 {
261 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
262 if (!tmp) {
263 perror("ltrace: malloc");
264 exit(1);
265 }
266 tmp->pid = atoi(optarg);
267 tmp->next = opt_p;
268 opt_p = tmp;
269 break;
270 }
271 case 'r': opt_r++;
272 break;
273 case 's': opt_s = atoi(optarg);
274 break;
275 case 'S': opt_S = 1;
276 break;
277 case 't': opt_t++;
278 break;
279 case 'T': opt_T++;
280 break;
281 case 'u': opt_u = optarg;
282 break;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200283 case 'V': printf("ltrace version " VERSION ".\n"
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200284"Copyright (C) 1997-2004 Juan Cespedes <cespedes@debian.org>.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200285"This is free software; see the GNU General Public Licence\n"
286"version 2 or later for copying conditions. There is NO warranty.\n");
Juan Cespedesd65efa32003-02-03 00:22:30 +0100287 exit(0);
Juan Cespedesac3db291998-04-25 14:31:58 +0200288
289 default:
290#if HAVE_GETOPT_LONG
Juan Cespedesd65efa32003-02-03 00:22:30 +0100291 fprintf(stderr, "Try `%s --help' for more information\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200292#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100293 fprintf(stderr, "Try `%s -h' for more information\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200294#endif
Juan Cespedesd65efa32003-02-03 00:22:30 +0100295 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100296 }
297 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200298 argc -= optind; argv += optind;
299#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100300
Juan Cespedesac3db291998-04-25 14:31:58 +0200301 if (!opt_p && argc<1) {
302 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200303 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100304 exit(1);
305 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200306 if (opt_r && opt_t) {
307 fprintf(stderr, "%s: Incompatible options -r and -t\n", progname);
308 exit(1);
309 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200310 if (argc>0) {
311 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100312 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200313 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100314}