blob: 99a3baaae48ff73fc12e8c98fdc254e961f3ef73 [file] [log] [blame]
Juan Cespedes5e01f651998-03-08 22:31:44 +01001#include <string.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <limits.h>
6
7#include "ltrace.h"
8#include "options.h"
9#include "defs.h"
10
11FILE * output;
12int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */
13int opt_d = 0; /* debug */
14int opt_i = 0; /* instruction pointer */
15int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */
16int opt_S = 0; /* display syscalls */
17int opt_L = 1; /* display library calls */
18int opt_f = 0; /* trace child processes as they are created */
Juan Cespedese1887051998-03-10 00:08:41 +010019char * opt_u = NULL; /* username to run command as */
Juan Cespedes5e01f651998-03-08 22:31:44 +010020
21/* List of pids given to option -p: */
22struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */
23
24static void usage(void)
25{
26 fprintf(stderr, "Usage: ltrace [-dfiLS] [-a column] [-s strlen] [-o filename]\n"
Juan Cespedese1887051998-03-10 00:08:41 +010027 " [-u username] [-p pid] ... [command [arg ...]]\n\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +010028}
29
30static char * search_for_command(char * filename)
31{
32 static char pathname[PATH_MAX];
33 char *path;
34 int m, n;
35
36 if (strchr(filename, '/')) {
37 return filename;
38 }
39 for (path = getenv("PATH"); path && *path; path += m) {
40 if (strchr(path, ':')) {
41 n = strchr(path, ':') - path;
42 m = n + 1;
43 } else {
44 m = n = strlen(path);
45 }
46 strncpy(pathname, path, n);
47 if (n && pathname[n - 1] != '/') {
48 pathname[n++] = '/';
49 }
50 strcpy(pathname + n, filename);
51 if (!access(pathname, X_OK)) {
52 return pathname;
53 }
54 }
55 return filename;
56}
57
58char ** process_options(int argc, char **argv)
59{
60 char *nextchar = NULL;
61
62 output = stderr;
63
64 while(1) {
65 if (!nextchar || !(*nextchar)) {
66 if (!argv[1] || argv[1][0] != '-' || !argv[1][1]) {
67 break;
68 }
69 nextchar = &argv[1][1];
70 argc--; argv++;
71 }
72 switch (*nextchar++) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010073 case 'a': if (!argv[1]) { usage(); exit(1); }
74 opt_a = atoi(argv[1]);
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 argc--; argv++;
76 break;
77 case 'd': opt_d++;
78 break;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010079 case 'o': if (!argv[1]) { usage(); exit(1); }
80 output = fopen(argv[1], "w");
Juan Cespedes5e01f651998-03-08 22:31:44 +010081 if (!output) {
82 fprintf(stderr, "Can't open %s for output: %s\n", argv[1], strerror(errno));
83 exit(1);
84 }
85 argc--; argv++;
86 break;
87 case 'i': opt_i++;
88 break;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010089 case 's': if (!argv[1]) { usage(); exit(1); }
90 opt_s = atoi(argv[1]);
Juan Cespedes5e01f651998-03-08 22:31:44 +010091 argc--; argv++;
92 break;
93 case 'L': opt_L = 0;
94 break;
95 case 'S': opt_S = 1;
96 break;
97 case 'f': opt_f = 1;
98 break;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010099 case 'u': if (!argv[1]) { usage(); exit(1); }
100 opt_u = argv[1];
Juan Cespedese1887051998-03-10 00:08:41 +0100101 argc--; argv++;
102 break;
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100103 case 'p': if (!argv[1]) { usage(); exit(1); }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100104 {
105 struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t));
106 if (!tmp) {
107 perror("malloc");
108 exit(1);
109 }
110 tmp->pid = atoi(argv[1]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100111 tmp->next = opt_p;
112 opt_p = tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100113 argc--; argv++;
114 break;
115 }
116 default: fprintf(stderr, "Unknown option '%c'\n", *(nextchar-1));
117 usage();
118 exit(1);
119 }
120 }
121
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100122 if (!opt_p && argc<2) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100123 usage();
124 exit(1);
125 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100126 if (argc>1) {
127 command = search_for_command(argv[1]);
128 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100129 return &argv[1];
130}