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; | |
1b1c65fa MD |
68 | break; |
69 | case LTTNG_DOMAIN_UST: | |
0a9c6494 DG |
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 | } | |
1b1c65fa MD |
91 | break; |
92 | default: | |
93 | goto error; /* Not implemented */ | |
54d01ffb DG |
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 | ||
0525e9ae DG |
113 | assert(ksession); |
114 | assert(channel_name); | |
115 | ||
54d01ffb DG |
116 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
117 | if (kchan == NULL) { | |
f73fabfd | 118 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
54d01ffb | 119 | goto error; |
0525e9ae DG |
120 | } |
121 | ||
122 | /* Only if channel is enabled disable it. */ | |
123 | if (kchan->enabled == 1) { | |
54d01ffb | 124 | ret = kernel_disable_channel(kchan); |
7885e399 | 125 | if (ret < 0 && ret != -EEXIST) { |
f73fabfd | 126 | ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL; |
54d01ffb DG |
127 | goto error; |
128 | } | |
129 | } | |
130 | ||
f73fabfd | 131 | ret = LTTNG_OK; |
54d01ffb DG |
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 | ||
0525e9ae DG |
145 | assert(ksession); |
146 | assert(kchan); | |
147 | ||
54d01ffb DG |
148 | if (kchan->enabled == 0) { |
149 | ret = kernel_enable_channel(kchan); | |
150 | if (ret < 0) { | |
f73fabfd | 151 | ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL; |
54d01ffb DG |
152 | goto error; |
153 | } | |
42224349 | 154 | } else { |
f73fabfd | 155 | ret = LTTNG_ERR_KERN_CHAN_EXIST; |
42224349 | 156 | goto error; |
54d01ffb DG |
157 | } |
158 | ||
f73fabfd | 159 | ret = LTTNG_OK; |
54d01ffb DG |
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, | |
ff4d74e6 | 169 | struct lttng_channel *attr, int kernel_pipe) |
54d01ffb DG |
170 | { |
171 | int ret; | |
ff4d74e6 | 172 | struct lttng_channel *defattr = NULL; |
54d01ffb | 173 | |
0525e9ae DG |
174 | assert(ksession); |
175 | ||
54d01ffb DG |
176 | /* Creating channel attributes if needed */ |
177 | if (attr == NULL) { | |
0a9c6494 DG |
178 | defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
179 | LTTNG_BUFFER_GLOBAL); | |
ff4d74e6 | 180 | if (defattr == NULL) { |
f73fabfd | 181 | ret = LTTNG_ERR_FATAL; |
54d01ffb DG |
182 | goto error; |
183 | } | |
ff4d74e6 | 184 | attr = defattr; |
54d01ffb DG |
185 | } |
186 | ||
27babd3a DG |
187 | if (ksession->snapshot_mode) { |
188 | /* Force channel attribute for snapshot mode. */ | |
189 | attr->attr.overwrite = 1; | |
190 | attr->attr.output = LTTNG_EVENT_MMAP; | |
191 | } | |
192 | ||
54d01ffb | 193 | /* Channel not found, creating it */ |
fdd9eb17 | 194 | ret = kernel_create_channel(ksession, attr); |
54d01ffb | 195 | if (ret < 0) { |
f73fabfd | 196 | ret = LTTNG_ERR_KERN_CHAN_FAIL; |
54d01ffb DG |
197 | goto error; |
198 | } | |
199 | ||
200 | /* Notify kernel thread that there is a new channel */ | |
201 | ret = notify_thread_pipe(kernel_pipe); | |
202 | if (ret < 0) { | |
f73fabfd | 203 | ret = LTTNG_ERR_FATAL; |
54d01ffb DG |
204 | goto error; |
205 | } | |
206 | ||
f73fabfd | 207 | ret = LTTNG_OK; |
54d01ffb | 208 | error: |
ff4d74e6 | 209 | free(defattr); |
54d01ffb DG |
210 | return ret; |
211 | } | |
7885e399 DG |
212 | |
213 | /* | |
214 | * Enable UST channel for session and domain. | |
215 | */ | |
7972aab2 | 216 | int channel_ust_enable(struct ltt_ust_session *usess, |
7885e399 DG |
217 | struct ltt_ust_channel *uchan) |
218 | { | |
f73fabfd | 219 | int ret = LTTNG_OK; |
7885e399 | 220 | |
0525e9ae DG |
221 | assert(usess); |
222 | assert(uchan); | |
223 | ||
7885e399 DG |
224 | /* If already enabled, everything is OK */ |
225 | if (uchan->enabled) { | |
226 | DBG3("Channel %s already enabled. Skipping", uchan->name); | |
f73fabfd | 227 | ret = LTTNG_ERR_UST_CHAN_EXIST; |
7885e399 DG |
228 | goto end; |
229 | } | |
230 | ||
7972aab2 DG |
231 | DBG2("Channel %s being enabled in UST domain", uchan->name); |
232 | ||
233 | /* | |
234 | * Enable channel for UST global domain on all applications. Ignore return | |
235 | * value here since whatever error we got, it means that the channel was | |
236 | * not created on one or many registered applications and we can not report | |
237 | * this to the user yet. However, at this stage, the channel was | |
238 | * successfully created on the session daemon side so the enable-channel | |
239 | * command is a success. | |
240 | */ | |
d54b4440 | 241 | (void) ust_app_enable_channel_glb(usess, uchan); |
7885e399 | 242 | |
7885e399 DG |
243 | uchan->enabled = 1; |
244 | DBG2("Channel %s enabled successfully", uchan->name); | |
245 | ||
246 | end: | |
7885e399 DG |
247 | return ret; |
248 | } | |
249 | ||
250 | /* | |
251 | * Create UST channel for session and domain. | |
252 | */ | |
7972aab2 DG |
253 | int channel_ust_create(struct ltt_ust_session *usess, |
254 | struct lttng_channel *attr, enum lttng_buffer_type type) | |
7885e399 | 255 | { |
f73fabfd | 256 | int ret = LTTNG_OK; |
7885e399 DG |
257 | struct ltt_ust_channel *uchan = NULL; |
258 | struct lttng_channel *defattr = NULL; | |
259 | ||
0525e9ae DG |
260 | assert(usess); |
261 | ||
7885e399 DG |
262 | /* Creating channel attributes if needed */ |
263 | if (attr == NULL) { | |
0a9c6494 | 264 | defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type); |
7885e399 | 265 | if (defattr == NULL) { |
f73fabfd | 266 | ret = LTTNG_ERR_FATAL; |
7885e399 DG |
267 | goto error; |
268 | } | |
269 | attr = defattr; | |
270 | } | |
271 | ||
27babd3a DG |
272 | if (usess->snapshot_mode) { |
273 | /* Force channel attribute for snapshot mode. */ | |
274 | attr->attr.overwrite = 1; | |
275 | attr->attr.output = LTTNG_EVENT_MMAP; | |
276 | } | |
277 | ||
b024d072 | 278 | /* |
0525e9ae DG |
279 | * Validate UST buffer size and number of buffers: must both be power of 2 |
280 | * and nonzero. We validate right here for UST, because applications will | |
281 | * not report the error to the user (unlike kernel tracing). | |
b024d072 | 282 | */ |
0525e9ae DG |
283 | if (!attr->attr.subbuf_size || |
284 | (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) { | |
f73fabfd | 285 | ret = LTTNG_ERR_INVALID; |
b024d072 MD |
286 | goto error; |
287 | } | |
0525e9ae | 288 | |
12744796 DG |
289 | /* |
290 | * Invalid subbuffer size if it's lower then the page size. | |
291 | */ | |
292 | if (attr->attr.subbuf_size < page_size) { | |
293 | ret = LTTNG_ERR_INVALID; | |
294 | goto error; | |
295 | } | |
296 | ||
0525e9ae DG |
297 | if (!attr->attr.num_subbuf || |
298 | (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) { | |
f73fabfd | 299 | ret = LTTNG_ERR_INVALID; |
b024d072 MD |
300 | goto error; |
301 | } | |
302 | ||
a79d84dd DG |
303 | if (attr->attr.output != LTTNG_EVENT_MMAP) { |
304 | ret = LTTNG_ERR_NOT_SUPPORTED; | |
305 | goto error; | |
306 | } | |
307 | ||
1624d5b7 JD |
308 | /* |
309 | * The tracefile_size should not be < to the subbuf_size, otherwise | |
310 | * we won't be able to write the packets on disk | |
311 | */ | |
312 | if ((attr->attr.tracefile_size > 0) && | |
313 | (attr->attr.tracefile_size < attr->attr.subbuf_size)) { | |
314 | ret = LTTNG_ERR_INVALID; | |
315 | goto error; | |
316 | } | |
317 | ||
2e8269f7 DG |
318 | /* Validate buffer type. */ |
319 | switch (type) { | |
320 | case LTTNG_BUFFER_PER_PID: | |
0a9c6494 | 321 | break; |
2e8269f7 DG |
322 | case LTTNG_BUFFER_PER_UID: |
323 | break; | |
324 | default: | |
325 | ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED; | |
326 | goto error; | |
327 | } | |
328 | ||
7885e399 | 329 | /* Create UST channel */ |
dec56f6c | 330 | uchan = trace_ust_create_channel(attr); |
7885e399 | 331 | if (uchan == NULL) { |
f73fabfd | 332 | ret = LTTNG_ERR_FATAL; |
7885e399 DG |
333 | goto error; |
334 | } | |
58f3ca76 | 335 | uchan->enabled = 1; |
7972aab2 DG |
336 | if (trace_ust_is_max_id(usess->used_channel_id)) { |
337 | ret = LTTNG_ERR_UST_CHAN_FAIL; | |
338 | goto error; | |
339 | } | |
340 | uchan->id = trace_ust_get_next_chan_id(usess); | |
341 | ||
342 | DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64, | |
343 | uchan->name, type, uchan->id); | |
344 | ||
345 | /* Flag session buffer type. */ | |
346 | if (!usess->buffer_type_changed) { | |
347 | usess->buffer_type = type; | |
348 | usess->buffer_type_changed = 1; | |
349 | } else if (usess->buffer_type != type) { | |
350 | /* Buffer type was already set. Refuse to create channel. */ | |
351 | ret = LTTNG_ERR_BUFFER_TYPE_MISMATCH; | |
7885e399 DG |
352 | goto error_free_chan; |
353 | } | |
354 | ||
7972aab2 DG |
355 | /* Enable channel for global domain */ |
356 | ret = ust_app_create_channel_glb(usess, uchan); | |
49c336c1 | 357 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) { |
f73fabfd | 358 | ret = LTTNG_ERR_UST_CHAN_FAIL; |
7885e399 DG |
359 | goto error_free_chan; |
360 | } | |
361 | ||
fc34caaa DG |
362 | /* Adding the channel to the channel hash table. */ |
363 | rcu_read_lock(); | |
364 | lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node); | |
365 | rcu_read_unlock(); | |
366 | ||
7885e399 DG |
367 | DBG2("Channel %s created successfully", uchan->name); |
368 | ||
369 | free(defattr); | |
f73fabfd | 370 | return LTTNG_OK; |
7885e399 DG |
371 | |
372 | error_free_chan: | |
24d1723f DG |
373 | /* |
374 | * No need to remove the channel from the hash table because at this point | |
375 | * it was not added hence the direct call and no call_rcu(). | |
376 | */ | |
7885e399 DG |
377 | trace_ust_destroy_channel(uchan); |
378 | error: | |
379 | free(defattr); | |
380 | return ret; | |
381 | } | |
382 | ||
383 | /* | |
384 | * Disable UST channel for session and domain. | |
385 | */ | |
7972aab2 | 386 | int channel_ust_disable(struct ltt_ust_session *usess, |
7885e399 DG |
387 | struct ltt_ust_channel *uchan) |
388 | { | |
f73fabfd | 389 | int ret = LTTNG_OK; |
7885e399 | 390 | |
0525e9ae DG |
391 | assert(usess); |
392 | assert(uchan); | |
393 | ||
7885e399 DG |
394 | /* Already disabled */ |
395 | if (uchan->enabled == 0) { | |
396 | DBG2("Channel UST %s already disabled", uchan->name); | |
397 | goto end; | |
398 | } | |
399 | ||
7972aab2 DG |
400 | DBG2("Channel %s being disabled in UST global domain", uchan->name); |
401 | /* Disable channel for global domain */ | |
402 | ret = ust_app_disable_channel_glb(usess, uchan); | |
49c336c1 | 403 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) { |
f73fabfd | 404 | ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL; |
7885e399 DG |
405 | goto error; |
406 | } | |
407 | ||
408 | uchan->enabled = 0; | |
409 | ||
410 | DBG2("Channel %s disabled successfully", uchan->name); | |
411 | ||
f73fabfd | 412 | return LTTNG_OK; |
7885e399 DG |
413 | |
414 | end: | |
415 | error: | |
416 | return ret; | |
417 | } |