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
= " ";
55 static struct poptOption snapshot_opts
[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
58 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
59 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
60 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
61 {"name", 'n', POPT_ARG_STRING
, &opt_output_name
, 0, 0, 0},
62 {"max-size", 'm', POPT_ARG_STRING
, 0, OPT_MAX_SIZE
, 0, 0},
63 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
67 static struct cmd_struct actions
[] = {
68 { "add-output", cmd_add_output
},
69 { "del-output", cmd_del_output
},
70 { "list-output", cmd_list_output
},
71 { "record", cmd_record
},
72 { NULL
, NULL
} /* Array closure */
78 static void usage(FILE *ofp
)
80 fprintf(ofp
, "usage: lttng snapshot [OPTION] ACTION\n");
82 fprintf(ofp
, "Actions:\n");
83 fprintf(ofp
, " add-output [-m <SIZE>] [-s <NAME>] [-n <NAME>] <URL> | -C <URL> -D <URL>\n");
84 fprintf(ofp
, " Setup and add an snapshot output for a session.\n");
86 fprintf(ofp
, " del-output ID | NAME [-s <NAME>]\n");
87 fprintf(ofp
, " Delete an output for a session using the ID.\n");
89 fprintf(ofp
, " list-output [-s <NAME>]\n");
90 fprintf(ofp
, " List the output of a session.\n");
92 fprintf(ofp
, " record [-m <SIZE>] [-s <NAME>] [-n <NAME>] [<URL> | -C <URL> -D <URL>]\n");
93 fprintf(ofp
, " Snapshot a session's buffer(s) for all domains. If an URL is\n");
94 fprintf(ofp
, " specified, it is used instead of a previously added output.\n");
95 fprintf(ofp
, " Specifying only a name or/a size will override the current output value.\n");
96 fprintf(ofp
, " For instance, you can record a snapshot with a custom maximum size\n");
97 fprintf(ofp
, " or with a different name.\n");
99 fprintf(ofp
, "Options:\n");
100 fprintf(ofp
, " -h, --help Show this help\n");
101 fprintf(ofp
, " --list-options Simple listing of options\n");
102 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
103 fprintf(ofp
, " -n, --name NAME Name of the output or snapshot\n");
104 fprintf(ofp
, " -m, --max-size SIZE Maximum bytes size of the snapshot {+k,+M,+G}\n");
105 fprintf(ofp
, " -C, --ctrl-url URL Set control path URL. (Must use -D also)\n");
106 fprintf(ofp
, " -D, --data-url URL Set data path URL. (Must use -C also)\n");
111 * Count and return the number of arguments in argv.
113 static int count_arguments(const char **argv
)
119 while (argv
[i
] != NULL
) {
127 * Create a snapshot output object from arguments using the given URL.
129 * Return a newly allocated object or NULL on error.
131 static struct lttng_snapshot_output
*create_output_from_args(const char *url
)
134 struct lttng_snapshot_output
*output
= NULL
;
136 output
= lttng_snapshot_output_create();
142 ret
= lttng_snapshot_output_set_ctrl_url(url
, output
);
146 } else if (opt_ctrl_url
) {
147 ret
= lttng_snapshot_output_set_ctrl_url(opt_ctrl_url
, output
);
154 ret
= lttng_snapshot_output_set_data_url(opt_data_url
, output
);
161 ret
= lttng_snapshot_output_set_size(opt_max_size
, output
);
167 if (opt_output_name
) {
168 ret
= lttng_snapshot_output_set_name(opt_output_name
, output
);
177 lttng_snapshot_output_destroy(output
);
182 static int list_output(void)
184 int ret
, output_seen
= 0;
185 struct lttng_snapshot_output
*s_iter
;
186 struct lttng_snapshot_output_list
*list
;
188 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
193 MSG("Snapshot output list for session %s", current_session_name
);
195 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != NULL
) {
196 MSG("%s[%" PRIu32
"] %s: %s", indent4
,
197 lttng_snapshot_output_get_id(s_iter
),
198 lttng_snapshot_output_get_name(s_iter
),
199 lttng_snapshot_output_get_ctrl_url(s_iter
));
203 lttng_snapshot_output_list_destroy(list
);
206 MSG("%sNone", indent4
);
214 * Delete output by ID.
216 static int del_output(uint32_t id
, const char *name
)
219 struct lttng_snapshot_output
*output
= NULL
;
221 output
= lttng_snapshot_output_create();
228 ret
= lttng_snapshot_output_set_name(name
, output
);
229 } else if (id
!= UINT32_MAX
) {
230 ret
= lttng_snapshot_output_set_id(id
, output
);
240 ret
= lttng_snapshot_del_output(current_session_name
, output
);
245 if (id
!= UINT32_MAX
) {
246 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
247 id
, current_session_name
);
249 MSG("Snapshot output %s successfully deleted for session %s",
250 name
, current_session_name
);
254 lttng_snapshot_output_destroy(output
);
259 * Add output from the user URL.
261 static int add_output(const char *url
)
264 struct lttng_snapshot_output
*output
= NULL
;
266 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
271 output
= create_output_from_args(url
);
277 /* This call, if successful, populates the id of the output object. */
278 ret
= lttng_snapshot_add_output(current_session_name
, output
);
283 MSG("Snapshot output successfully added for session %s",
284 current_session_name
);
285 MSG(" [%" PRIu32
"] %s: %s (max-size: %" PRId64
")",
286 lttng_snapshot_output_get_id(output
),
287 lttng_snapshot_output_get_name(output
),
288 lttng_snapshot_output_get_ctrl_url(output
),
289 lttng_snapshot_output_get_maxsize(output
));
291 lttng_snapshot_output_destroy(output
);
295 static int cmd_add_output(int argc
, const char **argv
)
297 int ret
= CMD_SUCCESS
;
299 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
305 ret
= add_output(argv
[1]);
311 static int cmd_del_output(int argc
, const char **argv
)
313 int ret
= CMD_SUCCESS
;
324 id
= strtol(argv
[1], &name
, 10);
325 if (id
== 0 && errno
== 0) {
326 ret
= del_output(UINT32_MAX
, name
);
327 } else if (errno
== 0 && *name
== '\0') {
328 ret
= del_output(id
, NULL
);
330 ERR("Argument %s not recognized", argv
[1]);
339 static int cmd_list_output(int argc
, const char **argv
)
341 return list_output();
345 * Do a snapshot record with the URL if one is given.
347 static int record(const char *url
)
350 struct lttng_snapshot_output
*output
= NULL
;
352 output
= create_output_from_args(url
);
358 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
363 MSG("Snapshot recorded successfully for session %s", current_session_name
);
366 MSG("Snapshot written at: %s", url
);
367 } else if (opt_ctrl_url
) {
368 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url
,
373 lttng_snapshot_output_destroy(output
);
377 static int cmd_record(int argc
, const char **argv
)
382 /* With a given URL */
383 ret
= record(argv
[1]);
391 static int handle_command(const char **argv
)
393 int ret
, i
= 0, argc
;
394 struct cmd_struct
*cmd
;
396 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
397 (opt_ctrl_url
&& !opt_data_url
)) {
403 argc
= count_arguments(argv
);
406 while (cmd
->func
!= NULL
) {
408 if (strcmp(argv
[0], cmd
->name
) == 0) {
409 ret
= cmd
->func(argc
, argv
);
416 /* Command not found */
424 * The 'snapshot <cmd> <options>' first level command
426 int cmd_snapshot(int argc
, const char **argv
)
428 int opt
, ret
= CMD_SUCCESS
;
429 char *session_name
= NULL
;
430 static poptContext pc
;
432 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
433 poptReadDefaultConfig(pc
, 0);
435 while ((opt
= poptGetNextOpt(pc
)) != -1) {
440 case OPT_LIST_OPTIONS
:
441 list_cmd_options(stdout
, snapshot_opts
);
446 const char *opt
= poptGetOptArg(pc
);
448 if (utils_parse_size_suffix((char *) opt
, &val
) < 0) {
449 ERR("Unable to handle max-size value %s", opt
);
465 if (!opt_session_name
) {
466 session_name
= get_session_name();
467 if (session_name
== NULL
) {
471 current_session_name
= session_name
;
473 current_session_name
= opt_session_name
;
476 ret
= handle_command(poptGetArgs(pc
));
478 if (ret
== -LTTNG_ERR_EPERM
) {
479 ERR("The session needs to be set in no output mode (--no-output)");
481 ERR("%s", lttng_strerror(ret
));
486 if (!opt_session_name
) {