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.
219 static int del_output(uint32_t id
, const char *name
)
222 struct lttng_snapshot_output
*output
= NULL
;
224 output
= lttng_snapshot_output_create();
231 ret
= lttng_snapshot_output_set_name(name
, output
);
232 } else if (id
!= UINT32_MAX
) {
233 ret
= lttng_snapshot_output_set_id(id
, output
);
243 ret
= lttng_snapshot_del_output(current_session_name
, output
);
248 if (id
!= UINT32_MAX
) {
249 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
250 id
, current_session_name
);
252 MSG("Snapshot output %s successfully deleted for session %s",
253 name
, current_session_name
);
257 ret
= mi_lttng_snapshot_del_output(writer
, id
, name
,
258 current_session_name
);
265 lttng_snapshot_output_destroy(output
);
270 * Add output from the user URL.
272 static int add_output(const char *url
)
275 struct lttng_snapshot_output
*output
= NULL
;
279 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
284 output
= create_output_from_args(url
);
290 /* This call, if successful, populates the id of the output object. */
291 ret
= lttng_snapshot_add_output(current_session_name
, output
);
296 n_ptr
= lttng_snapshot_output_get_name(output
);
297 if (*n_ptr
== '\0') {
299 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
300 lttng_snapshot_output_get_id(output
));
302 PERROR("snprintf add output name");
307 MSG("Snapshot output successfully added for session %s",
308 current_session_name
);
309 MSG(" [%" PRIu32
"] %s: %s (max-size: %" PRId64
")",
310 lttng_snapshot_output_get_id(output
), n_ptr
,
311 lttng_snapshot_output_get_ctrl_url(output
),
312 lttng_snapshot_output_get_maxsize(output
));
314 ret
= mi_lttng_snapshot_add_output(writer
, current_session_name
,
321 lttng_snapshot_output_destroy(output
);
325 static int cmd_add_output(int argc
, const char **argv
)
329 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
334 ret
= add_output(argv
[1]);
340 static int cmd_del_output(int argc
, const char **argv
)
352 id
= strtol(argv
[1], &name
, 10);
353 if (id
== 0 && (errno
== 0 || errno
== EINVAL
)) {
354 ret
= del_output(UINT32_MAX
, name
);
355 } else if (errno
== 0 && *name
== '\0') {
356 ret
= del_output(id
, NULL
);
358 ERR("Argument %s not recognized", argv
[1]);
367 static int cmd_list_output(int argc
, const char **argv
)
377 * Do a snapshot record with the URL if one is given.
379 static int record(const char *url
)
382 struct lttng_snapshot_output
*output
= NULL
;
384 output
= create_output_from_args(url
);
390 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
392 if (ret
== -LTTNG_ERR_MAX_SIZE_INVALID
) {
393 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
398 MSG("Snapshot recorded successfully for session %s", current_session_name
);
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
,
408 ret
= mi_lttng_snapshot_record(writer
, current_session_name
, url
,
409 opt_ctrl_url
, opt_data_url
);
416 lttng_snapshot_output_destroy(output
);
420 static int cmd_record(int argc
, const char **argv
)
425 ret
= record(argv
[1]);
433 static int handle_command(const char **argv
)
435 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
436 struct cmd_struct
*cmd
;
438 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
439 (opt_ctrl_url
&& !opt_data_url
)) {
440 command_ret
= CMD_ERROR
;
444 argc
= count_arguments(argv
);
447 while (cmd
->func
!= NULL
) {
449 if (strcmp(argv
[0], cmd
->name
) == 0) {
452 ret
= mi_lttng_writer_open_element(writer
,
453 mi_lttng_element_command_action
);
459 /* Name of the action */
460 ret
= mi_lttng_writer_write_element_string(writer
,
461 config_element_name
, argv
[0]);
467 /* Open output element */
468 ret
= mi_lttng_writer_open_element(writer
,
469 mi_lttng_element_command_output
);
476 command_ret
= cmd
->func(argc
, argv
);
479 /* Close output and action element */
480 ret
= mi_lttng_close_multi_element(writer
, 2);
495 /* Overwrite ret if an error occurred in cmd->func() */
496 ret
= command_ret
? command_ret
: ret
;
500 * The 'snapshot <cmd> <options>' first level command
502 int cmd_snapshot(int argc
, const char **argv
)
504 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
505 char *session_name
= NULL
;
506 static poptContext pc
;
508 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
509 poptReadDefaultConfig(pc
, 0);
513 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
515 ret
= -LTTNG_ERR_NOMEM
;
519 /* Open command element */
520 ret
= mi_lttng_writer_command_open(writer
,
521 mi_lttng_element_command_snapshot
);
527 /* Open output element */
528 ret
= mi_lttng_writer_open_element(writer
,
529 mi_lttng_element_command_output
);
536 while ((opt
= poptGetNextOpt(pc
)) != -1) {
541 case OPT_LIST_OPTIONS
:
542 list_cmd_options(stdout
, snapshot_opts
);
544 case OPT_LIST_COMMANDS
:
545 list_commands(actions
, stdout
);
550 const char *opt
= poptGetOptArg(pc
);
552 if (utils_parse_size_suffix((char *) opt
, &val
) < 0) {
553 ERR("Unable to handle max-size value %s", opt
);
568 if (!opt_session_name
) {
569 session_name
= get_session_name();
570 if (session_name
== NULL
) {
574 current_session_name
= session_name
;
576 current_session_name
= opt_session_name
;
579 command_ret
= handle_command(poptGetArgs(pc
));
581 switch (-command_ret
) {
582 case LTTNG_ERR_SNAPSHOT_NODATA
:
583 WARN("%s", lttng_strerror(command_ret
));
585 /* A warning is fine since the user has no control on
586 * whether or not applications (or the kernel) have
587 * produced any event between the start of the tracing
588 * session and the recording of the snapshot. MI wise
589 * the command is not a success since nothing was
595 ERR("%s", lttng_strerror(command_ret
));
602 /* Close output element */
603 ret
= mi_lttng_writer_close_element(writer
);
610 ret
= mi_lttng_writer_write_element_bool(writer
,
611 mi_lttng_element_command_success
, success
);
617 /* Command element close */
618 ret
= mi_lttng_writer_command_close(writer
);
627 if (writer
&& mi_lttng_writer_destroy(writer
)) {
628 /* Preserve original error code */
629 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
632 if (!opt_session_name
) {
636 /* Overwrite ret if an error occurred during handle_command */
637 ret
= command_ret
? command_ret
: ret
;