6b41e875affd59b68fa44af1b8f2d5481fd03fe6
[lttng-tools.git] / lttng / commands / add_channel.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; either version 2
7 * of the License, or (at your option) any later version.
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 "config.h"
30 #include "utils.h"
31
32 static char *opt_channel_name;
33 static char *opt_kernel;
34 static char *opt_cmd_name;
35 static int opt_pid_all;
36 static int opt_userspace;
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 poptOption long_options[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
54 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
55 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0},
56 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
57 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
58 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
59 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
60 {"subbuf_size", 0, POPT_ARG_DOUBLE, 0, OPT_SUBBUF_SIZE, 0, 0},
61 {"num_subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
62 {"switch_timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
63 {"read_timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
64 {0, 0, 0, 0, 0, 0, 0}
65 };
66
67 /*
68 * usage
69 */
70 static void usage(FILE *ofp)
71 {
72 fprintf(ofp, "usage: lttng add-channel NAME [options] [channel_options]\n");
73 fprintf(ofp, "\n");
74 fprintf(ofp, " -h, --help Show this help\n");
75 fprintf(ofp, " -k, --kernel Apply on the kernel tracer\n");
76 fprintf(ofp, " -u, --userspace [CMD] Apply on the user-space tracer\n");
77 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
78 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
79 fprintf(ofp, "\n");
80 fprintf(ofp, "Channel options:\n");
81 fprintf(ofp, " --discard Discard event when buffers are full (default)\n");
82 fprintf(ofp, " --overwrite Flight recorder mode\n");
83 fprintf(ofp, " --subbuf_size Subbuffer size in bytes (default: 4096)\n");
84 fprintf(ofp, " --num_subbuf Number of subbufers (default: 2)\n");
85 fprintf(ofp, " --switch_timer Switch timer interval in usec (default: 0)\n");
86 fprintf(ofp, " --read_timer Read timer interval in usec (default: 200)\n");
87 fprintf(ofp, "\n");
88 }
89
90 /*
91 * add_channel
92 *
93 * Adding channel using the lttng API.
94 */
95 static int add_channel(void)
96 {
97 int ret = CMD_SUCCESS;
98
99 if (set_session_name() < 0) {
100 ret = CMD_ERROR;
101 goto error;
102 }
103
104 /* Copy channel name and normalize it */
105 strncpy(chan.name, opt_channel_name, NAME_MAX);
106 chan.name[NAME_MAX - 1] = '\0';
107
108 /* Kernel tracer action */
109 if (opt_kernel) {
110 /* Create kernel channel */
111 ret = lttng_kernel_create_channel(&chan);
112 if (ret < 0) {
113 goto error;
114 }
115 } else if (opt_userspace) { /* User-space tracer action */
116 /*
117 * TODO: Waiting on lttng UST 2.0
118 */
119 if (opt_pid_all) {
120 } else if (opt_pid != 0) {
121 }
122 ret = CMD_NOT_IMPLEMENTED;
123 goto error;
124 } else {
125 ERR("Please specify a tracer (kernel or user-space)");
126 goto error;
127 }
128
129 MSG("Channel %s created", opt_channel_name);
130
131 error:
132 return ret;
133 }
134
135 /*
136 * init_channel_config
137 *
138 * Default value for channel configuration.
139 */
140 static void init_channel_config(void)
141 {
142 chan.attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
143 chan.attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
144 chan.attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
145 chan.attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
146 chan.attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
147 }
148
149 /*
150 * cmd_add_channel
151 *
152 * Add channel to trace session
153 */
154 int cmd_add_channel(int argc, const char **argv)
155 {
156 int opt, ret;
157 static poptContext pc;
158
159 init_channel_config();
160
161 pc = poptGetContext(NULL, argc, argv, long_options, 0);
162 poptReadDefaultConfig(pc, 0);
163
164 while ((opt = poptGetNextOpt(pc)) != -1) {
165 switch (opt) {
166 case OPT_HELP:
167 usage(stderr);
168 ret = CMD_SUCCESS;
169 goto end;
170 case OPT_USERSPACE:
171 opt_userspace = 1;
172 opt_cmd_name = poptGetOptArg(pc);
173 break;
174 case OPT_DISCARD:
175 chan.attr.overwrite = 0;
176 DBG("Channel set to discard");
177 break;
178 case OPT_OVERWRITE:
179 chan.attr.overwrite = 1;
180 DBG("Channel set to overwrite");
181 break;
182 case OPT_SUBBUF_SIZE:
183 chan.attr.subbuf_size = atol(poptGetOptArg(pc));
184 DBG("Channel subbuf size set to %lu", chan.attr.subbuf_size);
185 break;
186 case OPT_NUM_SUBBUF:
187 chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
188 DBG("Channel subbuf num set to %lu", chan.attr.num_subbuf);
189 break;
190 case OPT_SWITCH_TIMER:
191 chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc));
192 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
193 break;
194 case OPT_READ_TIMER:
195 chan.attr.read_timer_interval = atoi(poptGetOptArg(pc));
196 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
197 break;
198 default:
199 usage(stderr);
200 ret = CMD_UNDEFINED;
201 goto end;
202 }
203 }
204
205 opt_channel_name = (char*) poptGetArg(pc);
206 if (opt_channel_name == NULL) {
207 ERR("Missing channel name.\n");
208 usage(stderr);
209 ret = CMD_SUCCESS;
210 goto end;
211 }
212
213 ret = add_channel();
214
215 end:
216 return ret;
217 }
This page took 0.03298 seconds and 3 git commands to generate.