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 <lttng/snapshot.h>
31 #include "../command.h"
33 static const char *opt_session_name
;
34 static const char *opt_output_name
;
35 static const char *opt_data_url
;
36 static const char *opt_ctrl_url
;
37 static const char *current_session_name
;
38 static uint64_t opt_max_size
;
40 /* Stub for the cmd struct actions. */
41 static int cmd_add_output(int argc
, const char **argv
);
42 static int cmd_del_output(int argc
, const char **argv
);
43 static int cmd_list_output(int argc
, const char **argv
);
44 static int cmd_record(int argc
, const char **argv
);
46 static const char *indent4
= " ";
54 static struct poptOption snapshot_opts
[] = {
55 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
56 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
57 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
58 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
59 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
60 {"name", 'n', POPT_ARG_STRING
, &opt_output_name
, 0, 0, 0},
61 {"max-size", 'm', POPT_ARG_DOUBLE
, 0, OPT_MAX_SIZE
, 0, 0},
62 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
66 static struct cmd_struct actions
[] = {
67 { "add-output", cmd_add_output
},
68 { "del-output", cmd_del_output
},
69 { "list-output", cmd_list_output
},
70 { "record", cmd_record
},
71 { NULL
, NULL
} /* Array closure */
77 static void usage(FILE *ofp
)
79 fprintf(ofp
, "usage: lttng snapshot ACTION\n");
81 fprintf(ofp
, "Actions:\n");
82 fprintf(ofp
, " add-output [-m <SIZE>] [-s <NAME>] [-n <NAME>] <URL> | -C <URL> -D <URL>\n");
83 fprintf(ofp
, " Setup and add an snapshot output for a session.\n");
85 fprintf(ofp
, " del-output ID | NAME [-s <NAME>]\n");
86 fprintf(ofp
, " Delete an output for a session using the ID.\n");
88 fprintf(ofp
, " list-output [-s <NAME>]\n");
89 fprintf(ofp
, " List the output of a session.\n");
91 fprintf(ofp
, " record [-m <SIZE>] [-s <NAME>] [-n <NAME>] [<URL> | -C <URL> -D <URL>]\n");
92 fprintf(ofp
, " Snapshot a session's buffer(s) for all domains. If an URL is\n");
93 fprintf(ofp
, " specified, it is used instead of a previously added output.\n");
94 fprintf(ofp
, " Specifying only a name or/a size will override the current output value.\n");
95 fprintf(ofp
, " For instance, you can record a snapshot with a custom maximum size\n");
96 fprintf(ofp
, " or with a different name.\n");
98 fprintf(ofp
, "Options:\n");
99 fprintf(ofp
, " -h, --help Show this help\n");
100 fprintf(ofp
, " --list-options Simple listing of options\n");
101 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
102 fprintf(ofp
, " -n, --name NAME Name of the output or snapshot\n");
103 fprintf(ofp
, " -m, --max-size SIZE Maximum bytes size of the snapshot\n");
104 fprintf(ofp
, " -C, --ctrl-url URL Set control path URL. (Must use -D also)\n");
105 fprintf(ofp
, " -D, --data-url URL Set data path URL. (Must use -C also)\n");
110 * Count and return the number of arguments in argv.
112 static int count_arguments(const char **argv
)
118 while (argv
[i
] != NULL
) {
126 * Create a snapshot output object from arguments using the given URL.
128 * Return a newly allocated object or NULL on error.
130 static struct lttng_snapshot_output
*create_output_from_args(const char *url
)
133 struct lttng_snapshot_output
*output
= NULL
;
135 output
= lttng_snapshot_output_create();
141 ret
= lttng_snapshot_output_set_ctrl_url(url
, output
);
145 } else if (opt_ctrl_url
) {
146 ret
= lttng_snapshot_output_set_ctrl_url(opt_ctrl_url
, output
);
153 ret
= lttng_snapshot_output_set_data_url(opt_data_url
, output
);
160 ret
= lttng_snapshot_output_set_size(opt_max_size
, output
);
166 if (opt_output_name
) {
167 ret
= lttng_snapshot_output_set_name(opt_output_name
, output
);
176 lttng_snapshot_output_destroy(output
);
181 static int list_output(void)
183 int ret
, output_seen
= 0;
184 struct lttng_snapshot_output
*s_iter
;
185 struct lttng_snapshot_output_list
*list
;
187 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
192 MSG("Snapshot output list for session %s", current_session_name
);
194 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != NULL
) {
195 MSG("%s[%" PRIu32
"] %s: %s", indent4
,
196 lttng_snapshot_output_get_id(s_iter
),
197 lttng_snapshot_output_get_name(s_iter
),
198 lttng_snapshot_output_get_ctrl_url(s_iter
));
202 lttng_snapshot_output_list_destroy(list
);
205 MSG("%sNone", indent4
);
213 * Delete output by ID.
215 static int del_output(uint32_t id
, const char *name
)
218 struct lttng_snapshot_output
*output
= NULL
;
220 output
= lttng_snapshot_output_create();
227 ret
= lttng_snapshot_output_set_name(name
, output
);
228 } else if (id
!= UINT32_MAX
) {
229 ret
= lttng_snapshot_output_set_id(id
, output
);
239 ret
= lttng_snapshot_del_output(current_session_name
, output
);
244 if (id
!= UINT32_MAX
) {
245 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
246 id
, current_session_name
);
248 MSG("Snapshot output %s successfully deleted for session %s",
249 name
, current_session_name
);
253 lttng_snapshot_output_destroy(output
);
258 * Add output from the user URL.
260 static int add_output(const char *url
)
263 struct lttng_snapshot_output
*output
= NULL
;
265 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
270 output
= create_output_from_args(url
);
276 /* This call, if successful, populates the id of the output object. */
277 ret
= lttng_snapshot_add_output(current_session_name
, output
);
282 MSG("Snapshot output successfully added for session %s",
283 current_session_name
);
284 MSG(" [%" PRIu32
"] %s: %s (max-size: %" PRId64
")",
285 lttng_snapshot_output_get_id(output
),
286 lttng_snapshot_output_get_name(output
),
287 lttng_snapshot_output_get_ctrl_url(output
),
288 lttng_snapshot_output_get_maxsize(output
));
290 lttng_snapshot_output_destroy(output
);
294 static int cmd_add_output(int argc
, const char **argv
)
296 int ret
= CMD_SUCCESS
;
298 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
304 ret
= add_output(argv
[1]);
310 static int cmd_del_output(int argc
, const char **argv
)
312 int ret
= CMD_SUCCESS
;
323 id
= strtol(argv
[1], &name
, 10);
324 if (id
== 0 && errno
== 0) {
325 ret
= del_output(UINT32_MAX
, name
);
326 } else if (errno
== 0 && *name
== '\0') {
327 ret
= del_output(id
, NULL
);
329 ERR("Argument %s not recognized", argv
[1]);
338 static int cmd_list_output(int argc
, const char **argv
)
340 return list_output();
344 * Do a snapshot record with the URL if one is given.
346 static int record(const char *url
)
349 struct lttng_snapshot_output
*output
= NULL
;
351 output
= create_output_from_args(url
);
357 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
362 MSG("Snapshot recorded successfully for session %s", current_session_name
);
365 MSG("Snapshot written at: %s", url
);
366 } else if (opt_ctrl_url
) {
367 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url
,
372 lttng_snapshot_output_destroy(output
);
376 static int cmd_record(int argc
, const char **argv
)
381 /* With a given URL */
382 ret
= record(argv
[1]);
390 static int handle_command(const char **argv
)
392 int ret
, i
= 0, argc
;
393 struct cmd_struct
*cmd
;
395 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
396 (opt_ctrl_url
&& !opt_data_url
)) {
402 argc
= count_arguments(argv
);
405 while (cmd
->func
!= NULL
) {
407 if (strcmp(argv
[0], cmd
->name
) == 0) {
408 ret
= cmd
->func(argc
, argv
);
415 /* Command not found */
423 * The 'snapshot <cmd> <options>' first level command
425 int cmd_snapshot(int argc
, const char **argv
)
427 int opt
, ret
= CMD_SUCCESS
;
428 char *session_name
= NULL
;
429 static poptContext pc
;
431 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
432 poptReadDefaultConfig(pc
, 0);
434 while ((opt
= poptGetNextOpt(pc
)) != -1) {
439 case OPT_LIST_OPTIONS
:
440 list_cmd_options(stdout
, snapshot_opts
);
446 const char *opt
= poptGetOptArg(pc
);
448 /* Documented by the man page of strtoll(3). */
450 val
= strtoll(opt
, &endptr
, 10);
451 if ((errno
== ERANGE
&& (val
== LLONG_MAX
|| val
== LONG_MIN
))
452 || (errno
!= 0 && val
== 0)) {
453 ERR("Unable to handle max-size value %s", opt
);
459 ERR("No digits were found in %s", opt
);
474 if (!opt_session_name
) {
475 session_name
= get_session_name();
476 if (session_name
== NULL
) {
480 current_session_name
= session_name
;
482 current_session_name
= opt_session_name
;
485 ret
= handle_command(poptGetArgs(pc
));
487 if (ret
== -LTTNG_ERR_EPERM
) {
488 ERR("The session needs to be set in no output mode (--no-output)");
490 ERR("%s", lttng_strerror(ret
));
495 if (!opt_session_name
) {