Fix: support temporary snapshot max size and 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)
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 ret = lttng_snapshot_output_set_id(id, output);
226 if (ret < 0) {
227 ret = CMD_FATAL;
228 goto error;
229 }
230
231 ret = lttng_snapshot_del_output(current_session_name, output);
232 if (ret < 0) {
233 goto error;
234 }
235
236 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
237 id, current_session_name);
238
239 error:
240 lttng_snapshot_output_destroy(output);
241 return ret;
242 }
243
244 /*
245 * Add output from the user URL.
246 */
247 static int add_output(const char *url)
248 {
249 int ret;
250 struct lttng_snapshot_output *output = NULL;
251
252 if (!url && (!opt_data_url || !opt_ctrl_url)) {
253 ret = CMD_ERROR;
254 goto error;
255 }
256
257 output = create_output_from_args(url);
258 if (!output) {
259 ret = CMD_FATAL;
260 goto error;
261 }
262
263 /* This call, if successful, populates the id of the output object. */
264 ret = lttng_snapshot_add_output(current_session_name, output);
265 if (ret < 0) {
266 goto error;
267 }
268
269 MSG("Snapshot output successfully added for session %s",
270 current_session_name);
271 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
272 lttng_snapshot_output_get_id(output),
273 lttng_snapshot_output_get_name(output),
274 lttng_snapshot_output_get_ctrl_url(output),
275 lttng_snapshot_output_get_maxsize(output));
276 error:
277 lttng_snapshot_output_destroy(output);
278 return ret;
279 }
280
281 static int cmd_add_output(int argc, const char **argv)
282 {
283 int ret = CMD_SUCCESS;
284
285 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
286 usage(stderr);
287 ret = CMD_ERROR;
288 goto end;
289 }
290
291 ret = add_output(argv[1]);
292
293 end:
294 return ret;
295 }
296
297 static int cmd_del_output(int argc, const char **argv)
298 {
299 int ret = CMD_SUCCESS;
300
301 if (argc < 2) {
302 usage(stderr);
303 ret = CMD_ERROR;
304 goto end;
305 }
306
307 ret = del_output(atoi(argv[1]));
308
309 end:
310 return ret;
311 }
312
313 static int cmd_list_output(int argc, const char **argv)
314 {
315 return list_output();
316 }
317
318 /*
319 * Do a snapshot record with the URL if one is given.
320 */
321 static int record(const char *url)
322 {
323 int ret;
324 struct lttng_snapshot_output *output = NULL;
325
326 output = create_output_from_args(url);
327 if (!output) {
328 ret = CMD_FATAL;
329 goto error;
330 }
331
332 ret = lttng_snapshot_record(current_session_name, output, 0);
333 if (ret < 0) {
334 goto error;
335 }
336
337 MSG("Snapshot recorded successfully for session %s", current_session_name);
338
339 if (url) {
340 MSG("Snapshot written at: %s", url);
341 } else if (opt_ctrl_url) {
342 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
343 opt_data_url);
344 }
345
346 error:
347 lttng_snapshot_output_destroy(output);
348 return ret;
349 }
350
351 static int cmd_record(int argc, const char **argv)
352 {
353 int ret;
354
355 if (argc == 2) {
356 /* With a given URL */
357 ret = record(argv[1]);
358 } else {
359 ret = record(NULL);
360 }
361
362 return ret;
363 }
364
365 static int handle_command(const char **argv)
366 {
367 int ret, i = 0, argc;
368 struct cmd_struct *cmd;
369
370 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
371 (opt_ctrl_url && !opt_data_url)) {
372 usage(stderr);
373 ret = CMD_ERROR;
374 goto end;
375 }
376
377 argc = count_arguments(argv);
378
379 cmd = &actions[i];
380 while (cmd->func != NULL) {
381 /* Find command */
382 if (strcmp(argv[0], cmd->name) == 0) {
383 ret = cmd->func(argc, argv);
384 goto end;
385 }
386 i++;
387 cmd = &actions[i];
388 }
389
390 /* Command not found */
391 ret = CMD_UNDEFINED;
392
393 end:
394 return ret;
395 }
396
397 /*
398 * The 'snapshot <cmd> <options>' first level command
399 */
400 int cmd_snapshot(int argc, const char **argv)
401 {
402 int opt, ret = CMD_SUCCESS;
403 char *session_name = NULL;
404 static poptContext pc;
405
406 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
407 poptReadDefaultConfig(pc, 0);
408
409 while ((opt = poptGetNextOpt(pc)) != -1) {
410 switch (opt) {
411 case OPT_HELP:
412 usage(stdout);
413 goto end;
414 case OPT_LIST_OPTIONS:
415 list_cmd_options(stdout, snapshot_opts);
416 goto end;
417 case OPT_MAX_SIZE:
418 {
419 long long int val;
420 char *endptr;
421 const char *opt = poptGetOptArg(pc);
422
423 /* Documented by the man page of strtoll(3). */
424 errno = 0;
425 val = strtoll(opt, &endptr, 10);
426 if ((errno == ERANGE && (val == LLONG_MAX || val == LONG_MIN))
427 || (errno != 0 && val == 0)) {
428 ERR("Unable to handle max-size value %s", opt);
429 ret = CMD_ERROR;
430 goto end;
431 }
432
433 if (endptr == opt) {
434 ERR("No digits were found in %s", opt);
435 ret = CMD_ERROR;
436 goto end;
437 }
438 opt_max_size = val;
439
440 break;
441 }
442 default:
443 usage(stderr);
444 ret = CMD_UNDEFINED;
445 goto end;
446 }
447 }
448
449 if (!opt_session_name) {
450 session_name = get_session_name();
451 if (session_name == NULL) {
452 ret = CMD_ERROR;
453 goto end;
454 }
455 current_session_name = session_name;
456 } else {
457 current_session_name = opt_session_name;
458 }
459
460 ret = handle_command(poptGetArgs(pc));
461 if (ret < 0) {
462 if (ret == -LTTNG_ERR_EPERM) {
463 ERR("The session needs to be set in no output mode (--no-output)");
464 }
465 ERR("%s", lttng_strerror(ret));
466 goto end;
467 }
468
469 end:
470 if (!opt_session_name) {
471 free(session_name);
472 }
473 poptFreeContext(pc);
474 return ret;
475 }
This page took 0.039762 seconds and 4 git commands to generate.