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>
29 #include <common/utils.h>
30 #include <lttng/snapshot.h>
32 #include "../command.h"
34 static const char *opt_session_name
;
35 static const char *opt_output_name
;
36 static const char *opt_data_url
;
37 static const char *opt_ctrl_url
;
38 static const char *current_session_name
;
39 static uint64_t opt_max_size
;
41 /* Stub for the cmd struct actions. */
42 static int cmd_add_output(int argc
, const char **argv
);
43 static int cmd_del_output(int argc
, const char **argv
);
44 static int cmd_list_output(int argc
, const char **argv
);
45 static int cmd_record(int argc
, const char **argv
);
47 static const char *indent4
= " ";
56 static struct poptOption snapshot_opts
[] = {
57 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
58 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
59 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
60 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
61 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
62 {"name", 'n', POPT_ARG_STRING
, &opt_output_name
, 0, 0, 0},
63 {"max-size", 'm', POPT_ARG_STRING
, 0, OPT_MAX_SIZE
, 0, 0},
64 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
65 {"list-commands", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_COMMANDS
},
69 static struct cmd_struct actions
[] = {
70 { "add-output", cmd_add_output
},
71 { "del-output", cmd_del_output
},
72 { "list-output", cmd_list_output
},
73 { "record", cmd_record
},
74 { NULL
, NULL
} /* Array closure */
80 static void usage(FILE *ofp
)
82 fprintf(ofp
, "usage: lttng snapshot [OPTION] ACTION\n");
84 fprintf(ofp
, "Actions:\n");
85 fprintf(ofp
, " add-output [-m <SIZE>] [-s <NAME>] [-n <NAME>] <URL> | -C <URL> -D <URL>\n");
86 fprintf(ofp
, " Setup and add an snapshot output for a session.\n");
88 fprintf(ofp
, " del-output ID | NAME [-s <NAME>]\n");
89 fprintf(ofp
, " Delete an output for a session using the ID.\n");
91 fprintf(ofp
, " list-output [-s <NAME>]\n");
92 fprintf(ofp
, " List the output of a session.\n");
94 fprintf(ofp
, " record [-m <SIZE>] [-s <NAME>] [-n <NAME>] [<URL> | -C <URL> -D <URL>]\n");
95 fprintf(ofp
, " Snapshot a session's buffer(s) for all domains. If an URL is\n");
96 fprintf(ofp
, " specified, it is used instead of a previously added output.\n");
97 fprintf(ofp
, " Specifying only a name or/a size will override the current output value.\n");
98 fprintf(ofp
, " For instance, you can record a snapshot with a custom maximum size\n");
99 fprintf(ofp
, " or with a different name.\n");
101 fprintf(ofp
, "Options:\n");
102 fprintf(ofp
, " -h, --help Show this help\n");
103 fprintf(ofp
, " --list-options Simple listing of options\n");
104 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
105 fprintf(ofp
, " -n, --name NAME Name of the output or snapshot\n");
106 fprintf(ofp
, " -m, --max-size SIZE Maximum bytes size of the snapshot {+k,+M,+G}\n");
107 fprintf(ofp
, " -C, --ctrl-url URL Set control path URL. (Must use -D also)\n");
108 fprintf(ofp
, " -D, --data-url URL Set data path URL. (Must use -C also)\n");
113 * Count and return the number of arguments in argv.
115 static int count_arguments(const char **argv
)
121 while (argv
[i
] != NULL
) {
129 * Create a snapshot output object from arguments using the given URL.
131 * Return a newly allocated object or NULL on error.
133 static struct lttng_snapshot_output
*create_output_from_args(const char *url
)
136 struct lttng_snapshot_output
*output
= NULL
;
138 output
= lttng_snapshot_output_create();
144 ret
= lttng_snapshot_output_set_ctrl_url(url
, output
);
148 } else if (opt_ctrl_url
) {
149 ret
= lttng_snapshot_output_set_ctrl_url(opt_ctrl_url
, output
);
156 ret
= lttng_snapshot_output_set_data_url(opt_data_url
, output
);
163 ret
= lttng_snapshot_output_set_size(opt_max_size
, output
);
169 if (opt_output_name
) {
170 ret
= lttng_snapshot_output_set_name(opt_output_name
, output
);
179 lttng_snapshot_output_destroy(output
);
184 static int list_output(void)
186 int ret
, output_seen
= 0;
187 struct lttng_snapshot_output
*s_iter
;
188 struct lttng_snapshot_output_list
*list
;
190 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
195 MSG("Snapshot output list for session %s", current_session_name
);
197 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != NULL
) {
198 MSG("%s[%" PRIu32
"] %s: %s", indent4
,
199 lttng_snapshot_output_get_id(s_iter
),
200 lttng_snapshot_output_get_name(s_iter
),
201 lttng_snapshot_output_get_ctrl_url(s_iter
));
205 lttng_snapshot_output_list_destroy(list
);
208 MSG("%sNone", indent4
);
216 * Delete output by ID.
218 static int del_output(uint32_t id
, const char *name
)
221 struct lttng_snapshot_output
*output
= NULL
;
223 output
= lttng_snapshot_output_create();
230 ret
= lttng_snapshot_output_set_name(name
, output
);
231 } else if (id
!= UINT32_MAX
) {
232 ret
= lttng_snapshot_output_set_id(id
, output
);
242 ret
= lttng_snapshot_del_output(current_session_name
, output
);
247 if (id
!= UINT32_MAX
) {
248 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
249 id
, current_session_name
);
251 MSG("Snapshot output %s successfully deleted for session %s",
252 name
, current_session_name
);
256 lttng_snapshot_output_destroy(output
);
261 * Add output from the user URL.
263 static int add_output(const char *url
)
266 struct lttng_snapshot_output
*output
= NULL
;
268 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
273 output
= create_output_from_args(url
);
279 /* This call, if successful, populates the id of the output object. */
280 ret
= lttng_snapshot_add_output(current_session_name
, output
);
285 MSG("Snapshot output successfully added for session %s",
286 current_session_name
);
287 MSG(" [%" PRIu32
"] %s: %s (max-size: %" PRId64
")",
288 lttng_snapshot_output_get_id(output
),
289 lttng_snapshot_output_get_name(output
),
290 lttng_snapshot_output_get_ctrl_url(output
),
291 lttng_snapshot_output_get_maxsize(output
));
293 lttng_snapshot_output_destroy(output
);
297 static int cmd_add_output(int argc
, const char **argv
)
299 int ret
= CMD_SUCCESS
;
301 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
307 ret
= add_output(argv
[1]);
313 static int cmd_del_output(int argc
, const char **argv
)
315 int ret
= CMD_SUCCESS
;
326 id
= strtol(argv
[1], &name
, 10);
327 if (id
== 0 && errno
== 0) {
328 ret
= del_output(UINT32_MAX
, name
);
329 } else if (errno
== 0 && *name
== '\0') {
330 ret
= del_output(id
, NULL
);
332 ERR("Argument %s not recognized", argv
[1]);
341 static int cmd_list_output(int argc
, const char **argv
)
343 return list_output();
347 * Do a snapshot record with the URL if one is given.
349 static int record(const char *url
)
352 struct lttng_snapshot_output
*output
= NULL
;
354 output
= create_output_from_args(url
);
360 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
365 MSG("Snapshot recorded successfully for session %s", current_session_name
);
368 MSG("Snapshot written at: %s", url
);
369 } else if (opt_ctrl_url
) {
370 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url
,
375 lttng_snapshot_output_destroy(output
);
379 static int cmd_record(int argc
, const char **argv
)
384 /* With a given URL */
385 ret
= record(argv
[1]);
393 static int handle_command(const char **argv
)
395 int ret
, i
= 0, argc
;
396 struct cmd_struct
*cmd
;
398 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
399 (opt_ctrl_url
&& !opt_data_url
)) {
405 argc
= count_arguments(argv
);
408 while (cmd
->func
!= NULL
) {
410 if (strcmp(argv
[0], cmd
->name
) == 0) {
411 ret
= cmd
->func(argc
, argv
);
418 /* Command not found */
426 * The 'snapshot <cmd> <options>' first level command
428 int cmd_snapshot(int argc
, const char **argv
)
430 int opt
, ret
= CMD_SUCCESS
;
431 char *session_name
= NULL
;
432 static poptContext pc
;
434 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
435 poptReadDefaultConfig(pc
, 0);
437 while ((opt
= poptGetNextOpt(pc
)) != -1) {
442 case OPT_LIST_OPTIONS
:
443 list_cmd_options(stdout
, snapshot_opts
);
445 case OPT_LIST_COMMANDS
:
446 list_commands(actions
, stdout
);
451 const char *opt
= poptGetOptArg(pc
);
453 if (utils_parse_size_suffix((char *) opt
, &val
) < 0) {
454 ERR("Unable to handle max-size value %s", opt
);
470 if (!opt_session_name
) {
471 session_name
= get_session_name();
472 if (session_name
== NULL
) {
476 current_session_name
= session_name
;
478 current_session_name
= opt_session_name
;
481 ret
= handle_command(poptGetArgs(pc
));
483 if (ret
== -LTTNG_ERR_EPERM
) {
484 ERR("The session needs to be set in no output mode (--no-output)");
486 ERR("%s", lttng_strerror(ret
));
491 if (!opt_session_name
) {