Fix invalid channel creation
[lttng-tools.git] / 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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "cmd.h"
29 #include "conf.h"
30 #include "utils.h"
31
32 static char *opt_channels;
33 static char *opt_kernel;
34 static char *opt_cmd_name;
35 static char *opt_session_name;
36 static int opt_pid_all;
37 static int opt_userspace;
38 static pid_t opt_pid;
39 static struct lttng_channel chan;
40
41 enum {
42 OPT_HELP = 1,
43 OPT_DISCARD,
44 OPT_OVERWRITE,
45 OPT_SUBBUF_SIZE,
46 OPT_NUM_SUBBUF,
47 OPT_SWITCH_TIMER,
48 OPT_READ_TIMER,
49 OPT_USERSPACE,
50 };
51
52 static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
55 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
56 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
57 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0},
58 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
59 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
60 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
61 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
62 {"subbuf_size", 0, POPT_ARG_DOUBLE, 0, OPT_SUBBUF_SIZE, 0, 0},
63 {"num_subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
64 {"switch_timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
65 {"read_timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
66 {0, 0, 0, 0, 0, 0, 0}
67 };
68
69 /*
70 * usage
71 */
72 static void usage(FILE *ofp)
73 {
74 fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [options] [channel_options]\n");
75 fprintf(ofp, "\n");
76 fprintf(ofp, " -h, --help Show this help\n");
77 fprintf(ofp, " -s, --session Apply on session name\n");
78 fprintf(ofp, " -k, --kernel Apply on the kernel tracer\n");
79 fprintf(ofp, " -u, --userspace [CMD] Apply on the user-space tracer\n");
80 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
81 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
82 fprintf(ofp, "\n");
83 fprintf(ofp, "Channel options:\n");
84 fprintf(ofp, " --discard Discard event when buffers are full (default)\n");
85 fprintf(ofp, " --overwrite Flight recorder mode\n");
86 fprintf(ofp, " --subbuf_size Subbuffer size in bytes (default: 4096)\n");
87 fprintf(ofp, " --num_subbuf Number of subbufers (default: 2)\n");
88 fprintf(ofp, " --switch_timer Switch timer interval in usec (default: 0)\n");
89 fprintf(ofp, " --read_timer Read timer interval in usec (default: 200)\n");
90 fprintf(ofp, "\n");
91 }
92
93 /*
94 * enable_channel
95 *
96 * Adding channel using the lttng API.
97 */
98 static int enable_channel(void)
99 {
100 int ret = CMD_SUCCESS;
101 char *channel_name;
102 struct lttng_domain dom;
103
104 if (set_session_name(opt_session_name) < 0) {
105 ret = CMD_ERROR;
106 goto error;
107 }
108
109 if (opt_kernel) {
110 dom.type = LTTNG_DOMAIN_KERNEL;
111 }
112
113 /* Strip event list */
114 channel_name = strtok(opt_channels, ",");
115 while (channel_name != NULL) {
116 /* Kernel tracer action */
117 if (opt_kernel) {
118 DBG("Enabling kernel channel %s", channel_name);
119
120 /* Copy channel name and normalize it */
121 strncpy(chan.name, channel_name, NAME_MAX);
122 chan.name[NAME_MAX - 1] = '\0';
123
124 ret = lttng_enable_channel(&dom, &chan);
125 if (ret < 0) {
126 goto error;
127 } else {
128 MSG("Kernel channel enabled %s", channel_name);
129 }
130 } else if (opt_userspace) { /* User-space tracer action */
131 /*
132 * TODO: Waiting on lttng UST 2.0
133 */
134 if (opt_pid_all) {
135 } else if (opt_pid != 0) {
136 }
137 ret = CMD_NOT_IMPLEMENTED;
138 goto error;
139 } else {
140 ERR("Please specify a tracer (kernel or user-space)");
141 goto error;
142 }
143
144 /* Next event */
145 channel_name = strtok(NULL, ",");
146 }
147
148 error:
149 return ret;
150 }
151
152 /*
153 * init_channel_config
154 *
155 * Default value for channel configuration.
156 */
157 static void init_channel_config(void)
158 {
159 chan.attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
160 chan.attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
161 chan.attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
162 chan.attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
163 chan.attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
164 chan.attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
165 }
166
167 /*
168 * Add channel to trace session
169 */
170 int cmd_enable_channels(int argc, const char **argv)
171 {
172 int opt, ret;
173 static poptContext pc;
174
175 init_channel_config();
176
177 pc = poptGetContext(NULL, argc, argv, long_options, 0);
178 poptReadDefaultConfig(pc, 0);
179
180 while ((opt = poptGetNextOpt(pc)) != -1) {
181 switch (opt) {
182 case OPT_HELP:
183 usage(stderr);
184 ret = CMD_SUCCESS;
185 goto end;
186 case OPT_USERSPACE:
187 opt_userspace = 1;
188 opt_cmd_name = poptGetOptArg(pc);
189 break;
190 case OPT_DISCARD:
191 chan.attr.overwrite = 0;
192 DBG("Channel set to discard");
193 break;
194 case OPT_OVERWRITE:
195 chan.attr.overwrite = 1;
196 DBG("Channel set to overwrite");
197 break;
198 case OPT_SUBBUF_SIZE:
199 chan.attr.subbuf_size = atol(poptGetOptArg(pc));
200 DBG("Channel subbuf size set to %lu", chan.attr.subbuf_size);
201 break;
202 case OPT_NUM_SUBBUF:
203 chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
204 DBG("Channel subbuf num set to %lu", chan.attr.num_subbuf);
205 break;
206 case OPT_SWITCH_TIMER:
207 chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc));
208 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
209 break;
210 case OPT_READ_TIMER:
211 chan.attr.read_timer_interval = atoi(poptGetOptArg(pc));
212 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
213 break;
214 default:
215 usage(stderr);
216 ret = CMD_UNDEFINED;
217 goto end;
218 }
219 }
220
221 opt_channels = (char*) poptGetArg(pc);
222 if (opt_channels == NULL) {
223 ERR("Missing channel name.\n");
224 usage(stderr);
225 ret = CMD_SUCCESS;
226 goto end;
227 }
228
229 ret = enable_channel();
230
231 end:
232 return ret;
233 }
This page took 0.034509 seconds and 4 git commands to generate.