2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
14 #include <urcu/list.h>
15 #include <urcu/uatomic.h>
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>
50 #include "agent-thread.h"
52 #include "buffer-registry.h"
56 #include "event-notifier-error-accounting.h"
58 #include "health-sessiond.h"
59 #include "kernel-consumer.h"
61 #include "lttng-sessiond.h"
62 #include "lttng-syscall.h"
63 #include "notification-thread-commands.h"
64 #include "notification-thread.h"
66 #include "rotation-thread.h"
72 /* Sleep for 100ms between each check for the shm path's deletion. */
73 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
75 struct cmd_destroy_session_reply_context
{
77 bool implicit_rotation_on_destroy
;
79 * Indicates whether or not an error occurred while launching the
80 * destruction of a session.
82 enum lttng_error_code destruction_status
;
85 static enum lttng_error_code
wait_on_path(void *path
);
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.
91 * See comment in cmd_destroy_session() for the rationale.
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
= {
99 .data
= destroy_completion_handler
.shm_path
104 static struct cmd_completion_handler
*current_completion_handler
;
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
112 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
113 static uint64_t relayd_net_seq_idx
;
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
,
125 * Create a session path used by list_lttng_sessions for the case that the
126 * session consumer is on the network.
128 static int build_network_session_path(char *dst
, size_t size
,
129 struct ltt_session
*session
)
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
];
135 LTTNG_ASSERT(session
);
138 memset(tmp_urls
, 0, sizeof(tmp_urls
));
139 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
141 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
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
;
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
;
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
);
161 /* Build uuri URL string */
162 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
169 } else if (kuri
&& uuri
== NULL
) {
171 } else if (uuri
&& kuri
== NULL
) {
175 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
181 * Do we have a UST url set. If yes, this means we have both kernel and UST
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
);
189 if (kuri
|| (!kuri
&& !uuri
)) {
192 /* No kernel URI, use the UST port. */
195 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
203 * Get run-time attributes if the session has been started (discarded events,
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
)
212 if (!session
->has_been_started
) {
214 *discarded_events
= 0;
219 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
220 session
->kernel_session
->consumer
,
226 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
227 session
->kernel_session
->consumer
,
238 * Get run-time attributes if the session has been started (discarded events,
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
)
246 struct ltt_ust_session
*usess
;
248 if (!discarded_events
|| !lost_packets
) {
253 usess
= session
->ust_session
;
254 LTTNG_ASSERT(discarded_events
);
255 LTTNG_ASSERT(lost_packets
);
257 if (!usess
|| !session
->has_been_started
) {
258 *discarded_events
= 0;
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
,
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
,
280 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
281 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
283 ERR("Unsupported buffer type");
294 * Fill lttng_channel array of all channels.
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
)
301 struct ltt_kernel_channel
*kchan
;
303 DBG("Listing channels for session %s", session
->name
);
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
;
314 extended
= (struct lttng_channel_extended
*)
315 kchan
->channel
->attr
.extended
.ptr
;
317 ret
= get_kernel_runtime_stats(session
, kchan
,
318 &discarded_events
, &lost_packets
);
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
=
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;
335 case LTTNG_DOMAIN_UST
:
337 struct lttng_ht_iter iter
;
338 struct ltt_ust_channel
*uchan
;
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;
345 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
346 LTTNG_SYMBOL_NAME_LEN
)) {
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
;
361 * Map enum lttng_ust_output to enum lttng_event_output.
363 switch (uchan
->attr
.output
) {
364 case LTTNG_UST_ABI_MMAP
:
365 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
369 * LTTNG_UST_MMAP is the only supported UST
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
;
381 ret
= get_ust_runtime_stats(session
, uchan
,
382 &discarded_events
, &lost_packets
);
386 chan_exts
[i
].discarded_events
= discarded_events
;
387 chan_exts
[i
].lost_packets
= lost_packets
;
399 return -LTTNG_ERR_FATAL
;
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
)
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
;
418 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
419 sizeof(extended_header
));
424 if (filter_expression
) {
425 filter_len
= strlen(filter_expression
) + 1;
426 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
427 filter_expression
, filter_len
);
434 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
436 nb_exclusions
= exclusion
->count
;
438 ret
= lttng_dynamic_buffer_append(
439 &payload
->buffer
, &exclusion
->names
, len
);
445 if (probe_location
) {
446 const size_t size_before_probe
= payload
->buffer
.size
;
448 ret
= lttng_userspace_probe_location_serialize(probe_location
,
455 userspace_probe_location_len
=
456 payload
->buffer
.size
- size_before_probe
;
459 /* Set header fields */
460 p_extended_header
= (struct lttcomm_event_extended_header
*)
461 (payload
->buffer
.data
+ original_payload_size
);
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
;
474 * Create a list of agent domain events.
476 * Return number of events in list on success or else a negative value.
478 static int list_lttng_agent_events(struct agent
*agt
,
479 struct lttng_payload
*payload
)
481 int nb_events
= 0, ret
= 0;
482 const struct agent_event
*agent_event
;
483 struct lttng_ht_iter iter
;
487 DBG3("Listing agent events");
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
,
498 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
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
;
507 ret
= lttng_dynamic_buffer_append(
508 &payload
->buffer
, &event
, sizeof(event
));
510 ERR("Failed to append event to payload");
511 ret
= -LTTNG_ERR_NOMEM
;
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
,
524 ERR("Failed to append extended event info to payload");
525 ret
= -LTTNG_ERR_NOMEM
;
537 * Create a list of ust global domain events.
539 static int list_lttng_ust_global_events(char *channel_name
,
540 struct ltt_ust_domain_global
*ust_global
,
541 struct lttng_payload
*payload
)
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
;
550 DBG("Listing UST global events for channel %s", channel_name
);
554 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
555 node
= lttng_ht_iter_get_node_str(&iter
);
557 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
561 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
563 DBG3("Listing UST global events");
565 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
566 struct lttng_event event
= {};
568 if (uevent
->internal
) {
572 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
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
;
581 event
.enabled
= uevent
->enabled
;
583 switch (uevent
->attr
.instrumentation
) {
584 case LTTNG_UST_ABI_TRACEPOINT
:
585 event
.type
= LTTNG_EVENT_TRACEPOINT
;
587 case LTTNG_UST_ABI_PROBE
:
588 event
.type
= LTTNG_EVENT_PROBE
;
590 case LTTNG_UST_ABI_FUNCTION
:
591 event
.type
= LTTNG_EVENT_FUNCTION
;
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
;
600 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
601 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
603 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
604 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
608 if (uevent
->filter
) {
612 if (uevent
->exclusion
) {
616 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
618 ERR("Failed to append event to payload");
619 ret
= -LTTNG_ERR_NOMEM
;
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
);
631 ERR("Failed to append extended event info to payload");
632 ret
= -LTTNG_ERR_FATAL
;
644 * Fill lttng_event array of all kernel events in the channel.
646 static int list_lttng_kernel_events(char *channel_name
,
647 struct ltt_kernel_session
*kernel_session
,
648 struct lttng_payload
*payload
)
651 unsigned int nb_event
;
652 const struct ltt_kernel_event
*kevent
;
653 const struct ltt_kernel_channel
*kchan
;
655 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
657 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
661 nb_event
= kchan
->event_count
;
663 DBG("Listing events for channel %s", kchan
->channel
->name
);
665 /* Kernel channels */
666 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
667 struct lttng_event event
= {};
669 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
671 /* Internal error, invalid name. */
672 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
675 ret
= -LTTNG_ERR_UNK
;
679 event
.enabled
= kevent
->enabled
;
680 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
682 switch (kevent
->event
->instrumentation
) {
683 case LTTNG_KERNEL_ABI_TRACEPOINT
:
684 event
.type
= LTTNG_EVENT_TRACEPOINT
;
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
));
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
));
696 case LTTNG_KERNEL_ABI_UPROBE
:
697 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
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
));
704 case LTTNG_KERNEL_ABI_NOOP
:
705 event
.type
= LTTNG_EVENT_NOOP
;
707 case LTTNG_KERNEL_ABI_SYSCALL
:
708 event
.type
= LTTNG_EVENT_SYSCALL
;
710 case LTTNG_KERNEL_ABI_ALL
:
717 ret
= lttng_dynamic_buffer_append(
718 &payload
->buffer
, &event
, sizeof(event
));
720 ERR("Failed to append event to payload");
721 ret
= -LTTNG_ERR_NOMEM
;
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
);
731 DBG("Error appending extended info message");
732 ret
= -LTTNG_ERR_FATAL
;
744 * Add URI so the consumer output object. Set the correct path depending on the
745 * domain adding the default trace directory.
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
)
753 enum lttng_error_code ret_code
= LTTNG_OK
;
757 if (consumer
== NULL
) {
758 DBG("No consumer detected. Don't add URI. Stopping.");
759 ret_code
= LTTNG_ERR_NO_CONSUMER
;
764 case LTTNG_DOMAIN_KERNEL
:
765 ret
= lttng_strncpy(consumer
->domain_subdir
,
766 DEFAULT_KERNEL_TRACE_DIR
,
767 sizeof(consumer
->domain_subdir
));
769 case LTTNG_DOMAIN_UST
:
770 ret
= lttng_strncpy(consumer
->domain_subdir
,
771 DEFAULT_UST_TRACE_DIR
,
772 sizeof(consumer
->domain_subdir
));
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
780 memset(consumer
->domain_subdir
, 0,
781 sizeof(consumer
->domain_subdir
));
785 ERR("Failed to initialize consumer output domain subdirectory");
786 ret_code
= LTTNG_ERR_FATAL
;
790 switch (uri
->dtype
) {
793 DBG2("Setting network URI to consumer");
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
;
804 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
807 /* Set URI into consumer output object */
808 ret
= consumer_set_network_uri(session
, consumer
, uri
);
812 } else if (ret
== 1) {
814 * URI was the same in the consumer so we do not append the subdir
815 * again so to not duplicate output dir.
822 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
823 ret_code
= LTTNG_ERR_INVALID
;
826 DBG2("Setting trace directory path from URI to %s",
828 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
830 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
832 sizeof(consumer
->dst
.session_root_path
));
834 ret_code
= LTTNG_ERR_FATAL
;
837 consumer
->type
= CONSUMER_DST_LOCAL
;
847 * Init tracing by creating trace directory and sending fds kernel consumer.
849 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
852 struct lttng_ht_iter iter
;
853 struct consumer_socket
*socket
;
855 LTTNG_ASSERT(session
);
859 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
860 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
862 pthread_mutex_lock(socket
->lock
);
863 ret
= kernel_consumer_send_session(socket
, session
);
864 pthread_mutex_unlock(socket
->lock
);
866 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
878 * Create a socket to the relayd using the URI.
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.
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
)
888 enum lttng_error_code status
= LTTNG_OK
;
889 struct lttcomm_relayd_sock
*rsock
;
891 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
892 RELAYD_VERSION_COMM_MINOR
);
894 status
= LTTNG_ERR_FATAL
;
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.
904 ret
= relayd_connect(rsock
);
907 ERR("Unable to reach lttng-relayd");
908 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
912 /* Create socket for control stream. */
913 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
914 uint64_t result_flags
;
916 DBG3("Creating relayd stream socket from URI");
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
;
923 } else if (ret
< 0) {
924 ERR("Unable to reach lttng-relayd");
925 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
928 consumer
->relay_major_version
= rsock
->major
;
929 consumer
->relay_minor_version
= rsock
->minor
;
930 ret
= relayd_get_configuration(rsock
, 0,
933 ERR("Unable to get relayd configuration");
934 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
937 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
938 consumer
->relay_allows_clear
= true;
940 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
941 DBG3("Creating relayd data socket from URI");
943 /* Command is not valid */
944 ERR("Relayd invalid stream type: %d", uri
->stype
);
945 status
= LTTNG_ERR_INVALID
;
949 *relayd_sock
= rsock
;
954 /* The returned value is not useful since we are on an error path. */
955 (void) relayd_close(rsock
);
963 * Connect to the relayd using URI and send the socket to the right consumer.
965 * The consumer socket lock must be held by the caller.
967 * Returns LTTNG_OK on success or an LTTng error code on failure.
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
)
981 struct lttcomm_relayd_sock
*rsock
= NULL
;
982 enum lttng_error_code status
;
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
;
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
);
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.
999 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1000 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
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
);
1010 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
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;
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.
1027 if (status
!= LTTNG_OK
) {
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.
1033 consumer
->enabled
= 0;
1035 (void) relayd_close(rsock
);
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
1047 * The consumer socket lock must be held by the caller.
1049 * Returns LTTNG_OK, or an LTTng error code on failure.
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
)
1059 enum lttng_error_code status
= LTTNG_OK
;
1061 LTTNG_ASSERT(consumer
);
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
) {
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
) {
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
1097 int cmd_setup_relayd(struct ltt_session
*session
)
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
= {};
1106 LTTNG_ASSERT(session
);
1108 usess
= session
->ust_session
;
1109 ksess
= session
->kernel_session
;
1111 DBG("Setting relayd for session %s", session
->name
);
1114 if (session
->current_trace_chunk
) {
1115 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1116 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1118 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1119 current_chunk_id
.is_set
= true;
1121 ERR("Failed to get current trace chunk id");
1122 ret
= LTTNG_ERR_UNK
;
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
,
1137 session
->live_timer
,
1138 current_chunk_id
.is_set
? ¤t_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
) {
1145 /* Session is now ready for network streaming. */
1146 session
->net_handle
= 1;
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
;
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
,
1165 session
->live_timer
,
1166 current_chunk_id
.is_set
? ¤t_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
) {
1173 /* Session is now ready for network streaming. */
1174 session
->net_handle
= 1;
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
;
1190 * Start a kernel session by opening all necessary streams.
1192 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1195 struct ltt_kernel_channel
*kchan
;
1197 /* Open kernel metadata */
1198 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1199 ret
= kernel_open_metadata(ksess
);
1201 ret
= LTTNG_ERR_KERN_META_FAIL
;
1206 /* Open kernel metadata stream */
1207 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1208 ret
= kernel_open_metadata_stream(ksess
);
1210 ERR("Kernel create metadata stream failed");
1211 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
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
);
1221 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1224 /* Update the stream global counter */
1225 ksess
->stream_count_global
+= ret
;
1229 /* Setup kernel consumer socket and send fds to it */
1230 ret
= init_kernel_tracing(ksess
);
1232 ret
= LTTNG_ERR_KERN_START_FAIL
;
1236 /* This start the kernel tracing */
1237 ret
= kernel_start_session(ksess
);
1239 ret
= LTTNG_ERR_KERN_START_FAIL
;
1243 /* Quiescent wait after starting trace */
1244 kernel_wait_quiescent();
1254 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1256 struct ltt_kernel_channel
*kchan
;
1257 bool error_occurred
= false;
1260 if (!ksess
|| !ksess
->active
) {
1263 DBG("Stopping kernel tracing");
1265 ret
= kernel_stop_session(ksess
);
1267 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1271 kernel_wait_quiescent();
1273 /* Flush metadata after stopping (if exists) */
1274 if (ksess
->metadata_stream_fd
>= 0) {
1275 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1277 ERR("Kernel metadata flush failed");
1278 error_occurred
= true;
1282 /* Flush all buffers after stopping */
1283 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1284 ret
= kernel_flush_buffer(kchan
);
1286 ERR("Kernel flush buffer error");
1287 error_occurred
= true;
1292 if (error_occurred
) {
1293 ret
= LTTNG_ERR_UNK
;
1302 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1304 int cmd_disable_channel(struct ltt_session
*session
,
1305 enum lttng_domain_type domain
, char *channel_name
)
1308 struct ltt_ust_session
*usess
;
1310 usess
= session
->ust_session
;
1315 case LTTNG_DOMAIN_KERNEL
:
1317 ret
= channel_kernel_disable(session
->kernel_session
,
1319 if (ret
!= LTTNG_OK
) {
1323 kernel_wait_quiescent();
1326 case LTTNG_DOMAIN_UST
:
1328 struct ltt_ust_channel
*uchan
;
1329 struct lttng_ht
*chan_ht
;
1331 chan_ht
= usess
->domain_global
.channels
;
1333 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1334 if (uchan
== NULL
) {
1335 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1339 ret
= channel_ust_disable(usess
, uchan
);
1340 if (ret
!= LTTNG_OK
) {
1346 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1358 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1360 * The wpipe arguments is used as a notifier for the kernel thread.
1362 int cmd_enable_channel(struct ltt_session
*session
,
1363 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1366 struct ltt_ust_session
*usess
= session
->ust_session
;
1367 struct lttng_ht
*chan_ht
;
1369 struct lttng_channel attr
;
1371 LTTNG_ASSERT(session
);
1372 LTTNG_ASSERT(_attr
);
1373 LTTNG_ASSERT(domain
);
1376 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1378 /* Validate channel name */
1379 if (attr
.name
[0] == '.' ||
1380 memchr(attr
.name
, '/', len
) != NULL
) {
1381 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1385 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1390 * If the session is a live session, remove the switch timer, the
1391 * live timer does the same thing but sends also synchronisation
1392 * beacons for inactive streams.
1394 if (session
->live_timer
> 0) {
1395 attr
.attr
.live_timer_interval
= session
->live_timer
;
1396 attr
.attr
.switch_timer_interval
= 0;
1399 /* Check for feature support */
1400 switch (domain
->type
) {
1401 case LTTNG_DOMAIN_KERNEL
:
1403 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1404 /* Sampling position of buffer is not supported */
1405 WARN("Kernel tracer does not support buffer monitoring. "
1406 "Setting the monitor interval timer to 0 "
1407 "(disabled) for channel '%s' of session '%s'",
1408 attr
.name
, session
->name
);
1409 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1413 case LTTNG_DOMAIN_UST
:
1415 case LTTNG_DOMAIN_JUL
:
1416 case LTTNG_DOMAIN_LOG4J
:
1417 case LTTNG_DOMAIN_PYTHON
:
1418 if (!agent_tracing_is_enabled()) {
1419 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1420 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1425 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1429 switch (domain
->type
) {
1430 case LTTNG_DOMAIN_KERNEL
:
1432 struct ltt_kernel_channel
*kchan
;
1434 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1435 session
->kernel_session
);
1436 if (kchan
== NULL
) {
1438 * Don't try to create a channel if the session has been started at
1439 * some point in time before. The tracer does not allow it.
1441 if (session
->has_been_started
) {
1442 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
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
;
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;
1456 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1459 if (ret
!= LTTNG_OK
) {
1463 kernel_wait_quiescent();
1466 case LTTNG_DOMAIN_UST
:
1467 case LTTNG_DOMAIN_JUL
:
1468 case LTTNG_DOMAIN_LOG4J
:
1469 case LTTNG_DOMAIN_PYTHON
:
1471 struct ltt_ust_channel
*uchan
;
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
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
;
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
;
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
;
1501 chan_ht
= usess
->domain_global
.channels
;
1503 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1504 if (uchan
== NULL
) {
1506 * Don't try to create a channel if the session has been started at
1507 * some point in time before. The tracer does not allow it.
1509 if (session
->has_been_started
) {
1510 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1514 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1515 if (attr
.name
[0] != '\0') {
1516 usess
->has_non_default_channel
= 1;
1519 ret
= channel_ust_enable(usess
, uchan
);
1524 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1528 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1529 session
->has_non_mmap_channel
= true;
1537 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1538 struct ltt_session
*session
,
1539 enum lttng_domain_type domain
,
1540 enum lttng_process_attr process_attr
,
1541 enum lttng_tracking_policy
*policy
)
1543 enum lttng_error_code ret_code
= LTTNG_OK
;
1544 const struct process_attr_tracker
*tracker
;
1547 case LTTNG_DOMAIN_KERNEL
:
1548 if (!session
->kernel_session
) {
1549 ret_code
= LTTNG_ERR_INVALID
;
1552 tracker
= kernel_get_process_attr_tracker(
1553 session
->kernel_session
, process_attr
);
1555 case LTTNG_DOMAIN_UST
:
1556 if (!session
->ust_session
) {
1557 ret_code
= LTTNG_ERR_INVALID
;
1560 tracker
= trace_ust_get_process_attr_tracker(
1561 session
->ust_session
, process_attr
);
1564 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1568 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1570 ret_code
= LTTNG_ERR_INVALID
;
1576 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1577 struct ltt_session
*session
,
1578 enum lttng_domain_type domain
,
1579 enum lttng_process_attr process_attr
,
1580 enum lttng_tracking_policy policy
)
1582 enum lttng_error_code ret_code
= LTTNG_OK
;
1585 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1586 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1587 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1590 ret_code
= LTTNG_ERR_INVALID
;
1595 case LTTNG_DOMAIN_KERNEL
:
1596 if (!session
->kernel_session
) {
1597 ret_code
= LTTNG_ERR_INVALID
;
1600 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1601 session
->kernel_session
, process_attr
, policy
);
1603 case LTTNG_DOMAIN_UST
:
1604 if (!session
->ust_session
) {
1605 ret_code
= LTTNG_ERR_INVALID
;
1608 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1609 session
->ust_session
, process_attr
, policy
);
1612 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1619 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1620 struct ltt_session
*session
,
1621 enum lttng_domain_type domain
,
1622 enum lttng_process_attr process_attr
,
1623 const struct process_attr_value
*value
)
1625 enum lttng_error_code ret_code
= LTTNG_OK
;
1628 case LTTNG_DOMAIN_KERNEL
:
1629 if (!session
->kernel_session
) {
1630 ret_code
= LTTNG_ERR_INVALID
;
1633 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1634 session
->kernel_session
, process_attr
, value
);
1636 case LTTNG_DOMAIN_UST
:
1637 if (!session
->ust_session
) {
1638 ret_code
= LTTNG_ERR_INVALID
;
1641 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1642 session
->ust_session
, process_attr
, value
);
1645 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1652 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1653 struct ltt_session
*session
,
1654 enum lttng_domain_type domain
,
1655 enum lttng_process_attr process_attr
,
1656 const struct process_attr_value
*value
)
1658 enum lttng_error_code ret_code
= LTTNG_OK
;
1661 case LTTNG_DOMAIN_KERNEL
:
1662 if (!session
->kernel_session
) {
1663 ret_code
= LTTNG_ERR_INVALID
;
1666 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1667 session
->kernel_session
, process_attr
, value
);
1669 case LTTNG_DOMAIN_UST
:
1670 if (!session
->ust_session
) {
1671 ret_code
= LTTNG_ERR_INVALID
;
1674 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1675 session
->ust_session
, process_attr
, value
);
1678 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1685 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1686 struct ltt_session
*session
,
1687 enum lttng_domain_type domain
,
1688 enum lttng_process_attr process_attr
,
1689 struct lttng_process_attr_values
**values
)
1691 enum lttng_error_code ret_code
= LTTNG_OK
;
1692 const struct process_attr_tracker
*tracker
;
1693 enum process_attr_tracker_status status
;
1696 case LTTNG_DOMAIN_KERNEL
:
1697 if (!session
->kernel_session
) {
1698 ret_code
= LTTNG_ERR_INVALID
;
1701 tracker
= kernel_get_process_attr_tracker(
1702 session
->kernel_session
, process_attr
);
1704 case LTTNG_DOMAIN_UST
:
1705 if (!session
->ust_session
) {
1706 ret_code
= LTTNG_ERR_INVALID
;
1709 tracker
= trace_ust_get_process_attr_tracker(
1710 session
->ust_session
, process_attr
);
1713 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1718 ret_code
= LTTNG_ERR_INVALID
;
1722 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1724 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1725 ret_code
= LTTNG_OK
;
1727 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1728 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1730 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1731 ret_code
= LTTNG_ERR_NOMEM
;
1734 ret_code
= LTTNG_ERR_UNK
;
1743 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1745 int cmd_disable_event(struct ltt_session
*session
,
1746 enum lttng_domain_type domain
, const char *channel_name
,
1747 const struct lttng_event
*event
)
1750 const char *event_name
;
1752 DBG("Disable event command for event \'%s\'", event
->name
);
1754 event_name
= event
->name
;
1756 /* Error out on unhandled search criteria */
1757 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1758 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1759 ret
= LTTNG_ERR_UNK
;
1766 case LTTNG_DOMAIN_KERNEL
:
1768 struct ltt_kernel_channel
*kchan
;
1769 struct ltt_kernel_session
*ksess
;
1771 ksess
= session
->kernel_session
;
1774 * If a non-default channel has been created in the
1775 * session, explicitely require that -c chan_name needs
1778 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1779 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1783 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1784 if (kchan
== NULL
) {
1785 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1789 switch (event
->type
) {
1790 case LTTNG_EVENT_ALL
:
1791 case LTTNG_EVENT_TRACEPOINT
:
1792 case LTTNG_EVENT_SYSCALL
:
1793 case LTTNG_EVENT_PROBE
:
1794 case LTTNG_EVENT_FUNCTION
:
1795 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1796 if (event_name
[0] == '\0') {
1797 ret
= event_kernel_disable_event(kchan
,
1800 ret
= event_kernel_disable_event(kchan
,
1801 event_name
, event
->type
);
1803 if (ret
!= LTTNG_OK
) {
1808 ret
= LTTNG_ERR_UNK
;
1812 kernel_wait_quiescent();
1815 case LTTNG_DOMAIN_UST
:
1817 struct ltt_ust_channel
*uchan
;
1818 struct ltt_ust_session
*usess
;
1820 usess
= session
->ust_session
;
1822 if (validate_ust_event_name(event_name
)) {
1823 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1828 * If a non-default channel has been created in the
1829 * session, explicitly require that -c chan_name needs
1832 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1833 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1837 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1839 if (uchan
== NULL
) {
1840 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1844 switch (event
->type
) {
1845 case LTTNG_EVENT_ALL
:
1847 * An empty event name means that everything
1848 * should be disabled.
1850 if (event
->name
[0] == '\0') {
1851 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1853 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1856 if (ret
!= LTTNG_OK
) {
1861 ret
= LTTNG_ERR_UNK
;
1865 DBG3("Disable UST event %s in channel %s completed", event_name
,
1869 case LTTNG_DOMAIN_LOG4J
:
1870 case LTTNG_DOMAIN_JUL
:
1871 case LTTNG_DOMAIN_PYTHON
:
1874 struct ltt_ust_session
*usess
= session
->ust_session
;
1876 LTTNG_ASSERT(usess
);
1878 switch (event
->type
) {
1879 case LTTNG_EVENT_ALL
:
1882 ret
= LTTNG_ERR_UNK
;
1886 agt
= trace_ust_find_agent(usess
, domain
);
1888 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1892 * An empty event name means that everything
1893 * should be disabled.
1895 if (event
->name
[0] == '\0') {
1896 ret
= event_agent_disable_all(usess
, agt
);
1898 ret
= event_agent_disable(usess
, agt
, event_name
);
1900 if (ret
!= LTTNG_OK
) {
1907 ret
= LTTNG_ERR_UND
;
1920 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1922 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1923 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1925 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1926 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1929 * Don't try to add a context if the session has been started at
1930 * some point in time before. The tracer does not allow it and would
1931 * result in a corrupted trace.
1933 if (session
->has_been_started
) {
1934 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1938 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1939 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1940 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1944 case LTTNG_DOMAIN_KERNEL
:
1945 LTTNG_ASSERT(session
->kernel_session
);
1947 if (session
->kernel_session
->channel_count
== 0) {
1948 /* Create default channel */
1949 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1950 if (ret
!= LTTNG_OK
) {
1953 chan_kern_created
= 1;
1955 /* Add kernel context to kernel tracer */
1956 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1957 if (ret
!= LTTNG_OK
) {
1961 case LTTNG_DOMAIN_JUL
:
1962 case LTTNG_DOMAIN_LOG4J
:
1965 * Validate channel name.
1966 * If no channel name is given and the domain is JUL or LOG4J,
1967 * set it to the appropriate domain-specific channel name. If
1968 * a name is provided but does not match the expexted channel
1969 * name, return an error.
1971 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1972 strcmp(channel_name
,
1973 DEFAULT_JUL_CHANNEL_NAME
)) {
1974 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1976 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1977 strcmp(channel_name
,
1978 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1979 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1982 /* break is _not_ missing here. */
1984 case LTTNG_DOMAIN_UST
:
1986 struct ltt_ust_session
*usess
= session
->ust_session
;
1987 unsigned int chan_count
;
1989 LTTNG_ASSERT(usess
);
1991 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1992 if (chan_count
== 0) {
1993 struct lttng_channel
*attr
;
1994 /* Create default channel */
1995 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1997 ret
= LTTNG_ERR_FATAL
;
2001 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2002 if (ret
!= LTTNG_OK
) {
2006 channel_attr_destroy(attr
);
2007 chan_ust_created
= 1;
2010 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2011 free(app_ctx_provider_name
);
2013 app_ctx_name
= NULL
;
2014 app_ctx_provider_name
= NULL
;
2015 if (ret
!= LTTNG_OK
) {
2021 ret
= LTTNG_ERR_UND
;
2029 if (chan_kern_created
) {
2030 struct ltt_kernel_channel
*kchan
=
2031 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2032 session
->kernel_session
);
2033 /* Created previously, this should NOT fail. */
2034 LTTNG_ASSERT(kchan
);
2035 kernel_destroy_channel(kchan
);
2038 if (chan_ust_created
) {
2039 struct ltt_ust_channel
*uchan
=
2040 trace_ust_find_channel_by_name(
2041 session
->ust_session
->domain_global
.channels
,
2042 DEFAULT_CHANNEL_NAME
);
2043 /* Created previously, this should NOT fail. */
2044 LTTNG_ASSERT(uchan
);
2045 /* Remove from the channel list of the session. */
2046 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2048 trace_ust_destroy_channel(uchan
);
2051 free(app_ctx_provider_name
);
2056 static inline bool name_starts_with(const char *name
, const char *prefix
)
2058 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2060 return !strncmp(name
, prefix
, max_cmp_len
);
2063 /* Perform userspace-specific event name validation */
2064 static int validate_ust_event_name(const char *name
)
2074 * Check name against all internal UST event component namespaces used
2077 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2078 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2079 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2088 * Internal version of cmd_enable_event() with a supplemental
2089 * "internal_event" flag which is used to enable internal events which should
2090 * be hidden from clients. Such events are used in the agent implementation to
2091 * enable the events through which all "agent" events are funeled.
2093 static int _cmd_enable_event(struct ltt_session
*session
,
2094 const struct lttng_domain
*domain
,
2095 char *channel_name
, struct lttng_event
*event
,
2096 char *filter_expression
,
2097 struct lttng_bytecode
*filter
,
2098 struct lttng_event_exclusion
*exclusion
,
2099 int wpipe
, bool internal_event
)
2101 int ret
= 0, channel_created
= 0;
2102 struct lttng_channel
*attr
= NULL
;
2104 LTTNG_ASSERT(session
);
2105 LTTNG_ASSERT(event
);
2106 LTTNG_ASSERT(channel_name
);
2108 /* If we have a filter, we must have its filter expression */
2109 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2111 /* Normalize event name as a globbing pattern */
2112 strutils_normalize_star_glob_pattern(event
->name
);
2114 /* Normalize exclusion names as globbing patterns */
2118 for (i
= 0; i
< exclusion
->count
; i
++) {
2119 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2121 strutils_normalize_star_glob_pattern(name
);
2125 DBG("Enable event command for event \'%s\'", event
->name
);
2129 switch (domain
->type
) {
2130 case LTTNG_DOMAIN_KERNEL
:
2132 struct ltt_kernel_channel
*kchan
;
2135 * If a non-default channel has been created in the
2136 * session, explicitely require that -c chan_name needs
2139 if (session
->kernel_session
->has_non_default_channel
2140 && channel_name
[0] == '\0') {
2141 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2145 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2146 session
->kernel_session
);
2147 if (kchan
== NULL
) {
2148 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2149 LTTNG_BUFFER_GLOBAL
);
2151 ret
= LTTNG_ERR_FATAL
;
2154 if (lttng_strncpy(attr
->name
, channel_name
,
2155 sizeof(attr
->name
))) {
2156 ret
= LTTNG_ERR_INVALID
;
2160 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2161 if (ret
!= LTTNG_OK
) {
2164 channel_created
= 1;
2167 /* Get the newly created kernel channel pointer */
2168 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2169 session
->kernel_session
);
2170 if (kchan
== NULL
) {
2171 /* This sould not happen... */
2172 ret
= LTTNG_ERR_FATAL
;
2176 switch (event
->type
) {
2177 case LTTNG_EVENT_ALL
:
2179 char *filter_expression_a
= NULL
;
2180 struct lttng_bytecode
*filter_a
= NULL
;
2183 * We need to duplicate filter_expression and filter,
2184 * because ownership is passed to first enable
2187 if (filter_expression
) {
2188 filter_expression_a
= strdup(filter_expression
);
2189 if (!filter_expression_a
) {
2190 ret
= LTTNG_ERR_FATAL
;
2195 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2197 free(filter_expression_a
);
2198 ret
= LTTNG_ERR_FATAL
;
2201 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2203 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2204 ret
= event_kernel_enable_event(kchan
, event
,
2205 filter_expression
, filter
);
2206 /* We have passed ownership */
2207 filter_expression
= NULL
;
2209 if (ret
!= LTTNG_OK
) {
2210 if (channel_created
) {
2211 /* Let's not leak a useless channel. */
2212 kernel_destroy_channel(kchan
);
2214 free(filter_expression_a
);
2218 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2219 ret
= event_kernel_enable_event(kchan
, event
,
2220 filter_expression_a
, filter_a
);
2221 /* We have passed ownership */
2222 filter_expression_a
= NULL
;
2224 if (ret
!= LTTNG_OK
) {
2229 case LTTNG_EVENT_PROBE
:
2230 case LTTNG_EVENT_USERSPACE_PROBE
:
2231 case LTTNG_EVENT_FUNCTION
:
2232 case LTTNG_EVENT_FUNCTION_ENTRY
:
2233 case LTTNG_EVENT_TRACEPOINT
:
2234 ret
= event_kernel_enable_event(kchan
, event
,
2235 filter_expression
, filter
);
2236 /* We have passed ownership */
2237 filter_expression
= NULL
;
2239 if (ret
!= LTTNG_OK
) {
2240 if (channel_created
) {
2241 /* Let's not leak a useless channel. */
2242 kernel_destroy_channel(kchan
);
2247 case LTTNG_EVENT_SYSCALL
:
2248 ret
= event_kernel_enable_event(kchan
, event
,
2249 filter_expression
, filter
);
2250 /* We have passed ownership */
2251 filter_expression
= NULL
;
2253 if (ret
!= LTTNG_OK
) {
2258 ret
= LTTNG_ERR_UNK
;
2262 kernel_wait_quiescent();
2265 case LTTNG_DOMAIN_UST
:
2267 struct ltt_ust_channel
*uchan
;
2268 struct ltt_ust_session
*usess
= session
->ust_session
;
2270 LTTNG_ASSERT(usess
);
2273 * If a non-default channel has been created in the
2274 * session, explicitely require that -c chan_name needs
2277 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2278 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2282 /* Get channel from global UST domain */
2283 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2285 if (uchan
== NULL
) {
2286 /* Create default channel */
2287 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2288 usess
->buffer_type
);
2290 ret
= LTTNG_ERR_FATAL
;
2293 if (lttng_strncpy(attr
->name
, channel_name
,
2294 sizeof(attr
->name
))) {
2295 ret
= LTTNG_ERR_INVALID
;
2299 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2300 if (ret
!= LTTNG_OK
) {
2304 /* Get the newly created channel reference back */
2305 uchan
= trace_ust_find_channel_by_name(
2306 usess
->domain_global
.channels
, channel_name
);
2307 LTTNG_ASSERT(uchan
);
2310 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2312 * Don't allow users to add UST events to channels which
2313 * are assigned to a userspace subdomain (JUL, Log4J,
2316 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2320 if (!internal_event
) {
2322 * Ensure the event name is not reserved for internal
2325 ret
= validate_ust_event_name(event
->name
);
2327 WARN("Userspace event name %s failed validation.",
2329 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2334 /* At this point, the session and channel exist on the tracer */
2335 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2336 filter_expression
, filter
, exclusion
,
2338 /* We have passed ownership */
2339 filter_expression
= NULL
;
2342 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2343 goto already_enabled
;
2344 } else if (ret
!= LTTNG_OK
) {
2349 case LTTNG_DOMAIN_LOG4J
:
2350 case LTTNG_DOMAIN_JUL
:
2351 case LTTNG_DOMAIN_PYTHON
:
2353 const char *default_event_name
, *default_chan_name
;
2355 struct lttng_event uevent
;
2356 struct lttng_domain tmp_dom
;
2357 struct ltt_ust_session
*usess
= session
->ust_session
;
2359 LTTNG_ASSERT(usess
);
2361 if (!agent_tracing_is_enabled()) {
2362 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2363 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2367 agt
= trace_ust_find_agent(usess
, domain
->type
);
2369 agt
= agent_create(domain
->type
);
2371 ret
= LTTNG_ERR_NOMEM
;
2374 agent_add(agt
, usess
->agents
);
2377 /* Create the default tracepoint. */
2378 memset(&uevent
, 0, sizeof(uevent
));
2379 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2380 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2381 default_event_name
= event_get_default_agent_ust_name(
2383 if (!default_event_name
) {
2384 ret
= LTTNG_ERR_FATAL
;
2387 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2388 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2391 * The domain type is changed because we are about to enable the
2392 * default channel and event for the JUL domain that are hardcoded.
2393 * This happens in the UST domain.
2395 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2396 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2398 switch (domain
->type
) {
2399 case LTTNG_DOMAIN_LOG4J
:
2400 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2402 case LTTNG_DOMAIN_JUL
:
2403 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2405 case LTTNG_DOMAIN_PYTHON
:
2406 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2409 /* The switch/case we are in makes this impossible */
2414 char *filter_expression_copy
= NULL
;
2415 struct lttng_bytecode
*filter_copy
= NULL
;
2418 const size_t filter_size
= sizeof(
2419 struct lttng_bytecode
)
2422 filter_copy
= zmalloc(filter_size
);
2424 ret
= LTTNG_ERR_NOMEM
;
2427 memcpy(filter_copy
, filter
, filter_size
);
2429 filter_expression_copy
=
2430 strdup(filter_expression
);
2431 if (!filter_expression
) {
2432 ret
= LTTNG_ERR_NOMEM
;
2435 if (!filter_expression_copy
|| !filter_copy
) {
2436 free(filter_expression_copy
);
2442 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2443 (char *) default_chan_name
,
2444 &uevent
, filter_expression_copy
,
2445 filter_copy
, NULL
, wpipe
);
2448 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2449 goto already_enabled
;
2450 } else if (ret
!= LTTNG_OK
) {
2454 /* The wild card * means that everything should be enabled. */
2455 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2456 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2459 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2463 filter_expression
= NULL
;
2464 if (ret
!= LTTNG_OK
) {
2471 ret
= LTTNG_ERR_UND
;
2479 free(filter_expression
);
2482 channel_attr_destroy(attr
);
2488 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2489 * We own filter, exclusion, and filter_expression.
2491 int cmd_enable_event(struct ltt_session
*session
,
2492 const struct lttng_domain
*domain
,
2493 char *channel_name
, struct lttng_event
*event
,
2494 char *filter_expression
,
2495 struct lttng_bytecode
*filter
,
2496 struct lttng_event_exclusion
*exclusion
,
2499 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2500 filter_expression
, filter
, exclusion
, wpipe
, false);
2504 * Enable an event which is internal to LTTng. An internal should
2505 * never be made visible to clients and are immune to checks such as
2508 static int cmd_enable_event_internal(struct ltt_session
*session
,
2509 const struct lttng_domain
*domain
,
2510 char *channel_name
, struct lttng_event
*event
,
2511 char *filter_expression
,
2512 struct lttng_bytecode
*filter
,
2513 struct lttng_event_exclusion
*exclusion
,
2516 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2517 filter_expression
, filter
, exclusion
, wpipe
, true);
2521 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2523 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2524 struct lttng_event
**events
)
2527 ssize_t nb_events
= 0;
2530 case LTTNG_DOMAIN_KERNEL
:
2531 nb_events
= kernel_list_events(events
);
2532 if (nb_events
< 0) {
2533 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2537 case LTTNG_DOMAIN_UST
:
2538 nb_events
= ust_app_list_events(events
);
2539 if (nb_events
< 0) {
2540 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2544 case LTTNG_DOMAIN_LOG4J
:
2545 case LTTNG_DOMAIN_JUL
:
2546 case LTTNG_DOMAIN_PYTHON
:
2547 nb_events
= agent_list_events(events
, domain
);
2548 if (nb_events
< 0) {
2549 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2554 ret
= LTTNG_ERR_UND
;
2561 /* Return negative value to differentiate return code */
2566 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2568 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2569 struct lttng_event_field
**fields
)
2572 ssize_t nb_fields
= 0;
2575 case LTTNG_DOMAIN_UST
:
2576 nb_fields
= ust_app_list_event_fields(fields
);
2577 if (nb_fields
< 0) {
2578 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2582 case LTTNG_DOMAIN_KERNEL
:
2583 default: /* fall-through */
2584 ret
= LTTNG_ERR_UND
;
2591 /* Return negative value to differentiate return code */
2595 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2597 return syscall_table_list(events
);
2601 * Command LTTNG_START_TRACE processed by the client thread.
2603 * Called with session mutex held.
2605 int cmd_start_trace(struct ltt_session
*session
)
2607 enum lttng_error_code ret
;
2608 unsigned long nb_chan
= 0;
2609 struct ltt_kernel_session
*ksession
;
2610 struct ltt_ust_session
*usess
;
2611 const bool session_rotated_after_last_stop
=
2612 session
->rotated_after_last_stop
;
2613 const bool session_cleared_after_last_stop
=
2614 session
->cleared_after_last_stop
;
2616 LTTNG_ASSERT(session
);
2618 /* Ease our life a bit ;) */
2619 ksession
= session
->kernel_session
;
2620 usess
= session
->ust_session
;
2622 /* Is the session already started? */
2623 if (session
->active
) {
2624 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2625 /* Perform nothing */
2629 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2630 !session
->current_trace_chunk
) {
2632 * A rotation was launched while the session was stopped and
2633 * it has not been completed yet. It is not possible to start
2634 * the session since starting the session here would require a
2635 * rotation from "NULL" to a new trace chunk. That rotation
2636 * would overlap with the ongoing rotation, which is not
2639 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2641 ret
= LTTNG_ERR_ROTATION_PENDING
;
2646 * Starting a session without channel is useless since after that it's not
2647 * possible to enable channel thus inform the client.
2649 if (usess
&& usess
->domain_global
.channels
) {
2650 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2653 nb_chan
+= ksession
->channel_count
;
2656 ret
= LTTNG_ERR_NO_CHANNEL
;
2660 session
->active
= 1;
2661 session
->rotated_after_last_stop
= false;
2662 session
->cleared_after_last_stop
= false;
2663 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2664 if (!session
->has_been_started
) {
2665 struct lttng_trace_chunk
*trace_chunk
;
2667 DBG("Creating initial trace chunk of session \"%s\"",
2669 trace_chunk
= session_create_new_trace_chunk(
2670 session
, NULL
, NULL
, NULL
);
2672 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2675 LTTNG_ASSERT(!session
->current_trace_chunk
);
2676 ret
= session_set_trace_chunk(session
, trace_chunk
,
2678 lttng_trace_chunk_put(trace_chunk
);
2680 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2684 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2687 * Rotate existing streams into the new chunk.
2688 * This is a "quiet" rotation has no client has
2689 * explicitly requested this operation.
2691 * There is also no need to wait for the rotation
2692 * to complete as it will happen immediately. No data
2693 * was produced as the session was stopped, so the
2694 * rotation should happen on reception of the command.
2696 ret
= cmd_rotate_session(session
, NULL
, true,
2697 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2698 if (ret
!= LTTNG_OK
) {
2704 /* Kernel tracing */
2705 if (ksession
!= NULL
) {
2706 DBG("Start kernel tracing session %s", session
->name
);
2707 ret
= start_kernel_session(ksession
);
2708 if (ret
!= LTTNG_OK
) {
2713 /* Flag session that trace should start automatically */
2715 int int_ret
= ust_app_start_trace_all(usess
);
2718 ret
= LTTNG_ERR_UST_START_FAIL
;
2724 * Open a packet in every stream of the session to ensure that viewers
2725 * can correctly identify the boundaries of the periods during which
2726 * tracing was active for this session.
2728 ret
= session_open_packets(session
);
2729 if (ret
!= LTTNG_OK
) {
2734 * Clear the flag that indicates that a rotation was done while the
2735 * session was stopped.
2737 session
->rotated_after_last_stop
= false;
2739 if (session
->rotate_timer_period
) {
2740 int int_ret
= timer_session_rotation_schedule_timer_start(
2741 session
, session
->rotate_timer_period
);
2744 ERR("Failed to enable rotate timer");
2745 ret
= LTTNG_ERR_UNK
;
2753 if (ret
== LTTNG_OK
) {
2754 /* Flag this after a successful start. */
2755 session
->has_been_started
|= 1;
2757 session
->active
= 0;
2758 /* Restore initial state on error. */
2759 session
->rotated_after_last_stop
=
2760 session_rotated_after_last_stop
;
2761 session
->cleared_after_last_stop
=
2762 session_cleared_after_last_stop
;
2769 * Command LTTNG_STOP_TRACE processed by the client thread.
2771 int cmd_stop_trace(struct ltt_session
*session
)
2774 struct ltt_kernel_session
*ksession
;
2775 struct ltt_ust_session
*usess
;
2777 LTTNG_ASSERT(session
);
2779 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2781 ksession
= session
->kernel_session
;
2782 usess
= session
->ust_session
;
2784 /* Session is not active. Skip everythong and inform the client. */
2785 if (!session
->active
) {
2786 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2790 ret
= stop_kernel_session(ksession
);
2791 if (ret
!= LTTNG_OK
) {
2795 if (usess
&& usess
->active
) {
2796 ret
= ust_app_stop_trace_all(usess
);
2798 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2803 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2805 /* Flag inactive after a successful stop. */
2806 session
->active
= 0;
2814 * Set the base_path of the session only if subdir of a control uris is set.
2815 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2817 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2819 struct lttng_uri
*uris
)
2824 for (i
= 0; i
< nb_uri
; i
++) {
2825 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2826 uris
[i
].subdir
[0] == '\0') {
2827 /* Not interested in these URIs */
2831 if (session
->base_path
!= NULL
) {
2832 free(session
->base_path
);
2833 session
->base_path
= NULL
;
2836 /* Set session base_path */
2837 session
->base_path
= strdup(uris
[i
].subdir
);
2838 if (!session
->base_path
) {
2839 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2840 uris
[i
].subdir
, session
->name
);
2841 ret
= LTTNG_ERR_NOMEM
;
2844 DBG2("Setting base path \"%s\" for session \"%s\"",
2845 session
->base_path
, session
->name
);
2853 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2855 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2856 struct lttng_uri
*uris
)
2859 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2860 struct ltt_ust_session
*usess
= session
->ust_session
;
2862 LTTNG_ASSERT(session
);
2864 LTTNG_ASSERT(nb_uri
> 0);
2866 /* Can't set consumer URI if the session is active. */
2867 if (session
->active
) {
2868 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2873 * Set the session base path if any. This is done inside
2874 * cmd_set_consumer_uri to preserve backward compatibility of the
2875 * previous session creation api vs the session descriptor api.
2877 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2878 if (ret
!= LTTNG_OK
) {
2882 /* Set the "global" consumer URIs */
2883 for (i
= 0; i
< nb_uri
; i
++) {
2884 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2886 if (ret
!= LTTNG_OK
) {
2891 /* Set UST session URIs */
2892 if (session
->ust_session
) {
2893 for (i
= 0; i
< nb_uri
; i
++) {
2894 ret
= add_uri_to_consumer(session
,
2895 session
->ust_session
->consumer
,
2896 &uris
[i
], LTTNG_DOMAIN_UST
);
2897 if (ret
!= LTTNG_OK
) {
2903 /* Set kernel session URIs */
2904 if (session
->kernel_session
) {
2905 for (i
= 0; i
< nb_uri
; i
++) {
2906 ret
= add_uri_to_consumer(session
,
2907 session
->kernel_session
->consumer
,
2908 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2909 if (ret
!= LTTNG_OK
) {
2916 * Make sure to set the session in output mode after we set URI since a
2917 * session can be created without URL (thus flagged in no output mode).
2919 session
->output_traces
= 1;
2921 ksess
->output_traces
= 1;
2925 usess
->output_traces
= 1;
2936 enum lttng_error_code
set_session_output_from_descriptor(
2937 struct ltt_session
*session
,
2938 const struct lttng_session_descriptor
*descriptor
)
2941 enum lttng_error_code ret_code
= LTTNG_OK
;
2942 enum lttng_session_descriptor_type session_type
=
2943 lttng_session_descriptor_get_type(descriptor
);
2944 enum lttng_session_descriptor_output_type output_type
=
2945 lttng_session_descriptor_get_output_type(descriptor
);
2946 struct lttng_uri uris
[2] = {};
2947 size_t uri_count
= 0;
2949 switch (output_type
) {
2950 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2952 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2953 lttng_session_descriptor_get_local_output_uri(descriptor
,
2957 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2958 lttng_session_descriptor_get_network_output_uris(descriptor
,
2959 &uris
[0], &uris
[1]);
2963 ret_code
= LTTNG_ERR_INVALID
;
2967 switch (session_type
) {
2968 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2970 struct snapshot_output
*new_output
= NULL
;
2972 new_output
= snapshot_output_alloc();
2974 ret_code
= LTTNG_ERR_NOMEM
;
2978 ret
= snapshot_output_init_with_uri(session
,
2979 DEFAULT_SNAPSHOT_MAX_SIZE
,
2980 NULL
, uris
, uri_count
, session
->consumer
,
2981 new_output
, &session
->snapshot
);
2983 ret_code
= (ret
== -ENOMEM
) ?
2984 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2985 snapshot_output_destroy(new_output
);
2988 snapshot_add_output(&session
->snapshot
, new_output
);
2991 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2992 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2994 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2998 ret_code
= LTTNG_ERR_INVALID
;
3006 enum lttng_error_code
cmd_create_session_from_descriptor(
3007 struct lttng_session_descriptor
*descriptor
,
3008 const lttng_sock_cred
*creds
,
3009 const char *home_path
)
3012 enum lttng_error_code ret_code
;
3013 const char *session_name
;
3014 struct ltt_session
*new_session
= NULL
;
3015 enum lttng_session_descriptor_status descriptor_status
;
3017 session_lock_list();
3019 if (*home_path
!= '/') {
3020 ERR("Home path provided by client is not absolute");
3021 ret_code
= LTTNG_ERR_INVALID
;
3026 descriptor_status
= lttng_session_descriptor_get_session_name(
3027 descriptor
, &session_name
);
3028 switch (descriptor_status
) {
3029 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3031 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3032 session_name
= NULL
;
3035 ret_code
= LTTNG_ERR_INVALID
;
3039 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3041 if (ret_code
!= LTTNG_OK
) {
3045 if (!session_name
) {
3046 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3049 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3054 if (!lttng_session_descriptor_is_output_destination_initialized(
3057 * Only include the session's creation time in the output
3058 * destination if the name of the session itself was
3059 * not auto-generated.
3061 ret_code
= lttng_session_descriptor_set_default_output(
3063 session_name
? &new_session
->creation_time
: NULL
,
3065 if (ret_code
!= LTTNG_OK
) {
3069 new_session
->has_user_specified_directory
=
3070 lttng_session_descriptor_has_output_directory(
3074 switch (lttng_session_descriptor_get_type(descriptor
)) {
3075 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3076 new_session
->snapshot_mode
= 1;
3078 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3079 new_session
->live_timer
=
3080 lttng_session_descriptor_live_get_timer_interval(
3087 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3088 if (ret_code
!= LTTNG_OK
) {
3091 new_session
->consumer
->enabled
= 1;
3092 ret_code
= LTTNG_OK
;
3094 /* Release reference provided by the session_create function. */
3095 session_put(new_session
);
3096 if (ret_code
!= LTTNG_OK
&& new_session
) {
3097 /* Release the global reference on error. */
3098 session_destroy(new_session
);
3100 session_unlock_list();
3104 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3105 struct lttng_session_descriptor
**return_descriptor
)
3108 size_t payload_size
;
3109 struct lttng_dynamic_buffer payload
;
3110 struct lttng_buffer_view home_dir_view
;
3111 struct lttng_buffer_view session_descriptor_view
;
3112 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3113 enum lttng_error_code ret_code
;
3115 lttng_dynamic_buffer_init(&payload
);
3116 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3118 ret_code
= LTTNG_ERR_INVALID
;
3121 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3122 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3123 ret_code
= LTTNG_ERR_INVALID
;
3127 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3128 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3129 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3131 ret_code
= LTTNG_ERR_NOMEM
;
3135 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3137 ERR("Reception of session descriptor failed, aborting.");
3138 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3142 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3145 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3146 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3147 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3148 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3149 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3153 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3155 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3156 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3157 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3158 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3159 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3163 ret
= lttng_session_descriptor_create_from_buffer(
3164 &session_descriptor_view
, &session_descriptor
);
3166 ERR("Failed to create session descriptor from payload of \"create session\" command");
3167 ret_code
= LTTNG_ERR_INVALID
;
3172 * Sets the descriptor's auto-generated properties (name, output) if
3175 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3177 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3178 if (ret_code
!= LTTNG_OK
) {
3182 ret_code
= LTTNG_OK
;
3183 *return_descriptor
= session_descriptor
;
3184 session_descriptor
= NULL
;
3186 lttng_dynamic_buffer_reset(&payload
);
3187 lttng_session_descriptor_destroy(session_descriptor
);
3192 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3193 void *_reply_context
)
3197 const struct cmd_destroy_session_reply_context
*reply_context
=
3199 struct lttng_dynamic_buffer payload
;
3200 struct lttcomm_session_destroy_command_header cmd_header
;
3201 struct lttng_trace_archive_location
*location
= NULL
;
3202 struct lttcomm_lttng_msg llm
= {
3203 .cmd_type
= LTTNG_DESTROY_SESSION
,
3204 .ret_code
= reply_context
->destruction_status
,
3207 sizeof(struct lttcomm_session_destroy_command_header
),
3210 size_t payload_size_before_location
;
3212 lttng_dynamic_buffer_init(&payload
);
3214 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3216 ERR("Failed to append session destruction message");
3220 cmd_header
.rotation_state
=
3221 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3222 session
->rotation_state
:
3223 LTTNG_ROTATION_STATE_NO_ROTATION
);
3224 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3225 sizeof(cmd_header
));
3227 ERR("Failed to append session destruction command header");
3231 if (!reply_context
->implicit_rotation_on_destroy
) {
3232 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3236 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3237 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3242 location
= session_get_trace_archive_location(session
);
3244 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",