Refactor: embed mi in "list_output" to remove code duplication
[lttng-tools.git] / src / bin / lttng / commands / snapshot.c
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <inttypes.h>
21 #include <popt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <assert.h>
29
30 #include <common/utils.h>
31 #include <common/mi-lttng.h>
32 #include <lttng/snapshot.h>
33
34 #include "../command.h"
35
36 static const char *opt_session_name;
37 static const char *opt_output_name;
38 static const char *opt_data_url;
39 static const char *opt_ctrl_url;
40 static const char *current_session_name;
41 static uint64_t opt_max_size;
42
43 /* Stub for the cmd struct actions. */
44 static int cmd_add_output(int argc, const char **argv);
45 static int cmd_del_output(int argc, const char **argv);
46 static int cmd_list_output(int argc, const char **argv);
47 static int cmd_record(int argc, const char **argv);
48
49 static const char *indent4 = " ";
50
51 enum {
52 OPT_HELP = 1,
53 OPT_LIST_OPTIONS,
54 OPT_MAX_SIZE,
55 OPT_LIST_COMMANDS,
56 };
57
58 static struct mi_writer *writer;
59
60 static struct poptOption snapshot_opts[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
62 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
63 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
64 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
65 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
66 {"name", 'n', POPT_ARG_STRING, &opt_output_name, 0, 0, 0},
67 {"max-size", 'm', POPT_ARG_STRING, 0, OPT_MAX_SIZE, 0, 0},
68 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
69 {"list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS},
70 {0, 0, 0, 0, 0, 0, 0}
71 };
72
73 static struct cmd_struct actions[] = {
74 { "add-output", cmd_add_output },
75 { "del-output", cmd_del_output },
76 { "list-output", cmd_list_output },
77 { "record", cmd_record },
78 { NULL, NULL } /* Array closure */
79 };
80
81 /*
82 * Count and return the number of arguments in argv.
83 */
84 static int count_arguments(const char **argv)
85 {
86 int i = 0;
87
88 assert(argv);
89
90 while (argv[i] != NULL) {
91 i++;
92 }
93
94 return i;
95 }
96
97 /*
98 * Create a snapshot output object from arguments using the given URL.
99 *
100 * Return a newly allocated object or NULL on error.
101 */
102 static struct lttng_snapshot_output *create_output_from_args(const char *url)
103 {
104 int ret = 0;
105 struct lttng_snapshot_output *output = NULL;
106
107 output = lttng_snapshot_output_create();
108 if (!output) {
109 goto error_create;
110 }
111
112 if (url) {
113 ret = lttng_snapshot_output_set_ctrl_url(url, output);
114 if (ret < 0) {
115 goto error;
116 }
117 } else if (opt_ctrl_url) {
118 ret = lttng_snapshot_output_set_ctrl_url(opt_ctrl_url, output);
119 if (ret < 0) {
120 goto error;
121 }
122 }
123
124 if (opt_data_url) {
125 ret = lttng_snapshot_output_set_data_url(opt_data_url, output);
126 if (ret < 0) {
127 goto error;
128 }
129 }
130
131 if (opt_max_size) {
132 ret = lttng_snapshot_output_set_size(opt_max_size, output);
133 if (ret < 0) {
134 goto error;
135 }
136 }
137
138 if (opt_output_name) {
139 ret = lttng_snapshot_output_set_name(opt_output_name, output);
140 if (ret < 0) {
141 goto error;
142 }
143 }
144
145 return output;
146
147 error:
148 lttng_snapshot_output_destroy(output);
149 error_create:
150 return NULL;
151 }
152
153 static int list_output(void)
154 {
155 int ret, output_seen = 0;
156 struct lttng_snapshot_output *s_iter;
157 struct lttng_snapshot_output_list *list;
158
159 ret = lttng_snapshot_list_output(current_session_name, &list);
160 if (ret < 0) {
161 goto error;
162 }
163
164 MSG("Snapshot output list for session %s", current_session_name);
165
166 if (lttng_opt_mi) {
167 ret = mi_lttng_snapshot_output_session_name(writer,
168 current_session_name);
169 if (ret) {
170 ret = CMD_ERROR;
171 goto end;
172 }
173 }
174
175 while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) {
176 MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4,
177 lttng_snapshot_output_get_id(s_iter),
178 lttng_snapshot_output_get_name(s_iter),
179 lttng_snapshot_output_get_ctrl_url(s_iter),
180 lttng_snapshot_output_get_maxsize(s_iter));
181 output_seen = 1;
182 if (lttng_opt_mi) {
183 ret = mi_lttng_snapshot_list_output(writer, s_iter);
184 if (ret) {
185 ret = CMD_ERROR;
186 goto end;
187 }
188 }
189 }
190
191 if (lttng_opt_mi) {
192 /* Close snapshot snapshots element */
193 ret = mi_lttng_writer_close_element(writer);
194 if (ret) {
195 ret = CMD_ERROR;
196 goto end;
197 }
198
199 /* Close snapshot session element */
200 ret = mi_lttng_writer_close_element(writer);
201 if (ret) {
202 ret = CMD_ERROR;
203 }
204 }
205 end:
206 lttng_snapshot_output_list_destroy(list);
207
208 if (!output_seen) {
209 MSG("%sNone", indent4);
210 }
211
212 error:
213 return ret;
214 }
215
216 /*
217 * Delete output by ID (machine interface version).
218 */
219 static int mi_del_output(uint32_t id, const char *name)
220 {
221 int ret;
222 struct lttng_snapshot_output *output = NULL;
223
224 assert(writer);
225
226 output = lttng_snapshot_output_create();
227 if (!output) {
228 ret = CMD_FATAL;
229 goto error;
230 }
231
232 if (name) {
233 ret = lttng_snapshot_output_set_name(name, output);
234 } else if (id != UINT32_MAX) {
235 ret = lttng_snapshot_output_set_id(id, output);
236 } else {
237 ret = CMD_ERROR;
238 goto error;
239 }
240 if (ret < 0) {
241 ret = CMD_FATAL;
242 goto error;
243 }
244
245 ret = lttng_snapshot_del_output(current_session_name, output);
246 if (ret < 0) {
247 goto error;
248 }
249
250 ret = mi_lttng_snapshot_del_output(writer, id, name, current_session_name);
251 if (ret) {
252 ret = CMD_ERROR;
253 }
254
255 error:
256 lttng_snapshot_output_destroy(output);
257 return ret;
258 }
259
260 /*
261 * Delete output by ID.
262 */
263 static int del_output(uint32_t id, const char *name)
264 {
265 int ret;
266 struct lttng_snapshot_output *output = NULL;
267
268 output = lttng_snapshot_output_create();
269 if (!output) {
270 ret = CMD_FATAL;
271 goto error;
272 }
273
274 if (name) {
275 ret = lttng_snapshot_output_set_name(name, output);
276 } else if (id != UINT32_MAX) {
277 ret = lttng_snapshot_output_set_id(id, output);
278 } else {
279 ret = CMD_ERROR;
280 goto error;
281 }
282 if (ret < 0) {
283 ret = CMD_FATAL;
284 goto error;
285 }
286
287 ret = lttng_snapshot_del_output(current_session_name, output);
288 if (ret < 0) {
289 goto error;
290 }
291
292 if (id != UINT32_MAX) {
293 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
294 id, current_session_name);
295 } else {
296 MSG("Snapshot output %s successfully deleted for session %s",
297 name, current_session_name);
298 }
299
300 error:
301 lttng_snapshot_output_destroy(output);
302 return ret;
303 }
304
305 /*
306 * Add output from the user URL (machine interface).
307 */
308 static int mi_add_output(const char *url)
309 {
310 int ret;
311 struct lttng_snapshot_output *output = NULL;
312 char name[NAME_MAX];
313 const char *n_ptr;
314
315 if (!url && (!opt_data_url || !opt_ctrl_url)) {
316 ret = CMD_ERROR;
317 goto error;
318 }
319
320 output = create_output_from_args(url);
321 if (!output) {
322 ret = CMD_FATAL;
323 goto error;
324 }
325
326 /* This call, if successful, populates the id of the output object. */
327 ret = lttng_snapshot_add_output(current_session_name, output);
328 if (ret < 0) {
329 goto error;
330 }
331
332 n_ptr = lttng_snapshot_output_get_name(output);
333 if (*n_ptr == '\0') {
334 int pret;
335 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
336 lttng_snapshot_output_get_id(output));
337 if (pret < 0) {
338 PERROR("snprintf add output name");
339 }
340 n_ptr = name;
341 }
342
343 ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr,
344 output);
345 if (ret) {
346 ret = CMD_ERROR;
347 }
348
349 error:
350 lttng_snapshot_output_destroy(output);
351 return ret;
352 }
353
354 /*
355 * Add output from the user URL.
356 */
357 static int add_output(const char *url)
358 {
359 int ret;
360 struct lttng_snapshot_output *output = NULL;
361 char name[NAME_MAX];
362 const char *n_ptr;
363
364 if (!url && (!opt_data_url || !opt_ctrl_url)) {
365 ret = CMD_ERROR;
366 goto error;
367 }
368
369 output = create_output_from_args(url);
370 if (!output) {
371 ret = CMD_FATAL;
372 goto error;
373 }
374
375 /* This call, if successful, populates the id of the output object. */
376 ret = lttng_snapshot_add_output(current_session_name, output);
377 if (ret < 0) {
378 goto error;
379 }
380
381 n_ptr = lttng_snapshot_output_get_name(output);
382 if (*n_ptr == '\0') {
383 int pret;
384 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
385 lttng_snapshot_output_get_id(output));
386 if (pret < 0) {
387 PERROR("snprintf add output name");
388 }
389 n_ptr = name;
390 }
391
392 MSG("Snapshot output successfully added for session %s",
393 current_session_name);
394 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
395 lttng_snapshot_output_get_id(output), n_ptr,
396 lttng_snapshot_output_get_ctrl_url(output),
397 lttng_snapshot_output_get_maxsize(output));
398 error:
399 lttng_snapshot_output_destroy(output);
400 return ret;
401 }
402
403 static int cmd_add_output(int argc, const char **argv)
404 {
405 int ret;
406
407 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
408 ret = CMD_ERROR;
409 goto end;
410 }
411
412 if (lttng_opt_mi) {
413 ret = mi_add_output(argv[1]);
414 } else {
415 ret = add_output(argv[1]);
416 }
417
418 end:
419 return ret;
420 }
421
422 static int cmd_del_output(int argc, const char **argv)
423 {
424 int ret;
425 char *name;
426 long id;
427
428 if (argc < 2) {
429 ret = CMD_ERROR;
430 goto end;
431 }
432
433 errno = 0;
434 id = strtol(argv[1], &name, 10);
435 if (id == 0 && errno == 0) {
436 if (lttng_opt_mi) {
437 ret = mi_del_output(UINT32_MAX, name);
438 } else {
439 ret = del_output(UINT32_MAX, name);
440 }
441 } else if (errno == 0 && *name == '\0') {
442 if (lttng_opt_mi) {
443 ret = mi_del_output(id, NULL);
444 } else {
445 ret = del_output(id, NULL);
446 }
447 } else {
448 ERR("Argument %s not recognized", argv[1]);
449 ret = -1;
450 goto end;
451 }
452
453 end:
454 return ret;
455 }
456
457 static int cmd_list_output(int argc, const char **argv)
458 {
459 int ret;
460
461 ret = list_output();
462
463 return ret;
464 }
465
466 /*
467 * Do a snapshot record with the URL if one is given (machine interface).
468 */
469 static int mi_record(const char *url)
470 {
471 int ret;
472 struct lttng_snapshot_output *output = NULL;
473
474 output = create_output_from_args(url);
475 if (!output) {
476 ret = CMD_FATAL;
477 goto error;
478 }
479
480 ret = lttng_snapshot_record(current_session_name, output, 0);
481 if (ret < 0) {
482 ret = CMD_ERROR;
483 goto error;
484 }
485
486 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
487 opt_ctrl_url, opt_data_url);
488 if (ret) {
489 ret = CMD_ERROR;
490 }
491
492 error:
493 lttng_snapshot_output_destroy(output);
494 return ret;
495 }
496
497 /*
498 * Do a snapshot record with the URL if one is given.
499 */
500 static int record(const char *url)
501 {
502 int ret;
503 struct lttng_snapshot_output *output = NULL;
504
505 output = create_output_from_args(url);
506 if (!output) {
507 ret = CMD_FATAL;
508 goto error;
509 }
510
511 ret = lttng_snapshot_record(current_session_name, output, 0);
512 if (ret < 0) {
513 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
514 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
515 }
516 goto error;
517 }
518
519 MSG("Snapshot recorded successfully for session %s", current_session_name);
520
521 if (url) {
522 MSG("Snapshot written at: %s", url);
523 } else if (opt_ctrl_url) {
524 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
525 opt_data_url);
526 }
527
528 error:
529 lttng_snapshot_output_destroy(output);
530 return ret;
531 }
532
533 static int cmd_record(int argc, const char **argv)
534 {
535 int ret;
536
537 if (argc == 2) {
538 /* With a given URL */
539 if (lttng_opt_mi) {
540 ret = mi_record(argv[1]);
541 } else {
542 ret = record(argv[1]);
543 }
544 } else {
545 if (lttng_opt_mi) {
546 ret = mi_record(NULL);
547 } else {
548 ret = record(NULL);
549 }
550 }
551
552 return ret;
553 }
554
555 static int handle_command(const char **argv)
556 {
557 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
558 struct cmd_struct *cmd;
559
560 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
561 (opt_ctrl_url && !opt_data_url)) {
562 command_ret = CMD_ERROR;
563 goto end;
564 }
565
566 argc = count_arguments(argv);
567
568 cmd = &actions[i];
569 while (cmd->func != NULL) {
570 /* Find command */
571 if (strcmp(argv[0], cmd->name) == 0) {
572 if (lttng_opt_mi) {
573 /* Action element */
574 ret = mi_lttng_writer_open_element(writer,
575 mi_lttng_element_command_action);
576 if (ret) {
577 ret = CMD_ERROR;
578 goto end;
579 }
580
581 /* Name of the action */
582 ret = mi_lttng_writer_write_element_string(writer,
583 config_element_name, argv[0]);
584 if (ret) {
585 ret = CMD_ERROR;
586 goto end;
587 }
588
589 /* Open output element */
590 ret = mi_lttng_writer_open_element(writer,
591 mi_lttng_element_command_output);
592 if (ret) {
593 ret = CMD_ERROR;
594 goto end;
595 }
596 }
597
598 command_ret = cmd->func(argc, argv);
599
600 if (lttng_opt_mi) {
601 /* Close output and action element */
602 ret = mi_lttng_close_multi_element(writer, 2);
603 if (ret) {
604 ret = CMD_ERROR;
605 goto end;
606 }
607 }
608 goto end;
609 }
610 i++;
611 cmd = &actions[i];
612 }
613
614 ret = CMD_UNDEFINED;
615
616 end:
617 /* Overwrite ret if an error occurred in cmd->func() */
618 ret = command_ret ? command_ret : ret;
619 return ret;
620 }
621 /*
622 * The 'snapshot <cmd> <options>' first level command
623 */
624 int cmd_snapshot(int argc, const char **argv)
625 {
626 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
627 char *session_name = NULL;
628 static poptContext pc;
629
630 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
631 poptReadDefaultConfig(pc, 0);
632
633 /* Mi check */
634 if (lttng_opt_mi) {
635 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
636 if (!writer) {
637 ret = -LTTNG_ERR_NOMEM;
638 goto end;
639 }
640
641 /* Open command element */
642 ret = mi_lttng_writer_command_open(writer,
643 mi_lttng_element_command_snapshot);
644 if (ret) {
645 ret = CMD_ERROR;
646 goto end;
647 }
648
649 /* Open output element */
650 ret = mi_lttng_writer_open_element(writer,
651 mi_lttng_element_command_output);
652 if (ret) {
653 ret = CMD_ERROR;
654 goto end;
655 }
656 }
657
658 while ((opt = poptGetNextOpt(pc)) != -1) {
659 switch (opt) {
660 case OPT_HELP:
661 SHOW_HELP();
662 goto end;
663 case OPT_LIST_OPTIONS:
664 list_cmd_options(stdout, snapshot_opts);
665 goto end;
666 case OPT_LIST_COMMANDS:
667 list_commands(actions, stdout);
668 goto end;
669 case OPT_MAX_SIZE:
670 {
671 uint64_t val;
672 const char *opt = poptGetOptArg(pc);
673
674 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
675 ERR("Unable to handle max-size value %s", opt);
676 ret = CMD_ERROR;
677 goto end;
678 }
679
680 opt_max_size = val;
681
682 break;
683 }
684 default:
685 ret = CMD_UNDEFINED;
686 goto end;
687 }
688 }
689
690 if (!opt_session_name) {
691 session_name = get_session_name();
692 if (session_name == NULL) {
693 ret = CMD_ERROR;
694 goto end;
695 }
696 current_session_name = session_name;
697 } else {
698 current_session_name = opt_session_name;
699 }
700
701 command_ret = handle_command(poptGetArgs(pc));
702 if (command_ret) {
703 switch (-command_ret) {
704 case LTTNG_ERR_EPERM:
705 ERR("The session needs to be set in no output mode (--no-output)");
706 break;
707 case LTTNG_ERR_SNAPSHOT_NODATA:
708 WARN("%s", lttng_strerror(command_ret));
709
710 /* A warning is fine since the user has no control on
711 * whether or not applications (or the kernel) have
712 * produced any event between the start of the tracing
713 * session and the recording of the snapshot. MI wise
714 * the command is not a success since nothing was
715 * recorded.
716 */
717 command_ret = 0;
718 break;
719 default:
720 ERR("%s", lttng_strerror(command_ret));
721 break;
722 }
723 success = 0;
724 }
725
726 if (lttng_opt_mi) {
727 /* Close output element */
728 ret = mi_lttng_writer_close_element(writer);
729 if (ret) {
730 ret = CMD_ERROR;
731 goto end;
732 }
733
734 /* Success ? */
735 ret = mi_lttng_writer_write_element_bool(writer,
736 mi_lttng_element_command_success, success);
737 if (ret) {
738 ret = CMD_ERROR;
739 goto end;
740 }
741
742 /* Command element close */
743 ret = mi_lttng_writer_command_close(writer);
744 if (ret) {
745 ret = CMD_ERROR;
746 goto end;
747 }
748 }
749
750 end:
751 /* Mi clean-up */
752 if (writer && mi_lttng_writer_destroy(writer)) {
753 /* Preserve original error code */
754 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
755 }
756
757 if (!opt_session_name) {
758 free(session_name);
759 }
760
761 /* Overwrite ret if an error occured during handle_command */
762 ret = command_ret ? command_ret : ret;
763 poptFreeContext(pc);
764 return ret;
765 }
This page took 0.044363 seconds and 4 git commands to generate.