ust: improve cmd line arg help, add --help and -h
[lttng-ust.git] / ust / ust.c
1 #include <unistd.h>
2 #include <getopt.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6
7 #include "ustcomm.h"
8
9 struct ust_opts {
10 char *cmd;
11 pid_t *pids;
12 int take_reply;
13 };
14
15 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
16 {
17 int c;
18 int digit_optind = 0;
19
20 opts->cmd = NULL;
21 opts->pids = NULL;
22 opts->take_reply = 0;
23
24 while (1) {
25 int this_option_optind = optind ? optind : 1;
26 int option_index = 0;
27 static struct option long_options[] = {
28 {"start-trace", 0, 0, 1000},
29 {"stop-trace", 0, 0, 1001},
30 {"destroy-trace", 0, 0, 1002},
31 {"list-markers", 0, 0, 1004},
32 {"print-markers", 0, 0, 1005},
33 {"pid", 1, 0, 1006},
34 {"enable-marker", 1, 0, 1007},
35 {"disable-marker", 1, 0, 1008},
36 {"start", 0, 0, 1009},
37 {"help", 0, 0, 'h'},
38 {"version", 0, 0, 1010},
39 {0, 0, 0, 0}
40 };
41
42 c = getopt_long(argc, argv, "h", long_options, &option_index);
43 if (c == -1)
44 break;
45
46 switch (c) {
47 case 0:
48 printf("option %s", long_options[option_index].name);
49 if (optarg)
50 printf(" with arg %s", optarg);
51 printf("\n");
52 break;
53
54 case 1000:
55 opts->cmd = strdup("trace_start");
56 break;
57 case 1001:
58 opts->cmd = strdup("trace_stop");
59 break;
60 case 1009:
61 opts->cmd = strdup("start");
62 break;
63 case 1002:
64 opts->cmd = strdup("trace_destroy");
65 break;
66 case 1004:
67 opts->cmd = strdup("list_markers");
68 opts->take_reply = 1;
69 break;
70 case 1007:
71 asprintf(&opts->cmd, "enable_marker %s", optarg);
72 break;
73 case 1008:
74 asprintf(&opts->cmd, "disable_marker %s", optarg);
75 break;
76 case 'h':
77 usage();
78 exit(0);
79 case 1010:
80 printf("Version 0\n");
81
82 default:
83 /* unknown option or other error; error is printed by getopt, just return */
84 return 1;
85 }
86 }
87
88 if(argc - optind > 0) {
89 int i;
90 int pididx=0;
91 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
92
93 for(i=optind; i<argc; i++) {
94 opts->pids[pididx++] = atoi(argv[i]);
95 }
96 opts->pids[pididx] = -1;
97 }
98
99 return 0;
100 }
101
102 char *progname = NULL;
103
104 void usage(void)
105 {
106 fprintf(stderr, "usage: %s [OPTIONS] COMMAND PID...\n", progname);
107 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
108 \n\
109 Commands:\n\
110 --start-trace\t\t\tStart tracing\n\
111 --stop-trace\t\t\tStop tracing\n\
112 --destroy-trace\t\t\tDestroy the trace\n\
113 --enable-marker CHANNEL/MARKER\tEnable a marker\n\
114 --disable-marker CHANNEL/MARKER\tDisable a marker\n\
115 --list-markers\tList the markers of the process and their state\n\
116 \n\
117 ");
118 }
119
120 int main(int argc, char *argv[])
121 {
122 pid_t *pidit;
123 //char *msg = argv[2];
124 struct ustcomm_connection conn;
125 int result;
126 struct ust_opts opts;
127
128 progname = argv[0];
129
130 if(argc <= 1) {
131 fprintf(stderr, "No operation specified.\n");
132 usage();
133 exit(EXIT_FAILURE);
134 }
135
136 result = parse_opts_long(argc, argv, &opts);
137 if(result) {
138 usage();
139 exit(EXIT_FAILURE);
140 }
141
142 if(opts.pids == NULL) {
143 fprintf(stderr, "No pid specified.\n");
144 usage();
145 exit(EXIT_FAILURE);
146 }
147 if(opts.cmd == NULL) {
148 fprintf(stderr, "No command specified.\n");
149 usage();
150 exit(EXIT_FAILURE);
151 }
152
153 pidit = opts.pids;
154
155 while(*pidit != -1) {
156 char *reply;
157 char **preply;
158
159 if(opts.take_reply)
160 preply = &reply;
161 else
162 preply = NULL;
163
164 result = ustcomm_connect_app(*pidit, &conn);
165 if(result) {
166 fprintf(stderr, "error connecting to process\n");
167 exit(EXIT_FAILURE);
168 }
169 ustcomm_send_request(&conn, opts.cmd, preply);
170
171 if(opts.take_reply)
172 printf("%s", reply);
173 pidit++;
174 }
175
176 free(opts.pids);
177 free(opts.cmd);
178
179 return 0;
180 }
This page took 0.040291 seconds and 4 git commands to generate.