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