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 | ||
93 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); | |
94 | if (kchan == NULL) { | |
f73fabfd | 95 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
54d01ffb DG |
96 | goto error; |
97 | } else if (kchan->enabled == 1) { | |
98 | ret = kernel_disable_channel(kchan); | |
7885e399 | 99 | if (ret < 0 && ret != -EEXIST) { |
f73fabfd | 100 | ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL; |
54d01ffb DG |
101 | goto error; |
102 | } | |
103 | } | |
104 | ||
f73fabfd | 105 | ret = LTTNG_OK; |
54d01ffb DG |
106 | |
107 | error: | |
108 | return ret; | |
109 | } | |
110 | ||
111 | /* | |
112 | * Enable kernel channel of the kernel session. | |
113 | */ | |
114 | int channel_kernel_enable(struct ltt_kernel_session *ksession, | |
115 | struct ltt_kernel_channel *kchan) | |
116 | { | |
117 | int ret; | |
118 | ||
119 | if (kchan->enabled == 0) { | |
120 | ret = kernel_enable_channel(kchan); | |
121 | if (ret < 0) { | |
f73fabfd | 122 | ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL; |
54d01ffb DG |
123 | goto error; |
124 | } | |
42224349 | 125 | } else { |
f73fabfd | 126 | ret = LTTNG_ERR_KERN_CHAN_EXIST; |
42224349 | 127 | goto error; |
54d01ffb DG |
128 | } |
129 | ||
f73fabfd | 130 | ret = LTTNG_OK; |
54d01ffb DG |
131 | |
132 | error: | |
133 | return ret; | |
134 | } | |
135 | ||
136 | /* | |
137 | * Create kernel channel of the kernel session and notify kernel thread. | |
138 | */ | |
139 | int channel_kernel_create(struct ltt_kernel_session *ksession, | |
ff4d74e6 | 140 | struct lttng_channel *attr, int kernel_pipe) |
54d01ffb DG |
141 | { |
142 | int ret; | |
ff4d74e6 | 143 | struct lttng_channel *defattr = NULL; |
54d01ffb DG |
144 | |
145 | /* Creating channel attributes if needed */ | |
146 | if (attr == NULL) { | |
ff4d74e6 MD |
147 | defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL); |
148 | if (defattr == NULL) { | |
f73fabfd | 149 | ret = LTTNG_ERR_FATAL; |
54d01ffb DG |
150 | goto error; |
151 | } | |
ff4d74e6 | 152 | attr = defattr; |
54d01ffb DG |
153 | } |
154 | ||
155 | /* Channel not found, creating it */ | |
156 | ret = kernel_create_channel(ksession, attr, ksession->trace_path); | |
157 | if (ret < 0) { | |
f73fabfd | 158 | ret = LTTNG_ERR_KERN_CHAN_FAIL; |
54d01ffb DG |
159 | goto error; |
160 | } | |
161 | ||
162 | /* Notify kernel thread that there is a new channel */ | |
163 | ret = notify_thread_pipe(kernel_pipe); | |
164 | if (ret < 0) { | |
f73fabfd | 165 | ret = LTTNG_ERR_FATAL; |
54d01ffb DG |
166 | goto error; |
167 | } | |
168 | ||
f73fabfd | 169 | ret = LTTNG_OK; |
54d01ffb | 170 | error: |
ff4d74e6 | 171 | free(defattr); |
54d01ffb DG |
172 | return ret; |
173 | } | |
7885e399 DG |
174 | |
175 | /* | |
176 | * Enable UST channel for session and domain. | |
177 | */ | |
178 | int channel_ust_enable(struct ltt_ust_session *usess, int domain, | |
179 | struct ltt_ust_channel *uchan) | |
180 | { | |
f73fabfd | 181 | int ret = LTTNG_OK; |
7885e399 DG |
182 | |
183 | /* If already enabled, everything is OK */ | |
184 | if (uchan->enabled) { | |
185 | DBG3("Channel %s already enabled. Skipping", uchan->name); | |
f73fabfd | 186 | ret = LTTNG_ERR_UST_CHAN_EXIST; |
7885e399 DG |
187 | goto end; |
188 | } | |
189 | ||
190 | switch (domain) { | |
191 | case LTTNG_DOMAIN_UST: | |
192 | DBG2("Channel %s being enabled in UST global domain", uchan->name); | |
4d710ac2 DG |
193 | |
194 | /* | |
195 | * Enable channel for UST global domain on all applications. Ignore | |
196 | * return value here since whatever error we got, it means that the | |
197 | * channel was not created on one or many registered applications and | |
198 | * we can not report this to the user yet. However, at this stage, the | |
199 | * channel was successfully created on the session daemon side so the | |
200 | * enable-channel command is a success. | |
201 | */ | |
202 | (void) ust_app_create_channel_glb(usess, uchan); | |
7885e399 | 203 | break; |
d78d6610 | 204 | #if 0 |
7885e399 DG |
205 | case LTTNG_DOMAIN_UST_PID: |
206 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
207 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
d78d6610 DG |
208 | #endif |
209 | default: | |
f73fabfd | 210 | ret = LTTNG_ERR_UND; |
7885e399 DG |
211 | goto error; |
212 | } | |
213 | ||
7885e399 DG |
214 | uchan->enabled = 1; |
215 | DBG2("Channel %s enabled successfully", uchan->name); | |
216 | ||
217 | end: | |
218 | error: | |
219 | return ret; | |
220 | } | |
221 | ||
222 | /* | |
223 | * Create UST channel for session and domain. | |
224 | */ | |
225 | int channel_ust_create(struct ltt_ust_session *usess, int domain, | |
226 | struct lttng_channel *attr) | |
227 | { | |
f73fabfd | 228 | int ret = LTTNG_OK; |
7885e399 DG |
229 | struct ltt_ust_channel *uchan = NULL; |
230 | struct lttng_channel *defattr = NULL; | |
231 | ||
232 | /* Creating channel attributes if needed */ | |
233 | if (attr == NULL) { | |
234 | defattr = channel_new_default_attr(domain); | |
235 | if (defattr == NULL) { | |
f73fabfd | 236 | ret = LTTNG_ERR_FATAL; |
7885e399 DG |
237 | goto error; |
238 | } | |
239 | attr = defattr; | |
240 | } | |
241 | ||
95e047ff DG |
242 | if (attr->attr.subbuf_size < DEFAULT_UST_CHANNEL_SUBBUF_SIZE) { |
243 | ret = LTTNG_ERR_INVALID; | |
244 | goto error; | |
245 | } | |
246 | ||
b024d072 MD |
247 | /* |
248 | * Validate UST buffer size and number of buffers: must both be | |
249 | * power of 2 and nonzero. We validate right here for UST, | |
250 | * because applications will not report the error to the user | |
251 | * (unlike kernel tracing). | |
252 | */ | |
253 | if (!attr->attr.subbuf_size || (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) { | |
f73fabfd | 254 | ret = LTTNG_ERR_INVALID; |
b024d072 MD |
255 | goto error; |
256 | } | |
257 | if (!attr->attr.num_subbuf || (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) { | |
f73fabfd | 258 | ret = LTTNG_ERR_INVALID; |
b024d072 MD |
259 | goto error; |
260 | } | |
261 | ||
a79d84dd DG |
262 | if (attr->attr.output != LTTNG_EVENT_MMAP) { |
263 | ret = LTTNG_ERR_NOT_SUPPORTED; | |
264 | goto error; | |
265 | } | |
266 | ||
7885e399 DG |
267 | /* Create UST channel */ |
268 | uchan = trace_ust_create_channel(attr, usess->pathname); | |
269 | if (uchan == NULL) { | |
f73fabfd | 270 | ret = LTTNG_ERR_FATAL; |
7885e399 DG |
271 | goto error; |
272 | } | |
58f3ca76 | 273 | uchan->enabled = 1; |
7885e399 DG |
274 | |
275 | switch (domain) { | |
276 | case LTTNG_DOMAIN_UST: | |
277 | DBG2("Channel %s being created in UST global domain", uchan->name); | |
58f3ca76 | 278 | |
7885e399 DG |
279 | /* Enable channel for global domain */ |
280 | ret = ust_app_create_channel_glb(usess, uchan); | |
281 | break; | |
d78d6610 | 282 | #if 0 |
7885e399 DG |
283 | case LTTNG_DOMAIN_UST_PID: |
284 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
285 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
d78d6610 | 286 | #endif |
7885e399 | 287 | default: |
f73fabfd | 288 | ret = LTTNG_ERR_UND; |
7885e399 DG |
289 | goto error_free_chan; |
290 | } | |
291 | ||
49c336c1 | 292 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) { |
f73fabfd | 293 | ret = LTTNG_ERR_UST_CHAN_FAIL; |
7885e399 DG |
294 | goto error_free_chan; |
295 | } | |
296 | ||
fc34caaa DG |
297 | /* Adding the channel to the channel hash table. */ |
298 | rcu_read_lock(); | |
299 | lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node); | |
300 | rcu_read_unlock(); | |
301 | ||
7885e399 DG |
302 | DBG2("Channel %s created successfully", uchan->name); |
303 | ||
304 | free(defattr); | |
f73fabfd | 305 | return LTTNG_OK; |
7885e399 DG |
306 | |
307 | error_free_chan: | |
24d1723f DG |
308 | /* |
309 | * No need to remove the channel from the hash table because at this point | |
310 | * it was not added hence the direct call and no call_rcu(). | |
311 | */ | |
7885e399 DG |
312 | trace_ust_destroy_channel(uchan); |
313 | error: | |
314 | free(defattr); | |
315 | return ret; | |
316 | } | |
317 | ||
318 | /* | |
319 | * Disable UST channel for session and domain. | |
320 | */ | |
321 | int channel_ust_disable(struct ltt_ust_session *usess, int domain, | |
322 | struct ltt_ust_channel *uchan) | |
323 | { | |
f73fabfd | 324 | int ret = LTTNG_OK; |
7885e399 DG |
325 | |
326 | /* Already disabled */ | |
327 | if (uchan->enabled == 0) { | |
328 | DBG2("Channel UST %s already disabled", uchan->name); | |
329 | goto end; | |
330 | } | |
331 | ||
332 | /* Get the right channel's hashtable */ | |
333 | switch (domain) { | |
334 | case LTTNG_DOMAIN_UST: | |
335 | DBG2("Channel %s being disabled in UST global domain", uchan->name); | |
7885e399 DG |
336 | /* Disable channel for global domain */ |
337 | ret = ust_app_disable_channel_glb(usess, uchan); | |
338 | break; | |
d78d6610 | 339 | #if 0 |
7885e399 DG |
340 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
341 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
342 | case LTTNG_DOMAIN_UST_PID: | |
d78d6610 DG |
343 | #endif |
344 | default: | |
f73fabfd | 345 | ret = LTTNG_ERR_UND; |
7885e399 DG |
346 | goto error; |
347 | } | |
348 | ||
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 | } |