Rename cmd.h for command.h
[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
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 #include <inttypes.h>
28
29 #include "../command.h"
30
31 static char *opt_channels;
32 static int opt_kernel;
33 static char *opt_cmd_name;
34 static char *opt_session_name;
35 static int opt_userspace;
36 static char *opt_cmd_name;
37 static pid_t opt_pid;
38 static struct lttng_channel chan;
39
40 enum {
41 OPT_HELP = 1,
42 OPT_DISCARD,
43 OPT_OVERWRITE,
44 OPT_SUBBUF_SIZE,
45 OPT_NUM_SUBBUF,
46 OPT_SWITCH_TIMER,
47 OPT_READ_TIMER,
48 OPT_USERSPACE,
49 };
50
51 static struct lttng_handle *handle;
52
53 static struct poptOption long_options[] = {
54 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
55 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
56 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
57 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 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 for the user-space tracer\n");
80 fprintf(ofp, " If no CMD, the domain used is UST global\n");
81 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
82 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
83 fprintf(ofp, "\n");
84 fprintf(ofp, "Channel options:\n");
85 fprintf(ofp, " --discard Discard event when buffers are full%s\n",
86 DEFAULT_CHANNEL_OVERWRITE ? "" : " (default)");
87 fprintf(ofp, " --overwrite Flight recorder mode%s\n",
88 DEFAULT_CHANNEL_OVERWRITE ? " (default)" : "");
89 fprintf(ofp, " --subbuf-size Subbuffer size in bytes\n");
90 fprintf(ofp, " (default: %u, kernel default: %u)\n",
91 DEFAULT_CHANNEL_SUBBUF_SIZE,
92 DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE);
93 fprintf(ofp, " --num-subbuf Number of subbufers\n");
94 fprintf(ofp, " (default: %u, kernel default: %u)\n",
95 DEFAULT_CHANNEL_SUBBUF_NUM,
96 DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM);
97 fprintf(ofp, " --switch-timer Switch timer interval in usec (default: %u)\n",
98 DEFAULT_CHANNEL_SWITCH_TIMER);
99 fprintf(ofp, " --read-timer Read timer interval in usec (default: %u)\n",
100 DEFAULT_CHANNEL_READ_TIMER);
101 fprintf(ofp, "\n");
102 }
103
104 /*
105 * Set default attributes depending on those already defined from the command
106 * line.
107 */
108 static void set_default_attr(struct lttng_domain *dom)
109 {
110 struct lttng_channel_attr default_attr;
111
112 /* Set attributes */
113 lttng_channel_set_default_attr(dom, &default_attr);
114
115 if (chan.attr.overwrite == -1) {
116 chan.attr.overwrite = default_attr.overwrite;
117 }
118 if (chan.attr.subbuf_size == -1) {
119 chan.attr.subbuf_size = default_attr.subbuf_size;
120 }
121 if (chan.attr.num_subbuf == -1) {
122 chan.attr.num_subbuf = default_attr.num_subbuf;
123 }
124 if (chan.attr.switch_timer_interval == -1) {
125 chan.attr.switch_timer_interval = default_attr.switch_timer_interval;
126 }
127 if (chan.attr.read_timer_interval == -1) {
128 chan.attr.read_timer_interval = default_attr.read_timer_interval;
129 }
130 if (chan.attr.output == -1) {
131 chan.attr.output = default_attr.output;
132 }
133 }
134
135 /*
136 * Adding channel using the lttng API.
137 */
138 static int enable_channel(char *session_name)
139 {
140 int ret = CMD_SUCCESS;
141 char *channel_name;
142 struct lttng_domain dom;
143
144 if (opt_kernel) {
145 dom.type = LTTNG_DOMAIN_KERNEL;
146 } else if (opt_pid != 0) {
147 dom.type = LTTNG_DOMAIN_UST_PID;
148 dom.attr.pid = opt_pid;
149 DBG("PID %d set to lttng handle", opt_pid);
150 } else if (opt_userspace && opt_cmd_name == NULL) {
151 dom.type = LTTNG_DOMAIN_UST;
152 } else if (opt_userspace && opt_cmd_name != NULL) {
153 dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
154 strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
155 } else {
156 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
157 ret = CMD_NOT_IMPLEMENTED;
158 goto error;
159 }
160
161 set_default_attr(&dom);
162
163 handle = lttng_create_handle(session_name, &dom);
164 if (handle == NULL) {
165 ret = -1;
166 goto error;
167 }
168
169 /* Strip channel list (format: chan1,chan2,...) */
170 channel_name = strtok(opt_channels, ",");
171 while (channel_name != NULL) {
172 /* Copy channel name and normalize it */
173 strncpy(chan.name, channel_name, NAME_MAX);
174 chan.name[NAME_MAX - 1] = '\0';
175
176 DBG("Enabling channel %s", channel_name);
177
178 ret = lttng_enable_channel(handle, &chan);
179 if (ret < 0) {
180 goto error;
181 } else {
182 MSG("%s channel %s enabled for session %s",
183 opt_kernel ? "Kernel" : "UST", channel_name,
184 session_name);
185 }
186
187 /* Next event */
188 channel_name = strtok(NULL, ",");
189 }
190
191 error:
192 lttng_destroy_handle(handle);
193
194 return ret;
195 }
196
197 /*
198 * Default value for channel configuration.
199 */
200 static void init_channel_config(void)
201 {
202 /*
203 * Put -1 everywhere so we can identify those set by the command line and
204 * those needed to be set by the default values.
205 */
206 memset(&chan.attr, -1, sizeof(chan.attr));
207 }
208
209 /*
210 * Add channel to trace session
211 */
212 int cmd_enable_channels(int argc, const char **argv)
213 {
214 int opt, ret;
215 static poptContext pc;
216 char *session_name = NULL;
217
218 init_channel_config();
219
220 pc = poptGetContext(NULL, argc, argv, long_options, 0);
221 poptReadDefaultConfig(pc, 0);
222
223 while ((opt = poptGetNextOpt(pc)) != -1) {
224 switch (opt) {
225 case OPT_HELP:
226 usage(stderr);
227 ret = CMD_SUCCESS;
228 goto end;
229 case OPT_DISCARD:
230 chan.attr.overwrite = 0;
231 DBG("Channel set to discard");
232 break;
233 case OPT_OVERWRITE:
234 chan.attr.overwrite = 1;
235 DBG("Channel set to overwrite");
236 break;
237 case OPT_SUBBUF_SIZE:
238 chan.attr.subbuf_size = atol(poptGetOptArg(pc));
239 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
240 break;
241 case OPT_NUM_SUBBUF:
242 chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
243 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
244 break;
245 case OPT_SWITCH_TIMER:
246 chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc));
247 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
248 break;
249 case OPT_READ_TIMER:
250 chan.attr.read_timer_interval = atoi(poptGetOptArg(pc));
251 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
252 break;
253 case OPT_USERSPACE:
254 opt_userspace = 1;
255 break;
256 default:
257 usage(stderr);
258 ret = CMD_UNDEFINED;
259 goto end;
260 }
261 }
262
263 opt_channels = (char*) poptGetArg(pc);
264 if (opt_channels == NULL) {
265 ERR("Missing channel name.\n");
266 usage(stderr);
267 ret = CMD_SUCCESS;
268 goto end;
269 }
270
271 if (!opt_session_name) {
272 session_name = get_session_name();
273 if (session_name == NULL) {
274 ret = -1;
275 goto end;
276 }
277 } else {
278 session_name = opt_session_name;
279 }
280
281 ret = enable_channel(session_name);
282
283 end:
284 return ret;
285 }
This page took 0.036115 seconds and 5 git commands to generate.