Update version to 2.0-pre18
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.c
CommitLineData
d36b8583
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
d36b8583
DG
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>
5a0de755 27#include <inttypes.h>
d36b8583 28
c399183f 29#include "../command.h"
d36b8583
DG
30
31static char *opt_channels;
5edd7e09 32static int opt_kernel;
7d29a247 33static char *opt_cmd_name;
5440dc42 34static char *opt_session_name;
d36b8583 35static int opt_userspace;
eeac7d46 36static char *opt_cmd_name;
d36b8583 37static pid_t opt_pid;
7d29a247 38static struct lttng_channel chan;
d36b8583
DG
39
40enum {
41 OPT_HELP = 1,
7d29a247
DG
42 OPT_DISCARD,
43 OPT_OVERWRITE,
44 OPT_SUBBUF_SIZE,
45 OPT_NUM_SUBBUF,
46 OPT_SWITCH_TIMER,
47 OPT_READ_TIMER,
d36b8583
DG
48 OPT_USERSPACE,
49};
50
cd80958d
DG
51static struct lttng_handle *handle;
52
d36b8583
DG
53static struct poptOption long_options[] = {
54 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
55 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 56 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
d36b8583 57 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
6caa2bcc 58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
d36b8583 59 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
7d29a247
DG
60 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
61 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
1b579521
DG
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},
d36b8583
DG
66 {0, 0, 0, 0, 0, 0, 0}
67};
68
69/*
70 * usage
71 */
72static void usage(FILE *ofp)
73{
7d29a247 74 fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [options] [channel_options]\n");
d36b8583 75 fprintf(ofp, "\n");
7d29a247 76 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 77 fprintf(ofp, " -s, --session Apply on session name\n");
7d29a247 78 fprintf(ofp, " -k, --kernel Apply on the kernel tracer\n");
e14f64a8
DG
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");
7d29a247
DG
83 fprintf(ofp, "\n");
84 fprintf(ofp, "Channel options:\n");
cabd9892
MD
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);
d36b8583
DG
101 fprintf(ofp, "\n");
102}
103
5edd7e09
DG
104/*
105 * Set default attributes depending on those already defined from the command
106 * line.
107 */
108static 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
d36b8583 135/*
0fdd1e2c 136 * Adding channel using the lttng API.
d36b8583 137 */
cd80958d 138static int enable_channel(char *session_name)
d36b8583
DG
139{
140 int ret = CMD_SUCCESS;
141 char *channel_name;
7d29a247 142 struct lttng_domain dom;
d36b8583 143
7d29a247
DG
144 if (opt_kernel) {
145 dom.type = LTTNG_DOMAIN_KERNEL;
0fdd1e2c
DG
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);
f6a9efaa
DG
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);
0fdd1e2c 155 } else {
e14f64a8 156 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
0fdd1e2c
DG
157 ret = CMD_NOT_IMPLEMENTED;
158 goto error;
7d29a247
DG
159 }
160
5edd7e09
DG
161 set_default_attr(&dom);
162
cd80958d
DG
163 handle = lttng_create_handle(session_name, &dom);
164 if (handle == NULL) {
165 ret = -1;
166 goto error;
167 }
168
0fdd1e2c 169 /* Strip channel list (format: chan1,chan2,...) */
d36b8583
DG
170 channel_name = strtok(opt_channels, ",");
171 while (channel_name != NULL) {
0fdd1e2c
DG
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) {
d36b8583
DG
180 goto error;
181 } else {
7885e399
DG
182 MSG("%s channel %s enabled for session %s",
183 opt_kernel ? "Kernel" : "UST", channel_name,
184 session_name);
d36b8583
DG
185 }
186
187 /* Next event */
188 channel_name = strtok(NULL, ",");
189 }
190
191error:
cd80958d
DG
192 lttng_destroy_handle(handle);
193
d36b8583
DG
194 return ret;
195}
196
197/*
0fdd1e2c 198 * Default value for channel configuration.
7d29a247
DG
199 */
200static void init_channel_config(void)
201{
5edd7e09
DG
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));
7d29a247
DG
207}
208
209/*
0fdd1e2c 210 * Add channel to trace session
d36b8583
DG
211 */
212int cmd_enable_channels(int argc, const char **argv)
213{
214 int opt, ret;
215 static poptContext pc;
cd80958d 216 char *session_name = NULL;
d36b8583 217
7d29a247
DG
218 init_channel_config();
219
d36b8583
DG
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;
7d29a247
DG
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));
5a0de755 239 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
7d29a247
DG
240 break;
241 case OPT_NUM_SUBBUF:
242 chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
5a0de755 243 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
7d29a247
DG
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);
d36b8583 252 break;
eeac7d46
MD
253 case OPT_USERSPACE:
254 opt_userspace = 1;
eeac7d46 255 break;
d36b8583
DG
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) {
7d29a247 265 ERR("Missing channel name.\n");
d36b8583
DG
266 usage(stderr);
267 ret = CMD_SUCCESS;
268 goto end;
269 }
270
cd80958d
DG
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);
d36b8583
DG
282
283end:
284 return ret;
285}
This page took 0.042047 seconds and 5 git commands to generate.