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