Commit | Line | Data |
---|---|---|
57f272ed DG |
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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
57f272ed DG |
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> | |
50534d6f | 28 | #include <assert.h> |
57f272ed | 29 | |
a8f307d8 | 30 | #include <common/utils.h> |
50534d6f | 31 | #include <common/mi-lttng.h> |
57f272ed DG |
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, | |
3c9bd23c | 55 | OPT_LIST_COMMANDS, |
57f272ed DG |
56 | }; |
57 | ||
50534d6f JRJ |
58 | static struct mi_writer *writer; |
59 | ||
57f272ed DG |
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}, | |
a8f307d8 | 67 | {"max-size", 'm', POPT_ARG_STRING, 0, OPT_MAX_SIZE, 0, 0}, |
57f272ed | 68 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
3c9bd23c | 69 | {"list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS}, |
57f272ed DG |
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 | ||
57f272ed DG |
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 | ||
8e610b42 | 153 | static int list_output(void) |
50534d6f | 154 | { |
8e610b42 | 155 | int ret, output_seen = 0; |
50534d6f JRJ |
156 | struct lttng_snapshot_output *s_iter; |
157 | struct lttng_snapshot_output_list *list; | |
158 | ||
50534d6f JRJ |
159 | ret = lttng_snapshot_list_output(current_session_name, &list); |
160 | if (ret < 0) { | |
161 | goto error; | |
162 | } | |
163 | ||
8e610b42 | 164 | MSG("Snapshot output list for session %s", current_session_name); |
50534d6f | 165 | |
8e610b42 JR |
166 | if (lttng_opt_mi) { |
167 | ret = mi_lttng_snapshot_output_session_name(writer, | |
168 | current_session_name); | |
50534d6f JRJ |
169 | if (ret) { |
170 | ret = CMD_ERROR; | |
171 | goto end; | |
172 | } | |
173 | } | |
174 | ||
57f272ed | 175 | while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) { |
c3024823 | 176 | MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4, |
57f272ed DG |
177 | lttng_snapshot_output_get_id(s_iter), |
178 | lttng_snapshot_output_get_name(s_iter), | |
c3024823 DG |
179 | lttng_snapshot_output_get_ctrl_url(s_iter), |
180 | lttng_snapshot_output_get_maxsize(s_iter)); | |
57f272ed | 181 | output_seen = 1; |
8e610b42 JR |
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 | } | |
57f272ed DG |
189 | } |
190 | ||
8e610b42 JR |
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: | |
57f272ed DG |
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 | ||
50534d6f JRJ |
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) { | |
50534d6f JRJ |
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 | ||
57f272ed DG |
260 | /* |
261 | * Delete output by ID. | |
262 | */ | |
eb240553 | 263 | static int del_output(uint32_t id, const char *name) |
57f272ed DG |
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 | ||
eb240553 DG |
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 | } | |
57f272ed DG |
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 | ||
eb240553 DG |
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 | } | |
57f272ed DG |
299 | |
300 | error: | |
301 | lttng_snapshot_output_destroy(output); | |
302 | return ret; | |
303 | } | |
304 | ||
50534d6f JRJ |
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) { | |
50534d6f JRJ |
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 | ||
57f272ed DG |
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; | |
6efe784e DG |
361 | char name[NAME_MAX]; |
362 | const char *n_ptr; | |
57f272ed DG |
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 | ||
6efe784e DG |
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 | ||
57f272ed DG |
392 | MSG("Snapshot output successfully added for session %s", |
393 | current_session_name); | |
394 | MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", | |
6efe784e | 395 | lttng_snapshot_output_get_id(output), n_ptr, |
57f272ed DG |
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 | { | |
50534d6f | 405 | int ret; |
57f272ed DG |
406 | |
407 | if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) { | |
57f272ed DG |
408 | ret = CMD_ERROR; |
409 | goto end; | |
410 | } | |
411 | ||
50534d6f JRJ |
412 | if (lttng_opt_mi) { |
413 | ret = mi_add_output(argv[1]); | |
414 | } else { | |
415 | ret = add_output(argv[1]); | |
416 | } | |
57f272ed DG |
417 | |
418 | end: | |
419 | return ret; | |
420 | } | |
421 | ||
422 | static int cmd_del_output(int argc, const char **argv) | |
423 | { | |
50534d6f | 424 | int ret; |
eb240553 DG |
425 | char *name; |
426 | long id; | |
57f272ed DG |
427 | |
428 | if (argc < 2) { | |
57f272ed DG |
429 | ret = CMD_ERROR; |
430 | goto end; | |
431 | } | |
432 | ||
eb240553 DG |
433 | errno = 0; |
434 | id = strtol(argv[1], &name, 10); | |
435 | if (id == 0 && errno == 0) { | |
50534d6f JRJ |
436 | if (lttng_opt_mi) { |
437 | ret = mi_del_output(UINT32_MAX, name); | |
438 | } else { | |
439 | ret = del_output(UINT32_MAX, name); | |
440 | } | |
eb240553 | 441 | } else if (errno == 0 && *name == '\0') { |
50534d6f JRJ |
442 | if (lttng_opt_mi) { |
443 | ret = mi_del_output(id, NULL); | |
444 | } else { | |
445 | ret = del_output(id, NULL); | |
446 | } | |
eb240553 DG |
447 | } else { |
448 | ERR("Argument %s not recognized", argv[1]); | |
449 | ret = -1; | |
450 | goto end; | |
451 | } | |
57f272ed DG |
452 | |
453 | end: | |
454 | return ret; | |
455 | } | |
456 | ||
457 | static int cmd_list_output(int argc, const char **argv) | |
458 | { | |
50534d6f JRJ |
459 | int ret; |
460 | ||
8e610b42 | 461 | ret = list_output(); |
50534d6f JRJ |
462 | |
463 | return ret; | |
464 | } | |
465 | ||
466 | /* | |
467 | * Do a snapshot record with the URL if one is given (machine interface). | |
468 | */ | |
469 | static int mi_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 | ret = CMD_ERROR; | |
483 | goto error; | |
484 | } | |
485 | ||
486 | ret = mi_lttng_snapshot_record(writer, current_session_name, url, | |
487 | opt_ctrl_url, opt_data_url); | |
488 | if (ret) { | |
489 | ret = CMD_ERROR; | |
490 | } | |
491 | ||
492 | error: | |
493 | lttng_snapshot_output_destroy(output); | |
494 | return ret; | |
57f272ed DG |
495 | } |
496 | ||
497 | /* | |
498 | * Do a snapshot record with the URL if one is given. | |
499 | */ | |
500 | static int record(const char *url) | |
501 | { | |
502 | int ret; | |
503 | struct lttng_snapshot_output *output = NULL; | |
504 | ||
e1986656 DG |
505 | output = create_output_from_args(url); |
506 | if (!output) { | |
507 | ret = CMD_FATAL; | |
508 | goto error; | |
57f272ed DG |
509 | } |
510 | ||
511 | ret = lttng_snapshot_record(current_session_name, output, 0); | |
512 | if (ret < 0) { | |
68808f4e | 513 | if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) { |
d07ceecd | 514 | ERR("Invalid snapshot size. Cannot fit at least one packet per stream."); |
68808f4e | 515 | } |
57f272ed DG |
516 | goto error; |
517 | } | |
518 | ||
519 | MSG("Snapshot recorded successfully for session %s", current_session_name); | |
520 | ||
521 | if (url) { | |
522 | MSG("Snapshot written at: %s", url); | |
523 | } else if (opt_ctrl_url) { | |
524 | MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url, | |
525 | opt_data_url); | |
57f272ed DG |
526 | } |
527 | ||
528 | error: | |
cdcdb9dd | 529 | lttng_snapshot_output_destroy(output); |
57f272ed DG |
530 | return ret; |
531 | } | |
532 | ||
533 | static int cmd_record(int argc, const char **argv) | |
534 | { | |
535 | int ret; | |
536 | ||
537 | if (argc == 2) { | |
538 | /* With a given URL */ | |
50534d6f JRJ |
539 | if (lttng_opt_mi) { |
540 | ret = mi_record(argv[1]); | |
541 | } else { | |
542 | ret = record(argv[1]); | |
543 | } | |
57f272ed | 544 | } else { |
50534d6f JRJ |
545 | if (lttng_opt_mi) { |
546 | ret = mi_record(NULL); | |
547 | } else { | |
548 | ret = record(NULL); | |
549 | } | |
57f272ed DG |
550 | } |
551 | ||
552 | return ret; | |
553 | } | |
554 | ||
555 | static int handle_command(const char **argv) | |
556 | { | |
50534d6f | 557 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; |
57f272ed DG |
558 | struct cmd_struct *cmd; |
559 | ||
560 | if (argv == NULL || (!opt_ctrl_url && opt_data_url) || | |
561 | (opt_ctrl_url && !opt_data_url)) { | |
50534d6f | 562 | command_ret = CMD_ERROR; |
57f272ed DG |
563 | goto end; |
564 | } | |
565 | ||
566 | argc = count_arguments(argv); | |
567 | ||
568 | cmd = &actions[i]; | |
569 | while (cmd->func != NULL) { | |
570 | /* Find command */ | |
571 | if (strcmp(argv[0], cmd->name) == 0) { | |
50534d6f JRJ |
572 | if (lttng_opt_mi) { |
573 | /* Action element */ | |
574 | ret = mi_lttng_writer_open_element(writer, | |
575 | mi_lttng_element_command_action); | |
576 | if (ret) { | |
577 | ret = CMD_ERROR; | |
578 | goto end; | |
579 | } | |
580 | ||
581 | /* Name of the action */ | |
582 | ret = mi_lttng_writer_write_element_string(writer, | |
583 | config_element_name, argv[0]); | |
584 | if (ret) { | |
585 | ret = CMD_ERROR; | |
586 | goto end; | |
587 | } | |
588 | ||
589 | /* Open output element */ | |
590 | ret = mi_lttng_writer_open_element(writer, | |
591 | mi_lttng_element_command_output); | |
592 | if (ret) { | |
593 | ret = CMD_ERROR; | |
594 | goto end; | |
595 | } | |
596 | } | |
597 | ||
598 | command_ret = cmd->func(argc, argv); | |
599 | ||
600 | if (lttng_opt_mi) { | |
601 | /* Close output and action element */ | |
602 | ret = mi_lttng_close_multi_element(writer, 2); | |
603 | if (ret) { | |
604 | ret = CMD_ERROR; | |
605 | goto end; | |
606 | } | |
607 | } | |
57f272ed DG |
608 | goto end; |
609 | } | |
610 | i++; | |
611 | cmd = &actions[i]; | |
612 | } | |
613 | ||
1ec06ed3 | 614 | ret = CMD_UNDEFINED; |
57f272ed DG |
615 | |
616 | end: | |
1ec06ed3 | 617 | /* Overwrite ret if an error occurred in cmd->func() */ |
50534d6f | 618 | ret = command_ret ? command_ret : ret; |
57f272ed DG |
619 | return ret; |
620 | } | |
57f272ed DG |
621 | /* |
622 | * The 'snapshot <cmd> <options>' first level command | |
623 | */ | |
624 | int cmd_snapshot(int argc, const char **argv) | |
625 | { | |
50534d6f | 626 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
57f272ed DG |
627 | char *session_name = NULL; |
628 | static poptContext pc; | |
629 | ||
630 | pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0); | |
631 | poptReadDefaultConfig(pc, 0); | |
632 | ||
50534d6f | 633 | /* Mi check */ |
c7e35b03 | 634 | if (lttng_opt_mi) { |
50534d6f JRJ |
635 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
636 | if (!writer) { | |
637 | ret = -LTTNG_ERR_NOMEM; | |
638 | goto end; | |
639 | } | |
640 | ||
641 | /* Open command element */ | |
642 | ret = mi_lttng_writer_command_open(writer, | |
643 | mi_lttng_element_command_snapshot); | |
644 | if (ret) { | |
645 | ret = CMD_ERROR; | |
646 | goto end; | |
647 | } | |
648 | ||
649 | /* Open output element */ | |
650 | ret = mi_lttng_writer_open_element(writer, | |
651 | mi_lttng_element_command_output); | |
652 | if (ret) { | |
653 | ret = CMD_ERROR; | |
654 | goto end; | |
655 | } | |
c7e35b03 JR |
656 | } |
657 | ||
57f272ed DG |
658 | while ((opt = poptGetNextOpt(pc)) != -1) { |
659 | switch (opt) { | |
660 | case OPT_HELP: | |
4ba92f18 | 661 | SHOW_HELP(); |
57f272ed DG |
662 | goto end; |
663 | case OPT_LIST_OPTIONS: | |
664 | list_cmd_options(stdout, snapshot_opts); | |
665 | goto end; | |
3c9bd23c SM |
666 | case OPT_LIST_COMMANDS: |
667 | list_commands(actions, stdout); | |
668 | goto end; | |
57f272ed DG |
669 | case OPT_MAX_SIZE: |
670 | { | |
a8f307d8 | 671 | uint64_t val; |
57f272ed DG |
672 | const char *opt = poptGetOptArg(pc); |
673 | ||
a8f307d8 | 674 | if (utils_parse_size_suffix((char *) opt, &val) < 0) { |
57f272ed DG |
675 | ERR("Unable to handle max-size value %s", opt); |
676 | ret = CMD_ERROR; | |
677 | goto end; | |
678 | } | |
679 | ||
57f272ed DG |
680 | opt_max_size = val; |
681 | ||
682 | break; | |
683 | } | |
684 | default: | |
57f272ed DG |
685 | ret = CMD_UNDEFINED; |
686 | goto end; | |
687 | } | |
688 | } | |
689 | ||
690 | if (!opt_session_name) { | |
691 | session_name = get_session_name(); | |
692 | if (session_name == NULL) { | |
693 | ret = CMD_ERROR; | |
694 | goto end; | |
695 | } | |
696 | current_session_name = session_name; | |
697 | } else { | |
698 | current_session_name = opt_session_name; | |
699 | } | |
700 | ||
50534d6f | 701 | command_ret = handle_command(poptGetArgs(pc)); |
1ec06ed3 | 702 | if (command_ret) { |
50534d6f | 703 | switch (-command_ret) { |
7badf927 | 704 | case LTTNG_ERR_EPERM: |
6dc3064a | 705 | ERR("The session needs to be set in no output mode (--no-output)"); |
7badf927 DG |
706 | break; |
707 | case LTTNG_ERR_SNAPSHOT_NODATA: | |
50534d6f | 708 | WARN("%s", lttng_strerror(command_ret)); |
dad01b0a JR |
709 | |
710 | /* A warning is fine since the user has no control on | |
711 | * whether or not applications (or the kernel) have | |
712 | * produced any event between the start of the tracing | |
713 | * session and the recording of the snapshot. MI wise | |
714 | * the command is not a success since nothing was | |
715 | * recorded. | |
716 | */ | |
717 | command_ret = 0; | |
7badf927 DG |
718 | break; |
719 | default: | |
50534d6f | 720 | ERR("%s", lttng_strerror(command_ret)); |
7badf927 | 721 | break; |
6dc3064a | 722 | } |
50534d6f JRJ |
723 | success = 0; |
724 | } | |
725 | ||
726 | if (lttng_opt_mi) { | |
727 | /* Close output element */ | |
728 | ret = mi_lttng_writer_close_element(writer); | |
729 | if (ret) { | |
730 | ret = CMD_ERROR; | |
731 | goto end; | |
732 | } | |
733 | ||
734 | /* Success ? */ | |
735 | ret = mi_lttng_writer_write_element_bool(writer, | |
736 | mi_lttng_element_command_success, success); | |
737 | if (ret) { | |
738 | ret = CMD_ERROR; | |
739 | goto end; | |
740 | } | |
741 | ||
742 | /* Command element close */ | |
743 | ret = mi_lttng_writer_command_close(writer); | |
744 | if (ret) { | |
745 | ret = CMD_ERROR; | |
746 | goto end; | |
747 | } | |
57f272ed DG |
748 | } |
749 | ||
750 | end: | |
50534d6f JRJ |
751 | /* Mi clean-up */ |
752 | if (writer && mi_lttng_writer_destroy(writer)) { | |
753 | /* Preserve original error code */ | |
754 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
755 | } | |
756 | ||
57f272ed DG |
757 | if (!opt_session_name) { |
758 | free(session_name); | |
759 | } | |
50534d6f JRJ |
760 | |
761 | /* Overwrite ret if an error occured during handle_command */ | |
762 | ret = command_ret ? command_ret : ret; | |
57f272ed DG |
763 | poptFreeContext(pc); |
764 | return ret; | |
765 | } |