1bdf60667964ff9d75614dd1e60100b5e8b9537b
[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 #include <inttypes.h>
28
29 #include "../cmd.h"
30 #include "../conf.h"
31 #include "../utils.h"
32
33 static char *opt_channels;
34 static char *opt_kernel;
35 static char *opt_cmd_name;
36 static char *opt_session_name;
37 static int opt_pid_all;
38 static int opt_userspace;
39 static pid_t opt_pid;
40 static struct lttng_channel chan;
41
42 enum {
43 OPT_HELP = 1,
44 OPT_DISCARD,
45 OPT_OVERWRITE,
46 OPT_SUBBUF_SIZE,
47 OPT_NUM_SUBBUF,
48 OPT_SWITCH_TIMER,
49 OPT_READ_TIMER,
50 OPT_USERSPACE,
51 };
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, 0, OPT_USERSPACE, 0, 0},
59 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
60 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
61 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
62 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
63 {"subbuf-size", 0, POPT_ARG_DOUBLE, 0, OPT_SUBBUF_SIZE, 0, 0},
64 {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
65 {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
66 {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
67 {0, 0, 0, 0, 0, 0, 0}
68 };
69
70 /*
71 * usage
72 */
73 static void usage(FILE *ofp)
74 {
75 fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [options] [channel_options]\n");
76 fprintf(ofp, "\n");
77 fprintf(ofp, " -h, --help Show this help\n");
78 fprintf(ofp, " -s, --session Apply on session name\n");
79 fprintf(ofp, " -k, --kernel Apply on the kernel tracer\n");
80 fprintf(ofp, " -u, --userspace [CMD] Apply on the user-space tracer\n");
81 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
82 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
83 fprintf(ofp, "\n");
84 fprintf(ofp, "Channel options:\n");
85 fprintf(ofp, " --discard Discard event when buffers are full (default)\n");
86 fprintf(ofp, " --overwrite Flight recorder mode\n");
87 fprintf(ofp, " --subbuf-size Subbuffer size in bytes (default: 4096)\n");
88 fprintf(ofp, " --num-subbuf Number of subbufers (default: 2)\n");
89 fprintf(ofp, " --switch-timer Switch timer interval in usec (default: 0)\n");
90 fprintf(ofp, " --read-timer Read timer interval in usec (default: 200)\n");
91 fprintf(ofp, "\n");
92 }
93
94 /*
95 * enable_channel
96 *
97 * Adding channel using the lttng API.
98 */
99 static int enable_channel(void)
100 {
101 int ret = CMD_SUCCESS;
102 char *channel_name;
103 struct lttng_domain dom;
104
105 if (set_session_name(opt_session_name) < 0) {
106 ret = CMD_ERROR;
107 goto error;
108 }
109
110 if (opt_kernel) {
111 dom.type = LTTNG_DOMAIN_KERNEL;
112 }
113
114 /* Strip event list */
115 channel_name = strtok(opt_channels, ",");
116 while (channel_name != NULL) {
117 /* Kernel tracer action */
118 if (opt_kernel) {
119 DBG("Enabling kernel channel %s", channel_name);
120
121 /* Copy channel name and normalize it */
122 strncpy(chan.name, channel_name, NAME_MAX);
123 chan.name[NAME_MAX - 1] = '\0';
124
125 ret = lttng_enable_channel(&dom, &chan);
126 if (ret < 0) {
127 goto error;
128 } else {
129 MSG("Kernel channel enabled %s", channel_name);
130 }
131 } else if (opt_userspace) { /* User-space tracer action */
132 /*
133 * TODO: Waiting on lttng UST 2.0
134 */
135 if (opt_pid_all) {
136 } else if (opt_pid != 0) {
137 }
138 ret = CMD_NOT_IMPLEMENTED;
139 goto error;
140 } else {
141 ERR("Please specify a tracer (--kernel or --userspace)");
142 goto error;
143 }
144
145 /* Next event */
146 channel_name = strtok(NULL, ",");
147 }
148
149 error:
150 return ret;
151 }
152
153 /*
154 * init_channel_config
155 *
156 * Default value for channel configuration.
157 */
158 static void init_channel_config(void)
159 {
160 chan.attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
161 chan.attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
162 chan.attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
163 chan.attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
164 chan.attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
165 chan.attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
166 }
167
168 /*
169 * Add channel to trace session
170 */
171 int cmd_enable_channels(int argc, const char **argv)
172 {
173 int opt, ret;
174 static poptContext pc;
175
176 init_channel_config();
177
178 pc = poptGetContext(NULL, argc, argv, long_options, 0);
179 poptReadDefaultConfig(pc, 0);
180
181 while ((opt = poptGetNextOpt(pc)) != -1) {
182 switch (opt) {
183 case OPT_HELP:
184 usage(stderr);
185 ret = CMD_SUCCESS;
186 goto end;
187 case OPT_USERSPACE:
188 opt_userspace = 1;
189 opt_cmd_name = poptGetOptArg(pc);
190 break;
191 case OPT_DISCARD:
192 chan.attr.overwrite = 0;
193 DBG("Channel set to discard");
194 break;
195 case OPT_OVERWRITE:
196 chan.attr.overwrite = 1;
197 DBG("Channel set to overwrite");
198 break;
199 case OPT_SUBBUF_SIZE:
200 chan.attr.subbuf_size = atol(poptGetOptArg(pc));
201 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
202 break;
203 case OPT_NUM_SUBBUF:
204 chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
205 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
206 break;
207 case OPT_SWITCH_TIMER:
208 chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc));
209 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
210 break;
211 case OPT_READ_TIMER:
212 chan.attr.read_timer_interval = atoi(poptGetOptArg(pc));
213 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
214 break;
215 default:
216 usage(stderr);
217 ret = CMD_UNDEFINED;
218 goto end;
219 }
220 }
221
222 opt_channels = (char*) poptGetArg(pc);
223 if (opt_channels == NULL) {
224 ERR("Missing channel name.\n");
225 usage(stderr);
226 ret = CMD_SUCCESS;
227 goto end;
228 }
229
230 ret = enable_channel();
231
232 end:
233 return ret;
234 }
This page took 0.034039 seconds and 4 git commands to generate.