Add snapshot command to lttng UI
[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 if (url || (opt_ctrl_url && opt_data_url)) {
327 output = create_output_from_args(url);
328 if (!output) {
329 ret = CMD_FATAL;
330 goto error;
331 }
332 }
333
334 ret = lttng_snapshot_record(current_session_name, output, 0);
335 if (ret < 0) {
336 goto error;
337 }
338
339 MSG("Snapshot recorded successfully for session %s", current_session_name);
340
341 if (url) {
342 MSG("Snapshot written at: %s", url);
343 } else if (opt_ctrl_url) {
344 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
345 opt_data_url);
346 } else {
347 MSG("Snapshot written in session directory.");
348 }
349
350 error:
351 return ret;
352 }
353
354 static int cmd_record(int argc, const char **argv)
355 {
356 int ret;
357
358 if (argc == 2) {
359 /* With a given URL */
360 ret = record(argv[1]);
361 } else {
362 ret = record(NULL);
363 }
364
365 return ret;
366 }
367
368 static int handle_command(const char **argv)
369 {
370 int ret, i = 0, argc;
371 struct cmd_struct *cmd;
372
373 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
374 (opt_ctrl_url && !opt_data_url)) {
375 usage(stderr);
376 ret = CMD_ERROR;
377 goto end;
378 }
379
380 argc = count_arguments(argv);
381
382 cmd = &actions[i];
383 while (cmd->func != NULL) {
384 /* Find command */
385 if (strcmp(argv[0], cmd->name) == 0) {
386 ret = cmd->func(argc, argv);
387 goto end;
388 }
389 i++;
390 cmd = &actions[i];
391 }
392
393 /* Command not found */
394 ret = CMD_UNDEFINED;
395
396 end:
397 return ret;
398 }
399
400 /*
401 * The 'snapshot <cmd> <options>' first level command
402 */
403 int cmd_snapshot(int argc, const char **argv)
404 {
405 int opt, ret = CMD_SUCCESS;
406 char *session_name = NULL;
407 static poptContext pc;
408
409 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
410 poptReadDefaultConfig(pc, 0);
411
412 while ((opt = poptGetNextOpt(pc)) != -1) {
413 switch (opt) {
414 case OPT_HELP:
415 usage(stdout);
416 goto end;
417 case OPT_LIST_OPTIONS:
418 list_cmd_options(stdout, snapshot_opts);
419 goto end;
420 case OPT_MAX_SIZE:
421 {
422 long long int val;
423 char *endptr;
424 const char *opt = poptGetOptArg(pc);
425
426 val = strtoll(opt, &endptr, 10);
427 if ((errno == ERANGE && (val == LLONG_MAX || val == LONG_MIN))
428 || (errno != 0 && val == 0)) {
429 ERR("Unable to handle max-size value %s", opt);
430 ret = CMD_ERROR;
431 goto end;
432 }
433
434 if (endptr == opt) {
435 ERR("No digits were found in %s", opt);
436 ret = CMD_ERROR;
437 goto end;
438 }
439 opt_max_size = val;
440
441 break;
442 }
443 default:
444 usage(stderr);
445 ret = CMD_UNDEFINED;
446 goto end;
447 }
448 }
449
450 if (!opt_session_name) {
451 session_name = get_session_name();
452 if (session_name == NULL) {
453 ret = CMD_ERROR;
454 goto end;
455 }
456 current_session_name = session_name;
457 } else {
458 current_session_name = opt_session_name;
459 }
460
461 ret = handle_command(poptGetArgs(pc));
462 if (ret < 0) {
463 ERR("%s", lttng_strerror(ret));
464 goto end;
465 }
466
467 end:
468 if (!opt_session_name) {
469 free(session_name);
470 }
471 poptFreeContext(pc);
472 return ret;
473 }
This page took 0.039337 seconds and 5 git commands to generate.