Refactor: embed mi in "del_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
216/*
217 * Delete output by ID.
218 */
eb240553 219static int del_output(uint32_t id, const char *name)
57f272ed
DG
220{
221 int ret;
222 struct lttng_snapshot_output *output = NULL;
223
224 output = lttng_snapshot_output_create();
225 if (!output) {
226 ret = CMD_FATAL;
227 goto error;
228 }
229
eb240553
DG
230 if (name) {
231 ret = lttng_snapshot_output_set_name(name, output);
232 } else if (id != UINT32_MAX) {
233 ret = lttng_snapshot_output_set_id(id, output);
234 } else {
235 ret = CMD_ERROR;
236 goto error;
237 }
57f272ed
DG
238 if (ret < 0) {
239 ret = CMD_FATAL;
240 goto error;
241 }
242
243 ret = lttng_snapshot_del_output(current_session_name, output);
244 if (ret < 0) {
245 goto error;
246 }
247
eb240553
DG
248 if (id != UINT32_MAX) {
249 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
250 id, current_session_name);
251 } else {
252 MSG("Snapshot output %s successfully deleted for session %s",
253 name, current_session_name);
254 }
57f272ed 255
6546176b
JR
256 if (lttng_opt_mi) {
257 ret = mi_lttng_snapshot_del_output(writer, id, name,
258 current_session_name);
259 if (ret) {
260 ret = CMD_ERROR;
261 }
262 }
263
57f272ed
DG
264error:
265 lttng_snapshot_output_destroy(output);
266 return ret;
267}
268
50534d6f
JRJ
269/*
270 * Add output from the user URL (machine interface).
271 */
272static int mi_add_output(const char *url)
273{
274 int ret;
275 struct lttng_snapshot_output *output = NULL;
276 char name[NAME_MAX];
277 const char *n_ptr;
278
279 if (!url && (!opt_data_url || !opt_ctrl_url)) {
280 ret = CMD_ERROR;
281 goto error;
282 }
283
284 output = create_output_from_args(url);
285 if (!output) {
286 ret = CMD_FATAL;
287 goto error;
288 }
289
290 /* This call, if successful, populates the id of the output object. */
291 ret = lttng_snapshot_add_output(current_session_name, output);
292 if (ret < 0) {
50534d6f
JRJ
293 goto error;
294 }
295
296 n_ptr = lttng_snapshot_output_get_name(output);
297 if (*n_ptr == '\0') {
298 int pret;
299 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
300 lttng_snapshot_output_get_id(output));
301 if (pret < 0) {
302 PERROR("snprintf add output name");
303 }
304 n_ptr = name;
305 }
306
307 ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr,
308 output);
309 if (ret) {
310 ret = CMD_ERROR;
311 }
312
313error:
314 lttng_snapshot_output_destroy(output);
315 return ret;
316}
317
57f272ed
DG
318/*
319 * Add output from the user URL.
320 */
321static int add_output(const char *url)
322{
323 int ret;
324 struct lttng_snapshot_output *output = NULL;
6efe784e
DG
325 char name[NAME_MAX];
326 const char *n_ptr;
57f272ed
DG
327
328 if (!url && (!opt_data_url || !opt_ctrl_url)) {
329 ret = CMD_ERROR;
330 goto error;
331 }
332
333 output = create_output_from_args(url);
334 if (!output) {
335 ret = CMD_FATAL;
336 goto error;
337 }
338
339 /* This call, if successful, populates the id of the output object. */
340 ret = lttng_snapshot_add_output(current_session_name, output);
341 if (ret < 0) {
342 goto error;
343 }
344
6efe784e
DG
345 n_ptr = lttng_snapshot_output_get_name(output);
346 if (*n_ptr == '\0') {
347 int pret;
348 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
349 lttng_snapshot_output_get_id(output));
350 if (pret < 0) {
351 PERROR("snprintf add output name");
352 }
353 n_ptr = name;
354 }
355
57f272ed
DG
356 MSG("Snapshot output successfully added for session %s",
357 current_session_name);
358 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
6efe784e 359 lttng_snapshot_output_get_id(output), n_ptr,
57f272ed
DG
360 lttng_snapshot_output_get_ctrl_url(output),
361 lttng_snapshot_output_get_maxsize(output));
362error:
363 lttng_snapshot_output_destroy(output);
364 return ret;
365}
366
367static int cmd_add_output(int argc, const char **argv)
368{
50534d6f 369 int ret;
57f272ed
DG
370
371 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
57f272ed
DG
372 ret = CMD_ERROR;
373 goto end;
374 }
375
50534d6f
JRJ
376 if (lttng_opt_mi) {
377 ret = mi_add_output(argv[1]);
378 } else {
379 ret = add_output(argv[1]);
380 }
57f272ed
DG
381
382end:
383 return ret;
384}
385
386static int cmd_del_output(int argc, const char **argv)
387{
50534d6f 388 int ret;
eb240553
DG
389 char *name;
390 long id;
57f272ed
DG
391
392 if (argc < 2) {
57f272ed
DG
393 ret = CMD_ERROR;
394 goto end;
395 }
396
eb240553
DG
397 errno = 0;
398 id = strtol(argv[1], &name, 10);
399 if (id == 0 && errno == 0) {
6546176b 400 ret = del_output(UINT32_MAX, name);
eb240553 401 } else if (errno == 0 && *name == '\0') {
6546176b 402 ret = del_output(id, NULL);
eb240553
DG
403 } else {
404 ERR("Argument %s not recognized", argv[1]);
405 ret = -1;
406 goto end;
407 }
57f272ed
DG
408
409end:
410 return ret;
411}
412
413static int cmd_list_output(int argc, const char **argv)
414{
50534d6f
JRJ
415 int ret;
416
8e610b42 417 ret = list_output();
50534d6f
JRJ
418
419 return ret;
420}
421
57f272ed
DG
422/*
423 * Do a snapshot record with the URL if one is given.
424 */
425static int record(const char *url)
426{
427 int ret;
428 struct lttng_snapshot_output *output = NULL;
429
e1986656
DG
430 output = create_output_from_args(url);
431 if (!output) {
432 ret = CMD_FATAL;
433 goto error;
57f272ed
DG
434 }
435
436 ret = lttng_snapshot_record(current_session_name, output, 0);
437 if (ret < 0) {
68808f4e 438 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
d07ceecd 439 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
68808f4e 440 }
57f272ed
DG
441 goto error;
442 }
443
444 MSG("Snapshot recorded successfully for session %s", current_session_name);
445
446 if (url) {
447 MSG("Snapshot written at: %s", url);
448 } else if (opt_ctrl_url) {
449 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
450 opt_data_url);
57f272ed
DG
451 }
452
1862dfe0
JR
453 if (lttng_opt_mi) {
454 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
455 opt_ctrl_url, opt_data_url);
456 if (ret) {
457 ret = CMD_ERROR;
458 }
459 }
460
57f272ed 461error:
cdcdb9dd 462 lttng_snapshot_output_destroy(output);
57f272ed
DG
463 return ret;
464}
465
466static int cmd_record(int argc, const char **argv)
467{
468 int ret;
469
470 if (argc == 2) {
1862dfe0 471 ret = record(argv[1]);
57f272ed 472 } else {
1862dfe0 473 ret = record(NULL);
57f272ed
DG
474 }
475
476 return ret;
477}
478
479static int handle_command(const char **argv)
480{
50534d6f 481 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
57f272ed
DG
482 struct cmd_struct *cmd;
483
484 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
485 (opt_ctrl_url && !opt_data_url)) {
50534d6f 486 command_ret = CMD_ERROR;
57f272ed
DG
487 goto end;
488 }
489
490 argc = count_arguments(argv);
491
492 cmd = &actions[i];
493 while (cmd->func != NULL) {
494 /* Find command */
495 if (strcmp(argv[0], cmd->name) == 0) {
50534d6f
JRJ
496 if (lttng_opt_mi) {
497 /* Action element */
498 ret = mi_lttng_writer_open_element(writer,
499 mi_lttng_element_command_action);
500 if (ret) {
501 ret = CMD_ERROR;
502 goto end;
503 }
504
505 /* Name of the action */
506 ret = mi_lttng_writer_write_element_string(writer,
507 config_element_name, argv[0]);
508 if (ret) {
509 ret = CMD_ERROR;
510 goto end;
511 }
512
513 /* Open output element */
514 ret = mi_lttng_writer_open_element(writer,
515 mi_lttng_element_command_output);
516 if (ret) {
517 ret = CMD_ERROR;
518 goto end;
519 }
520 }
521
522 command_ret = cmd->func(argc, argv);
523
524 if (lttng_opt_mi) {
525 /* Close output and action element */
526 ret = mi_lttng_close_multi_element(writer, 2);
527 if (ret) {
528 ret = CMD_ERROR;
529 goto end;
530 }
531 }
57f272ed
DG
532 goto end;
533 }
534 i++;
535 cmd = &actions[i];
536 }
537
1ec06ed3 538 ret = CMD_UNDEFINED;
57f272ed
DG
539
540end:
1ec06ed3 541 /* Overwrite ret if an error occurred in cmd->func() */
50534d6f 542 ret = command_ret ? command_ret : ret;
57f272ed
DG
543 return ret;
544}
57f272ed
DG
545/*
546 * The 'snapshot <cmd> <options>' first level command
547 */
548int cmd_snapshot(int argc, const char **argv)
549{
50534d6f 550 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
57f272ed
DG
551 char *session_name = NULL;
552 static poptContext pc;
553
554 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
555 poptReadDefaultConfig(pc, 0);
556
50534d6f 557 /* Mi check */
c7e35b03 558 if (lttng_opt_mi) {
50534d6f
JRJ
559 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
560 if (!writer) {
561 ret = -LTTNG_ERR_NOMEM;
562 goto end;
563 }
564
565 /* Open command element */
566 ret = mi_lttng_writer_command_open(writer,
567 mi_lttng_element_command_snapshot);
568 if (ret) {
569 ret = CMD_ERROR;
570 goto end;
571 }
572
573 /* Open output element */
574 ret = mi_lttng_writer_open_element(writer,
575 mi_lttng_element_command_output);
576 if (ret) {
577 ret = CMD_ERROR;
578 goto end;
579 }
c7e35b03
JR
580 }
581
57f272ed
DG
582 while ((opt = poptGetNextOpt(pc)) != -1) {
583 switch (opt) {
584 case OPT_HELP:
4ba92f18 585 SHOW_HELP();
57f272ed
DG
586 goto end;
587 case OPT_LIST_OPTIONS:
588 list_cmd_options(stdout, snapshot_opts);
589 goto end;
3c9bd23c
SM
590 case OPT_LIST_COMMANDS:
591 list_commands(actions, stdout);
592 goto end;
57f272ed
DG
593 case OPT_MAX_SIZE:
594 {
a8f307d8 595 uint64_t val;
57f272ed
DG
596 const char *opt = poptGetOptArg(pc);
597
a8f307d8 598 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
57f272ed
DG
599 ERR("Unable to handle max-size value %s", opt);
600 ret = CMD_ERROR;
601 goto end;
602 }
603
57f272ed
DG
604 opt_max_size = val;
605
606 break;
607 }
608 default:
57f272ed
DG
609 ret = CMD_UNDEFINED;
610 goto end;
611 }
612 }
613
614 if (!opt_session_name) {
615 session_name = get_session_name();
616 if (session_name == NULL) {
617 ret = CMD_ERROR;
618 goto end;
619 }
620 current_session_name = session_name;
621 } else {
622 current_session_name = opt_session_name;
623 }
624
50534d6f 625 command_ret = handle_command(poptGetArgs(pc));
1ec06ed3 626 if (command_ret) {
50534d6f 627 switch (-command_ret) {
7badf927 628 case LTTNG_ERR_EPERM:
6dc3064a 629 ERR("The session needs to be set in no output mode (--no-output)");
7badf927
DG
630 break;
631 case LTTNG_ERR_SNAPSHOT_NODATA:
50534d6f 632 WARN("%s", lttng_strerror(command_ret));
dad01b0a
JR
633
634 /* A warning is fine since the user has no control on
635 * whether or not applications (or the kernel) have
636 * produced any event between the start of the tracing
637 * session and the recording of the snapshot. MI wise
638 * the command is not a success since nothing was
639 * recorded.
640 */
641 command_ret = 0;
7badf927
DG
642 break;
643 default:
50534d6f 644 ERR("%s", lttng_strerror(command_ret));
7badf927 645 break;
6dc3064a 646 }
50534d6f
JRJ
647 success = 0;
648 }
649
650 if (lttng_opt_mi) {
651 /* Close output element */
652 ret = mi_lttng_writer_close_element(writer);
653 if (ret) {
654 ret = CMD_ERROR;
655 goto end;
656 }
657
658 /* Success ? */
659 ret = mi_lttng_writer_write_element_bool(writer,
660 mi_lttng_element_command_success, success);
661 if (ret) {
662 ret = CMD_ERROR;
663 goto end;
664 }
665
666 /* Command element close */
667 ret = mi_lttng_writer_command_close(writer);
668 if (ret) {
669 ret = CMD_ERROR;
670 goto end;
671 }
57f272ed
DG
672 }
673
674end:
50534d6f
JRJ
675 /* Mi clean-up */
676 if (writer && mi_lttng_writer_destroy(writer)) {
677 /* Preserve original error code */
678 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
679 }
680
57f272ed
DG
681 if (!opt_session_name) {
682 free(session_name);
683 }
50534d6f
JRJ
684
685 /* Overwrite ret if an error occured during handle_command */
686 ret = command_ret ? command_ret : ret;
57f272ed
DG
687 poptFreeContext(pc);
688 return ret;
689}
This page took 0.061759 seconds and 4 git commands to generate.