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