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