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