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