Manpage: cleanup layout of snapshot
[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
29#include <lttng/snapshot.h>
30
31#include "../command.h"
32
33static const char *opt_session_name;
34static const char *opt_output_name;
35static const char *opt_data_url;
36static const char *opt_ctrl_url;
37static const char *current_session_name;
38static uint64_t opt_max_size;
39
40/* Stub for the cmd struct actions. */
41static int cmd_add_output(int argc, const char **argv);
42static int cmd_del_output(int argc, const char **argv);
43static int cmd_list_output(int argc, const char **argv);
44static int cmd_record(int argc, const char **argv);
45
46static const char *indent4 = " ";
47
48enum {
49 OPT_HELP = 1,
50 OPT_LIST_OPTIONS,
51 OPT_MAX_SIZE,
52};
53
54static 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},
63 {0, 0, 0, 0, 0, 0, 0}
64};
65
66static 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 */
72};
73
74/*
75 * usage
76 */
77static void usage(FILE *ofp)
78{
8df3bfe9 79 fprintf(ofp, "usage: lttng snapshot [OPTION] ACTION\n");
57f272ed
DG
80 fprintf(ofp, "\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");
84 fprintf(ofp, "\n");
e0dcb8bf 85 fprintf(ofp, " del-output ID | NAME [-s <NAME>]\n");
57f272ed
DG
86 fprintf(ofp, " Delete an output for a session using the ID.\n");
87 fprintf(ofp, "\n");
88 fprintf(ofp, " list-output [-s <NAME>]\n");
89 fprintf(ofp, " List the output of a session.\n");
90 fprintf(ofp, "\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");
e0dcb8bf
DG
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");
57f272ed
DG
97 fprintf(ofp, "\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");
106 fprintf(ofp, "\n");
107}
108
109/*
110 * Count and return the number of arguments in argv.
111 */
112static int count_arguments(const char **argv)
113{
114 int i = 0;
115
116 assert(argv);
117
118 while (argv[i] != NULL) {
119 i++;
120 }
121
122 return i;
123}
124
125/*
126 * Create a snapshot output object from arguments using the given URL.
127 *
128 * Return a newly allocated object or NULL on error.
129 */
130static struct lttng_snapshot_output *create_output_from_args(const char *url)
131{
132 int ret = 0;
133 struct lttng_snapshot_output *output = NULL;
134
135 output = lttng_snapshot_output_create();
136 if (!output) {
137 goto error_create;
138 }
139
140 if (url) {
141 ret = lttng_snapshot_output_set_ctrl_url(url, output);
142 if (ret < 0) {
143 goto error;
144 }
145 } else if (opt_ctrl_url) {
146 ret = lttng_snapshot_output_set_ctrl_url(opt_ctrl_url, output);
147 if (ret < 0) {
148 goto error;
149 }
150 }
151
152 if (opt_data_url) {
153 ret = lttng_snapshot_output_set_data_url(opt_data_url, output);
154 if (ret < 0) {
155 goto error;
156 }
157 }
158
159 if (opt_max_size) {
160 ret = lttng_snapshot_output_set_size(opt_max_size, output);
161 if (ret < 0) {
162 goto error;
163 }
164 }
165
166 if (opt_output_name) {
167 ret = lttng_snapshot_output_set_name(opt_output_name, output);
168 if (ret < 0) {
169 goto error;
170 }
171 }
172
173 return output;
174
175error:
176 lttng_snapshot_output_destroy(output);
177error_create:
178 return NULL;
179}
180
181static int list_output(void)
182{
183 int ret, output_seen = 0;
184 struct lttng_snapshot_output *s_iter;
185 struct lttng_snapshot_output_list *list;
186
187 ret = lttng_snapshot_list_output(current_session_name, &list);
188 if (ret < 0) {
189 goto error;
190 }
191
192 MSG("Snapshot output list for session %s", current_session_name);
193
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));
199 output_seen = 1;
200 }
201
202 lttng_snapshot_output_list_destroy(list);
203
204 if (!output_seen) {
205 MSG("%sNone", indent4);
206 }
207
208error:
209 return ret;
210}
211
212/*
213 * Delete output by ID.
214 */
eb240553 215static int del_output(uint32_t id, const char *name)
57f272ed
DG
216{
217 int ret;
218 struct lttng_snapshot_output *output = NULL;
219
220 output = lttng_snapshot_output_create();
221 if (!output) {
222 ret = CMD_FATAL;
223 goto error;
224 }
225
eb240553
DG
226 if (name) {
227 ret = lttng_snapshot_output_set_name(name, output);
228 } else if (id != UINT32_MAX) {
229 ret = lttng_snapshot_output_set_id(id, output);
230 } else {
231 ret = CMD_ERROR;
232 goto error;
233 }
57f272ed
DG
234 if (ret < 0) {
235 ret = CMD_FATAL;
236 goto error;
237 }
238
239 ret = lttng_snapshot_del_output(current_session_name, output);
240 if (ret < 0) {
241 goto error;
242 }
243
eb240553
DG
244 if (id != UINT32_MAX) {
245 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
246 id, current_session_name);
247 } else {
248 MSG("Snapshot output %s successfully deleted for session %s",
249 name, current_session_name);
250 }
57f272ed
DG
251
252error:
253 lttng_snapshot_output_destroy(output);
254 return ret;
255}
256
257/*
258 * Add output from the user URL.
259 */
260static int add_output(const char *url)
261{
262 int ret;
263 struct lttng_snapshot_output *output = NULL;
264
265 if (!url && (!opt_data_url || !opt_ctrl_url)) {
266 ret = CMD_ERROR;
267 goto error;
268 }
269
270 output = create_output_from_args(url);
271 if (!output) {
272 ret = CMD_FATAL;
273 goto error;
274 }
275
276 /* This call, if successful, populates the id of the output object. */
277 ret = lttng_snapshot_add_output(current_session_name, output);
278 if (ret < 0) {
279 goto error;
280 }
281
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));
289error:
290 lttng_snapshot_output_destroy(output);
291 return ret;
292}
293
294static int cmd_add_output(int argc, const char **argv)
295{
296 int ret = CMD_SUCCESS;
297
298 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
299 usage(stderr);
300 ret = CMD_ERROR;
301 goto end;
302 }
303
304 ret = add_output(argv[1]);
305
306end:
307 return ret;
308}
309
310static int cmd_del_output(int argc, const char **argv)
311{
312 int ret = CMD_SUCCESS;
eb240553
DG
313 char *name;
314 long id;
57f272ed
DG
315
316 if (argc < 2) {
317 usage(stderr);
318 ret = CMD_ERROR;
319 goto end;
320 }
321
eb240553
DG
322 errno = 0;
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);
328 } else {
329 ERR("Argument %s not recognized", argv[1]);
330 ret = -1;
331 goto end;
332 }
57f272ed
DG
333
334end:
335 return ret;
336}
337
338static int cmd_list_output(int argc, const char **argv)
339{
340 return list_output();
341}
342
343/*
344 * Do a snapshot record with the URL if one is given.
345 */
346static int record(const char *url)
347{
348 int ret;
349 struct lttng_snapshot_output *output = NULL;
350
e1986656
DG
351 output = create_output_from_args(url);
352 if (!output) {
353 ret = CMD_FATAL;
354 goto error;
57f272ed
DG
355 }
356
357 ret = lttng_snapshot_record(current_session_name, output, 0);
358 if (ret < 0) {
359 goto error;
360 }
361
362 MSG("Snapshot recorded successfully for session %s", current_session_name);
363
364 if (url) {
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,
368 opt_data_url);
57f272ed
DG
369 }
370
371error:
cdcdb9dd 372 lttng_snapshot_output_destroy(output);
57f272ed
DG
373 return ret;
374}
375
376static int cmd_record(int argc, const char **argv)
377{
378 int ret;
379
380 if (argc == 2) {
381 /* With a given URL */
382 ret = record(argv[1]);
383 } else {
384 ret = record(NULL);
385 }
386
387 return ret;
388}
389
390static int handle_command(const char **argv)
391{
392 int ret, i = 0, argc;
393 struct cmd_struct *cmd;
394
395 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
396 (opt_ctrl_url && !opt_data_url)) {
397 usage(stderr);
398 ret = CMD_ERROR;
399 goto end;
400 }
401
402 argc = count_arguments(argv);
403
404 cmd = &actions[i];
405 while (cmd->func != NULL) {
406 /* Find command */
407 if (strcmp(argv[0], cmd->name) == 0) {
408 ret = cmd->func(argc, argv);
409 goto end;
410 }
411 i++;
412 cmd = &actions[i];
413 }
414
415 /* Command not found */
416 ret = CMD_UNDEFINED;
417
418end:
419 return ret;
420}
421
422/*
423 * The 'snapshot <cmd> <options>' first level command
424 */
425int cmd_snapshot(int argc, const char **argv)
426{
427 int opt, ret = CMD_SUCCESS;
428 char *session_name = NULL;
429 static poptContext pc;
430
431 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
432 poptReadDefaultConfig(pc, 0);
433
434 while ((opt = poptGetNextOpt(pc)) != -1) {
435 switch (opt) {
436 case OPT_HELP:
437 usage(stdout);
438 goto end;
439 case OPT_LIST_OPTIONS:
440 list_cmd_options(stdout, snapshot_opts);
441 goto end;
442 case OPT_MAX_SIZE:
443 {
444 long long int val;
445 char *endptr;
446 const char *opt = poptGetOptArg(pc);
447
e1986656
DG
448 /* Documented by the man page of strtoll(3). */
449 errno = 0;
57f272ed
DG
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);
454 ret = CMD_ERROR;
455 goto end;
456 }
457
458 if (endptr == opt) {
459 ERR("No digits were found in %s", opt);
460 ret = CMD_ERROR;
461 goto end;
462 }
463 opt_max_size = val;
464
465 break;
466 }
467 default:
468 usage(stderr);
469 ret = CMD_UNDEFINED;
470 goto end;
471 }
472 }
473
474 if (!opt_session_name) {
475 session_name = get_session_name();
476 if (session_name == NULL) {
477 ret = CMD_ERROR;
478 goto end;
479 }
480 current_session_name = session_name;
481 } else {
482 current_session_name = opt_session_name;
483 }
484
485 ret = handle_command(poptGetArgs(pc));
486 if (ret < 0) {
6dc3064a
DG
487 if (ret == -LTTNG_ERR_EPERM) {
488 ERR("The session needs to be set in no output mode (--no-output)");
489 }
57f272ed
DG
490 ERR("%s", lttng_strerror(ret));
491 goto end;
492 }
493
494end:
495 if (!opt_session_name) {
496 free(session_name);
497 }
498 poptFreeContext(pc);
499 return ret;
500}
This page took 0.041556 seconds and 4 git commands to generate.