Fix: channel names are not validated
[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 _GNU_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 "../command.h"
31 #include "../utils.h"
32
33 #include <src/common/sessiond-comm/sessiond-comm.h>
34 #include <src/common/utils.h>
35
36 static char *opt_channels;
37 static int opt_kernel;
38 static char *opt_session_name;
39 static int opt_userspace;
40 static struct lttng_channel chan;
41 static char *opt_output;
42 static int opt_buffer_uid;
43 static int opt_buffer_pid;
44 static int opt_buffer_global;
45
46 enum {
47 OPT_HELP = 1,
48 OPT_DISCARD,
49 OPT_OVERWRITE,
50 OPT_SUBBUF_SIZE,
51 OPT_NUM_SUBBUF,
52 OPT_SWITCH_TIMER,
53 OPT_READ_TIMER,
54 OPT_USERSPACE,
55 OPT_LIST_OPTIONS,
56 OPT_TRACEFILE_SIZE,
57 OPT_TRACEFILE_COUNT,
58 };
59
60 static struct lttng_handle *handle;
61
62 const char *output_mmap = "mmap";
63 const char *output_splice = "splice";
64
65 static struct poptOption long_options[] = {
66 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
67 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
68 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
69 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
70 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
71 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
72 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
73 {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0},
74 {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
75 {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
76 {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
77 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
78 {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0},
79 {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0},
80 {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0},
81 {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0},
82 {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0},
83 {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0},
84 {0, 0, 0, 0, 0, 0, 0}
85 };
86
87 /*
88 * usage
89 */
90 static void usage(FILE *ofp)
91 {
92 fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] (-u | -k) [OPTIONS]\n");
93 fprintf(ofp, "\n");
94 fprintf(ofp, "Options:\n");
95 fprintf(ofp, " -h, --help Show this help\n");
96 fprintf(ofp, " --list-options Simple listing of options\n");
97 fprintf(ofp, " -s, --session NAME Apply to session name\n");
98 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
99 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
100 fprintf(ofp, "\n");
101 fprintf(ofp, "Channel options:\n");
102 fprintf(ofp, " --discard Discard event when buffers are full%s\n",
103 DEFAULT_CHANNEL_OVERWRITE ? "" : " (default)");
104 fprintf(ofp, " --overwrite Flight recorder mode%s\n",
105 DEFAULT_CHANNEL_OVERWRITE ? " (default)" : "");
106 fprintf(ofp, " --subbuf-size SIZE Subbuffer size in bytes {+k,+M,+G}\n");
107 fprintf(ofp, " (default UST uid: %zu, UST pid: %zu, kernel: %zu, metadata: %zu)\n",
108 default_get_ust_uid_channel_subbuf_size(),
109 default_get_ust_pid_channel_subbuf_size(),
110 default_get_kernel_channel_subbuf_size(),
111 default_get_metadata_subbuf_size());
112 fprintf(ofp, " Rounded up to the next power of 2.\n");
113 fprintf(ofp, " --num-subbuf NUM Number of subbufers\n");
114 fprintf(ofp, " (default UST uid: %u, UST pid: %u, kernel: %u, metadata: %u)\n",
115 DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM, DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM,
116 DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM, DEFAULT_METADATA_SUBBUF_NUM);
117 fprintf(ofp, " Rounded up to the next power of 2.\n");
118 fprintf(ofp, " --switch-timer USEC Switch timer interval in usec\n");
119 fprintf(ofp, " (default UST uid: %u, UST pid: %u, kernel: %u, metadata: %u)\n",
120 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER, DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER,
121 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER, DEFAULT_METADATA_SWITCH_TIMER);
122 fprintf(ofp, " --read-timer USEC Read timer interval in usec.\n");
123 fprintf(ofp, " (default UST uid: %u, UST pid: %u, kernel: %u, metadata: %u)\n",
124 DEFAULT_UST_UID_CHANNEL_READ_TIMER, DEFAULT_UST_UID_CHANNEL_READ_TIMER,
125 DEFAULT_KERNEL_CHANNEL_READ_TIMER, DEFAULT_METADATA_READ_TIMER);
126 fprintf(ofp, " --output TYPE Channel output type (Values: %s, %s)\n",
127 output_mmap, output_splice);
128 fprintf(ofp, " (default UST uid: %s, UST pid: %s, kernel: %s, metadata: %s)\n",
129 DEFAULT_UST_UID_CHANNEL_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice,
130 DEFAULT_UST_PID_CHANNEL_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice,
131 DEFAULT_KERNEL_CHANNEL_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice,
132 DEFAULT_METADATA_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice);
133 fprintf(ofp, " --buffers-uid Use per UID buffer (-u only)\n");
134 fprintf(ofp, " --buffers-pid Use per PID buffer (-u only)\n");
135 fprintf(ofp, " --buffers-global Use shared buffer for the whole system (-k only)\n");
136 fprintf(ofp, " -C, --tracefile-size SIZE\n");
137 fprintf(ofp, " Maximum size of each tracefile within a stream (in bytes). 0 means unlimited.\n");
138 fprintf(ofp, " (default: %u)\n", DEFAULT_CHANNEL_TRACEFILE_SIZE);
139 fprintf(ofp, " Note: traces generated with this option may inaccurately report\n");
140 fprintf(ofp, " discarded events as per CTF 1.8.\n");
141 fprintf(ofp, " -W, --tracefile-count COUNT\n");
142 fprintf(ofp, " Used in conjunction with -C option, this will limit the number\n");
143 fprintf(ofp, " of files created to the specified count. 0 means unlimited.\n");
144 fprintf(ofp, " (default: %u)\n", DEFAULT_CHANNEL_TRACEFILE_COUNT);
145 fprintf(ofp, "\n");
146 }
147
148 /*
149 * Set default attributes depending on those already defined from the command
150 * line.
151 */
152 static void set_default_attr(struct lttng_domain *dom)
153 {
154 struct lttng_channel_attr default_attr;
155
156 /* Set attributes */
157 lttng_channel_set_default_attr(dom, &default_attr);
158
159 if (chan.attr.overwrite == -1) {
160 chan.attr.overwrite = default_attr.overwrite;
161 }
162 if (chan.attr.subbuf_size == -1) {
163 chan.attr.subbuf_size = default_attr.subbuf_size;
164 }
165 if (chan.attr.num_subbuf == -1) {
166 chan.attr.num_subbuf = default_attr.num_subbuf;
167 }
168 if (chan.attr.switch_timer_interval == -1) {
169 chan.attr.switch_timer_interval = default_attr.switch_timer_interval;
170 }
171 if (chan.attr.read_timer_interval == -1) {
172 chan.attr.read_timer_interval = default_attr.read_timer_interval;
173 }
174 if ((int) chan.attr.output == -1) {
175 chan.attr.output = default_attr.output;
176 }
177 if (chan.attr.tracefile_count == -1) {
178 chan.attr.tracefile_count = default_attr.tracefile_count;
179 }
180 if (chan.attr.tracefile_size == -1) {
181 chan.attr.tracefile_size = default_attr.tracefile_size;
182 }
183 }
184
185 /*
186 * Adding channel using the lttng API.
187 */
188 static int enable_channel(char *session_name)
189 {
190 int ret = CMD_SUCCESS, warn = 0;
191 char *channel_name;
192 struct lttng_domain dom;
193
194 memset(&dom, 0, sizeof(dom));
195
196 /* Create lttng domain */
197 if (opt_kernel) {
198 dom.type = LTTNG_DOMAIN_KERNEL;
199 dom.buf_type = LTTNG_BUFFER_GLOBAL;
200 if (opt_buffer_uid || opt_buffer_pid) {
201 ERR("Buffer type not supported for domain -k");
202 ret = CMD_ERROR;
203 goto error;
204 }
205 } else if (opt_userspace) {
206 dom.type = LTTNG_DOMAIN_UST;
207 if (opt_buffer_pid) {
208 dom.buf_type = LTTNG_BUFFER_PER_PID;
209 } else {
210 if (opt_buffer_global) {
211 ERR("Buffer type not supported for domain -u");
212 ret = CMD_ERROR;
213 goto error;
214 }
215 dom.buf_type = LTTNG_BUFFER_PER_UID;
216 }
217 } else {
218 print_missing_domain();
219 ret = CMD_ERROR;
220 goto error;
221 }
222
223 set_default_attr(&dom);
224
225 if (chan.attr.tracefile_size == 0 && chan.attr.tracefile_count) {
226 ERR("Missing option --tracefile-size. "
227 "A file count without a size won't do anything.");
228 ret = CMD_ERROR;
229 goto error;
230 }
231
232 if ((chan.attr.tracefile_size > 0) &&
233 (chan.attr.tracefile_size < chan.attr.subbuf_size)) {
234 WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")",
235 chan.attr.tracefile_size, chan.attr.subbuf_size);
236 chan.attr.tracefile_size = chan.attr.subbuf_size;
237 }
238
239 /* Setting channel output */
240 if (opt_output) {
241 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
242 chan.attr.output = LTTNG_EVENT_MMAP;
243 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
244 chan.attr.output = LTTNG_EVENT_SPLICE;
245 } else {
246 ERR("Unknown output type %s. Possible values are: %s, %s\n",
247 opt_output, output_mmap, output_splice);
248 usage(stderr);
249 ret = CMD_ERROR;
250 goto error;
251 }
252 }
253
254 handle = lttng_create_handle(session_name, &dom);
255 if (handle == NULL) {
256 ret = -1;
257 goto error;
258 }
259
260 /* Strip channel list (format: chan1,chan2,...) */
261 channel_name = strtok(opt_channels, ",");
262 while (channel_name != NULL) {
263 /* Validate channel name's length */
264 if (strlen(channel_name) >= NAME_MAX) {
265 ERR("Channel name is too long (max. %zu characters)",
266 sizeof(chan.name) - 1);
267 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
268 goto error;
269 }
270
271 /* Copy channel name */
272 strcpy(chan.name, channel_name);
273
274 DBG("Enabling channel %s", channel_name);
275
276 ret = lttng_enable_channel(handle, &chan);
277 if (ret < 0) {
278 switch (-ret) {
279 case LTTNG_ERR_KERN_CHAN_EXIST:
280 case LTTNG_ERR_UST_CHAN_EXIST:
281 case LTTNG_ERR_CHAN_EXIST:
282 WARN("Channel %s: %s (session %s)", channel_name,
283 lttng_strerror(ret), session_name);
284 goto error;
285 case LTTNG_ERR_INVALID_CHANNEL_NAME:
286 ERR("Invalid channel name: \"%s\". "
287 "Channel names may not start with '.', and "
288 "may not contain '/'.", channel_name);
289 goto error;
290 default:
291 ERR("Channel %s: %s (session %s)", channel_name,
292 lttng_strerror(ret), session_name);
293 break;
294 }
295 warn = 1;
296 } else {
297 MSG("%s channel %s enabled for session %s",
298 get_domain_str(dom.type), channel_name, session_name);
299 }
300
301 channel_name = strtok(NULL, ",");
302 }
303
304 ret = CMD_SUCCESS;
305
306 error:
307 if (warn) {
308 ret = CMD_WARNING;
309 }
310
311 lttng_destroy_handle(handle);
312
313 return ret;
314 }
315
316 /*
317 * Default value for channel configuration.
318 */
319 static void init_channel_config(void)
320 {
321 /*
322 * Put -1 everywhere so we can identify those set by the command line and
323 * those needed to be set by the default values.
324 */
325 memset(&chan.attr, -1, sizeof(chan.attr));
326 }
327
328 /*
329 * Add channel to trace session
330 */
331 int cmd_enable_channels(int argc, const char **argv)
332 {
333 int opt, ret = CMD_SUCCESS;
334 static poptContext pc;
335 char *session_name = NULL;
336 char *opt_arg = NULL;
337
338 init_channel_config();
339
340 pc = poptGetContext(NULL, argc, argv, long_options, 0);
341 poptReadDefaultConfig(pc, 0);
342
343 while ((opt = poptGetNextOpt(pc)) != -1) {
344 switch (opt) {
345 case OPT_HELP:
346 usage(stdout);
347 goto end;
348 case OPT_DISCARD:
349 chan.attr.overwrite = 0;
350 DBG("Channel set to discard");
351 break;
352 case OPT_OVERWRITE:
353 chan.attr.overwrite = 1;
354 DBG("Channel set to overwrite");
355 break;
356 case OPT_SUBBUF_SIZE:
357 {
358 uint64_t rounded_size;
359 int order;
360
361 /* Parse the size */
362 opt_arg = poptGetOptArg(pc);
363 if (utils_parse_size_suffix(opt_arg, &chan.attr.subbuf_size) < 0 || !chan.attr.subbuf_size) {
364 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
365 ret = CMD_ERROR;
366 goto end;
367 }
368
369 order = get_count_order_u64(chan.attr.subbuf_size);
370 assert(order >= 0);
371 rounded_size = 1ULL << order;
372 if (rounded_size < chan.attr.subbuf_size) {
373 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
374 chan.attr.subbuf_size);
375 ret = CMD_ERROR;
376 goto end;
377 }
378
379 if (rounded_size != chan.attr.subbuf_size) {
380 WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
381 chan.attr.subbuf_size, rounded_size);
382 chan.attr.subbuf_size = rounded_size;
383 }
384
385 /* Should now be power of 2 */
386 assert(!((chan.attr.subbuf_size - 1) & chan.attr.subbuf_size));
387
388 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
389 break;
390 }
391 case OPT_NUM_SUBBUF:
392 {
393 uint64_t rounded_size;
394 int order;
395
396 errno = 0;
397 opt_arg = poptGetOptArg(pc);
398 chan.attr.num_subbuf = strtoull(opt_arg, NULL, 0);
399 if (errno != 0 || !chan.attr.num_subbuf || !isdigit(opt_arg[0])) {
400 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
401 ret = CMD_ERROR;
402 goto end;
403 }
404
405 order = get_count_order_u64(chan.attr.num_subbuf);
406 assert(order >= 0);
407 rounded_size = 1ULL << order;
408 if (rounded_size < chan.attr.num_subbuf) {
409 ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!",
410 chan.attr.num_subbuf);
411 ret = CMD_ERROR;
412 goto end;
413 }
414
415 if (rounded_size != chan.attr.num_subbuf) {
416 WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
417 chan.attr.num_subbuf, rounded_size);
418 chan.attr.num_subbuf = rounded_size;
419 }
420
421 /* Should now be power of 2 */
422 assert(!((chan.attr.num_subbuf - 1) & chan.attr.num_subbuf));
423
424 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
425 break;
426 }
427 case OPT_SWITCH_TIMER:
428 {
429 unsigned long v;
430
431 errno = 0;
432 opt_arg = poptGetOptArg(pc);
433 v = strtoul(opt_arg, NULL, 0);
434 if (errno != 0 || !isdigit(opt_arg[0])) {
435 ERR("Wrong value in --switch-timer parameter: %s", opt_arg);
436 ret = CMD_ERROR;
437 goto end;
438 }
439 if (v != (uint32_t) v) {
440 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
441 ret = CMD_ERROR;
442 goto end;
443 }
444 chan.attr.switch_timer_interval = (uint32_t) v;
445 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
446 break;
447 }
448 case OPT_READ_TIMER:
449 {
450 unsigned long v;
451
452 errno = 0;
453 opt_arg = poptGetOptArg(pc);
454 v = strtoul(opt_arg, NULL, 0);
455 if (errno != 0 || !isdigit(opt_arg[0])) {
456 ERR("Wrong value in --read-timer parameter: %s", opt_arg);
457 ret = CMD_ERROR;
458 goto end;
459 }
460 if (v != (uint32_t) v) {
461 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
462 ret = CMD_ERROR;
463 goto end;
464 }
465 chan.attr.read_timer_interval = (uint32_t) v;
466 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
467 break;
468 }
469 case OPT_USERSPACE:
470 opt_userspace = 1;
471 break;
472 case OPT_TRACEFILE_SIZE:
473 opt_arg = poptGetOptArg(pc);
474 if (utils_parse_size_suffix(opt_arg, &chan.attr.tracefile_size) < 0) {
475 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
476 ret = CMD_ERROR;
477 goto end;
478 }
479 DBG("Maximum tracefile size set to %" PRIu64,
480 chan.attr.tracefile_size);
481 break;
482 case OPT_TRACEFILE_COUNT:
483 {
484 unsigned long v;
485
486 errno = 0;
487 opt_arg = poptGetOptArg(pc);
488 v = strtoul(opt_arg, NULL, 0);
489 if (errno != 0 || !isdigit(opt_arg[0])) {
490 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
491 ret = CMD_ERROR;
492 goto end;
493 }
494 if (v != (uint32_t) v) {
495 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
496 ret = CMD_ERROR;
497 goto end;
498 }
499 chan.attr.tracefile_count = (uint32_t) v;
500 DBG("Maximum tracefile count set to %" PRIu64,
501 chan.attr.tracefile_count);
502 break;
503 }
504 case OPT_LIST_OPTIONS:
505 list_cmd_options(stdout, long_options);
506 goto end;
507 default:
508 usage(stderr);
509 ret = CMD_UNDEFINED;
510 goto end;
511 }
512 }
513
514 opt_channels = (char*) poptGetArg(pc);
515 if (opt_channels == NULL) {
516 ERR("Missing channel name.\n");
517 usage(stderr);
518 ret = CMD_ERROR;
519 goto end;
520 }
521
522 if (!opt_session_name) {
523 session_name = get_session_name();
524 if (session_name == NULL) {
525 ret = CMD_ERROR;
526 goto end;
527 }
528 } else {
529 session_name = opt_session_name;
530 }
531
532 ret = enable_channel(session_name);
533
534 end:
535 if (!opt_session_name && session_name) {
536 free(session_name);
537 }
538 poptFreeContext(pc);
539 return ret;
540 }
This page took 0.042235 seconds and 4 git commands to generate.