Mi: mi backend + mi for command version
[lttng-tools.git] / src / bin / lttng / commands / snapshot.c
CommitLineData
57f272ed
DG
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18#define _GNU_SOURCE
19#include <assert.h>
20#include <inttypes.h>
21#include <popt.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
28
a8f307d8 29#include <common/utils.h>
57f272ed
DG
30#include <lttng/snapshot.h>
31
32#include "../command.h"
33
34static const char *opt_session_name;
35static const char *opt_output_name;
36static const char *opt_data_url;
37static const char *opt_ctrl_url;
38static const char *current_session_name;
39static uint64_t opt_max_size;
40
41/* Stub for the cmd struct actions. */
42static int cmd_add_output(int argc, const char **argv);
43static int cmd_del_output(int argc, const char **argv);
44static int cmd_list_output(int argc, const char **argv);
45static int cmd_record(int argc, const char **argv);
46
47static const char *indent4 = " ";
48
49enum {
50 OPT_HELP = 1,
51 OPT_LIST_OPTIONS,
52 OPT_MAX_SIZE,
3c9bd23c 53 OPT_LIST_COMMANDS,
57f272ed
DG
54};
55
56static 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},
a8f307d8 63 {"max-size", 'm', POPT_ARG_STRING, 0, OPT_MAX_SIZE, 0, 0},
57f272ed 64 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
3c9bd23c 65 {"list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS},
57f272ed
DG
66 {0, 0, 0, 0, 0, 0, 0}
67};
68
69static 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 */
75};
76
77/*
78 * usage
79 */
80static void usage(FILE *ofp)
81{
8df3bfe9 82 fprintf(ofp, "usage: lttng snapshot [OPTION] ACTION\n");
57f272ed
DG
83 fprintf(ofp, "\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");
87 fprintf(ofp, "\n");
e0dcb8bf 88 fprintf(ofp, " del-output ID | NAME [-s <NAME>]\n");
57f272ed
DG
89 fprintf(ofp, " Delete an output for a session using the ID.\n");
90 fprintf(ofp, "\n");
91 fprintf(ofp, " list-output [-s <NAME>]\n");
92 fprintf(ofp, " List the output of a session.\n");
93 fprintf(ofp, "\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");
e0dcb8bf
DG
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");
57f272ed
DG
100 fprintf(ofp, "\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");
a8f307d8 106 fprintf(ofp, " -m, --max-size SIZE Maximum bytes size of the snapshot {+k,+M,+G}\n");
57f272ed
DG
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");
109 fprintf(ofp, "\n");
110}
111
112/*
113 * Count and return the number of arguments in argv.
114 */
115static int count_arguments(const char **argv)
116{
117 int i = 0;
118
119 assert(argv);
120
121 while (argv[i] != NULL) {
122 i++;
123 }
124
125 return i;
126}
127
128/*
129 * Create a snapshot output object from arguments using the given URL.
130 *
131 * Return a newly allocated object or NULL on error.
132 */
133static struct lttng_snapshot_output *create_output_from_args(const char *url)
134{
135 int ret = 0;
136 struct lttng_snapshot_output *output = NULL;
137
138 output = lttng_snapshot_output_create();
139 if (!output) {
140 goto error_create;
141 }
142
143 if (url) {
144 ret = lttng_snapshot_output_set_ctrl_url(url, output);
145 if (ret < 0) {
146 goto error;
147 }
148 } else if (opt_ctrl_url) {
149 ret = lttng_snapshot_output_set_ctrl_url(opt_ctrl_url, output);
150 if (ret < 0) {
151 goto error;
152 }
153 }
154
155 if (opt_data_url) {
156 ret = lttng_snapshot_output_set_data_url(opt_data_url, output);
157 if (ret < 0) {
158 goto error;
159 }
160 }
161
162 if (opt_max_size) {
163 ret = lttng_snapshot_output_set_size(opt_max_size, output);
164 if (ret < 0) {
165 goto error;
166 }
167 }
168
169 if (opt_output_name) {
170 ret = lttng_snapshot_output_set_name(opt_output_name, output);
171 if (ret < 0) {
172 goto error;
173 }
174 }
175
176 return output;
177
178error:
179 lttng_snapshot_output_destroy(output);
180error_create:
181 return NULL;
182}
183
184static int list_output(void)
185{
186 int ret, output_seen = 0;
187 struct lttng_snapshot_output *s_iter;
188 struct lttng_snapshot_output_list *list;
189
190 ret = lttng_snapshot_list_output(current_session_name, &list);
191 if (ret < 0) {
192 goto error;
193 }
194
195 MSG("Snapshot output list for session %s", current_session_name);
196
197 while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) {
c3024823 198 MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4,
57f272ed
DG
199 lttng_snapshot_output_get_id(s_iter),
200 lttng_snapshot_output_get_name(s_iter),
c3024823
DG
201 lttng_snapshot_output_get_ctrl_url(s_iter),
202 lttng_snapshot_output_get_maxsize(s_iter));
57f272ed
DG
203 output_seen = 1;
204 }
205
206 lttng_snapshot_output_list_destroy(list);
207
208 if (!output_seen) {
209 MSG("%sNone", indent4);
210 }
211
212error:
213 return ret;
214}
215
216/*
217 * Delete output by ID.
218 */
eb240553 219static int del_output(uint32_t id, const char *name)
57f272ed
DG
220{
221 int ret;
222 struct lttng_snapshot_output *output = NULL;
223
224 output = lttng_snapshot_output_create();
225 if (!output) {
226 ret = CMD_FATAL;
227 goto error;
228 }
229
eb240553
DG
230 if (name) {
231 ret = lttng_snapshot_output_set_name(name, output);
232 } else if (id != UINT32_MAX) {
233 ret = lttng_snapshot_output_set_id(id, output);
234 } else {
235 ret = CMD_ERROR;
236 goto error;
237 }
57f272ed
DG
238 if (ret < 0) {
239 ret = CMD_FATAL;
240 goto error;
241 }
242
243 ret = lttng_snapshot_del_output(current_session_name, output);
244 if (ret < 0) {
245 goto error;
246 }
247
eb240553
DG
248 if (id != UINT32_MAX) {
249 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
250 id, current_session_name);
251 } else {
252 MSG("Snapshot output %s successfully deleted for session %s",
253 name, current_session_name);
254 }
57f272ed
DG
255
256error:
257 lttng_snapshot_output_destroy(output);
258 return ret;
259}
260
261/*
262 * Add output from the user URL.
263 */
264static int add_output(const char *url)
265{
266 int ret;
267 struct lttng_snapshot_output *output = NULL;
6efe784e
DG
268 char name[NAME_MAX];
269 const char *n_ptr;
57f272ed
DG
270
271 if (!url && (!opt_data_url || !opt_ctrl_url)) {
272 ret = CMD_ERROR;
273 goto error;
274 }
275
276 output = create_output_from_args(url);
277 if (!output) {
278 ret = CMD_FATAL;
279 goto error;
280 }
281
282 /* This call, if successful, populates the id of the output object. */
283 ret = lttng_snapshot_add_output(current_session_name, output);
284 if (ret < 0) {
285 goto error;
286 }
287
6efe784e
DG
288 n_ptr = lttng_snapshot_output_get_name(output);
289 if (*n_ptr == '\0') {
290 int pret;
291 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
292 lttng_snapshot_output_get_id(output));
293 if (pret < 0) {
294 PERROR("snprintf add output name");
295 }
296 n_ptr = name;
297 }
298
57f272ed
DG
299 MSG("Snapshot output successfully added for session %s",
300 current_session_name);
301 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
6efe784e 302 lttng_snapshot_output_get_id(output), n_ptr,
57f272ed
DG
303 lttng_snapshot_output_get_ctrl_url(output),
304 lttng_snapshot_output_get_maxsize(output));
305error:
306 lttng_snapshot_output_destroy(output);
307 return ret;
308}
309
310static int cmd_add_output(int argc, const char **argv)
311{
312 int ret = CMD_SUCCESS;
313
314 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
315 usage(stderr);
316 ret = CMD_ERROR;
317 goto end;
318 }
319
320 ret = add_output(argv[1]);
321
322end:
323 return ret;
324}
325
326static int cmd_del_output(int argc, const char **argv)
327{
328 int ret = CMD_SUCCESS;
eb240553
DG
329 char *name;
330 long id;
57f272ed
DG
331
332 if (argc < 2) {
333 usage(stderr);
334 ret = CMD_ERROR;
335 goto end;
336 }
337
eb240553
DG
338 errno = 0;
339 id = strtol(argv[1], &name, 10);
340 if (id == 0 && errno == 0) {
341 ret = del_output(UINT32_MAX, name);
342 } else if (errno == 0 && *name == '\0') {
343 ret = del_output(id, NULL);
344 } else {
345 ERR("Argument %s not recognized", argv[1]);
346 ret = -1;
347 goto end;
348 }
57f272ed
DG
349
350end:
351 return ret;
352}
353
354static int cmd_list_output(int argc, const char **argv)
355{
356 return list_output();
357}
358
359/*
360 * Do a snapshot record with the URL if one is given.
361 */
362static int record(const char *url)
363{
364 int ret;
365 struct lttng_snapshot_output *output = NULL;
366
e1986656
DG
367 output = create_output_from_args(url);
368 if (!output) {
369 ret = CMD_FATAL;
370 goto error;
57f272ed
DG
371 }
372
373 ret = lttng_snapshot_record(current_session_name, output, 0);
374 if (ret < 0) {
68808f4e
DG
375 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
376 ERR("The minimum size of a snapshot is computed by multiplying "
377 "the total amount of streams with the largest subbuffer "
378 "in the session.");
379 }
57f272ed
DG
380 goto error;
381 }
382
383 MSG("Snapshot recorded successfully for session %s", current_session_name);
384
385 if (url) {
386 MSG("Snapshot written at: %s", url);
387 } else if (opt_ctrl_url) {
388 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
389 opt_data_url);
57f272ed
DG
390 }
391
392error:
cdcdb9dd 393 lttng_snapshot_output_destroy(output);
57f272ed
DG
394 return ret;
395}
396
397static int cmd_record(int argc, const char **argv)
398{
399 int ret;
400
401 if (argc == 2) {
402 /* With a given URL */
403 ret = record(argv[1]);
404 } else {
405 ret = record(NULL);
406 }
407
408 return ret;
409}
410
411static int handle_command(const char **argv)
412{
413 int ret, i = 0, argc;
414 struct cmd_struct *cmd;
415
416 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
417 (opt_ctrl_url && !opt_data_url)) {
418 usage(stderr);
419 ret = CMD_ERROR;
420 goto end;
421 }
422
423 argc = count_arguments(argv);
424
425 cmd = &actions[i];
426 while (cmd->func != NULL) {
427 /* Find command */
428 if (strcmp(argv[0], cmd->name) == 0) {
429 ret = cmd->func(argc, argv);
430 goto end;
431 }
432 i++;
433 cmd = &actions[i];
434 }
435
436 /* Command not found */
437 ret = CMD_UNDEFINED;
438
439end:
440 return ret;
441}
442
443/*
444 * The 'snapshot <cmd> <options>' first level command
445 */
446int cmd_snapshot(int argc, const char **argv)
447{
448 int opt, ret = CMD_SUCCESS;
449 char *session_name = NULL;
450 static poptContext pc;
451
452 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
453 poptReadDefaultConfig(pc, 0);
454
c7e35b03
JR
455 /* TODO: mi support */
456 if (lttng_opt_mi) {
457 ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED;
458 ERR("mi option not supported");
459 goto end;
460 }
461
57f272ed
DG
462 while ((opt = poptGetNextOpt(pc)) != -1) {
463 switch (opt) {
464 case OPT_HELP:
465 usage(stdout);
466 goto end;
467 case OPT_LIST_OPTIONS:
468 list_cmd_options(stdout, snapshot_opts);
469 goto end;
3c9bd23c
SM
470 case OPT_LIST_COMMANDS:
471 list_commands(actions, stdout);
472 goto end;
57f272ed
DG
473 case OPT_MAX_SIZE:
474 {
a8f307d8 475 uint64_t val;
57f272ed
DG
476 const char *opt = poptGetOptArg(pc);
477
a8f307d8 478 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
57f272ed
DG
479 ERR("Unable to handle max-size value %s", opt);
480 ret = CMD_ERROR;
481 goto end;
482 }
483
57f272ed
DG
484 opt_max_size = val;
485
486 break;
487 }
488 default:
489 usage(stderr);
490 ret = CMD_UNDEFINED;
491 goto end;
492 }
493 }
494
495 if (!opt_session_name) {
496 session_name = get_session_name();
497 if (session_name == NULL) {
498 ret = CMD_ERROR;
499 goto end;
500 }
501 current_session_name = session_name;
502 } else {
503 current_session_name = opt_session_name;
504 }
505
506 ret = handle_command(poptGetArgs(pc));
507 if (ret < 0) {
7badf927
DG
508 switch (-ret) {
509 case LTTNG_ERR_EPERM:
6dc3064a 510 ERR("The session needs to be set in no output mode (--no-output)");
7badf927
DG
511 break;
512 case LTTNG_ERR_SNAPSHOT_NODATA:
513 WARN("%s", lttng_strerror(ret));
514 break;
515 default:
516 ERR("%s", lttng_strerror(ret));
517 break;
6dc3064a 518 }
57f272ed
DG
519 goto end;
520 }
521
522end:
523 if (!opt_session_name) {
524 free(session_name);
525 }
526 poptFreeContext(pc);
527 return ret;
528}
This page took 0.047125 seconds and 4 git commands to generate.