Refactor: embed mi in "del_record" to remove code duplication
[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.
218 */
219 static int del_output(uint32_t id, const char *name)
220 {
221 int ret;
222 struct lttng_snapshot_output *output = NULL;
223
224 output = lttng_snapshot_output_create();
225 if (!output) {
226 ret = CMD_FATAL;
227 goto error;
228 }
229
230 if (name) {
231 ret = lttng_snapshot_output_set_name(name, output);
232 } else if (id != UINT32_MAX) {
233 ret = lttng_snapshot_output_set_id(id, output);
234 } else {
235 ret = CMD_ERROR;
236 goto error;
237 }
238 if (ret < 0) {
239 ret = CMD_FATAL;
240 goto error;
241 }
242
243 ret = lttng_snapshot_del_output(current_session_name, output);
244 if (ret < 0) {
245 goto error;
246 }
247
248 if (id != UINT32_MAX) {
249 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
250 id, current_session_name);
251 } else {
252 MSG("Snapshot output %s successfully deleted for session %s",
253 name, current_session_name);
254 }
255
256 if (lttng_opt_mi) {
257 ret = mi_lttng_snapshot_del_output(writer, id, name,
258 current_session_name);
259 if (ret) {
260 ret = CMD_ERROR;
261 }
262 }
263
264 error:
265 lttng_snapshot_output_destroy(output);
266 return ret;
267 }
268
269 /*
270 * Add output from the user URL (machine interface).
271 */
272 static int mi_add_output(const char *url)
273 {
274 int ret;
275 struct lttng_snapshot_output *output = NULL;
276 char name[NAME_MAX];
277 const char *n_ptr;
278
279 if (!url && (!opt_data_url || !opt_ctrl_url)) {
280 ret = CMD_ERROR;
281 goto error;
282 }
283
284 output = create_output_from_args(url);
285 if (!output) {
286 ret = CMD_FATAL;
287 goto error;
288 }
289
290 /* This call, if successful, populates the id of the output object. */
291 ret = lttng_snapshot_add_output(current_session_name, output);
292 if (ret < 0) {
293 goto error;
294 }
295
296 n_ptr = lttng_snapshot_output_get_name(output);
297 if (*n_ptr == '\0') {
298 int pret;
299 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
300 lttng_snapshot_output_get_id(output));
301 if (pret < 0) {
302 PERROR("snprintf add output name");
303 }
304 n_ptr = name;
305 }
306
307 ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr,
308 output);
309 if (ret) {
310 ret = CMD_ERROR;
311 }
312
313 error:
314 lttng_snapshot_output_destroy(output);
315 return ret;
316 }
317
318 /*
319 * Add output from the user URL.
320 */
321 static int add_output(const char *url)
322 {
323 int ret;
324 struct lttng_snapshot_output *output = NULL;
325 char name[NAME_MAX];
326 const char *n_ptr;
327
328 if (!url && (!opt_data_url || !opt_ctrl_url)) {
329 ret = CMD_ERROR;
330 goto error;
331 }
332
333 output = create_output_from_args(url);
334 if (!output) {
335 ret = CMD_FATAL;
336 goto error;
337 }
338
339 /* This call, if successful, populates the id of the output object. */
340 ret = lttng_snapshot_add_output(current_session_name, output);
341 if (ret < 0) {
342 goto error;
343 }
344
345 n_ptr = lttng_snapshot_output_get_name(output);
346 if (*n_ptr == '\0') {
347 int pret;
348 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
349 lttng_snapshot_output_get_id(output));
350 if (pret < 0) {
351 PERROR("snprintf add output name");
352 }
353 n_ptr = name;
354 }
355
356 MSG("Snapshot output successfully added for session %s",
357 current_session_name);
358 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
359 lttng_snapshot_output_get_id(output), n_ptr,
360 lttng_snapshot_output_get_ctrl_url(output),
361 lttng_snapshot_output_get_maxsize(output));
362 error:
363 lttng_snapshot_output_destroy(output);
364 return ret;
365 }
366
367 static int cmd_add_output(int argc, const char **argv)
368 {
369 int ret;
370
371 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
372 ret = CMD_ERROR;
373 goto end;
374 }
375
376 if (lttng_opt_mi) {
377 ret = mi_add_output(argv[1]);
378 } else {
379 ret = add_output(argv[1]);
380 }
381
382 end:
383 return ret;
384 }
385
386 static int cmd_del_output(int argc, const char **argv)
387 {
388 int ret;
389 char *name;
390 long id;
391
392 if (argc < 2) {
393 ret = CMD_ERROR;
394 goto end;
395 }
396
397 errno = 0;
398 id = strtol(argv[1], &name, 10);
399 if (id == 0 && errno == 0) {
400 ret = del_output(UINT32_MAX, name);
401 } else if (errno == 0 && *name == '\0') {
402 ret = del_output(id, NULL);
403 } else {
404 ERR("Argument %s not recognized", argv[1]);
405 ret = -1;
406 goto end;
407 }
408
409 end:
410 return ret;
411 }
412
413 static int cmd_list_output(int argc, const char **argv)
414 {
415 int ret;
416
417 ret = list_output();
418
419 return ret;
420 }
421
422 /*
423 * Do a snapshot record with the URL if one is given.
424 */
425 static int record(const char *url)
426 {
427 int ret;
428 struct lttng_snapshot_output *output = NULL;
429
430 output = create_output_from_args(url);
431 if (!output) {
432 ret = CMD_FATAL;
433 goto error;
434 }
435
436 ret = lttng_snapshot_record(current_session_name, output, 0);
437 if (ret < 0) {
438 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
439 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
440 }
441 goto error;
442 }
443
444 MSG("Snapshot recorded successfully for session %s", current_session_name);
445
446 if (url) {
447 MSG("Snapshot written at: %s", url);
448 } else if (opt_ctrl_url) {
449 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
450 opt_data_url);
451 }
452
453 if (lttng_opt_mi) {
454 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
455 opt_ctrl_url, opt_data_url);
456 if (ret) {
457 ret = CMD_ERROR;
458 }
459 }
460
461 error:
462 lttng_snapshot_output_destroy(output);
463 return ret;
464 }
465
466 static int cmd_record(int argc, const char **argv)
467 {
468 int ret;
469
470 if (argc == 2) {
471 ret = record(argv[1]);
472 } else {
473 ret = record(NULL);
474 }
475
476 return ret;
477 }
478
479 static int handle_command(const char **argv)
480 {
481 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
482 struct cmd_struct *cmd;
483
484 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
485 (opt_ctrl_url && !opt_data_url)) {
486 command_ret = CMD_ERROR;
487 goto end;
488 }
489
490 argc = count_arguments(argv);
491
492 cmd = &actions[i];
493 while (cmd->func != NULL) {
494 /* Find command */
495 if (strcmp(argv[0], cmd->name) == 0) {
496 if (lttng_opt_mi) {
497 /* Action element */
498 ret = mi_lttng_writer_open_element(writer,
499 mi_lttng_element_command_action);
500 if (ret) {
501 ret = CMD_ERROR;
502 goto end;
503 }
504
505 /* Name of the action */
506 ret = mi_lttng_writer_write_element_string(writer,
507 config_element_name, argv[0]);
508 if (ret) {
509 ret = CMD_ERROR;
510 goto end;
511 }
512
513 /* Open output element */
514 ret = mi_lttng_writer_open_element(writer,
515 mi_lttng_element_command_output);
516 if (ret) {
517 ret = CMD_ERROR;
518 goto end;
519 }
520 }
521
522 command_ret = cmd->func(argc, argv);
523
524 if (lttng_opt_mi) {
525 /* Close output and action element */
526 ret = mi_lttng_close_multi_element(writer, 2);
527 if (ret) {
528 ret = CMD_ERROR;
529 goto end;
530 }
531 }
532 goto end;
533 }
534 i++;
535 cmd = &actions[i];
536 }
537
538 ret = CMD_UNDEFINED;
539
540 end:
541 /* Overwrite ret if an error occurred in cmd->func() */
542 ret = command_ret ? command_ret : ret;
543 return ret;
544 }
545 /*
546 * The 'snapshot <cmd> <options>' first level command
547 */
548 int cmd_snapshot(int argc, const char **argv)
549 {
550 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
551 char *session_name = NULL;
552 static poptContext pc;
553
554 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
555 poptReadDefaultConfig(pc, 0);
556
557 /* Mi check */
558 if (lttng_opt_mi) {
559 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
560 if (!writer) {
561 ret = -LTTNG_ERR_NOMEM;
562 goto end;
563 }
564
565 /* Open command element */
566 ret = mi_lttng_writer_command_open(writer,
567 mi_lttng_element_command_snapshot);
568 if (ret) {
569 ret = CMD_ERROR;
570 goto end;
571 }
572
573 /* Open output element */
574 ret = mi_lttng_writer_open_element(writer,
575 mi_lttng_element_command_output);
576 if (ret) {
577 ret = CMD_ERROR;
578 goto end;
579 }
580 }
581
582 while ((opt = poptGetNextOpt(pc)) != -1) {
583 switch (opt) {
584 case OPT_HELP:
585 SHOW_HELP();
586 goto end;
587 case OPT_LIST_OPTIONS:
588 list_cmd_options(stdout, snapshot_opts);
589 goto end;
590 case OPT_LIST_COMMANDS:
591 list_commands(actions, stdout);
592 goto end;
593 case OPT_MAX_SIZE:
594 {
595 uint64_t val;
596 const char *opt = poptGetOptArg(pc);
597
598 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
599 ERR("Unable to handle max-size value %s", opt);
600 ret = CMD_ERROR;
601 goto end;
602 }
603
604 opt_max_size = val;
605
606 break;
607 }
608 default:
609 ret = CMD_UNDEFINED;
610 goto end;
611 }
612 }
613
614 if (!opt_session_name) {
615 session_name = get_session_name();
616 if (session_name == NULL) {
617 ret = CMD_ERROR;
618 goto end;
619 }
620 current_session_name = session_name;
621 } else {
622 current_session_name = opt_session_name;
623 }
624
625 command_ret = handle_command(poptGetArgs(pc));
626 if (command_ret) {
627 switch (-command_ret) {
628 case LTTNG_ERR_EPERM:
629 ERR("The session needs to be set in no output mode (--no-output)");
630 break;
631 case LTTNG_ERR_SNAPSHOT_NODATA:
632 WARN("%s", lttng_strerror(command_ret));
633
634 /* A warning is fine since the user has no control on
635 * whether or not applications (or the kernel) have
636 * produced any event between the start of the tracing
637 * session and the recording of the snapshot. MI wise
638 * the command is not a success since nothing was
639 * recorded.
640 */
641 command_ret = 0;
642 break;
643 default:
644 ERR("%s", lttng_strerror(command_ret));
645 break;
646 }
647 success = 0;
648 }
649
650 if (lttng_opt_mi) {
651 /* Close output element */
652 ret = mi_lttng_writer_close_element(writer);
653 if (ret) {
654 ret = CMD_ERROR;
655 goto end;
656 }
657
658 /* Success ? */
659 ret = mi_lttng_writer_write_element_bool(writer,
660 mi_lttng_element_command_success, success);
661 if (ret) {
662 ret = CMD_ERROR;
663 goto end;
664 }
665
666 /* Command element close */
667 ret = mi_lttng_writer_command_close(writer);
668 if (ret) {
669 ret = CMD_ERROR;
670 goto end;
671 }
672 }
673
674 end:
675 /* Mi clean-up */
676 if (writer && mi_lttng_writer_destroy(writer)) {
677 /* Preserve original error code */
678 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
679 }
680
681 if (!opt_session_name) {
682 free(session_name);
683 }
684
685 /* Overwrite ret if an error occured during handle_command */
686 ret = command_ret ? command_ret : ret;
687 poptFreeContext(pc);
688 return ret;
689 }
This page took 0.073168 seconds and 4 git commands to generate.