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