Refactor: embed mi in "record" to remove code duplication
[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
8e610b42 153static int list_output(void)
50534d6f 154{
8e610b42 155 int ret, output_seen = 0;
50534d6f
JRJ
156 struct lttng_snapshot_output *s_iter;
157 struct lttng_snapshot_output_list *list;
158
50534d6f
JRJ
159 ret = lttng_snapshot_list_output(current_session_name, &list);
160 if (ret < 0) {
161 goto error;
162 }
163
8e610b42 164 MSG("Snapshot output list for session %s", current_session_name);
50534d6f 165
8e610b42
JR
166 if (lttng_opt_mi) {
167 ret = mi_lttng_snapshot_output_session_name(writer,
168 current_session_name);
50534d6f
JRJ
169 if (ret) {
170 ret = CMD_ERROR;
171 goto end;
172 }
173 }
174
57f272ed 175 while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) {
c3024823 176 MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4,
57f272ed
DG
177 lttng_snapshot_output_get_id(s_iter),
178 lttng_snapshot_output_get_name(s_iter),
c3024823
DG
179 lttng_snapshot_output_get_ctrl_url(s_iter),
180 lttng_snapshot_output_get_maxsize(s_iter));
57f272ed 181 output_seen = 1;
8e610b42
JR
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 }
57f272ed
DG
189 }
190
8e610b42
JR
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 }
205end:
57f272ed
DG
206 lttng_snapshot_output_list_destroy(list);
207
208 if (!output_seen) {
209 MSG("%sNone", indent4);
210 }
211
212error:
213 return ret;
214}
215
50534d6f
JRJ
216/*
217 * Delete output by ID (machine interface version).
218 */
219static 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) {
50534d6f
JRJ
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
255error:
256 lttng_snapshot_output_destroy(output);
257 return ret;
258}
259
57f272ed
DG
260/*
261 * Delete output by ID.
262 */
eb240553 263static int del_output(uint32_t id, const char *name)
57f272ed
DG
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
eb240553
DG
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 }
57f272ed
DG
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
eb240553
DG
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 }
57f272ed
DG
299
300error:
301 lttng_snapshot_output_destroy(output);
302 return ret;
303}
304
50534d6f
JRJ
305/*
306 * Add output from the user URL (machine interface).
307 */
308static 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) {
50534d6f
JRJ
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
349error:
350 lttng_snapshot_output_destroy(output);
351 return ret;
352}
353
57f272ed
DG
354/*
355 * Add output from the user URL.
356 */
357static int add_output(const char *url)
358{
359 int ret;
360 struct lttng_snapshot_output *output = NULL;
6efe784e
DG
361 char name[NAME_MAX];
362 const char *n_ptr;
57f272ed
DG
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
6efe784e
DG
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
57f272ed
DG
392 MSG("Snapshot output successfully added for session %s",
393 current_session_name);
394 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
6efe784e 395 lttng_snapshot_output_get_id(output), n_ptr,
57f272ed
DG
396 lttng_snapshot_output_get_ctrl_url(output),
397 lttng_snapshot_output_get_maxsize(output));
398error:
399 lttng_snapshot_output_destroy(output);
400 return ret;
401}
402
403static int cmd_add_output(int argc, const char **argv)
404{
50534d6f 405 int ret;
57f272ed
DG
406
407 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
57f272ed
DG
408 ret = CMD_ERROR;
409 goto end;
410 }
411
50534d6f
JRJ
412 if (lttng_opt_mi) {
413 ret = mi_add_output(argv[1]);
414 } else {
415 ret = add_output(argv[1]);
416 }
57f272ed
DG
417
418end:
419 return ret;
420}
421
422static int cmd_del_output(int argc, const char **argv)
423{
50534d6f 424 int ret;
eb240553
DG
425 char *name;
426 long id;
57f272ed
DG
427
428 if (argc < 2) {
57f272ed
DG
429 ret = CMD_ERROR;
430 goto end;
431 }
432
eb240553
DG
433 errno = 0;
434 id = strtol(argv[1], &name, 10);
435 if (id == 0 && errno == 0) {
50534d6f
JRJ
436 if (lttng_opt_mi) {
437 ret = mi_del_output(UINT32_MAX, name);
438 } else {
439 ret = del_output(UINT32_MAX, name);
440 }
eb240553 441 } else if (errno == 0 && *name == '\0') {
50534d6f
JRJ
442 if (lttng_opt_mi) {
443 ret = mi_del_output(id, NULL);
444 } else {
445 ret = del_output(id, NULL);
446 }
eb240553
DG
447 } else {
448 ERR("Argument %s not recognized", argv[1]);
449 ret = -1;
450 goto end;
451 }
57f272ed
DG
452
453end:
454 return ret;
455}
456
457static int cmd_list_output(int argc, const char **argv)
458{
50534d6f
JRJ
459 int ret;
460
8e610b42 461 ret = list_output();
50534d6f
JRJ
462
463 return ret;
464}
465
57f272ed
DG
466/*
467 * Do a snapshot record with the URL if one is given.
468 */
469static int record(const char *url)
470{
471 int ret;
472 struct lttng_snapshot_output *output = NULL;
473
e1986656
DG
474 output = create_output_from_args(url);
475 if (!output) {
476 ret = CMD_FATAL;
477 goto error;
57f272ed
DG
478 }
479
480 ret = lttng_snapshot_record(current_session_name, output, 0);
481 if (ret < 0) {
68808f4e 482 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
d07ceecd 483 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
68808f4e 484 }
57f272ed
DG
485 goto error;
486 }
487
488 MSG("Snapshot recorded successfully for session %s", current_session_name);
489
490 if (url) {
491 MSG("Snapshot written at: %s", url);
492 } else if (opt_ctrl_url) {
493 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
494 opt_data_url);
57f272ed
DG
495 }
496
1862dfe0
JR
497 if (lttng_opt_mi) {
498 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
499 opt_ctrl_url, opt_data_url);
500 if (ret) {
501 ret = CMD_ERROR;
502 }
503 }
504
57f272ed 505error:
cdcdb9dd 506 lttng_snapshot_output_destroy(output);
57f272ed
DG
507 return ret;
508}
509
510static int cmd_record(int argc, const char **argv)
511{
512 int ret;
513
514 if (argc == 2) {
1862dfe0 515 ret = record(argv[1]);
57f272ed 516 } else {
1862dfe0 517 ret = record(NULL);
57f272ed
DG
518 }
519
520 return ret;
521}
522
523static int handle_command(const char **argv)
524{
50534d6f 525 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
57f272ed
DG
526 struct cmd_struct *cmd;
527
528 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
529 (opt_ctrl_url && !opt_data_url)) {
50534d6f 530 command_ret = CMD_ERROR;
57f272ed
DG
531 goto end;
532 }
533
534 argc = count_arguments(argv);
535
536 cmd = &actions[i];
537 while (cmd->func != NULL) {
538 /* Find command */
539 if (strcmp(argv[0], cmd->name) == 0) {
50534d6f
JRJ
540 if (lttng_opt_mi) {
541 /* Action element */
542 ret = mi_lttng_writer_open_element(writer,
543 mi_lttng_element_command_action);
544 if (ret) {
545 ret = CMD_ERROR;
546 goto end;
547 }
548
549 /* Name of the action */
550 ret = mi_lttng_writer_write_element_string(writer,
551 config_element_name, argv[0]);
552 if (ret) {
553 ret = CMD_ERROR;
554 goto end;
555 }
556
557 /* Open output element */
558 ret = mi_lttng_writer_open_element(writer,
559 mi_lttng_element_command_output);
560 if (ret) {
561 ret = CMD_ERROR;
562 goto end;
563 }
564 }
565
566 command_ret = cmd->func(argc, argv);
567
568 if (lttng_opt_mi) {
569 /* Close output and action element */
570 ret = mi_lttng_close_multi_element(writer, 2);
571 if (ret) {
572 ret = CMD_ERROR;
573 goto end;
574 }
575 }
57f272ed
DG
576 goto end;
577 }
578 i++;
579 cmd = &actions[i];
580 }
581
1ec06ed3 582 ret = CMD_UNDEFINED;
57f272ed
DG
583
584end:
1ec06ed3 585 /* Overwrite ret if an error occurred in cmd->func() */
50534d6f 586 ret = command_ret ? command_ret : ret;
57f272ed
DG
587 return ret;
588}
57f272ed
DG
589/*
590 * The 'snapshot <cmd> <options>' first level command
591 */
592int cmd_snapshot(int argc, const char **argv)
593{
50534d6f 594 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
57f272ed
DG
595 char *session_name = NULL;
596 static poptContext pc;
597
598 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
599 poptReadDefaultConfig(pc, 0);
600
50534d6f 601 /* Mi check */
c7e35b03 602 if (lttng_opt_mi) {
50534d6f
JRJ
603 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
604 if (!writer) {
605 ret = -LTTNG_ERR_NOMEM;
606 goto end;
607 }
608
609 /* Open command element */
610 ret = mi_lttng_writer_command_open(writer,
611 mi_lttng_element_command_snapshot);
612 if (ret) {
613 ret = CMD_ERROR;
614 goto end;
615 }
616
617 /* Open output element */
618 ret = mi_lttng_writer_open_element(writer,
619 mi_lttng_element_command_output);
620 if (ret) {
621 ret = CMD_ERROR;
622 goto end;
623 }
c7e35b03
JR
624 }
625
57f272ed
DG
626 while ((opt = poptGetNextOpt(pc)) != -1) {
627 switch (opt) {
628 case OPT_HELP:
4ba92f18 629 SHOW_HELP();
57f272ed
DG
630 goto end;
631 case OPT_LIST_OPTIONS:
632 list_cmd_options(stdout, snapshot_opts);
633 goto end;
3c9bd23c
SM
634 case OPT_LIST_COMMANDS:
635 list_commands(actions, stdout);
636 goto end;
57f272ed
DG
637 case OPT_MAX_SIZE:
638 {
a8f307d8 639 uint64_t val;
57f272ed
DG
640 const char *opt = poptGetOptArg(pc);
641
a8f307d8 642 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
57f272ed
DG
643 ERR("Unable to handle max-size value %s", opt);
644 ret = CMD_ERROR;
645 goto end;
646 }
647
57f272ed
DG
648 opt_max_size = val;
649
650 break;
651 }
652 default:
57f272ed
DG
653 ret = CMD_UNDEFINED;
654 goto end;
655 }
656 }
657
658 if (!opt_session_name) {
659 session_name = get_session_name();
660 if (session_name == NULL) {
661 ret = CMD_ERROR;
662 goto end;
663 }
664 current_session_name = session_name;
665 } else {
666 current_session_name = opt_session_name;
667 }
668
50534d6f 669 command_ret = handle_command(poptGetArgs(pc));
1ec06ed3 670 if (command_ret) {
50534d6f 671 switch (-command_ret) {
7badf927 672 case LTTNG_ERR_EPERM:
6dc3064a 673 ERR("The session needs to be set in no output mode (--no-output)");
7badf927
DG
674 break;
675 case LTTNG_ERR_SNAPSHOT_NODATA:
50534d6f 676 WARN("%s", lttng_strerror(command_ret));
dad01b0a
JR
677
678 /* A warning is fine since the user has no control on
679 * whether or not applications (or the kernel) have
680 * produced any event between the start of the tracing
681 * session and the recording of the snapshot. MI wise
682 * the command is not a success since nothing was
683 * recorded.
684 */
685 command_ret = 0;
7badf927
DG
686 break;
687 default:
50534d6f 688 ERR("%s", lttng_strerror(command_ret));
7badf927 689 break;
6dc3064a 690 }
50534d6f
JRJ
691 success = 0;
692 }
693
694 if (lttng_opt_mi) {
695 /* Close output element */
696 ret = mi_lttng_writer_close_element(writer);
697 if (ret) {
698 ret = CMD_ERROR;
699 goto end;
700 }
701
702 /* Success ? */
703 ret = mi_lttng_writer_write_element_bool(writer,
704 mi_lttng_element_command_success, success);
705 if (ret) {
706 ret = CMD_ERROR;
707 goto end;
708 }
709
710 /* Command element close */
711 ret = mi_lttng_writer_command_close(writer);
712 if (ret) {
713 ret = CMD_ERROR;
714 goto end;
715 }
57f272ed
DG
716 }
717
718end:
50534d6f
JRJ
719 /* Mi clean-up */
720 if (writer && mi_lttng_writer_destroy(writer)) {
721 /* Preserve original error code */
722 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
723 }
724
57f272ed
DG
725 if (!opt_session_name) {
726 free(session_name);
727 }
50534d6f
JRJ
728
729 /* Overwrite ret if an error occured during handle_command */
730 ret = command_ret ? command_ret : ret;
57f272ed
DG
731 poptFreeContext(pc);
732 return ret;
733}
This page took 0.065148 seconds and 4 git commands to generate.