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
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
269/*
270 * Add output from the user URL.
271 */
272static int add_output(const char *url)
273{
274 int ret;
275 struct lttng_snapshot_output *output = NULL;
6efe784e
DG
276 char name[NAME_MAX];
277 const char *n_ptr;
57f272ed
DG
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) {
293 goto error;
294 }
295
6efe784e
DG
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
57f272ed
DG
307 MSG("Snapshot output successfully added for session %s",
308 current_session_name);
309 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
6efe784e 310 lttng_snapshot_output_get_id(output), n_ptr,
57f272ed
DG
311 lttng_snapshot_output_get_ctrl_url(output),
312 lttng_snapshot_output_get_maxsize(output));
779f455d
JR
313 if (lttng_opt_mi) {
314 ret = mi_lttng_snapshot_add_output(writer, current_session_name,
315 n_ptr, output);
316 if (ret) {
317 ret = CMD_ERROR;
318 }
319 }
57f272ed
DG
320error:
321 lttng_snapshot_output_destroy(output);
322 return ret;
323}
324
325static int cmd_add_output(int argc, const char **argv)
326{
50534d6f 327 int ret;
57f272ed
DG
328
329 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
57f272ed
DG
330 ret = CMD_ERROR;
331 goto end;
332 }
333
779f455d 334 ret = add_output(argv[1]);
57f272ed
DG
335
336end:
337 return ret;
338}
339
340static int cmd_del_output(int argc, const char **argv)
341{
50534d6f 342 int ret;
eb240553
DG
343 char *name;
344 long id;
57f272ed
DG
345
346 if (argc < 2) {
57f272ed
DG
347 ret = CMD_ERROR;
348 goto end;
349 }
350
eb240553
DG
351 errno = 0;
352 id = strtol(argv[1], &name, 10);
07f50237 353 if (id == 0 && (errno == 0 || errno == EINVAL)) {
6546176b 354 ret = del_output(UINT32_MAX, name);
eb240553 355 } else if (errno == 0 && *name == '\0') {
6546176b 356 ret = del_output(id, NULL);
eb240553
DG
357 } else {
358 ERR("Argument %s not recognized", argv[1]);
359 ret = -1;
360 goto end;
361 }
57f272ed
DG
362
363end:
364 return ret;
365}
366
367static int cmd_list_output(int argc, const char **argv)
368{
50534d6f
JRJ
369 int ret;
370
8e610b42 371 ret = list_output();
50534d6f
JRJ
372
373 return ret;
374}
375
57f272ed
DG
376/*
377 * Do a snapshot record with the URL if one is given.
378 */
379static int record(const char *url)
380{
381 int ret;
382 struct lttng_snapshot_output *output = NULL;
383
e1986656
DG
384 output = create_output_from_args(url);
385 if (!output) {
386 ret = CMD_FATAL;
387 goto error;
57f272ed
DG
388 }
389
390 ret = lttng_snapshot_record(current_session_name, output, 0);
391 if (ret < 0) {
68808f4e 392 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
d07ceecd 393 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
68808f4e 394 }
57f272ed
DG
395 goto error;
396 }
397
398 MSG("Snapshot recorded successfully for session %s", current_session_name);
399
400 if (url) {
401 MSG("Snapshot written at: %s", url);
402 } else if (opt_ctrl_url) {
403 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
404 opt_data_url);
57f272ed
DG
405 }
406
1862dfe0
JR
407 if (lttng_opt_mi) {
408 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
409 opt_ctrl_url, opt_data_url);
410 if (ret) {
411 ret = CMD_ERROR;
412 }
413 }
414
57f272ed 415error:
cdcdb9dd 416 lttng_snapshot_output_destroy(output);
57f272ed
DG
417 return ret;
418}
419
420static int cmd_record(int argc, const char **argv)
421{
422 int ret;
423
424 if (argc == 2) {
1862dfe0 425 ret = record(argv[1]);
57f272ed 426 } else {
1862dfe0 427 ret = record(NULL);
57f272ed
DG
428 }
429
430 return ret;
431}
432
c29db611 433static enum cmd_error_code handle_command(const char **argv)
57f272ed 434{
c29db611
JG
435 int mi_ret, i = 0, argc;
436 enum cmd_error_code cmd_ret;
57f272ed
DG
437 struct cmd_struct *cmd;
438
c29db611
JG
439 if (!argv) {
440 ERR("No action specified for snapshot command.");
441 cmd_ret = CMD_ERROR;
442 goto end;
443 }
444
445 if ((!opt_ctrl_url && opt_data_url) ||
57f272ed 446 (opt_ctrl_url && !opt_data_url)) {
c29db611
JG
447 ERR("URLs must be specified for both data and control");
448 cmd_ret = CMD_ERROR;
57f272ed
DG
449 goto end;
450 }
451
452 argc = count_arguments(argv);
c29db611
JG
453 /* popt should have passed NULL if no arguments are present. */
454 assert(argc > 0);
57f272ed
DG
455
456 cmd = &actions[i];
457 while (cmd->func != NULL) {
458 /* Find command */
459 if (strcmp(argv[0], cmd->name) == 0) {
c29db611
JG
460 int result;
461
50534d6f
JRJ
462 if (lttng_opt_mi) {
463 /* Action element */
c29db611 464 mi_ret = mi_lttng_writer_open_element(writer,
50534d6f 465 mi_lttng_element_command_action);
c29db611
JG
466 if (mi_ret) {
467 cmd_ret = CMD_ERROR;
50534d6f
JRJ
468 goto end;
469 }
470
471 /* Name of the action */
c29db611 472 mi_ret = mi_lttng_writer_write_element_string(writer,
50534d6f 473 config_element_name, argv[0]);
c29db611
JG
474 if (mi_ret) {
475 cmd_ret = CMD_ERROR;
50534d6f
JRJ
476 goto end;
477 }
478
479 /* Open output element */
c29db611 480 mi_ret = mi_lttng_writer_open_element(writer,
50534d6f 481 mi_lttng_element_command_output);
c29db611
JG
482 if (mi_ret) {
483 cmd_ret = CMD_ERROR;
50534d6f
JRJ
484 goto end;
485 }
486 }
487
c29db611
JG
488 result = cmd->func(argc, argv);
489 if (result) {
490 switch (-result) {
491 case LTTNG_ERR_SNAPSHOT_NODATA:
492 WARN("%s", lttng_strerror(result));
493
494 /* A warning is fine since the user has no control on
495 * whether or not applications (or the kernel) have
496 * produced any event between the start of the tracing
497 * session and the recording of the snapshot. MI wise
498 * the command is not a success since nothing was
499 * recorded.
500 */
501 cmd_ret = CMD_SUCCESS;
502 break;
503 default:
504 ERR("%s", lttng_strerror(result));
505 cmd_ret = CMD_ERROR;
506 break;
507 }
508 } else {
509 cmd_ret = CMD_SUCCESS;
510 }
511
50534d6f
JRJ
512
513 if (lttng_opt_mi) {
514 /* Close output and action element */
c29db611
JG
515 mi_ret = mi_lttng_close_multi_element(writer, 2);
516 if (mi_ret) {
517 cmd_ret = CMD_ERROR;
50534d6f
JRJ
518 goto end;
519 }
520 }
57f272ed
DG
521 goto end;
522 }
523 i++;
524 cmd = &actions[i];
525 }
526
c29db611 527 cmd_ret = CMD_UNDEFINED;
57f272ed
DG
528
529end:
c29db611 530 return cmd_ret;
57f272ed 531}
57f272ed
DG
532/*
533 * The 'snapshot <cmd> <options>' first level command
534 */
535int cmd_snapshot(int argc, const char **argv)
536{
c29db611
JG
537 int opt;
538 int mi_ret;
539 enum cmd_error_code cmd_ret = CMD_SUCCESS;
57f272ed
DG
540 char *session_name = NULL;
541 static poptContext pc;
542
543 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
544 poptReadDefaultConfig(pc, 0);
545
50534d6f 546 /* Mi check */
c7e35b03 547 if (lttng_opt_mi) {
50534d6f
JRJ
548 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
549 if (!writer) {
c29db611 550 cmd_ret = CMD_ERROR;
50534d6f
JRJ
551 goto end;
552 }
553
554 /* Open command element */
c29db611 555 mi_ret = mi_lttng_writer_command_open(writer,
50534d6f 556 mi_lttng_element_command_snapshot);
c29db611
JG
557 if (mi_ret) {
558 cmd_ret = CMD_ERROR;
50534d6f
JRJ
559 goto end;
560 }
561
562 /* Open output element */
c29db611 563 mi_ret = mi_lttng_writer_open_element(writer,
50534d6f 564 mi_lttng_element_command_output);
c29db611
JG
565 if (mi_ret) {
566 cmd_ret = CMD_ERROR;
50534d6f
JRJ
567 goto end;
568 }
c7e35b03
JR
569 }
570
57f272ed
DG
571 while ((opt = poptGetNextOpt(pc)) != -1) {
572 switch (opt) {
573 case OPT_HELP:
c29db611
JG
574 {
575 int ret;
576
577 /* SHOW_HELP assigns to ret. */
4ba92f18 578 SHOW_HELP();
c29db611 579 cmd_ret = ret;
57f272ed 580 goto end;
c29db611 581 }
57f272ed
DG
582 case OPT_LIST_OPTIONS:
583 list_cmd_options(stdout, snapshot_opts);
584 goto end;
3c9bd23c
SM
585 case OPT_LIST_COMMANDS:
586 list_commands(actions, stdout);
587 goto end;
57f272ed
DG
588 case OPT_MAX_SIZE:
589 {
a8f307d8 590 uint64_t val;
57f272ed
DG
591 const char *opt = poptGetOptArg(pc);
592
a8f307d8 593 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
57f272ed 594 ERR("Unable to handle max-size value %s", opt);
c29db611 595 cmd_ret = CMD_ERROR;
57f272ed
DG
596 goto end;
597 }
598
57f272ed
DG
599 opt_max_size = val;
600
601 break;
602 }
603 default:
c29db611 604 cmd_ret = CMD_UNDEFINED;
57f272ed
DG
605 goto end;
606 }
607 }
608
609 if (!opt_session_name) {
610 session_name = get_session_name();
611 if (session_name == NULL) {
c29db611 612 cmd_ret = CMD_ERROR;
57f272ed
DG
613 goto end;
614 }
615 current_session_name = session_name;
616 } else {
617 current_session_name = opt_session_name;
618 }
619
c29db611 620 cmd_ret = handle_command(poptGetArgs(pc));
50534d6f
JRJ
621
622 if (lttng_opt_mi) {
623 /* Close output element */
c29db611
JG
624 mi_ret = mi_lttng_writer_close_element(writer);
625 if (mi_ret) {
626 cmd_ret = CMD_ERROR;
50534d6f
JRJ
627 goto end;
628 }
629
630 /* Success ? */
c29db611
JG
631 mi_ret = mi_lttng_writer_write_element_bool(writer,
632 mi_lttng_element_command_success,
633 cmd_ret == CMD_SUCCESS);
634 if (mi_ret) {
635 cmd_ret = CMD_ERROR;
50534d6f
JRJ
636 goto end;
637 }
638
639 /* Command element close */
c29db611
JG
640 mi_ret = mi_lttng_writer_command_close(writer);
641 if (mi_ret) {
642 cmd_ret = CMD_ERROR;
50534d6f
JRJ
643 goto end;
644 }
57f272ed
DG
645 }
646
647end:
50534d6f
JRJ
648 /* Mi clean-up */
649 if (writer && mi_lttng_writer_destroy(writer)) {
c29db611 650 cmd_ret = CMD_ERROR;
50534d6f
JRJ
651 }
652
57f272ed
DG
653 if (!opt_session_name) {
654 free(session_name);
655 }
50534d6f 656
57f272ed 657 poptFreeContext(pc);
c29db611 658 return cmd_ret;
57f272ed 659}
This page took 0.064136 seconds and 4 git commands to generate.