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