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