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