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