ce63e5e875c858fc749a1e63199605469c5fc87a
[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 _LGPL_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 #include <assert.h>
29
30 #include <common/utils.h>
31 #include <common/mi-lttng.h>
32 #include <lttng/snapshot.h>
33
34 #include "../command.h"
35
36 static const char *opt_session_name;
37 static const char *opt_output_name;
38 static const char *opt_data_url;
39 static const char *opt_ctrl_url;
40 static const char *current_session_name;
41 static uint64_t opt_max_size;
42
43 /* Stub for the cmd struct actions. */
44 static int cmd_add_output(int argc, const char **argv);
45 static int cmd_del_output(int argc, const char **argv);
46 static int cmd_list_output(int argc, const char **argv);
47 static int cmd_record(int argc, const char **argv);
48
49 static const char *indent4 = " ";
50
51 enum {
52 OPT_HELP = 1,
53 OPT_LIST_OPTIONS,
54 OPT_MAX_SIZE,
55 OPT_LIST_COMMANDS,
56 };
57
58 static struct mi_writer *writer;
59
60 static struct poptOption snapshot_opts[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
62 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
63 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
64 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
65 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
66 {"name", 'n', POPT_ARG_STRING, &opt_output_name, 0, 0, 0},
67 {"max-size", 'm', POPT_ARG_STRING, 0, OPT_MAX_SIZE, 0, 0},
68 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
69 {"list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS},
70 {0, 0, 0, 0, 0, 0, 0}
71 };
72
73 static struct cmd_struct actions[] = {
74 { "add-output", cmd_add_output },
75 { "del-output", cmd_del_output },
76 { "list-output", cmd_list_output },
77 { "record", cmd_record },
78 { NULL, NULL } /* Array closure */
79 };
80
81 /*
82 * Count and return the number of arguments in argv.
83 */
84 static int count_arguments(const char **argv)
85 {
86 int i = 0;
87
88 assert(argv);
89
90 while (argv[i] != NULL) {
91 i++;
92 }
93
94 return i;
95 }
96
97 /*
98 * Create a snapshot output object from arguments using the given URL.
99 *
100 * Return a newly allocated object or NULL on error.
101 */
102 static struct lttng_snapshot_output *create_output_from_args(const char *url)
103 {
104 int ret = 0;
105 struct lttng_snapshot_output *output = NULL;
106
107 output = lttng_snapshot_output_create();
108 if (!output) {
109 goto error_create;
110 }
111
112 if (url) {
113 ret = lttng_snapshot_output_set_ctrl_url(url, output);
114 if (ret < 0) {
115 goto error;
116 }
117 } else if (opt_ctrl_url) {
118 ret = lttng_snapshot_output_set_ctrl_url(opt_ctrl_url, output);
119 if (ret < 0) {
120 goto error;
121 }
122 }
123
124 if (opt_data_url) {
125 ret = lttng_snapshot_output_set_data_url(opt_data_url, output);
126 if (ret < 0) {
127 goto error;
128 }
129 }
130
131 if (opt_max_size) {
132 ret = lttng_snapshot_output_set_size(opt_max_size, output);
133 if (ret < 0) {
134 goto error;
135 }
136 }
137
138 if (opt_output_name) {
139 ret = lttng_snapshot_output_set_name(opt_output_name, output);
140 if (ret < 0) {
141 goto error;
142 }
143 }
144
145 return output;
146
147 error:
148 lttng_snapshot_output_destroy(output);
149 error_create:
150 return NULL;
151 }
152
153 static int list_output(void)
154 {
155 int ret, output_seen = 0;
156 struct lttng_snapshot_output *s_iter;
157 struct lttng_snapshot_output_list *list;
158
159 ret = lttng_snapshot_list_output(current_session_name, &list);
160 if (ret < 0) {
161 goto error;
162 }
163
164 MSG("Snapshot output list for session %s", current_session_name);
165
166 if (lttng_opt_mi) {
167 ret = mi_lttng_snapshot_output_session_name(writer,
168 current_session_name);
169 if (ret) {
170 ret = CMD_ERROR;
171 goto end;
172 }
173 }
174
175 while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) {
176 MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4,
177 lttng_snapshot_output_get_id(s_iter),
178 lttng_snapshot_output_get_name(s_iter),
179 lttng_snapshot_output_get_ctrl_url(s_iter),
180 lttng_snapshot_output_get_maxsize(s_iter));
181 output_seen = 1;
182 if (lttng_opt_mi) {
183 ret = mi_lttng_snapshot_list_output(writer, s_iter);
184 if (ret) {
185 ret = CMD_ERROR;
186 goto end;
187 }
188 }
189 }
190
191 if (lttng_opt_mi) {
192 /* Close snapshot snapshots element */
193 ret = mi_lttng_writer_close_element(writer);
194 if (ret) {
195 ret = CMD_ERROR;
196 goto end;
197 }
198
199 /* Close snapshot session element */
200 ret = mi_lttng_writer_close_element(writer);
201 if (ret) {
202 ret = CMD_ERROR;
203 }
204 }
205 end:
206 lttng_snapshot_output_list_destroy(list);
207
208 if (!output_seen) {
209 MSG("%sNone", indent4);
210 }
211
212 error:
213 return ret;
214 }
215
216 /*
217 * Delete output by ID (machine interface version).
218 */
219 static int mi_del_output(uint32_t id, const char *name)
220 {
221 int ret;
222 struct lttng_snapshot_output *output = NULL;
223
224 assert(writer);
225
226 output = lttng_snapshot_output_create();
227 if (!output) {
228 ret = CMD_FATAL;
229 goto error;
230 }
231
232 if (name) {
233 ret = lttng_snapshot_output_set_name(name, output);
234 } else if (id != UINT32_MAX) {
235 ret = lttng_snapshot_output_set_id(id, output);
236 } else {
237 ret = CMD_ERROR;
238 goto error;
239 }
240 if (ret < 0) {
241 ret = CMD_FATAL;
242 goto error;
243 }
244
245 ret = lttng_snapshot_del_output(current_session_name, output);
246 if (ret < 0) {
247 goto error;
248 }
249
250 ret = mi_lttng_snapshot_del_output(writer, id, name, current_session_name);
251 if (ret) {
252 ret = CMD_ERROR;
253 }
254
255 error:
256 lttng_snapshot_output_destroy(output);
257 return ret;
258 }
259
260 /*
261 * Delete output by ID.
262 */
263 static int del_output(uint32_t id, const char *name)
264 {
265 int ret;
266 struct lttng_snapshot_output *output = NULL;
267
268 output = lttng_snapshot_output_create();
269 if (!output) {
270 ret = CMD_FATAL;
271 goto error;
272 }
273
274 if (name) {
275 ret = lttng_snapshot_output_set_name(name, output);
276 } else if (id != UINT32_MAX) {
277 ret = lttng_snapshot_output_set_id(id, output);
278 } else {
279 ret = CMD_ERROR;
280 goto error;
281 }
282 if (ret < 0) {
283 ret = CMD_FATAL;
284 goto error;
285 }
286
287 ret = lttng_snapshot_del_output(current_session_name, output);
288 if (ret < 0) {
289 goto error;
290 }
291
292 if (id != UINT32_MAX) {
293 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
294 id, current_session_name);
295 } else {
296 MSG("Snapshot output %s successfully deleted for session %s",
297 name, current_session_name);
298 }
299
300 error:
301 lttng_snapshot_output_destroy(output);
302 return ret;
303 }
304
305 /*
306 * Add output from the user URL (machine interface).
307 */
308 static int mi_add_output(const char *url)
309 {
310 int ret;
311 struct lttng_snapshot_output *output = NULL;
312 char name[NAME_MAX];
313 const char *n_ptr;
314
315 if (!url && (!opt_data_url || !opt_ctrl_url)) {
316 ret = CMD_ERROR;
317 goto error;
318 }
319
320 output = create_output_from_args(url);
321 if (!output) {
322 ret = CMD_FATAL;
323 goto error;
324 }
325
326 /* This call, if successful, populates the id of the output object. */
327 ret = lttng_snapshot_add_output(current_session_name, output);
328 if (ret < 0) {
329 goto error;
330 }
331
332 n_ptr = lttng_snapshot_output_get_name(output);
333 if (*n_ptr == '\0') {
334 int pret;
335 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
336 lttng_snapshot_output_get_id(output));
337 if (pret < 0) {
338 PERROR("snprintf add output name");
339 }
340 n_ptr = name;
341 }
342
343 ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr,
344 output);
345 if (ret) {
346 ret = CMD_ERROR;
347 }
348
349 error:
350 lttng_snapshot_output_destroy(output);
351 return ret;
352 }
353
354 /*
355 * Add output from the user URL.
356 */
357 static int add_output(const char *url)
358 {
359 int ret;
360 struct lttng_snapshot_output *output = NULL;
361 char name[NAME_MAX];
362 const char *n_ptr;
363
364 if (!url && (!opt_data_url || !opt_ctrl_url)) {
365 ret = CMD_ERROR;
366 goto error;
367 }
368
369 output = create_output_from_args(url);
370 if (!output) {
371 ret = CMD_FATAL;
372 goto error;
373 }
374
375 /* This call, if successful, populates the id of the output object. */
376 ret = lttng_snapshot_add_output(current_session_name, output);
377 if (ret < 0) {
378 goto error;
379 }
380
381 n_ptr = lttng_snapshot_output_get_name(output);
382 if (*n_ptr == '\0') {
383 int pret;
384 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
385 lttng_snapshot_output_get_id(output));
386 if (pret < 0) {
387 PERROR("snprintf add output name");
388 }
389 n_ptr = name;
390 }
391
392 MSG("Snapshot output successfully added for session %s",
393 current_session_name);
394 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
395 lttng_snapshot_output_get_id(output), n_ptr,
396 lttng_snapshot_output_get_ctrl_url(output),
397 lttng_snapshot_output_get_maxsize(output));
398 error:
399 lttng_snapshot_output_destroy(output);
400 return ret;
401 }
402
403 static int cmd_add_output(int argc, const char **argv)
404 {
405 int ret;
406
407 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
408 ret = CMD_ERROR;
409 goto end;
410 }
411
412 if (lttng_opt_mi) {
413 ret = mi_add_output(argv[1]);
414 } else {
415 ret = add_output(argv[1]);
416 }
417
418 end:
419 return ret;
420 }
421
422 static int cmd_del_output(int argc, const char **argv)
423 {
424 int ret;
425 char *name;
426 long id;
427
428 if (argc < 2) {
429 ret = CMD_ERROR;
430 goto end;
431 }
432
433 errno = 0;
434 id = strtol(argv[1], &name, 10);
435 if (id == 0 && errno == 0) {
436 if (lttng_opt_mi) {
437 ret = mi_del_output(UINT32_MAX, name);
438 } else {
439 ret = del_output(UINT32_MAX, name);
440 }
441 } else if (errno == 0 && *name == '\0') {
442 if (lttng_opt_mi) {
443 ret = mi_del_output(id, NULL);
444 } else {
445 ret = del_output(id, NULL);
446 }
447 } else {
448 ERR("Argument %s not recognized", argv[1]);
449 ret = -1;
450 goto end;
451 }
452
453 end:
454 return ret;
455 }
456
457 static int cmd_list_output(int argc, const char **argv)
458 {
459 int ret;
460
461 ret = list_output();
462
463 return ret;
464 }
465
466 /*
467 * Do a snapshot record with the URL if one is given.
468 */
469 static int record(const char *url)
470 {
471 int ret;
472 struct lttng_snapshot_output *output = NULL;
473
474 output = create_output_from_args(url);
475 if (!output) {
476 ret = CMD_FATAL;
477 goto error;
478 }
479
480 ret = lttng_snapshot_record(current_session_name, output, 0);
481 if (ret < 0) {
482 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
483 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
484 }
485 goto error;
486 }
487
488 MSG("Snapshot recorded successfully for session %s", current_session_name);
489
490 if (url) {
491 MSG("Snapshot written at: %s", url);
492 } else if (opt_ctrl_url) {
493 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
494 opt_data_url);
495 }
496
497 if (lttng_opt_mi) {
498 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
499 opt_ctrl_url, opt_data_url);
500 if (ret) {
501 ret = CMD_ERROR;
502 }
503 }
504
505 error:
506 lttng_snapshot_output_destroy(output);
507 return ret;
508 }
509
510 static int cmd_record(int argc, const char **argv)
511 {
512 int ret;
513
514 if (argc == 2) {
515 ret = record(argv[1]);
516 } else {
517 ret = record(NULL);
518 }
519
520 return ret;
521 }
522
523 static int handle_command(const char **argv)
524 {
525 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
526 struct cmd_struct *cmd;
527
528 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
529 (opt_ctrl_url && !opt_data_url)) {
530 command_ret = CMD_ERROR;
531 goto end;
532 }
533
534 argc = count_arguments(argv);
535
536 cmd = &actions[i];
537 while (cmd->func != NULL) {
538 /* Find command */
539 if (strcmp(argv[0], cmd->name) == 0) {
540 if (lttng_opt_mi) {
541 /* Action element */
542 ret = mi_lttng_writer_open_element(writer,
543 mi_lttng_element_command_action);
544 if (ret) {
545 ret = CMD_ERROR;
546 goto end;
547 }
548
549 /* Name of the action */
550 ret = mi_lttng_writer_write_element_string(writer,
551 config_element_name, argv[0]);
552 if (ret) {
553 ret = CMD_ERROR;
554 goto end;
555 }
556
557 /* Open output element */
558 ret = mi_lttng_writer_open_element(writer,
559 mi_lttng_element_command_output);
560 if (ret) {
561 ret = CMD_ERROR;
562 goto end;
563 }
564 }
565
566 command_ret = cmd->func(argc, argv);
567
568 if (lttng_opt_mi) {
569 /* Close output and action element */
570 ret = mi_lttng_close_multi_element(writer, 2);
571 if (ret) {
572 ret = CMD_ERROR;
573 goto end;
574 }
575 }
576 goto end;
577 }
578 i++;
579 cmd = &actions[i];
580 }
581
582 ret = CMD_UNDEFINED;
583
584 end:
585 /* Overwrite ret if an error occurred in cmd->func() */
586 ret = command_ret ? command_ret : ret;
587 return ret;
588 }
589 /*
590 * The 'snapshot <cmd> <options>' first level command
591 */
592 int cmd_snapshot(int argc, const char **argv)
593 {
594 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
595 char *session_name = NULL;
596 static poptContext pc;
597
598 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
599 poptReadDefaultConfig(pc, 0);
600
601 /* Mi check */
602 if (lttng_opt_mi) {
603 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
604 if (!writer) {
605 ret = -LTTNG_ERR_NOMEM;
606 goto end;
607 }
608
609 /* Open command element */
610 ret = mi_lttng_writer_command_open(writer,
611 mi_lttng_element_command_snapshot);
612 if (ret) {
613 ret = CMD_ERROR;
614 goto end;
615 }
616
617 /* Open output element */
618 ret = mi_lttng_writer_open_element(writer,
619 mi_lttng_element_command_output);
620 if (ret) {
621 ret = CMD_ERROR;
622 goto end;
623 }
624 }
625
626 while ((opt = poptGetNextOpt(pc)) != -1) {
627 switch (opt) {
628 case OPT_HELP:
629 SHOW_HELP();
630 goto end;
631 case OPT_LIST_OPTIONS:
632 list_cmd_options(stdout, snapshot_opts);
633 goto end;
634 case OPT_LIST_COMMANDS:
635 list_commands(actions, stdout);
636 goto end;
637 case OPT_MAX_SIZE:
638 {
639 uint64_t val;
640 const char *opt = poptGetOptArg(pc);
641
642 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
643 ERR("Unable to handle max-size value %s", opt);
644 ret = CMD_ERROR;
645 goto end;
646 }
647
648 opt_max_size = val;
649
650 break;
651 }
652 default:
653 ret = CMD_UNDEFINED;
654 goto end;
655 }
656 }
657
658 if (!opt_session_name) {
659 session_name = get_session_name();
660 if (session_name == NULL) {
661 ret = CMD_ERROR;
662 goto end;
663 }
664 current_session_name = session_name;
665 } else {
666 current_session_name = opt_session_name;
667 }
668
669 command_ret = handle_command(poptGetArgs(pc));
670 if (command_ret) {
671 switch (-command_ret) {
672 case LTTNG_ERR_EPERM:
673 ERR("The session needs to be set in no output mode (--no-output)");
674 break;
675 case LTTNG_ERR_SNAPSHOT_NODATA:
676 WARN("%s", lttng_strerror(command_ret));
677
678 /* A warning is fine since the user has no control on
679 * whether or not applications (or the kernel) have
680 * produced any event between the start of the tracing
681 * session and the recording of the snapshot. MI wise
682 * the command is not a success since nothing was
683 * recorded.
684 */
685 command_ret = 0;
686 break;
687 default:
688 ERR("%s", lttng_strerror(command_ret));
689 break;
690 }
691 success = 0;
692 }
693
694 if (lttng_opt_mi) {
695 /* Close output element */
696 ret = mi_lttng_writer_close_element(writer);
697 if (ret) {
698 ret = CMD_ERROR;
699 goto end;
700 }
701
702 /* Success ? */
703 ret = mi_lttng_writer_write_element_bool(writer,
704 mi_lttng_element_command_success, success);
705 if (ret) {
706 ret = CMD_ERROR;
707 goto end;
708 }
709
710 /* Command element close */
711 ret = mi_lttng_writer_command_close(writer);
712 if (ret) {
713 ret = CMD_ERROR;
714 goto end;
715 }
716 }
717
718 end:
719 /* Mi clean-up */
720 if (writer && mi_lttng_writer_destroy(writer)) {
721 /* Preserve original error code */
722 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
723 }
724
725 if (!opt_session_name) {
726 free(session_name);
727 }
728
729 /* Overwrite ret if an error occured during handle_command */
730 ret = command_ret ? command_ret : ret;
731 poptFreeContext(pc);
732 return ret;
733 }
This page took 0.069222 seconds and 3 git commands to generate.