Tests: add kernel snapshot streaming to root regression
[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 <inttypes.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <common/common.h>
24 #include <common/defaults.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26
27 #include "channel.h"
28 #include "lttng-sessiond.h"
29 #include "kernel.h"
30 #include "ust-ctl.h"
31 #include "utils.h"
32 #include "ust-app.h"
33
34 /*
35 * Return allocated channel attributes.
36 */
37 struct lttng_channel *channel_new_default_attr(int dom,
38 enum lttng_buffer_type type)
39 {
40 struct lttng_channel *chan;
41
42 chan = zmalloc(sizeof(struct lttng_channel));
43 if (chan == NULL) {
44 PERROR("zmalloc channel init");
45 goto error_alloc;
46 }
47
48 if (snprintf(chan->name, sizeof(chan->name), "%s",
49 DEFAULT_CHANNEL_NAME) < 0) {
50 PERROR("snprintf default channel name");
51 goto error;
52 }
53
54 /* Same for all domains. */
55 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
56 chan->attr.tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
57 chan->attr.tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
58
59 switch (dom) {
60 case LTTNG_DOMAIN_KERNEL:
61 assert(type == LTTNG_BUFFER_GLOBAL);
62 chan->attr.subbuf_size =
63 default_get_kernel_channel_subbuf_size();
64 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
65 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
66 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
67 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
68 break;
69 case LTTNG_DOMAIN_UST:
70 switch (type) {
71 case LTTNG_BUFFER_PER_UID:
72 chan->attr.subbuf_size = default_get_ust_uid_channel_subbuf_size();
73 chan->attr.num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
74 chan->attr.output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
75 chan->attr.switch_timer_interval =
76 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
77 chan->attr.read_timer_interval =
78 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
79 break;
80 case LTTNG_BUFFER_PER_PID:
81 default:
82 chan->attr.subbuf_size = default_get_ust_pid_channel_subbuf_size();
83 chan->attr.num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
84 chan->attr.output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
85 chan->attr.switch_timer_interval =
86 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
87 chan->attr.read_timer_interval =
88 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
89 break;
90 }
91 break;
92 default:
93 goto error; /* Not implemented */
94 }
95
96 return chan;
97
98 error:
99 free(chan);
100 error_alloc:
101 return NULL;
102 }
103
104 /*
105 * Disable kernel channel of the kernel session.
106 */
107 int channel_kernel_disable(struct ltt_kernel_session *ksession,
108 char *channel_name)
109 {
110 int ret;
111 struct ltt_kernel_channel *kchan;
112
113 assert(ksession);
114 assert(channel_name);
115
116 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
117 if (kchan == NULL) {
118 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
119 goto error;
120 }
121
122 /* Only if channel is enabled disable it. */
123 if (kchan->enabled == 1) {
124 ret = kernel_disable_channel(kchan);
125 if (ret < 0 && ret != -EEXIST) {
126 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
127 goto error;
128 }
129 }
130
131 ret = LTTNG_OK;
132
133 error:
134 return ret;
135 }
136
137 /*
138 * Enable kernel channel of the kernel session.
139 */
140 int channel_kernel_enable(struct ltt_kernel_session *ksession,
141 struct ltt_kernel_channel *kchan)
142 {
143 int ret;
144
145 assert(ksession);
146 assert(kchan);
147
148 if (kchan->enabled == 0) {
149 ret = kernel_enable_channel(kchan);
150 if (ret < 0) {
151 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
152 goto error;
153 }
154 } else {
155 ret = LTTNG_ERR_KERN_CHAN_EXIST;
156 goto error;
157 }
158
159 ret = LTTNG_OK;
160
161 error:
162 return ret;
163 }
164
165 /*
166 * Create kernel channel of the kernel session and notify kernel thread.
167 */
168 int channel_kernel_create(struct ltt_kernel_session *ksession,
169 struct lttng_channel *attr, int kernel_pipe)
170 {
171 int ret;
172 struct lttng_channel *defattr = NULL;
173
174 assert(ksession);
175
176 /* Creating channel attributes if needed */
177 if (attr == NULL) {
178 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
179 LTTNG_BUFFER_GLOBAL);
180 if (defattr == NULL) {
181 ret = LTTNG_ERR_FATAL;
182 goto error;
183 }
184 attr = defattr;
185 }
186
187 /* Channel not found, creating it */
188 ret = kernel_create_channel(ksession, attr);
189 if (ret < 0) {
190 ret = LTTNG_ERR_KERN_CHAN_FAIL;
191 goto error;
192 }
193
194 /* Notify kernel thread that there is a new channel */
195 ret = notify_thread_pipe(kernel_pipe);
196 if (ret < 0) {
197 ret = LTTNG_ERR_FATAL;
198 goto error;
199 }
200
201 ret = LTTNG_OK;
202 error:
203 free(defattr);
204 return ret;
205 }
206
207 /*
208 * Enable UST channel for session and domain.
209 */
210 int channel_ust_enable(struct ltt_ust_session *usess,
211 struct ltt_ust_channel *uchan)
212 {
213 int ret = LTTNG_OK;
214
215 assert(usess);
216 assert(uchan);
217
218 /* If already enabled, everything is OK */
219 if (uchan->enabled) {
220 DBG3("Channel %s already enabled. Skipping", uchan->name);
221 ret = LTTNG_ERR_UST_CHAN_EXIST;
222 goto end;
223 }
224
225 DBG2("Channel %s being enabled in UST domain", uchan->name);
226
227 /*
228 * Enable channel for UST global domain on all applications. Ignore return
229 * value here since whatever error we got, it means that the channel was
230 * not created on one or many registered applications and we can not report
231 * this to the user yet. However, at this stage, the channel was
232 * successfully created on the session daemon side so the enable-channel
233 * command is a success.
234 */
235 (void) ust_app_enable_channel_glb(usess, uchan);
236
237 uchan->enabled = 1;
238 DBG2("Channel %s enabled successfully", uchan->name);
239
240 end:
241 return ret;
242 }
243
244 /*
245 * Create UST channel for session and domain.
246 */
247 int channel_ust_create(struct ltt_ust_session *usess,
248 struct lttng_channel *attr, enum lttng_buffer_type type)
249 {
250 int ret = LTTNG_OK;
251 struct ltt_ust_channel *uchan = NULL;
252 struct lttng_channel *defattr = NULL;
253
254 assert(usess);
255
256 /* Creating channel attributes if needed */
257 if (attr == NULL) {
258 defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type);
259 if (defattr == NULL) {
260 ret = LTTNG_ERR_FATAL;
261 goto error;
262 }
263 attr = defattr;
264 }
265
266 /*
267 * Validate UST buffer size and number of buffers: must both be power of 2
268 * and nonzero. We validate right here for UST, because applications will
269 * not report the error to the user (unlike kernel tracing).
270 */
271 if (!attr->attr.subbuf_size ||
272 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
273 ret = LTTNG_ERR_INVALID;
274 goto error;
275 }
276
277 /*
278 * Invalid subbuffer size if it's lower then the page size.
279 */
280 if (attr->attr.subbuf_size < page_size) {
281 ret = LTTNG_ERR_INVALID;
282 goto error;
283 }
284
285 if (!attr->attr.num_subbuf ||
286 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
287 ret = LTTNG_ERR_INVALID;
288 goto error;
289 }
290
291 if (attr->attr.output != LTTNG_EVENT_MMAP) {
292 ret = LTTNG_ERR_NOT_SUPPORTED;
293 goto error;
294 }
295
296 /*
297 * The tracefile_size should not be < to the subbuf_size, otherwise
298 * we won't be able to write the packets on disk
299 */
300 if ((attr->attr.tracefile_size > 0) &&
301 (attr->attr.tracefile_size < attr->attr.subbuf_size)) {
302 ret = LTTNG_ERR_INVALID;
303 goto error;
304 }
305
306 /* Validate buffer type. */
307 switch (type) {
308 case LTTNG_BUFFER_PER_PID:
309 break;
310 case LTTNG_BUFFER_PER_UID:
311 break;
312 default:
313 ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED;
314 goto error;
315 }
316
317 /* Create UST channel */
318 uchan = trace_ust_create_channel(attr);
319 if (uchan == NULL) {
320 ret = LTTNG_ERR_FATAL;
321 goto error;
322 }
323 uchan->enabled = 1;
324 if (trace_ust_is_max_id(usess->used_channel_id)) {
325 ret = LTTNG_ERR_UST_CHAN_FAIL;
326 goto error;
327 }
328 uchan->id = trace_ust_get_next_chan_id(usess);
329
330 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64,
331 uchan->name, type, uchan->id);
332
333 /* Flag session buffer type. */
334 if (!usess->buffer_type_changed) {
335 usess->buffer_type = type;
336 usess->buffer_type_changed = 1;
337 } else if (usess->buffer_type != type) {
338 /* Buffer type was already set. Refuse to create channel. */
339 ret = LTTNG_ERR_BUFFER_TYPE_MISMATCH;
340 goto error_free_chan;
341 }
342
343 /* Enable channel for global domain */
344 ret = ust_app_create_channel_glb(usess, uchan);
345 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
346 ret = LTTNG_ERR_UST_CHAN_FAIL;
347 goto error_free_chan;
348 }
349
350 /* Adding the channel to the channel hash table. */
351 rcu_read_lock();
352 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
353 rcu_read_unlock();
354
355 DBG2("Channel %s created successfully", uchan->name);
356
357 free(defattr);
358 return LTTNG_OK;
359
360 error_free_chan:
361 /*
362 * No need to remove the channel from the hash table because at this point
363 * it was not added hence the direct call and no call_rcu().
364 */
365 trace_ust_destroy_channel(uchan);
366 error:
367 free(defattr);
368 return ret;
369 }
370
371 /*
372 * Disable UST channel for session and domain.
373 */
374 int channel_ust_disable(struct ltt_ust_session *usess,
375 struct ltt_ust_channel *uchan)
376 {
377 int ret = LTTNG_OK;
378
379 assert(usess);
380 assert(uchan);
381
382 /* Already disabled */
383 if (uchan->enabled == 0) {
384 DBG2("Channel UST %s already disabled", uchan->name);
385 goto end;
386 }
387
388 DBG2("Channel %s being disabled in UST global domain", uchan->name);
389 /* Disable channel for global domain */
390 ret = ust_app_disable_channel_glb(usess, uchan);
391 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
392 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
393 goto error;
394 }
395
396 uchan->enabled = 0;
397
398 DBG2("Channel %s disabled successfully", uchan->name);
399
400 return LTTNG_OK;
401
402 end:
403 error:
404 return ret;
405 }
This page took 0.036863 seconds and 4 git commands to generate.