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 | ||
18 | #define _GNU_SOURCE | |
19 | #include <assert.h> | |
20 | #include <inttypes.h> | |
21 | #include <popt.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
27 | #include <unistd.h> | |
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 | ||
81 | /* | |
82 | * usage | |
83 | */ | |
84 | static void usage(FILE *ofp) | |
85 | { | |
8df3bfe9 | 86 | fprintf(ofp, "usage: lttng snapshot [OPTION] ACTION\n"); |
57f272ed DG |
87 | fprintf(ofp, "\n"); |
88 | fprintf(ofp, "Actions:\n"); | |
89 | fprintf(ofp, " add-output [-m <SIZE>] [-s <NAME>] [-n <NAME>] <URL> | -C <URL> -D <URL>\n"); | |
90 | fprintf(ofp, " Setup and add an snapshot output for a session.\n"); | |
91 | fprintf(ofp, "\n"); | |
e0dcb8bf | 92 | fprintf(ofp, " del-output ID | NAME [-s <NAME>]\n"); |
57f272ed DG |
93 | fprintf(ofp, " Delete an output for a session using the ID.\n"); |
94 | fprintf(ofp, "\n"); | |
95 | fprintf(ofp, " list-output [-s <NAME>]\n"); | |
96 | fprintf(ofp, " List the output of a session.\n"); | |
97 | fprintf(ofp, "\n"); | |
98 | fprintf(ofp, " record [-m <SIZE>] [-s <NAME>] [-n <NAME>] [<URL> | -C <URL> -D <URL>]\n"); | |
99 | fprintf(ofp, " Snapshot a session's buffer(s) for all domains. If an URL is\n"); | |
100 | fprintf(ofp, " specified, it is used instead of a previously added output.\n"); | |
e0dcb8bf DG |
101 | fprintf(ofp, " Specifying only a name or/a size will override the current output value.\n"); |
102 | fprintf(ofp, " For instance, you can record a snapshot with a custom maximum size\n"); | |
103 | fprintf(ofp, " or with a different name.\n"); | |
57f272ed DG |
104 | fprintf(ofp, "\n"); |
105 | fprintf(ofp, "Options:\n"); | |
106 | fprintf(ofp, " -h, --help Show this help\n"); | |
107 | fprintf(ofp, " --list-options Simple listing of options\n"); | |
108 | fprintf(ofp, " -s, --session NAME Apply to session name\n"); | |
109 | fprintf(ofp, " -n, --name NAME Name of the output or snapshot\n"); | |
a8f307d8 | 110 | fprintf(ofp, " -m, --max-size SIZE Maximum bytes size of the snapshot {+k,+M,+G}\n"); |
57f272ed DG |
111 | fprintf(ofp, " -C, --ctrl-url URL Set control path URL. (Must use -D also)\n"); |
112 | fprintf(ofp, " -D, --data-url URL Set data path URL. (Must use -C also)\n"); | |
113 | fprintf(ofp, "\n"); | |
114 | } | |
115 | ||
116 | /* | |
117 | * Count and return the number of arguments in argv. | |
118 | */ | |
119 | static int count_arguments(const char **argv) | |
120 | { | |
121 | int i = 0; | |
122 | ||
123 | assert(argv); | |
124 | ||
125 | while (argv[i] != NULL) { | |
126 | i++; | |
127 | } | |
128 | ||
129 | return i; | |
130 | } | |
131 | ||
132 | /* | |
133 | * Create a snapshot output object from arguments using the given URL. | |
134 | * | |
135 | * Return a newly allocated object or NULL on error. | |
136 | */ | |
137 | static struct lttng_snapshot_output *create_output_from_args(const char *url) | |
138 | { | |
139 | int ret = 0; | |
140 | struct lttng_snapshot_output *output = NULL; | |
141 | ||
142 | output = lttng_snapshot_output_create(); | |
143 | if (!output) { | |
144 | goto error_create; | |
145 | } | |
146 | ||
147 | if (url) { | |
148 | ret = lttng_snapshot_output_set_ctrl_url(url, output); | |
149 | if (ret < 0) { | |
150 | goto error; | |
151 | } | |
152 | } else if (opt_ctrl_url) { | |
153 | ret = lttng_snapshot_output_set_ctrl_url(opt_ctrl_url, output); | |
154 | if (ret < 0) { | |
155 | goto error; | |
156 | } | |
157 | } | |
158 | ||
159 | if (opt_data_url) { | |
160 | ret = lttng_snapshot_output_set_data_url(opt_data_url, output); | |
161 | if (ret < 0) { | |
162 | goto error; | |
163 | } | |
164 | } | |
165 | ||
166 | if (opt_max_size) { | |
167 | ret = lttng_snapshot_output_set_size(opt_max_size, output); | |
168 | if (ret < 0) { | |
169 | goto error; | |
170 | } | |
171 | } | |
172 | ||
173 | if (opt_output_name) { | |
174 | ret = lttng_snapshot_output_set_name(opt_output_name, output); | |
175 | if (ret < 0) { | |
176 | goto error; | |
177 | } | |
178 | } | |
179 | ||
180 | return output; | |
181 | ||
182 | error: | |
183 | lttng_snapshot_output_destroy(output); | |
184 | error_create: | |
185 | return NULL; | |
186 | } | |
187 | ||
50534d6f JRJ |
188 | static int mi_list_output(void) |
189 | { | |
190 | int ret; | |
191 | struct lttng_snapshot_output *s_iter; | |
192 | struct lttng_snapshot_output_list *list; | |
193 | ||
194 | assert(writer); | |
195 | ||
196 | ret = lttng_snapshot_list_output(current_session_name, &list); | |
197 | if (ret < 0) { | |
198 | goto error; | |
199 | } | |
200 | ||
201 | ret = mi_lttng_snapshot_output_session_name(writer, current_session_name); | |
202 | if (ret) { | |
203 | ret = CMD_ERROR; | |
204 | goto end; | |
205 | } | |
206 | ||
207 | while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) { | |
208 | ret = mi_lttng_snapshot_list_output(writer, s_iter); | |
209 | if (ret) { | |
210 | ret = CMD_ERROR; | |
211 | goto end; | |
212 | } | |
213 | } | |
214 | ||
215 | ||
216 | /* Close snapshot snapshots element */ | |
217 | ret = mi_lttng_writer_close_element(writer); | |
218 | if (ret) { | |
219 | ret = CMD_ERROR; | |
220 | goto end; | |
221 | } | |
222 | ||
223 | /* Close snapshot session element */ | |
224 | ret = mi_lttng_writer_close_element(writer); | |
225 | if (ret) { | |
226 | ret = CMD_ERROR; | |
227 | } | |
228 | end: | |
229 | lttng_snapshot_output_list_destroy(list); | |
230 | error: | |
231 | return ret; | |
232 | } | |
233 | ||
57f272ed DG |
234 | static int list_output(void) |
235 | { | |
236 | int ret, output_seen = 0; | |
237 | struct lttng_snapshot_output *s_iter; | |
238 | struct lttng_snapshot_output_list *list; | |
239 | ||
240 | ret = lttng_snapshot_list_output(current_session_name, &list); | |
241 | if (ret < 0) { | |
242 | goto error; | |
243 | } | |
244 | ||
245 | MSG("Snapshot output list for session %s", current_session_name); | |
246 | ||
247 | while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) { | |
c3024823 | 248 | MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4, |
57f272ed DG |
249 | lttng_snapshot_output_get_id(s_iter), |
250 | lttng_snapshot_output_get_name(s_iter), | |
c3024823 DG |
251 | lttng_snapshot_output_get_ctrl_url(s_iter), |
252 | lttng_snapshot_output_get_maxsize(s_iter)); | |
57f272ed DG |
253 | output_seen = 1; |
254 | } | |
255 | ||
256 | lttng_snapshot_output_list_destroy(list); | |
257 | ||
258 | if (!output_seen) { | |
259 | MSG("%sNone", indent4); | |
260 | } | |
261 | ||
262 | error: | |
263 | return ret; | |
264 | } | |
265 | ||
50534d6f JRJ |
266 | /* |
267 | * Delete output by ID (machine interface version). | |
268 | */ | |
269 | static int mi_del_output(uint32_t id, const char *name) | |
270 | { | |
271 | int ret; | |
272 | struct lttng_snapshot_output *output = NULL; | |
273 | ||
274 | assert(writer); | |
275 | ||
276 | output = lttng_snapshot_output_create(); | |
277 | if (!output) { | |
278 | ret = CMD_FATAL; | |
279 | goto error; | |
280 | } | |
281 | ||
282 | if (name) { | |
283 | ret = lttng_snapshot_output_set_name(name, output); | |
284 | } else if (id != UINT32_MAX) { | |
285 | ret = lttng_snapshot_output_set_id(id, output); | |
286 | } else { | |
287 | ret = CMD_ERROR; | |
288 | goto error; | |
289 | } | |
290 | if (ret < 0) { | |
291 | ret = CMD_FATAL; | |
292 | goto error; | |
293 | } | |
294 | ||
295 | ret = lttng_snapshot_del_output(current_session_name, output); | |
296 | if (ret < 0) { | |
50534d6f JRJ |
297 | goto error; |
298 | } | |
299 | ||
300 | ret = mi_lttng_snapshot_del_output(writer, id, name, current_session_name); | |
301 | if (ret) { | |
302 | ret = CMD_ERROR; | |
303 | } | |
304 | ||
305 | error: | |
306 | lttng_snapshot_output_destroy(output); | |
307 | return ret; | |
308 | } | |
309 | ||
57f272ed DG |
310 | /* |
311 | * Delete output by ID. | |
312 | */ | |
eb240553 | 313 | static int del_output(uint32_t id, const char *name) |
57f272ed DG |
314 | { |
315 | int ret; | |
316 | struct lttng_snapshot_output *output = NULL; | |
317 | ||
318 | output = lttng_snapshot_output_create(); | |
319 | if (!output) { | |
320 | ret = CMD_FATAL; | |
321 | goto error; | |
322 | } | |
323 | ||
eb240553 DG |
324 | if (name) { |
325 | ret = lttng_snapshot_output_set_name(name, output); | |
326 | } else if (id != UINT32_MAX) { | |
327 | ret = lttng_snapshot_output_set_id(id, output); | |
328 | } else { | |
329 | ret = CMD_ERROR; | |
330 | goto error; | |
331 | } | |
57f272ed DG |
332 | if (ret < 0) { |
333 | ret = CMD_FATAL; | |
334 | goto error; | |
335 | } | |
336 | ||
337 | ret = lttng_snapshot_del_output(current_session_name, output); | |
338 | if (ret < 0) { | |
339 | goto error; | |
340 | } | |
341 | ||
eb240553 DG |
342 | if (id != UINT32_MAX) { |
343 | MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s", | |
344 | id, current_session_name); | |
345 | } else { | |
346 | MSG("Snapshot output %s successfully deleted for session %s", | |
347 | name, current_session_name); | |
348 | } | |
57f272ed DG |
349 | |
350 | error: | |
351 | lttng_snapshot_output_destroy(output); | |
352 | return ret; | |
353 | } | |
354 | ||
50534d6f JRJ |
355 | /* |
356 | * Add output from the user URL (machine interface). | |
357 | */ | |
358 | static int mi_add_output(const char *url) | |
359 | { | |
360 | int ret; | |
361 | struct lttng_snapshot_output *output = NULL; | |
362 | char name[NAME_MAX]; | |
363 | const char *n_ptr; | |
364 | ||
365 | if (!url && (!opt_data_url || !opt_ctrl_url)) { | |
366 | ret = CMD_ERROR; | |
367 | goto error; | |
368 | } | |
369 | ||
370 | output = create_output_from_args(url); | |
371 | if (!output) { | |
372 | ret = CMD_FATAL; | |
373 | goto error; | |
374 | } | |
375 | ||
376 | /* This call, if successful, populates the id of the output object. */ | |
377 | ret = lttng_snapshot_add_output(current_session_name, output); | |
378 | if (ret < 0) { | |
50534d6f JRJ |
379 | goto error; |
380 | } | |
381 | ||
382 | n_ptr = lttng_snapshot_output_get_name(output); | |
383 | if (*n_ptr == '\0') { | |
384 | int pret; | |
385 | pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32, | |
386 | lttng_snapshot_output_get_id(output)); | |
387 | if (pret < 0) { | |
388 | PERROR("snprintf add output name"); | |
389 | } | |
390 | n_ptr = name; | |
391 | } | |
392 | ||
393 | ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr, | |
394 | output); | |
395 | if (ret) { | |
396 | ret = CMD_ERROR; | |
397 | } | |
398 | ||
399 | error: | |
400 | lttng_snapshot_output_destroy(output); | |
401 | return ret; | |
402 | } | |
403 | ||
57f272ed DG |
404 | /* |
405 | * Add output from the user URL. | |
406 | */ | |
407 | static int add_output(const char *url) | |
408 | { | |
409 | int ret; | |
410 | struct lttng_snapshot_output *output = NULL; | |
6efe784e DG |
411 | char name[NAME_MAX]; |
412 | const char *n_ptr; | |
57f272ed DG |
413 | |
414 | if (!url && (!opt_data_url || !opt_ctrl_url)) { | |
415 | ret = CMD_ERROR; | |
416 | goto error; | |
417 | } | |
418 | ||
419 | output = create_output_from_args(url); | |
420 | if (!output) { | |
421 | ret = CMD_FATAL; | |
422 | goto error; | |
423 | } | |
424 | ||
425 | /* This call, if successful, populates the id of the output object. */ | |
426 | ret = lttng_snapshot_add_output(current_session_name, output); | |
427 | if (ret < 0) { | |
428 | goto error; | |
429 | } | |
430 | ||
6efe784e DG |
431 | n_ptr = lttng_snapshot_output_get_name(output); |
432 | if (*n_ptr == '\0') { | |
433 | int pret; | |
434 | pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32, | |
435 | lttng_snapshot_output_get_id(output)); | |
436 | if (pret < 0) { | |
437 | PERROR("snprintf add output name"); | |
438 | } | |
439 | n_ptr = name; | |
440 | } | |
441 | ||
57f272ed DG |
442 | MSG("Snapshot output successfully added for session %s", |
443 | current_session_name); | |
444 | MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", | |
6efe784e | 445 | lttng_snapshot_output_get_id(output), n_ptr, |
57f272ed DG |
446 | lttng_snapshot_output_get_ctrl_url(output), |
447 | lttng_snapshot_output_get_maxsize(output)); | |
448 | error: | |
449 | lttng_snapshot_output_destroy(output); | |
450 | return ret; | |
451 | } | |
452 | ||
453 | static int cmd_add_output(int argc, const char **argv) | |
454 | { | |
50534d6f | 455 | int ret; |
57f272ed DG |
456 | |
457 | if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) { | |
458 | usage(stderr); | |
459 | ret = CMD_ERROR; | |
460 | goto end; | |
461 | } | |
462 | ||
50534d6f JRJ |
463 | if (lttng_opt_mi) { |
464 | ret = mi_add_output(argv[1]); | |
465 | } else { | |
466 | ret = add_output(argv[1]); | |
467 | } | |
57f272ed DG |
468 | |
469 | end: | |
470 | return ret; | |
471 | } | |
472 | ||
473 | static int cmd_del_output(int argc, const char **argv) | |
474 | { | |
50534d6f | 475 | int ret; |
eb240553 DG |
476 | char *name; |
477 | long id; | |
57f272ed DG |
478 | |
479 | if (argc < 2) { | |
480 | usage(stderr); | |
481 | ret = CMD_ERROR; | |
482 | goto end; | |
483 | } | |
484 | ||
eb240553 DG |
485 | errno = 0; |
486 | id = strtol(argv[1], &name, 10); | |
487 | if (id == 0 && errno == 0) { | |
50534d6f JRJ |
488 | if (lttng_opt_mi) { |
489 | ret = mi_del_output(UINT32_MAX, name); | |
490 | } else { | |
491 | ret = del_output(UINT32_MAX, name); | |
492 | } | |
eb240553 | 493 | } else if (errno == 0 && *name == '\0') { |
50534d6f JRJ |
494 | if (lttng_opt_mi) { |
495 | ret = mi_del_output(id, NULL); | |
496 | } else { | |
497 | ret = del_output(id, NULL); | |
498 | } | |
eb240553 DG |
499 | } else { |
500 | ERR("Argument %s not recognized", argv[1]); | |
501 | ret = -1; | |
502 | goto end; | |
503 | } | |
57f272ed DG |
504 | |
505 | end: | |
506 | return ret; | |
507 | } | |
508 | ||
509 | static int cmd_list_output(int argc, const char **argv) | |
510 | { | |
50534d6f JRJ |
511 | int ret; |
512 | ||
513 | if (lttng_opt_mi) { | |
514 | ret = mi_list_output(); | |
515 | } else { | |
516 | ret = list_output(); | |
517 | } | |
518 | ||
519 | return ret; | |
520 | } | |
521 | ||
522 | /* | |
523 | * Do a snapshot record with the URL if one is given (machine interface). | |
524 | */ | |
525 | static int mi_record(const char *url) | |
526 | { | |
527 | int ret; | |
528 | struct lttng_snapshot_output *output = NULL; | |
529 | ||
530 | output = create_output_from_args(url); | |
531 | if (!output) { | |
532 | ret = CMD_FATAL; | |
533 | goto error; | |
534 | } | |
535 | ||
536 | ret = lttng_snapshot_record(current_session_name, output, 0); | |
537 | if (ret < 0) { | |
538 | ret = CMD_ERROR; | |
539 | goto error; | |
540 | } | |
541 | ||
542 | ret = mi_lttng_snapshot_record(writer, current_session_name, url, | |
543 | opt_ctrl_url, opt_data_url); | |
544 | if (ret) { | |
545 | ret = CMD_ERROR; | |
546 | } | |
547 | ||
548 | error: | |
549 | lttng_snapshot_output_destroy(output); | |
550 | return ret; | |
57f272ed DG |
551 | } |
552 | ||
553 | /* | |
554 | * Do a snapshot record with the URL if one is given. | |
555 | */ | |
556 | static int record(const char *url) | |
557 | { | |
558 | int ret; | |
559 | struct lttng_snapshot_output *output = NULL; | |
560 | ||
e1986656 DG |
561 | output = create_output_from_args(url); |
562 | if (!output) { | |
563 | ret = CMD_FATAL; | |
564 | goto error; | |
57f272ed DG |
565 | } |
566 | ||
567 | ret = lttng_snapshot_record(current_session_name, output, 0); | |
568 | if (ret < 0) { | |
68808f4e DG |
569 | if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) { |
570 | ERR("The minimum size of a snapshot is computed by multiplying " | |
571 | "the total amount of streams with the largest subbuffer " | |
572 | "in the session."); | |
573 | } | |
57f272ed DG |
574 | goto error; |
575 | } | |
576 | ||
577 | MSG("Snapshot recorded successfully for session %s", current_session_name); | |
578 | ||
579 | if (url) { | |
580 | MSG("Snapshot written at: %s", url); | |
581 | } else if (opt_ctrl_url) { | |
582 | MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url, | |
583 | opt_data_url); | |
57f272ed DG |
584 | } |
585 | ||
586 | error: | |
cdcdb9dd | 587 | lttng_snapshot_output_destroy(output); |
57f272ed DG |
588 | return ret; |
589 | } | |
590 | ||
591 | static int cmd_record(int argc, const char **argv) | |
592 | { | |
593 | int ret; | |
594 | ||
595 | if (argc == 2) { | |
596 | /* With a given URL */ | |
50534d6f JRJ |
597 | if (lttng_opt_mi) { |
598 | ret = mi_record(argv[1]); | |
599 | } else { | |
600 | ret = record(argv[1]); | |
601 | } | |
57f272ed | 602 | } else { |
50534d6f JRJ |
603 | if (lttng_opt_mi) { |
604 | ret = mi_record(NULL); | |
605 | } else { | |
606 | ret = record(NULL); | |
607 | } | |
57f272ed DG |
608 | } |
609 | ||
610 | return ret; | |
611 | } | |
612 | ||
613 | static int handle_command(const char **argv) | |
614 | { | |
50534d6f | 615 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; |
57f272ed DG |
616 | struct cmd_struct *cmd; |
617 | ||
618 | if (argv == NULL || (!opt_ctrl_url && opt_data_url) || | |
619 | (opt_ctrl_url && !opt_data_url)) { | |
620 | usage(stderr); | |
50534d6f | 621 | command_ret = CMD_ERROR; |
57f272ed DG |
622 | goto end; |
623 | } | |
624 | ||
625 | argc = count_arguments(argv); | |
626 | ||
627 | cmd = &actions[i]; | |
628 | while (cmd->func != NULL) { | |
629 | /* Find command */ | |
630 | if (strcmp(argv[0], cmd->name) == 0) { | |
50534d6f JRJ |
631 | if (lttng_opt_mi) { |
632 | /* Action element */ | |
633 | ret = mi_lttng_writer_open_element(writer, | |
634 | mi_lttng_element_command_action); | |
635 | if (ret) { | |
636 | ret = CMD_ERROR; | |
637 | goto end; | |
638 | } | |
639 | ||
640 | /* Name of the action */ | |
641 | ret = mi_lttng_writer_write_element_string(writer, | |
642 | config_element_name, argv[0]); | |
643 | if (ret) { | |
644 | ret = CMD_ERROR; | |
645 | goto end; | |
646 | } | |
647 | ||
648 | /* Open output element */ | |
649 | ret = mi_lttng_writer_open_element(writer, | |
650 | mi_lttng_element_command_output); | |
651 | if (ret) { | |
652 | ret = CMD_ERROR; | |
653 | goto end; | |
654 | } | |
655 | } | |
656 | ||
657 | command_ret = cmd->func(argc, argv); | |
658 | ||
659 | if (lttng_opt_mi) { | |
660 | /* Close output and action element */ | |
661 | ret = mi_lttng_close_multi_element(writer, 2); | |
662 | if (ret) { | |
663 | ret = CMD_ERROR; | |
664 | goto end; | |
665 | } | |
666 | } | |
57f272ed DG |
667 | goto end; |
668 | } | |
669 | i++; | |
670 | cmd = &actions[i]; | |
671 | } | |
672 | ||
1ec06ed3 | 673 | ret = CMD_UNDEFINED; |
57f272ed DG |
674 | |
675 | end: | |
1ec06ed3 | 676 | /* Overwrite ret if an error occurred in cmd->func() */ |
50534d6f | 677 | ret = command_ret ? command_ret : ret; |
57f272ed DG |
678 | return ret; |
679 | } | |
57f272ed DG |
680 | /* |
681 | * The 'snapshot <cmd> <options>' first level command | |
682 | */ | |
683 | int cmd_snapshot(int argc, const char **argv) | |
684 | { | |
50534d6f | 685 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
57f272ed DG |
686 | char *session_name = NULL; |
687 | static poptContext pc; | |
688 | ||
689 | pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0); | |
690 | poptReadDefaultConfig(pc, 0); | |
691 | ||
50534d6f | 692 | /* Mi check */ |
c7e35b03 | 693 | if (lttng_opt_mi) { |
50534d6f JRJ |
694 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
695 | if (!writer) { | |
696 | ret = -LTTNG_ERR_NOMEM; | |
697 | goto end; | |
698 | } | |
699 | ||
700 | /* Open command element */ | |
701 | ret = mi_lttng_writer_command_open(writer, | |
702 | mi_lttng_element_command_snapshot); | |
703 | if (ret) { | |
704 | ret = CMD_ERROR; | |
705 | goto end; | |
706 | } | |
707 | ||
708 | /* Open output element */ | |
709 | ret = mi_lttng_writer_open_element(writer, | |
710 | mi_lttng_element_command_output); | |
711 | if (ret) { | |
712 | ret = CMD_ERROR; | |
713 | goto end; | |
714 | } | |
c7e35b03 JR |
715 | } |
716 | ||
57f272ed DG |
717 | while ((opt = poptGetNextOpt(pc)) != -1) { |
718 | switch (opt) { | |
719 | case OPT_HELP: | |
720 | usage(stdout); | |
721 | goto end; | |
722 | case OPT_LIST_OPTIONS: | |
723 | list_cmd_options(stdout, snapshot_opts); | |
724 | goto end; | |
3c9bd23c SM |
725 | case OPT_LIST_COMMANDS: |
726 | list_commands(actions, stdout); | |
727 | goto end; | |
57f272ed DG |
728 | case OPT_MAX_SIZE: |
729 | { | |
a8f307d8 | 730 | uint64_t val; |
57f272ed DG |
731 | const char *opt = poptGetOptArg(pc); |
732 | ||
a8f307d8 | 733 | if (utils_parse_size_suffix((char *) opt, &val) < 0) { |
57f272ed DG |
734 | ERR("Unable to handle max-size value %s", opt); |
735 | ret = CMD_ERROR; | |
736 | goto end; | |
737 | } | |
738 | ||
57f272ed DG |
739 | opt_max_size = val; |
740 | ||
741 | break; | |
742 | } | |
743 | default: | |
744 | usage(stderr); | |
745 | ret = CMD_UNDEFINED; | |
746 | goto end; | |
747 | } | |
748 | } | |
749 | ||
750 | if (!opt_session_name) { | |
751 | session_name = get_session_name(); | |
752 | if (session_name == NULL) { | |
753 | ret = CMD_ERROR; | |
754 | goto end; | |
755 | } | |
756 | current_session_name = session_name; | |
757 | } else { | |
758 | current_session_name = opt_session_name; | |
759 | } | |
760 | ||
50534d6f | 761 | command_ret = handle_command(poptGetArgs(pc)); |
1ec06ed3 | 762 | if (command_ret) { |
50534d6f | 763 | switch (-command_ret) { |
7badf927 | 764 | case LTTNG_ERR_EPERM: |
6dc3064a | 765 | ERR("The session needs to be set in no output mode (--no-output)"); |
7badf927 DG |
766 | break; |
767 | case LTTNG_ERR_SNAPSHOT_NODATA: | |
50534d6f | 768 | WARN("%s", lttng_strerror(command_ret)); |
7badf927 DG |
769 | break; |
770 | default: | |
50534d6f | 771 | ERR("%s", lttng_strerror(command_ret)); |
7badf927 | 772 | break; |
6dc3064a | 773 | } |
50534d6f JRJ |
774 | success = 0; |
775 | } | |
776 | ||
777 | if (lttng_opt_mi) { | |
778 | /* Close output element */ | |
779 | ret = mi_lttng_writer_close_element(writer); | |
780 | if (ret) { | |
781 | ret = CMD_ERROR; | |
782 | goto end; | |
783 | } | |
784 | ||
785 | /* Success ? */ | |
786 | ret = mi_lttng_writer_write_element_bool(writer, | |
787 | mi_lttng_element_command_success, success); | |
788 | if (ret) { | |
789 | ret = CMD_ERROR; | |
790 | goto end; | |
791 | } | |
792 | ||
793 | /* Command element close */ | |
794 | ret = mi_lttng_writer_command_close(writer); | |
795 | if (ret) { | |
796 | ret = CMD_ERROR; | |
797 | goto end; | |
798 | } | |
57f272ed DG |
799 | } |
800 | ||
801 | end: | |
50534d6f JRJ |
802 | /* Mi clean-up */ |
803 | if (writer && mi_lttng_writer_destroy(writer)) { | |
804 | /* Preserve original error code */ | |
805 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
806 | } | |
807 | ||
57f272ed DG |
808 | if (!opt_session_name) { |
809 | free(session_name); | |
810 | } | |
50534d6f JRJ |
811 | |
812 | /* Overwrite ret if an error occured during handle_command */ | |
813 | ret = command_ret ? command_ret : ret; | |
57f272ed DG |
814 | poptFreeContext(pc); |
815 | return ret; | |
816 | } |