b59c05861beeb021b0c1327def90381fe21378a4
[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/-j only)\n");
134 fprintf(ofp, " --buffers-pid Use per PID buffer (-u/-j 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, " -W, --tracefile-count COUNT\n");
140 fprintf(ofp, " Used in conjunction with -C option, this will limit the number\n");
141 fprintf(ofp, " of files created to the specified count. 0 means unlimited.\n");
142 fprintf(ofp, " (default: %u)\n", DEFAULT_CHANNEL_TRACEFILE_COUNT);
143 fprintf(ofp, "\n");
144 }
145
146 /*
147 * Set default attributes depending on those already defined from the command
148 * line.
149 */
150 static void set_default_attr(struct lttng_domain *dom)
151 {
152 struct lttng_channel_attr default_attr;
153
154 /* Set attributes */
155 lttng_channel_set_default_attr(dom, &default_attr);
156
157 if (chan.attr.overwrite == -1) {
158 chan.attr.overwrite = default_attr.overwrite;
159 }
160 if (chan.attr.subbuf_size == -1) {
161 chan.attr.subbuf_size = default_attr.subbuf_size;
162 }
163 if (chan.attr.num_subbuf == -1) {
164 chan.attr.num_subbuf = default_attr.num_subbuf;
165 }
166 if (chan.attr.switch_timer_interval == -1) {
167 chan.attr.switch_timer_interval = default_attr.switch_timer_interval;
168 }
169 if (chan.attr.read_timer_interval == -1) {
170 chan.attr.read_timer_interval = default_attr.read_timer_interval;
171 }
172 if (chan.attr.output == -1) {
173 chan.attr.output = default_attr.output;
174 }
175 if (chan.attr.tracefile_count == -1) {
176 chan.attr.tracefile_count = default_attr.tracefile_count;
177 }
178 if (chan.attr.tracefile_size == -1) {
179 chan.attr.tracefile_size = default_attr.tracefile_size;
180 }
181 }
182
183 /*
184 * Adding channel using the lttng API.
185 */
186 static int enable_channel(char *session_name)
187 {
188 int ret = CMD_SUCCESS, warn = 0;
189 char *channel_name;
190 struct lttng_domain dom;
191
192 memset(&dom, 0, sizeof(dom));
193
194 /* Create lttng domain */
195 if (opt_kernel) {
196 dom.type = LTTNG_DOMAIN_KERNEL;
197 dom.buf_type = LTTNG_BUFFER_GLOBAL;
198 if (opt_buffer_uid || opt_buffer_pid) {
199 ERR("Buffer type not supported for domain -k");
200 ret = CMD_ERROR;
201 goto error;
202 }
203 } else if (opt_userspace) {
204 dom.type = LTTNG_DOMAIN_UST;
205 if (opt_buffer_pid) {
206 dom.buf_type = LTTNG_BUFFER_PER_PID;
207 } else {
208 if (opt_buffer_global) {
209 ERR("Buffer type not supported for domain -u");
210 ret = CMD_ERROR;
211 goto error;
212 }
213 dom.buf_type = LTTNG_BUFFER_PER_UID;
214 }
215 } else {
216 print_missing_domain();
217 ret = CMD_ERROR;
218 goto error;
219 }
220
221 set_default_attr(&dom);
222
223 if (chan.attr.tracefile_size == 0 && chan.attr.tracefile_count) {
224 ERR("Missing option --tracefile-size. "
225 "A file count without a size won't do anything.");
226 ret = CMD_ERROR;
227 goto error;
228 }
229
230 if ((chan.attr.tracefile_size > 0) &&
231 (chan.attr.tracefile_size < chan.attr.subbuf_size)) {
232 WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")",
233 chan.attr.tracefile_size, chan.attr.subbuf_size);
234 chan.attr.tracefile_size = chan.attr.subbuf_size;
235 }
236
237 /* Setting channel output */
238 if (opt_output) {
239 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
240 chan.attr.output = LTTNG_EVENT_MMAP;
241 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
242 chan.attr.output = LTTNG_EVENT_SPLICE;
243 } else {
244 ERR("Unknown output type %s. Possible values are: %s, %s\n",
245 opt_output, output_mmap, output_splice);
246 usage(stderr);
247 ret = CMD_ERROR;
248 goto error;
249 }
250 }
251
252 handle = lttng_create_handle(session_name, &dom);
253 if (handle == NULL) {
254 ret = -1;
255 goto error;
256 }
257
258 /* Strip channel list (format: chan1,chan2,...) */
259 channel_name = strtok(opt_channels, ",");
260 while (channel_name != NULL) {
261 /* Copy channel name and normalize it */
262 strncpy(chan.name, channel_name, NAME_MAX);
263 chan.name[NAME_MAX - 1] = '\0';
264
265 DBG("Enabling channel %s", channel_name);
266
267 ret = lttng_enable_channel(handle, &chan);
268 if (ret < 0) {
269 switch (-ret) {
270 case LTTNG_ERR_KERN_CHAN_EXIST:
271 case LTTNG_ERR_UST_CHAN_EXIST:
272 case LTTNG_ERR_CHAN_EXIST:
273 WARN("Channel %s: %s (session %s)", channel_name,
274 lttng_strerror(ret), session_name);
275 goto error;
276 default:
277 ERR("Channel %s: %s (session %s)", channel_name,
278 lttng_strerror(ret), session_name);
279 break;
280 }
281 warn = 1;
282 } else {
283 MSG("%s channel %s enabled for session %s",
284 get_domain_str(dom.type), channel_name, session_name);
285 }
286
287 /* Next event */
288 channel_name = strtok(NULL, ",");
289 }
290
291 ret = CMD_SUCCESS;
292
293 error:
294 if (warn) {
295 ret = CMD_WARNING;
296 }
297
298 lttng_destroy_handle(handle);
299
300 return ret;
301 }
302
303 /*
304 * Default value for channel configuration.
305 */
306 static void init_channel_config(void)
307 {
308 /*
309 * Put -1 everywhere so we can identify those set by the command line and
310 * those needed to be set by the default values.
311 */
312 memset(&chan.attr, -1, sizeof(chan.attr));
313 }
314
315 /*
316 * Add channel to trace session
317 */
318 int cmd_enable_channels(int argc, const char **argv)
319 {
320 int opt, ret = CMD_SUCCESS;
321 static poptContext pc;
322 char *session_name = NULL;
323 char *opt_arg = NULL;
324
325 init_channel_config();
326
327 pc = poptGetContext(NULL, argc, argv, long_options, 0);
328 poptReadDefaultConfig(pc, 0);
329
330 while ((opt = poptGetNextOpt(pc)) != -1) {
331 switch (opt) {
332 case OPT_HELP:
333 usage(stdout);
334 goto end;
335 case OPT_DISCARD:
336 chan.attr.overwrite = 0;
337 DBG("Channel set to discard");
338 break;
339 case OPT_OVERWRITE:
340 chan.attr.overwrite = 1;
341 DBG("Channel set to overwrite");
342 break;
343 case OPT_SUBBUF_SIZE:
344 {
345 uint64_t rounded_size;
346 int order;
347
348 /* Parse the size */
349 opt_arg = poptGetOptArg(pc);
350 if (utils_parse_size_suffix(opt_arg, &chan.attr.subbuf_size) < 0 || !chan.attr.subbuf_size) {
351 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
352 ret = CMD_ERROR;
353 goto end;
354 }
355
356 order = get_count_order_u64(chan.attr.subbuf_size);
357 assert(order >= 0);
358 rounded_size = 1ULL << order;
359 if (rounded_size < chan.attr.subbuf_size) {
360 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
361 chan.attr.subbuf_size);
362 ret = CMD_ERROR;
363 goto end;
364 }
365
366 if (rounded_size != chan.attr.subbuf_size) {
367 WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
368 chan.attr.subbuf_size, rounded_size);
369 chan.attr.subbuf_size = rounded_size;
370 }
371
372 /* Should now be power of 2 */
373 assert(!((chan.attr.subbuf_size - 1) & chan.attr.subbuf_size));
374
375 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
376 break;
377 }
378 case OPT_NUM_SUBBUF:
379 {
380 uint64_t rounded_size;
381 int order;
382
383 errno = 0;
384 opt_arg = poptGetOptArg(pc);
385 chan.attr.num_subbuf = strtoull(opt_arg, NULL, 0);
386 if (errno != 0 || !chan.attr.num_subbuf || !isdigit(opt_arg[0])) {
387 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
388 ret = CMD_ERROR;
389 goto end;
390 }
391
392 order = get_count_order_u64(chan.attr.num_subbuf);
393 assert(order >= 0);
394 rounded_size = 1ULL << order;
395 if (rounded_size < chan.attr.subbuf_size) {
396 ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!",
397 chan.attr.num_subbuf);
398 ret = CMD_ERROR;
399 goto end;
400 }
401
402 if (rounded_size != chan.attr.num_subbuf) {
403 WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
404 chan.attr.num_subbuf, rounded_size);
405 chan.attr.num_subbuf = rounded_size;
406 }
407
408 /* Should now be power of 2 */
409 assert(!((chan.attr.num_subbuf - 1) & chan.attr.num_subbuf));
410
411 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
412 break;
413 }
414 case OPT_SWITCH_TIMER:
415 {
416 unsigned long v;
417
418 errno = 0;
419 opt_arg = poptGetOptArg(pc);
420 v = strtoul(opt_arg, NULL, 0);
421 if (errno != 0 || !isdigit(opt_arg[0])) {
422 ERR("Wrong value in --switch-timer parameter: %s", opt_arg);
423 ret = CMD_ERROR;
424 goto end;
425 }
426 if (v != (uint32_t) v) {
427 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
428 ret = CMD_ERROR;
429 goto end;
430 }
431 chan.attr.switch_timer_interval = (uint32_t) v;
432 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
433 break;
434 }
435 case OPT_READ_TIMER:
436 {
437 unsigned long v;
438
439 errno = 0;
440 opt_arg = poptGetOptArg(pc);
441 v = strtoul(opt_arg, NULL, 0);
442 if (errno != 0 || !isdigit(opt_arg[0])) {
443 ERR("Wrong value in --read-timer parameter: %s", opt_arg);
444 ret = CMD_ERROR;
445 goto end;
446 }
447 if (v != (uint32_t) v) {
448 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
449 ret = CMD_ERROR;
450 goto end;
451 }
452 chan.attr.read_timer_interval = (uint32_t) v;
453 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
454 break;
455 }
456 case OPT_USERSPACE:
457 opt_userspace = 1;
458 break;
459 case OPT_TRACEFILE_SIZE:
460 opt_arg = poptGetOptArg(pc);
461 if (utils_parse_size_suffix(opt_arg, &chan.attr.tracefile_size) < 0) {
462 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
463 ret = CMD_ERROR;
464 goto end;
465 }
466 DBG("Maximum tracefile size set to %" PRIu64,
467 chan.attr.tracefile_size);
468 break;
469 case OPT_TRACEFILE_COUNT:
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 --tracefile-count parameter: %s", opt_arg);
478 ret = CMD_ERROR;
479 goto end;
480 }
481 if (v != (uint32_t) v) {
482 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
483 ret = CMD_ERROR;
484 goto end;
485 }
486 chan.attr.tracefile_count = (uint32_t) v;
487 DBG("Maximum tracefile count set to %" PRIu64,
488 chan.attr.tracefile_count);
489 break;
490 }
491 case OPT_LIST_OPTIONS:
492 list_cmd_options(stdout, long_options);
493 goto end;
494 default:
495 usage(stderr);
496 ret = CMD_UNDEFINED;
497 goto end;
498 }
499 }
500
501 opt_channels = (char*) poptGetArg(pc);
502 if (opt_channels == NULL) {
503 ERR("Missing channel name.\n");
504 usage(stderr);
505 ret = CMD_ERROR;
506 goto end;
507 }
508
509 if (!opt_session_name) {
510 session_name = get_session_name();
511 if (session_name == NULL) {
512 ret = CMD_ERROR;
513 goto end;
514 }
515 } else {
516 session_name = opt_session_name;
517 }
518
519 ret = enable_channel(session_name);
520
521 end:
522 if (!opt_session_name && session_name) {
523 free(session_name);
524 }
525 poptFreeContext(pc);
526 return ret;
527 }
This page took 0.040984 seconds and 4 git commands to generate.