2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
12 #include <common/utils.hpp>
14 #include <lttng/lttng.h>
22 #include <sys/types.h>
25 static const char *opt_session_name
;
26 static const char *opt_output_name
;
27 static const char *opt_data_url
;
28 static const char *opt_ctrl_url
;
29 static const char *current_session_name
;
30 static uint64_t opt_max_size
;
32 /* Stub for the cmd struct actions. */
33 static int cmd_add_output(int argc
, const char **argv
);
34 static int cmd_del_output(int argc
, const char **argv
);
35 static int cmd_list_output(int argc
, const char **argv
);
36 static int cmd_record(int argc
, const char **argv
);
38 static const char *indent4
= " ";
40 #ifdef LTTNG_EMBED_HELP
41 static const char help_msg
[] =
42 #include <lttng-snapshot.1.h>
53 static struct mi_writer
*writer
;
55 static struct poptOption snapshot_opts
[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
58 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
59 { "ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, nullptr, nullptr },
60 { "data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, nullptr, nullptr },
61 { "name", 'n', POPT_ARG_STRING
, &opt_output_name
, 0, nullptr, nullptr },
62 { "max-size", 'm', POPT_ARG_STRING
, nullptr, OPT_MAX_SIZE
, nullptr, nullptr },
63 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
64 { "list-commands", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_COMMANDS
, nullptr, nullptr },
65 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
68 static struct cmd_struct actions
[] = {
69 { "add-output", cmd_add_output
},
70 { "del-output", cmd_del_output
},
71 { "list-output", cmd_list_output
},
72 { "record", cmd_record
},
73 { nullptr, nullptr } /* Array closure */
77 * Count and return the number of arguments in argv.
79 static int count_arguments(const char **argv
)
85 while (argv
[i
] != nullptr) {
93 * Create a snapshot output object from arguments using the given URL.
95 * Return a newly allocated object or NULL on error.
97 static struct lttng_snapshot_output
*create_output_from_args(const char *url
)
100 struct lttng_snapshot_output
*output
= nullptr;
102 output
= lttng_snapshot_output_create();
108 ret
= lttng_snapshot_output_set_ctrl_url(url
, output
);
112 } else if (opt_ctrl_url
) {
113 ret
= lttng_snapshot_output_set_ctrl_url(opt_ctrl_url
, output
);
120 ret
= lttng_snapshot_output_set_data_url(opt_data_url
, output
);
127 ret
= lttng_snapshot_output_set_size(opt_max_size
, output
);
133 if (opt_output_name
) {
134 ret
= lttng_snapshot_output_set_name(opt_output_name
, output
);
143 lttng_snapshot_output_destroy(output
);
148 static int list_output()
150 int ret
, output_seen
= 0;
151 struct lttng_snapshot_output
*s_iter
;
152 struct lttng_snapshot_output_list
*list
;
154 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
159 MSG("Snapshot output list for session %s", current_session_name
);
162 ret
= mi_lttng_snapshot_output_session_name(writer
, current_session_name
);
169 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != nullptr) {
170 if (lttng_snapshot_output_get_maxsize(s_iter
)) {
171 MSG("%s[%" PRIu32
"] %s: %s (max size: %" PRIu64
" bytes)",
173 lttng_snapshot_output_get_id(s_iter
),
174 lttng_snapshot_output_get_name(s_iter
),
175 lttng_snapshot_output_get_ctrl_url(s_iter
),
176 lttng_snapshot_output_get_maxsize(s_iter
));
178 MSG("%s[%" PRIu32
"] %s: %s",
180 lttng_snapshot_output_get_id(s_iter
),
181 lttng_snapshot_output_get_name(s_iter
),
182 lttng_snapshot_output_get_ctrl_url(s_iter
));
186 ret
= mi_lttng_snapshot_list_output(writer
, s_iter
);
195 /* Close snapshot snapshots element */
196 ret
= mi_lttng_writer_close_element(writer
);
202 /* Close snapshot session element */
203 ret
= mi_lttng_writer_close_element(writer
);
209 lttng_snapshot_output_list_destroy(list
);
212 MSG("%sNone", indent4
);
220 * Delete output by ID.
222 static int del_output(uint32_t id
, const char *name
)
225 struct lttng_snapshot_output
*output
= nullptr;
227 output
= lttng_snapshot_output_create();
234 ret
= lttng_snapshot_output_set_name(name
, output
);
235 } else if (id
!= UINT32_MAX
) {
236 ret
= lttng_snapshot_output_set_id(id
, output
);
246 ret
= lttng_snapshot_del_output(current_session_name
, output
);
251 if (id
!= UINT32_MAX
) {
252 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
254 current_session_name
);
256 MSG("Snapshot output %s successfully deleted for session %s",
258 current_session_name
);
262 ret
= mi_lttng_snapshot_del_output(writer
, id
, name
, current_session_name
);
269 lttng_snapshot_output_destroy(output
);
274 * Add output from the user URL.
276 static int add_output(const char *url
)
279 struct lttng_snapshot_output
*output
= nullptr;
283 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
288 output
= create_output_from_args(url
);
294 /* This call, if successful, populates the id of the output object. */
295 ret
= lttng_snapshot_add_output(current_session_name
, output
);
300 n_ptr
= lttng_snapshot_output_get_name(output
);
301 if (*n_ptr
== '\0') {
303 pret
= snprintf(name
,
305 DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
306 lttng_snapshot_output_get_id(output
));
308 PERROR("snprintf add output name");
313 MSG("Snapshot output successfully added for session %s", current_session_name
);
315 MSG(" [%" PRIu32
"] %s: %s (max size: %" PRIu64
" bytes)",
316 lttng_snapshot_output_get_id(output
),
318 lttng_snapshot_output_get_ctrl_url(output
),
319 lttng_snapshot_output_get_maxsize(output
));
321 MSG(" [%" PRIu32
"] %s: %s",
322 lttng_snapshot_output_get_id(output
),
324 lttng_snapshot_output_get_ctrl_url(output
));
327 ret
= mi_lttng_snapshot_add_output(writer
, current_session_name
, n_ptr
, output
);
333 lttng_snapshot_output_destroy(output
);
337 static int cmd_add_output(int argc
, const char **argv
)
341 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
342 ERR("An output destination must be specified to add a snapshot output.");
347 ret
= add_output(argv
[1]);
350 case LTTNG_ERR_SNAPSHOT_UNSUPPORTED
:
351 ERR("Session \"%s\" contains a channel that is incompatible with the snapshot functionality.\nMake sure all channels are configured in 'mmap' output mode.",
352 current_session_name
);
364 static int cmd_del_output(int argc
, const char **argv
)
371 ERR("A snapshot output name or id must be provided to delete a snapshot output.");
377 id
= strtol(argv
[1], &name
, 10);
378 if (id
== 0 && (errno
== 0 || errno
== EINVAL
)) {
379 ret
= del_output(UINT32_MAX
, name
);
380 } else if (errno
== 0 && *name
== '\0') {
381 ret
= del_output(id
, nullptr);
383 ERR("Argument %s not recognized", argv
[1]);
392 static int cmd_list_output(int argc
__attribute__((unused
)),
393 const char **argv
__attribute__((unused
)))
403 * Do a snapshot record with the URL if one is given.
405 static int record(const char *url
)
408 struct lttng_snapshot_output
*output
= nullptr;
410 output
= create_output_from_args(url
);
416 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
418 if (ret
== -LTTNG_ERR_MAX_SIZE_INVALID
) {
419 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
424 MSG("Snapshot recorded successfully for session %s", current_session_name
);
427 MSG("Snapshot written at: %s", url
);
428 } else if (opt_ctrl_url
) {
429 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url
, opt_data_url
);
433 ret
= mi_lttng_snapshot_record(writer
, url
, opt_ctrl_url
, opt_data_url
);
440 lttng_snapshot_output_destroy(output
);
444 static int cmd_record(int argc
, const char **argv
)
449 ret
= record(argv
[1]);
451 ret
= record(nullptr);
457 static enum cmd_error_code
handle_command(const char **argv
)
459 int mi_ret
, i
= 0, argc
;
460 enum cmd_error_code cmd_ret
;
461 struct cmd_struct
*cmd
;
464 ERR("No action specified for snapshot command.");
469 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
470 ERR("URLs must be specified for both data and control");
475 argc
= count_arguments(argv
);
476 /* popt should have passed NULL if no arguments are present. */
477 LTTNG_ASSERT(argc
> 0);
480 while (cmd
->func
!= nullptr) {
482 if (strcmp(argv
[0], cmd
->name
) == 0) {
487 mi_ret
= mi_lttng_writer_open_element(
488 writer
, mi_lttng_element_command_action
);
494 /* Name of the action */
495 mi_ret
= mi_lttng_writer_write_element_string(
496 writer
, config_element_name
, argv
[0]);
502 /* Open output element */
503 mi_ret
= mi_lttng_writer_open_element(
504 writer
, mi_lttng_element_command_output
);
511 result
= cmd
->func(argc
, argv
);
518 case CMD_UNSUPPORTED
:
520 * Sub-commands mix lttng_error_codes
521 * and cmd_error_codes. This should be
522 * cleaned-up, but in the meantime this
523 * hack works since the values of the
524 * two enums do not intersect.
526 cmd_ret
= (cmd_error_code
) result
;
528 case -LTTNG_ERR_SNAPSHOT_NODATA
:
529 WARN("%s", lttng_strerror(result
));
531 /* A warning is fine since the user has no control on
532 * whether or not applications (or the kernel) have
533 * produced any event between the start of the tracing
534 * session and the recording of the snapshot. MI wise
535 * the command is not a success since nothing was
538 cmd_ret
= CMD_SUCCESS
;
541 ERR("%s", lttng_strerror(result
));
546 cmd_ret
= CMD_SUCCESS
;
550 /* Close output and action element */
551 mi_ret
= mi_lttng_close_multi_element(writer
, 2);
563 cmd_ret
= CMD_UNDEFINED
;
569 * The 'snapshot <cmd> <options>' first level command
571 int cmd_snapshot(int argc
, const char **argv
)
575 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
576 char *session_name
= nullptr;
577 static poptContext pc
;
579 pc
= poptGetContext(nullptr, argc
, argv
, snapshot_opts
, 0);
580 poptReadDefaultConfig(pc
, 0);
584 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
590 /* Open command element */
591 mi_ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_snapshot
);
597 /* Open output element */
598 mi_ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
605 while ((opt
= poptGetNextOpt(pc
)) != -1) {
611 /* SHOW_HELP assigns to ret. */
613 cmd_ret
= (cmd_error_code
) ret
;
616 case OPT_LIST_OPTIONS
:
617 list_cmd_options(stdout
, snapshot_opts
);
619 case OPT_LIST_COMMANDS
:
620 list_commands(actions
, stdout
);
625 char *max_size_arg
= poptGetOptArg(pc
);
626 const int parse_ret
= utils_parse_size_suffix((char *) max_size_arg
, &val
);
629 ERR("Unable to handle max-size value %s", max_size_arg
);
640 cmd_ret
= CMD_UNDEFINED
;
645 if (!opt_session_name
) {
646 session_name
= get_session_name();
647 if (session_name
== nullptr) {
651 current_session_name
= session_name
;
653 current_session_name
= opt_session_name
;
656 cmd_ret
= handle_command(poptGetArgs(pc
));
659 /* Close output element */
660 mi_ret
= mi_lttng_writer_close_element(writer
);
667 mi_ret
= mi_lttng_writer_write_element_bool(
668 writer
, mi_lttng_element_command_success
, cmd_ret
== CMD_SUCCESS
);
674 /* Command element close */
675 mi_ret
= mi_lttng_writer_command_close(writer
);
684 if (writer
&& mi_lttng_writer_destroy(writer
)) {
688 if (!opt_session_name
) {