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