dfc7892674bbe1f15dc74d818175ea3c60c3c65d
[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
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 = LTTNG_ERR_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 = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
100 goto error;
101 }
102 }
103
104 ret = LTTNG_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 = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
122 goto error;
123 }
124 } else {
125 ret = LTTNG_ERR_KERN_CHAN_EXIST;
126 goto error;
127 }
128
129 ret = LTTNG_OK;
130
131 error:
132 return ret;
133 }
134
135 /*
136 * Create kernel channel of the kernel session and notify kernel thread.
137 */
138 int channel_kernel_create(struct ltt_kernel_session *ksession,
139 struct lttng_channel *attr, int kernel_pipe)
140 {
141 int ret;
142 struct lttng_channel *defattr = NULL;
143
144 /* Creating channel attributes if needed */
145 if (attr == NULL) {
146 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL);
147 if (defattr == NULL) {
148 ret = LTTNG_ERR_FATAL;
149 goto error;
150 }
151 attr = defattr;
152 }
153
154 /* Channel not found, creating it */
155 ret = kernel_create_channel(ksession, attr, ksession->trace_path);
156 if (ret < 0) {
157 ret = LTTNG_ERR_KERN_CHAN_FAIL;
158 goto error;
159 }
160
161 /* Notify kernel thread that there is a new channel */
162 ret = notify_thread_pipe(kernel_pipe);
163 if (ret < 0) {
164 ret = LTTNG_ERR_FATAL;
165 goto error;
166 }
167
168 ret = LTTNG_OK;
169 error:
170 free(defattr);
171 return ret;
172 }
173
174 /*
175 * Enable UST channel for session and domain.
176 */
177 int channel_ust_enable(struct ltt_ust_session *usess, int domain,
178 struct ltt_ust_channel *uchan)
179 {
180 int ret = LTTNG_OK;
181
182 /* If already enabled, everything is OK */
183 if (uchan->enabled) {
184 DBG3("Channel %s already enabled. Skipping", uchan->name);
185 ret = LTTNG_ERR_UST_CHAN_EXIST;
186 goto end;
187 }
188
189 switch (domain) {
190 case LTTNG_DOMAIN_UST:
191 DBG2("Channel %s being enabled in UST global domain", uchan->name);
192 /* Enable channel for global domain */
193 ret = ust_app_enable_channel_glb(usess, uchan);
194 break;
195 #if 0
196 case LTTNG_DOMAIN_UST_PID:
197 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
198 case LTTNG_DOMAIN_UST_EXEC_NAME:
199 #endif
200 default:
201 ret = LTTNG_ERR_UND;
202 goto error;
203 }
204
205 if (ret < 0) {
206 if (ret != -EEXIST) {
207 ret = LTTNG_ERR_UST_CHAN_ENABLE_FAIL;
208 goto error;
209 } else {
210 ret = LTTNG_OK;
211 }
212 }
213
214 uchan->enabled = 1;
215 DBG2("Channel %s enabled successfully", uchan->name);
216
217 end:
218 error:
219 return ret;
220 }
221
222 /*
223 * Create UST channel for session and domain.
224 */
225 int channel_ust_create(struct ltt_ust_session *usess, int domain,
226 struct lttng_channel *attr)
227 {
228 int ret = LTTNG_OK;
229 struct ltt_ust_channel *uchan = NULL;
230 struct lttng_channel *defattr = NULL;
231
232 /* Creating channel attributes if needed */
233 if (attr == NULL) {
234 defattr = channel_new_default_attr(domain);
235 if (defattr == NULL) {
236 ret = LTTNG_ERR_FATAL;
237 goto error;
238 }
239 attr = defattr;
240 }
241
242 /*
243 * Validate UST buffer size and number of buffers: must both be
244 * power of 2 and nonzero. We validate right here for UST,
245 * because applications will not report the error to the user
246 * (unlike kernel tracing).
247 */
248 if (!attr->attr.subbuf_size || (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
249 ret = LTTNG_ERR_INVALID;
250 goto error;
251 }
252 if (!attr->attr.num_subbuf || (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
253 ret = LTTNG_ERR_INVALID;
254 goto error;
255 }
256
257 /* Create UST channel */
258 uchan = trace_ust_create_channel(attr, usess->pathname);
259 if (uchan == NULL) {
260 ret = LTTNG_ERR_FATAL;
261 goto error;
262 }
263 uchan->enabled = 1;
264
265 switch (domain) {
266 case LTTNG_DOMAIN_UST:
267 DBG2("Channel %s being created in UST global domain", uchan->name);
268
269 /* Enable channel for global domain */
270 ret = ust_app_create_channel_glb(usess, uchan);
271 break;
272 #if 0
273 case LTTNG_DOMAIN_UST_PID:
274 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
275 case LTTNG_DOMAIN_UST_EXEC_NAME:
276 #endif
277 default:
278 ret = LTTNG_ERR_UND;
279 goto error_free_chan;
280 }
281
282 if (ret < 0 && ret != -EEXIST) {
283 ret = LTTNG_ERR_UST_CHAN_FAIL;
284 goto error_free_chan;
285 }
286
287 /* Adding the channel to the channel hash table. */
288 rcu_read_lock();
289 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
290 rcu_read_unlock();
291
292 DBG2("Channel %s created successfully", uchan->name);
293
294 free(defattr);
295 return LTTNG_OK;
296
297 error_free_chan:
298 /*
299 * No need to remove the channel from the hash table because at this point
300 * it was not added hence the direct call and no call_rcu().
301 */
302 trace_ust_destroy_channel(uchan);
303 error:
304 free(defattr);
305 return ret;
306 }
307
308 /*
309 * Disable UST channel for session and domain.
310 */
311 int channel_ust_disable(struct ltt_ust_session *usess, int domain,
312 struct ltt_ust_channel *uchan)
313 {
314 int ret = LTTNG_OK;
315
316 /* Already disabled */
317 if (uchan->enabled == 0) {
318 DBG2("Channel UST %s already disabled", uchan->name);
319 goto end;
320 }
321
322 /* Get the right channel's hashtable */
323 switch (domain) {
324 case LTTNG_DOMAIN_UST:
325 DBG2("Channel %s being disabled in UST global domain", uchan->name);
326 /* Disable channel for global domain */
327 ret = ust_app_disable_channel_glb(usess, uchan);
328 break;
329 #if 0
330 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
331 case LTTNG_DOMAIN_UST_EXEC_NAME:
332 case LTTNG_DOMAIN_UST_PID:
333 #endif
334 default:
335 ret = LTTNG_ERR_UND;
336 goto error;
337 }
338
339 if (ret < 0 && ret != -EEXIST) {
340 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
341 goto error;
342 }
343
344 uchan->enabled = 0;
345
346 DBG2("Channel %s disabled successfully", uchan->name);
347
348 return LTTNG_OK;
349
350 end:
351 error:
352 return ret;
353 }
This page took 0.034645 seconds and 3 git commands to generate.