common: move lttng_payload[_view] to libcommon
[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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #include "bin/lttng-sessiond/tracker.h"
10 #include "lttng/lttng-error.h"
11 #include "lttng/tracker.h"
12 #define _LGPL_SOURCE
13 #include <assert.h>
14 #include <inttypes.h>
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
17 #include <sys/stat.h>
18 #include <stdio.h>
19
20 #include <common/defaults.h>
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/relayd/relayd.h>
24 #include <common/utils.h>
25 #include <common/compat/string.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/dynamic-buffer.h>
28 #include <common/buffer-view.h>
29 #include <common/payload.h>
30 #include <common/payload-view.h>
31 #include <common/trace-chunk.h>
32 #include <lttng/location-internal.h>
33 #include <lttng/trigger/trigger-internal.h>
34 #include <lttng/condition/condition.h>
35 #include <lttng/action/action.h>
36 #include <lttng/channel.h>
37 #include <lttng/channel-internal.h>
38 #include <lttng/rotate-internal.h>
39 #include <lttng/location-internal.h>
40 #include <lttng/session-internal.h>
41 #include <lttng/userspace-probe-internal.h>
42 #include <lttng/session-descriptor-internal.h>
43 #include <common/string-utils/string-utils.h>
44
45 #include "channel.h"
46 #include "consumer.h"
47 #include "event.h"
48 #include "health-sessiond.h"
49 #include "kernel.h"
50 #include "kernel-consumer.h"
51 #include "lttng-sessiond.h"
52 #include "utils.h"
53 #include "lttng-syscall.h"
54 #include "agent.h"
55 #include "buffer-registry.h"
56 #include "notification-thread.h"
57 #include "notification-thread-commands.h"
58 #include "rotate.h"
59 #include "rotation-thread.h"
60 #include "timer.h"
61 #include "agent-thread.h"
62
63 #include "cmd.h"
64
65 /* Sleep for 100ms between each check for the shm path's deletion. */
66 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
67
68 struct cmd_destroy_session_reply_context {
69 int reply_sock_fd;
70 bool implicit_rotation_on_destroy;
71 /*
72 * Indicates whether or not an error occurred while launching the
73 * destruction of a session.
74 */
75 enum lttng_error_code destruction_status;
76 };
77
78 static enum lttng_error_code wait_on_path(void *path);
79
80 /*
81 * Command completion handler that is used by the destroy command
82 * when a session that has a non-default shm_path is being destroyed.
83 *
84 * See comment in cmd_destroy_session() for the rationale.
85 */
86 static struct destroy_completion_handler {
87 struct cmd_completion_handler handler;
88 char shm_path[member_sizeof(struct ltt_session, shm_path)];
89 } destroy_completion_handler = {
90 .handler = {
91 .run = wait_on_path,
92 .data = destroy_completion_handler.shm_path
93 },
94 .shm_path = { 0 },
95 };
96
97 static struct cmd_completion_handler *current_completion_handler;
98
99 /*
100 * Used to keep a unique index for each relayd socket created where this value
101 * is associated with streams on the consumer so it can match the right relayd
102 * to send to. It must be accessed with the relayd_net_seq_idx_lock
103 * held.
104 */
105 static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
106 static uint64_t relayd_net_seq_idx;
107
108 static int validate_ust_event_name(const char *);
109 static int cmd_enable_event_internal(struct ltt_session *session,
110 const struct lttng_domain *domain,
111 char *channel_name, struct lttng_event *event,
112 char *filter_expression,
113 struct lttng_filter_bytecode *filter,
114 struct lttng_event_exclusion *exclusion,
115 int wpipe);
116
117 /*
118 * Create a session path used by list_lttng_sessions for the case that the
119 * session consumer is on the network.
120 */
121 static int build_network_session_path(char *dst, size_t size,
122 struct ltt_session *session)
123 {
124 int ret, kdata_port, udata_port;
125 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
126 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
127
128 assert(session);
129 assert(dst);
130
131 memset(tmp_urls, 0, sizeof(tmp_urls));
132 memset(tmp_uurl, 0, sizeof(tmp_uurl));
133
134 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
135
136 if (session->kernel_session && session->kernel_session->consumer) {
137 kuri = &session->kernel_session->consumer->dst.net.control;
138 kdata_port = session->kernel_session->consumer->dst.net.data.port;
139 }
140
141 if (session->ust_session && session->ust_session->consumer) {
142 uuri = &session->ust_session->consumer->dst.net.control;
143 udata_port = session->ust_session->consumer->dst.net.data.port;
144 }
145
146 if (uuri == NULL && kuri == NULL) {
147 uri = &session->consumer->dst.net.control;
148 kdata_port = session->consumer->dst.net.data.port;
149 } else if (kuri && uuri) {
150 ret = uri_compare(kuri, uuri);
151 if (ret) {
152 /* Not Equal */
153 uri = kuri;
154 /* Build uuri URL string */
155 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
156 if (ret < 0) {
157 goto error;
158 }
159 } else {
160 uri = kuri;
161 }
162 } else if (kuri && uuri == NULL) {
163 uri = kuri;
164 } else if (uuri && kuri == NULL) {
165 uri = uuri;
166 }
167
168 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
169 if (ret < 0) {
170 goto error;
171 }
172
173 /*
174 * Do we have a UST url set. If yes, this means we have both kernel and UST
175 * to print.
176 */
177 if (*tmp_uurl != '\0') {
178 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
179 tmp_urls, kdata_port, tmp_uurl, udata_port);
180 } else {
181 int dport;
182 if (kuri || (!kuri && !uuri)) {
183 dport = kdata_port;
184 } else {
185 /* No kernel URI, use the UST port. */
186 dport = udata_port;
187 }
188 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
189 }
190
191 error:
192 return ret;
193 }
194
195 /*
196 * Get run-time attributes if the session has been started (discarded events,
197 * lost packets).
198 */
199 static int get_kernel_runtime_stats(struct ltt_session *session,
200 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
201 uint64_t *lost_packets)
202 {
203 int ret;
204
205 if (!session->has_been_started) {
206 ret = 0;
207 *discarded_events = 0;
208 *lost_packets = 0;
209 goto end;
210 }
211
212 ret = consumer_get_discarded_events(session->id, kchan->key,
213 session->kernel_session->consumer,
214 discarded_events);
215 if (ret < 0) {
216 goto end;
217 }
218
219 ret = consumer_get_lost_packets(session->id, kchan->key,
220 session->kernel_session->consumer,
221 lost_packets);
222 if (ret < 0) {
223 goto end;
224 }
225
226 end:
227 return ret;
228 }
229
230 /*
231 * Get run-time attributes if the session has been started (discarded events,
232 * lost packets).
233 */
234 static int get_ust_runtime_stats(struct ltt_session *session,
235 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
236 uint64_t *lost_packets)
237 {
238 int ret;
239 struct ltt_ust_session *usess;
240
241 if (!discarded_events || !lost_packets) {
242 ret = -1;
243 goto end;
244 }
245
246 usess = session->ust_session;
247 assert(discarded_events);
248 assert(lost_packets);
249
250 if (!usess || !session->has_been_started) {
251 *discarded_events = 0;
252 *lost_packets = 0;
253 ret = 0;
254 goto end;
255 }
256
257 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
258 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
259 &usess->buffer_reg_uid_list,
260 usess->consumer, uchan->id,
261 uchan->attr.overwrite,
262 discarded_events,
263 lost_packets);
264 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
265 ret = ust_app_pid_get_channel_runtime_stats(usess,
266 uchan, usess->consumer,
267 uchan->attr.overwrite,
268 discarded_events,
269 lost_packets);
270 if (ret < 0) {
271 goto end;
272 }
273 *discarded_events += uchan->per_pid_closed_app_discarded;
274 *lost_packets += uchan->per_pid_closed_app_lost;
275 } else {
276 ERR("Unsupported buffer type");
277 assert(0);
278 ret = -1;
279 goto end;
280 }
281
282 end:
283 return ret;
284 }
285
286 /*
287 * Fill lttng_channel array of all channels.
288 */
289 static ssize_t list_lttng_channels(enum lttng_domain_type domain,
290 struct ltt_session *session, struct lttng_channel *channels,
291 struct lttng_channel_extended *chan_exts)
292 {
293 int i = 0, ret = 0;
294 struct ltt_kernel_channel *kchan;
295
296 DBG("Listing channels for session %s", session->name);
297
298 switch (domain) {
299 case LTTNG_DOMAIN_KERNEL:
300 /* Kernel channels */
301 if (session->kernel_session != NULL) {
302 cds_list_for_each_entry(kchan,
303 &session->kernel_session->channel_list.head, list) {
304 uint64_t discarded_events, lost_packets;
305 struct lttng_channel_extended *extended;
306
307 extended = (struct lttng_channel_extended *)
308 kchan->channel->attr.extended.ptr;
309
310 ret = get_kernel_runtime_stats(session, kchan,
311 &discarded_events, &lost_packets);
312 if (ret < 0) {
313 goto end;
314 }
315 /* Copy lttng_channel struct to array */
316 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
317 channels[i].enabled = kchan->enabled;
318 chan_exts[i].discarded_events =
319 discarded_events;
320 chan_exts[i].lost_packets = lost_packets;
321 chan_exts[i].monitor_timer_interval =
322 extended->monitor_timer_interval;
323 chan_exts[i].blocking_timeout = 0;
324 i++;
325 }
326 }
327 break;
328 case LTTNG_DOMAIN_UST:
329 {
330 struct lttng_ht_iter iter;
331 struct ltt_ust_channel *uchan;
332
333 rcu_read_lock();
334 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
335 &iter.iter, uchan, node.node) {
336 uint64_t discarded_events = 0, lost_packets = 0;
337
338 if (lttng_strncpy(channels[i].name, uchan->name,
339 LTTNG_SYMBOL_NAME_LEN)) {
340 break;
341 }
342 channels[i].attr.overwrite = uchan->attr.overwrite;
343 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
344 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
345 channels[i].attr.switch_timer_interval =
346 uchan->attr.switch_timer_interval;
347 channels[i].attr.read_timer_interval =
348 uchan->attr.read_timer_interval;
349 channels[i].enabled = uchan->enabled;
350 channels[i].attr.tracefile_size = uchan->tracefile_size;
351 channels[i].attr.tracefile_count = uchan->tracefile_count;
352
353 /*
354 * Map enum lttng_ust_output to enum lttng_event_output.
355 */
356 switch (uchan->attr.output) {
357 case LTTNG_UST_MMAP:
358 channels[i].attr.output = LTTNG_EVENT_MMAP;
359 break;
360 default:
361 /*
362 * LTTNG_UST_MMAP is the only supported UST
363 * output mode.
364 */
365 assert(0);
366 break;
367 }
368
369 chan_exts[i].monitor_timer_interval =
370 uchan->monitor_timer_interval;
371 chan_exts[i].blocking_timeout =
372 uchan->attr.u.s.blocking_timeout;
373
374 ret = get_ust_runtime_stats(session, uchan,
375 &discarded_events, &lost_packets);
376 if (ret < 0) {
377 break;
378 }
379 chan_exts[i].discarded_events = discarded_events;
380 chan_exts[i].lost_packets = lost_packets;
381 i++;
382 }
383 rcu_read_unlock();
384 break;
385 }
386 default:
387 break;
388 }
389
390 end:
391 if (ret < 0) {
392 return -LTTNG_ERR_FATAL;
393 } else {
394 return LTTNG_OK;
395 }
396 }
397
398 static int increment_extended_len(const char *filter_expression,
399 struct lttng_event_exclusion *exclusion,
400 const struct lttng_userspace_probe_location *probe_location,
401 size_t *extended_len)
402 {
403 int ret = 0;
404
405 *extended_len += sizeof(struct lttcomm_event_extended_header);
406
407 if (filter_expression) {
408 *extended_len += strlen(filter_expression) + 1;
409 }
410
411 if (exclusion) {
412 *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN;
413 }
414
415 if (probe_location) {
416 ret = lttng_userspace_probe_location_serialize(probe_location,
417 NULL, NULL);
418 if (ret < 0) {
419 goto end;
420 }
421 *extended_len += ret;
422 }
423 ret = 0;
424 end:
425 return ret;
426 }
427
428 static int append_extended_info(const char *filter_expression,
429 struct lttng_event_exclusion *exclusion,
430 struct lttng_userspace_probe_location *probe_location,
431 void **extended_at)
432 {
433 int ret = 0;
434 size_t filter_len = 0;
435 size_t nb_exclusions = 0;
436 size_t userspace_probe_location_len = 0;
437 struct lttng_dynamic_buffer location_buffer;
438 struct lttcomm_event_extended_header extended_header;
439
440 if (filter_expression) {
441 filter_len = strlen(filter_expression) + 1;
442 }
443
444 if (exclusion) {
445 nb_exclusions = exclusion->count;
446 }
447
448 if (probe_location) {
449 lttng_dynamic_buffer_init(&location_buffer);
450 ret = lttng_userspace_probe_location_serialize(probe_location,
451 &location_buffer, NULL);
452 if (ret < 0) {
453 ret = -1;
454 goto end;
455 }
456 userspace_probe_location_len = location_buffer.size;
457 }
458
459 /* Set header fields */
460 extended_header.filter_len = filter_len;
461 extended_header.nb_exclusions = nb_exclusions;
462 extended_header.userspace_probe_location_len = userspace_probe_location_len;
463
464 /* Copy header */
465 memcpy(*extended_at, &extended_header, sizeof(extended_header));
466 *extended_at += sizeof(extended_header);
467
468 /* Copy filter string */
469 if (filter_expression) {
470 memcpy(*extended_at, filter_expression, filter_len);
471 *extended_at += filter_len;
472 }
473
474 /* Copy exclusion names */
475 if (exclusion) {
476 size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
477
478 memcpy(*extended_at, &exclusion->names, len);
479 *extended_at += len;
480 }
481
482 if (probe_location) {
483 memcpy(*extended_at, location_buffer.data, location_buffer.size);
484 *extended_at += location_buffer.size;
485 lttng_dynamic_buffer_reset(&location_buffer);
486 }
487 ret = 0;
488 end:
489 return ret;
490 }
491
492 /*
493 * Create a list of agent domain events.
494 *
495 * Return number of events in list on success or else a negative value.
496 */
497 static int list_lttng_agent_events(struct agent *agt,
498 struct lttng_event **events, size_t *total_size)
499 {
500 int i = 0, ret = 0;
501 unsigned int nb_event = 0;
502 struct agent_event *event;
503 struct lttng_event *tmp_events = NULL;
504 struct lttng_ht_iter iter;
505 size_t extended_len = 0;
506 void *extended_at;
507
508 assert(agt);
509 assert(events);
510
511 DBG3("Listing agent events");
512
513 rcu_read_lock();
514 nb_event = lttng_ht_get_count(agt->events);
515 rcu_read_unlock();
516 if (nb_event == 0) {
517 ret = nb_event;
518 *total_size = 0;
519 goto error;
520 }
521
522 /* Compute required extended infos size */
523 extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
524
525 /*
526 * This is only valid because the commands which add events are
527 * processed in the same thread as the listing.
528 */
529 rcu_read_lock();
530 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
531 ret = increment_extended_len(event->filter_expression, NULL, NULL,
532 &extended_len);
533 if (ret) {
534 DBG("Error computing the length of extended info message");
535 ret = -LTTNG_ERR_FATAL;
536 goto error;
537 }
538 }
539 rcu_read_unlock();
540
541 *total_size = nb_event * sizeof(*tmp_events) + extended_len;
542 tmp_events = zmalloc(*total_size);
543 if (!tmp_events) {
544 PERROR("zmalloc agent events session");
545 ret = -LTTNG_ERR_FATAL;
546 goto error;
547 }
548
549 extended_at = ((uint8_t *) tmp_events) +
550 nb_event * sizeof(struct lttng_event);
551
552 rcu_read_lock();
553 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
554 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
555 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
556 tmp_events[i].enabled = event->enabled;
557 tmp_events[i].loglevel = event->loglevel_value;
558 tmp_events[i].loglevel_type = event->loglevel_type;
559 i++;
560
561 /* Append extended info */
562 ret = append_extended_info(event->filter_expression, NULL, NULL,
563 &extended_at);
564 if (ret) {
565 DBG("Error appending extended info message");
566 ret = -LTTNG_ERR_FATAL;
567 goto error;
568 }
569 }
570
571 *events = tmp_events;
572 ret = nb_event;
573 assert(nb_event == i);
574
575 end:
576 rcu_read_unlock();
577 return ret;
578 error:
579 free(tmp_events);
580 goto end;
581 }
582
583 /*
584 * Create a list of ust global domain events.
585 */
586 static int list_lttng_ust_global_events(char *channel_name,
587 struct ltt_ust_domain_global *ust_global,
588 struct lttng_event **events, size_t *total_size)
589 {
590 int i = 0, ret = 0;
591 unsigned int nb_event = 0;
592 struct lttng_ht_iter iter;
593 struct lttng_ht_node_str *node;
594 struct ltt_ust_channel *uchan;
595 struct ltt_ust_event *uevent;
596 struct lttng_event *tmp;
597 size_t extended_len = 0;
598 void *extended_at;
599
600 DBG("Listing UST global events for channel %s", channel_name);
601
602 rcu_read_lock();
603
604 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
605 node = lttng_ht_iter_get_node_str(&iter);
606 if (node == NULL) {
607 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
608 goto end;
609 }
610
611 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
612
613 nb_event = lttng_ht_get_count(uchan->events);
614 if (nb_event == 0) {
615 ret = nb_event;
616 *total_size = 0;
617 goto end;
618 }
619
620 DBG3("Listing UST global %d events", nb_event);
621
622 /* Compute required extended infos size */
623 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
624 if (uevent->internal) {
625 nb_event--;
626 continue;
627 }
628
629 ret = increment_extended_len(uevent->filter_expression,
630 uevent->exclusion, NULL, &extended_len);
631 if (ret) {
632 DBG("Error computing the length of extended info message");
633 ret = -LTTNG_ERR_FATAL;
634 goto end;
635 }
636 }
637 if (nb_event == 0) {
638 /* All events are internal, skip. */
639 ret = 0;
640 *total_size = 0;
641 goto end;
642 }
643
644 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
645 tmp = zmalloc(*total_size);
646 if (tmp == NULL) {
647 ret = -LTTNG_ERR_FATAL;
648 goto end;
649 }
650
651 extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event);
652
653 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
654 if (uevent->internal) {
655 /* This event should remain hidden from clients */
656 continue;
657 }
658 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
659 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
660 tmp[i].enabled = uevent->enabled;
661
662 switch (uevent->attr.instrumentation) {
663 case LTTNG_UST_TRACEPOINT:
664 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
665 break;
666 case LTTNG_UST_PROBE:
667 tmp[i].type = LTTNG_EVENT_PROBE;
668 break;
669 case LTTNG_UST_FUNCTION:
670 tmp[i].type = LTTNG_EVENT_FUNCTION;
671 break;
672 }
673
674 tmp[i].loglevel = uevent->attr.loglevel;
675 switch (uevent->attr.loglevel_type) {
676 case LTTNG_UST_LOGLEVEL_ALL:
677 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
678 break;
679 case LTTNG_UST_LOGLEVEL_RANGE:
680 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
681 break;
682 case LTTNG_UST_LOGLEVEL_SINGLE:
683 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
684 break;
685 }
686 if (uevent->filter) {
687 tmp[i].filter = 1;
688 }
689 if (uevent->exclusion) {
690 tmp[i].exclusion = 1;
691 }
692 i++;
693
694 /* Append extended info */
695 ret = append_extended_info(uevent->filter_expression,
696 uevent->exclusion, NULL, &extended_at);
697 if (ret) {
698 DBG("Error appending extended info message");
699 ret = -LTTNG_ERR_FATAL;
700 goto end;
701 }
702 }
703
704 ret = nb_event;
705 *events = tmp;
706 end:
707 rcu_read_unlock();
708 return ret;
709 }
710
711 /*
712 * Fill lttng_event array of all kernel events in the channel.
713 */
714 static int list_lttng_kernel_events(char *channel_name,
715 struct ltt_kernel_session *kernel_session,
716 struct lttng_event **events, size_t *total_size)
717 {
718 int i = 0, ret;
719 unsigned int nb_event;
720 struct ltt_kernel_event *event;
721 struct ltt_kernel_channel *kchan;
722 size_t extended_len = 0;
723 void *extended_at;
724
725 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
726 if (kchan == NULL) {
727 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
728 goto error;
729 }
730
731 nb_event = kchan->event_count;
732
733 DBG("Listing events for channel %s", kchan->channel->name);
734
735 if (nb_event == 0) {
736 *total_size = 0;
737 *events = NULL;
738 goto end;
739 }
740
741 /* Compute required extended infos size */
742 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
743 ret = increment_extended_len(event->filter_expression, NULL,
744 event->userspace_probe_location,
745 &extended_len);
746 if (ret) {
747 DBG("Error computing the length of extended info message");
748 ret = -LTTNG_ERR_FATAL;
749 goto error;
750 }
751 }
752
753 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
754 *events = zmalloc(*total_size);
755 if (*events == NULL) {
756 ret = -LTTNG_ERR_FATAL;
757 goto error;
758 }
759
760 extended_at = ((void *) *events) +
761 nb_event * sizeof(struct lttng_event);
762
763 /* Kernel channels */
764 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
765 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
766 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
767 (*events)[i].enabled = event->enabled;
768 (*events)[i].filter =
769 (unsigned char) !!event->filter_expression;
770
771 switch (event->event->instrumentation) {
772 case LTTNG_KERNEL_TRACEPOINT:
773 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
774 break;
775 case LTTNG_KERNEL_KRETPROBE:
776 (*events)[i].type = LTTNG_EVENT_FUNCTION;
777 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
778 sizeof(struct lttng_kernel_kprobe));
779 break;
780 case LTTNG_KERNEL_KPROBE:
781 (*events)[i].type = LTTNG_EVENT_PROBE;
782 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
783 sizeof(struct lttng_kernel_kprobe));
784 break;
785 case LTTNG_KERNEL_UPROBE:
786 (*events)[i].type = LTTNG_EVENT_USERSPACE_PROBE;
787 break;
788 case LTTNG_KERNEL_FUNCTION:
789 (*events)[i].type = LTTNG_EVENT_FUNCTION;
790 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
791 sizeof(struct lttng_kernel_function));
792 break;
793 case LTTNG_KERNEL_NOOP:
794 (*events)[i].type = LTTNG_EVENT_NOOP;
795 break;
796 case LTTNG_KERNEL_SYSCALL:
797 (*events)[i].type = LTTNG_EVENT_SYSCALL;
798 break;
799 case LTTNG_KERNEL_ALL:
800 /* fall-through. */
801 default:
802 assert(0);
803 break;
804 }
805 i++;
806
807 /* Append extended info */
808 ret = append_extended_info(event->filter_expression, NULL,
809 event->userspace_probe_location, &extended_at);
810 if (ret) {
811 DBG("Error appending extended info message");
812 ret = -LTTNG_ERR_FATAL;
813 goto error;
814 }
815 }
816
817 end:
818 return nb_event;
819
820 error:
821 return ret;
822 }
823
824 /*
825 * Add URI so the consumer output object. Set the correct path depending on the
826 * domain adding the default trace directory.
827 */
828 static enum lttng_error_code add_uri_to_consumer(
829 const struct ltt_session *session,
830 struct consumer_output *consumer,
831 struct lttng_uri *uri, enum lttng_domain_type domain)
832 {
833 int ret;
834 enum lttng_error_code ret_code = LTTNG_OK;
835
836 assert(uri);
837
838 if (consumer == NULL) {
839 DBG("No consumer detected. Don't add URI. Stopping.");
840 ret_code = LTTNG_ERR_NO_CONSUMER;
841 goto error;
842 }
843
844 switch (domain) {
845 case LTTNG_DOMAIN_KERNEL:
846 ret = lttng_strncpy(consumer->domain_subdir,
847 DEFAULT_KERNEL_TRACE_DIR,
848 sizeof(consumer->domain_subdir));
849 break;
850 case LTTNG_DOMAIN_UST:
851 ret = lttng_strncpy(consumer->domain_subdir,
852 DEFAULT_UST_TRACE_DIR,
853 sizeof(consumer->domain_subdir));
854 break;
855 default:
856 /*
857 * This case is possible is we try to add the URI to the global
858 * tracing session consumer object which in this case there is
859 * no subdir.
860 */
861 memset(consumer->domain_subdir, 0,
862 sizeof(consumer->domain_subdir));
863 ret = 0;
864 }
865 if (ret) {
866 ERR("Failed to initialize consumer output domain subdirectory");
867 ret_code = LTTNG_ERR_FATAL;
868 goto error;
869 }
870
871 switch (uri->dtype) {
872 case LTTNG_DST_IPV4:
873 case LTTNG_DST_IPV6:
874 DBG2("Setting network URI to consumer");
875
876 if (consumer->type == CONSUMER_DST_NET) {
877 if ((uri->stype == LTTNG_STREAM_CONTROL &&
878 consumer->dst.net.control_isset) ||
879 (uri->stype == LTTNG_STREAM_DATA &&
880 consumer->dst.net.data_isset)) {
881 ret_code = LTTNG_ERR_URL_EXIST;
882 goto error;
883 }
884 } else {
885 memset(&consumer->dst, 0, sizeof(consumer->dst));
886 }
887
888 /* Set URI into consumer output object */
889 ret = consumer_set_network_uri(session, consumer, uri);
890 if (ret < 0) {
891 ret_code = -ret;
892 goto error;
893 } else if (ret == 1) {
894 /*
895 * URI was the same in the consumer so we do not append the subdir
896 * again so to not duplicate output dir.
897 */
898 ret_code = LTTNG_OK;
899 goto error;
900 }
901 break;
902 case LTTNG_DST_PATH:
903 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
904 ret_code = LTTNG_ERR_INVALID;
905 goto error;
906 }
907 DBG2("Setting trace directory path from URI to %s",
908 uri->dst.path);
909 memset(&consumer->dst, 0, sizeof(consumer->dst));
910
911 ret = lttng_strncpy(consumer->dst.session_root_path,
912 uri->dst.path,
913 sizeof(consumer->dst.session_root_path));
914 if (ret) {
915 ret_code = LTTNG_ERR_FATAL;
916 goto error;
917 }
918 consumer->type = CONSUMER_DST_LOCAL;
919 break;
920 }
921
922 ret_code = LTTNG_OK;
923 error:
924 return ret_code;
925 }
926
927 /*
928 * Init tracing by creating trace directory and sending fds kernel consumer.
929 */
930 static int init_kernel_tracing(struct ltt_kernel_session *session)
931 {
932 int ret = 0;
933 struct lttng_ht_iter iter;
934 struct consumer_socket *socket;
935
936 assert(session);
937
938 rcu_read_lock();
939
940 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
941 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
942 socket, node.node) {
943 pthread_mutex_lock(socket->lock);
944 ret = kernel_consumer_send_session(socket, session);
945 pthread_mutex_unlock(socket->lock);
946 if (ret < 0) {
947 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
948 goto error;
949 }
950 }
951 }
952
953 error:
954 rcu_read_unlock();
955 return ret;
956 }
957
958 /*
959 * Create a socket to the relayd using the URI.
960 *
961 * On success, the relayd_sock pointer is set to the created socket.
962 * Else, it remains untouched and an LTTng error code is returned.
963 */
964 static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
965 struct lttcomm_relayd_sock **relayd_sock,
966 struct consumer_output *consumer)
967 {
968 int ret;
969 enum lttng_error_code status = LTTNG_OK;
970 struct lttcomm_relayd_sock *rsock;
971
972 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
973 RELAYD_VERSION_COMM_MINOR);
974 if (!rsock) {
975 status = LTTNG_ERR_FATAL;
976 goto error;
977 }
978
979 /*
980 * Connect to relayd so we can proceed with a session creation. This call
981 * can possibly block for an arbitrary amount of time to set the health
982 * state to be in poll execution.
983 */
984 health_poll_entry();
985 ret = relayd_connect(rsock);
986 health_poll_exit();
987 if (ret < 0) {
988 ERR("Unable to reach lttng-relayd");
989 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
990 goto free_sock;
991 }
992
993 /* Create socket for control stream. */
994 if (uri->stype == LTTNG_STREAM_CONTROL) {
995 uint64_t result_flags;
996
997 DBG3("Creating relayd stream socket from URI");
998
999 /* Check relayd version */
1000 ret = relayd_version_check(rsock);
1001 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
1002 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
1003 goto close_sock;
1004 } else if (ret < 0) {
1005 ERR("Unable to reach lttng-relayd");
1006 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
1007 goto close_sock;
1008 }
1009 consumer->relay_major_version = rsock->major;
1010 consumer->relay_minor_version = rsock->minor;
1011 ret = relayd_get_configuration(rsock, 0,
1012 &result_flags);
1013 if (ret < 0) {
1014 ERR("Unable to get relayd configuration");
1015 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
1016 goto close_sock;
1017 }
1018 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
1019 consumer->relay_allows_clear = true;
1020 }
1021 } else if (uri->stype == LTTNG_STREAM_DATA) {
1022 DBG3("Creating relayd data socket from URI");
1023 } else {
1024 /* Command is not valid */
1025 ERR("Relayd invalid stream type: %d", uri->stype);
1026 status = LTTNG_ERR_INVALID;
1027 goto close_sock;
1028 }
1029
1030 *relayd_sock = rsock;
1031
1032 return status;
1033
1034 close_sock:
1035 /* The returned value is not useful since we are on an error path. */
1036 (void) relayd_close(rsock);
1037 free_sock:
1038 free(rsock);
1039 error:
1040 return status;
1041 }
1042
1043 /*
1044 * Connect to the relayd using URI and send the socket to the right consumer.
1045 *
1046 * The consumer socket lock must be held by the caller.
1047 *
1048 * Returns LTTNG_OK on success or an LTTng error code on failure.
1049 */
1050 static enum lttng_error_code send_consumer_relayd_socket(
1051 unsigned int session_id,
1052 struct lttng_uri *relayd_uri,
1053 struct consumer_output *consumer,
1054 struct consumer_socket *consumer_sock,
1055 const char *session_name, const char *hostname,
1056 const char *base_path, int session_live_timer,
1057 const uint64_t *current_chunk_id,
1058 time_t session_creation_time,
1059 bool session_name_contains_creation_time)
1060 {
1061 int ret;
1062 struct lttcomm_relayd_sock *rsock = NULL;
1063 enum lttng_error_code status;
1064
1065 /* Connect to relayd and make version check if uri is the control. */
1066 status = create_connect_relayd(relayd_uri, &rsock, consumer);
1067 if (status != LTTNG_OK) {
1068 goto relayd_comm_error;
1069 }
1070 assert(rsock);
1071
1072 /* Set the network sequence index if not set. */
1073 if (consumer->net_seq_index == (uint64_t) -1ULL) {
1074 pthread_mutex_lock(&relayd_net_seq_idx_lock);
1075 /*
1076 * Increment net_seq_idx because we are about to transfer the
1077 * new relayd socket to the consumer.
1078 * Assign unique key so the consumer can match streams.
1079 */
1080 consumer->net_seq_index = ++relayd_net_seq_idx;
1081 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
1082 }
1083
1084 /* Send relayd socket to consumer. */
1085 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
1086 relayd_uri->stype, session_id,
1087 session_name, hostname, base_path,
1088 session_live_timer, current_chunk_id,
1089 session_creation_time, session_name_contains_creation_time);
1090 if (ret < 0) {
1091 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
1092 goto close_sock;
1093 }
1094
1095 /* Flag that the corresponding socket was sent. */
1096 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
1097 consumer_sock->control_sock_sent = 1;
1098 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
1099 consumer_sock->data_sock_sent = 1;
1100 }
1101
1102 /*
1103 * Close socket which was dup on the consumer side. The session daemon does
1104 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1105 */
1106
1107 close_sock:
1108 if (status != LTTNG_OK) {
1109 /*
1110 * The consumer output for this session should not be used anymore
1111 * since the relayd connection failed thus making any tracing or/and
1112 * streaming not usable.
1113 */
1114 consumer->enabled = 0;
1115 }
1116 (void) relayd_close(rsock);
1117 free(rsock);
1118
1119 relayd_comm_error:
1120 return status;
1121 }
1122
1123 /*
1124 * Send both relayd sockets to a specific consumer and domain. This is a
1125 * helper function to facilitate sending the information to the consumer for a
1126 * session.
1127 *
1128 * The consumer socket lock must be held by the caller.
1129 *
1130 * Returns LTTNG_OK, or an LTTng error code on failure.
1131 */
1132 static enum lttng_error_code send_consumer_relayd_sockets(
1133 enum lttng_domain_type domain,
1134 unsigned int session_id, struct consumer_output *consumer,
1135 struct consumer_socket *sock, const char *session_name,
1136 const char *hostname, const char *base_path, int session_live_timer,
1137 const uint64_t *current_chunk_id, time_t session_creation_time,
1138 bool session_name_contains_creation_time)
1139 {
1140 enum lttng_error_code status = LTTNG_OK;
1141
1142 assert(consumer);
1143 assert(sock);
1144
1145 /* Sending control relayd socket. */
1146 if (!sock->control_sock_sent) {
1147 status = send_consumer_relayd_socket(session_id,
1148 &consumer->dst.net.control, consumer, sock,
1149 session_name, hostname, base_path, session_live_timer,
1150 current_chunk_id, session_creation_time,
1151 session_name_contains_creation_time);
1152 if (status != LTTNG_OK) {
1153 goto error;
1154 }
1155 }
1156
1157 /* Sending data relayd socket. */
1158 if (!sock->data_sock_sent) {
1159 status = send_consumer_relayd_socket(session_id,
1160 &consumer->dst.net.data, consumer, sock,
1161 session_name, hostname, base_path, session_live_timer,
1162 current_chunk_id, session_creation_time,
1163 session_name_contains_creation_time);
1164 if (status != LTTNG_OK) {
1165 goto error;
1166 }
1167 }
1168
1169 error:
1170 return status;
1171 }
1172
1173 /*
1174 * Setup relayd connections for a tracing session. First creates the socket to
1175 * the relayd and send them to the right domain consumer. Consumer type MUST be
1176 * network.
1177 */
1178 int cmd_setup_relayd(struct ltt_session *session)
1179 {
1180 int ret = LTTNG_OK;
1181 struct ltt_ust_session *usess;
1182 struct ltt_kernel_session *ksess;
1183 struct consumer_socket *socket;
1184 struct lttng_ht_iter iter;
1185 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
1186
1187 assert(session);
1188
1189 usess = session->ust_session;
1190 ksess = session->kernel_session;
1191
1192 DBG("Setting relayd for session %s", session->name);
1193
1194 rcu_read_lock();
1195 if (session->current_trace_chunk) {
1196 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1197 session->current_trace_chunk, &current_chunk_id.value);
1198
1199 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1200 current_chunk_id.is_set = true;
1201 } else {
1202 ERR("Failed to get current trace chunk id");
1203 ret = LTTNG_ERR_UNK;
1204 goto error;
1205 }
1206 }
1207
1208 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1209 && usess->consumer->enabled) {
1210 /* For each consumer socket, send relayd sockets */
1211 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1212 socket, node.node) {
1213 pthread_mutex_lock(socket->lock);
1214 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
1215 usess->consumer, socket,
1216 session->name, session->hostname,
1217 session->base_path,
1218 session->live_timer,
1219 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1220 session->creation_time,
1221 session->name_contains_creation_time);
1222 pthread_mutex_unlock(socket->lock);
1223 if (ret != LTTNG_OK) {
1224 goto error;
1225 }
1226 /* Session is now ready for network streaming. */
1227 session->net_handle = 1;
1228 }
1229 session->consumer->relay_major_version =
1230 usess->consumer->relay_major_version;
1231 session->consumer->relay_minor_version =
1232 usess->consumer->relay_minor_version;
1233 session->consumer->relay_allows_clear =
1234 usess->consumer->relay_allows_clear;
1235 }
1236
1237 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1238 && ksess->consumer->enabled) {
1239 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1240 socket, node.node) {
1241 pthread_mutex_lock(socket->lock);
1242 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
1243 ksess->consumer, socket,
1244 session->name, session->hostname,
1245 session->base_path,
1246 session->live_timer,
1247 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1248 session->creation_time,
1249 session->name_contains_creation_time);
1250 pthread_mutex_unlock(socket->lock);
1251 if (ret != LTTNG_OK) {
1252 goto error;
1253 }
1254 /* Session is now ready for network streaming. */
1255 session->net_handle = 1;
1256 }
1257 session->consumer->relay_major_version =
1258 ksess->consumer->relay_major_version;
1259 session->consumer->relay_minor_version =
1260 ksess->consumer->relay_minor_version;
1261 session->consumer->relay_allows_clear =
1262 ksess->consumer->relay_allows_clear;
1263 }
1264
1265 error:
1266 rcu_read_unlock();
1267 return ret;
1268 }
1269
1270 /*
1271 * Start a kernel session by opening all necessary streams.
1272 */
1273 int start_kernel_session(struct ltt_kernel_session *ksess)
1274 {
1275 int ret;
1276 struct ltt_kernel_channel *kchan;
1277
1278 /* Open kernel metadata */
1279 if (ksess->metadata == NULL && ksess->output_traces) {
1280 ret = kernel_open_metadata(ksess);
1281 if (ret < 0) {
1282 ret = LTTNG_ERR_KERN_META_FAIL;
1283 goto error;
1284 }
1285 }
1286
1287 /* Open kernel metadata stream */
1288 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1289 ret = kernel_open_metadata_stream(ksess);
1290 if (ret < 0) {
1291 ERR("Kernel create metadata stream failed");
1292 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1293 goto error;
1294 }
1295 }
1296
1297 /* For each channel */
1298 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1299 if (kchan->stream_count == 0) {
1300 ret = kernel_open_channel_stream(kchan);
1301 if (ret < 0) {
1302 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1303 goto error;
1304 }
1305 /* Update the stream global counter */
1306 ksess->stream_count_global += ret;
1307 }
1308 }
1309
1310 /* Setup kernel consumer socket and send fds to it */
1311 ret = init_kernel_tracing(ksess);
1312 if (ret != 0) {
1313 ret = LTTNG_ERR_KERN_START_FAIL;
1314 goto error;
1315 }
1316
1317 /* This start the kernel tracing */
1318 ret = kernel_start_session(ksess);
1319 if (ret < 0) {
1320 ret = LTTNG_ERR_KERN_START_FAIL;
1321 goto error;
1322 }
1323
1324 /* Quiescent wait after starting trace */
1325 kernel_wait_quiescent();
1326
1327 ksess->active = 1;
1328
1329 ret = LTTNG_OK;
1330
1331 error:
1332 return ret;
1333 }
1334
1335 int stop_kernel_session(struct ltt_kernel_session *ksess)
1336 {
1337 struct ltt_kernel_channel *kchan;
1338 bool error_occurred = false;
1339 int ret;
1340
1341 if (!ksess || !ksess->active) {
1342 return LTTNG_OK;
1343 }
1344 DBG("Stopping kernel tracing");
1345
1346 ret = kernel_stop_session(ksess);
1347 if (ret < 0) {
1348 ret = LTTNG_ERR_KERN_STOP_FAIL;
1349 goto error;
1350 }
1351
1352 kernel_wait_quiescent();
1353
1354 /* Flush metadata after stopping (if exists) */
1355 if (ksess->metadata_stream_fd >= 0) {
1356 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1357 if (ret < 0) {
1358 ERR("Kernel metadata flush failed");
1359 error_occurred = true;
1360 }
1361 }
1362
1363 /* Flush all buffers after stopping */
1364 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1365 ret = kernel_flush_buffer(kchan);
1366 if (ret < 0) {
1367 ERR("Kernel flush buffer error");
1368 error_occurred = true;
1369 }
1370 }
1371
1372 ksess->active = 0;
1373 if (error_occurred) {
1374 ret = LTTNG_ERR_UNK;
1375 } else {
1376 ret = LTTNG_OK;
1377 }
1378 error:
1379 return ret;
1380 }
1381
1382 /*
1383 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1384 */
1385 int cmd_disable_channel(struct ltt_session *session,
1386 enum lttng_domain_type domain, char *channel_name)
1387 {
1388 int ret;
1389 struct ltt_ust_session *usess;
1390
1391 usess = session->ust_session;
1392
1393 rcu_read_lock();
1394
1395 switch (domain) {
1396 case LTTNG_DOMAIN_KERNEL:
1397 {
1398 ret = channel_kernel_disable(session->kernel_session,
1399 channel_name);
1400 if (ret != LTTNG_OK) {
1401 goto error;
1402 }
1403
1404 kernel_wait_quiescent();
1405 break;
1406 }
1407 case LTTNG_DOMAIN_UST:
1408 {
1409 struct ltt_ust_channel *uchan;
1410 struct lttng_ht *chan_ht;
1411
1412 chan_ht = usess->domain_global.channels;
1413
1414 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1415 if (uchan == NULL) {
1416 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1417 goto error;
1418 }
1419
1420 ret = channel_ust_disable(usess, uchan);
1421 if (ret != LTTNG_OK) {
1422 goto error;
1423 }
1424 break;
1425 }
1426 default:
1427 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1428 goto error;
1429 }
1430
1431 ret = LTTNG_OK;
1432
1433 error:
1434 rcu_read_unlock();
1435 return ret;
1436 }
1437
1438 /*
1439 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1440 *
1441 * The wpipe arguments is used as a notifier for the kernel thread.
1442 */
1443 int cmd_enable_channel(struct ltt_session *session,
1444 const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe)
1445 {
1446 int ret;
1447 struct ltt_ust_session *usess = session->ust_session;
1448 struct lttng_ht *chan_ht;
1449 size_t len;
1450 struct lttng_channel attr;
1451
1452 assert(session);
1453 assert(_attr);
1454 assert(domain);
1455
1456 attr = *_attr;
1457 len = lttng_strnlen(attr.name, sizeof(attr.name));
1458
1459 /* Validate channel name */
1460 if (attr.name[0] == '.' ||
1461 memchr(attr.name, '/', len) != NULL) {
1462 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1463 goto end;
1464 }
1465
1466 DBG("Enabling channel %s for session %s", attr.name, session->name);
1467
1468 rcu_read_lock();
1469
1470 /*
1471 * Don't try to enable a channel if the session has been started at
1472 * some point in time before. The tracer does not allow it.
1473 */
1474 if (session->has_been_started) {
1475 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1476 goto error;
1477 }
1478
1479 /*
1480 * If the session is a live session, remove the switch timer, the
1481 * live timer does the same thing but sends also synchronisation
1482 * beacons for inactive streams.
1483 */
1484 if (session->live_timer > 0) {
1485 attr.attr.live_timer_interval = session->live_timer;
1486 attr.attr.switch_timer_interval = 0;
1487 }
1488
1489 /* Check for feature support */
1490 switch (domain->type) {
1491 case LTTNG_DOMAIN_KERNEL:
1492 {
1493 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1494 /* Sampling position of buffer is not supported */
1495 WARN("Kernel tracer does not support buffer monitoring. "
1496 "Setting the monitor interval timer to 0 "
1497 "(disabled) for channel '%s' of session '%s'",
1498 attr.name, session->name);
1499 lttng_channel_set_monitor_timer_interval(&attr, 0);
1500 }
1501 break;
1502 }
1503 case LTTNG_DOMAIN_UST:
1504 break;
1505 case LTTNG_DOMAIN_JUL:
1506 case LTTNG_DOMAIN_LOG4J:
1507 case LTTNG_DOMAIN_PYTHON:
1508 if (!agent_tracing_is_enabled()) {
1509 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1510 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1511 goto error;
1512 }
1513 break;
1514 default:
1515 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1516 goto error;
1517 }
1518
1519 switch (domain->type) {
1520 case LTTNG_DOMAIN_KERNEL:
1521 {
1522 struct ltt_kernel_channel *kchan;
1523
1524 kchan = trace_kernel_get_channel_by_name(attr.name,
1525 session->kernel_session);
1526 if (kchan == NULL) {
1527 if (session->snapshot.nb_output > 0 ||
1528 session->snapshot_mode) {
1529 /* Enforce mmap output for snapshot sessions. */
1530 attr.attr.output = LTTNG_EVENT_MMAP;
1531 }
1532 ret = channel_kernel_create(session->kernel_session, &attr, wpipe);
1533 if (attr.name[0] != '\0') {
1534 session->kernel_session->has_non_default_channel = 1;
1535 }
1536 } else {
1537 ret = channel_kernel_enable(session->kernel_session, kchan);
1538 }
1539
1540 if (ret != LTTNG_OK) {
1541 goto error;
1542 }
1543
1544 kernel_wait_quiescent();
1545 break;
1546 }
1547 case LTTNG_DOMAIN_UST:
1548 case LTTNG_DOMAIN_JUL:
1549 case LTTNG_DOMAIN_LOG4J:
1550 case LTTNG_DOMAIN_PYTHON:
1551 {
1552 struct ltt_ust_channel *uchan;
1553
1554 /*
1555 * FIXME
1556 *
1557 * Current agent implementation limitations force us to allow
1558 * only one channel at once in "agent" subdomains. Each
1559 * subdomain has a default channel name which must be strictly
1560 * adhered to.
1561 */
1562 if (domain->type == LTTNG_DOMAIN_JUL) {
1563 if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME,
1564 LTTNG_SYMBOL_NAME_LEN)) {
1565 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1566 goto error;
1567 }
1568 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1569 if (strncmp(attr.name, DEFAULT_LOG4J_CHANNEL_NAME,
1570 LTTNG_SYMBOL_NAME_LEN)) {
1571 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1572 goto error;
1573 }
1574 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1575 if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME,
1576 LTTNG_SYMBOL_NAME_LEN)) {
1577 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1578 goto error;
1579 }
1580 }
1581
1582 chan_ht = usess->domain_global.channels;
1583
1584 uchan = trace_ust_find_channel_by_name(chan_ht, attr.name);
1585 if (uchan == NULL) {
1586 ret = channel_ust_create(usess, &attr, domain->buf_type);
1587 if (attr.name[0] != '\0') {
1588 usess->has_non_default_channel = 1;
1589 }
1590 } else {
1591 ret = channel_ust_enable(usess, uchan);
1592 }
1593 break;
1594 }
1595 default:
1596 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1597 goto error;
1598 }
1599
1600 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
1601 session->has_non_mmap_channel = true;
1602 }
1603 error:
1604 rcu_read_unlock();
1605 end:
1606 return ret;
1607 }
1608
1609 enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy(
1610 struct ltt_session *session,
1611 enum lttng_domain_type domain,
1612 enum lttng_process_attr process_attr,
1613 enum lttng_tracking_policy *policy)
1614 {
1615 enum lttng_error_code ret_code = LTTNG_OK;
1616 const struct process_attr_tracker *tracker;
1617
1618 switch (domain) {
1619 case LTTNG_DOMAIN_KERNEL:
1620 if (!session->kernel_session) {
1621 ret_code = LTTNG_ERR_INVALID;
1622 goto end;
1623 }
1624 tracker = kernel_get_process_attr_tracker(
1625 session->kernel_session, process_attr);
1626 break;
1627 case LTTNG_DOMAIN_UST:
1628 if (!session->ust_session) {
1629 ret_code = LTTNG_ERR_INVALID;
1630 goto end;
1631 }
1632 tracker = trace_ust_get_process_attr_tracker(
1633 session->ust_session, process_attr);
1634 break;
1635 default:
1636 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1637 goto end;
1638 }
1639 if (tracker) {
1640 *policy = process_attr_tracker_get_tracking_policy(tracker);
1641 } else {
1642 ret_code = LTTNG_ERR_INVALID;
1643 }
1644 end:
1645 return ret_code;
1646 }
1647
1648 enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy(
1649 struct ltt_session *session,
1650 enum lttng_domain_type domain,
1651 enum lttng_process_attr process_attr,
1652 enum lttng_tracking_policy policy)
1653 {
1654 enum lttng_error_code ret_code = LTTNG_OK;
1655
1656 switch (policy) {
1657 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1658 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1659 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1660 break;
1661 default:
1662 ret_code = LTTNG_ERR_INVALID;
1663 goto end;
1664 }
1665
1666 switch (domain) {
1667 case LTTNG_DOMAIN_KERNEL:
1668 if (!session->kernel_session) {
1669 ret_code = LTTNG_ERR_INVALID;
1670 goto end;
1671 }
1672 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1673 session->kernel_session, process_attr, policy);
1674 break;
1675 case LTTNG_DOMAIN_UST:
1676 if (!session->ust_session) {
1677 ret_code = LTTNG_ERR_INVALID;
1678 goto end;
1679 }
1680 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1681 session->ust_session, process_attr, policy);
1682 break;
1683 default:
1684 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1685 break;
1686 }
1687 end:
1688 return ret_code;
1689 }
1690
1691 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value(
1692 struct ltt_session *session,
1693 enum lttng_domain_type domain,
1694 enum lttng_process_attr process_attr,
1695 const struct process_attr_value *value)
1696 {
1697 enum lttng_error_code ret_code = LTTNG_OK;
1698
1699 switch (domain) {
1700 case LTTNG_DOMAIN_KERNEL:
1701 if (!session->kernel_session) {
1702 ret_code = LTTNG_ERR_INVALID;
1703 goto end;
1704 }
1705 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1706 session->kernel_session, process_attr, value);
1707 break;
1708 case LTTNG_DOMAIN_UST:
1709 if (!session->ust_session) {
1710 ret_code = LTTNG_ERR_INVALID;
1711 goto end;
1712 }
1713 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1714 session->ust_session, process_attr, value);
1715 break;
1716 default:
1717 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1718 break;
1719 }
1720 end:
1721 return ret_code;
1722 }
1723
1724 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value(
1725 struct ltt_session *session,
1726 enum lttng_domain_type domain,
1727 enum lttng_process_attr process_attr,
1728 const struct process_attr_value *value)
1729 {
1730 enum lttng_error_code ret_code = LTTNG_OK;
1731
1732 switch (domain) {
1733 case LTTNG_DOMAIN_KERNEL:
1734 if (!session->kernel_session) {
1735 ret_code = LTTNG_ERR_INVALID;
1736 goto end;
1737 }
1738 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1739 session->kernel_session, process_attr, value);
1740 break;
1741 case LTTNG_DOMAIN_UST:
1742 if (!session->ust_session) {
1743 ret_code = LTTNG_ERR_INVALID;
1744 goto end;
1745 }
1746 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1747 session->ust_session, process_attr, value);
1748 break;
1749 default:
1750 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1751 break;
1752 }
1753 end:
1754 return ret_code;
1755 }
1756
1757 enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set(
1758 struct ltt_session *session,
1759 enum lttng_domain_type domain,
1760 enum lttng_process_attr process_attr,
1761 struct lttng_process_attr_values **values)
1762 {
1763 enum lttng_error_code ret_code = LTTNG_OK;
1764 const struct process_attr_tracker *tracker;
1765 enum process_attr_tracker_status status;
1766
1767 switch (domain) {
1768 case LTTNG_DOMAIN_KERNEL:
1769 if (!session->kernel_session) {
1770 ret_code = LTTNG_ERR_INVALID;
1771 goto end;
1772 }
1773 tracker = kernel_get_process_attr_tracker(
1774 session->kernel_session, process_attr);
1775 break;
1776 case LTTNG_DOMAIN_UST:
1777 if (!session->ust_session) {
1778 ret_code = LTTNG_ERR_INVALID;
1779 goto end;
1780 }
1781 tracker = trace_ust_get_process_attr_tracker(
1782 session->ust_session, process_attr);
1783 break;
1784 default:
1785 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1786 goto end;
1787 }
1788
1789 if (!tracker) {
1790 ret_code = LTTNG_ERR_INVALID;
1791 goto end;
1792 }
1793
1794 status = process_attr_tracker_get_inclusion_set(tracker, values);
1795 switch (status) {
1796 case PROCESS_ATTR_TRACKER_STATUS_OK:
1797 ret_code = LTTNG_OK;
1798 break;
1799 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1800 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1801 break;
1802 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1803 ret_code = LTTNG_ERR_NOMEM;
1804 break;
1805 default:
1806 ret_code = LTTNG_ERR_UNK;
1807 break;
1808 }
1809
1810 end:
1811 return ret_code;
1812 }
1813
1814 /*
1815 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1816 */
1817 int cmd_disable_event(struct ltt_session *session,
1818 enum lttng_domain_type domain, const char *channel_name,
1819 const struct lttng_event *event)
1820 {
1821 int ret;
1822 const char *event_name;
1823
1824 DBG("Disable event command for event \'%s\'", event->name);
1825
1826 event_name = event->name;
1827
1828 /* Error out on unhandled search criteria */
1829 if (event->loglevel_type || event->loglevel != -1 || event->enabled
1830 || event->pid || event->filter || event->exclusion) {
1831 ret = LTTNG_ERR_UNK;
1832 goto error;
1833 }
1834
1835 rcu_read_lock();
1836
1837 switch (domain) {
1838 case LTTNG_DOMAIN_KERNEL:
1839 {
1840 struct ltt_kernel_channel *kchan;
1841 struct ltt_kernel_session *ksess;
1842
1843 ksess = session->kernel_session;
1844
1845 /*
1846 * If a non-default channel has been created in the
1847 * session, explicitely require that -c chan_name needs
1848 * to be provided.
1849 */
1850 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1851 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1852 goto error_unlock;
1853 }
1854
1855 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1856 if (kchan == NULL) {
1857 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1858 goto error_unlock;
1859 }
1860
1861 switch (event->type) {
1862 case LTTNG_EVENT_ALL:
1863 case LTTNG_EVENT_TRACEPOINT:
1864 case LTTNG_EVENT_SYSCALL:
1865 case LTTNG_EVENT_PROBE:
1866 case LTTNG_EVENT_FUNCTION:
1867 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1868 if (event_name[0] == '\0') {
1869 ret = event_kernel_disable_event(kchan,
1870 NULL, event->type);
1871 } else {
1872 ret = event_kernel_disable_event(kchan,
1873 event_name, event->type);
1874 }
1875 if (ret != LTTNG_OK) {
1876 goto error_unlock;
1877 }
1878 break;
1879 default:
1880 ret = LTTNG_ERR_UNK;
1881 goto error_unlock;
1882 }
1883
1884 kernel_wait_quiescent();
1885 break;
1886 }
1887 case LTTNG_DOMAIN_UST:
1888 {
1889 struct ltt_ust_channel *uchan;
1890 struct ltt_ust_session *usess;
1891
1892 usess = session->ust_session;
1893
1894 if (validate_ust_event_name(event_name)) {
1895 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1896 goto error_unlock;
1897 }
1898
1899 /*
1900 * If a non-default channel has been created in the
1901 * session, explicitly require that -c chan_name needs
1902 * to be provided.
1903 */
1904 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1905 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1906 goto error_unlock;
1907 }
1908
1909 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1910 channel_name);
1911 if (uchan == NULL) {
1912 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1913 goto error_unlock;
1914 }
1915
1916 switch (event->type) {
1917 case LTTNG_EVENT_ALL:
1918 /*
1919 * An empty event name means that everything
1920 * should be disabled.
1921 */
1922 if (event->name[0] == '\0') {
1923 ret = event_ust_disable_all_tracepoints(usess, uchan);
1924 } else {
1925 ret = event_ust_disable_tracepoint(usess, uchan,
1926 event_name);
1927 }
1928 if (ret != LTTNG_OK) {
1929 goto error_unlock;
1930 }
1931 break;
1932 default:
1933 ret = LTTNG_ERR_UNK;
1934 goto error_unlock;
1935 }
1936
1937 DBG3("Disable UST event %s in channel %s completed", event_name,
1938 channel_name);
1939 break;
1940 }
1941 case LTTNG_DOMAIN_LOG4J:
1942 case LTTNG_DOMAIN_JUL:
1943 case LTTNG_DOMAIN_PYTHON:
1944 {
1945 struct agent *agt;
1946 struct ltt_ust_session *usess = session->ust_session;
1947
1948 assert(usess);
1949
1950 switch (event->type) {
1951 case LTTNG_EVENT_ALL:
1952 break;
1953 default:
1954 ret = LTTNG_ERR_UNK;
1955 goto error_unlock;
1956 }
1957
1958 agt = trace_ust_find_agent(usess, domain);
1959 if (!agt) {
1960 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1961 goto error_unlock;
1962 }
1963 /*
1964 * An empty event name means that everything
1965 * should be disabled.
1966 */
1967 if (event->name[0] == '\0') {
1968 ret = event_agent_disable_all(usess, agt);
1969 } else {
1970 ret = event_agent_disable(usess, agt, event_name);
1971 }
1972 if (ret != LTTNG_OK) {
1973 goto error_unlock;
1974 }
1975
1976 break;
1977 }
1978 default:
1979 ret = LTTNG_ERR_UND;
1980 goto error_unlock;
1981 }
1982
1983 ret = LTTNG_OK;
1984
1985 error_unlock:
1986 rcu_read_unlock();
1987 error:
1988 return ret;
1989 }
1990
1991 /*
1992 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1993 */
1994 int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
1995 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
1996 {
1997 int ret, chan_kern_created = 0, chan_ust_created = 0;
1998 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1999
2000 /*
2001 * Don't try to add a context if the session has been started at
2002 * some point in time before. The tracer does not allow it and would
2003 * result in a corrupted trace.
2004 */
2005 if (session->has_been_started) {
2006 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2007 goto end;
2008 }
2009
2010 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2011 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
2012 app_ctx_name = ctx->u.app_ctx.ctx_name;
2013 }
2014
2015 switch (domain) {
2016 case LTTNG_DOMAIN_KERNEL:
2017 assert(session->kernel_session);
2018
2019 if (session->kernel_session->channel_count == 0) {
2020 /* Create default channel */
2021 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
2022 if (ret != LTTNG_OK) {
2023 goto error;
2024 }
2025 chan_kern_created = 1;
2026 }
2027 /* Add kernel context to kernel tracer */
2028 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
2029 if (ret != LTTNG_OK) {
2030 goto error;
2031 }
2032 break;
2033 case LTTNG_DOMAIN_JUL:
2034 case LTTNG_DOMAIN_LOG4J:
2035 {
2036 /*
2037 * Validate channel name.
2038 * If no channel name is given and the domain is JUL or LOG4J,
2039 * set it to the appropriate domain-specific channel name. If
2040 * a name is provided but does not match the expexted channel
2041 * name, return an error.
2042 */
2043 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
2044 strcmp(channel_name,
2045 DEFAULT_JUL_CHANNEL_NAME)) {
2046 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2047 goto error;
2048 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
2049 strcmp(channel_name,
2050 DEFAULT_LOG4J_CHANNEL_NAME)) {
2051 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2052 goto error;
2053 }
2054 /* break is _not_ missing here. */
2055 }
2056 case LTTNG_DOMAIN_UST:
2057 {
2058 struct ltt_ust_session *usess = session->ust_session;
2059 unsigned int chan_count;
2060
2061 assert(usess);
2062
2063 chan_count = lttng_ht_get_count(usess->domain_global.channels);
2064 if (chan_count == 0) {
2065 struct lttng_channel *attr;
2066 /* Create default channel */
2067 attr = channel_new_default_attr(domain, usess->buffer_type);
2068 if (attr == NULL) {
2069 ret = LTTNG_ERR_FATAL;
2070 goto error;
2071 }
2072
2073 ret = channel_ust_create(usess, attr, usess->buffer_type);
2074 if (ret != LTTNG_OK) {
2075 free(attr);
2076 goto error;
2077 }
2078 channel_attr_destroy(attr);
2079 chan_ust_created = 1;
2080 }
2081
2082 ret = context_ust_add(usess, domain, ctx, channel_name);
2083 free(app_ctx_provider_name);
2084 free(app_ctx_name);
2085 app_ctx_name = NULL;
2086 app_ctx_provider_name = NULL;
2087 if (ret != LTTNG_OK) {
2088 goto error;
2089 }
2090 break;
2091 }
2092 default:
2093 ret = LTTNG_ERR_UND;
2094 goto error;
2095 }
2096
2097 ret = LTTNG_OK;
2098 goto end;
2099
2100 error:
2101 if (chan_kern_created) {
2102 struct ltt_kernel_channel *kchan =
2103 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
2104 session->kernel_session);
2105 /* Created previously, this should NOT fail. */
2106 assert(kchan);
2107 kernel_destroy_channel(kchan);
2108 }
2109
2110 if (chan_ust_created) {
2111 struct ltt_ust_channel *uchan =
2112 trace_ust_find_channel_by_name(
2113 session->ust_session->domain_global.channels,
2114 DEFAULT_CHANNEL_NAME);
2115 /* Created previously, this should NOT fail. */
2116 assert(uchan);
2117 /* Remove from the channel list of the session. */
2118 trace_ust_delete_channel(session->ust_session->domain_global.channels,
2119 uchan);
2120 trace_ust_destroy_channel(uchan);
2121 }
2122 end:
2123 free(app_ctx_provider_name);
2124 free(app_ctx_name);
2125 return ret;
2126 }
2127
2128 static inline bool name_starts_with(const char *name, const char *prefix)
2129 {
2130 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
2131
2132 return !strncmp(name, prefix, max_cmp_len);
2133 }
2134
2135 /* Perform userspace-specific event name validation */
2136 static int validate_ust_event_name(const char *name)
2137 {
2138 int ret = 0;
2139
2140 if (!name) {
2141 ret = -1;
2142 goto end;
2143 }
2144
2145 /*
2146 * Check name against all internal UST event component namespaces used
2147 * by the agents.
2148 */
2149 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2150 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2151 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2152 ret = -1;
2153 }
2154
2155 end:
2156 return ret;
2157 }
2158
2159 /*
2160 * Internal version of cmd_enable_event() with a supplemental
2161 * "internal_event" flag which is used to enable internal events which should
2162 * be hidden from clients. Such events are used in the agent implementation to
2163 * enable the events through which all "agent" events are funeled.
2164 */
2165 static int _cmd_enable_event(struct ltt_session *session,
2166 const struct lttng_domain *domain,
2167 char *channel_name, struct lttng_event *event,
2168 char *filter_expression,
2169 struct lttng_filter_bytecode *filter,
2170 struct lttng_event_exclusion *exclusion,
2171 int wpipe, bool internal_event)
2172 {
2173 int ret = 0, channel_created = 0;
2174 struct lttng_channel *attr = NULL;
2175
2176 assert(session);
2177 assert(event);
2178 assert(channel_name);
2179
2180 /* If we have a filter, we must have its filter expression */
2181 assert(!(!!filter_expression ^ !!filter));
2182
2183 /* Normalize event name as a globbing pattern */
2184 strutils_normalize_star_glob_pattern(event->name);
2185
2186 /* Normalize exclusion names as globbing patterns */
2187 if (exclusion) {
2188 size_t i;
2189
2190 for (i = 0; i < exclusion->count; i++) {
2191 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2192
2193 strutils_normalize_star_glob_pattern(name);
2194 }
2195 }
2196
2197 DBG("Enable event command for event \'%s\'", event->name);
2198
2199 rcu_read_lock();
2200
2201 switch (domain->type) {
2202 case LTTNG_DOMAIN_KERNEL:
2203 {
2204 struct ltt_kernel_channel *kchan;
2205
2206 /*
2207 * If a non-default channel has been created in the
2208 * session, explicitely require that -c chan_name needs
2209 * to be provided.
2210 */
2211 if (session->kernel_session->has_non_default_channel
2212 && channel_name[0] == '\0') {
2213 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2214 goto error;
2215 }
2216
2217 kchan = trace_kernel_get_channel_by_name(channel_name,
2218 session->kernel_session);
2219 if (kchan == NULL) {
2220 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2221 LTTNG_BUFFER_GLOBAL);
2222 if (attr == NULL) {
2223 ret = LTTNG_ERR_FATAL;
2224 goto error;
2225 }
2226 if (lttng_strncpy(attr->name, channel_name,
2227 sizeof(attr->name))) {
2228 ret = LTTNG_ERR_INVALID;
2229 goto error;
2230 }
2231
2232 ret = cmd_enable_channel(session, domain, attr, wpipe);
2233 if (ret != LTTNG_OK) {
2234 goto error;
2235 }
2236 channel_created = 1;
2237 }
2238
2239 /* Get the newly created kernel channel pointer */
2240 kchan = trace_kernel_get_channel_by_name(channel_name,
2241 session->kernel_session);
2242 if (kchan == NULL) {
2243 /* This sould not happen... */
2244 ret = LTTNG_ERR_FATAL;
2245 goto error;
2246 }
2247
2248 switch (event->type) {
2249 case LTTNG_EVENT_ALL:
2250 {
2251 char *filter_expression_a = NULL;
2252 struct lttng_filter_bytecode *filter_a = NULL;
2253
2254 /*
2255 * We need to duplicate filter_expression and filter,
2256 * because ownership is passed to first enable
2257 * event.
2258 */
2259 if (filter_expression) {
2260 filter_expression_a = strdup(filter_expression);
2261 if (!filter_expression_a) {
2262 ret = LTTNG_ERR_FATAL;
2263 goto error;
2264 }
2265 }
2266 if (filter) {
2267 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2268 if (!filter_a) {
2269 free(filter_expression_a);
2270 ret = LTTNG_ERR_FATAL;
2271 goto error;
2272 }
2273 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2274 }
2275 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2276 ret = event_kernel_enable_event(kchan, event,
2277 filter_expression, filter);
2278 /* We have passed ownership */
2279 filter_expression = NULL;
2280 filter = NULL;
2281 if (ret != LTTNG_OK) {
2282 if (channel_created) {
2283 /* Let's not leak a useless channel. */
2284 kernel_destroy_channel(kchan);
2285 }
2286 free(filter_expression_a);
2287 free(filter_a);
2288 goto error;
2289 }
2290 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2291 ret = event_kernel_enable_event(kchan, event,
2292 filter_expression_a, filter_a);
2293 /* We have passed ownership */
2294 filter_expression_a = NULL;
2295 filter_a = NULL;
2296 if (ret != LTTNG_OK) {
2297 goto error;
2298 }
2299 break;
2300 }
2301 case LTTNG_EVENT_PROBE:
2302 case LTTNG_EVENT_USERSPACE_PROBE:
2303 case LTTNG_EVENT_FUNCTION:
2304 case LTTNG_EVENT_FUNCTION_ENTRY:
2305 case LTTNG_EVENT_TRACEPOINT:
2306 ret = event_kernel_enable_event(kchan, event,
2307 filter_expression, filter);
2308 /* We have passed ownership */
2309 filter_expression = NULL;
2310 filter = NULL;
2311 if (ret != LTTNG_OK) {
2312 if (channel_created) {
2313 /* Let's not leak a useless channel. */
2314 kernel_destroy_channel(kchan);
2315 }
2316 goto error;
2317 }
2318 break;
2319 case LTTNG_EVENT_SYSCALL:
2320 ret = event_kernel_enable_event(kchan, event,
2321 filter_expression, filter);
2322 /* We have passed ownership */
2323 filter_expression = NULL;
2324 filter = NULL;
2325 if (ret != LTTNG_OK) {
2326 goto error;
2327 }
2328 break;
2329 default:
2330 ret = LTTNG_ERR_UNK;
2331 goto error;
2332 }
2333
2334 kernel_wait_quiescent();
2335 break;
2336 }
2337 case LTTNG_DOMAIN_UST:
2338 {
2339 struct ltt_ust_channel *uchan;
2340 struct ltt_ust_session *usess = session->ust_session;
2341
2342 assert(usess);
2343
2344 /*
2345 * If a non-default channel has been created in the
2346 * session, explicitely require that -c chan_name needs
2347 * to be provided.
2348 */
2349 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2350 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2351 goto error;
2352 }
2353
2354 /* Get channel from global UST domain */
2355 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2356 channel_name);
2357 if (uchan == NULL) {
2358 /* Create default channel */
2359 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2360 usess->buffer_type);
2361 if (attr == NULL) {
2362 ret = LTTNG_ERR_FATAL;
2363 goto error;
2364 }
2365 if (lttng_strncpy(attr->name, channel_name,
2366 sizeof(attr->name))) {
2367 ret = LTTNG_ERR_INVALID;
2368 goto error;
2369 }
2370
2371 ret = cmd_enable_channel(session, domain, attr, wpipe);
2372 if (ret != LTTNG_OK) {
2373 goto error;
2374 }
2375
2376 /* Get the newly created channel reference back */
2377 uchan = trace_ust_find_channel_by_name(
2378 usess->domain_global.channels, channel_name);
2379 assert(uchan);
2380 }
2381
2382 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2383 /*
2384 * Don't allow users to add UST events to channels which
2385 * are assigned to a userspace subdomain (JUL, Log4J,
2386 * Python, etc.).
2387 */
2388 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2389 goto error;
2390 }
2391
2392 if (!internal_event) {
2393 /*
2394 * Ensure the event name is not reserved for internal
2395 * use.
2396 */
2397 ret = validate_ust_event_name(event->name);
2398 if (ret) {
2399 WARN("Userspace event name %s failed validation.",
2400 event->name);
2401 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2402 goto error;
2403 }
2404 }
2405
2406 /* At this point, the session and channel exist on the tracer */
2407 ret = event_ust_enable_tracepoint(usess, uchan, event,
2408 filter_expression, filter, exclusion,
2409 internal_event);
2410 /* We have passed ownership */
2411 filter_expression = NULL;
2412 filter = NULL;
2413 exclusion = NULL;
2414 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2415 goto already_enabled;
2416 } else if (ret != LTTNG_OK) {
2417 goto error;
2418 }
2419 break;
2420 }
2421 case LTTNG_DOMAIN_LOG4J:
2422 case LTTNG_DOMAIN_JUL:
2423 case LTTNG_DOMAIN_PYTHON:
2424 {
2425 const char *default_event_name, *default_chan_name;
2426 struct agent *agt;
2427 struct lttng_event uevent;
2428 struct lttng_domain tmp_dom;
2429 struct ltt_ust_session *usess = session->ust_session;
2430
2431 assert(usess);
2432
2433 if (!agent_tracing_is_enabled()) {
2434 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2435 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2436 goto error;
2437 }
2438
2439 agt = trace_ust_find_agent(usess, domain->type);
2440 if (!agt) {
2441 agt = agent_create(domain->type);
2442 if (!agt) {
2443 ret = LTTNG_ERR_NOMEM;
2444 goto error;
2445 }
2446 agent_add(agt, usess->agents);
2447 }
2448
2449 /* Create the default tracepoint. */
2450 memset(&uevent, 0, sizeof(uevent));
2451 uevent.type = LTTNG_EVENT_TRACEPOINT;
2452 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2453 default_event_name = event_get_default_agent_ust_name(
2454 domain->type);
2455 if (!default_event_name) {
2456 ret = LTTNG_ERR_FATAL;
2457 goto error;
2458 }
2459 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2460 uevent.name[sizeof(uevent.name) - 1] = '\0';
2461
2462 /*
2463 * The domain type is changed because we are about to enable the
2464 * default channel and event for the JUL domain that are hardcoded.
2465 * This happens in the UST domain.
2466 */
2467 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2468 tmp_dom.type = LTTNG_DOMAIN_UST;
2469
2470 switch (domain->type) {
2471 case LTTNG_DOMAIN_LOG4J:
2472 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2473 break;
2474 case LTTNG_DOMAIN_JUL:
2475 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2476 break;
2477 case LTTNG_DOMAIN_PYTHON:
2478 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2479 break;
2480 default:
2481 /* The switch/case we are in makes this impossible */
2482 assert(0);
2483 }
2484
2485 {
2486 char *filter_expression_copy = NULL;
2487 struct lttng_filter_bytecode *filter_copy = NULL;
2488
2489 if (filter) {
2490 const size_t filter_size = sizeof(
2491 struct lttng_filter_bytecode)
2492 + filter->len;
2493
2494 filter_copy = zmalloc(filter_size);
2495 if (!filter_copy) {
2496 ret = LTTNG_ERR_NOMEM;
2497 goto error;
2498 }
2499 memcpy(filter_copy, filter, filter_size);
2500
2501 filter_expression_copy =
2502 strdup(filter_expression);
2503 if (!filter_expression) {
2504 ret = LTTNG_ERR_NOMEM;
2505 }
2506
2507 if (!filter_expression_copy || !filter_copy) {
2508 free(filter_expression_copy);
2509 free(filter_copy);
2510 goto error;
2511 }
2512 }
2513
2514 ret = cmd_enable_event_internal(session, &tmp_dom,
2515 (char *) default_chan_name,
2516 &uevent, filter_expression_copy,
2517 filter_copy, NULL, wpipe);
2518 }
2519
2520 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2521 goto already_enabled;
2522 } else if (ret != LTTNG_OK) {
2523 goto error;
2524 }
2525
2526 /* The wild card * means that everything should be enabled. */
2527 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2528 ret = event_agent_enable_all(usess, agt, event, filter,
2529 filter_expression);
2530 } else {
2531 ret = event_agent_enable(usess, agt, event, filter,
2532 filter_expression);
2533 }
2534 filter = NULL;
2535 filter_expression = NULL;
2536 if (ret != LTTNG_OK) {
2537 goto error;
2538 }
2539
2540 break;
2541 }
2542 default:
2543 ret = LTTNG_ERR_UND;
2544 goto error;
2545 }
2546
2547 ret = LTTNG_OK;
2548
2549 already_enabled:
2550 error:
2551 free(filter_expression);
2552 free(filter);
2553 free(exclusion);
2554 channel_attr_destroy(attr);
2555 rcu_read_unlock();
2556 return ret;
2557 }
2558
2559 /*
2560 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2561 * We own filter, exclusion, and filter_expression.
2562 */
2563 int cmd_enable_event(struct ltt_session *session,
2564 const struct lttng_domain *domain,
2565 char *channel_name, struct lttng_event *event,
2566 char *filter_expression,
2567 struct lttng_filter_bytecode *filter,
2568 struct lttng_event_exclusion *exclusion,
2569 int wpipe)
2570 {
2571 return _cmd_enable_event(session, domain, channel_name, event,
2572 filter_expression, filter, exclusion, wpipe, false);
2573 }
2574
2575 /*
2576 * Enable an event which is internal to LTTng. An internal should
2577 * never be made visible to clients and are immune to checks such as
2578 * reserved names.
2579 */
2580 static int cmd_enable_event_internal(struct ltt_session *session,
2581 const struct lttng_domain *domain,
2582 char *channel_name, struct lttng_event *event,
2583 char *filter_expression,
2584 struct lttng_filter_bytecode *filter,
2585 struct lttng_event_exclusion *exclusion,
2586 int wpipe)
2587 {
2588 return _cmd_enable_event(session, domain, channel_name, event,
2589 filter_expression, filter, exclusion, wpipe, true);
2590 }
2591
2592 /*
2593 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2594 */
2595 ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2596 struct lttng_event **events)
2597 {
2598 int ret;
2599 ssize_t nb_events = 0;
2600
2601 switch (domain) {
2602 case LTTNG_DOMAIN_KERNEL:
2603 nb_events = kernel_list_events(events);
2604 if (nb_events < 0) {
2605 ret = LTTNG_ERR_KERN_LIST_FAIL;
2606 goto error;
2607 }
2608 break;
2609 case LTTNG_DOMAIN_UST:
2610 nb_events = ust_app_list_events(events);
2611 if (nb_events < 0) {
2612 ret = LTTNG_ERR_UST_LIST_FAIL;
2613 goto error;
2614 }
2615 break;
2616 case LTTNG_DOMAIN_LOG4J:
2617 case LTTNG_DOMAIN_JUL:
2618 case LTTNG_DOMAIN_PYTHON:
2619 nb_events = agent_list_events(events, domain);
2620 if (nb_events < 0) {
2621 ret = LTTNG_ERR_UST_LIST_FAIL;
2622 goto error;
2623 }
2624 break;
2625 default:
2626 ret = LTTNG_ERR_UND;
2627 goto error;
2628 }
2629
2630 return nb_events;
2631
2632 error:
2633 /* Return negative value to differentiate return code */
2634 return -ret;
2635 }
2636
2637 /*
2638 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2639 */
2640 ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2641 struct lttng_event_field **fields)
2642 {
2643 int ret;
2644 ssize_t nb_fields = 0;
2645
2646 switch (domain) {
2647 case LTTNG_DOMAIN_UST:
2648 nb_fields = ust_app_list_event_fields(fields);
2649 if (nb_fields < 0) {
2650 ret = LTTNG_ERR_UST_LIST_FAIL;
2651 goto error;
2652 }
2653 break;
2654 case LTTNG_DOMAIN_KERNEL:
2655 default: /* fall-through */
2656 ret = LTTNG_ERR_UND;
2657 goto error;
2658 }
2659
2660 return nb_fields;
2661
2662 error:
2663 /* Return negative value to differentiate return code */
2664 return -ret;
2665 }
2666
2667 ssize_t cmd_list_syscalls(struct lttng_event **events)
2668 {
2669 return syscall_table_list(events);
2670 }
2671
2672 /*
2673 * Command LTTNG_START_TRACE processed by the client thread.
2674 *
2675 * Called with session mutex held.
2676 */
2677 int cmd_start_trace(struct ltt_session *session)
2678 {
2679 enum lttng_error_code ret;
2680 unsigned long nb_chan = 0;
2681 struct ltt_kernel_session *ksession;
2682 struct ltt_ust_session *usess;
2683 const bool session_rotated_after_last_stop =
2684 session->rotated_after_last_stop;
2685 const bool session_cleared_after_last_stop =
2686 session->cleared_after_last_stop;
2687
2688 assert(session);
2689
2690 /* Ease our life a bit ;) */
2691 ksession = session->kernel_session;
2692 usess = session->ust_session;
2693
2694 /* Is the session already started? */
2695 if (session->active) {
2696 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2697 /* Perform nothing */
2698 goto end;
2699 }
2700
2701 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2702 !session->current_trace_chunk) {
2703 /*
2704 * A rotation was launched while the session was stopped and
2705 * it has not been completed yet. It is not possible to start
2706 * the session since starting the session here would require a
2707 * rotation from "NULL" to a new trace chunk. That rotation
2708 * would overlap with the ongoing rotation, which is not
2709 * supported.
2710 */
2711 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2712 session->name);
2713 ret = LTTNG_ERR_ROTATION_PENDING;
2714 goto error;
2715 }
2716
2717 /*
2718 * Starting a session without channel is useless since after that it's not
2719 * possible to enable channel thus inform the client.
2720 */
2721 if (usess && usess->domain_global.channels) {
2722 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2723 }
2724 if (ksession) {
2725 nb_chan += ksession->channel_count;
2726 }
2727 if (!nb_chan) {
2728 ret = LTTNG_ERR_NO_CHANNEL;
2729 goto error;
2730 }
2731
2732 session->active = 1;
2733 session->rotated_after_last_stop = false;
2734 session->cleared_after_last_stop = false;
2735 if (session->output_traces && !session->current_trace_chunk) {
2736 if (!session->has_been_started) {
2737 struct lttng_trace_chunk *trace_chunk;
2738
2739 DBG("Creating initial trace chunk of session \"%s\"",
2740 session->name);
2741 trace_chunk = session_create_new_trace_chunk(
2742 session, NULL, NULL, NULL);
2743 if (!trace_chunk) {
2744 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2745 goto error;
2746 }
2747 assert(!session->current_trace_chunk);
2748 ret = session_set_trace_chunk(session, trace_chunk,
2749 NULL);
2750 lttng_trace_chunk_put(trace_chunk);
2751 if (ret) {
2752 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2753 goto error;
2754 }
2755 } else {
2756 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2757 session->name);
2758 /*
2759 * Rotate existing streams into the new chunk.
2760 * This is a "quiet" rotation has no client has
2761 * explicitly requested this operation.
2762 *
2763 * There is also no need to wait for the rotation
2764 * to complete as it will happen immediately. No data
2765 * was produced as the session was stopped, so the
2766 * rotation should happen on reception of the command.
2767 */
2768 ret = cmd_rotate_session(session, NULL, true,
2769 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
2770 if (ret != LTTNG_OK) {
2771 goto error;
2772 }
2773 }
2774 }
2775
2776 /* Kernel tracing */
2777 if (ksession != NULL) {
2778 DBG("Start kernel tracing session %s", session->name);
2779 ret = start_kernel_session(ksession);
2780 if (ret != LTTNG_OK) {
2781 goto error;
2782 }
2783 }
2784
2785 /* Flag session that trace should start automatically */
2786 if (usess) {
2787 int int_ret = ust_app_start_trace_all(usess);
2788
2789 if (int_ret < 0) {
2790 ret = LTTNG_ERR_UST_START_FAIL;
2791 goto error;
2792 }
2793 }
2794
2795 /*
2796 * Open a packet in every stream of the session to ensure that viewers
2797 * can correctly identify the boundaries of the periods during which
2798 * tracing was active for this session.
2799 */
2800 ret = session_open_packets(session);
2801 if (ret != LTTNG_OK) {
2802 goto error;
2803 }
2804
2805 /*
2806 * Clear the flag that indicates that a rotation was done while the
2807 * session was stopped.
2808 */
2809 session->rotated_after_last_stop = false;
2810
2811 if (session->rotate_timer_period) {
2812 int int_ret = timer_session_rotation_schedule_timer_start(
2813 session, session->rotate_timer_period);
2814
2815 if (int_ret < 0) {
2816 ERR("Failed to enable rotate timer");
2817 ret = LTTNG_ERR_UNK;
2818 goto error;
2819 }
2820 }
2821
2822 ret = LTTNG_OK;
2823
2824 error:
2825 if (ret == LTTNG_OK) {
2826 /* Flag this after a successful start. */
2827 session->has_been_started |= 1;
2828 } else {
2829 session->active = 0;
2830 /* Restore initial state on error. */
2831 session->rotated_after_last_stop =
2832 session_rotated_after_last_stop;
2833 session->cleared_after_last_stop =
2834 session_cleared_after_last_stop;
2835 }
2836 end:
2837 return ret;
2838 }
2839
2840 /*
2841 * Command LTTNG_STOP_TRACE processed by the client thread.
2842 */
2843 int cmd_stop_trace(struct ltt_session *session)
2844 {
2845 int ret;
2846 struct ltt_kernel_session *ksession;
2847 struct ltt_ust_session *usess;
2848
2849 assert(session);
2850
2851 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2852 /* Short cut */
2853 ksession = session->kernel_session;
2854 usess = session->ust_session;
2855
2856 /* Session is not active. Skip everythong and inform the client. */
2857 if (!session->active) {
2858 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2859 goto error;
2860 }
2861
2862 ret = stop_kernel_session(ksession);
2863 if (ret != LTTNG_OK) {
2864 goto error;
2865 }
2866
2867 if (usess && usess->active) {
2868 ret = ust_app_stop_trace_all(usess);
2869 if (ret < 0) {
2870 ret = LTTNG_ERR_UST_STOP_FAIL;
2871 goto error;
2872 }
2873 }
2874
2875 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2876 session->id);
2877 /* Flag inactive after a successful stop. */
2878 session->active = 0;
2879 ret = LTTNG_OK;
2880
2881 error:
2882 return ret;
2883 }
2884
2885 /*
2886 * Set the base_path of the session only if subdir of a control uris is set.
2887 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2888 */
2889 static int set_session_base_path_from_uris(struct ltt_session *session,
2890 size_t nb_uri,
2891 struct lttng_uri *uris)
2892 {
2893 int ret;
2894 size_t i;
2895
2896 for (i = 0; i < nb_uri; i++) {
2897 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2898 uris[i].subdir[0] == '\0') {
2899 /* Not interested in these URIs */
2900 continue;
2901 }
2902
2903 if (session->base_path != NULL) {
2904 free(session->base_path);
2905 session->base_path = NULL;
2906 }
2907
2908 /* Set session base_path */
2909 session->base_path = strdup(uris[i].subdir);
2910 if (!session->base_path) {
2911 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2912 uris[i].subdir, session->name);
2913 ret = LTTNG_ERR_NOMEM;
2914 goto error;
2915 }
2916 DBG2("Setting base path \"%s\" for session \"%s\"",
2917 session->base_path, session->name);
2918 }
2919 ret = LTTNG_OK;
2920 error:
2921 return ret;
2922 }
2923
2924 /*
2925 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2926 */
2927 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2928 struct lttng_uri *uris)
2929 {
2930 int ret, i;
2931 struct ltt_kernel_session *ksess = session->kernel_session;
2932 struct ltt_ust_session *usess = session->ust_session;
2933
2934 assert(session);
2935 assert(uris);
2936 assert(nb_uri > 0);
2937
2938 /* Can't set consumer URI if the session is active. */
2939 if (session->active) {
2940 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2941 goto error;
2942 }
2943
2944 /*
2945 * Set the session base path if any. This is done inside
2946 * cmd_set_consumer_uri to preserve backward compatibility of the
2947 * previous session creation api vs the session descriptor api.
2948 */
2949 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2950 if (ret != LTTNG_OK) {
2951 goto error;
2952 }
2953
2954 /* Set the "global" consumer URIs */
2955 for (i = 0; i < nb_uri; i++) {
2956 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2957 LTTNG_DOMAIN_NONE);
2958 if (ret != LTTNG_OK) {
2959 goto error;
2960 }
2961 }
2962
2963 /* Set UST session URIs */
2964 if (session->ust_session) {
2965 for (i = 0; i < nb_uri; i++) {
2966 ret = add_uri_to_consumer(session,
2967 session->ust_session->consumer,
2968 &uris[i], LTTNG_DOMAIN_UST);
2969 if (ret != LTTNG_OK) {
2970 goto error;
2971 }
2972 }
2973 }
2974
2975 /* Set kernel session URIs */
2976 if (session->kernel_session) {
2977 for (i = 0; i < nb_uri; i++) {
2978 ret = add_uri_to_consumer(session,
2979 session->kernel_session->consumer,
2980 &uris[i], LTTNG_DOMAIN_KERNEL);
2981 if (ret != LTTNG_OK) {
2982 goto error;
2983 }
2984 }
2985 }
2986
2987 /*
2988 * Make sure to set the session in output mode after we set URI since a
2989 * session can be created without URL (thus flagged in no output mode).
2990 */
2991 session->output_traces = 1;
2992 if (ksess) {
2993 ksess->output_traces = 1;
2994 }
2995
2996 if (usess) {
2997 usess->output_traces = 1;
2998 }
2999
3000 /* All good! */
3001 ret = LTTNG_OK;
3002
3003 error:
3004 return ret;
3005 }
3006
3007 static
3008 enum lttng_error_code set_session_output_from_descriptor(
3009 struct ltt_session *session,
3010 const struct lttng_session_descriptor *descriptor)
3011 {
3012 int ret;
3013 enum lttng_error_code ret_code = LTTNG_OK;
3014 enum lttng_session_descriptor_type session_type =
3015 lttng_session_descriptor_get_type(descriptor);
3016 enum lttng_session_descriptor_output_type output_type =
3017 lttng_session_descriptor_get_output_type(descriptor);
3018 struct lttng_uri uris[2] = {};
3019 size_t uri_count = 0;
3020
3021 switch (output_type) {
3022 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
3023 goto end;
3024 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
3025 lttng_session_descriptor_get_local_output_uri(descriptor,
3026 &uris[0]);
3027 uri_count = 1;
3028 break;
3029 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
3030 lttng_session_descriptor_get_network_output_uris(descriptor,
3031 &uris[0], &uris[1]);
3032 uri_count = 2;
3033 break;
3034 default:
3035 ret_code = LTTNG_ERR_INVALID;
3036 goto end;
3037 }
3038
3039 switch (session_type) {
3040 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3041 {
3042 struct snapshot_output *new_output = NULL;
3043
3044 new_output = snapshot_output_alloc();
3045 if (!new_output) {
3046 ret_code = LTTNG_ERR_NOMEM;
3047 goto end;
3048 }
3049
3050 ret = snapshot_output_init_with_uri(session,
3051 DEFAULT_SNAPSHOT_MAX_SIZE,
3052 NULL, uris, uri_count, session->consumer,
3053 new_output, &session->snapshot);
3054 if (ret < 0) {
3055 ret_code = (ret == -ENOMEM) ?
3056 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
3057 snapshot_output_destroy(new_output);
3058 goto end;
3059 }
3060 snapshot_add_output(&session->snapshot, new_output);
3061 break;
3062 }
3063 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
3064 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3065 {
3066 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
3067 break;
3068 }
3069 default:
3070 ret_code = LTTNG_ERR_INVALID;
3071 goto end;
3072 }
3073 end:
3074 return ret_code;
3075 }
3076
3077 static
3078 enum lttng_error_code cmd_create_session_from_descriptor(
3079 struct lttng_session_descriptor *descriptor,
3080 const lttng_sock_cred *creds,
3081 const char *home_path)
3082 {
3083 int ret;
3084 enum lttng_error_code ret_code;
3085 const char *session_name;
3086 struct ltt_session *new_session = NULL;
3087 enum lttng_session_descriptor_status descriptor_status;
3088
3089 session_lock_list();
3090 if (home_path) {
3091 if (*home_path != '/') {
3092 ERR("Home path provided by client is not absolute");
3093 ret_code = LTTNG_ERR_INVALID;
3094 goto end;
3095 }
3096 }
3097
3098 descriptor_status = lttng_session_descriptor_get_session_name(
3099 descriptor, &session_name);
3100 switch (descriptor_status) {
3101 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3102 break;
3103 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3104 session_name = NULL;
3105 break;
3106 default:
3107 ret_code = LTTNG_ERR_INVALID;
3108 goto end;
3109 }
3110
3111 ret_code = session_create(session_name, creds->uid, creds->gid,
3112 &new_session);
3113 if (ret_code != LTTNG_OK) {
3114 goto end;
3115 }
3116
3117 if (!session_name) {
3118 ret = lttng_session_descriptor_set_session_name(descriptor,
3119 new_session->name);
3120 if (ret) {
3121 ret_code = LTTNG_ERR_SESSION_FAIL;
3122 goto end;
3123 }
3124 }
3125
3126 if (!lttng_session_descriptor_is_output_destination_initialized(
3127 descriptor)) {
3128 /*
3129 * Only include the session's creation time in the output
3130 * destination if the name of the session itself was
3131 * not auto-generated.
3132 */
3133 ret_code = lttng_session_descriptor_set_default_output(
3134 descriptor,
3135 session_name ? &new_session->creation_time : NULL,
3136 home_path);
3137 if (ret_code != LTTNG_OK) {
3138 goto end;
3139 }
3140 } else {
3141 new_session->has_user_specified_directory =
3142 lttng_session_descriptor_has_output_directory(
3143 descriptor);
3144 }
3145
3146 switch (lttng_session_descriptor_get_type(descriptor)) {
3147 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3148 new_session->snapshot_mode = 1;
3149 break;
3150 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3151 new_session->live_timer =
3152 lttng_session_descriptor_live_get_timer_interval(
3153 descriptor);
3154 break;
3155 default:
3156 break;
3157 }
3158
3159 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3160 if (ret_code != LTTNG_OK) {
3161 goto end;
3162 }
3163 new_session->consumer->enabled = 1;
3164 ret_code = LTTNG_OK;
3165 end:
3166 /* Release reference provided by the session_create function. */
3167 session_put(new_session);
3168 if (ret_code != LTTNG_OK && new_session) {
3169 /* Release the global reference on error. */
3170 session_destroy(new_session);
3171 }
3172 session_unlock_list();
3173 return ret_code;
3174 }
3175
3176 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3177 struct lttng_session_descriptor **return_descriptor)
3178 {
3179 int ret;
3180 size_t payload_size;
3181 struct lttng_dynamic_buffer payload;
3182 struct lttng_buffer_view home_dir_view;
3183 struct lttng_buffer_view session_descriptor_view;
3184 struct lttng_session_descriptor *session_descriptor = NULL;
3185 enum lttng_error_code ret_code;
3186
3187 lttng_dynamic_buffer_init(&payload);
3188 if (cmd_ctx->lsm.u.create_session.home_dir_size >=
3189 LTTNG_PATH_MAX) {
3190 ret_code = LTTNG_ERR_INVALID;
3191 goto error;
3192 }
3193 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
3194 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3195 ret_code = LTTNG_ERR_INVALID;
3196 goto error;
3197 }
3198
3199 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3200 cmd_ctx->lsm.u.create_session.session_descriptor_size;
3201 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3202 if (ret) {
3203 ret_code = LTTNG_ERR_NOMEM;
3204 goto error;
3205 }
3206
3207 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3208 if (ret <= 0) {
3209 ERR("Reception of session descriptor failed, aborting.");
3210 ret_code = LTTNG_ERR_SESSION_FAIL;
3211 goto error;
3212 }
3213
3214 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3215 &payload,
3216 0,
3217 cmd_ctx->lsm.u.create_session.home_dir_size);
3218 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3219 &payload,
3220 cmd_ctx->lsm.u.create_session.home_dir_size,
3221 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3222
3223 ret = lttng_session_descriptor_create_from_buffer(
3224 &session_descriptor_view, &session_descriptor);
3225 if (ret < 0) {
3226 ERR("Failed to create session descriptor from payload of \"create session\" command");
3227 ret_code = LTTNG_ERR_INVALID;
3228 goto error;
3229 }
3230
3231 /*
3232 * Sets the descriptor's auto-generated properties (name, output) if
3233 * needed.
3234 */
3235 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3236 &cmd_ctx->creds,
3237 home_dir_view.size ? home_dir_view.data : NULL);
3238 if (ret_code != LTTNG_OK) {
3239 goto error;
3240 }
3241
3242 ret_code = LTTNG_OK;
3243 *return_descriptor