disable ustctl --version until it is saner
[ust.git] / ustctl / ustctl.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24
25 #include "ustcomm.h"
26 #include "ustcmd.h"
27 #include "usterr.h"
28
29 enum command {
30 CREATE_TRACE,
31 START_TRACE,
32 STOP_TRACE,
33 START,
34 DESTROY,
35 LIST_MARKERS,
36 ENABLE_MARKER,
37 DISABLE_MARKER,
38 GET_ONLINE_PIDS,
39 SET_SUBBUF_SIZE,
40 SET_SUBBUF_NUM,
41 ALLOC_TRACE,
42 UNKNOWN
43 };
44
45 struct ust_opts {
46 enum command cmd;
47 pid_t *pids;
48 char *regex;
49 };
50
51 char *progname = NULL;
52
53 void usage(void)
54 {
55 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
56 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
57 \n\
58 Commands:\n\
59 --create-trace\t\t\tCreate trace\n\
60 --start-trace\t\t\tStart tracing\n\
61 --stop-trace\t\t\tStop tracing\n\
62 --destroy-trace\t\t\tDestroy the trace\n\
63 --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\
64 --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\
65 --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\
66 --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\
67 --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\
68 \
69 ");
70 }
71
72 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
73 {
74 int c;
75
76 opts->pids = NULL;
77 opts->regex = NULL;
78
79 while (1) {
80 int option_index = 0;
81 static struct option long_options[] = {
82 {"create-trace", 0, 0, 1012},
83 {"alloc-trace", 0, 0, 1015},
84 {"start-trace", 0, 0, 1000},
85 {"stop-trace", 0, 0, 1001},
86 {"destroy-trace", 0, 0, 1002},
87 {"list-markers", 0, 0, 1004},
88 {"print-markers", 0, 0, 1005},
89 {"pid", 1, 0, 1006},
90 {"enable-marker", 1, 0, 1007},
91 {"disable-marker", 1, 0, 1008},
92 {"start", 0, 0, 1009},
93 {"help", 0, 0, 'h'},
94 /*{"version", 0, 0, 1010},*/
95 {"online-pids", 0, 0, 1011},
96 {"set-subbuf-size", 1, 0, 1013},
97 {"set-subbuf-num", 1, 0, 1014},
98 {0, 0, 0, 0}
99 };
100
101 c = getopt_long(argc, argv, "h", long_options, &option_index);
102 if (c == -1)
103 break;
104
105 switch (c) {
106 case 0:
107 printf("option %s", long_options[option_index].name);
108 if (optarg)
109 printf(" with arg %s", optarg);
110 printf("\n");
111 break;
112
113 case 1000:
114 opts->cmd = START_TRACE;
115 break;
116 case 1001:
117 opts->cmd = STOP_TRACE;
118 break;
119 case 1009:
120 opts->cmd = START;
121 break;
122 case 1002:
123 opts->cmd = DESTROY;
124 break;
125 case 1004:
126 opts->cmd = LIST_MARKERS;
127 break;
128 case 1007:
129 opts->cmd = ENABLE_MARKER;
130 opts->regex = strdup(optarg);
131 break;
132 case 1008:
133 opts->cmd = DISABLE_MARKER;
134 opts->regex = strdup(optarg);
135 break;
136 case 1011:
137 opts->cmd = GET_ONLINE_PIDS;
138 break;
139 case 'h':
140 usage();
141 exit(0);
142 /*
143 case 1010:
144 printf("Version 0.1\n");
145 break;
146 */
147 case 1012:
148 opts->cmd = CREATE_TRACE;
149 break;
150 case 1013:
151 opts->cmd = SET_SUBBUF_SIZE;
152 opts->regex = strdup(optarg);
153 break;
154 case 1014:
155 opts->cmd = SET_SUBBUF_NUM;
156 opts->regex = strdup(optarg);
157 break;
158 case 1015:
159 opts->cmd = ALLOC_TRACE;
160 break;
161 default:
162 /* unknown option or other error; error is
163 printed by getopt, just return */
164 opts->cmd = UNKNOWN;
165 return 1;
166 }
167 }
168
169 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
170 int i;
171 int pididx=0;
172 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
173
174 for(i=optind; i<argc; i++) {
175 /* don't take any chances, use a long long */
176 long long tmp;
177 char *endptr;
178 tmp = strtoull(argv[i], &endptr, 10);
179 if(*endptr != '\0') {
180 ERR("The pid \"%s\" is invalid.", argv[i]);
181 return 1;
182 }
183 opts->pids[pididx++] = (pid_t) tmp;
184 }
185 opts->pids[pididx] = -1;
186 }
187
188 return 0;
189 }
190
191 int main(int argc, char *argv[])
192 {
193 pid_t *pidit;
194 int result;
195 struct ust_opts opts;
196
197 progname = argv[0];
198
199 if(argc <= 1) {
200 fprintf(stderr, "No operation specified.\n");
201 usage();
202 exit(EXIT_FAILURE);
203 }
204
205 result = parse_opts_long(argc, argv, &opts);
206 if(result) {
207 fprintf(stderr, "\n");
208 usage();
209 exit(EXIT_FAILURE);
210 }
211
212 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
213 fprintf(stderr, "No pid specified.\n");
214 usage();
215 exit(EXIT_FAILURE);
216 }
217 if(opts.cmd == UNKNOWN) {
218 fprintf(stderr, "No command specified.\n");
219 usage();
220 exit(EXIT_FAILURE);
221 }
222 if (opts.cmd == GET_ONLINE_PIDS) {
223 pid_t *pp = ustcmd_get_online_pids();
224 unsigned int i = 0;
225
226 if (pp) {
227 while (pp[i] != 0) {
228 printf("%u\n", (unsigned int) pp[i]);
229 ++i;
230 }
231 free(pp);
232 }
233
234 exit(EXIT_SUCCESS);
235 }
236
237 pidit = opts.pids;
238 struct marker_status *cmsf = NULL;
239
240 while(*pidit != -1) {
241 switch (opts.cmd) {
242 case CREATE_TRACE:
243 result = ustcmd_create_trace(*pidit);
244 if (result) {
245 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
246 break;
247 }
248 break;
249
250 case START_TRACE:
251 result = ustcmd_start_trace(*pidit);
252 if (result) {
253 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
254 break;
255 }
256 //printf("sucessfully started trace for PID %u\n", (unsigned int) *pidit);
257 break;
258
259 case STOP_TRACE:
260 result = ustcmd_stop_trace(*pidit);
261 if (result) {
262 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
263 break;
264 }
265 //printf("sucessfully stopped trace for PID %u\n", (unsigned int) *pidit);
266 break;
267
268 case START:
269 result = ustcmd_setup_and_start(*pidit);
270 if (result) {
271 ERR("error while trying to setup/start trace for PID %u\n", (unsigned int) *pidit);
272 break;
273 }
274 //printf("sucessfully setup/started trace for PID %u\n", (unsigned int) *pidit);
275 break;
276
277 case DESTROY:
278 result = ustcmd_destroy_trace(*pidit);
279 if (result) {
280 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
281 break;
282 }
283 //printf("sucessfully destroyed trace for PID %u\n", (unsigned int) *pidit);
284 break;
285
286 case LIST_MARKERS:
287 cmsf = NULL;
288 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
289 fprintf(stderr,
290 "error while trying to list markers for"
291 " PID %u\n", (unsigned int) *pidit);
292 break;
293 }
294 unsigned int i = 0;
295 while (cmsf[i].channel != NULL) {
296 printf("{PID: %u, channel/marker: %s/%s, "
297 "state: %u, fmt: %s}\n",
298 (unsigned int) *pidit,
299 cmsf[i].channel,
300 cmsf[i].marker,
301 cmsf[i].state,
302 cmsf[i].fs);
303 ++i;
304 }
305 ustcmd_free_cmsf(cmsf);
306 break;
307
308 case ENABLE_MARKER:
309 if(opts.regex)
310 ustcmd_set_marker_state(opts.regex, 1, *pidit);
311 break;
312 case DISABLE_MARKER:
313 if(opts.regex)
314 ustcmd_set_marker_state(opts.regex, 0, *pidit);
315 break;
316
317 case SET_SUBBUF_SIZE:
318 ustcmd_set_subbuf_size(opts.regex, *pidit);
319 break;
320
321 case SET_SUBBUF_NUM:
322 ustcmd_set_subbuf_num(opts.regex, *pidit);
323 break;
324
325 case ALLOC_TRACE:
326 result = ustcmd_alloc_trace(*pidit);
327 if (result) {
328 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit);
329 break;
330 }
331 break;
332
333 default:
334 ERR("unknown command\n");
335 break;
336 }
337
338 pidit++;
339 }
340
341 if (opts.pids != NULL) {
342 free(opts.pids);
343 }
344 if (opts.regex != NULL) {
345 free(opts.regex);
346 }
347
348 return 0;
349 }
350
This page took 0.035603 seconds and 4 git commands to generate.