58a36b97c8b2f54085ed0b2ad463ef005c68d53a
[lttng-tools.git] / src / bin / lttng-sessiond / 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 modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <common/common.h>
23 #include <common/defaults.h>
24 #include <common/sessiond-comm/sessiond-comm.h>
25
26 #include "channel.h"
27 #include "kernel.h"
28 #include "ust-ctl.h"
29 #include "utils.h"
30 #include "ust-app.h"
31
32 /*
33 * Return allocated channel attributes.
34 */
35 struct lttng_channel *channel_new_default_attr(int dom)
36 {
37 struct lttng_channel *chan;
38
39 chan = zmalloc(sizeof(struct lttng_channel));
40 if (chan == NULL) {
41 PERROR("zmalloc channel init");
42 goto error_alloc;
43 }
44
45 if (snprintf(chan->name, sizeof(chan->name), "%s",
46 DEFAULT_CHANNEL_NAME) < 0) {
47 PERROR("snprintf default channel name");
48 goto error;
49 }
50
51 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
52 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
53 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
54
55 switch (dom) {
56 case LTTNG_DOMAIN_KERNEL:
57 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
58 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
59 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
60 break;
61 case LTTNG_DOMAIN_UST:
62 #if 0
63 case LTTNG_DOMAIN_UST_PID:
64 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
65 case LTTNG_DOMAIN_UST_EXEC_NAME:
66 #endif
67 chan->attr.subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
68 chan->attr.num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
69 chan->attr.output = DEFAULT_UST_CHANNEL_OUTPUT;
70 break;
71 default:
72 goto error; /* Not implemented */
73 }
74
75 return chan;
76
77 error:
78 free(chan);
79 error_alloc:
80 return NULL;
81 }
82
83 /*
84 * Disable kernel channel of the kernel session.
85 */
86 int channel_kernel_disable(struct ltt_kernel_session *ksession,
87 char *channel_name)
88 {
89 int ret;
90 struct ltt_kernel_channel *kchan;
91
92 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
93 if (kchan == NULL) {
94 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
95 goto error;
96 } else if (kchan->enabled == 1) {
97 ret = kernel_disable_channel(kchan);
98 if (ret < 0 && ret != -EEXIST) {
99 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
100 goto error;
101 }
102 }
103
104 ret = LTTCOMM_OK;
105
106 error:
107 return ret;
108 }
109
110 /*
111 * Enable kernel channel of the kernel session.
112 */
113 int channel_kernel_enable(struct ltt_kernel_session *ksession,
114 struct ltt_kernel_channel *kchan)
115 {
116 int ret;
117
118 if (kchan->enabled == 0) {
119 ret = kernel_enable_channel(kchan);
120 if (ret < 0) {
121 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
122 goto error;
123 }
124 }
125
126 ret = LTTCOMM_OK;
127
128 error:
129 return ret;
130 }
131
132 /*
133 * Create kernel channel of the kernel session and notify kernel thread.
134 */
135 int channel_kernel_create(struct ltt_kernel_session *ksession,
136 struct lttng_channel *attr, int kernel_pipe)
137 {
138 int ret;
139 struct lttng_channel *defattr = NULL;
140
141 /* Creating channel attributes if needed */
142 if (attr == NULL) {
143 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL);
144 if (defattr == NULL) {
145 ret = LTTCOMM_FATAL;
146 goto error;
147 }
148 attr = defattr;
149 }
150
151 /* Channel not found, creating it */
152 ret = kernel_create_channel(ksession, attr, ksession->trace_path);
153 if (ret < 0) {
154 ret = LTTCOMM_KERN_CHAN_FAIL;
155 goto error;
156 }
157
158 /* Notify kernel thread that there is a new channel */
159 ret = notify_thread_pipe(kernel_pipe);
160 if (ret < 0) {
161 ret = LTTCOMM_FATAL;
162 goto error;
163 }
164
165 ret = LTTCOMM_OK;
166 error:
167 free(defattr);
168 return ret;
169 }
170
171 /*
172 * Enable UST channel for session and domain.
173 */
174 int channel_ust_enable(struct ltt_ust_session *usess, int domain,
175 struct ltt_ust_channel *uchan)
176 {
177 int ret = LTTCOMM_OK;
178
179 /* If already enabled, everything is OK */
180 if (uchan->enabled) {
181 DBG3("Channel %s already enabled. Skipping", uchan->name);
182 goto end;
183 }
184
185 switch (domain) {
186 case LTTNG_DOMAIN_UST:
187 DBG2("Channel %s being enabled in UST global domain", uchan->name);
188 /* Enable channel for global domain */
189 ret = ust_app_enable_channel_glb(usess, uchan);
190 break;
191 #if 0
192 case LTTNG_DOMAIN_UST_PID:
193 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
194 case LTTNG_DOMAIN_UST_EXEC_NAME:
195 #endif
196 default:
197 ret = LTTCOMM_UND;
198 goto error;
199 }
200
201 if (ret < 0) {
202 if (ret != -EEXIST) {
203 ret = LTTCOMM_UST_CHAN_ENABLE_FAIL;
204 goto error;
205 } else {
206 ret = LTTCOMM_OK;
207 }
208 }
209
210 uchan->enabled = 1;
211 DBG2("Channel %s enabled successfully", uchan->name);
212
213 end:
214 error:
215 return ret;
216 }
217
218 /*
219 * Create UST channel for session and domain.
220 */
221 int channel_ust_create(struct ltt_ust_session *usess, int domain,
222 struct lttng_channel *attr)
223 {
224 int ret = LTTCOMM_OK;
225 struct ltt_ust_channel *uchan = NULL;
226 struct lttng_channel *defattr = NULL;
227
228 /* Creating channel attributes if needed */
229 if (attr == NULL) {
230 defattr = channel_new_default_attr(domain);
231 if (defattr == NULL) {
232 ret = LTTCOMM_FATAL;
233 goto error;
234 }
235 attr = defattr;
236 }
237
238 /*
239 * Validate UST buffer size and number of buffers: must both be
240 * power of 2 and nonzero. We validate right here for UST,
241 * because applications will not report the error to the user
242 * (unlike kernel tracing).
243 */
244 if (!attr->attr.subbuf_size || (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
245 ret = LTTCOMM_INVALID;
246 goto error;
247 }
248 if (!attr->attr.num_subbuf || (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
249 ret = LTTCOMM_INVALID;
250 goto error;
251 }
252
253 /* Create UST channel */
254 uchan = trace_ust_create_channel(attr, usess->pathname);
255 if (uchan == NULL) {
256 ret = LTTCOMM_FATAL;
257 goto error;
258 }
259 uchan->enabled = 1;
260
261 switch (domain) {
262 case LTTNG_DOMAIN_UST:
263 DBG2("Channel %s being created in UST global domain", uchan->name);
264
265 /* Enable channel for global domain */
266 ret = ust_app_create_channel_glb(usess, uchan);
267 break;
268 #if 0
269 case LTTNG_DOMAIN_UST_PID:
270 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
271 case LTTNG_DOMAIN_UST_EXEC_NAME:
272 #endif
273 default:
274 ret = LTTCOMM_UND;
275 goto error_free_chan;
276 }
277
278 if (ret < 0 && ret != -EEXIST) {
279 ret = LTTCOMM_UST_CHAN_ENABLE_FAIL;
280 goto error_free_chan;
281 }
282
283 /* Adding the channel to the channel hash table. */
284 rcu_read_lock();
285 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
286 rcu_read_unlock();
287
288 DBG2("Channel %s created successfully", uchan->name);
289
290 free(defattr);
291 return LTTCOMM_OK;
292
293 error_free_chan:
294 /*
295 * No need to remove the channel from the hash table because at this point
296 * it was not added hence the direct call and no call_rcu().
297 */
298 trace_ust_destroy_channel(uchan);
299 error:
300 free(defattr);
301 return ret;
302 }
303
304 /*
305 * Disable UST channel for session and domain.
306 */
307 int channel_ust_disable(struct ltt_ust_session *usess, int domain,
308 struct ltt_ust_channel *uchan)
309 {
310 int ret = LTTCOMM_OK;
311
312 /* Already disabled */
313 if (uchan->enabled == 0) {
314 DBG2("Channel UST %s already disabled", uchan->name);
315 goto end;
316 }
317
318 /* Get the right channel's hashtable */
319 switch (domain) {
320 case LTTNG_DOMAIN_UST:
321 DBG2("Channel %s being disabled in UST global domain", uchan->name);
322 /* Disable channel for global domain */
323 ret = ust_app_disable_channel_glb(usess, uchan);
324 break;
325 #if 0
326 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
327 case LTTNG_DOMAIN_UST_EXEC_NAME:
328 case LTTNG_DOMAIN_UST_PID:
329 #endif
330 default:
331 ret = LTTCOMM_UND;
332 goto error;
333 }
334
335 if (ret < 0 && ret != -EEXIST) {
336 ret = LTTCOMM_UST_DISABLE_FAIL;
337 goto error;
338 }
339
340 uchan->enabled = 0;
341
342 DBG2("Channel %s disabled successfully", uchan->name);
343
344 return LTTCOMM_OK;
345
346 end:
347 error:
348 return ret;
349 }
This page took 0.035954 seconds and 3 git commands to generate.