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) { | |
297 | ret = CMD_FATAL; | |
298 | goto error; | |
299 | } | |
300 | ||
301 | ret = mi_lttng_snapshot_del_output(writer, id, name, current_session_name); | |
302 | if (ret) { | |
303 | ret = CMD_ERROR; | |
304 | } | |
305 | ||
306 | error: | |
307 | lttng_snapshot_output_destroy(output); | |
308 | return ret; | |
309 | } | |
310 | ||
57f272ed DG |
311 | /* |
312 | * Delete output by ID. | |
313 | */ | |
eb240553 | 314 | static int del_output(uint32_t id, const char *name) |
57f272ed DG |
315 | { |
316 | int ret; | |
317 | struct lttng_snapshot_output *output = NULL; | |
318 | ||
319 | output = lttng_snapshot_output_create(); | |
320 | if (!output) { | |
321 | ret = CMD_FATAL; | |
322 | goto error; | |
323 | } | |
324 | ||
eb240553 DG |
325 | if (name) { |
326 | ret = lttng_snapshot_output_set_name(name, output); | |
327 | } else if (id != UINT32_MAX) { | |
328 | ret = lttng_snapshot_output_set_id(id, output); | |
329 | } else { | |
330 | ret = CMD_ERROR; | |
331 | goto error; | |
332 | } | |
57f272ed DG |
333 | if (ret < 0) { |
334 | ret = CMD_FATAL; | |
335 | goto error; | |
336 | } | |
337 | ||
338 | ret = lttng_snapshot_del_output(current_session_name, output); | |
339 | if (ret < 0) { | |
340 | goto error; | |
341 | } | |
342 | ||
eb240553 DG |
343 | if (id != UINT32_MAX) { |
344 | MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s", | |
345 | id, current_session_name); | |
346 | } else { | |
347 | MSG("Snapshot output %s successfully deleted for session %s", | |
348 | name, current_session_name); | |
349 | } | |
57f272ed DG |
350 | |
351 | error: | |
352 | lttng_snapshot_output_destroy(output); | |
353 | return ret; | |
354 | } | |
355 | ||
50534d6f JRJ |
356 | /* |
357 | * Add output from the user URL (machine interface). | |
358 | */ | |
359 | static int mi_add_output(const char *url) | |
360 | { | |
361 | int ret; | |
362 | struct lttng_snapshot_output *output = NULL; | |
363 | char name[NAME_MAX]; | |
364 | const char *n_ptr; | |
365 | ||
366 | if (!url && (!opt_data_url || !opt_ctrl_url)) { | |
367 | ret = CMD_ERROR; | |
368 | goto error; | |
369 | } | |
370 | ||
371 | output = create_output_from_args(url); | |
372 | if (!output) { | |
373 | ret = CMD_FATAL; | |
374 | goto error; | |
375 | } | |
376 | ||
377 | /* This call, if successful, populates the id of the output object. */ | |
378 | ret = lttng_snapshot_add_output(current_session_name, output); | |
379 | if (ret < 0) { | |
380 | ret = CMD_ERROR; | |
381 | goto error; | |
382 | } | |
383 | ||
384 | n_ptr = lttng_snapshot_output_get_name(output); | |
385 | if (*n_ptr == '\0') { | |
386 | int pret; | |
387 | pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32, | |
388 | lttng_snapshot_output_get_id(output)); | |
389 | if (pret < 0) { | |
390 | PERROR("snprintf add output name"); | |
391 | } | |
392 | n_ptr = name; | |
393 | } | |
394 | ||
395 | ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr, | |
396 | output); | |
397 | if (ret) { | |
398 | ret = CMD_ERROR; | |
399 | } | |
400 | ||
401 | error: | |
402 | lttng_snapshot_output_destroy(output); | |
403 | return ret; | |
404 | } | |
405 | ||
57f272ed DG |
406 | /* |
407 | * Add output from the user URL. | |
408 | */ | |
409 | static int add_output(const char *url) | |
410 | { | |
411 | int ret; | |
412 | struct lttng_snapshot_output *output = NULL; | |
6efe784e DG |
413 | char name[NAME_MAX]; |
414 | const char *n_ptr; | |
57f272ed DG |
415 | |
416 | if (!url && (!opt_data_url || !opt_ctrl_url)) { | |
417 | ret = CMD_ERROR; | |
418 | goto error; | |
419 | } | |
420 | ||
421 | output = create_output_from_args(url); | |
422 | if (!output) { | |
423 | ret = CMD_FATAL; | |
424 | goto error; | |
425 | } | |
426 | ||
427 | /* This call, if successful, populates the id of the output object. */ | |
428 | ret = lttng_snapshot_add_output(current_session_name, output); | |
429 | if (ret < 0) { | |
430 | goto error; | |
431 | } | |
432 | ||
6efe784e DG |
433 | n_ptr = lttng_snapshot_output_get_name(output); |
434 | if (*n_ptr == '\0') { | |
435 | int pret; | |
436 | pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32, | |
437 | lttng_snapshot_output_get_id(output)); | |
438 | if (pret < 0) { | |
439 | PERROR("snprintf add output name"); | |
440 | } | |
441 | n_ptr = name; | |
442 | } | |
443 | ||
57f272ed DG |
444 | MSG("Snapshot output successfully added for session %s", |
445 | current_session_name); | |
446 | MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", | |
6efe784e | 447 | lttng_snapshot_output_get_id(output), n_ptr, |
57f272ed DG |
448 | lttng_snapshot_output_get_ctrl_url(output), |
449 | lttng_snapshot_output_get_maxsize(output)); | |
450 | error: | |
451 | lttng_snapshot_output_destroy(output); | |
452 | return ret; | |
453 | } | |
454 | ||
455 | static int cmd_add_output(int argc, const char **argv) | |
456 | { | |
50534d6f | 457 | int ret; |
57f272ed DG |
458 | |
459 | if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) { | |
460 | usage(stderr); | |
461 | ret = CMD_ERROR; | |
462 | goto end; | |
463 | } | |
464 | ||
50534d6f JRJ |
465 | if (lttng_opt_mi) { |
466 | ret = mi_add_output(argv[1]); | |
467 | } else { | |
468 | ret = add_output(argv[1]); | |
469 | } | |
57f272ed DG |
470 | |
471 | end: | |
472 | return ret; | |
473 | } | |
474 | ||
475 | static int cmd_del_output(int argc, const char **argv) | |
476 | { | |
50534d6f | 477 | int ret; |
eb240553 DG |
478 | char *name; |
479 | long id; | |
57f272ed DG |
480 | |
481 | if (argc < 2) { | |
482 | usage(stderr); | |
483 | ret = CMD_ERROR; | |
484 | goto end; | |
485 | } | |
486 | ||
eb240553 DG |
487 | errno = 0; |
488 | id = strtol(argv[1], &name, 10); | |
489 | if (id == 0 && errno == 0) { | |
50534d6f JRJ |
490 | if (lttng_opt_mi) { |
491 | ret = mi_del_output(UINT32_MAX, name); | |
492 | } else { | |
493 | ret = del_output(UINT32_MAX, name); | |
494 | } | |
eb240553 | 495 | } else if (errno == 0 && *name == '\0') { |
50534d6f JRJ |
496 | if (lttng_opt_mi) { |
497 | ret = mi_del_output(id, NULL); | |
498 | } else { | |
499 | ret = del_output(id, NULL); | |
500 | } | |
eb240553 DG |
501 | } else { |
502 | ERR("Argument %s not recognized", argv[1]); | |
503 | ret = -1; | |
504 | goto end; | |
505 | } | |
57f272ed DG |
506 | |
507 | end: | |
508 | return ret; | |
509 | } | |
510 | ||
511 | static int cmd_list_output(int argc, const char **argv) | |
512 | { | |
50534d6f JRJ |
513 | int ret; |
514 | ||
515 | if (lttng_opt_mi) { | |
516 | ret = mi_list_output(); | |
517 | } else { | |
518 | ret = list_output(); | |
519 | } | |
520 | ||
521 | return ret; | |
522 | } | |
523 | ||
524 | /* | |
525 | * Do a snapshot record with the URL if one is given (machine interface). | |
526 | */ | |
527 | static int mi_record(const char *url) | |
528 | { | |
529 | int ret; | |
530 | struct lttng_snapshot_output *output = NULL; | |
531 | ||
532 | output = create_output_from_args(url); | |
533 | if (!output) { | |
534 | ret = CMD_FATAL; | |
535 | goto error; | |
536 | } | |
537 | ||
538 | ret = lttng_snapshot_record(current_session_name, output, 0); | |
539 | if (ret < 0) { | |
540 | ret = CMD_ERROR; | |
541 | goto error; | |
542 | } | |
543 | ||
544 | ret = mi_lttng_snapshot_record(writer, current_session_name, url, | |
545 | opt_ctrl_url, opt_data_url); | |
546 | if (ret) { | |
547 | ret = CMD_ERROR; | |
548 | } | |
549 | ||
550 | error: | |
551 | lttng_snapshot_output_destroy(output); | |
552 | return ret; | |
57f272ed DG |
553 | } |
554 | ||
555 | /* | |
556 | * Do a snapshot record with the URL if one is given. | |
557 | */ | |
558 | static int record(const char *url) | |
559 | { | |
560 | int ret; | |
561 | struct lttng_snapshot_output *output = NULL; | |
562 | ||
e1986656 DG |
563 | output = create_output_from_args(url); |
564 | if (!output) { | |
565 | ret = CMD_FATAL; | |
566 | goto error; | |
57f272ed DG |
567 | } |
568 | ||
569 | ret = lttng_snapshot_record(current_session_name, output, 0); | |
570 | if (ret < 0) { | |
68808f4e DG |
571 | if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) { |
572 | ERR("The minimum size of a snapshot is computed by multiplying " | |
573 | "the total amount of streams with the largest subbuffer " | |
574 | "in the session."); | |
575 | } | |
57f272ed DG |
576 | goto error; |
577 | } | |
578 | ||
579 | MSG("Snapshot recorded successfully for session %s", current_session_name); | |
580 | ||
581 | if (url) { | |
582 | MSG("Snapshot written at: %s", url); | |
583 | } else if (opt_ctrl_url) { | |
584 | MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url, | |
585 | opt_data_url); | |
57f272ed DG |
586 | } |
587 | ||
588 | error: | |
cdcdb9dd | 589 | lttng_snapshot_output_destroy(output); |
57f272ed DG |
590 | return ret; |
591 | } | |
592 | ||
593 | static int cmd_record(int argc, const char **argv) | |
594 | { | |
595 | int ret; | |
596 | ||
597 | if (argc == 2) { | |
598 | /* With a given URL */ | |
50534d6f JRJ |
599 | if (lttng_opt_mi) { |
600 | ret = mi_record(argv[1]); | |
601 | } else { | |
602 | ret = record(argv[1]); | |
603 | } | |
57f272ed | 604 | } else { |
50534d6f JRJ |
605 | if (lttng_opt_mi) { |
606 | ret = mi_record(NULL); | |
607 | } else { | |
608 | ret = record(NULL); | |
609 | } | |
57f272ed DG |
610 | } |
611 | ||
612 | return ret; | |
613 | } | |
614 | ||
615 | static int handle_command(const char **argv) | |
616 | { | |
50534d6f | 617 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; |
57f272ed DG |
618 | struct cmd_struct *cmd; |
619 | ||
620 | if (argv == NULL || (!opt_ctrl_url && opt_data_url) || | |
621 | (opt_ctrl_url && !opt_data_url)) { | |
622 | usage(stderr); | |
50534d6f | 623 | command_ret = CMD_ERROR; |
57f272ed DG |
624 | goto end; |
625 | } | |
626 | ||
627 | argc = count_arguments(argv); | |
628 | ||
629 | cmd = &actions[i]; | |
630 | while (cmd->func != NULL) { | |
631 | /* Find command */ | |
632 | if (strcmp(argv[0], cmd->name) == 0) { | |
50534d6f JRJ |
633 | if (lttng_opt_mi) { |
634 | /* Action element */ | |
635 | ret = mi_lttng_writer_open_element(writer, | |
636 | mi_lttng_element_command_action); | |
637 | if (ret) { | |
638 | ret = CMD_ERROR; | |
639 | goto end; | |
640 | } | |
641 | ||
642 | /* Name of the action */ | |
643 | ret = mi_lttng_writer_write_element_string(writer, | |
644 | config_element_name, argv[0]); | |
645 | if (ret) { | |
646 | ret = CMD_ERROR; | |
647 | goto end; | |
648 | } | |
649 | ||
650 | /* Open output element */ | |
651 | ret = mi_lttng_writer_open_element(writer, | |
652 | mi_lttng_element_command_output); | |
653 | if (ret) { | |
654 | ret = CMD_ERROR; | |
655 | goto end; | |
656 | } | |
657 | } | |
658 | ||
659 | command_ret = cmd->func(argc, argv); | |
660 | ||
661 | if (lttng_opt_mi) { | |
662 | /* Close output and action element */ | |
663 | ret = mi_lttng_close_multi_element(writer, 2); | |
664 | if (ret) { | |
665 | ret = CMD_ERROR; | |
666 | goto end; | |
667 | } | |
668 | } | |
57f272ed DG |
669 | goto end; |
670 | } | |
671 | i++; | |
672 | cmd = &actions[i]; | |
673 | } | |
674 | ||
50534d6f | 675 | ret = -CMD_UNDEFINED; |
57f272ed DG |
676 | |
677 | end: | |
50534d6f JRJ |
678 | /* Overwrite ret if an error occured in cmd->func() */ |
679 | ret = command_ret ? command_ret : ret; | |
57f272ed DG |
680 | return ret; |
681 | } | |
57f272ed DG |
682 | /* |
683 | * The 'snapshot <cmd> <options>' first level command | |
684 | */ | |
685 | int cmd_snapshot(int argc, const char **argv) | |
686 | { | |
50534d6f | 687 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
57f272ed DG |
688 | char *session_name = NULL; |
689 | static poptContext pc; | |
690 | ||
691 | pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0); | |
692 | poptReadDefaultConfig(pc, 0); | |
693 | ||
50534d6f | 694 | /* Mi check */ |
c7e35b03 | 695 | if (lttng_opt_mi) { |
50534d6f JRJ |
696 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
697 | if (!writer) { | |
698 | ret = -LTTNG_ERR_NOMEM; | |
699 | goto end; | |
700 | } | |
701 | ||
702 | /* Open command element */ | |
703 | ret = mi_lttng_writer_command_open(writer, | |
704 | mi_lttng_element_command_snapshot); | |
705 | if (ret) { | |
706 | ret = CMD_ERROR; | |
707 | goto end; | |
708 | } | |
709 | ||
710 | /* Open output element */ | |
711 | ret = mi_lttng_writer_open_element(writer, | |
712 | mi_lttng_element_command_output); | |
713 | if (ret) { | |
714 | ret = CMD_ERROR; | |
715 | goto end; | |
716 | } | |
c7e35b03 JR |
717 | } |
718 | ||
57f272ed DG |
719 | while ((opt = poptGetNextOpt(pc)) != -1) { |
720 | switch (opt) { | |
721 | case OPT_HELP: | |
722 | usage(stdout); | |
723 | goto end; | |
724 | case OPT_LIST_OPTIONS: | |
725 | list_cmd_options(stdout, snapshot_opts); | |
726 | goto end; | |
3c9bd23c SM |
727 | case OPT_LIST_COMMANDS: |
728 | list_commands(actions, stdout); | |
729 | goto end; | |
57f272ed DG |
730 | case OPT_MAX_SIZE: |
731 | { | |
a8f307d8 | 732 | uint64_t val; |
57f272ed DG |
733 | const char *opt = poptGetOptArg(pc); |
734 | ||
a8f307d8 | 735 | if (utils_parse_size_suffix((char *) opt, &val) < 0) { |
57f272ed DG |
736 | ERR("Unable to handle max-size value %s", opt); |
737 | ret = CMD_ERROR; | |
738 | goto end; | |
739 | } | |
740 | ||
57f272ed DG |
741 | opt_max_size = val; |
742 | ||
743 | break; | |
744 | } | |
745 | default: | |
746 | usage(stderr); | |
747 | ret = CMD_UNDEFINED; | |
748 | goto end; | |
749 | } | |
750 | } | |
751 | ||
752 | if (!opt_session_name) { | |
753 | session_name = get_session_name(); | |
754 | if (session_name == NULL) { | |
755 | ret = CMD_ERROR; | |
756 | goto end; | |
757 | } | |
758 | current_session_name = session_name; | |
759 | } else { | |
760 | current_session_name = opt_session_name; | |
761 | } | |
762 | ||
50534d6f JRJ |
763 | command_ret = handle_command(poptGetArgs(pc)); |
764 | if (command_ret < 0) { | |
765 | switch (-command_ret) { | |
7badf927 | 766 | case LTTNG_ERR_EPERM: |
6dc3064a | 767 | ERR("The session needs to be set in no output mode (--no-output)"); |
7badf927 DG |
768 | break; |
769 | case LTTNG_ERR_SNAPSHOT_NODATA: | |
50534d6f | 770 | WARN("%s", lttng_strerror(command_ret)); |
7badf927 DG |
771 | break; |
772 | default: | |
50534d6f | 773 | ERR("%s", lttng_strerror(command_ret)); |
7badf927 | 774 | break; |
6dc3064a | 775 | } |
50534d6f JRJ |
776 | success = 0; |
777 | } | |
778 | ||
779 | if (lttng_opt_mi) { | |
780 | /* Close output element */ | |
781 | ret = mi_lttng_writer_close_element(writer); | |
782 | if (ret) { | |
783 | ret = CMD_ERROR; | |
784 | goto end; | |
785 | } | |
786 | ||
787 | /* Success ? */ | |
788 | ret = mi_lttng_writer_write_element_bool(writer, | |
789 | mi_lttng_element_command_success, success); | |
790 | if (ret) { | |
791 | ret = CMD_ERROR; | |
792 | goto end; | |
793 | } | |
794 | ||
795 | /* Command element close */ | |
796 | ret = mi_lttng_writer_command_close(writer); | |
797 | if (ret) { | |
798 | ret = CMD_ERROR; | |
799 | goto end; | |
800 | } | |
57f272ed DG |
801 | } |
802 | ||
803 | end: | |
50534d6f JRJ |
804 | /* Mi clean-up */ |
805 | if (writer && mi_lttng_writer_destroy(writer)) { | |
806 | /* Preserve original error code */ | |
807 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
808 | } | |
809 | ||
57f272ed DG |
810 | if (!opt_session_name) { |
811 | free(session_name); | |
812 | } | |
50534d6f JRJ |
813 | |
814 | /* Overwrite ret if an error occured during handle_command */ | |
815 | ret = command_ret ? command_ret : ret; | |
57f272ed DG |
816 | poptFreeContext(pc); |
817 | return ret; | |
818 | } |