Add trace name handling throughout tracectl, ustcomm and ustcmd
[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 "ust/ustcmd.h"
26 #include "usterr.h"
27
28 enum command {
29 CREATE_TRACE=1000,
30 ALLOC_TRACE,
31 START_TRACE,
32 STOP_TRACE,
33 DESTROY_TRACE,
34 LIST_MARKERS,
35 LIST_TRACE_EVENTS,
36 ENABLE_MARKER,
37 DISABLE_MARKER,
38 GET_ONLINE_PIDS,
39 SET_SUBBUF_SIZE,
40 SET_SUBBUF_NUM,
41 GET_SUBBUF_SIZE,
42 GET_SUBBUF_NUM,
43 GET_SOCK_PATH,
44 SET_SOCK_PATH,
45 FORCE_SWITCH,
46 UNKNOWN
47 };
48
49 struct ust_opts {
50 enum command cmd;
51 pid_t *pids;
52 char *regex;
53 };
54
55 char *progname = NULL;
56
57 void usage(void)
58 {
59 fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname);
60 fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\
61 \n\
62 Commands:\n\
63 --create-trace\t\t\tCreate trace\n\
64 --alloc-trace\t\t\tAlloc trace\n\
65 --start-trace\t\t\tStart tracing\n\
66 --stop-trace\t\t\tStop tracing\n\
67 --destroy-trace\t\t\tDestroy the trace\n\
68 --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\
69 --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\
70 --set-sock-path\t\t\tSet the path of the daemon socket\n\
71 --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\
72 --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\
73 --get-sock-path\t\t\tGet the path of the daemon socket\n\
74 --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\
75 --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\
76 --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\
77 --list-trace-events\t\t\tList the trace-events of the process\n\
78 --force-switch\t\t\tForce a subbuffer switch\n\
79 \
80 ");
81 }
82
83 int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
84 {
85 int c;
86
87 opts->pids = NULL;
88 opts->regex = NULL;
89
90 while (1) {
91 int option_index = 0;
92 static struct option long_options[] = {
93 { "create-trace", 0, 0, CREATE_TRACE },
94 { "alloc-trace", 0, 0, ALLOC_TRACE },
95 { "start-trace", 0, 0, START_TRACE },
96 { "stop-trace", 0, 0, STOP_TRACE },
97 { "destroy-trace", 0, 0, DESTROY_TRACE },
98 { "list-markers", 0, 0, LIST_MARKERS },
99 { "list-trace-events", 0, 0, LIST_TRACE_EVENTS},
100 { "enable-marker", 1, 0, ENABLE_MARKER },
101 { "disable-marker", 1, 0, DISABLE_MARKER },
102 { "help", 0, 0, 'h' },
103 { "online-pids", 0, 0, GET_ONLINE_PIDS },
104 { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE },
105 { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM },
106 { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE },
107 { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM },
108 { "get-sock-path", 0, 0, GET_SOCK_PATH },
109 { "set-sock-path", 1, 0, SET_SOCK_PATH },
110 { "force-switch", 0, 0, FORCE_SWITCH },
111 { 0, 0, 0, 0 }
112 };
113
114 c = getopt_long(argc, argv, "h", long_options, &option_index);
115 if (c == -1)
116 break;
117
118 if(c >= 1000)
119 opts->cmd = c;
120
121 switch (c) {
122 case 0:
123 printf("option %s", long_options[option_index].name);
124 if (optarg)
125 printf(" with arg %s", optarg);
126 printf("\n");
127 break;
128
129 case ENABLE_MARKER:
130 case DISABLE_MARKER:
131 case SET_SUBBUF_SIZE:
132 case SET_SUBBUF_NUM:
133 case GET_SUBBUF_SIZE:
134 case GET_SUBBUF_NUM:
135 case SET_SOCK_PATH:
136 opts->regex = strdup(optarg);
137 break;
138
139 case 'h':
140 usage();
141 exit(0);
142
143 case '?':
144 fprintf(stderr, "Invalid argument\n\n");
145 usage();
146 exit(1);
147 }
148 }
149
150 if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
151 int i;
152 int pididx=0;
153 opts->pids = zmalloc((argc-optind+1) * sizeof(pid_t));
154
155 for(i=optind; i<argc; i++) {
156 /* don't take any chances, use a long long */
157 long long tmp;
158 char *endptr;
159 tmp = strtoull(argv[i], &endptr, 10);
160 if(*endptr != '\0') {
161 ERR("The pid \"%s\" is invalid.", argv[i]);
162 return 1;
163 }
164 opts->pids[pididx++] = (pid_t) tmp;
165 }
166 opts->pids[pididx] = -1;
167 }
168
169 return 0;
170 }
171
172 static int scan_ch_marker(const char *channel_marker, char **channel,
173 char **marker)
174 {
175 int result;
176
177 *channel = NULL;
178 *marker = NULL;
179
180 result = sscanf(channel_marker, "%a[^/]/%as", channel, marker);
181 if (result != 2) {
182 if (errno) {
183 PERROR("Failed to read channel and marker names");
184 } else {
185 ERR("Failed to parse marker and channel names");
186 }
187 if (*channel) {
188 free(*channel);
189 }
190 if (*marker) {
191 free(*marker);
192 }
193 return -1;
194 } else {
195 return 0;
196 }
197 }
198
199 static int scan_ch_and_num(const char *ch_num, char **channel, unsigned int *num)
200 {
201 int result;
202
203 *channel = NULL;
204
205 result = sscanf(ch_num, "%a[^/]/%u", channel, num);
206 if (result != 2) {
207 if (errno) {
208 PERROR("Failed to parse channel and number");
209 } else {
210 ERR("Failed to parse channel and number");
211 }
212 if (*channel) {
213 free(*channel);
214 }
215 return -1;
216 }
217 }
218
219 char *trace = "auto";
220
221 int main(int argc, char *argv[])
222 {
223 pid_t *pidit;
224 int result;
225 int retval = EXIT_SUCCESS;
226 char *tmp;
227 struct ust_opts opts;
228
229 progname = argv[0];
230
231 if(argc <= 1) {
232 fprintf(stderr, "No operation specified.\n");
233 usage();
234 exit(EXIT_FAILURE);
235 }
236
237 result = parse_opts_long(argc, argv, &opts);
238 if(result) {
239 fprintf(stderr, "\n");
240 usage();
241 exit(EXIT_FAILURE);
242 }
243
244 if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) {
245 fprintf(stderr, "No pid specified.\n");
246 usage();
247 exit(EXIT_FAILURE);
248 }
249 if(opts.cmd == UNKNOWN) {
250 fprintf(stderr, "No command specified.\n");
251 usage();
252 exit(EXIT_FAILURE);
253 }
254 if (opts.cmd == GET_ONLINE_PIDS) {
255 pid_t *pp = ustcmd_get_online_pids();
256 unsigned int i = 0;
257
258 if (pp) {
259 while (pp[i] != 0) {
260 printf("%u\n", (unsigned int) pp[i]);
261 ++i;
262 }
263 free(pp);
264 }
265
266 exit(EXIT_SUCCESS);
267 }
268
269 pidit = opts.pids;
270 struct marker_status *cmsf = NULL;
271 struct trace_event_status *tes = NULL;
272 unsigned int i = 0;
273
274 while(*pidit != -1) {
275 switch (opts.cmd) {
276 case CREATE_TRACE:
277 result = ustcmd_create_trace(trace, *pidit);
278 if (result) {
279 ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
280 retval = EXIT_FAILURE;
281 break;
282 }
283 break;
284
285 case START_TRACE:
286 result = ustcmd_start_trace(trace, *pidit);
287 if (result) {
288 ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
289 retval = EXIT_FAILURE;
290 break;
291 }
292 break;
293
294 case STOP_TRACE:
295 result = ustcmd_stop_trace(trace, *pidit);
296 if (result) {
297 ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
298 retval = EXIT_FAILURE;
299 break;
300 }
301 break;
302
303 case DESTROY_TRACE:
304 result = ustcmd_destroy_trace(trace, *pidit);
305 if (result) {
306 ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
307 retval = EXIT_FAILURE;
308 break;
309 }
310 break;
311
312 case LIST_MARKERS:
313 cmsf = NULL;
314 if (ustcmd_get_cmsf(&cmsf, *pidit)) {
315 ERR("error while trying to list markers for PID %u\n", (unsigned int) *pidit);
316 retval = EXIT_FAILURE;
317 break;
318 }
319 i = 0;
320 while (cmsf[i].channel != NULL) {
321 printf("{PID: %u, channel/marker: %s/%s, "
322 "state: %u, fmt: %s}\n",
323 (unsigned int) *pidit,
324 cmsf[i].channel,
325 cmsf[i].marker,
326 cmsf[i].state,
327 cmsf[i].fs);
328 ++i;
329 }
330 ustcmd_free_cmsf(cmsf);
331 break;
332
333 case LIST_TRACE_EVENTS:
334 tes = NULL;
335 if (ustcmd_get_tes(&tes, *pidit)) {
336 ERR("error while trying to list "
337 "trace_events for PID %u\n",
338 (unsigned int) *pidit);
339 break;
340 }
341 i = 0;
342 while (tes[i].name != NULL) {
343 printf("{PID: %u, trace_event: %s}\n",
344 (unsigned int) *pidit,
345 tes[i].name);
346 ++i;
347 }
348 ustcmd_free_tes(tes);
349
350 break;
351 case ENABLE_MARKER:
352 if (opts.regex) {
353 char *channel, *marker;
354
355 if (scan_ch_marker(opts.regex,
356 &channel, &marker)) {
357 retval = EXIT_FAILURE;
358 break;
359 }
360 if (ustcmd_set_marker_state(trace, channel, marker, 1, *pidit)) {
361 PERROR("error while trying to enable marker %s with PID %u",
362 opts.regex, (unsigned int) *pidit);
363 retval = EXIT_FAILURE;
364 }
365 }
366
367 break;
368 case DISABLE_MARKER:
369 if (opts.regex) {
370 char *channel, *marker;
371
372 if (scan_ch_marker(opts.regex,
373 &channel, &marker)) {
374 retval = EXIT_FAILURE;
375 break;
376 }
377 if (ustcmd_set_marker_state(trace, channel, marker, 0, *pidit)) {
378 ERR("error while trying to disable marker %s with PID %u\n",
379 opts.regex, (unsigned int) *pidit);
380 retval = EXIT_FAILURE;
381 }
382 }
383 break;
384
385 case SET_SUBBUF_SIZE:
386 if (opts.regex) {
387 char *channel;
388 unsigned int size;
389 if (scan_ch_and_num(opts.regex, &channel, &size)) {
390 retval = EXIT_FAILURE;
391 break;
392 }
393
394 if (ustcmd_set_subbuf_size(trace, channel, size, *pidit)) {
395 ERR("error while trying to set the size of subbuffers with PID %u\n",
396 (unsigned int) *pidit);
397 retval = EXIT_FAILURE;
398 }
399 }
400 break;
401
402 case SET_SUBBUF_NUM:
403 if (opts.regex) {
404 char *channel;
405 unsigned int num;
406 if (scan_ch_and_num(opts.regex, &channel, &num)) {
407 retval = EXIT_FAILURE;
408 break;
409 }
410
411 if (num < 2) {
412 ERR("Subbuffer count should be greater or equal to 2");
413 retval = EXIT_FAILURE;
414 break;
415 }
416 if (ustcmd_set_subbuf_num(trace, channel, num, *pidit)) {
417 ERR("error while trying to set the number of subbuffers with PID %u\n",
418 (unsigned int) *pidit);
419 retval = EXIT_FAILURE;
420 }
421 }
422 break;
423
424 case GET_SUBBUF_SIZE:
425 result = ustcmd_get_subbuf_size(trace, opts.regex, *pidit);
426 if (result == -1) {
427 ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit);
428 retval = EXIT_FAILURE;
429 break;
430 }
431
432 printf("the size of subbufers is %d\n", result);
433 break;
434
435 case GET_SUBBUF_NUM:
436 result = ustcmd_get_subbuf_num(trace, opts.regex, *pidit);
437 if (result == -1) {
438 ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit);
439 retval = EXIT_FAILURE;
440 break;
441 }
442
443 printf("the number of subbufers is %d\n", result);
444 break;
445
446 case ALLOC_TRACE:
447 result = ustcmd_alloc_trace(trace, *pidit);
448 if (result) {
449 ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit);
450 retval = EXIT_FAILURE;
451 }
452 break;
453
454 case GET_SOCK_PATH:
455 result = ustcmd_get_sock_path(&tmp, *pidit);
456 if (result) {
457 ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit);
458 retval = EXIT_FAILURE;
459 break;
460 }
461 printf("the socket path is %s\n", tmp);
462 free(tmp);
463 break;
464
465 case SET_SOCK_PATH:
466 result = ustcmd_set_sock_path(opts.regex, *pidit);
467 if (result) {
468 ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit);
469 retval = EXIT_FAILURE;
470 }
471 break;
472
473 case FORCE_SWITCH:
474 result = ustcmd_force_switch(*pidit);
475 if (result) {
476 ERR("error while trying to force switch for PID %u\n", (unsigned int) *pidit);
477 retval = EXIT_FAILURE;
478 }
479 break;
480
481 default:
482 ERR("unknown command\n");
483 retval = EXIT_FAILURE;
484 break;
485 }
486
487 pidit++;
488 }
489
490 if (opts.pids != NULL) {
491 free(opts.pids);
492 }
493 if (opts.regex != NULL) {
494 free(opts.regex);
495 }
496
497 return retval;
498 }
499
This page took 0.03872 seconds and 4 git commands to generate.