Make "please specify tracer" comment more helpful
[lttng-tools.git] / 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
6e2d116c
DG
29#include "../cmd.h"
30#include "../conf.h"
31#include "../utils.h"
d36b8583
DG
32
33static char *opt_channels;
34static char *opt_kernel;
7d29a247 35static char *opt_cmd_name;
5440dc42 36static char *opt_session_name;
d36b8583
DG
37static int opt_pid_all;
38static int opt_userspace;
39static pid_t opt_pid;
7d29a247 40static struct lttng_channel chan;
d36b8583
DG
41
42enum {
43 OPT_HELP = 1,
7d29a247
DG
44 OPT_DISCARD,
45 OPT_OVERWRITE,
46 OPT_SUBBUF_SIZE,
47 OPT_NUM_SUBBUF,
48 OPT_SWITCH_TIMER,
49 OPT_READ_TIMER,
d36b8583
DG
50 OPT_USERSPACE,
51};
52
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},
7d29a247 58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0},
d36b8583
DG
59 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
60 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
7d29a247
DG
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},
d36b8583
DG
67 {0, 0, 0, 0, 0, 0, 0}
68};
69
70/*
71 * usage
72 */
73static void usage(FILE *ofp)
74{
7d29a247 75 fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [options] [channel_options]\n");
d36b8583 76 fprintf(ofp, "\n");
7d29a247 77 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 78 fprintf(ofp, " -s, --session Apply on session name\n");
7d29a247
DG
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");
d36b8583
DG
91 fprintf(ofp, "\n");
92}
93
94/*
7d29a247 95 * enable_channel
d36b8583 96 *
7d29a247 97 * Adding channel using the lttng API.
d36b8583 98 */
7d29a247 99static int enable_channel(void)
d36b8583
DG
100{
101 int ret = CMD_SUCCESS;
102 char *channel_name;
7d29a247 103 struct lttng_domain dom;
d36b8583 104
5440dc42 105 if (set_session_name(opt_session_name) < 0) {
d36b8583
DG
106 ret = CMD_ERROR;
107 goto error;
108 }
109
7d29a247
DG
110 if (opt_kernel) {
111 dom.type = LTTNG_DOMAIN_KERNEL;
112 }
113
d36b8583
DG
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);
7d29a247
DG
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);
d36b8583
DG
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 {
048e2159 141 ERR("Please specify a tracer (--kernel or --userspace)");
d36b8583
DG
142 goto error;
143 }
144
145 /* Next event */
146 channel_name = strtok(NULL, ",");
147 }
148
149error:
150 return ret;
151}
152
153/*
7d29a247 154 * init_channel_config
d36b8583 155 *
7d29a247
DG
156 * Default value for channel configuration.
157 */
158static 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
d36b8583
DG
170 */
171int cmd_enable_channels(int argc, const char **argv)
172{
173 int opt, ret;
174 static poptContext pc;
175
7d29a247
DG
176 init_channel_config();
177
d36b8583
DG
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;
7d29a247
DG
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));
5a0de755 201 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
7d29a247
DG
202 break;
203 case OPT_NUM_SUBBUF:
204 chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
5a0de755 205 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
7d29a247
DG
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);
d36b8583
DG
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) {
7d29a247 224 ERR("Missing channel name.\n");
d36b8583
DG
225 usage(stderr);
226 ret = CMD_SUCCESS;
227 goto end;
228 }
229
7d29a247 230 ret = enable_channel();
d36b8583
DG
231
232end:
233 return ret;
234}
This page took 0.034319 seconds and 4 git commands to generate.