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