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