Fix: snapshot del-output with name on musl
[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
433static int handle_command(const char **argv)
434{
50534d6f 435 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
57f272ed
DG
436 struct cmd_struct *cmd;
437
438 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
439 (opt_ctrl_url && !opt_data_url)) {
50534d6f 440 command_ret = CMD_ERROR;
57f272ed
DG
441 goto end;
442 }
443
444 argc = count_arguments(argv);
445
446 cmd = &actions[i];
447 while (cmd->func != NULL) {
448 /* Find command */
449 if (strcmp(argv[0], cmd->name) == 0) {
50534d6f
JRJ
450 if (lttng_opt_mi) {
451 /* Action element */
452 ret = mi_lttng_writer_open_element(writer,
453 mi_lttng_element_command_action);
454 if (ret) {
455 ret = CMD_ERROR;
456 goto end;
457 }
458
459 /* Name of the action */
460 ret = mi_lttng_writer_write_element_string(writer,
461 config_element_name, argv[0]);
462 if (ret) {
463 ret = CMD_ERROR;
464 goto end;
465 }
466
467 /* Open output element */
468 ret = mi_lttng_writer_open_element(writer,
469 mi_lttng_element_command_output);
470 if (ret) {
471 ret = CMD_ERROR;
472 goto end;
473 }
474 }
475
476 command_ret = cmd->func(argc, argv);
477
478 if (lttng_opt_mi) {
479 /* Close output and action element */
480 ret = mi_lttng_close_multi_element(writer, 2);
481 if (ret) {
482 ret = CMD_ERROR;
483 goto end;
484 }
485 }
57f272ed
DG
486 goto end;
487 }
488 i++;
489 cmd = &actions[i];
490 }
491
1ec06ed3 492 ret = CMD_UNDEFINED;
57f272ed
DG
493
494end:
1ec06ed3 495 /* Overwrite ret if an error occurred in cmd->func() */
50534d6f 496 ret = command_ret ? command_ret : ret;
57f272ed
DG
497 return ret;
498}
57f272ed
DG
499/*
500 * The 'snapshot <cmd> <options>' first level command
501 */
502int cmd_snapshot(int argc, const char **argv)
503{
50534d6f 504 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
57f272ed
DG
505 char *session_name = NULL;
506 static poptContext pc;
507
508 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
509 poptReadDefaultConfig(pc, 0);
510
50534d6f 511 /* Mi check */
c7e35b03 512 if (lttng_opt_mi) {
50534d6f
JRJ
513 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
514 if (!writer) {
515 ret = -LTTNG_ERR_NOMEM;
516 goto end;
517 }
518
519 /* Open command element */
520 ret = mi_lttng_writer_command_open(writer,
521 mi_lttng_element_command_snapshot);
522 if (ret) {
523 ret = CMD_ERROR;
524 goto end;
525 }
526
527 /* Open output element */
528 ret = mi_lttng_writer_open_element(writer,
529 mi_lttng_element_command_output);
530 if (ret) {
531 ret = CMD_ERROR;
532 goto end;
533 }
c7e35b03
JR
534 }
535
57f272ed
DG
536 while ((opt = poptGetNextOpt(pc)) != -1) {
537 switch (opt) {
538 case OPT_HELP:
4ba92f18 539 SHOW_HELP();
57f272ed
DG
540 goto end;
541 case OPT_LIST_OPTIONS:
542 list_cmd_options(stdout, snapshot_opts);
543 goto end;
3c9bd23c
SM
544 case OPT_LIST_COMMANDS:
545 list_commands(actions, stdout);
546 goto end;
57f272ed
DG
547 case OPT_MAX_SIZE:
548 {
a8f307d8 549 uint64_t val;
57f272ed
DG
550 const char *opt = poptGetOptArg(pc);
551
a8f307d8 552 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
57f272ed
DG
553 ERR("Unable to handle max-size value %s", opt);
554 ret = CMD_ERROR;
555 goto end;
556 }
557
57f272ed
DG
558 opt_max_size = val;
559
560 break;
561 }
562 default:
57f272ed
DG
563 ret = CMD_UNDEFINED;
564 goto end;
565 }
566 }
567
568 if (!opt_session_name) {
569 session_name = get_session_name();
570 if (session_name == NULL) {
571 ret = CMD_ERROR;
572 goto end;
573 }
574 current_session_name = session_name;
575 } else {
576 current_session_name = opt_session_name;
577 }
578
50534d6f 579 command_ret = handle_command(poptGetArgs(pc));
1ec06ed3 580 if (command_ret) {
50534d6f 581 switch (-command_ret) {
7badf927 582 case LTTNG_ERR_EPERM:
6dc3064a 583 ERR("The session needs to be set in no output mode (--no-output)");
7badf927
DG
584 break;
585 case LTTNG_ERR_SNAPSHOT_NODATA:
50534d6f 586 WARN("%s", lttng_strerror(command_ret));
dad01b0a
JR
587
588 /* A warning is fine since the user has no control on
589 * whether or not applications (or the kernel) have
590 * produced any event between the start of the tracing
591 * session and the recording of the snapshot. MI wise
592 * the command is not a success since nothing was
593 * recorded.
594 */
595 command_ret = 0;
7badf927
DG
596 break;
597 default:
50534d6f 598 ERR("%s", lttng_strerror(command_ret));
7badf927 599 break;
6dc3064a 600 }
50534d6f
JRJ
601 success = 0;
602 }
603
604 if (lttng_opt_mi) {
605 /* Close output element */
606 ret = mi_lttng_writer_close_element(writer);
607 if (ret) {
608 ret = CMD_ERROR;
609 goto end;
610 }
611
612 /* Success ? */
613 ret = mi_lttng_writer_write_element_bool(writer,
614 mi_lttng_element_command_success, success);
615 if (ret) {
616 ret = CMD_ERROR;
617 goto end;
618 }
619
620 /* Command element close */
621 ret = mi_lttng_writer_command_close(writer);
622 if (ret) {
623 ret = CMD_ERROR;
624 goto end;
625 }
57f272ed
DG
626 }
627
628end:
50534d6f
JRJ
629 /* Mi clean-up */
630 if (writer && mi_lttng_writer_destroy(writer)) {
631 /* Preserve original error code */
632 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
633 }
634
57f272ed
DG
635 if (!opt_session_name) {
636 free(session_name);
637 }
50534d6f 638
83f4233d 639 /* Overwrite ret if an error occurred during handle_command */
50534d6f 640 ret = command_ret ? command_ret : ret;
57f272ed
DG
641 poptFreeContext(pc);
642 return ret;
643}
This page took 0.06937 seconds and 5 git commands to generate.