2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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.
26 #include <sys/types.h>
30 #include <common/utils.h>
31 #include <common/mi-lttng.h>
32 #include <lttng/snapshot.h>
34 #include "../command.h"
36 static const char *opt_session_name
;
37 static const char *opt_output_name
;
38 static const char *opt_data_url
;
39 static const char *opt_ctrl_url
;
40 static const char *current_session_name
;
41 static uint64_t opt_max_size
;
43 /* Stub for the cmd struct actions. */
44 static int cmd_add_output(int argc
, const char **argv
);
45 static int cmd_del_output(int argc
, const char **argv
);
46 static int cmd_list_output(int argc
, const char **argv
);
47 static int cmd_record(int argc
, const char **argv
);
49 static const char *indent4
= " ";
58 static struct mi_writer
*writer
;
60 static 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},
67 {"max-size", 'm', POPT_ARG_STRING
, 0, OPT_MAX_SIZE
, 0, 0},
68 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
69 {"list-commands", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_COMMANDS
},
73 static 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 */
82 * Count and return the number of arguments in argv.
84 static int count_arguments(const char **argv
)
90 while (argv
[i
] != NULL
) {
98 * Create a snapshot output object from arguments using the given URL.
100 * Return a newly allocated object or NULL on error.
102 static struct lttng_snapshot_output
*create_output_from_args(const char *url
)
105 struct lttng_snapshot_output
*output
= NULL
;
107 output
= lttng_snapshot_output_create();
113 ret
= lttng_snapshot_output_set_ctrl_url(url
, output
);
117 } else if (opt_ctrl_url
) {
118 ret
= lttng_snapshot_output_set_ctrl_url(opt_ctrl_url
, output
);
125 ret
= lttng_snapshot_output_set_data_url(opt_data_url
, output
);
132 ret
= lttng_snapshot_output_set_size(opt_max_size
, output
);
138 if (opt_output_name
) {
139 ret
= lttng_snapshot_output_set_name(opt_output_name
, output
);
148 lttng_snapshot_output_destroy(output
);
153 static int list_output(void)
155 int ret
, output_seen
= 0;
156 struct lttng_snapshot_output
*s_iter
;
157 struct lttng_snapshot_output_list
*list
;
159 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
164 MSG("Snapshot output list for session %s", current_session_name
);
167 ret
= mi_lttng_snapshot_output_session_name(writer
,
168 current_session_name
);
175 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != NULL
) {
176 MSG("%s[%" PRIu32
"] %s: %s (max-size: %" PRId64
")", indent4
,
177 lttng_snapshot_output_get_id(s_iter
),
178 lttng_snapshot_output_get_name(s_iter
),
179 lttng_snapshot_output_get_ctrl_url(s_iter
),
180 lttng_snapshot_output_get_maxsize(s_iter
));
183 ret
= mi_lttng_snapshot_list_output(writer
, s_iter
);
192 /* Close snapshot snapshots element */
193 ret
= mi_lttng_writer_close_element(writer
);
199 /* Close snapshot session element */
200 ret
= mi_lttng_writer_close_element(writer
);
206 lttng_snapshot_output_list_destroy(list
);
209 MSG("%sNone", indent4
);
217 * Delete output by ID (machine interface version).
219 static int mi_del_output(uint32_t id
, const char *name
)
222 struct lttng_snapshot_output
*output
= NULL
;
226 output
= lttng_snapshot_output_create();
233 ret
= lttng_snapshot_output_set_name(name
, output
);
234 } else if (id
!= UINT32_MAX
) {
235 ret
= lttng_snapshot_output_set_id(id
, output
);
245 ret
= lttng_snapshot_del_output(current_session_name
, output
);
250 ret
= mi_lttng_snapshot_del_output(writer
, id
, name
, current_session_name
);
256 lttng_snapshot_output_destroy(output
);
261 * Delete output by ID.
263 static int del_output(uint32_t id
, const char *name
)
266 struct lttng_snapshot_output
*output
= NULL
;
268 output
= lttng_snapshot_output_create();
275 ret
= lttng_snapshot_output_set_name(name
, output
);
276 } else if (id
!= UINT32_MAX
) {
277 ret
= lttng_snapshot_output_set_id(id
, output
);
287 ret
= lttng_snapshot_del_output(current_session_name
, output
);
292 if (id
!= UINT32_MAX
) {
293 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
294 id
, current_session_name
);
296 MSG("Snapshot output %s successfully deleted for session %s",
297 name
, current_session_name
);
301 lttng_snapshot_output_destroy(output
);
306 * Add output from the user URL (machine interface).
308 static int mi_add_output(const char *url
)
311 struct lttng_snapshot_output
*output
= NULL
;
315 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
320 output
= create_output_from_args(url
);
326 /* This call, if successful, populates the id of the output object. */
327 ret
= lttng_snapshot_add_output(current_session_name
, output
);
332 n_ptr
= lttng_snapshot_output_get_name(output
);
333 if (*n_ptr
== '\0') {
335 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
336 lttng_snapshot_output_get_id(output
));
338 PERROR("snprintf add output name");
343 ret
= mi_lttng_snapshot_add_output(writer
, current_session_name
, n_ptr
,
350 lttng_snapshot_output_destroy(output
);
355 * Add output from the user URL.
357 static int add_output(const char *url
)
360 struct lttng_snapshot_output
*output
= NULL
;
364 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
369 output
= create_output_from_args(url
);
375 /* This call, if successful, populates the id of the output object. */
376 ret
= lttng_snapshot_add_output(current_session_name
, output
);
381 n_ptr
= lttng_snapshot_output_get_name(output
);
382 if (*n_ptr
== '\0') {
384 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
385 lttng_snapshot_output_get_id(output
));
387 PERROR("snprintf add output name");
392 MSG("Snapshot output successfully added for session %s",
393 current_session_name
);
394 MSG(" [%" PRIu32
"] %s: %s (max-size: %" PRId64
")",
395 lttng_snapshot_output_get_id(output
), n_ptr
,
396 lttng_snapshot_output_get_ctrl_url(output
),
397 lttng_snapshot_output_get_maxsize(output
));
399 lttng_snapshot_output_destroy(output
);
403 static int cmd_add_output(int argc
, const char **argv
)
407 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
413 ret
= mi_add_output(argv
[1]);
415 ret
= add_output(argv
[1]);
422 static int cmd_del_output(int argc
, const char **argv
)
434 id
= strtol(argv
[1], &name
, 10);
435 if (id
== 0 && errno
== 0) {
437 ret
= mi_del_output(UINT32_MAX
, name
);
439 ret
= del_output(UINT32_MAX
, name
);
441 } else if (errno
== 0 && *name
== '\0') {
443 ret
= mi_del_output(id
, NULL
);
445 ret
= del_output(id
, NULL
);
448 ERR("Argument %s not recognized", argv
[1]);
457 static int cmd_list_output(int argc
, const char **argv
)
467 * Do a snapshot record with the URL if one is given (machine interface).
469 static int mi_record(const char *url
)
472 struct lttng_snapshot_output
*output
= NULL
;
474 output
= create_output_from_args(url
);
480 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
486 ret
= mi_lttng_snapshot_record(writer
, current_session_name
, url
,
487 opt_ctrl_url
, opt_data_url
);
493 lttng_snapshot_output_destroy(output
);
498 * Do a snapshot record with the URL if one is given.
500 static int record(const char *url
)
503 struct lttng_snapshot_output
*output
= NULL
;
505 output
= create_output_from_args(url
);
511 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
513 if (ret
== -LTTNG_ERR_MAX_SIZE_INVALID
) {
514 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
519 MSG("Snapshot recorded successfully for session %s", current_session_name
);
522 MSG("Snapshot written at: %s", url
);
523 } else if (opt_ctrl_url
) {
524 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url
,
529 lttng_snapshot_output_destroy(output
);
533 static int cmd_record(int argc
, const char **argv
)
538 /* With a given URL */
540 ret
= mi_record(argv
[1]);
542 ret
= record(argv
[1]);
546 ret
= mi_record(NULL
);
555 static int handle_command(const char **argv
)
557 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
558 struct cmd_struct
*cmd
;
560 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
561 (opt_ctrl_url
&& !opt_data_url
)) {
562 command_ret
= CMD_ERROR
;
566 argc
= count_arguments(argv
);
569 while (cmd
->func
!= NULL
) {
571 if (strcmp(argv
[0], cmd
->name
) == 0) {
574 ret
= mi_lttng_writer_open_element(writer
,
575 mi_lttng_element_command_action
);
581 /* Name of the action */
582 ret
= mi_lttng_writer_write_element_string(writer
,
583 config_element_name
, argv
[0]);
589 /* Open output element */
590 ret
= mi_lttng_writer_open_element(writer
,
591 mi_lttng_element_command_output
);
598 command_ret
= cmd
->func(argc
, argv
);
601 /* Close output and action element */
602 ret
= mi_lttng_close_multi_element(writer
, 2);
617 /* Overwrite ret if an error occurred in cmd->func() */
618 ret
= command_ret
? command_ret
: ret
;
622 * The 'snapshot <cmd> <options>' first level command
624 int cmd_snapshot(int argc
, const char **argv
)
626 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
627 char *session_name
= NULL
;
628 static poptContext pc
;
630 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
631 poptReadDefaultConfig(pc
, 0);
635 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
637 ret
= -LTTNG_ERR_NOMEM
;
641 /* Open command element */
642 ret
= mi_lttng_writer_command_open(writer
,
643 mi_lttng_element_command_snapshot
);
649 /* Open output element */
650 ret
= mi_lttng_writer_open_element(writer
,
651 mi_lttng_element_command_output
);
658 while ((opt
= poptGetNextOpt(pc
)) != -1) {
663 case OPT_LIST_OPTIONS
:
664 list_cmd_options(stdout
, snapshot_opts
);
666 case OPT_LIST_COMMANDS
:
667 list_commands(actions
, stdout
);
672 const char *opt
= poptGetOptArg(pc
);
674 if (utils_parse_size_suffix((char *) opt
, &val
) < 0) {
675 ERR("Unable to handle max-size value %s", opt
);
690 if (!opt_session_name
) {
691 session_name
= get_session_name();
692 if (session_name
== NULL
) {
696 current_session_name
= session_name
;
698 current_session_name
= opt_session_name
;
701 command_ret
= handle_command(poptGetArgs(pc
));
703 switch (-command_ret
) {
704 case LTTNG_ERR_EPERM
:
705 ERR("The session needs to be set in no output mode (--no-output)");
707 case LTTNG_ERR_SNAPSHOT_NODATA
:
708 WARN("%s", lttng_strerror(command_ret
));
710 /* A warning is fine since the user has no control on
711 * whether or not applications (or the kernel) have
712 * produced any event between the start of the tracing
713 * session and the recording of the snapshot. MI wise
714 * the command is not a success since nothing was
720 ERR("%s", lttng_strerror(command_ret
));
727 /* Close output element */
728 ret
= mi_lttng_writer_close_element(writer
);
735 ret
= mi_lttng_writer_write_element_bool(writer
,
736 mi_lttng_element_command_success
, success
);
742 /* Command element close */
743 ret
= mi_lttng_writer_command_close(writer
);
752 if (writer
&& mi_lttng_writer_destroy(writer
)) {
753 /* Preserve original error code */
754 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
757 if (!opt_session_name
) {
761 /* Overwrite ret if an error occured during handle_command */
762 ret
= command_ret
? command_ret
: ret
;