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