Commit | Line | Data |
---|---|---|
2f77fc4b DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
2f77fc4b | 20 | #include <assert.h> |
1f345e94 | 21 | #include <string.h> |
6dc3064a | 22 | #include <inttypes.h> |
2f77fc4b DG |
23 | #include <urcu/list.h> |
24 | #include <urcu/uatomic.h> | |
25 | ||
26 | #include <common/defaults.h> | |
27 | #include <common/common.h> | |
28 | #include <common/sessiond-comm/sessiond-comm.h> | |
29 | #include <common/relayd/relayd.h> | |
10a50311 | 30 | #include <common/utils.h> |
2f77fc4b DG |
31 | |
32 | #include "channel.h" | |
33 | #include "consumer.h" | |
34 | #include "event.h" | |
8782cc74 | 35 | #include "health-sessiond.h" |
2f77fc4b DG |
36 | #include "kernel.h" |
37 | #include "kernel-consumer.h" | |
38 | #include "lttng-sessiond.h" | |
39 | #include "utils.h" | |
834978fd | 40 | #include "syscall.h" |
2f77fc4b DG |
41 | |
42 | #include "cmd.h" | |
43 | ||
44 | /* | |
45 | * Used to keep a unique index for each relayd socket created where this value | |
46 | * is associated with streams on the consumer so it can match the right relayd | |
d88aee68 DG |
47 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
48 | * held. | |
2f77fc4b | 49 | */ |
d88aee68 DG |
50 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
51 | static uint64_t relayd_net_seq_idx; | |
2f77fc4b | 52 | |
18a720cd MD |
53 | /* |
54 | * Both functions below are special case for the Kernel domain when | |
55 | * enabling/disabling all events. | |
56 | */ | |
57 | static | |
58 | int enable_kevent_all(struct ltt_session *session, | |
59 | struct lttng_domain *domain, char *channel_name, | |
60 | struct lttng_event *event, | |
61 | char *filter_expression, | |
62 | struct lttng_filter_bytecode *filter, int wpipe); | |
63 | static | |
64 | int disable_kevent_all(struct ltt_session *session, int domain, | |
65 | char *channel_name, | |
66 | struct lttng_event *event); | |
67 | ||
2f77fc4b DG |
68 | /* |
69 | * Create a session path used by list_lttng_sessions for the case that the | |
70 | * session consumer is on the network. | |
71 | */ | |
72 | static int build_network_session_path(char *dst, size_t size, | |
73 | struct ltt_session *session) | |
74 | { | |
75 | int ret, kdata_port, udata_port; | |
76 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
77 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
78 | ||
79 | assert(session); | |
80 | assert(dst); | |
81 | ||
82 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
83 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
84 | ||
85 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
86 | ||
87 | if (session->kernel_session && session->kernel_session->consumer) { | |
88 | kuri = &session->kernel_session->consumer->dst.net.control; | |
89 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
90 | } | |
91 | ||
92 | if (session->ust_session && session->ust_session->consumer) { | |
93 | uuri = &session->ust_session->consumer->dst.net.control; | |
94 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
95 | } | |
96 | ||
97 | if (uuri == NULL && kuri == NULL) { | |
98 | uri = &session->consumer->dst.net.control; | |
99 | kdata_port = session->consumer->dst.net.data.port; | |
100 | } else if (kuri && uuri) { | |
101 | ret = uri_compare(kuri, uuri); | |
102 | if (ret) { | |
103 | /* Not Equal */ | |
104 | uri = kuri; | |
105 | /* Build uuri URL string */ | |
106 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
107 | if (ret < 0) { | |
108 | goto error; | |
109 | } | |
110 | } else { | |
111 | uri = kuri; | |
112 | } | |
113 | } else if (kuri && uuri == NULL) { | |
114 | uri = kuri; | |
115 | } else if (uuri && kuri == NULL) { | |
116 | uri = uuri; | |
117 | } | |
118 | ||
119 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
120 | if (ret < 0) { | |
121 | goto error; | |
122 | } | |
123 | ||
9aa9f900 DG |
124 | /* |
125 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
126 | * to print. | |
127 | */ | |
9d035200 | 128 | if (*tmp_uurl != '\0') { |
2f77fc4b DG |
129 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
130 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
131 | } else { | |
9aa9f900 | 132 | int dport; |
bef08707 | 133 | if (kuri || (!kuri && !uuri)) { |
9aa9f900 DG |
134 | dport = kdata_port; |
135 | } else { | |
136 | /* No kernel URI, use the UST port. */ | |
137 | dport = udata_port; | |
138 | } | |
139 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
140 | } |
141 | ||
142 | error: | |
143 | return ret; | |
144 | } | |
145 | ||
146 | /* | |
147 | * Fill lttng_channel array of all channels. | |
148 | */ | |
149 | static void list_lttng_channels(int domain, struct ltt_session *session, | |
150 | struct lttng_channel *channels) | |
151 | { | |
152 | int i = 0; | |
153 | struct ltt_kernel_channel *kchan; | |
154 | ||
155 | DBG("Listing channels for session %s", session->name); | |
156 | ||
157 | switch (domain) { | |
158 | case LTTNG_DOMAIN_KERNEL: | |
159 | /* Kernel channels */ | |
160 | if (session->kernel_session != NULL) { | |
161 | cds_list_for_each_entry(kchan, | |
162 | &session->kernel_session->channel_list.head, list) { | |
163 | /* Copy lttng_channel struct to array */ | |
164 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
165 | channels[i].enabled = kchan->enabled; | |
166 | i++; | |
167 | } | |
168 | } | |
169 | break; | |
170 | case LTTNG_DOMAIN_UST: | |
171 | { | |
172 | struct lttng_ht_iter iter; | |
173 | struct ltt_ust_channel *uchan; | |
174 | ||
e7fe706f | 175 | rcu_read_lock(); |
2f77fc4b DG |
176 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
177 | &iter.iter, uchan, node.node) { | |
178 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); | |
179 | channels[i].attr.overwrite = uchan->attr.overwrite; | |
180 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
181 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
182 | channels[i].attr.switch_timer_interval = | |
183 | uchan->attr.switch_timer_interval; | |
184 | channels[i].attr.read_timer_interval = | |
185 | uchan->attr.read_timer_interval; | |
186 | channels[i].enabled = uchan->enabled; | |
2f785fe7 DG |
187 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
188 | channels[i].attr.tracefile_count = uchan->tracefile_count; | |
2f77fc4b DG |
189 | switch (uchan->attr.output) { |
190 | case LTTNG_UST_MMAP: | |
191 | default: | |
192 | channels[i].attr.output = LTTNG_EVENT_MMAP; | |
193 | break; | |
194 | } | |
195 | i++; | |
196 | } | |
e7fe706f | 197 | rcu_read_unlock(); |
2f77fc4b DG |
198 | break; |
199 | } | |
200 | default: | |
201 | break; | |
202 | } | |
203 | } | |
204 | ||
3c6a091f | 205 | /* |
022d91ba | 206 | * Create a list of agent domain events. |
3c6a091f DG |
207 | * |
208 | * Return number of events in list on success or else a negative value. | |
209 | */ | |
022d91ba | 210 | static int list_lttng_agent_events(struct agent *agt, |
3c6a091f DG |
211 | struct lttng_event **events) |
212 | { | |
213 | int i = 0, ret = 0; | |
214 | unsigned int nb_event = 0; | |
022d91ba | 215 | struct agent_event *event; |
3c6a091f DG |
216 | struct lttng_event *tmp_events; |
217 | struct lttng_ht_iter iter; | |
218 | ||
022d91ba | 219 | assert(agt); |
3c6a091f DG |
220 | assert(events); |
221 | ||
022d91ba | 222 | DBG3("Listing agent events"); |
3c6a091f | 223 | |
e5bbf678 | 224 | rcu_read_lock(); |
022d91ba | 225 | nb_event = lttng_ht_get_count(agt->events); |
e5bbf678 | 226 | rcu_read_unlock(); |
3c6a091f DG |
227 | if (nb_event == 0) { |
228 | ret = nb_event; | |
229 | goto error; | |
230 | } | |
231 | ||
232 | tmp_events = zmalloc(nb_event * sizeof(*tmp_events)); | |
233 | if (!tmp_events) { | |
022d91ba | 234 | PERROR("zmalloc agent events session"); |
3c6a091f DG |
235 | ret = -LTTNG_ERR_FATAL; |
236 | goto error; | |
237 | } | |
238 | ||
239 | rcu_read_lock(); | |
022d91ba | 240 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { |
3c6a091f DG |
241 | strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); |
242 | tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; | |
243 | tmp_events[i].enabled = event->enabled; | |
ff94328f DG |
244 | tmp_events[i].loglevel = event->loglevel; |
245 | tmp_events[i].loglevel_type = event->loglevel_type; | |
3c6a091f DG |
246 | i++; |
247 | } | |
248 | rcu_read_unlock(); | |
249 | ||
250 | *events = tmp_events; | |
251 | ret = nb_event; | |
252 | ||
253 | error: | |
254 | assert(nb_event == i); | |
255 | return ret; | |
256 | } | |
257 | ||
2f77fc4b DG |
258 | /* |
259 | * Create a list of ust global domain events. | |
260 | */ | |
261 | static int list_lttng_ust_global_events(char *channel_name, | |
262 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) | |
263 | { | |
264 | int i = 0, ret = 0; | |
265 | unsigned int nb_event = 0; | |
266 | struct lttng_ht_iter iter; | |
267 | struct lttng_ht_node_str *node; | |
268 | struct ltt_ust_channel *uchan; | |
269 | struct ltt_ust_event *uevent; | |
270 | struct lttng_event *tmp; | |
271 | ||
272 | DBG("Listing UST global events for channel %s", channel_name); | |
273 | ||
274 | rcu_read_lock(); | |
275 | ||
276 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); | |
277 | node = lttng_ht_iter_get_node_str(&iter); | |
278 | if (node == NULL) { | |
f73fabfd | 279 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
280 | goto error; |
281 | } | |
282 | ||
283 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
284 | ||
285 | nb_event += lttng_ht_get_count(uchan->events); | |
286 | ||
287 | if (nb_event == 0) { | |
288 | ret = nb_event; | |
289 | goto error; | |
290 | } | |
291 | ||
292 | DBG3("Listing UST global %d events", nb_event); | |
293 | ||
294 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); | |
295 | if (tmp == NULL) { | |
f73fabfd | 296 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
297 | goto error; |
298 | } | |
299 | ||
300 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
301 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); | |
302 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
303 | tmp[i].enabled = uevent->enabled; | |
304 | ||
305 | switch (uevent->attr.instrumentation) { | |
306 | case LTTNG_UST_TRACEPOINT: | |
307 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
308 | break; | |
309 | case LTTNG_UST_PROBE: | |
310 | tmp[i].type = LTTNG_EVENT_PROBE; | |
311 | break; | |
312 | case LTTNG_UST_FUNCTION: | |
313 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
314 | break; | |
315 | } | |
316 | ||
317 | tmp[i].loglevel = uevent->attr.loglevel; | |
318 | switch (uevent->attr.loglevel_type) { | |
319 | case LTTNG_UST_LOGLEVEL_ALL: | |
320 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
321 | break; | |
322 | case LTTNG_UST_LOGLEVEL_RANGE: | |
323 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; | |
324 | break; | |
325 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
326 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
327 | break; | |
328 | } | |
329 | if (uevent->filter) { | |
330 | tmp[i].filter = 1; | |
331 | } | |
4634f12e JI |
332 | if (uevent->exclusion) { |
333 | tmp[i].exclusion = 1; | |
334 | } | |
2f77fc4b DG |
335 | i++; |
336 | } | |
337 | ||
338 | ret = nb_event; | |
339 | *events = tmp; | |
340 | ||
341 | error: | |
342 | rcu_read_unlock(); | |
343 | return ret; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Fill lttng_event array of all kernel events in the channel. | |
348 | */ | |
349 | static int list_lttng_kernel_events(char *channel_name, | |
350 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) | |
351 | { | |
352 | int i = 0, ret; | |
353 | unsigned int nb_event; | |
354 | struct ltt_kernel_event *event; | |
355 | struct ltt_kernel_channel *kchan; | |
356 | ||
357 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
358 | if (kchan == NULL) { | |
f73fabfd | 359 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
360 | goto error; |
361 | } | |
362 | ||
363 | nb_event = kchan->event_count; | |
364 | ||
365 | DBG("Listing events for channel %s", kchan->channel->name); | |
366 | ||
367 | if (nb_event == 0) { | |
834978fd DG |
368 | *events = NULL; |
369 | goto syscall; | |
2f77fc4b DG |
370 | } |
371 | ||
372 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); | |
373 | if (*events == NULL) { | |
f73fabfd | 374 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
375 | goto error; |
376 | } | |
377 | ||
378 | /* Kernel channels */ | |
379 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
380 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
381 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
382 | (*events)[i].enabled = event->enabled; | |
383 | ||
384 | switch (event->event->instrumentation) { | |
385 | case LTTNG_KERNEL_TRACEPOINT: | |
386 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; | |
387 | break; | |
2f77fc4b | 388 | case LTTNG_KERNEL_KRETPROBE: |
1896972b DG |
389 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
390 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
391 | sizeof(struct lttng_kernel_kprobe)); | |
392 | break; | |
393 | case LTTNG_KERNEL_KPROBE: | |
2f77fc4b DG |
394 | (*events)[i].type = LTTNG_EVENT_PROBE; |
395 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
396 | sizeof(struct lttng_kernel_kprobe)); | |
397 | break; | |
398 | case LTTNG_KERNEL_FUNCTION: | |
399 | (*events)[i].type = LTTNG_EVENT_FUNCTION; | |
400 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
401 | sizeof(struct lttng_kernel_function)); | |
402 | break; | |
403 | case LTTNG_KERNEL_NOOP: | |
404 | (*events)[i].type = LTTNG_EVENT_NOOP; | |
405 | break; | |
406 | case LTTNG_KERNEL_SYSCALL: | |
407 | (*events)[i].type = LTTNG_EVENT_SYSCALL; | |
408 | break; | |
409 | case LTTNG_KERNEL_ALL: | |
410 | assert(0); | |
411 | break; | |
412 | } | |
413 | i++; | |
414 | } | |
415 | ||
834978fd DG |
416 | syscall: |
417 | if (syscall_table) { | |
418 | ssize_t new_size; | |
419 | ||
420 | new_size = syscall_list_channel(kchan, events, nb_event); | |
421 | if (new_size < 0) { | |
422 | free(events); | |
423 | ret = -new_size; | |
424 | goto error; | |
425 | } | |
426 | nb_event = new_size; | |
427 | } | |
428 | ||
2f77fc4b DG |
429 | return nb_event; |
430 | ||
431 | error: | |
f73fabfd DG |
432 | /* Negate the error code to differentiate the size from an error */ |
433 | return -ret; | |
2f77fc4b DG |
434 | } |
435 | ||
436 | /* | |
437 | * Add URI so the consumer output object. Set the correct path depending on the | |
438 | * domain adding the default trace directory. | |
439 | */ | |
440 | static int add_uri_to_consumer(struct consumer_output *consumer, | |
441 | struct lttng_uri *uri, int domain, const char *session_name) | |
442 | { | |
f73fabfd | 443 | int ret = LTTNG_OK; |
2f77fc4b DG |
444 | const char *default_trace_dir; |
445 | ||
446 | assert(uri); | |
447 | ||
448 | if (consumer == NULL) { | |
449 | DBG("No consumer detected. Don't add URI. Stopping."); | |
f73fabfd | 450 | ret = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
451 | goto error; |
452 | } | |
453 | ||
454 | switch (domain) { | |
455 | case LTTNG_DOMAIN_KERNEL: | |
456 | default_trace_dir = DEFAULT_KERNEL_TRACE_DIR; | |
457 | break; | |
458 | case LTTNG_DOMAIN_UST: | |
459 | default_trace_dir = DEFAULT_UST_TRACE_DIR; | |
460 | break; | |
461 | default: | |
462 | /* | |
463 | * This case is possible is we try to add the URI to the global tracing | |
464 | * session consumer object which in this case there is no subdir. | |
465 | */ | |
466 | default_trace_dir = ""; | |
467 | } | |
468 | ||
469 | switch (uri->dtype) { | |
470 | case LTTNG_DST_IPV4: | |
471 | case LTTNG_DST_IPV6: | |
472 | DBG2("Setting network URI to consumer"); | |
473 | ||
df75acac DG |
474 | if (consumer->type == CONSUMER_DST_NET) { |
475 | if ((uri->stype == LTTNG_STREAM_CONTROL && | |
785d2d0d DG |
476 | consumer->dst.net.control_isset) || |
477 | (uri->stype == LTTNG_STREAM_DATA && | |
478 | consumer->dst.net.data_isset)) { | |
df75acac DG |
479 | ret = LTTNG_ERR_URL_EXIST; |
480 | goto error; | |
481 | } | |
482 | } else { | |
483 | memset(&consumer->dst.net, 0, sizeof(consumer->dst.net)); | |
785d2d0d DG |
484 | } |
485 | ||
df75acac DG |
486 | consumer->type = CONSUMER_DST_NET; |
487 | ||
2f77fc4b DG |
488 | /* Set URI into consumer output object */ |
489 | ret = consumer_set_network_uri(consumer, uri); | |
490 | if (ret < 0) { | |
a74934ba | 491 | ret = -ret; |
2f77fc4b DG |
492 | goto error; |
493 | } else if (ret == 1) { | |
494 | /* | |
495 | * URI was the same in the consumer so we do not append the subdir | |
496 | * again so to not duplicate output dir. | |
497 | */ | |
f86c9f4e | 498 | ret = LTTNG_OK; |
2f77fc4b DG |
499 | goto error; |
500 | } | |
501 | ||
502 | if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) { | |
503 | ret = consumer_set_subdir(consumer, session_name); | |
504 | if (ret < 0) { | |
f73fabfd | 505 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
506 | goto error; |
507 | } | |
508 | } | |
509 | ||
510 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
511 | /* On a new subdir, reappend the default trace dir. */ | |
c30ce0b3 CB |
512 | strncat(consumer->subdir, default_trace_dir, |
513 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
514 | DBG3("Append domain trace name to subdir %s", consumer->subdir); |
515 | } | |
516 | ||
517 | break; | |
518 | case LTTNG_DST_PATH: | |
519 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); | |
520 | memset(consumer->dst.trace_path, 0, | |
521 | sizeof(consumer->dst.trace_path)); | |
522 | strncpy(consumer->dst.trace_path, uri->dst.path, | |
523 | sizeof(consumer->dst.trace_path)); | |
524 | /* Append default trace dir */ | |
525 | strncat(consumer->dst.trace_path, default_trace_dir, | |
c30ce0b3 CB |
526 | sizeof(consumer->dst.trace_path) - |
527 | strlen(consumer->dst.trace_path) - 1); | |
2f77fc4b DG |
528 | /* Flag consumer as local. */ |
529 | consumer->type = CONSUMER_DST_LOCAL; | |
530 | break; | |
531 | } | |
532 | ||
a74934ba DG |
533 | ret = LTTNG_OK; |
534 | ||
2f77fc4b DG |
535 | error: |
536 | return ret; | |
537 | } | |
538 | ||
539 | /* | |
540 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
541 | */ | |
542 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
543 | { | |
544 | int ret = 0; | |
545 | struct lttng_ht_iter iter; | |
546 | struct consumer_socket *socket; | |
547 | ||
548 | assert(session); | |
549 | ||
e7fe706f DG |
550 | rcu_read_lock(); |
551 | ||
2f77fc4b DG |
552 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
553 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
554 | socket, node.node) { | |
2f77fc4b | 555 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 556 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
557 | pthread_mutex_unlock(socket->lock); |
558 | if (ret < 0) { | |
f73fabfd | 559 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
560 | goto error; |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | error: | |
e7fe706f | 566 | rcu_read_unlock(); |
2f77fc4b DG |
567 | return ret; |
568 | } | |
569 | ||
570 | /* | |
571 | * Create a socket to the relayd using the URI. | |
572 | * | |
573 | * On success, the relayd_sock pointer is set to the created socket. | |
574 | * Else, it's stays untouched and a lttcomm error code is returned. | |
575 | */ | |
6151a90f JD |
576 | static int create_connect_relayd(struct lttng_uri *uri, |
577 | struct lttcomm_relayd_sock **relayd_sock) | |
2f77fc4b DG |
578 | { |
579 | int ret; | |
6151a90f | 580 | struct lttcomm_relayd_sock *rsock; |
2f77fc4b | 581 | |
6151a90f JD |
582 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
583 | RELAYD_VERSION_COMM_MINOR); | |
584 | if (!rsock) { | |
f73fabfd | 585 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
586 | goto error; |
587 | } | |
588 | ||
ffe60014 DG |
589 | /* |
590 | * Connect to relayd so we can proceed with a session creation. This call | |
591 | * can possibly block for an arbitrary amount of time to set the health | |
592 | * state to be in poll execution. | |
593 | */ | |
594 | health_poll_entry(); | |
6151a90f | 595 | ret = relayd_connect(rsock); |
ffe60014 | 596 | health_poll_exit(); |
2f77fc4b DG |
597 | if (ret < 0) { |
598 | ERR("Unable to reach lttng-relayd"); | |
f73fabfd | 599 | ret = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
600 | goto free_sock; |
601 | } | |
602 | ||
603 | /* Create socket for control stream. */ | |
604 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
605 | DBG3("Creating relayd stream socket from URI"); | |
606 | ||
607 | /* Check relayd version */ | |
6151a90f | 608 | ret = relayd_version_check(rsock); |
2f77fc4b | 609 | if (ret < 0) { |
f73fabfd | 610 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
2f77fc4b DG |
611 | goto close_sock; |
612 | } | |
613 | } else if (uri->stype == LTTNG_STREAM_DATA) { | |
614 | DBG3("Creating relayd data socket from URI"); | |
615 | } else { | |
616 | /* Command is not valid */ | |
617 | ERR("Relayd invalid stream type: %d", uri->stype); | |
f73fabfd | 618 | ret = LTTNG_ERR_INVALID; |
2f77fc4b DG |
619 | goto close_sock; |
620 | } | |
621 | ||
6151a90f | 622 | *relayd_sock = rsock; |
2f77fc4b | 623 | |
f73fabfd | 624 | return LTTNG_OK; |
2f77fc4b DG |
625 | |
626 | close_sock: | |
6151a90f JD |
627 | /* The returned value is not useful since we are on an error path. */ |
628 | (void) relayd_close(rsock); | |
2f77fc4b | 629 | free_sock: |
6151a90f | 630 | free(rsock); |
2f77fc4b DG |
631 | error: |
632 | return ret; | |
633 | } | |
634 | ||
635 | /* | |
636 | * Connect to the relayd using URI and send the socket to the right consumer. | |
637 | */ | |
6dc3064a | 638 | static int send_consumer_relayd_socket(int domain, unsigned int session_id, |
2f77fc4b | 639 | struct lttng_uri *relayd_uri, struct consumer_output *consumer, |
d3e2ba59 JD |
640 | struct consumer_socket *consumer_sock, |
641 | char *session_name, char *hostname, int session_live_timer) | |
2f77fc4b DG |
642 | { |
643 | int ret; | |
6151a90f | 644 | struct lttcomm_relayd_sock *rsock = NULL; |
2f77fc4b | 645 | |
ffe60014 | 646 | /* Connect to relayd and make version check if uri is the control. */ |
6151a90f | 647 | ret = create_connect_relayd(relayd_uri, &rsock); |
ffe60014 | 648 | if (ret != LTTNG_OK) { |
6151a90f | 649 | goto error; |
ffe60014 | 650 | } |
6151a90f | 651 | assert(rsock); |
ffe60014 | 652 | |
2f77fc4b | 653 | /* Set the network sequence index if not set. */ |
d88aee68 DG |
654 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
655 | pthread_mutex_lock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
656 | /* |
657 | * Increment net_seq_idx because we are about to transfer the | |
658 | * new relayd socket to the consumer. | |
d88aee68 | 659 | * Assign unique key so the consumer can match streams. |
2f77fc4b | 660 | */ |
d88aee68 DG |
661 | consumer->net_seq_index = ++relayd_net_seq_idx; |
662 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
663 | } |
664 | ||
2f77fc4b | 665 | /* Send relayd socket to consumer. */ |
6151a90f | 666 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
d3e2ba59 JD |
667 | relayd_uri->stype, session_id, |
668 | session_name, hostname, session_live_timer); | |
2f77fc4b | 669 | if (ret < 0) { |
f73fabfd | 670 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
671 | goto close_sock; |
672 | } | |
673 | ||
c890b720 DG |
674 | /* Flag that the corresponding socket was sent. */ |
675 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
ffe60014 | 676 | consumer_sock->control_sock_sent = 1; |
c890b720 | 677 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
ffe60014 | 678 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
679 | } |
680 | ||
f73fabfd | 681 | ret = LTTNG_OK; |
2f77fc4b DG |
682 | |
683 | /* | |
684 | * Close socket which was dup on the consumer side. The session daemon does | |
685 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
686 | */ | |
687 | ||
688 | close_sock: | |
6151a90f JD |
689 | (void) relayd_close(rsock); |
690 | free(rsock); | |
2f77fc4b | 691 | |
6151a90f | 692 | error: |
ffe60014 DG |
693 | if (ret != LTTNG_OK) { |
694 | /* | |
d9078d0c DG |
695 | * The consumer output for this session should not be used anymore |
696 | * since the relayd connection failed thus making any tracing or/and | |
697 | * streaming not usable. | |
ffe60014 | 698 | */ |
d9078d0c | 699 | consumer->enabled = 0; |
ffe60014 | 700 | } |
2f77fc4b DG |
701 | return ret; |
702 | } | |
703 | ||
704 | /* | |
705 | * Send both relayd sockets to a specific consumer and domain. This is a | |
706 | * helper function to facilitate sending the information to the consumer for a | |
707 | * session. | |
708 | */ | |
6dc3064a | 709 | static int send_consumer_relayd_sockets(int domain, unsigned int session_id, |
d3e2ba59 JD |
710 | struct consumer_output *consumer, struct consumer_socket *sock, |
711 | char *session_name, char *hostname, int session_live_timer) | |
2f77fc4b | 712 | { |
dda67f6c | 713 | int ret = LTTNG_OK; |
2f77fc4b | 714 | |
2f77fc4b | 715 | assert(consumer); |
6dc3064a | 716 | assert(sock); |
2f77fc4b | 717 | |
2f77fc4b | 718 | /* Sending control relayd socket. */ |
ffe60014 | 719 | if (!sock->control_sock_sent) { |
6dc3064a | 720 | ret = send_consumer_relayd_socket(domain, session_id, |
d3e2ba59 JD |
721 | &consumer->dst.net.control, consumer, sock, |
722 | session_name, hostname, session_live_timer); | |
c890b720 DG |
723 | if (ret != LTTNG_OK) { |
724 | goto error; | |
725 | } | |
2f77fc4b DG |
726 | } |
727 | ||
728 | /* Sending data relayd socket. */ | |
ffe60014 | 729 | if (!sock->data_sock_sent) { |
6dc3064a | 730 | ret = send_consumer_relayd_socket(domain, session_id, |
d3e2ba59 JD |
731 | &consumer->dst.net.data, consumer, sock, |
732 | session_name, hostname, session_live_timer); | |
c890b720 DG |
733 | if (ret != LTTNG_OK) { |
734 | goto error; | |
735 | } | |
2f77fc4b DG |
736 | } |
737 | ||
2f77fc4b DG |
738 | error: |
739 | return ret; | |
740 | } | |
741 | ||
742 | /* | |
743 | * Setup relayd connections for a tracing session. First creates the socket to | |
744 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
745 | * network. | |
746 | */ | |
ffe60014 | 747 | int cmd_setup_relayd(struct ltt_session *session) |
2f77fc4b | 748 | { |
f73fabfd | 749 | int ret = LTTNG_OK; |
2f77fc4b DG |
750 | struct ltt_ust_session *usess; |
751 | struct ltt_kernel_session *ksess; | |
752 | struct consumer_socket *socket; | |
753 | struct lttng_ht_iter iter; | |
754 | ||
755 | assert(session); | |
756 | ||
757 | usess = session->ust_session; | |
758 | ksess = session->kernel_session; | |
759 | ||
785d2d0d | 760 | DBG("Setting relayd for session %s", session->name); |
2f77fc4b | 761 | |
e7fe706f DG |
762 | rcu_read_lock(); |
763 | ||
2f77fc4b DG |
764 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
765 | && usess->consumer->enabled) { | |
766 | /* For each consumer socket, send relayd sockets */ | |
767 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
768 | socket, node.node) { | |
2f77fc4b | 769 | pthread_mutex_lock(socket->lock); |
6dc3064a | 770 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
d3e2ba59 JD |
771 | usess->consumer, socket, |
772 | session->name, session->hostname, | |
773 | session->live_timer); | |
2f77fc4b | 774 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 775 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
776 | goto error; |
777 | } | |
6dc3064a DG |
778 | /* Session is now ready for network streaming. */ |
779 | session->net_handle = 1; | |
2f77fc4b DG |
780 | } |
781 | } | |
782 | ||
783 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
784 | && ksess->consumer->enabled) { | |
785 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
786 | socket, node.node) { | |
2f77fc4b | 787 | pthread_mutex_lock(socket->lock); |
6dc3064a | 788 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
d3e2ba59 JD |
789 | ksess->consumer, socket, |
790 | session->name, session->hostname, | |
791 | session->live_timer); | |
2f77fc4b | 792 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 793 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
794 | goto error; |
795 | } | |
6dc3064a DG |
796 | /* Session is now ready for network streaming. */ |
797 | session->net_handle = 1; | |
2f77fc4b DG |
798 | } |
799 | } | |
800 | ||
801 | error: | |
e7fe706f | 802 | rcu_read_unlock(); |
2f77fc4b DG |
803 | return ret; |
804 | } | |
805 | ||
9b6c7ec5 DG |
806 | /* |
807 | * Start a kernel session by opening all necessary streams. | |
808 | */ | |
809 | static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe) | |
810 | { | |
811 | int ret; | |
812 | struct ltt_kernel_channel *kchan; | |
813 | ||
814 | /* Open kernel metadata */ | |
07b86b52 | 815 | if (ksess->metadata == NULL && ksess->output_traces) { |
9b6c7ec5 DG |
816 | ret = kernel_open_metadata(ksess); |
817 | if (ret < 0) { | |
818 | ret = LTTNG_ERR_KERN_META_FAIL; | |
819 | goto error; | |
820 | } | |
821 | } | |
822 | ||
823 | /* Open kernel metadata stream */ | |
07b86b52 | 824 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
9b6c7ec5 DG |
825 | ret = kernel_open_metadata_stream(ksess); |
826 | if (ret < 0) { | |
827 | ERR("Kernel create metadata stream failed"); | |
828 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
829 | goto error; | |
830 | } | |
831 | } | |
832 | ||
833 | /* For each channel */ | |
834 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
835 | if (kchan->stream_count == 0) { | |
836 | ret = kernel_open_channel_stream(kchan); | |
837 | if (ret < 0) { | |
838 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
839 | goto error; | |
840 | } | |
841 | /* Update the stream global counter */ | |
842 | ksess->stream_count_global += ret; | |
843 | } | |
844 | } | |
845 | ||
846 | /* Setup kernel consumer socket and send fds to it */ | |
847 | ret = init_kernel_tracing(ksess); | |
e43c41c5 | 848 | if (ret != 0) { |
9b6c7ec5 DG |
849 | ret = LTTNG_ERR_KERN_START_FAIL; |
850 | goto error; | |
851 | } | |
852 | ||
853 | /* This start the kernel tracing */ | |
854 | ret = kernel_start_session(ksess); | |
855 | if (ret < 0) { | |
856 | ret = LTTNG_ERR_KERN_START_FAIL; | |
857 | goto error; | |
858 | } | |
859 | ||
860 | /* Quiescent wait after starting trace */ | |
784303b0 | 861 | kernel_wait_quiescent(kernel_tracer_fd); |
9b6c7ec5 | 862 | |
14fb1ebe | 863 | ksess->active = 1; |
9b6c7ec5 DG |
864 | |
865 | ret = LTTNG_OK; | |
866 | ||
867 | error: | |
868 | return ret; | |
869 | } | |
870 | ||
2f77fc4b DG |
871 | /* |
872 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
873 | */ | |
874 | int cmd_disable_channel(struct ltt_session *session, int domain, | |
875 | char *channel_name) | |
876 | { | |
877 | int ret; | |
878 | struct ltt_ust_session *usess; | |
879 | ||
880 | usess = session->ust_session; | |
881 | ||
2223c96f DG |
882 | rcu_read_lock(); |
883 | ||
2f77fc4b DG |
884 | switch (domain) { |
885 | case LTTNG_DOMAIN_KERNEL: | |
886 | { | |
887 | ret = channel_kernel_disable(session->kernel_session, | |
888 | channel_name); | |
f73fabfd | 889 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
890 | goto error; |
891 | } | |
892 | ||
893 | kernel_wait_quiescent(kernel_tracer_fd); | |
894 | break; | |
895 | } | |
896 | case LTTNG_DOMAIN_UST: | |
897 | { | |
898 | struct ltt_ust_channel *uchan; | |
899 | struct lttng_ht *chan_ht; | |
900 | ||
901 | chan_ht = usess->domain_global.channels; | |
902 | ||
903 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
904 | if (uchan == NULL) { | |
f73fabfd | 905 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
906 | goto error; |
907 | } | |
908 | ||
7972aab2 | 909 | ret = channel_ust_disable(usess, uchan); |
f73fabfd | 910 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
911 | goto error; |
912 | } | |
913 | break; | |
914 | } | |
915 | #if 0 | |
916 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
917 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
918 | case LTTNG_DOMAIN_UST_PID: | |
919 | #endif | |
920 | default: | |
f73fabfd | 921 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
922 | goto error; |
923 | } | |
924 | ||
f73fabfd | 925 | ret = LTTNG_OK; |
2f77fc4b DG |
926 | |
927 | error: | |
2223c96f | 928 | rcu_read_unlock(); |
2f77fc4b DG |
929 | return ret; |
930 | } | |
931 | ||
932 | /* | |
933 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
934 | * | |
935 | * The wpipe arguments is used as a notifier for the kernel thread. | |
936 | */ | |
937 | int cmd_enable_channel(struct ltt_session *session, | |
7972aab2 | 938 | struct lttng_domain *domain, struct lttng_channel *attr, int wpipe) |
2f77fc4b DG |
939 | { |
940 | int ret; | |
941 | struct ltt_ust_session *usess = session->ust_session; | |
942 | struct lttng_ht *chan_ht; | |
1f345e94 | 943 | size_t len; |
2f77fc4b DG |
944 | |
945 | assert(session); | |
946 | assert(attr); | |
7972aab2 | 947 | assert(domain); |
2f77fc4b | 948 | |
1f345e94 PP |
949 | len = strnlen(attr->name, sizeof(attr->name)); |
950 | ||
951 | /* Validate channel name */ | |
952 | if (attr->name[0] == '.' || | |
953 | memchr(attr->name, '/', len) != NULL) { | |
954 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
955 | goto end; | |
956 | } | |
957 | ||
2f77fc4b DG |
958 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
959 | ||
03b4fdcf DG |
960 | rcu_read_lock(); |
961 | ||
ecc48a90 JD |
962 | /* |
963 | * Don't try to enable a channel if the session has been started at | |
964 | * some point in time before. The tracer does not allow it. | |
965 | */ | |
8382cf6f | 966 | if (session->has_been_started) { |
ecc48a90 JD |
967 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
968 | goto error; | |
969 | } | |
970 | ||
971 | /* | |
972 | * If the session is a live session, remove the switch timer, the | |
973 | * live timer does the same thing but sends also synchronisation | |
974 | * beacons for inactive streams. | |
975 | */ | |
976 | if (session->live_timer > 0) { | |
977 | attr->attr.live_timer_interval = session->live_timer; | |
978 | attr->attr.switch_timer_interval = 0; | |
979 | } | |
980 | ||
33fbd469 DG |
981 | /* |
982 | * The ringbuffer (both in user space and kernel) behave badly in overwrite | |
983 | * mode and with less than 2 subbuf so block it right away and send back an | |
984 | * invalid attribute error. | |
985 | */ | |
986 | if (attr->attr.overwrite && attr->attr.num_subbuf < 2) { | |
987 | ret = LTTNG_ERR_INVALID; | |
988 | goto error; | |
989 | } | |
990 | ||
7972aab2 | 991 | switch (domain->type) { |
2f77fc4b DG |
992 | case LTTNG_DOMAIN_KERNEL: |
993 | { | |
994 | struct ltt_kernel_channel *kchan; | |
995 | ||
2f77fc4b DG |
996 | kchan = trace_kernel_get_channel_by_name(attr->name, |
997 | session->kernel_session); | |
998 | if (kchan == NULL) { | |
999 | ret = channel_kernel_create(session->kernel_session, attr, wpipe); | |
85076754 MD |
1000 | if (attr->name[0] != '\0') { |
1001 | session->kernel_session->has_non_default_channel = 1; | |
1002 | } | |
2f77fc4b DG |
1003 | } else { |
1004 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1005 | } | |
1006 | ||
f73fabfd | 1007 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1008 | goto error; |
1009 | } | |
1010 | ||
784303b0 | 1011 | kernel_wait_quiescent(kernel_tracer_fd); |
2f77fc4b DG |
1012 | break; |
1013 | } | |
1014 | case LTTNG_DOMAIN_UST: | |
1015 | { | |
1016 | struct ltt_ust_channel *uchan; | |
1017 | ||
1018 | chan_ht = usess->domain_global.channels; | |
1019 | ||
1020 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); | |
1021 | if (uchan == NULL) { | |
7972aab2 | 1022 | ret = channel_ust_create(usess, attr, domain->buf_type); |
85076754 MD |
1023 | if (attr->name[0] != '\0') { |
1024 | usess->has_non_default_channel = 1; | |
1025 | } | |
2f77fc4b | 1026 | } else { |
7972aab2 | 1027 | ret = channel_ust_enable(usess, uchan); |
2f77fc4b DG |
1028 | } |
1029 | break; | |
1030 | } | |
2f77fc4b | 1031 | default: |
f73fabfd | 1032 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1033 | goto error; |
1034 | } | |
1035 | ||
1036 | error: | |
2223c96f | 1037 | rcu_read_unlock(); |
1f345e94 | 1038 | end: |
2f77fc4b DG |
1039 | return ret; |
1040 | } | |
1041 | ||
1042 | ||
1043 | /* | |
1044 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
1045 | */ | |
1046 | int cmd_disable_event(struct ltt_session *session, int domain, | |
6e911cad MD |
1047 | char *channel_name, |
1048 | struct lttng_event *event) | |
2f77fc4b DG |
1049 | { |
1050 | int ret; | |
6e911cad MD |
1051 | char *event_name; |
1052 | ||
18a720cd MD |
1053 | DBG("Disable event command for event \'%s\'", event->name); |
1054 | ||
6e911cad MD |
1055 | event_name = event->name; |
1056 | ||
9b7431cf JG |
1057 | /* Error out on unhandled search criteria */ |
1058 | if (event->loglevel_type || event->loglevel != -1 || event->enabled | |
6e911cad MD |
1059 | || event->pid || event->filter || event->exclusion) { |
1060 | return LTTNG_ERR_UNK; | |
1061 | } | |
18a720cd MD |
1062 | /* Special handling for kernel domain all events. */ |
1063 | if (domain == LTTNG_DOMAIN_KERNEL && !strcmp(event_name, "*")) { | |
1064 | return disable_kevent_all(session, domain, channel_name, event); | |
1065 | } | |
2f77fc4b | 1066 | |
2223c96f DG |
1067 | rcu_read_lock(); |
1068 | ||
2f77fc4b DG |
1069 | switch (domain) { |
1070 | case LTTNG_DOMAIN_KERNEL: | |
1071 | { | |
1072 | struct ltt_kernel_channel *kchan; | |
1073 | struct ltt_kernel_session *ksess; | |
1074 | ||
1075 | ksess = session->kernel_session; | |
1076 | ||
85076754 MD |
1077 | /* |
1078 | * If a non-default channel has been created in the | |
1079 | * session, explicitely require that -c chan_name needs | |
1080 | * to be provided. | |
1081 | */ | |
1082 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
1083 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1084 | goto error; | |
1085 | } | |
1086 | ||
2f77fc4b DG |
1087 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
1088 | if (kchan == NULL) { | |
f73fabfd | 1089 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
1090 | goto error; |
1091 | } | |
1092 | ||
6e911cad MD |
1093 | switch (event->type) { |
1094 | case LTTNG_EVENT_ALL: | |
1095 | case LTTNG_EVENT_TRACEPOINT: | |
1096 | ret = event_kernel_disable_tracepoint(kchan, event_name); | |
1097 | if (ret != LTTNG_OK) { | |
1098 | goto error; | |
1099 | } | |
1100 | break; | |
1101 | case LTTNG_EVENT_SYSCALL: | |
1102 | ret = event_kernel_disable_syscall(kchan, event_name); | |
e2b957af MD |
1103 | if (ret != LTTNG_OK) { |
1104 | goto error; | |
1105 | } | |
6e911cad MD |
1106 | break; |
1107 | default: | |
1108 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
1109 | goto error; |
1110 | } | |
1111 | ||
1112 | kernel_wait_quiescent(kernel_tracer_fd); | |
1113 | break; | |
1114 | } | |
1115 | case LTTNG_DOMAIN_UST: | |
1116 | { | |
1117 | struct ltt_ust_channel *uchan; | |
1118 | struct ltt_ust_session *usess; | |
1119 | ||
1120 | usess = session->ust_session; | |
1121 | ||
85076754 MD |
1122 | /* |
1123 | * If a non-default channel has been created in the | |
1124 | * session, explicitely require that -c chan_name needs | |
1125 | * to be provided. | |
1126 | */ | |
1127 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1128 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1129 | goto error; | |
1130 | } | |
1131 | ||
2f77fc4b DG |
1132 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
1133 | channel_name); | |
1134 | if (uchan == NULL) { | |
f73fabfd | 1135 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
1136 | goto error; |
1137 | } | |
1138 | ||
6e911cad MD |
1139 | switch (event->type) { |
1140 | case LTTNG_EVENT_ALL: | |
1141 | ret = event_ust_disable_tracepoint(usess, uchan, event_name); | |
1142 | if (ret != LTTNG_OK) { | |
1143 | goto error; | |
1144 | } | |
1145 | break; | |
1146 | default: | |
1147 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
1148 | goto error; |
1149 | } | |
1150 | ||
1151 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
1152 | channel_name); | |
1153 | break; | |
1154 | } | |
5cdb6027 | 1155 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1156 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1157 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1158 | { |
fefd409b | 1159 | struct agent *agt; |
f20baf8e DG |
1160 | struct ltt_ust_session *usess = session->ust_session; |
1161 | ||
1162 | assert(usess); | |
1163 | ||
6e911cad MD |
1164 | switch (event->type) { |
1165 | case LTTNG_EVENT_ALL: | |
1166 | break; | |
1167 | default: | |
1168 | ret = LTTNG_ERR_UNK; | |
1169 | goto error; | |
1170 | } | |
1171 | ||
5cdb6027 | 1172 | agt = trace_ust_find_agent(usess, domain); |
fefd409b DG |
1173 | if (!agt) { |
1174 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
1175 | goto error; | |
1176 | } | |
18a720cd MD |
1177 | /* The wild card * means that everything should be disabled. */ |
1178 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
1179 | ret = event_agent_disable_all(usess, agt); | |
1180 | } else { | |
1181 | ret = event_agent_disable(usess, agt, event_name); | |
1182 | } | |
f20baf8e DG |
1183 | if (ret != LTTNG_OK) { |
1184 | goto error; | |
1185 | } | |
1186 | ||
1187 | break; | |
1188 | } | |
2f77fc4b DG |
1189 | #if 0 |
1190 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
1191 | case LTTNG_DOMAIN_UST_PID: | |
1192 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
1193 | #endif | |
1194 | default: | |
f73fabfd | 1195 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1196 | goto error; |
1197 | } | |
1198 | ||
f73fabfd | 1199 | ret = LTTNG_OK; |
2f77fc4b DG |
1200 | |
1201 | error: | |
2223c96f | 1202 | rcu_read_unlock(); |
2f77fc4b DG |
1203 | return ret; |
1204 | } | |
1205 | ||
1206 | /* | |
18a720cd | 1207 | * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread. |
2f77fc4b | 1208 | */ |
18a720cd MD |
1209 | static |
1210 | int disable_kevent_all(struct ltt_session *session, int domain, | |
6e911cad MD |
1211 | char *channel_name, |
1212 | struct lttng_event *event) | |
2f77fc4b DG |
1213 | { |
1214 | int ret; | |
1215 | ||
2223c96f DG |
1216 | rcu_read_lock(); |
1217 | ||
2f77fc4b DG |
1218 | switch (domain) { |
1219 | case LTTNG_DOMAIN_KERNEL: | |
1220 | { | |
1221 | struct ltt_kernel_session *ksess; | |
1222 | struct ltt_kernel_channel *kchan; | |
1223 | ||
1224 | ksess = session->kernel_session; | |
1225 | ||
85076754 MD |
1226 | /* |
1227 | * If a non-default channel has been created in the | |
1228 | * session, explicitely require that -c chan_name needs | |
1229 | * to be provided. | |
1230 | */ | |
1231 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
1232 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1233 | goto error; | |
1234 | } | |
1235 | ||
2f77fc4b DG |
1236 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
1237 | if (kchan == NULL) { | |
f73fabfd | 1238 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
1239 | goto error; |
1240 | } | |
1241 | ||
6e911cad MD |
1242 | switch (event->type) { |
1243 | case LTTNG_EVENT_ALL: | |
1244 | ret = event_kernel_disable_all(kchan); | |
1245 | if (ret != LTTNG_OK) { | |
1246 | goto error; | |
1247 | } | |
1248 | break; | |
1249 | case LTTNG_EVENT_SYSCALL: | |
e2b957af MD |
1250 | ret = event_kernel_disable_syscall(kchan, ""); |
1251 | if (ret != LTTNG_OK) { | |
1252 | goto error; | |
1253 | } | |
6e911cad MD |
1254 | break; |
1255 | default: | |
1256 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
1257 | goto error; |
1258 | } | |
1259 | ||
1260 | kernel_wait_quiescent(kernel_tracer_fd); | |
1261 | break; | |
1262 | } | |
2f77fc4b | 1263 | default: |
f73fabfd | 1264 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1265 | goto error; |
1266 | } | |
1267 | ||
f73fabfd | 1268 | ret = LTTNG_OK; |
2f77fc4b DG |
1269 | |
1270 | error: | |
2223c96f | 1271 | rcu_read_unlock(); |
2f77fc4b DG |
1272 | return ret; |
1273 | } | |
1274 | ||
1275 | /* | |
1276 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
1277 | */ | |
1278 | int cmd_add_context(struct ltt_session *session, int domain, | |
601d5acf | 1279 | char *channel_name, struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b | 1280 | { |
d5979e4a | 1281 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
2f77fc4b DG |
1282 | |
1283 | switch (domain) { | |
1284 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
1285 | assert(session->kernel_session); |
1286 | ||
1287 | if (session->kernel_session->channel_count == 0) { | |
1288 | /* Create default channel */ | |
1289 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
1290 | if (ret != LTTNG_OK) { | |
1291 | goto error; | |
1292 | } | |
d5979e4a | 1293 | chan_kern_created = 1; |
979e618e | 1294 | } |
2f77fc4b | 1295 | /* Add kernel context to kernel tracer */ |
601d5acf | 1296 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 1297 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1298 | goto error; |
1299 | } | |
1300 | break; | |
1301 | case LTTNG_DOMAIN_UST: | |
1302 | { | |
1303 | struct ltt_ust_session *usess = session->ust_session; | |
85076754 MD |
1304 | unsigned int chan_count; |
1305 | ||
2f77fc4b DG |
1306 | assert(usess); |
1307 | ||
85076754 | 1308 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
979e618e DG |
1309 | if (chan_count == 0) { |
1310 | struct lttng_channel *attr; | |
1311 | /* Create default channel */ | |
0a9c6494 | 1312 | attr = channel_new_default_attr(domain, usess->buffer_type); |
979e618e DG |
1313 | if (attr == NULL) { |
1314 | ret = LTTNG_ERR_FATAL; | |
1315 | goto error; | |
1316 | } | |
1317 | ||
7972aab2 | 1318 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
979e618e DG |
1319 | if (ret != LTTNG_OK) { |
1320 | free(attr); | |
1321 | goto error; | |
1322 | } | |
1323 | free(attr); | |
d5979e4a | 1324 | chan_ust_created = 1; |
979e618e DG |
1325 | } |
1326 | ||
601d5acf | 1327 | ret = context_ust_add(usess, domain, ctx, channel_name); |
f73fabfd | 1328 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1329 | goto error; |
1330 | } | |
1331 | break; | |
1332 | } | |
1333 | #if 0 | |
1334 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
1335 | case LTTNG_DOMAIN_UST_PID: | |
1336 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
1337 | #endif | |
1338 | default: | |
f73fabfd | 1339 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1340 | goto error; |
1341 | } | |
1342 | ||
d5979e4a | 1343 | return LTTNG_OK; |
2f77fc4b DG |
1344 | |
1345 | error: | |
d5979e4a DG |
1346 | if (chan_kern_created) { |
1347 | struct ltt_kernel_channel *kchan = | |
1348 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, | |
1349 | session->kernel_session); | |
1350 | /* Created previously, this should NOT fail. */ | |
1351 | assert(kchan); | |
1352 | kernel_destroy_channel(kchan); | |
1353 | } | |
1354 | ||
1355 | if (chan_ust_created) { | |
1356 | struct ltt_ust_channel *uchan = | |
1357 | trace_ust_find_channel_by_name( | |
1358 | session->ust_session->domain_global.channels, | |
1359 | DEFAULT_CHANNEL_NAME); | |
1360 | /* Created previously, this should NOT fail. */ | |
1361 | assert(uchan); | |
1362 | /* Remove from the channel list of the session. */ | |
1363 | trace_ust_delete_channel(session->ust_session->domain_global.channels, | |
1364 | uchan); | |
1365 | trace_ust_destroy_channel(uchan); | |
1366 | } | |
2f77fc4b DG |
1367 | return ret; |
1368 | } | |
1369 | ||
930a2e99 JG |
1370 | static int validate_event_name(const char *name) |
1371 | { | |
1372 | int ret = 0; | |
1373 | const char *c = name; | |
1374 | const char *event_name_end = c + LTTNG_SYMBOL_NAME_LEN; | |
1375 | ||
1376 | /* | |
1377 | * Make sure that unescaped wildcards are only used as the last | |
1378 | * character of the event name. | |
1379 | */ | |
1380 | while (c < event_name_end) { | |
1381 | switch (*c) { | |
1382 | case '\0': | |
1383 | goto end; | |
1384 | case '\\': | |
1385 | c++; | |
1386 | break; | |
1387 | case '*': | |
1388 | if ((c + 1) < event_name_end && *(c + 1)) { | |
1389 | /* Wildcard is not the last character */ | |
1390 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1391 | goto end; | |
1392 | } | |
1393 | default: | |
1394 | break; | |
1395 | } | |
1396 | c++; | |
1397 | } | |
1398 | end: | |
1399 | return ret; | |
1400 | } | |
1401 | ||
2f77fc4b DG |
1402 | /* |
1403 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
49d21f93 | 1404 | * We own filter, exclusion, and filter_expression. |
2f77fc4b | 1405 | */ |
7972aab2 | 1406 | int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, |
025faf73 | 1407 | char *channel_name, struct lttng_event *event, |
6b453b5e | 1408 | char *filter_expression, |
db8f1377 JI |
1409 | struct lttng_filter_bytecode *filter, |
1410 | struct lttng_event_exclusion *exclusion, | |
1411 | int wpipe) | |
2f77fc4b | 1412 | { |
e5f5db7f | 1413 | int ret, channel_created = 0; |
2f77fc4b DG |
1414 | struct lttng_channel *attr; |
1415 | ||
1416 | assert(session); | |
1417 | assert(event); | |
1418 | assert(channel_name); | |
1419 | ||
18a720cd MD |
1420 | DBG("Enable event command for event \'%s\'", event->name); |
1421 | ||
1422 | /* Special handling for kernel domain all events. */ | |
1423 | if (domain->type == LTTNG_DOMAIN_KERNEL && !strcmp(event->name, "*")) { | |
1424 | return enable_kevent_all(session, domain, channel_name, event, | |
1425 | filter_expression, filter, wpipe); | |
1426 | } | |
1427 | ||
930a2e99 JG |
1428 | ret = validate_event_name(event->name); |
1429 | if (ret) { | |
1430 | goto error; | |
1431 | } | |
1432 | ||
2223c96f DG |
1433 | rcu_read_lock(); |
1434 | ||
7972aab2 | 1435 | switch (domain->type) { |
2f77fc4b DG |
1436 | case LTTNG_DOMAIN_KERNEL: |
1437 | { | |
1438 | struct ltt_kernel_channel *kchan; | |
1439 | ||
85076754 MD |
1440 | /* |
1441 | * If a non-default channel has been created in the | |
1442 | * session, explicitely require that -c chan_name needs | |
1443 | * to be provided. | |
1444 | */ | |
1445 | if (session->kernel_session->has_non_default_channel | |
1446 | && channel_name[0] == '\0') { | |
1447 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1448 | goto error; | |
1449 | } | |
1450 | ||
2f77fc4b DG |
1451 | kchan = trace_kernel_get_channel_by_name(channel_name, |
1452 | session->kernel_session); | |
1453 | if (kchan == NULL) { | |
0a9c6494 DG |
1454 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
1455 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 1456 | if (attr == NULL) { |
f73fabfd | 1457 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1458 | goto error; |
1459 | } | |
1460 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1461 | ||
1462 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1463 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1464 | free(attr); |
1465 | goto error; | |
1466 | } | |
1467 | free(attr); | |
e5f5db7f DG |
1468 | |
1469 | channel_created = 1; | |
2f77fc4b DG |
1470 | } |
1471 | ||
1472 | /* Get the newly created kernel channel pointer */ | |
1473 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1474 | session->kernel_session); | |
1475 | if (kchan == NULL) { | |
1476 | /* This sould not happen... */ | |
f73fabfd | 1477 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1478 | goto error; |
1479 | } | |
1480 | ||
6e911cad MD |
1481 | switch (event->type) { |
1482 | case LTTNG_EVENT_ALL: | |
6e6ef3d7 DG |
1483 | case LTTNG_EVENT_PROBE: |
1484 | case LTTNG_EVENT_FUNCTION: | |
1485 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
6e911cad MD |
1486 | case LTTNG_EVENT_TRACEPOINT: |
1487 | ret = event_kernel_enable_tracepoint(kchan, event); | |
1488 | if (ret != LTTNG_OK) { | |
1489 | if (channel_created) { | |
1490 | /* Let's not leak a useless channel. */ | |
1491 | kernel_destroy_channel(kchan); | |
1492 | } | |
1493 | goto error; | |
e5f5db7f | 1494 | } |
6e911cad MD |
1495 | break; |
1496 | case LTTNG_EVENT_SYSCALL: | |
1497 | ret = event_kernel_enable_syscall(kchan, event->name); | |
e2b957af MD |
1498 | if (ret != LTTNG_OK) { |
1499 | goto error; | |
1500 | } | |
6e911cad MD |
1501 | break; |
1502 | default: | |
1503 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
1504 | goto error; |
1505 | } | |
1506 | ||
1507 | kernel_wait_quiescent(kernel_tracer_fd); | |
1508 | break; | |
1509 | } | |
1510 | case LTTNG_DOMAIN_UST: | |
1511 | { | |
1512 | struct ltt_ust_channel *uchan; | |
1513 | struct ltt_ust_session *usess = session->ust_session; | |
1514 | ||
1515 | assert(usess); | |
1516 | ||
85076754 MD |
1517 | /* |
1518 | * If a non-default channel has been created in the | |
1519 | * session, explicitely require that -c chan_name needs | |
1520 | * to be provided. | |
1521 | */ | |
1522 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1523 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1524 | goto error; | |
1525 | } | |
1526 | ||
2f77fc4b DG |
1527 | /* Get channel from global UST domain */ |
1528 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
1529 | channel_name); | |
1530 | if (uchan == NULL) { | |
1531 | /* Create default channel */ | |
0a9c6494 DG |
1532 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
1533 | usess->buffer_type); | |
2f77fc4b | 1534 | if (attr == NULL) { |
f73fabfd | 1535 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1536 | goto error; |
1537 | } | |
1538 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1539 | ||
1540 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1541 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1542 | free(attr); |
1543 | goto error; | |
1544 | } | |
1545 | free(attr); | |
1546 | ||
1547 | /* Get the newly created channel reference back */ | |
1548 | uchan = trace_ust_find_channel_by_name( | |
1549 | usess->domain_global.channels, channel_name); | |
1550 | assert(uchan); | |
1551 | } | |
1552 | ||
1553 | /* At this point, the session and channel exist on the tracer */ | |
6b453b5e JG |
1554 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
1555 | filter_expression, filter, exclusion); | |
49d21f93 MD |
1556 | /* We have passed ownership */ |
1557 | filter_expression = NULL; | |
1558 | filter = NULL; | |
1559 | exclusion = NULL; | |
f73fabfd | 1560 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1561 | goto error; |
1562 | } | |
1563 | break; | |
1564 | } | |
5cdb6027 | 1565 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1566 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1567 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1568 | { |
da6c3a50 | 1569 | const char *default_event_name, *default_chan_name; |
fefd409b | 1570 | struct agent *agt; |
f20baf8e DG |
1571 | struct lttng_event uevent; |
1572 | struct lttng_domain tmp_dom; | |
1573 | struct ltt_ust_session *usess = session->ust_session; | |
1574 | ||
1575 | assert(usess); | |
1576 | ||
5cdb6027 | 1577 | agt = trace_ust_find_agent(usess, domain->type); |
fefd409b | 1578 | if (!agt) { |
5cdb6027 | 1579 | agt = agent_create(domain->type); |
fefd409b DG |
1580 | if (!agt) { |
1581 | ret = -LTTNG_ERR_NOMEM; | |
1582 | goto error; | |
1583 | } | |
1584 | agent_add(agt, usess->agents); | |
1585 | } | |
1586 | ||
022d91ba | 1587 | /* Create the default tracepoint. */ |
996de3c7 | 1588 | memset(&uevent, 0, sizeof(uevent)); |
f20baf8e DG |
1589 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
1590 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
da6c3a50 DG |
1591 | default_event_name = event_get_default_agent_ust_name(domain->type); |
1592 | if (!default_event_name) { | |
1593 | ret = -LTTNG_ERR_FATAL; | |
1594 | goto error; | |
f43f95a9 | 1595 | } |
da6c3a50 | 1596 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
f20baf8e DG |
1597 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
1598 | ||
1599 | /* | |
1600 | * The domain type is changed because we are about to enable the | |
1601 | * default channel and event for the JUL domain that are hardcoded. | |
1602 | * This happens in the UST domain. | |
1603 | */ | |
1604 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); | |
1605 | tmp_dom.type = LTTNG_DOMAIN_UST; | |
1606 | ||
0e115563 DG |
1607 | switch (domain->type) { |
1608 | case LTTNG_DOMAIN_LOG4J: | |
da6c3a50 | 1609 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
0e115563 DG |
1610 | break; |
1611 | case LTTNG_DOMAIN_JUL: | |
da6c3a50 | 1612 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
0e115563 DG |
1613 | break; |
1614 | case LTTNG_DOMAIN_PYTHON: | |
1615 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
1616 | break; | |
1617 | default: | |
1618 | /* The switch/case we are in should avoid this else big problem */ | |
1619 | assert(0); | |
da6c3a50 DG |
1620 | } |
1621 | ||
971da06a JG |
1622 | { |
1623 | struct lttng_filter_bytecode *filter_copy = NULL; | |
1624 | ||
1625 | if (filter) { | |
1626 | filter_copy = zmalloc( | |
1627 | sizeof(struct lttng_filter_bytecode) | |
1628 | + filter->len); | |
1629 | if (!filter_copy) { | |
1630 | goto error; | |
1631 | } | |
1632 | ||
1633 | memcpy(filter_copy, filter, | |
1634 | sizeof(struct lttng_filter_bytecode) | |
1635 | + filter->len); | |
1636 | } | |
1637 | ||
1638 | ret = cmd_enable_event(session, &tmp_dom, | |
1639 | (char *) default_chan_name, | |
1640 | &uevent, filter_expression, filter_copy, | |
1641 | NULL, wpipe); | |
1642 | /* We have passed ownership */ | |
1643 | filter_expression = NULL; | |
1644 | } | |
1645 | ||
f20baf8e DG |
1646 | if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) { |
1647 | goto error; | |
1648 | } | |
1649 | ||
1650 | /* The wild card * means that everything should be enabled. */ | |
1651 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
fefd409b | 1652 | ret = event_agent_enable_all(usess, agt, event, filter); |
971da06a | 1653 | filter = NULL; |
f20baf8e | 1654 | } else { |
fefd409b | 1655 | ret = event_agent_enable(usess, agt, event, filter); |
971da06a | 1656 | filter = NULL; |
f20baf8e DG |
1657 | } |
1658 | if (ret != LTTNG_OK) { | |
1659 | goto error; | |
1660 | } | |
1661 | ||
1662 | break; | |
1663 | } | |
2f77fc4b DG |
1664 | #if 0 |
1665 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
1666 | case LTTNG_DOMAIN_UST_PID: | |
1667 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
1668 | #endif | |
1669 | default: | |
f73fabfd | 1670 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1671 | goto error; |
1672 | } | |
1673 | ||
f73fabfd | 1674 | ret = LTTNG_OK; |
2f77fc4b DG |
1675 | |
1676 | error: | |
49d21f93 MD |
1677 | free(filter_expression); |
1678 | free(filter); | |
1679 | free(exclusion); | |
2223c96f | 1680 | rcu_read_unlock(); |
2f77fc4b DG |
1681 | return ret; |
1682 | } | |
1683 | ||
1684 | /* | |
18a720cd | 1685 | * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread. |
2f77fc4b | 1686 | */ |
18a720cd MD |
1687 | static |
1688 | int enable_kevent_all(struct ltt_session *session, | |
1689 | struct lttng_domain *domain, char *channel_name, | |
1690 | struct lttng_event *event, | |
6b453b5e | 1691 | char *filter_expression, |
025faf73 | 1692 | struct lttng_filter_bytecode *filter, int wpipe) |
2f77fc4b DG |
1693 | { |
1694 | int ret; | |
1695 | struct lttng_channel *attr; | |
1696 | ||
1697 | assert(session); | |
1698 | assert(channel_name); | |
1699 | ||
2223c96f DG |
1700 | rcu_read_lock(); |
1701 | ||
7972aab2 | 1702 | switch (domain->type) { |
2f77fc4b DG |
1703 | case LTTNG_DOMAIN_KERNEL: |
1704 | { | |
1705 | struct ltt_kernel_channel *kchan; | |
1706 | ||
1707 | assert(session->kernel_session); | |
1708 | ||
85076754 MD |
1709 | /* |
1710 | * If a non-default channel has been created in the | |
1711 | * session, explicitely require that -c chan_name needs | |
1712 | * to be provided. | |
1713 | */ | |
1714 | if (session->kernel_session->has_non_default_channel | |
1715 | && channel_name[0] == '\0') { | |
1716 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1717 | goto error; | |
1718 | } | |
1719 | ||
2f77fc4b DG |
1720 | kchan = trace_kernel_get_channel_by_name(channel_name, |
1721 | session->kernel_session); | |
1722 | if (kchan == NULL) { | |
1723 | /* Create default channel */ | |
0a9c6494 DG |
1724 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
1725 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 1726 | if (attr == NULL) { |
f73fabfd | 1727 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1728 | goto error; |
1729 | } | |
1730 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1731 | ||
1732 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1733 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1734 | free(attr); |
1735 | goto error; | |
1736 | } | |
1737 | free(attr); | |
1738 | ||
1739 | /* Get the newly created kernel channel pointer */ | |
1740 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1741 | session->kernel_session); | |
1742 | assert(kchan); | |
1743 | } | |
1744 | ||
18a720cd | 1745 | switch (event->type) { |
2f77fc4b | 1746 | case LTTNG_EVENT_SYSCALL: |
6e911cad | 1747 | ret = event_kernel_enable_syscall(kchan, ""); |
e2b957af MD |
1748 | if (ret != LTTNG_OK) { |
1749 | goto error; | |
1750 | } | |
2f77fc4b DG |
1751 | break; |
1752 | case LTTNG_EVENT_TRACEPOINT: | |
1753 | /* | |
1754 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and | |
1755 | * events already registered to the channel. | |
1756 | */ | |
d3a56674 | 1757 | ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd); |
2f77fc4b DG |
1758 | break; |
1759 | case LTTNG_EVENT_ALL: | |
1760 | /* Enable syscalls and tracepoints */ | |
d3a56674 | 1761 | ret = event_kernel_enable_all(kchan, kernel_tracer_fd); |
2f77fc4b DG |
1762 | break; |
1763 | default: | |
f73fabfd | 1764 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
2f77fc4b DG |
1765 | goto error; |
1766 | } | |
1767 | ||
1768 | /* Manage return value */ | |
f73fabfd | 1769 | if (ret != LTTNG_OK) { |
e5f5db7f DG |
1770 | /* |
1771 | * On error, cmd_enable_channel call will take care of destroying | |
1772 | * the created channel if it was needed. | |
1773 | */ | |
2f77fc4b DG |
1774 | goto error; |
1775 | } | |
1776 | ||
1777 | kernel_wait_quiescent(kernel_tracer_fd); | |
1778 | break; | |
1779 | } | |
2f77fc4b | 1780 | default: |
f73fabfd | 1781 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1782 | goto error; |
1783 | } | |
1784 | ||
f73fabfd | 1785 | ret = LTTNG_OK; |
2f77fc4b DG |
1786 | |
1787 | error: | |
2223c96f | 1788 | rcu_read_unlock(); |
2f77fc4b DG |
1789 | return ret; |
1790 | } | |
1791 | ||
1792 | ||
1793 | /* | |
1794 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
1795 | */ | |
1796 | ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) | |
1797 | { | |
1798 | int ret; | |
1799 | ssize_t nb_events = 0; | |
1800 | ||
1801 | switch (domain) { | |
1802 | case LTTNG_DOMAIN_KERNEL: | |
1803 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
1804 | if (nb_events < 0) { | |
f73fabfd | 1805 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
1806 | goto error; |
1807 | } | |
1808 | break; | |
1809 | case LTTNG_DOMAIN_UST: | |
1810 | nb_events = ust_app_list_events(events); | |
1811 | if (nb_events < 0) { | |
f73fabfd | 1812 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
1813 | goto error; |
1814 | } | |
1815 | break; | |
5cdb6027 | 1816 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 1817 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1818 | case LTTNG_DOMAIN_PYTHON: |
f60140a1 | 1819 | nb_events = agent_list_events(events, domain); |
3c6a091f DG |
1820 | if (nb_events < 0) { |
1821 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
1822 | goto error; | |
1823 | } | |
1824 | break; | |
2f77fc4b | 1825 | default: |
f73fabfd | 1826 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1827 | goto error; |
1828 | } | |
1829 | ||
1830 | return nb_events; | |
1831 | ||
1832 | error: | |
1833 | /* Return negative value to differentiate return code */ | |
1834 | return -ret; | |
1835 | } | |
1836 | ||
1837 | /* | |
1838 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
1839 | */ | |
1840 | ssize_t cmd_list_tracepoint_fields(int domain, | |
1841 | struct lttng_event_field **fields) | |
1842 | { | |
1843 | int ret; | |
1844 | ssize_t nb_fields = 0; | |
1845 | ||
1846 | switch (domain) { | |
1847 | case LTTNG_DOMAIN_UST: | |
1848 | nb_fields = ust_app_list_event_fields(fields); | |
1849 | if (nb_fields < 0) { | |
f73fabfd | 1850 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
1851 | goto error; |
1852 | } | |
1853 | break; | |
1854 | case LTTNG_DOMAIN_KERNEL: | |
1855 | default: /* fall-through */ | |
f73fabfd | 1856 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1857 | goto error; |
1858 | } | |
1859 | ||
1860 | return nb_fields; | |
1861 | ||
1862 | error: | |
1863 | /* Return negative value to differentiate return code */ | |
1864 | return -ret; | |
1865 | } | |
1866 | ||
834978fd DG |
1867 | ssize_t cmd_list_syscalls(struct lttng_event **events) |
1868 | { | |
1869 | return syscall_table_list(events); | |
1870 | } | |
1871 | ||
2f77fc4b DG |
1872 | /* |
1873 | * Command LTTNG_START_TRACE processed by the client thread. | |
1874 | */ | |
1875 | int cmd_start_trace(struct ltt_session *session) | |
1876 | { | |
1877 | int ret; | |
cde3e505 | 1878 | unsigned long nb_chan = 0; |
2f77fc4b DG |
1879 | struct ltt_kernel_session *ksession; |
1880 | struct ltt_ust_session *usess; | |
2f77fc4b DG |
1881 | |
1882 | assert(session); | |
1883 | ||
1884 | /* Ease our life a bit ;) */ | |
1885 | ksession = session->kernel_session; | |
1886 | usess = session->ust_session; | |
1887 | ||
8382cf6f DG |
1888 | /* Is the session already started? */ |
1889 | if (session->active) { | |
f73fabfd | 1890 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
1891 | goto error; |
1892 | } | |
1893 | ||
cde3e505 DG |
1894 | /* |
1895 | * Starting a session without channel is useless since after that it's not | |
1896 | * possible to enable channel thus inform the client. | |
1897 | */ | |
1898 | if (usess && usess->domain_global.channels) { | |
a9e41044 | 1899 | rcu_read_lock(); |
cde3e505 | 1900 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); |
a9e41044 | 1901 | rcu_read_unlock(); |
cde3e505 DG |
1902 | } |
1903 | if (ksession) { | |
1904 | nb_chan += ksession->channel_count; | |
1905 | } | |
1906 | if (!nb_chan) { | |
1907 | ret = LTTNG_ERR_NO_CHANNEL; | |
1908 | goto error; | |
1909 | } | |
1910 | ||
2f77fc4b DG |
1911 | /* Kernel tracing */ |
1912 | if (ksession != NULL) { | |
9b6c7ec5 DG |
1913 | ret = start_kernel_session(ksession, kernel_tracer_fd); |
1914 | if (ret != LTTNG_OK) { | |
2f77fc4b DG |
1915 | goto error; |
1916 | } | |
2f77fc4b DG |
1917 | } |
1918 | ||
1919 | /* Flag session that trace should start automatically */ | |
1920 | if (usess) { | |
14fb1ebe DG |
1921 | /* |
1922 | * Even though the start trace might fail, flag this session active so | |
1923 | * other application coming in are started by default. | |
1924 | */ | |
1925 | usess->active = 1; | |
2f77fc4b DG |
1926 | |
1927 | ret = ust_app_start_trace_all(usess); | |
1928 | if (ret < 0) { | |
f73fabfd | 1929 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
1930 | goto error; |
1931 | } | |
1932 | } | |
1933 | ||
8382cf6f DG |
1934 | /* Flag this after a successful start. */ |
1935 | session->has_been_started = 1; | |
1936 | session->active = 1; | |
9b6c7ec5 | 1937 | |
f73fabfd | 1938 | ret = LTTNG_OK; |
2f77fc4b DG |
1939 | |
1940 | error: | |
1941 | return ret; | |
1942 | } | |
1943 | ||
1944 | /* | |
1945 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
1946 | */ | |
1947 | int cmd_stop_trace(struct ltt_session *session) | |
1948 | { | |
1949 | int ret; | |
1950 | struct ltt_kernel_channel *kchan; | |
1951 | struct ltt_kernel_session *ksession; | |
1952 | struct ltt_ust_session *usess; | |
1953 | ||
1954 | assert(session); | |
1955 | ||
1956 | /* Short cut */ | |
1957 | ksession = session->kernel_session; | |
1958 | usess = session->ust_session; | |
1959 | ||
8382cf6f DG |
1960 | /* Session is not active. Skip everythong and inform the client. */ |
1961 | if (!session->active) { | |
f73fabfd | 1962 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
1963 | goto error; |
1964 | } | |
1965 | ||
2f77fc4b | 1966 | /* Kernel tracer */ |
14fb1ebe | 1967 | if (ksession && ksession->active) { |
2f77fc4b DG |
1968 | DBG("Stop kernel tracing"); |
1969 | ||
1970 | /* Flush metadata if exist */ | |
1971 | if (ksession->metadata_stream_fd >= 0) { | |
1972 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
1973 | if (ret < 0) { | |
1974 | ERR("Kernel metadata flush failed"); | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | /* Flush all buffers before stopping */ | |
1979 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
1980 | ret = kernel_flush_buffer(kchan); | |
1981 | if (ret < 0) { | |
1982 | ERR("Kernel flush buffer error"); | |
1983 | } | |
1984 | } | |
1985 | ||
1986 | ret = kernel_stop_session(ksession); | |
1987 | if (ret < 0) { | |
f73fabfd | 1988 | ret = LTTNG_ERR_KERN_STOP_FAIL; |
2f77fc4b DG |
1989 | goto error; |
1990 | } | |
1991 | ||
1992 | kernel_wait_quiescent(kernel_tracer_fd); | |
9b6c7ec5 | 1993 | |
14fb1ebe | 1994 | ksession->active = 0; |
2f77fc4b DG |
1995 | } |
1996 | ||
14fb1ebe DG |
1997 | if (usess && usess->active) { |
1998 | /* | |
1999 | * Even though the stop trace might fail, flag this session inactive so | |
2000 | * other application coming in are not started by default. | |
2001 | */ | |
2002 | usess->active = 0; | |
2f77fc4b DG |
2003 | |
2004 | ret = ust_app_stop_trace_all(usess); | |
2005 | if (ret < 0) { | |
f73fabfd | 2006 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
2007 | goto error; |
2008 | } | |
2009 | } | |
2010 | ||
8382cf6f DG |
2011 | /* Flag inactive after a successful stop. */ |
2012 | session->active = 0; | |
f73fabfd | 2013 | ret = LTTNG_OK; |
2f77fc4b DG |
2014 | |
2015 | error: | |
2016 | return ret; | |
2017 | } | |
2018 | ||
2019 | /* | |
2020 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
2021 | */ | |
bda32d56 JG |
2022 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, |
2023 | struct lttng_uri *uris) | |
2f77fc4b DG |
2024 | { |
2025 | int ret, i; | |
2026 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2027 | struct ltt_ust_session *usess = session->ust_session; | |
2f77fc4b DG |
2028 | |
2029 | assert(session); | |
2030 | assert(uris); | |
2031 | assert(nb_uri > 0); | |
2032 | ||
8382cf6f DG |
2033 | /* Can't set consumer URI if the session is active. */ |
2034 | if (session->active) { | |
f73fabfd | 2035 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2036 | goto error; |
2037 | } | |
2038 | ||
bda32d56 | 2039 | /* Set the "global" consumer URIs */ |
2f77fc4b | 2040 | for (i = 0; i < nb_uri; i++) { |
bda32d56 JG |
2041 | ret = add_uri_to_consumer(session->consumer, |
2042 | &uris[i], 0, session->name); | |
a74934ba | 2043 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2044 | goto error; |
2045 | } | |
2f77fc4b DG |
2046 | } |
2047 | ||
bda32d56 JG |
2048 | /* Set UST session URIs */ |
2049 | if (session->ust_session) { | |
2050 | for (i = 0; i < nb_uri; i++) { | |
2051 | ret = add_uri_to_consumer( | |
2052 | session->ust_session->consumer, | |
2053 | &uris[i], LTTNG_DOMAIN_UST, | |
2054 | session->name); | |
2055 | if (ret != LTTNG_OK) { | |
2056 | goto error; | |
2057 | } | |
2058 | } | |
2059 | } | |
2060 | ||
2061 | /* Set kernel session URIs */ | |
2062 | if (session->kernel_session) { | |
2063 | for (i = 0; i < nb_uri; i++) { | |
2064 | ret = add_uri_to_consumer( | |
2065 | session->kernel_session->consumer, | |
2066 | &uris[i], LTTNG_DOMAIN_KERNEL, | |
2067 | session->name); | |
2068 | if (ret != LTTNG_OK) { | |
2069 | goto error; | |
2070 | } | |
2071 | } | |
2072 | } | |
2073 | ||
7ab70fe0 DG |
2074 | /* |
2075 | * Make sure to set the session in output mode after we set URI since a | |
2076 | * session can be created without URL (thus flagged in no output mode). | |
2077 | */ | |
2078 | session->output_traces = 1; | |
2079 | if (ksess) { | |
2080 | ksess->output_traces = 1; | |
bda32d56 JG |
2081 | } |
2082 | ||
2083 | if (usess) { | |
7ab70fe0 DG |
2084 | usess->output_traces = 1; |
2085 | } | |
2086 | ||
2f77fc4b | 2087 | /* All good! */ |
f73fabfd | 2088 | ret = LTTNG_OK; |
2f77fc4b DG |
2089 | |
2090 | error: | |
2091 | return ret; | |
2092 | } | |
2093 | ||
2094 | /* | |
2095 | * Command LTTNG_CREATE_SESSION processed by the client thread. | |
2096 | */ | |
2097 | int cmd_create_session_uri(char *name, struct lttng_uri *uris, | |
ecc48a90 | 2098 | size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer) |
2f77fc4b DG |
2099 | { |
2100 | int ret; | |
2f77fc4b DG |
2101 | struct ltt_session *session; |
2102 | ||
2103 | assert(name); | |
2bba9e53 | 2104 | assert(creds); |
785d2d0d | 2105 | |
2f77fc4b DG |
2106 | /* |
2107 | * Verify if the session already exist | |
2108 | * | |
2109 | * XXX: There is no need for the session lock list here since the caller | |
2110 | * (process_client_msg) is holding it. We might want to change that so a | |
2111 | * single command does not lock the entire session list. | |
2112 | */ | |
2113 | session = session_find_by_name(name); | |
2114 | if (session != NULL) { | |
f73fabfd | 2115 | ret = LTTNG_ERR_EXIST_SESS; |
2f77fc4b DG |
2116 | goto find_error; |
2117 | } | |
2118 | ||
2119 | /* Create tracing session in the registry */ | |
dec56f6c | 2120 | ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds), |
2f77fc4b | 2121 | LTTNG_SOCK_GET_GID_CRED(creds)); |
f73fabfd | 2122 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2123 | goto session_error; |
2124 | } | |
2125 | ||
2126 | /* | |
2127 | * Get the newly created session pointer back | |
2128 | * | |
2129 | * XXX: There is no need for the session lock list here since the caller | |
2130 | * (process_client_msg) is holding it. We might want to change that so a | |
2131 | * single command does not lock the entire session list. | |
2132 | */ | |
2133 | session = session_find_by_name(name); | |
2134 | assert(session); | |
2135 | ||
ecc48a90 | 2136 | session->live_timer = live_timer; |
2f77fc4b DG |
2137 | /* Create default consumer output for the session not yet created. */ |
2138 | session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
2139 | if (session->consumer == NULL) { | |
f73fabfd | 2140 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2141 | goto consumer_error; |
2142 | } | |
2143 | ||
2bba9e53 | 2144 | if (uris) { |
bda32d56 | 2145 | ret = cmd_set_consumer_uri(session, nb_uri, uris); |
2bba9e53 DG |
2146 | if (ret != LTTNG_OK) { |
2147 | goto consumer_error; | |
2148 | } | |
2149 | session->output_traces = 1; | |
2150 | } else { | |
2151 | session->output_traces = 0; | |
2152 | DBG2("Session %s created with no output", session->name); | |
2f77fc4b DG |
2153 | } |
2154 | ||
2155 | session->consumer->enabled = 1; | |
2156 | ||
f73fabfd | 2157 | return LTTNG_OK; |
2f77fc4b DG |
2158 | |
2159 | consumer_error: | |
2160 | session_destroy(session); | |
2161 | session_error: | |
2162 | find_error: | |
2163 | return ret; | |
2164 | } | |
2165 | ||
27babd3a DG |
2166 | /* |
2167 | * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread. | |
2168 | */ | |
2169 | int cmd_create_session_snapshot(char *name, struct lttng_uri *uris, | |
2170 | size_t nb_uri, lttng_sock_cred *creds) | |
2171 | { | |
2172 | int ret; | |
2173 | struct ltt_session *session; | |
2174 | struct snapshot_output *new_output = NULL; | |
2175 | ||
2176 | assert(name); | |
2177 | assert(creds); | |
2178 | ||
2179 | /* | |
2180 | * Create session in no output mode with URIs set to NULL. The uris we've | |
2181 | * received are for a default snapshot output if one. | |
2182 | */ | |
ecc48a90 | 2183 | ret = cmd_create_session_uri(name, NULL, 0, creds, -1); |
27babd3a DG |
2184 | if (ret != LTTNG_OK) { |
2185 | goto error; | |
2186 | } | |
2187 | ||
2188 | /* Get the newly created session pointer back. This should NEVER fail. */ | |
2189 | session = session_find_by_name(name); | |
2190 | assert(session); | |
2191 | ||
2192 | /* Flag session for snapshot mode. */ | |
2193 | session->snapshot_mode = 1; | |
2194 | ||
2195 | /* Skip snapshot output creation if no URI is given. */ | |
2196 | if (nb_uri == 0) { | |
2197 | goto end; | |
2198 | } | |
2199 | ||
2200 | new_output = snapshot_output_alloc(); | |
2201 | if (!new_output) { | |
2202 | ret = LTTNG_ERR_NOMEM; | |
2203 | goto error_snapshot_alloc; | |
2204 | } | |
2205 | ||
2206 | ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL, | |
2207 | uris, nb_uri, session->consumer, new_output, &session->snapshot); | |
2208 | if (ret < 0) { | |
2209 | if (ret == -ENOMEM) { | |
2210 | ret = LTTNG_ERR_NOMEM; | |
2211 | } else { | |
2212 | ret = LTTNG_ERR_INVALID; | |
2213 | } | |
2214 | goto error_snapshot; | |
2215 | } | |
2216 | ||
2217 | rcu_read_lock(); | |
2218 | snapshot_add_output(&session->snapshot, new_output); | |
2219 | rcu_read_unlock(); | |
2220 | ||
2221 | end: | |
2222 | return LTTNG_OK; | |
2223 | ||
2224 | error_snapshot: | |
2225 | snapshot_output_destroy(new_output); | |
2226 | error_snapshot_alloc: | |
2227 | session_destroy(session); | |
2228 | error: | |
2229 | return ret; | |
2230 | } | |
2231 | ||
2f77fc4b DG |
2232 | /* |
2233 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
2234 | */ | |
2235 | int cmd_destroy_session(struct ltt_session *session, int wpipe) | |
2236 | { | |
2237 | int ret; | |
2238 | struct ltt_ust_session *usess; | |
2239 | struct ltt_kernel_session *ksess; | |
2240 | ||
2241 | /* Safety net */ | |
2242 | assert(session); | |
2243 | ||
2244 | usess = session->ust_session; | |
2245 | ksess = session->kernel_session; | |
2246 | ||
2247 | /* Clean kernel session teardown */ | |
2248 | kernel_destroy_session(ksess); | |
2249 | ||
2250 | /* UST session teardown */ | |
2251 | if (usess) { | |
2252 | /* Close any relayd session */ | |
2253 | consumer_output_send_destroy_relayd(usess->consumer); | |
2254 | ||
2255 | /* Destroy every UST application related to this session. */ | |
2256 | ret = ust_app_destroy_trace_all(usess); | |
2257 | if (ret) { | |
2258 | ERR("Error in ust_app_destroy_trace_all"); | |
2259 | } | |
2260 | ||
2261 | /* Clean up the rest. */ | |
2262 | trace_ust_destroy_session(usess); | |
2263 | } | |
2264 | ||
2265 | /* | |
2266 | * Must notify the kernel thread here to update it's poll set in order to | |
2267 | * remove the channel(s)' fd just destroyed. | |
2268 | */ | |
2269 | ret = notify_thread_pipe(wpipe); | |
2270 | if (ret < 0) { | |
2271 | PERROR("write kernel poll pipe"); | |
2272 | } | |
2273 | ||
2274 | ret = session_destroy(session); | |
2275 | ||
2276 | return ret; | |
2277 | } | |
2278 | ||
2279 | /* | |
2280 | * Command LTTNG_CALIBRATE processed by the client thread. | |
2281 | */ | |
2282 | int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) | |
2283 | { | |
2284 | int ret; | |
2285 | ||
2286 | switch (domain) { | |
2287 | case LTTNG_DOMAIN_KERNEL: | |
2288 | { | |
2289 | struct lttng_kernel_calibrate kcalibrate; | |
2290 | ||
2e84128e DG |
2291 | switch (calibrate->type) { |
2292 | case LTTNG_CALIBRATE_FUNCTION: | |
2293 | default: | |
2294 | /* Default and only possible calibrate option. */ | |
2295 | kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE; | |
2296 | break; | |
2297 | } | |
2298 | ||
2f77fc4b DG |
2299 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); |
2300 | if (ret < 0) { | |
f73fabfd | 2301 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
2f77fc4b DG |
2302 | goto error; |
2303 | } | |
2304 | break; | |
2305 | } | |
2306 | case LTTNG_DOMAIN_UST: | |
2307 | { | |
2308 | struct lttng_ust_calibrate ucalibrate; | |
2309 | ||
2e84128e DG |
2310 | switch (calibrate->type) { |
2311 | case LTTNG_CALIBRATE_FUNCTION: | |
2312 | default: | |
2313 | /* Default and only possible calibrate option. */ | |
2314 | ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT; | |
2315 | break; | |
2316 | } | |
2317 | ||
2f77fc4b DG |
2318 | ret = ust_app_calibrate_glb(&ucalibrate); |
2319 | if (ret < 0) { | |
f73fabfd | 2320 | ret = LTTNG_ERR_UST_CALIBRATE_FAIL; |
2f77fc4b DG |
2321 | goto error; |
2322 | } | |
2323 | break; | |
2324 | } | |
2325 | default: | |
f73fabfd | 2326 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2327 | goto error; |
2328 | } | |
2329 | ||
f73fabfd | 2330 | ret = LTTNG_OK; |
2f77fc4b DG |
2331 | |
2332 | error: | |
2333 | return ret; | |
2334 | } | |
2335 | ||
2336 | /* | |
2337 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
2338 | */ | |
2339 | int cmd_register_consumer(struct ltt_session *session, int domain, | |
2340 | const char *sock_path, struct consumer_data *cdata) | |
2341 | { | |
2342 | int ret, sock; | |
dd81b457 | 2343 | struct consumer_socket *socket = NULL; |
2f77fc4b DG |
2344 | |
2345 | assert(session); | |
2346 | assert(cdata); | |
2347 | assert(sock_path); | |
2348 | ||
2349 | switch (domain) { | |
2350 | case LTTNG_DOMAIN_KERNEL: | |
2351 | { | |
2352 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2353 | ||
2354 | assert(ksess); | |
2355 | ||
2356 | /* Can't register a consumer if there is already one */ | |
2357 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 2358 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
2359 | goto error; |
2360 | } | |
2361 | ||
2362 | sock = lttcomm_connect_unix_sock(sock_path); | |
2363 | if (sock < 0) { | |
f73fabfd | 2364 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
2365 | goto error; |
2366 | } | |
4ce514c4 | 2367 | cdata->cmd_sock = sock; |
2f77fc4b | 2368 | |
4ce514c4 | 2369 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
2f77fc4b | 2370 | if (socket == NULL) { |
f66c074c DG |
2371 | ret = close(sock); |
2372 | if (ret < 0) { | |
2373 | PERROR("close register consumer"); | |
2374 | } | |
4ce514c4 | 2375 | cdata->cmd_sock = -1; |
f73fabfd | 2376 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2377 | goto error; |
2378 | } | |
2379 | ||
2380 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
2381 | if (socket->lock == NULL) { | |
2382 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 2383 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2384 | goto error; |
2385 | } | |
2386 | pthread_mutex_init(socket->lock, NULL); | |
2387 | socket->registered = 1; | |
2388 | ||
2389 | rcu_read_lock(); | |
2390 | consumer_add_socket(socket, ksess->consumer); | |
2391 | rcu_read_unlock(); | |
2392 | ||
2393 | pthread_mutex_lock(&cdata->pid_mutex); | |
2394 | cdata->pid = -1; | |
2395 | pthread_mutex_unlock(&cdata->pid_mutex); | |
2396 | ||
2397 | break; | |
2398 | } | |
2399 | default: | |
2400 | /* TODO: Userspace tracing */ | |
f73fabfd | 2401 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2402 | goto error; |
2403 | } | |
2404 | ||
dd81b457 | 2405 | return LTTNG_OK; |
2f77fc4b DG |
2406 | |
2407 | error: | |
dd81b457 DG |
2408 | if (socket) { |
2409 | consumer_destroy_socket(socket); | |
2410 | } | |
2f77fc4b DG |
2411 | return ret; |
2412 | } | |
2413 | ||
2414 | /* | |
2415 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
2416 | */ | |
2417 | ssize_t cmd_list_domains(struct ltt_session *session, | |
2418 | struct lttng_domain **domains) | |
2419 | { | |
2420 | int ret, index = 0; | |
2421 | ssize_t nb_dom = 0; | |
fefd409b DG |
2422 | struct agent *agt; |
2423 | struct lttng_ht_iter iter; | |
2f77fc4b DG |
2424 | |
2425 | if (session->kernel_session != NULL) { | |
2426 | DBG3("Listing domains found kernel domain"); | |
2427 | nb_dom++; | |
2428 | } | |
2429 | ||
2430 | if (session->ust_session != NULL) { | |
2431 | DBG3("Listing domains found UST global domain"); | |
2432 | nb_dom++; | |
3c6a091f | 2433 | |
e0a74f01 | 2434 | rcu_read_lock(); |
fefd409b DG |
2435 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
2436 | agt, node.node) { | |
2437 | if (agt->being_used) { | |
2438 | nb_dom++; | |
2439 | } | |
3c6a091f | 2440 | } |
e0a74f01 | 2441 | rcu_read_unlock(); |
2f77fc4b DG |
2442 | } |
2443 | ||
fa64dfb4 JG |
2444 | if (!nb_dom) { |
2445 | goto end; | |
2446 | } | |
2447 | ||
2f77fc4b DG |
2448 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
2449 | if (*domains == NULL) { | |
f73fabfd | 2450 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2451 | goto error; |
2452 | } | |
2453 | ||
2454 | if (session->kernel_session != NULL) { | |
2455 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
2456 | index++; | |
2457 | } | |
2458 | ||
2459 | if (session->ust_session != NULL) { | |
2460 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
88c5f0d8 | 2461 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
2f77fc4b | 2462 | index++; |
3c6a091f | 2463 | |
e0a74f01 | 2464 | rcu_read_lock(); |
fefd409b DG |
2465 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
2466 | agt, node.node) { | |
2467 | if (agt->being_used) { | |
2468 | (*domains)[index].type = agt->domain; | |
2469 | (*domains)[index].buf_type = session->ust_session->buffer_type; | |
2470 | index++; | |
2471 | } | |
3c6a091f | 2472 | } |
e0a74f01 | 2473 | rcu_read_unlock(); |
2f77fc4b | 2474 | } |
fa64dfb4 | 2475 | end: |
2f77fc4b DG |
2476 | return nb_dom; |
2477 | ||
2478 | error: | |
f73fabfd DG |
2479 | /* Return negative value to differentiate return code */ |
2480 | return -ret; | |
2f77fc4b DG |
2481 | } |
2482 | ||
2483 | ||
2484 | /* | |
2485 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
2486 | */ | |
2487 | ssize_t cmd_list_channels(int domain, struct ltt_session *session, | |
2488 | struct lttng_channel **channels) | |
2489 | { | |
2490 | int ret; | |
2491 | ssize_t nb_chan = 0; | |
2492 | ||
2493 | switch (domain) { | |
2494 | case LTTNG_DOMAIN_KERNEL: | |
2495 | if (session->kernel_session != NULL) { | |
2496 | nb_chan = session->kernel_session->channel_count; | |
2497 | } | |
2498 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 DG |
2499 | if (nb_chan <= 0) { |
2500 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; | |
2501 | } | |
2f77fc4b DG |
2502 | break; |
2503 | case LTTNG_DOMAIN_UST: | |
2504 | if (session->ust_session != NULL) { | |
7ffc31a2 | 2505 | rcu_read_lock(); |
2f77fc4b | 2506 | nb_chan = lttng_ht_get_count( |
7ffc31a2 JG |
2507 | session->ust_session->domain_global.channels); |
2508 | rcu_read_unlock(); | |
2f77fc4b DG |
2509 | } |
2510 | DBG3("Number of UST global channels %zd", nb_chan); | |
ade7ce52 | 2511 | if (nb_chan < 0) { |
c7d620a2 | 2512 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
ade7ce52 | 2513 | goto error; |
c7d620a2 | 2514 | } |
2f77fc4b DG |
2515 | break; |
2516 | default: | |
f73fabfd | 2517 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2518 | goto error; |
2519 | } | |
2520 | ||
2521 | if (nb_chan > 0) { | |
2522 | *channels = zmalloc(nb_chan * sizeof(struct lttng_channel)); | |
2523 | if (*channels == NULL) { | |
f73fabfd | 2524 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2525 | goto error; |
2526 | } | |
2527 | ||
2528 | list_lttng_channels(domain, session, *channels); | |
2f77fc4b DG |
2529 | } |
2530 | ||
2531 | return nb_chan; | |
2532 | ||
2533 | error: | |
f73fabfd DG |
2534 | /* Return negative value to differentiate return code */ |
2535 | return -ret; | |
2f77fc4b DG |
2536 | } |
2537 | ||
2538 | /* | |
2539 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
2540 | */ | |
2541 | ssize_t cmd_list_events(int domain, struct ltt_session *session, | |
2542 | char *channel_name, struct lttng_event **events) | |
2543 | { | |
2544 | int ret = 0; | |
2545 | ssize_t nb_event = 0; | |
2546 | ||
2547 | switch (domain) { | |
2548 | case LTTNG_DOMAIN_KERNEL: | |
2549 | if (session->kernel_session != NULL) { | |
2550 | nb_event = list_lttng_kernel_events(channel_name, | |
2551 | session->kernel_session, events); | |
2552 | } | |
2553 | break; | |
2554 | case LTTNG_DOMAIN_UST: | |
2555 | { | |
2556 | if (session->ust_session != NULL) { | |
2557 | nb_event = list_lttng_ust_global_events(channel_name, | |
2558 | &session->ust_session->domain_global, events); | |
2559 | } | |
2560 | break; | |
2561 | } | |
5cdb6027 | 2562 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 2563 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2564 | case LTTNG_DOMAIN_PYTHON: |
3c6a091f | 2565 | if (session->ust_session) { |
fefd409b DG |
2566 | struct lttng_ht_iter iter; |
2567 | struct agent *agt; | |
2568 | ||
b11feea5 | 2569 | rcu_read_lock(); |
fefd409b DG |
2570 | cds_lfht_for_each_entry(session->ust_session->agents->ht, |
2571 | &iter.iter, agt, node.node) { | |
2572 | nb_event = list_lttng_agent_events(agt, events); | |
2573 | } | |
b11feea5 | 2574 | rcu_read_unlock(); |
3c6a091f DG |
2575 | } |
2576 | break; | |
2f77fc4b | 2577 | default: |
f73fabfd | 2578 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2579 | goto error; |
2580 | } | |
2581 | ||
f73fabfd | 2582 | return nb_event; |
2f77fc4b DG |
2583 | |
2584 | error: | |
f73fabfd DG |
2585 | /* Return negative value to differentiate return code */ |
2586 | return -ret; | |
2f77fc4b DG |
2587 | } |
2588 | ||
2589 | /* | |
2590 | * Using the session list, filled a lttng_session array to send back to the | |
2591 | * client for session listing. | |
2592 | * | |
2593 | * The session list lock MUST be acquired before calling this function. Use | |
2594 | * session_lock_list() and session_unlock_list(). | |
2595 | */ | |
2596 | void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid, | |
2597 | gid_t gid) | |
2598 | { | |
2599 | int ret; | |
2600 | unsigned int i = 0; | |
2601 | struct ltt_session *session; | |
2602 | struct ltt_session_list *list = session_get_list(); | |
2603 | ||
2604 | DBG("Getting all available session for UID %d GID %d", | |
2605 | uid, gid); | |
2606 | /* | |
2607 | * Iterate over session list and append data after the control struct in | |
2608 | * the buffer. | |
2609 | */ | |
2610 | cds_list_for_each_entry(session, &list->head, list) { | |
2611 | /* | |
2612 | * Only list the sessions the user can control. | |
2613 | */ | |
2614 | if (!session_access_ok(session, uid, gid)) { | |
2615 | continue; | |
2616 | } | |
2617 | ||
2618 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2619 | struct ltt_ust_session *usess = session->ust_session; | |
2620 | ||
2621 | if (session->consumer->type == CONSUMER_DST_NET || | |
2622 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
2623 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
2624 | ret = build_network_session_path(sessions[i].path, | |
dec56f6c | 2625 | sizeof(sessions[i].path), session); |
2f77fc4b | 2626 | } else { |
dec56f6c | 2627 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
2f77fc4b DG |
2628 | session->consumer->dst.trace_path); |
2629 | } | |
2630 | if (ret < 0) { | |
2631 | PERROR("snprintf session path"); | |
2632 | continue; | |
2633 | } | |
2634 | ||
2635 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
2636 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
8382cf6f | 2637 | sessions[i].enabled = session->active; |
2cbf8fed | 2638 | sessions[i].snapshot_mode = session->snapshot_mode; |
8960e9cd | 2639 | sessions[i].live_timer_interval = session->live_timer; |
2f77fc4b DG |
2640 | i++; |
2641 | } | |
2642 | } | |
2643 | ||
806e2684 | 2644 | /* |
6d805429 | 2645 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 2646 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 2647 | */ |
6d805429 | 2648 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
2649 | { |
2650 | int ret; | |
2651 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2652 | struct ltt_ust_session *usess = session->ust_session; | |
2653 | ||
2654 | assert(session); | |
2655 | ||
2656 | /* Session MUST be stopped to ask for data availability. */ | |
8382cf6f | 2657 | if (session->active) { |
806e2684 DG |
2658 | ret = LTTNG_ERR_SESSION_STARTED; |
2659 | goto error; | |
3a89d11a DG |
2660 | } else { |
2661 | /* | |
2662 | * If stopped, just make sure we've started before else the above call | |
2663 | * will always send that there is data pending. | |
2664 | * | |
2665 | * The consumer assumes that when the data pending command is received, | |
2666 | * the trace has been started before or else no output data is written | |
2667 | * by the streams which is a condition for data pending. So, this is | |
2668 | * *VERY* important that we don't ask the consumer before a start | |
2669 | * trace. | |
2670 | */ | |
8382cf6f | 2671 | if (!session->has_been_started) { |
3a89d11a DG |
2672 | ret = 0; |
2673 | goto error; | |
2674 | } | |
806e2684 DG |
2675 | } |
2676 | ||
2677 | if (ksess && ksess->consumer) { | |
6d805429 DG |
2678 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
2679 | if (ret == 1) { | |
806e2684 DG |
2680 | /* Data is still being extracted for the kernel. */ |
2681 | goto error; | |
2682 | } | |
2683 | } | |
2684 | ||
2685 | if (usess && usess->consumer) { | |
6d805429 DG |
2686 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
2687 | if (ret == 1) { | |
806e2684 DG |
2688 | /* Data is still being extracted for the kernel. */ |
2689 | goto error; | |
2690 | } | |
2691 | } | |
2692 | ||
2693 | /* Data is ready to be read by a viewer */ | |
6d805429 | 2694 | ret = 0; |
806e2684 DG |
2695 | |
2696 | error: | |
2697 | return ret; | |
2698 | } | |
2699 | ||
6dc3064a DG |
2700 | /* |
2701 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
2702 | * | |
2703 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
2704 | */ | |
2705 | int cmd_snapshot_add_output(struct ltt_session *session, | |
2706 | struct lttng_snapshot_output *output, uint32_t *id) | |
2707 | { | |
2708 | int ret; | |
2709 | struct snapshot_output *new_output; | |
2710 | ||
2711 | assert(session); | |
2712 | assert(output); | |
2713 | ||
2714 | DBG("Cmd snapshot add output for session %s", session->name); | |
2715 | ||
2716 | /* | |
d3f14b8a MD |
2717 | * Permission denied to create an output if the session is not |
2718 | * set in no output mode. | |
6dc3064a DG |
2719 | */ |
2720 | if (session->output_traces) { | |
2721 | ret = LTTNG_ERR_EPERM; | |
2722 | goto error; | |
2723 | } | |
2724 | ||
2725 | /* Only one output is allowed until we have the "tee" feature. */ | |
2726 | if (session->snapshot.nb_output == 1) { | |
2727 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
2728 | goto error; | |
2729 | } | |
2730 | ||
2731 | new_output = snapshot_output_alloc(); | |
2732 | if (!new_output) { | |
2733 | ret = LTTNG_ERR_NOMEM; | |
2734 | goto error; | |
2735 | } | |
2736 | ||
2737 | ret = snapshot_output_init(output->max_size, output->name, | |
2738 | output->ctrl_url, output->data_url, session->consumer, new_output, | |
2739 | &session->snapshot); | |
2740 | if (ret < 0) { | |
2741 | if (ret == -ENOMEM) { | |
2742 | ret = LTTNG_ERR_NOMEM; | |
2743 | } else { | |
2744 | ret = LTTNG_ERR_INVALID; | |
2745 | } | |
2746 | goto free_error; | |
2747 | } | |
2748 | ||
6dc3064a DG |
2749 | rcu_read_lock(); |
2750 | snapshot_add_output(&session->snapshot, new_output); | |
2751 | if (id) { | |
2752 | *id = new_output->id; | |
2753 | } | |
2754 | rcu_read_unlock(); | |
2755 | ||
2756 | return LTTNG_OK; | |
2757 | ||
2758 | free_error: | |
2759 | snapshot_output_destroy(new_output); | |
2760 | error: | |
2761 | return ret; | |
2762 | } | |
2763 | ||
2764 | /* | |
2765 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
2766 | * | |
2767 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
2768 | */ | |
2769 | int cmd_snapshot_del_output(struct ltt_session *session, | |
2770 | struct lttng_snapshot_output *output) | |
2771 | { | |
2772 | int ret; | |
eb240553 | 2773 | struct snapshot_output *sout = NULL; |
6dc3064a DG |
2774 | |
2775 | assert(session); | |
2776 | assert(output); | |
2777 | ||
6dc3064a DG |
2778 | rcu_read_lock(); |
2779 | ||
2780 | /* | |
d3f14b8a MD |
2781 | * Permission denied to create an output if the session is not |
2782 | * set in no output mode. | |
6dc3064a DG |
2783 | */ |
2784 | if (session->output_traces) { | |
2785 | ret = LTTNG_ERR_EPERM; | |
2786 | goto error; | |
2787 | } | |
2788 | ||
eb240553 DG |
2789 | if (output->id) { |
2790 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
2791 | session->name); | |
2792 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
2793 | } else if (*output->name != '\0') { | |
2794 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
2795 | session->name); | |
2796 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
2797 | } | |
6dc3064a DG |
2798 | if (!sout) { |
2799 | ret = LTTNG_ERR_INVALID; | |
2800 | goto error; | |
2801 | } | |
2802 | ||
2803 | snapshot_delete_output(&session->snapshot, sout); | |
2804 | snapshot_output_destroy(sout); | |
2805 | ret = LTTNG_OK; | |
2806 | ||
2807 | error: | |
2808 | rcu_read_unlock(); | |
2809 | return ret; | |
2810 | } | |
2811 | ||
2812 | /* | |
2813 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
2814 | * | |
2815 | * If no output is available, outputs is untouched and 0 is returned. | |
2816 | * | |
2817 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
2818 | */ | |
2819 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
2820 | struct lttng_snapshot_output **outputs) | |
2821 | { | |
2822 | int ret, idx = 0; | |
b223ca94 | 2823 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
2824 | struct lttng_ht_iter iter; |
2825 | struct snapshot_output *output; | |
2826 | ||
2827 | assert(session); | |
2828 | assert(outputs); | |
2829 | ||
2830 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
2831 | ||
2832 | /* | |
d3f14b8a MD |
2833 | * Permission denied to create an output if the session is not |
2834 | * set in no output mode. | |
6dc3064a DG |
2835 | */ |
2836 | if (session->output_traces) { | |
b223ca94 | 2837 | ret = -LTTNG_ERR_EPERM; |
6dc3064a DG |
2838 | goto error; |
2839 | } | |
2840 | ||
2841 | if (session->snapshot.nb_output == 0) { | |
2842 | ret = 0; | |
2843 | goto error; | |
2844 | } | |
2845 | ||
2846 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); | |
2847 | if (!list) { | |
b223ca94 | 2848 | ret = -LTTNG_ERR_NOMEM; |
6dc3064a DG |
2849 | goto error; |
2850 | } | |
2851 | ||
2852 | /* Copy list from session to the new list object. */ | |
b223ca94 | 2853 | rcu_read_lock(); |
6dc3064a DG |
2854 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
2855 | output, node.node) { | |
2856 | assert(output->consumer); | |
2857 | list[idx].id = output->id; | |
2858 | list[idx].max_size = output->max_size; | |
2859 | strncpy(list[idx].name, output->name, sizeof(list[idx].name)); | |
2860 | if (output->consumer->type == CONSUMER_DST_LOCAL) { | |
2861 | strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path, | |
2862 | sizeof(list[idx].ctrl_url)); | |
2863 | } else { | |
2864 | /* Control URI. */ | |
2865 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
2866 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
2867 | if (ret < 0) { | |
b223ca94 JG |
2868 | ret = -LTTNG_ERR_NOMEM; |
2869 | goto error; | |
6dc3064a DG |
2870 | } |
2871 | ||
2872 | /* Data URI. */ | |
2873 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
2874 | list[idx].data_url, sizeof(list[idx].data_url)); | |
2875 | if (ret < 0) { | |
b223ca94 JG |
2876 | ret = -LTTNG_ERR_NOMEM; |
2877 | goto error; | |
6dc3064a DG |
2878 | } |
2879 | } | |
2880 | idx++; | |
2881 | } | |
2882 | ||
2883 | *outputs = list; | |
b223ca94 JG |
2884 | list = NULL; |
2885 | ret = session->snapshot.nb_output; | |
6dc3064a | 2886 | error: |
b223ca94 JG |
2887 | free(list); |
2888 | rcu_read_unlock(); | |
2889 | return ret; | |
6dc3064a DG |
2890 | } |
2891 | ||
2892 | /* | |
2893 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
2894 | * snapshot output is *not* set with a remote destination. | |
2895 | * | |
a934f4af | 2896 | * Return 0 on success or a LTTNG_ERR code. |
6dc3064a DG |
2897 | */ |
2898 | static int set_relayd_for_snapshot(struct consumer_output *consumer, | |
2899 | struct snapshot_output *snap_output, struct ltt_session *session) | |
2900 | { | |
a934f4af | 2901 | int ret = LTTNG_OK; |
6dc3064a DG |
2902 | struct lttng_ht_iter iter; |
2903 | struct consumer_socket *socket; | |
2904 | ||
2905 | assert(consumer); | |
2906 | assert(snap_output); | |
2907 | assert(session); | |
2908 | ||
2909 | DBG2("Set relayd object from snapshot output"); | |
2910 | ||
2911 | /* Ignore if snapshot consumer output is not network. */ | |
2912 | if (snap_output->consumer->type != CONSUMER_DST_NET) { | |
2913 | goto error; | |
2914 | } | |
2915 | ||
2916 | /* | |
2917 | * For each consumer socket, create and send the relayd object of the | |
2918 | * snapshot output. | |
2919 | */ | |
2920 | rcu_read_lock(); | |
5eecee74 DG |
2921 | cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter, |
2922 | socket, node.node) { | |
6dc3064a | 2923 | ret = send_consumer_relayd_sockets(0, session->id, |
d3e2ba59 JD |
2924 | snap_output->consumer, socket, |
2925 | session->name, session->hostname, | |
2926 | session->live_timer); | |
a934f4af | 2927 | if (ret != LTTNG_OK) { |
6dc3064a DG |
2928 | rcu_read_unlock(); |
2929 | goto error; | |
2930 | } | |
2931 | } | |
2932 | rcu_read_unlock(); | |
2933 | ||
2934 | error: | |
2935 | return ret; | |
2936 | } | |
2937 | ||
2938 | /* | |
2939 | * Record a kernel snapshot. | |
2940 | * | |
fac41e72 | 2941 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a DG |
2942 | */ |
2943 | static int record_kernel_snapshot(struct ltt_kernel_session *ksess, | |
5c786ded | 2944 | struct snapshot_output *output, struct ltt_session *session, |
68808f4e | 2945 | int wait, uint64_t max_stream_size) |
6dc3064a DG |
2946 | { |
2947 | int ret; | |
2948 | ||
2949 | assert(ksess); | |
2950 | assert(output); | |
2951 | assert(session); | |
2952 | ||
10a50311 JD |
2953 | /* Get the datetime for the snapshot output directory. */ |
2954 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, | |
2955 | sizeof(output->datetime)); | |
2956 | if (!ret) { | |
a934f4af | 2957 | ret = LTTNG_ERR_INVALID; |
10a50311 JD |
2958 | goto error; |
2959 | } | |
2960 | ||
af706bb7 DG |
2961 | /* |
2962 | * Copy kernel session sockets so we can communicate with the right | |
2963 | * consumer for the snapshot record command. | |
2964 | */ | |
2965 | ret = consumer_copy_sockets(output->consumer, ksess->consumer); | |
2966 | if (ret < 0) { | |
a934f4af | 2967 | ret = LTTNG_ERR_NOMEM; |
af706bb7 | 2968 | goto error; |
6dc3064a DG |
2969 | } |
2970 | ||
2971 | ret = set_relayd_for_snapshot(ksess->consumer, output, session); | |
a934f4af | 2972 | if (ret != LTTNG_OK) { |
af706bb7 | 2973 | goto error_snapshot; |
6dc3064a DG |
2974 | } |
2975 | ||
68808f4e | 2976 | ret = kernel_snapshot_record(ksess, output, wait, max_stream_size); |
2a06df8d | 2977 | if (ret != LTTNG_OK) { |
af706bb7 | 2978 | goto error_snapshot; |
6dc3064a DG |
2979 | } |
2980 | ||
a934f4af | 2981 | ret = LTTNG_OK; |
1371fc12 | 2982 | goto end; |
a934f4af | 2983 | |
af706bb7 DG |
2984 | error_snapshot: |
2985 | /* Clean up copied sockets so this output can use some other later on. */ | |
2986 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a | 2987 | error: |
1371fc12 | 2988 | end: |
6dc3064a DG |
2989 | return ret; |
2990 | } | |
2991 | ||
2992 | /* | |
2993 | * Record a UST snapshot. | |
2994 | * | |
a934f4af | 2995 | * Return 0 on success or a LTTNG_ERR error code. |
6dc3064a DG |
2996 | */ |
2997 | static int record_ust_snapshot(struct ltt_ust_session *usess, | |
5c786ded | 2998 | struct snapshot_output *output, struct ltt_session *session, |
68808f4e | 2999 | int wait, uint64_t max_stream_size) |
6dc3064a DG |
3000 | { |
3001 | int ret; | |
3002 | ||
3003 | assert(usess); | |
3004 | assert(output); | |
3005 | assert(session); | |
3006 | ||
10a50311 JD |
3007 | /* Get the datetime for the snapshot output directory. */ |
3008 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, | |
3009 | sizeof(output->datetime)); | |
3010 | if (!ret) { | |
a934f4af | 3011 | ret = LTTNG_ERR_INVALID; |
10a50311 JD |
3012 | goto error; |
3013 | } | |
3014 | ||
af706bb7 | 3015 | /* |
d3f14b8a | 3016 | * Copy UST session sockets so we can communicate with the right |
af706bb7 DG |
3017 | * consumer for the snapshot record command. |
3018 | */ | |
3019 | ret = consumer_copy_sockets(output->consumer, usess->consumer); | |
3020 | if (ret < 0) { | |
a934f4af | 3021 | ret = LTTNG_ERR_NOMEM; |
af706bb7 | 3022 | goto error; |
6dc3064a DG |
3023 | } |
3024 | ||
3025 | ret = set_relayd_for_snapshot(usess->consumer, output, session); | |
a934f4af | 3026 | if (ret != LTTNG_OK) { |
af706bb7 | 3027 | goto error_snapshot; |
6dc3064a DG |
3028 | } |
3029 | ||
68808f4e | 3030 | ret = ust_app_snapshot_record(usess, output, wait, max_stream_size); |
6dc3064a | 3031 | if (ret < 0) { |
7badf927 DG |
3032 | switch (-ret) { |
3033 | case EINVAL: | |
a934f4af | 3034 | ret = LTTNG_ERR_INVALID; |
7badf927 DG |
3035 | break; |
3036 | case ENODATA: | |
3037 | ret = LTTNG_ERR_SNAPSHOT_NODATA; | |
3038 | break; | |
3039 | default: | |
3040 | ret = LTTNG_ERR_SNAPSHOT_FAIL; | |
3041 | break; | |
5c786ded | 3042 | } |
af706bb7 | 3043 | goto error_snapshot; |
6dc3064a DG |
3044 | } |
3045 | ||
a934f4af DG |
3046 | ret = LTTNG_OK; |
3047 | ||
af706bb7 DG |
3048 | error_snapshot: |
3049 | /* Clean up copied sockets so this output can use some other later on. */ | |
3050 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a DG |
3051 | error: |
3052 | return ret; | |
3053 | } | |
3054 | ||
68808f4e DG |
3055 | /* |
3056 | * Return the biggest subbuffer size of all channels in the given session. | |
3057 | */ | |
3058 | static uint64_t get_session_max_subbuf_size(struct ltt_session *session) | |
3059 | { | |
3060 | uint64_t max_size = 0; | |
3061 | ||
3062 | assert(session); | |
3063 | ||
3064 | if (session->kernel_session) { | |
3065 | struct ltt_kernel_channel *chan; | |
3066 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3067 | ||
3068 | /* | |
3069 | * For each channel, add to the max size the size of each subbuffer | |
3070 | * multiplied by their sized. | |
3071 | */ | |
3072 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
3073 | if (chan->channel->attr.subbuf_size > max_size) { | |
3074 | max_size = chan->channel->attr.subbuf_size; | |
3075 | } | |
3076 | } | |
3077 | } | |
3078 | ||
3079 | if (session->ust_session) { | |
3080 | struct lttng_ht_iter iter; | |
3081 | struct ltt_ust_channel *uchan; | |
3082 | struct ltt_ust_session *usess = session->ust_session; | |
3083 | ||
50d7299d | 3084 | rcu_read_lock(); |
68808f4e DG |
3085 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
3086 | uchan, node.node) { | |
3087 | if (uchan->attr.subbuf_size > max_size) { | |
3088 | max_size = uchan->attr.subbuf_size; | |
3089 | } | |
3090 | } | |
50d7299d | 3091 | rcu_read_unlock(); |
68808f4e DG |
3092 | } |
3093 | ||
3094 | return max_size; | |
3095 | } | |
3096 | ||
5c786ded JD |
3097 | /* |
3098 | * Returns the total number of streams for a session or a negative value | |
3099 | * on error. | |
3100 | */ | |
68808f4e | 3101 | static unsigned int get_session_nb_streams(struct ltt_session *session) |
5c786ded JD |
3102 | { |
3103 | unsigned int total_streams = 0; | |
3104 | ||
3105 | if (session->kernel_session) { | |
3106 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3107 | ||
3108 | total_streams += ksess->stream_count_global; | |
3109 | } | |
3110 | ||
3111 | if (session->ust_session) { | |
3112 | struct ltt_ust_session *usess = session->ust_session; | |
3113 | ||
3114 | total_streams += ust_app_get_nb_stream(usess); | |
3115 | } | |
3116 | ||
3117 | return total_streams; | |
3118 | } | |
3119 | ||
6dc3064a DG |
3120 | /* |
3121 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
3122 | * | |
3123 | * The wait parameter is ignored so this call always wait for the snapshot to | |
3124 | * complete before returning. | |
3125 | * | |
3126 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3127 | */ | |
3128 | int cmd_snapshot_record(struct ltt_session *session, | |
3129 | struct lttng_snapshot_output *output, int wait) | |
3130 | { | |
3131 | int ret = LTTNG_OK; | |
e1986656 DG |
3132 | unsigned int use_tmp_output = 0; |
3133 | struct snapshot_output tmp_output; | |
1bfe7328 | 3134 | unsigned int nb_streams, snapshot_success = 0; |
68808f4e | 3135 | uint64_t session_max_size = 0, max_stream_size = 0; |
6dc3064a DG |
3136 | |
3137 | assert(session); | |
ba45d9f0 | 3138 | assert(output); |
6dc3064a DG |
3139 | |
3140 | DBG("Cmd snapshot record for session %s", session->name); | |
3141 | ||
3142 | /* | |
d3f14b8a MD |
3143 | * Permission denied to create an output if the session is not |
3144 | * set in no output mode. | |
6dc3064a DG |
3145 | */ |
3146 | if (session->output_traces) { | |
3147 | ret = LTTNG_ERR_EPERM; | |
3148 | goto error; | |
3149 | } | |
3150 | ||
3151 | /* The session needs to be started at least once. */ | |
8382cf6f | 3152 | if (!session->has_been_started) { |
6dc3064a DG |
3153 | ret = LTTNG_ERR_START_SESSION_ONCE; |
3154 | goto error; | |
3155 | } | |
3156 | ||
3157 | /* Use temporary output for the session. */ | |
ba45d9f0 | 3158 | if (*output->ctrl_url != '\0') { |
6dc3064a DG |
3159 | ret = snapshot_output_init(output->max_size, output->name, |
3160 | output->ctrl_url, output->data_url, session->consumer, | |
e1986656 | 3161 | &tmp_output, NULL); |
6dc3064a DG |
3162 | if (ret < 0) { |
3163 | if (ret == -ENOMEM) { | |
3164 | ret = LTTNG_ERR_NOMEM; | |
3165 | } else { | |
3166 | ret = LTTNG_ERR_INVALID; | |
3167 | } | |
3168 | goto error; | |
3169 | } | |
1bfe7328 DG |
3170 | /* Use the global session count for the temporary snapshot. */ |
3171 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; | |
e1986656 | 3172 | use_tmp_output = 1; |
6dc3064a DG |
3173 | } |
3174 | ||
5c786ded | 3175 | /* |
68808f4e DG |
3176 | * Get the session maximum size for a snapshot meaning it will compute the |
3177 | * size of all streams from all domain. | |
3178 | */ | |
3179 | max_stream_size = get_session_max_subbuf_size(session); | |
3180 | ||
3181 | nb_streams = get_session_nb_streams(session); | |
3182 | if (nb_streams) { | |
3183 | /* | |
3184 | * The maximum size of the snapshot is the number of streams multiplied | |
3185 | * by the biggest subbuf size of all channels in a session which is the | |
3186 | * maximum stream size available for each stream. The session max size | |
3187 | * is now checked against the snapshot max size value given by the user | |
3188 | * and if lower, an error is returned. | |
3189 | */ | |
3190 | session_max_size = max_stream_size * nb_streams; | |
3191 | } | |
3192 | ||
3193 | DBG3("Snapshot max size is %" PRIu64 " for max stream size of %" PRIu64, | |
3194 | session_max_size, max_stream_size); | |
3195 | ||
3196 | /* | |
3197 | * If we use a temporary output, check right away if the max size fits else | |
3198 | * for each output the max size will be checked. | |
5c786ded | 3199 | */ |
68808f4e DG |
3200 | if (use_tmp_output && |
3201 | (tmp_output.max_size != 0 && | |
3202 | tmp_output.max_size < session_max_size)) { | |
3203 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
3204 | goto error; | |
3205 | } | |
5c786ded | 3206 | |
6dc3064a DG |
3207 | if (session->kernel_session) { |
3208 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3209 | ||
e1986656 DG |
3210 | if (use_tmp_output) { |
3211 | ret = record_kernel_snapshot(ksess, &tmp_output, session, | |
68808f4e | 3212 | wait, max_stream_size); |
a934f4af | 3213 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3214 | goto error; |
3215 | } | |
1bfe7328 | 3216 | snapshot_success = 1; |
6dc3064a DG |
3217 | } else { |
3218 | struct snapshot_output *sout; | |
3219 | struct lttng_ht_iter iter; | |
3220 | ||
3221 | rcu_read_lock(); | |
3222 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
3223 | &iter.iter, sout, node.node) { | |
e1986656 DG |
3224 | /* |
3225 | * Make a local copy of the output and assign the possible | |
3226 | * temporary value given by the caller. | |
3227 | */ | |
3228 | memset(&tmp_output, 0, sizeof(tmp_output)); | |
3229 | memcpy(&tmp_output, sout, sizeof(tmp_output)); | |
3230 | ||
3231 | /* Use temporary max size. */ | |
3232 | if (output->max_size != (uint64_t) -1ULL) { | |
3233 | tmp_output.max_size = output->max_size; | |
3234 | } | |
3235 | ||
68808f4e DG |
3236 | if (tmp_output.max_size != 0 && |
3237 | tmp_output.max_size < session_max_size) { | |
3238 | rcu_read_unlock(); | |
3239 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
3240 | goto error; | |
3241 | } | |
3242 | ||
e1986656 DG |
3243 | /* Use temporary name. */ |
3244 | if (*output->name != '\0') { | |
3245 | strncpy(tmp_output.name, output->name, | |
3246 | sizeof(tmp_output.name)); | |
3247 | } | |
3248 | ||
1bfe7328 DG |
3249 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
3250 | ||
e1986656 | 3251 | ret = record_kernel_snapshot(ksess, &tmp_output, |
68808f4e | 3252 | session, wait, max_stream_size); |
a934f4af | 3253 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3254 | rcu_read_unlock(); |
3255 | goto error; | |
3256 | } | |
1bfe7328 | 3257 | snapshot_success = 1; |
6dc3064a DG |
3258 | } |
3259 | rcu_read_unlock(); | |
3260 | } | |
3261 | } | |
3262 | ||
3263 | if (session->ust_session) { | |
3264 | struct ltt_ust_session *usess = session->ust_session; | |
3265 | ||
e1986656 DG |
3266 | if (use_tmp_output) { |
3267 | ret = record_ust_snapshot(usess, &tmp_output, session, | |
68808f4e | 3268 | wait, max_stream_size); |
a934f4af | 3269 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3270 | goto error; |
3271 | } | |
1bfe7328 | 3272 | snapshot_success = 1; |
6dc3064a DG |
3273 | } else { |
3274 | struct snapshot_output *sout; | |
3275 | struct lttng_ht_iter iter; | |
3276 | ||
3277 | rcu_read_lock(); | |
3278 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
3279 | &iter.iter, sout, node.node) { | |
e1986656 DG |
3280 | /* |
3281 | * Make a local copy of the output and assign the possible | |
3282 | * temporary value given by the caller. | |
3283 | */ | |
3284 | memset(&tmp_output, 0, sizeof(tmp_output)); | |
3285 | memcpy(&tmp_output, sout, sizeof(tmp_output)); | |
3286 | ||
e1986656 DG |
3287 | /* Use temporary max size. */ |
3288 | if (output->max_size != (uint64_t) -1ULL) { | |
3289 | tmp_output.max_size = output->max_size; | |
3290 | } | |
3291 | ||
68808f4e DG |
3292 | if (tmp_output.max_size != 0 && |
3293 | tmp_output.max_size < session_max_size) { | |
3294 | rcu_read_unlock(); | |
3295 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
3296 | goto error; | |
3297 | } | |
3298 | ||
e1986656 DG |
3299 | /* Use temporary name. */ |
3300 | if (*output->name != '\0') { | |
3301 | strncpy(tmp_output.name, output->name, | |
3302 | sizeof(tmp_output.name)); | |
3303 | } | |
3304 | ||
1bfe7328 DG |
3305 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
3306 | ||
e1986656 | 3307 | ret = record_ust_snapshot(usess, &tmp_output, session, |
68808f4e | 3308 | wait, max_stream_size); |
a934f4af | 3309 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3310 | rcu_read_unlock(); |
3311 | goto error; | |
3312 | } | |
1bfe7328 | 3313 | snapshot_success = 1; |
6dc3064a DG |
3314 | } |
3315 | rcu_read_unlock(); | |
3316 | } | |
3317 | } | |
3318 | ||
1bfe7328 DG |
3319 | if (snapshot_success) { |
3320 | session->snapshot.nb_snapshot++; | |
b67578cb MD |
3321 | } else { |
3322 | ret = LTTNG_ERR_SNAPSHOT_FAIL; | |
1bfe7328 DG |
3323 | } |
3324 | ||
6dc3064a | 3325 | error: |
6dc3064a DG |
3326 | return ret; |
3327 | } | |
3328 | ||
2f77fc4b DG |
3329 | /* |
3330 | * Init command subsystem. | |
3331 | */ | |
3332 | void cmd_init(void) | |
3333 | { | |
3334 | /* | |
d88aee68 DG |
3335 | * Set network sequence index to 1 for streams to match a relayd |
3336 | * socket on the consumer side. | |
2f77fc4b | 3337 | */ |
d88aee68 DG |
3338 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
3339 | relayd_net_seq_idx = 1; | |
3340 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
3341 | |
3342 | DBG("Command subsystem initialized"); | |
3343 | } |