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