c8a7ba0588f4d47fcc5ed5de98101caee937ace5
[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.
271 */
272 static int 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 MSG("Snapshot output successfully added for session %s",
308 current_session_name);
309 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
310 lttng_snapshot_output_get_id(output), n_ptr,
311 lttng_snapshot_output_get_ctrl_url(output),
312 lttng_snapshot_output_get_maxsize(output));
313 if (lttng_opt_mi) {
314 ret = mi_lttng_snapshot_add_output(writer, current_session_name,
315 n_ptr, output);
316 if (ret) {
317 ret = CMD_ERROR;
318 }
319 }
320 error:
321 lttng_snapshot_output_destroy(output);
322 return ret;
323 }
324
325 static int cmd_add_output(int argc, const char **argv)
326 {
327 int ret;
328
329 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
330 ret = CMD_ERROR;
331 goto end;
332 }
333
334 ret = add_output(argv[1]);
335
336 end:
337 return ret;
338 }
339
340 static int cmd_del_output(int argc, const char **argv)
341 {
342 int ret;
343 char *name;
344 long id;
345
346 if (argc < 2) {
347 ret = CMD_ERROR;
348 goto end;
349 }
350
351 errno = 0;
352 id = strtol(argv[1], &name, 10);
353 if (id == 0 && errno == 0) {
354 ret = del_output(UINT32_MAX, name);
355 } else if (errno == 0 && *name == '\0') {
356 ret = del_output(id, NULL);
357 } else {
358 ERR("Argument %s not recognized", argv[1]);
359 ret = -1;
360 goto end;
361 }
362
363 end:
364 return ret;
365 }
366
367 static int cmd_list_output(int argc, const char **argv)
368 {
369 int ret;
370
371 ret = list_output();
372
373 return ret;
374 }
375
376 /*
377 * Do a snapshot record with the URL if one is given.
378 */
379 static int record(const char *url)
380 {
381 int ret;
382 struct lttng_snapshot_output *output = NULL;
383
384 output = create_output_from_args(url);
385 if (!output) {
386 ret = CMD_FATAL;
387 goto error;
388 }
389
390 ret = lttng_snapshot_record(current_session_name, output, 0);
391 if (ret < 0) {
392 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
393 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
394 }
395 goto error;
396 }
397
398 MSG("Snapshot recorded successfully for session %s", current_session_name);
399
400 if (url) {
401 MSG("Snapshot written at: %s", url);
402 } else if (opt_ctrl_url) {
403 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
404 opt_data_url);
405 }
406
407 if (lttng_opt_mi) {
408 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
409 opt_ctrl_url, opt_data_url);
410 if (ret) {
411 ret = CMD_ERROR;
412 }
413 }
414
415 error:
416 lttng_snapshot_output_destroy(output);
417 return ret;
418 }
419
420 static int cmd_record(int argc, const char **argv)
421 {
422 int ret;
423
424 if (argc == 2) {
425 ret = record(argv[1]);
426 } else {
427 ret = record(NULL);
428 }
429
430 return ret;
431 }
432
433 static int handle_command(const char **argv)
434 {
435 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
436 struct cmd_struct *cmd;
437
438 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
439 (opt_ctrl_url && !opt_data_url)) {
440 command_ret = CMD_ERROR;
441 goto end;
442 }
443
444 argc = count_arguments(argv);
445
446 cmd = &actions[i];
447 while (cmd->func != NULL) {
448 /* Find command */
449 if (strcmp(argv[0], cmd->name) == 0) {
450 if (lttng_opt_mi) {
451 /* Action element */
452 ret = mi_lttng_writer_open_element(writer,
453 mi_lttng_element_command_action);
454 if (ret) {
455 ret = CMD_ERROR;
456 goto end;
457 }
458
459 /* Name of the action */
460 ret = mi_lttng_writer_write_element_string(writer,
461 config_element_name, argv[0]);
462 if (ret) {
463 ret = CMD_ERROR;
464 goto end;
465 }
466
467 /* Open output element */
468 ret = mi_lttng_writer_open_element(writer,
469 mi_lttng_element_command_output);
470 if (ret) {
471 ret = CMD_ERROR;
472 goto end;
473 }
474 }
475
476 command_ret = cmd->func(argc, argv);
477
478 if (lttng_opt_mi) {
479 /* Close output and action element */
480 ret = mi_lttng_close_multi_element(writer, 2);
481 if (ret) {
482 ret = CMD_ERROR;
483 goto end;
484 }
485 }
486 goto end;
487 }
488 i++;
489 cmd = &actions[i];
490 }
491
492 ret = CMD_UNDEFINED;
493
494 end:
495 /* Overwrite ret if an error occurred in cmd->func() */
496 ret = command_ret ? command_ret : ret;
497 return ret;
498 }
499 /*
500 * The 'snapshot <cmd> <options>' first level command
501 */
502 int cmd_snapshot(int argc, const char **argv)
503 {
504 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
505 char *session_name = NULL;
506 static poptContext pc;
507
508 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
509 poptReadDefaultConfig(pc, 0);
510
511 /* Mi check */
512 if (lttng_opt_mi) {
513 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
514 if (!writer) {
515 ret = -LTTNG_ERR_NOMEM;
516 goto end;
517 }
518
519 /* Open command element */
520 ret = mi_lttng_writer_command_open(writer,
521 mi_lttng_element_command_snapshot);
522 if (ret) {
523 ret = CMD_ERROR;
524 goto end;
525 }
526
527 /* Open output element */
528 ret = mi_lttng_writer_open_element(writer,
529 mi_lttng_element_command_output);
530 if (ret) {
531 ret = CMD_ERROR;
532 goto end;
533 }
534 }
535
536 while ((opt = poptGetNextOpt(pc)) != -1) {
537 switch (opt) {
538 case OPT_HELP:
539 SHOW_HELP();
540 goto end;
541 case OPT_LIST_OPTIONS:
542 list_cmd_options(stdout, snapshot_opts);
543 goto end;
544 case OPT_LIST_COMMANDS:
545 list_commands(actions, stdout);
546 goto end;
547 case OPT_MAX_SIZE:
548 {
549 uint64_t val;
550 const char *opt = poptGetOptArg(pc);
551
552 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
553 ERR("Unable to handle max-size value %s", opt);
554 ret = CMD_ERROR;
555 goto end;
556 }
557
558 opt_max_size = val;
559
560 break;
561 }
562 default:
563 ret = CMD_UNDEFINED;
564 goto end;
565 }
566 }
567
568 if (!opt_session_name) {
569 session_name = get_session_name();
570 if (session_name == NULL) {
571 ret = CMD_ERROR;
572 goto end;
573 }
574 current_session_name = session_name;
575 } else {
576 current_session_name = opt_session_name;
577 }
578
579 command_ret = handle_command(poptGetArgs(pc));
580 if (command_ret) {
581 switch (-command_ret) {
582 case LTTNG_ERR_EPERM:
583 ERR("The session needs to be set in no output mode (--no-output)");
584 break;
585 case LTTNG_ERR_SNAPSHOT_NODATA:
586 WARN("%s", lttng_strerror(command_ret));
587
588 /* A warning is fine since the user has no control on
589 * whether or not applications (or the kernel) have
590 * produced any event between the start of the tracing
591 * session and the recording of the snapshot. MI wise
592 * the command is not a success since nothing was
593 * recorded.
594 */
595 command_ret = 0;
596 break;
597 default:
598 ERR("%s", lttng_strerror(command_ret));
599 break;
600 }
601 success = 0;
602 }
603
604 if (lttng_opt_mi) {
605 /* Close output element */
606 ret = mi_lttng_writer_close_element(writer);
607 if (ret) {
608 ret = CMD_ERROR;
609 goto end;
610 }
611
612 /* Success ? */
613 ret = mi_lttng_writer_write_element_bool(writer,
614 mi_lttng_element_command_success, success);
615 if (ret) {
616 ret = CMD_ERROR;
617 goto end;
618 }
619
620 /* Command element close */
621 ret = mi_lttng_writer_command_close(writer);
622 if (ret) {
623 ret = CMD_ERROR;
624 goto end;
625 }
626 }
627
628 end:
629 /* Mi clean-up */
630 if (writer && mi_lttng_writer_destroy(writer)) {
631 /* Preserve original error code */
632 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
633 }
634
635 if (!opt_session_name) {
636 free(session_name);
637 }
638
639 /* Overwrite ret if an error occured during handle_command */
640 ret = command_ret ? command_ret : ret;
641 poptFreeContext(pc);
642 return ret;
643 }
This page took 0.040481 seconds and 3 git commands to generate.