Fix: use of uninitialized channel attributes in client
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <popt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <inttypes.h>
27 #include <assert.h>
28 #include <ctype.h>
29
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/utils.h>
32 #include <common/mi-lttng.h>
33
34 #include "../command.h"
35 #include "../utils.h"
36
37
38 static struct lttng_channel chan_opts;
39 static char *opt_channels;
40 static int opt_kernel;
41 static char *opt_session_name;
42 static int opt_userspace;
43 static char *opt_output;
44 static int opt_buffer_uid;
45 static int opt_buffer_pid;
46 static int opt_buffer_global;
47 static struct {
48 bool set;
49 uint32_t interval;
50 } opt_monitor_timer;
51
52 static struct mi_writer *writer;
53
54 #ifdef LTTNG_EMBED_HELP
55 static const char help_msg[] =
56 #include <lttng-enable-channel.1.h>
57 ;
58 #endif
59
60 enum {
61 OPT_HELP = 1,
62 OPT_DISCARD,
63 OPT_OVERWRITE,
64 OPT_SUBBUF_SIZE,
65 OPT_NUM_SUBBUF,
66 OPT_SWITCH_TIMER,
67 OPT_MONITOR_TIMER,
68 OPT_READ_TIMER,
69 OPT_USERSPACE,
70 OPT_LIST_OPTIONS,
71 OPT_TRACEFILE_SIZE,
72 OPT_TRACEFILE_COUNT,
73 };
74
75 static struct lttng_handle *handle;
76
77 const char *output_mmap = "mmap";
78 const char *output_splice = "splice";
79
80 static struct poptOption long_options[] = {
81 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
82 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
83 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
84 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
85 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
86 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
87 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
88 {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0},
89 {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
90 {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
91 {"monitor-timer", 0, POPT_ARG_INT, 0, OPT_MONITOR_TIMER, 0, 0},
92 {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
93 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
94 {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0},
95 {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0},
96 {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0},
97 {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0},
98 {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0},
99 {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0},
100 {0, 0, 0, 0, 0, 0, 0}
101 };
102
103 /*
104 * Set default attributes depending on those already defined from the command
105 * line.
106 */
107 static void set_default_attr(struct lttng_domain *dom)
108 {
109 struct lttng_channel_attr default_attr;
110
111 memset(&default_attr, 0, sizeof(default_attr));
112
113 /* Set attributes */
114 lttng_channel_set_default_attr(dom, &default_attr);
115
116 if (chan_opts.attr.overwrite == -1) {
117 chan_opts.attr.overwrite = default_attr.overwrite;
118 }
119 if (chan_opts.attr.subbuf_size == -1) {
120 chan_opts.attr.subbuf_size = default_attr.subbuf_size;
121 }
122 if (chan_opts.attr.num_subbuf == -1) {
123 chan_opts.attr.num_subbuf = default_attr.num_subbuf;
124 }
125 if (chan_opts.attr.switch_timer_interval == -1) {
126 chan_opts.attr.switch_timer_interval = default_attr.switch_timer_interval;
127 }
128 if (chan_opts.attr.read_timer_interval == -1) {
129 chan_opts.attr.read_timer_interval = default_attr.read_timer_interval;
130 }
131 if ((int) chan_opts.attr.output == -1) {
132 chan_opts.attr.output = default_attr.output;
133 }
134 if (chan_opts.attr.tracefile_count == -1) {
135 chan_opts.attr.tracefile_count = default_attr.tracefile_count;
136 }
137 if (chan_opts.attr.tracefile_size == -1) {
138 chan_opts.attr.tracefile_size = default_attr.tracefile_size;
139 }
140 }
141
142 /*
143 * Adding channel using the lttng API.
144 */
145 static int enable_channel(char *session_name)
146 {
147 struct lttng_channel *channel = NULL;
148 int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0;
149 char *channel_name;
150 struct lttng_domain dom;
151
152 memset(&dom, 0, sizeof(dom));
153
154 /* Create lttng domain */
155 if (opt_kernel) {
156 dom.type = LTTNG_DOMAIN_KERNEL;
157 dom.buf_type = LTTNG_BUFFER_GLOBAL;
158 if (opt_buffer_uid || opt_buffer_pid) {
159 ERR("Buffer type not supported for domain -k");
160 ret = CMD_ERROR;
161 goto error;
162 }
163 } else if (opt_userspace) {
164 dom.type = LTTNG_DOMAIN_UST;
165 if (opt_buffer_pid) {
166 dom.buf_type = LTTNG_BUFFER_PER_PID;
167 } else {
168 if (opt_buffer_global) {
169 ERR("Buffer type not supported for domain -u");
170 ret = CMD_ERROR;
171 goto error;
172 }
173 dom.buf_type = LTTNG_BUFFER_PER_UID;
174 }
175 } else {
176 /* Checked by the caller. */
177 assert(0);
178 }
179
180 set_default_attr(&dom);
181
182 if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) {
183 ERR("Missing option --tracefile-size. "
184 "A file count without a size won't do anything.");
185 ret = CMD_ERROR;
186 goto error;
187 }
188
189 if ((chan_opts.attr.tracefile_size > 0) &&
190 (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) {
191 WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")",
192 chan_opts.attr.tracefile_size, chan_opts.attr.subbuf_size);
193 chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size;
194 }
195
196 /* Setting channel output */
197 if (opt_output) {
198 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
199 chan_opts.attr.output = LTTNG_EVENT_MMAP;
200 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
201 chan_opts.attr.output = LTTNG_EVENT_SPLICE;
202 } else {
203 ERR("Unknown output type %s. Possible values are: %s, %s\n",
204 opt_output, output_mmap, output_splice);
205 ret = CMD_ERROR;
206 goto error;
207 }
208 }
209
210 handle = lttng_create_handle(session_name, &dom);
211 if (handle == NULL) {
212 ret = -1;
213 goto error;
214 }
215
216 /* Mi open channels element */
217 if (lttng_opt_mi) {
218 assert(writer);
219 ret = mi_lttng_channels_open(writer);
220 if (ret) {
221 ret = CMD_ERROR;
222 goto error;
223 }
224 }
225
226 /* Strip channel list (format: chan1,chan2,...) */
227 channel_name = strtok(opt_channels, ",");
228 while (channel_name != NULL) {
229 void *extended_ptr;
230
231 /* Validate channel name's length */
232 if (strlen(channel_name) >= NAME_MAX) {
233 ERR("Channel name is too long (max. %zu characters)",
234 sizeof(chan_opts.name) - 1);
235 error = 1;
236 goto skip_enable;
237 }
238
239 /*
240 * A dynamically-allocated channel is used in order to allow
241 * the configuration of extended attributes (post-2.9).
242 */
243 channel = lttng_channel_create(&dom);
244 if (!channel) {
245 ERR("Unable to create channel object");
246 error = 1;
247 goto error;
248 }
249
250 /* Copy channel name */
251 strcpy(channel->name, channel_name);
252 channel->enabled = 1;
253 extended_ptr = channel->attr.extended.ptr;
254 memcpy(&channel->attr, &chan_opts.attr, sizeof(chan_opts.attr));
255 channel->attr.extended.ptr = extended_ptr;
256 if (opt_monitor_timer.set) {
257 ret = lttng_channel_set_monitor_timer_interval(channel,
258 opt_monitor_timer.interval);
259 if (ret) {
260 ERR("Failed to set the channel's monitor timer interval");
261 error = 1;
262 goto error;
263 }
264 }
265
266 DBG("Enabling channel %s", channel_name);
267
268 ret = lttng_enable_channel(handle, channel);
269 if (ret < 0) {
270 success = 0;
271 switch (-ret) {
272 case LTTNG_ERR_KERN_CHAN_EXIST:
273 case LTTNG_ERR_UST_CHAN_EXIST:
274 case LTTNG_ERR_CHAN_EXIST:
275 WARN("Channel %s: %s (session %s)", channel_name,
276 lttng_strerror(ret), session_name);
277 warn = 1;
278 break;
279 case LTTNG_ERR_INVALID_CHANNEL_NAME:
280 ERR("Invalid channel name: \"%s\". "
281 "Channel names may not start with '.', and "
282 "may not contain '/'.", channel_name);
283 error = 1;
284 break;
285 default:
286 ERR("Channel %s: %s (session %s)", channel_name,
287 lttng_strerror(ret), session_name);
288 error = 1;
289 break;
290 }
291 } else {
292 MSG("%s channel %s enabled for session %s",
293 get_domain_str(dom.type), channel_name, session_name);
294 success = 1;
295 }
296
297 skip_enable:
298 if (lttng_opt_mi) {
299 /* Mi print the channel element and leave it open */
300 ret = mi_lttng_channel(writer, channel, 1);
301 if (ret) {
302 ret = CMD_ERROR;
303 goto error;
304 }
305
306 /* Individual Success ? */
307 ret = mi_lttng_writer_write_element_bool(writer,
308 mi_lttng_element_command_success, success);
309 if (ret) {
310 ret = CMD_ERROR;
311 goto error;
312 }
313
314 /* Close channel element */
315 ret = mi_lttng_writer_close_element(writer);
316 if (ret) {
317 ret = CMD_ERROR;
318 goto error;
319 }
320 }
321
322 /* Next channel */
323 channel_name = strtok(NULL, ",");
324 lttng_channel_destroy(channel);
325 channel = NULL;
326 }
327
328 if (lttng_opt_mi) {
329 /* Close channels element */
330 ret = mi_lttng_writer_close_element(writer);
331 if (ret) {
332 ret = CMD_ERROR;
333 goto error;
334 }
335 }
336
337 ret = CMD_SUCCESS;
338
339 error:
340 if (channel) {
341 lttng_channel_destroy(channel);
342 }
343 /* If more important error happen bypass the warning */
344 if (!ret && warn) {
345 ret = CMD_WARNING;
346 }
347 /* If more important error happen bypass the warning */
348 if (!ret && error) {
349 ret = CMD_ERROR;
350 }
351
352 lttng_destroy_handle(handle);
353
354 return ret;
355 }
356
357 /*
358 * Default value for channel configuration.
359 */
360 static void init_channel_config(void)
361 {
362 /*
363 * Put -1 everywhere so we can identify those set by the command line and
364 * those needed to be set by the default values.
365 */
366 memset(&chan_opts.attr, -1, sizeof(chan_opts.attr));
367 chan_opts.attr.extended.ptr = NULL;
368 }
369
370 /*
371 * Add channel to trace session
372 */
373 int cmd_enable_channels(int argc, const char **argv)
374 {
375 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
376 static poptContext pc;
377 char *session_name = NULL;
378 char *opt_arg = NULL;
379
380 init_channel_config();
381
382 pc = poptGetContext(NULL, argc, argv, long_options, 0);
383 poptReadDefaultConfig(pc, 0);
384
385 while ((opt = poptGetNextOpt(pc)) != -1) {
386 switch (opt) {
387 case OPT_HELP:
388 SHOW_HELP();
389 goto end;
390 case OPT_DISCARD:
391 chan_opts.attr.overwrite = 0;
392 DBG("Channel set to discard");
393 break;
394 case OPT_OVERWRITE:
395 chan_opts.attr.overwrite = 1;
396 DBG("Channel set to overwrite");
397 break;
398 case OPT_SUBBUF_SIZE:
399 {
400 uint64_t rounded_size;
401 int order;
402
403 /* Parse the size */
404 opt_arg = poptGetOptArg(pc);
405 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || !chan_opts.attr.subbuf_size) {
406 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
407 ret = CMD_ERROR;
408 goto end;
409 }
410
411 order = get_count_order_u64(chan_opts.attr.subbuf_size);
412 assert(order >= 0);
413 rounded_size = 1ULL << order;
414 if (rounded_size < chan_opts.attr.subbuf_size) {
415 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
416 chan_opts.attr.subbuf_size);
417 ret = CMD_ERROR;
418 goto end;
419 }
420
421 if (rounded_size != chan_opts.attr.subbuf_size) {
422 WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
423 chan_opts.attr.subbuf_size, rounded_size);
424 chan_opts.attr.subbuf_size = rounded_size;
425 }
426
427 /* Should now be power of 2 */
428 assert(!((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size));
429
430 DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size);
431 break;
432 }
433 case OPT_NUM_SUBBUF:
434 {
435 uint64_t rounded_size;
436 int order;
437
438 errno = 0;
439 opt_arg = poptGetOptArg(pc);
440 chan_opts.attr.num_subbuf = strtoull(opt_arg, NULL, 0);
441 if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) {
442 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
443 ret = CMD_ERROR;
444 goto end;
445 }
446
447 order = get_count_order_u64(chan_opts.attr.num_subbuf);
448 assert(order >= 0);
449 rounded_size = 1ULL << order;
450 if (rounded_size < chan_opts.attr.num_subbuf) {
451 ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!",
452 chan_opts.attr.num_subbuf);
453 ret = CMD_ERROR;
454 goto end;
455 }
456
457 if (rounded_size != chan_opts.attr.num_subbuf) {
458 WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
459 chan_opts.attr.num_subbuf, rounded_size);
460 chan_opts.attr.num_subbuf = rounded_size;
461 }
462
463 /* Should now be power of 2 */
464 assert(!((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf));
465
466 DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf);
467 break;
468 }
469 case OPT_SWITCH_TIMER:
470 {
471 unsigned long v;
472
473 errno = 0;
474 opt_arg = poptGetOptArg(pc);
475 v = strtoul(opt_arg, NULL, 0);
476 if (errno != 0 || !isdigit(opt_arg[0])) {
477 ERR("Wrong value in --switch-timer parameter: %s", opt_arg);
478 ret = CMD_ERROR;
479 goto end;
480 }
481 if (v != (uint32_t) v) {
482 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
483 ret = CMD_ERROR;
484 goto end;
485 }
486 chan_opts.attr.switch_timer_interval = (uint32_t) v;
487 DBG("Channel switch timer interval set to %d", chan_opts.attr.switch_timer_interval);
488 break;
489 }
490 case OPT_READ_TIMER:
491 {
492 unsigned long v;
493
494 errno = 0;
495 opt_arg = poptGetOptArg(pc);
496 v = strtoul(opt_arg, NULL, 0);
497 if (errno != 0 || !isdigit(opt_arg[0])) {
498 ERR("Wrong value in --read-timer parameter: %s", opt_arg);
499 ret = CMD_ERROR;
500 goto end;
501 }
502 if (v != (uint32_t) v) {
503 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
504 ret = CMD_ERROR;
505 goto end;
506 }
507 chan_opts.attr.read_timer_interval = (uint32_t) v;
508 DBG("Channel read timer interval set to %d", chan_opts.attr.read_timer_interval);
509 break;
510 }
511 case OPT_MONITOR_TIMER:
512 {
513 unsigned long v;
514
515 errno = 0;
516 opt_arg = poptGetOptArg(pc);
517 v = strtoul(opt_arg, NULL, 0);
518 if (errno != 0 || !isdigit(opt_arg[0])) {
519 ERR("Wrong value in --monitor-timer parameter: %s", opt_arg);
520 ret = CMD_ERROR;
521 goto end;
522 }
523 if (v != (uint32_t) v) {
524 ERR("32-bit overflow in --monitor-timer parameter: %s", opt_arg);
525 ret = CMD_ERROR;
526 goto end;
527 }
528 opt_monitor_timer.interval = (uint32_t) v;
529 opt_monitor_timer.set = true;
530 DBG("Channel monitor timer interval set to %d", opt_monitor_timer.interval);
531 break;
532 }
533 case OPT_USERSPACE:
534 opt_userspace = 1;
535 break;
536 case OPT_TRACEFILE_SIZE:
537 opt_arg = poptGetOptArg(pc);
538 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) {
539 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
540 ret = CMD_ERROR;
541 goto end;
542 }
543 DBG("Maximum tracefile size set to %" PRIu64,
544 chan_opts.attr.tracefile_size);
545 break;
546 case OPT_TRACEFILE_COUNT:
547 {
548 unsigned long v;
549
550 errno = 0;
551 opt_arg = poptGetOptArg(pc);
552 v = strtoul(opt_arg, NULL, 0);
553 if (errno != 0 || !isdigit(opt_arg[0])) {
554 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
555 ret = CMD_ERROR;
556 goto end;
557 }
558 if (v != (uint32_t) v) {
559 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
560 ret = CMD_ERROR;
561 goto end;
562 }
563 chan_opts.attr.tracefile_count = (uint32_t) v;
564 DBG("Maximum tracefile count set to %" PRIu64,
565 chan_opts.attr.tracefile_count);
566 break;
567 }
568 case OPT_LIST_OPTIONS:
569 list_cmd_options(stdout, long_options);
570 goto end;
571 default:
572 ret = CMD_UNDEFINED;
573 goto end;
574 }
575 }
576
577 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
578 if (ret) {
579 ret = CMD_ERROR;
580 goto end;
581 }
582
583 /* Mi check */
584 if (lttng_opt_mi) {
585 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
586 if (!writer) {
587 ret = -LTTNG_ERR_NOMEM;
588 goto end;
589 }
590
591 /* Open command element */
592 ret = mi_lttng_writer_command_open(writer,
593 mi_lttng_element_command_enable_channels);
594 if (ret) {
595 ret = CMD_ERROR;
596 goto end;
597 }
598
599 /* Open output element */
600 ret = mi_lttng_writer_open_element(writer,
601 mi_lttng_element_command_output);
602 if (ret) {
603 ret = CMD_ERROR;
604 goto end;
605 }
606 }
607
608 opt_channels = (char*) poptGetArg(pc);
609 if (opt_channels == NULL) {
610 ERR("Missing channel name.\n");
611 ret = CMD_ERROR;
612 success = 0;
613 goto mi_closing;
614 }
615
616 if (!opt_session_name) {
617 session_name = get_session_name();
618 if (session_name == NULL) {
619 command_ret = CMD_ERROR;
620 success = 0;
621 goto mi_closing;
622 }
623 } else {
624 session_name = opt_session_name;
625 }
626
627 command_ret = enable_channel(session_name);
628 if (command_ret) {
629 success = 0;
630 }
631
632 mi_closing:
633 /* Mi closing */
634 if (lttng_opt_mi) {
635 /* Close output element */
636 ret = mi_lttng_writer_close_element(writer);
637 if (ret) {
638 goto end;
639 }
640
641 /* Success ? */
642 ret = mi_lttng_writer_write_element_bool(writer,
643 mi_lttng_element_command_success, success);
644 if (ret) {
645 goto end;
646 }
647
648 /* Command element close */
649 ret = mi_lttng_writer_command_close(writer);
650 if (ret) {
651 goto end;
652 }
653 }
654
655 end:
656 /* Mi clean-up */
657 if (writer && mi_lttng_writer_destroy(writer)) {
658 /* Preserve original error code */
659 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
660 }
661
662 if (!opt_session_name && session_name) {
663 free(session_name);
664 }
665
666 /* Overwrite ret if an error occurred when enable_channel */
667 ret = command_ret ? command_ret : ret;
668 poptFreeContext(pc);
669 return ret;
670 }
This page took 0.06618 seconds and 4 git commands to generate.