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