Fix: lttng: memory leak in snapshot record command
[lttng-tools.git] / src / bin / lttng / commands / snapshot.c
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>
28
29 #include <lttng/snapshot.h>
30
31 #include "../command.h"
32
33 static const char *opt_session_name;
34 static const char *opt_output_name;
35 static const char *opt_data_url;
36 static const char *opt_ctrl_url;
37 static const char *current_session_name;
38 static uint64_t opt_max_size;
39
40 /* Stub for the cmd struct actions. */
41 static int cmd_add_output(int argc, const char **argv);
42 static int cmd_del_output(int argc, const char **argv);
43 static int cmd_list_output(int argc, const char **argv);
44 static int cmd_record(int argc, const char **argv);
45
46 static const char *indent4 = " ";
47
48 enum {
49 OPT_HELP = 1,
50 OPT_LIST_OPTIONS,
51 OPT_MAX_SIZE,
52 };
53
54 static struct poptOption snapshot_opts[] = {
55 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
56 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
57 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
58 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
59 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
60 {"name", 'n', POPT_ARG_STRING, &opt_output_name, 0, 0, 0},
61 {"max-size", 'm', POPT_ARG_DOUBLE, 0, OPT_MAX_SIZE, 0, 0},
62 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
63 {0, 0, 0, 0, 0, 0, 0}
64 };
65
66 static struct cmd_struct actions[] = {
67 { "add-output", cmd_add_output },
68 { "del-output", cmd_del_output },
69 { "list-output", cmd_list_output },
70 { "record", cmd_record },
71 { NULL, NULL } /* Array closure */
72 };
73
74 /*
75 * usage
76 */
77 static void usage(FILE *ofp)
78 {
79 fprintf(ofp, "usage: lttng snapshot [ACTION]\n");
80 fprintf(ofp, "\n");
81 fprintf(ofp, "Actions:\n");
82 fprintf(ofp, " add-output [-m <SIZE>] [-s <NAME>] [-n <NAME>] <URL> | -C <URL> -D <URL>\n");
83 fprintf(ofp, " Setup and add an snapshot output for a session.\n");
84 fprintf(ofp, "\n");
85 fprintf(ofp, " del-output ID [-s <NAME>]\n");
86 fprintf(ofp, " Delete an output for a session using the ID.\n");
87 fprintf(ofp, "\n");
88 fprintf(ofp, " list-output [-s <NAME>]\n");
89 fprintf(ofp, " List the output of a session.\n");
90 fprintf(ofp, "\n");
91 fprintf(ofp, " record [-m <SIZE>] [-s <NAME>] [-n <NAME>] [<URL> | -C <URL> -D <URL>]\n");
92 fprintf(ofp, " Snapshot a session's buffer(s) for all domains. If an URL is\n");
93 fprintf(ofp, " specified, it is used instead of a previously added output.\n");
94 fprintf(ofp, " The snapshot is saved in the session directory in snapshot/ with\n");
95 fprintf(ofp, " the top directory being NAME or the default: snapshot-ID/\n");
96 fprintf(ofp, "\n");
97 fprintf(ofp, "Options:\n");
98 fprintf(ofp, " -h, --help Show this help\n");
99 fprintf(ofp, " --list-options Simple listing of options\n");
100 fprintf(ofp, " -s, --session NAME Apply to session name\n");
101 fprintf(ofp, " -n, --name NAME Name of the output or snapshot\n");
102 fprintf(ofp, " -m, --max-size SIZE Maximum bytes size of the snapshot\n");
103 fprintf(ofp, " -C, --ctrl-url URL Set control path URL. (Must use -D also)\n");
104 fprintf(ofp, " -D, --data-url URL Set data path URL. (Must use -C also)\n");
105 fprintf(ofp, "\n");
106 }
107
108 /*
109 * Count and return the number of arguments in argv.
110 */
111 static int count_arguments(const char **argv)
112 {
113 int i = 0;
114
115 assert(argv);
116
117 while (argv[i] != NULL) {
118 i++;
119 }
120
121 return i;
122 }
123
124 /*
125 * Create a snapshot output object from arguments using the given URL.
126 *
127 * Return a newly allocated object or NULL on error.
128 */
129 static struct lttng_snapshot_output *create_output_from_args(const char *url)
130 {
131 int ret = 0;
132 struct lttng_snapshot_output *output = NULL;
133
134 output = lttng_snapshot_output_create();
135 if (!output) {
136 goto error_create;
137 }
138
139 if (url) {
140 ret = lttng_snapshot_output_set_ctrl_url(url, output);
141 if (ret < 0) {
142 goto error;
143 }
144 } else if (opt_ctrl_url) {
145 ret = lttng_snapshot_output_set_ctrl_url(opt_ctrl_url, output);
146 if (ret < 0) {
147 goto error;
148 }
149 }
150
151 if (opt_data_url) {
152 ret = lttng_snapshot_output_set_data_url(opt_data_url, output);
153 if (ret < 0) {
154 goto error;
155 }
156 }
157
158 if (opt_max_size) {
159 ret = lttng_snapshot_output_set_size(opt_max_size, output);
160 if (ret < 0) {
161 goto error;
162 }
163 }
164
165 if (opt_output_name) {
166 ret = lttng_snapshot_output_set_name(opt_output_name, output);
167 if (ret < 0) {
168 goto error;
169 }
170 }
171
172 return output;
173
174 error:
175 lttng_snapshot_output_destroy(output);
176 error_create:
177 return NULL;
178 }
179
180 static int list_output(void)
181 {
182 int ret, output_seen = 0;
183 struct lttng_snapshot_output *s_iter;
184 struct lttng_snapshot_output_list *list;
185
186 ret = lttng_snapshot_list_output(current_session_name, &list);
187 if (ret < 0) {
188 goto error;
189 }
190
191 MSG("Snapshot output list for session %s", current_session_name);
192
193 while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) {
194 MSG("%s[%" PRIu32 "] %s: %s", indent4,
195 lttng_snapshot_output_get_id(s_iter),
196 lttng_snapshot_output_get_name(s_iter),
197 lttng_snapshot_output_get_ctrl_url(s_iter));
198 output_seen = 1;
199 }
200
201 lttng_snapshot_output_list_destroy(list);
202
203 if (!output_seen) {
204 MSG("%sNone", indent4);
205 }
206
207 error:
208 return ret;
209 }
210
211 /*
212 * Delete output by ID.
213 */
214 static int del_output(uint32_t id)
215 {
216 int ret;
217 struct lttng_snapshot_output *output = NULL;
218
219 output = lttng_snapshot_output_create();
220 if (!output) {
221 ret = CMD_FATAL;
222 goto error;
223 }
224
225 ret = lttng_snapshot_output_set_id(id, output);
226 if (ret < 0) {
227 ret = CMD_FATAL;
228 goto error;
229 }
230
231 ret = lttng_snapshot_del_output(current_session_name, output);
232 if (ret < 0) {
233 goto error;
234 }
235
236 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
237 id, current_session_name);
238
239 error:
240 lttng_snapshot_output_destroy(output);
241 return ret;
242 }
243
244 /*
245 * Add output from the user URL.
246 */
247 static int add_output(const char *url)
248 {
249 int ret;
250 struct lttng_snapshot_output *output = NULL;
251
252 if (!url && (!opt_data_url || !opt_ctrl_url)) {
253 ret = CMD_ERROR;
254 goto error;
255 }
256
257 output = create_output_from_args(url);
258 if (!output) {
259 ret = CMD_FATAL;
260 goto error;
261 }
262
263 /* This call, if successful, populates the id of the output object. */
264 ret = lttng_snapshot_add_output(current_session_name, output);
265 if (ret < 0) {
266 goto error;
267 }
268
269 MSG("Snapshot output successfully added for session %s",
270 current_session_name);
271 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
272 lttng_snapshot_output_get_id(output),
273 lttng_snapshot_output_get_name(output),
274 lttng_snapshot_output_get_ctrl_url(output),
275 lttng_snapshot_output_get_maxsize(output));
276 error:
277 lttng_snapshot_output_destroy(output);
278 return ret;
279 }
280
281 static int cmd_add_output(int argc, const char **argv)
282 {
283 int ret = CMD_SUCCESS;
284
285 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
286 usage(stderr);
287 ret = CMD_ERROR;
288 goto end;
289 }
290
291 ret = add_output(argv[1]);
292
293 end:
294 return ret;
295 }
296
297 static int cmd_del_output(int argc, const char **argv)
298 {
299 int ret = CMD_SUCCESS;
300
301 if (argc < 2) {
302 usage(stderr);
303 ret = CMD_ERROR;
304 goto end;
305 }
306
307 ret = del_output(atoi(argv[1]));
308
309 end:
310 return ret;
311 }
312
313 static int cmd_list_output(int argc, const char **argv)
314 {
315 return list_output();
316 }
317
318 /*
319 * Do a snapshot record with the URL if one is given.
320 */
321 static int record(const char *url)
322 {
323 int ret;
324 struct lttng_snapshot_output *output = NULL;
325
326 if (url || (opt_ctrl_url && opt_data_url)) {
327 output = create_output_from_args(url);
328 if (!output) {
329 ret = CMD_FATAL;
330 goto error;
331 }
332 }
333
334 ret = lttng_snapshot_record(current_session_name, output, 0);
335 if (ret < 0) {
336 goto error;
337 }
338
339 MSG("Snapshot recorded successfully for session %s", current_session_name);
340
341 if (url) {
342 MSG("Snapshot written at: %s", url);
343 } else if (opt_ctrl_url) {
344 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
345 opt_data_url);
346 } else {
347 MSG("Snapshot written in session directory.");
348 }
349
350 error:
351 lttng_snapshot_output_destroy(output);
352 return ret;
353 }
354
355 static int cmd_record(int argc, const char **argv)
356 {
357 int ret;
358
359 if (argc == 2) {
360 /* With a given URL */
361 ret = record(argv[1]);
362 } else {
363 ret = record(NULL);
364 }
365
366 return ret;
367 }
368
369 static int handle_command(const char **argv)
370 {
371 int ret, i = 0, argc;
372 struct cmd_struct *cmd;
373
374 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
375 (opt_ctrl_url && !opt_data_url)) {
376 usage(stderr);
377 ret = CMD_ERROR;
378 goto end;
379 }
380
381 argc = count_arguments(argv);
382
383 cmd = &actions[i];
384 while (cmd->func != NULL) {
385 /* Find command */
386 if (strcmp(argv[0], cmd->name) == 0) {
387 ret = cmd->func(argc, argv);
388 goto end;
389 }
390 i++;
391 cmd = &actions[i];
392 }
393
394 /* Command not found */
395 ret = CMD_UNDEFINED;
396
397 end:
398 return ret;
399 }
400
401 /*
402 * The 'snapshot <cmd> <options>' first level command
403 */
404 int cmd_snapshot(int argc, const char **argv)
405 {
406 int opt, ret = CMD_SUCCESS;
407 char *session_name = NULL;
408 static poptContext pc;
409
410 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
411 poptReadDefaultConfig(pc, 0);
412
413 while ((opt = poptGetNextOpt(pc)) != -1) {
414 switch (opt) {
415 case OPT_HELP:
416 usage(stdout);
417 goto end;
418 case OPT_LIST_OPTIONS:
419 list_cmd_options(stdout, snapshot_opts);
420 goto end;
421 case OPT_MAX_SIZE:
422 {
423 long long int val;
424 char *endptr;
425 const char *opt = poptGetOptArg(pc);
426
427 val = strtoll(opt, &endptr, 10);
428 if ((errno == ERANGE && (val == LLONG_MAX || val == LONG_MIN))
429 || (errno != 0 && val == 0)) {
430 ERR("Unable to handle max-size value %s", opt);
431 ret = CMD_ERROR;
432 goto end;
433 }
434
435 if (endptr == opt) {
436 ERR("No digits were found in %s", opt);
437 ret = CMD_ERROR;
438 goto end;
439 }
440 opt_max_size = val;
441
442 break;
443 }
444 default:
445 usage(stderr);
446 ret = CMD_UNDEFINED;
447 goto end;
448 }
449 }
450
451 if (!opt_session_name) {
452 session_name = get_session_name();
453 if (session_name == NULL) {
454 ret = CMD_ERROR;
455 goto end;
456 }
457 current_session_name = session_name;
458 } else {
459 current_session_name = opt_session_name;
460 }
461
462 ret = handle_command(poptGetArgs(pc));
463 if (ret < 0) {
464 if (ret == -LTTNG_ERR_EPERM) {
465 ERR("The session needs to be set in no output mode (--no-output)");
466 }
467 ERR("%s", lttng_strerror(ret));
468 goto end;
469 }
470
471 end:
472 if (!opt_session_name) {
473 free(session_name);
474 }
475 poptFreeContext(pc);
476 return ret;
477 }
This page took 0.039852 seconds and 5 git commands to generate.