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