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