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
9 #include "bin/lttng-sessiond/tracker.h"
10 #include "lttng/lttng-error.h"
11 #include "lttng/tracker.h"
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
20 #include <common/defaults.h>
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/relayd/relayd.h>
24 #include <common/utils.h>
25 #include <common/compat/string.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/dynamic-buffer.h>
28 #include <common/buffer-view.h>
29 #include <common/trace-chunk.h>
30 #include <lttng/location-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/condition/condition.h>
33 #include <lttng/action/action.h>
34 #include <lttng/channel.h>
35 #include <lttng/channel-internal.h>
36 #include <lttng/rotate-internal.h>
37 #include <lttng/location-internal.h>
38 #include <lttng/session-internal.h>
39 #include <lttng/userspace-probe-internal.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <common/string-utils/string-utils.h>
46 #include "health-sessiond.h"
48 #include "kernel-consumer.h"
49 #include "lttng-sessiond.h"
51 #include "lttng-syscall.h"
53 #include "buffer-registry.h"
54 #include "notification-thread.h"
55 #include "notification-thread-commands.h"
57 #include "rotation-thread.h"
59 #include "agent-thread.h"
63 /* Sleep for 100ms between each check for the shm path's deletion. */
64 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
66 struct cmd_destroy_session_reply_context
{
68 bool implicit_rotation_on_destroy
;
70 * Indicates whether or not an error occurred while launching the
71 * destruction of a session.
73 enum lttng_error_code destruction_status
;
76 static enum lttng_error_code
wait_on_path(void *path
);
79 * Command completion handler that is used by the destroy command
80 * when a session that has a non-default shm_path is being destroyed.
82 * See comment in cmd_destroy_session() for the rationale.
84 static struct destroy_completion_handler
{
85 struct cmd_completion_handler handler
;
86 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
87 } destroy_completion_handler
= {
90 .data
= destroy_completion_handler
.shm_path
95 static struct cmd_completion_handler
*current_completion_handler
;
98 * Used to keep a unique index for each relayd socket created where this value
99 * is associated with streams on the consumer so it can match the right relayd
100 * to send to. It must be accessed with the relayd_net_seq_idx_lock
103 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
104 static uint64_t relayd_net_seq_idx
;
106 static int validate_ust_event_name(const char *);
107 static int cmd_enable_event_internal(struct ltt_session
*session
,
108 const struct lttng_domain
*domain
,
109 char *channel_name
, struct lttng_event
*event
,
110 char *filter_expression
,
111 struct lttng_filter_bytecode
*filter
,
112 struct lttng_event_exclusion
*exclusion
,
116 * Create a session path used by list_lttng_sessions for the case that the
117 * session consumer is on the network.
119 static int build_network_session_path(char *dst
, size_t size
,
120 struct ltt_session
*session
)
122 int ret
, kdata_port
, udata_port
;
123 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
124 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
129 memset(tmp_urls
, 0, sizeof(tmp_urls
));
130 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
132 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
134 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
135 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
136 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
139 if (session
->ust_session
&& session
->ust_session
->consumer
) {
140 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
141 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
144 if (uuri
== NULL
&& kuri
== NULL
) {
145 uri
= &session
->consumer
->dst
.net
.control
;
146 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
147 } else if (kuri
&& uuri
) {
148 ret
= uri_compare(kuri
, uuri
);
152 /* Build uuri URL string */
153 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
160 } else if (kuri
&& uuri
== NULL
) {
162 } else if (uuri
&& kuri
== NULL
) {
166 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
172 * Do we have a UST url set. If yes, this means we have both kernel and UST
175 if (*tmp_uurl
!= '\0') {
176 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
177 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
180 if (kuri
|| (!kuri
&& !uuri
)) {
183 /* No kernel URI, use the UST port. */
186 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
194 * Get run-time attributes if the session has been started (discarded events,
197 static int get_kernel_runtime_stats(struct ltt_session
*session
,
198 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
199 uint64_t *lost_packets
)
203 if (!session
->has_been_started
) {
205 *discarded_events
= 0;
210 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
211 session
->kernel_session
->consumer
,
217 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
218 session
->kernel_session
->consumer
,
229 * Get run-time attributes if the session has been started (discarded events,
232 static int get_ust_runtime_stats(struct ltt_session
*session
,
233 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
234 uint64_t *lost_packets
)
237 struct ltt_ust_session
*usess
;
239 if (!discarded_events
|| !lost_packets
) {
244 usess
= session
->ust_session
;
245 assert(discarded_events
);
246 assert(lost_packets
);
248 if (!usess
|| !session
->has_been_started
) {
249 *discarded_events
= 0;
255 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
256 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
257 &usess
->buffer_reg_uid_list
,
258 usess
->consumer
, uchan
->id
,
259 uchan
->attr
.overwrite
,
262 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
263 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
264 uchan
, usess
->consumer
,
265 uchan
->attr
.overwrite
,
271 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
272 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
274 ERR("Unsupported buffer type");
285 * Fill lttng_channel array of all channels.
287 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
288 struct ltt_session
*session
, struct lttng_channel
*channels
,
289 struct lttng_channel_extended
*chan_exts
)
292 struct ltt_kernel_channel
*kchan
;
294 DBG("Listing channels for session %s", session
->name
);
297 case LTTNG_DOMAIN_KERNEL
:
298 /* Kernel channels */
299 if (session
->kernel_session
!= NULL
) {
300 cds_list_for_each_entry(kchan
,
301 &session
->kernel_session
->channel_list
.head
, list
) {
302 uint64_t discarded_events
, lost_packets
;
303 struct lttng_channel_extended
*extended
;
305 extended
= (struct lttng_channel_extended
*)
306 kchan
->channel
->attr
.extended
.ptr
;
308 ret
= get_kernel_runtime_stats(session
, kchan
,
309 &discarded_events
, &lost_packets
);
313 /* Copy lttng_channel struct to array */
314 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
315 channels
[i
].enabled
= kchan
->enabled
;
316 chan_exts
[i
].discarded_events
=
318 chan_exts
[i
].lost_packets
= lost_packets
;
319 chan_exts
[i
].monitor_timer_interval
=
320 extended
->monitor_timer_interval
;
321 chan_exts
[i
].blocking_timeout
= 0;
326 case LTTNG_DOMAIN_UST
:
328 struct lttng_ht_iter iter
;
329 struct ltt_ust_channel
*uchan
;
332 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
333 &iter
.iter
, uchan
, node
.node
) {
334 uint64_t discarded_events
= 0, lost_packets
= 0;
336 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
337 LTTNG_SYMBOL_NAME_LEN
)) {
340 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
341 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
342 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
343 channels
[i
].attr
.switch_timer_interval
=
344 uchan
->attr
.switch_timer_interval
;
345 channels
[i
].attr
.read_timer_interval
=
346 uchan
->attr
.read_timer_interval
;
347 channels
[i
].enabled
= uchan
->enabled
;
348 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
349 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
352 * Map enum lttng_ust_output to enum lttng_event_output.
354 switch (uchan
->attr
.output
) {
356 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
360 * LTTNG_UST_MMAP is the only supported UST
367 chan_exts
[i
].monitor_timer_interval
=
368 uchan
->monitor_timer_interval
;
369 chan_exts
[i
].blocking_timeout
=
370 uchan
->attr
.u
.s
.blocking_timeout
;
372 ret
= get_ust_runtime_stats(session
, uchan
,
373 &discarded_events
, &lost_packets
);
377 chan_exts
[i
].discarded_events
= discarded_events
;
378 chan_exts
[i
].lost_packets
= lost_packets
;
390 return -LTTNG_ERR_FATAL
;
396 static int increment_extended_len(const char *filter_expression
,
397 struct lttng_event_exclusion
*exclusion
,
398 const struct lttng_userspace_probe_location
*probe_location
,
399 size_t *extended_len
)
403 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
405 if (filter_expression
) {
406 *extended_len
+= strlen(filter_expression
) + 1;
410 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
413 if (probe_location
) {
414 ret
= lttng_userspace_probe_location_serialize(probe_location
,
419 *extended_len
+= ret
;
426 static int append_extended_info(const char *filter_expression
,
427 struct lttng_event_exclusion
*exclusion
,
428 struct lttng_userspace_probe_location
*probe_location
,
432 size_t filter_len
= 0;
433 size_t nb_exclusions
= 0;
434 size_t userspace_probe_location_len
= 0;
435 struct lttng_dynamic_buffer location_buffer
;
436 struct lttcomm_event_extended_header extended_header
;
438 if (filter_expression
) {
439 filter_len
= strlen(filter_expression
) + 1;
443 nb_exclusions
= exclusion
->count
;
446 if (probe_location
) {
447 lttng_dynamic_buffer_init(&location_buffer
);
448 ret
= lttng_userspace_probe_location_serialize(probe_location
,
449 &location_buffer
, NULL
);
454 userspace_probe_location_len
= location_buffer
.size
;
457 /* Set header fields */
458 extended_header
.filter_len
= filter_len
;
459 extended_header
.nb_exclusions
= nb_exclusions
;
460 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
463 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
464 *extended_at
+= sizeof(extended_header
);
466 /* Copy filter string */
467 if (filter_expression
) {
468 memcpy(*extended_at
, filter_expression
, filter_len
);
469 *extended_at
+= filter_len
;
472 /* Copy exclusion names */
474 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
476 memcpy(*extended_at
, &exclusion
->names
, len
);
480 if (probe_location
) {
481 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
482 *extended_at
+= location_buffer
.size
;
483 lttng_dynamic_buffer_reset(&location_buffer
);
491 * Create a list of agent domain events.
493 * Return number of events in list on success or else a negative value.
495 static int list_lttng_agent_events(struct agent
*agt
,
496 struct lttng_event
**events
, size_t *total_size
)
499 unsigned int nb_event
= 0;
500 struct agent_event
*event
;
501 struct lttng_event
*tmp_events
= NULL
;
502 struct lttng_ht_iter iter
;
503 size_t extended_len
= 0;
509 DBG3("Listing agent events");
512 nb_event
= lttng_ht_get_count(agt
->events
);
520 /* Compute required extended infos size */
521 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
524 * This is only valid because the commands which add events are
525 * processed in the same thread as the listing.
528 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
529 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
532 DBG("Error computing the length of extended info message");
533 ret
= -LTTNG_ERR_FATAL
;
539 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
540 tmp_events
= zmalloc(*total_size
);
542 PERROR("zmalloc agent events session");
543 ret
= -LTTNG_ERR_FATAL
;
547 extended_at
= ((uint8_t *) tmp_events
) +
548 nb_event
* sizeof(struct lttng_event
);
551 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
552 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
553 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
554 tmp_events
[i
].enabled
= event
->enabled
;
555 tmp_events
[i
].loglevel
= event
->loglevel_value
;
556 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
559 /* Append extended info */
560 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
563 DBG("Error appending extended info message");
564 ret
= -LTTNG_ERR_FATAL
;
569 *events
= tmp_events
;
571 assert(nb_event
== i
);
582 * Create a list of ust global domain events.
584 static int list_lttng_ust_global_events(char *channel_name
,
585 struct ltt_ust_domain_global
*ust_global
,
586 struct lttng_event
**events
, size_t *total_size
)
589 unsigned int nb_event
= 0;
590 struct lttng_ht_iter iter
;
591 struct lttng_ht_node_str
*node
;
592 struct ltt_ust_channel
*uchan
;
593 struct ltt_ust_event
*uevent
;
594 struct lttng_event
*tmp
;
595 size_t extended_len
= 0;
598 DBG("Listing UST global events for channel %s", channel_name
);
602 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
603 node
= lttng_ht_iter_get_node_str(&iter
);
605 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
609 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
611 nb_event
= lttng_ht_get_count(uchan
->events
);
618 DBG3("Listing UST global %d events", nb_event
);
620 /* Compute required extended infos size */
621 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
622 if (uevent
->internal
) {
627 ret
= increment_extended_len(uevent
->filter_expression
,
628 uevent
->exclusion
, NULL
, &extended_len
);
630 DBG("Error computing the length of extended info message");
631 ret
= -LTTNG_ERR_FATAL
;
636 /* All events are internal, skip. */
642 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
643 tmp
= zmalloc(*total_size
);
645 ret
= -LTTNG_ERR_FATAL
;
649 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
651 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
652 if (uevent
->internal
) {
653 /* This event should remain hidden from clients */
656 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
657 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
658 tmp
[i
].enabled
= uevent
->enabled
;
660 switch (uevent
->attr
.instrumentation
) {
661 case LTTNG_UST_TRACEPOINT
:
662 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
664 case LTTNG_UST_PROBE
:
665 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
667 case LTTNG_UST_FUNCTION
:
668 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
672 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
673 switch (uevent
->attr
.loglevel_type
) {
674 case LTTNG_UST_LOGLEVEL_ALL
:
675 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
677 case LTTNG_UST_LOGLEVEL_RANGE
:
678 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
680 case LTTNG_UST_LOGLEVEL_SINGLE
:
681 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
684 if (uevent
->filter
) {
687 if (uevent
->exclusion
) {
688 tmp
[i
].exclusion
= 1;
692 /* Append extended info */
693 ret
= append_extended_info(uevent
->filter_expression
,
694 uevent
->exclusion
, NULL
, &extended_at
);
696 DBG("Error appending extended info message");
697 ret
= -LTTNG_ERR_FATAL
;
710 * Fill lttng_event array of all kernel events in the channel.
712 static int list_lttng_kernel_events(char *channel_name
,
713 struct ltt_kernel_session
*kernel_session
,
714 struct lttng_event
**events
, size_t *total_size
)
717 unsigned int nb_event
;
718 struct ltt_kernel_event
*event
;
719 struct ltt_kernel_channel
*kchan
;
720 size_t extended_len
= 0;
723 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
725 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
729 nb_event
= kchan
->event_count
;
731 DBG("Listing events for channel %s", kchan
->channel
->name
);
739 /* Compute required extended infos size */
740 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
741 ret
= increment_extended_len(event
->filter_expression
, NULL
,
742 event
->userspace_probe_location
,
745 DBG("Error computing the length of extended info message");
746 ret
= -LTTNG_ERR_FATAL
;
751 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
752 *events
= zmalloc(*total_size
);
753 if (*events
== NULL
) {
754 ret
= -LTTNG_ERR_FATAL
;
758 extended_at
= ((void *) *events
) +
759 nb_event
* sizeof(struct lttng_event
);
761 /* Kernel channels */
762 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
763 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
764 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
765 (*events
)[i
].enabled
= event
->enabled
;
766 (*events
)[i
].filter
=
767 (unsigned char) !!event
->filter_expression
;
769 switch (event
->event
->instrumentation
) {
770 case LTTNG_KERNEL_TRACEPOINT
:
771 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
773 case LTTNG_KERNEL_KRETPROBE
:
774 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
775 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
776 sizeof(struct lttng_kernel_kprobe
));
778 case LTTNG_KERNEL_KPROBE
:
779 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
780 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
781 sizeof(struct lttng_kernel_kprobe
));
783 case LTTNG_KERNEL_UPROBE
:
784 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
786 case LTTNG_KERNEL_FUNCTION
:
787 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
788 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
789 sizeof(struct lttng_kernel_function
));
791 case LTTNG_KERNEL_NOOP
:
792 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
794 case LTTNG_KERNEL_SYSCALL
:
795 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
797 case LTTNG_KERNEL_ALL
:
805 /* Append extended info */
806 ret
= append_extended_info(event
->filter_expression
, NULL
,
807 event
->userspace_probe_location
, &extended_at
);
809 DBG("Error appending extended info message");
810 ret
= -LTTNG_ERR_FATAL
;
819 /* Negate the error code to differentiate the size from an error */
824 * Add URI so the consumer output object. Set the correct path depending on the
825 * domain adding the default trace directory.
827 static enum lttng_error_code
add_uri_to_consumer(
828 const struct ltt_session
*session
,
829 struct consumer_output
*consumer
,
830 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
833 enum lttng_error_code ret_code
= LTTNG_OK
;
837 if (consumer
== NULL
) {
838 DBG("No consumer detected. Don't add URI. Stopping.");
839 ret_code
= LTTNG_ERR_NO_CONSUMER
;
844 case LTTNG_DOMAIN_KERNEL
:
845 ret
= lttng_strncpy(consumer
->domain_subdir
,
846 DEFAULT_KERNEL_TRACE_DIR
,
847 sizeof(consumer
->domain_subdir
));
849 case LTTNG_DOMAIN_UST
:
850 ret
= lttng_strncpy(consumer
->domain_subdir
,
851 DEFAULT_UST_TRACE_DIR
,
852 sizeof(consumer
->domain_subdir
));
856 * This case is possible is we try to add the URI to the global
857 * tracing session consumer object which in this case there is
860 memset(consumer
->domain_subdir
, 0,
861 sizeof(consumer
->domain_subdir
));
865 ERR("Failed to initialize consumer output domain subdirectory");
866 ret_code
= LTTNG_ERR_FATAL
;
870 switch (uri
->dtype
) {
873 DBG2("Setting network URI to consumer");
875 if (consumer
->type
== CONSUMER_DST_NET
) {
876 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
877 consumer
->dst
.net
.control_isset
) ||
878 (uri
->stype
== LTTNG_STREAM_DATA
&&
879 consumer
->dst
.net
.data_isset
)) {
880 ret_code
= LTTNG_ERR_URL_EXIST
;
884 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
887 /* Set URI into consumer output object */
888 ret
= consumer_set_network_uri(session
, consumer
, uri
);
892 } else if (ret
== 1) {
894 * URI was the same in the consumer so we do not append the subdir
895 * again so to not duplicate output dir.
902 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
903 ret_code
= LTTNG_ERR_INVALID
;
906 DBG2("Setting trace directory path from URI to %s",
908 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
910 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
912 sizeof(consumer
->dst
.session_root_path
));
914 ret_code
= LTTNG_ERR_FATAL
;
917 consumer
->type
= CONSUMER_DST_LOCAL
;
927 * Init tracing by creating trace directory and sending fds kernel consumer.
929 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
932 struct lttng_ht_iter iter
;
933 struct consumer_socket
*socket
;
939 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
940 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
942 pthread_mutex_lock(socket
->lock
);
943 ret
= kernel_consumer_send_session(socket
, session
);
944 pthread_mutex_unlock(socket
->lock
);
946 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
958 * Create a socket to the relayd using the URI.
960 * On success, the relayd_sock pointer is set to the created socket.
961 * Else, it remains untouched and an LTTng error code is returned.
963 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
964 struct lttcomm_relayd_sock
**relayd_sock
,
965 struct consumer_output
*consumer
)
968 enum lttng_error_code status
= LTTNG_OK
;
969 struct lttcomm_relayd_sock
*rsock
;
971 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
972 RELAYD_VERSION_COMM_MINOR
);
974 status
= LTTNG_ERR_FATAL
;
979 * Connect to relayd so we can proceed with a session creation. This call
980 * can possibly block for an arbitrary amount of time to set the health
981 * state to be in poll execution.
984 ret
= relayd_connect(rsock
);
987 ERR("Unable to reach lttng-relayd");
988 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
992 /* Create socket for control stream. */
993 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
994 uint64_t result_flags
;
996 DBG3("Creating relayd stream socket from URI");
998 /* Check relayd version */
999 ret
= relayd_version_check(rsock
);
1000 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
1001 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
1003 } else if (ret
< 0) {
1004 ERR("Unable to reach lttng-relayd");
1005 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1008 consumer
->relay_major_version
= rsock
->major
;
1009 consumer
->relay_minor_version
= rsock
->minor
;
1010 ret
= relayd_get_configuration(rsock
, 0,
1013 ERR("Unable to get relayd configuration");
1014 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1017 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
1018 consumer
->relay_allows_clear
= true;
1020 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1021 DBG3("Creating relayd data socket from URI");
1023 /* Command is not valid */
1024 ERR("Relayd invalid stream type: %d", uri
->stype
);
1025 status
= LTTNG_ERR_INVALID
;
1029 *relayd_sock
= rsock
;
1034 /* The returned value is not useful since we are on an error path. */
1035 (void) relayd_close(rsock
);
1043 * Connect to the relayd using URI and send the socket to the right consumer.
1045 * The consumer socket lock must be held by the caller.
1047 * Returns LTTNG_OK on success or an LTTng error code on failure.
1049 static enum lttng_error_code
send_consumer_relayd_socket(
1050 unsigned int session_id
,
1051 struct lttng_uri
*relayd_uri
,
1052 struct consumer_output
*consumer
,
1053 struct consumer_socket
*consumer_sock
,
1054 const char *session_name
, const char *hostname
,
1055 const char *base_path
, int session_live_timer
,
1056 const uint64_t *current_chunk_id
,
1057 time_t session_creation_time
,
1058 bool session_name_contains_creation_time
)
1061 struct lttcomm_relayd_sock
*rsock
= NULL
;
1062 enum lttng_error_code status
;
1064 /* Connect to relayd and make version check if uri is the control. */
1065 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1066 if (status
!= LTTNG_OK
) {
1067 goto relayd_comm_error
;
1071 /* Set the network sequence index if not set. */
1072 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1073 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1075 * Increment net_seq_idx because we are about to transfer the
1076 * new relayd socket to the consumer.
1077 * Assign unique key so the consumer can match streams.
1079 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1080 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1083 /* Send relayd socket to consumer. */
1084 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1085 relayd_uri
->stype
, session_id
,
1086 session_name
, hostname
, base_path
,
1087 session_live_timer
, current_chunk_id
,
1088 session_creation_time
, session_name_contains_creation_time
);
1090 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1094 /* Flag that the corresponding socket was sent. */
1095 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1096 consumer_sock
->control_sock_sent
= 1;
1097 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1098 consumer_sock
->data_sock_sent
= 1;
1102 * Close socket which was dup on the consumer side. The session daemon does
1103 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1107 if (status
!= LTTNG_OK
) {
1109 * The consumer output for this session should not be used anymore
1110 * since the relayd connection failed thus making any tracing or/and
1111 * streaming not usable.
1113 consumer
->enabled
= 0;
1115 (void) relayd_close(rsock
);
1123 * Send both relayd sockets to a specific consumer and domain. This is a
1124 * helper function to facilitate sending the information to the consumer for a
1127 * The consumer socket lock must be held by the caller.
1129 * Returns LTTNG_OK, or an LTTng error code on failure.
1131 static enum lttng_error_code
send_consumer_relayd_sockets(
1132 enum lttng_domain_type domain
,
1133 unsigned int session_id
, struct consumer_output
*consumer
,
1134 struct consumer_socket
*sock
, const char *session_name
,
1135 const char *hostname
, const char *base_path
, int session_live_timer
,
1136 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1137 bool session_name_contains_creation_time
)
1139 enum lttng_error_code status
= LTTNG_OK
;
1144 /* Sending control relayd socket. */
1145 if (!sock
->control_sock_sent
) {
1146 status
= send_consumer_relayd_socket(session_id
,
1147 &consumer
->dst
.net
.control
, consumer
, sock
,
1148 session_name
, hostname
, base_path
, session_live_timer
,
1149 current_chunk_id
, session_creation_time
,
1150 session_name_contains_creation_time
);
1151 if (status
!= LTTNG_OK
) {
1156 /* Sending data relayd socket. */
1157 if (!sock
->data_sock_sent
) {
1158 status
= send_consumer_relayd_socket(session_id
,
1159 &consumer
->dst
.net
.data
, consumer
, sock
,
1160 session_name
, hostname
, base_path
, session_live_timer
,
1161 current_chunk_id
, session_creation_time
,
1162 session_name_contains_creation_time
);
1163 if (status
!= LTTNG_OK
) {
1173 * Setup relayd connections for a tracing session. First creates the socket to
1174 * the relayd and send them to the right domain consumer. Consumer type MUST be
1177 int cmd_setup_relayd(struct ltt_session
*session
)
1180 struct ltt_ust_session
*usess
;
1181 struct ltt_kernel_session
*ksess
;
1182 struct consumer_socket
*socket
;
1183 struct lttng_ht_iter iter
;
1184 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1188 usess
= session
->ust_session
;
1189 ksess
= session
->kernel_session
;
1191 DBG("Setting relayd for session %s", session
->name
);
1194 if (session
->current_trace_chunk
) {
1195 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1196 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1198 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1199 current_chunk_id
.is_set
= true;
1201 ERR("Failed to get current trace chunk id");
1202 ret
= LTTNG_ERR_UNK
;
1207 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1208 && usess
->consumer
->enabled
) {
1209 /* For each consumer socket, send relayd sockets */
1210 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1211 socket
, node
.node
) {
1212 pthread_mutex_lock(socket
->lock
);
1213 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1214 usess
->consumer
, socket
,
1215 session
->name
, session
->hostname
,
1217 session
->live_timer
,
1218 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1219 session
->creation_time
,
1220 session
->name_contains_creation_time
);
1221 pthread_mutex_unlock(socket
->lock
);
1222 if (ret
!= LTTNG_OK
) {
1225 /* Session is now ready for network streaming. */
1226 session
->net_handle
= 1;
1228 session
->consumer
->relay_major_version
=
1229 usess
->consumer
->relay_major_version
;
1230 session
->consumer
->relay_minor_version
=
1231 usess
->consumer
->relay_minor_version
;
1232 session
->consumer
->relay_allows_clear
=
1233 usess
->consumer
->relay_allows_clear
;
1236 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1237 && ksess
->consumer
->enabled
) {
1238 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1239 socket
, node
.node
) {
1240 pthread_mutex_lock(socket
->lock
);
1241 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1242 ksess
->consumer
, socket
,
1243 session
->name
, session
->hostname
,
1245 session
->live_timer
,
1246 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1247 session
->creation_time
,
1248 session
->name_contains_creation_time
);
1249 pthread_mutex_unlock(socket
->lock
);
1250 if (ret
!= LTTNG_OK
) {
1253 /* Session is now ready for network streaming. */
1254 session
->net_handle
= 1;
1256 session
->consumer
->relay_major_version
=
1257 ksess
->consumer
->relay_major_version
;
1258 session
->consumer
->relay_minor_version
=
1259 ksess
->consumer
->relay_minor_version
;
1260 session
->consumer
->relay_allows_clear
=
1261 ksess
->consumer
->relay_allows_clear
;
1270 * Start a kernel session by opening all necessary streams.
1272 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1275 struct ltt_kernel_channel
*kchan
;
1277 /* Open kernel metadata */
1278 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1279 ret
= kernel_open_metadata(ksess
);
1281 ret
= LTTNG_ERR_KERN_META_FAIL
;
1286 /* Open kernel metadata stream */
1287 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1288 ret
= kernel_open_metadata_stream(ksess
);
1290 ERR("Kernel create metadata stream failed");
1291 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1296 /* For each channel */
1297 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1298 if (kchan
->stream_count
== 0) {
1299 ret
= kernel_open_channel_stream(kchan
);
1301 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1304 /* Update the stream global counter */
1305 ksess
->stream_count_global
+= ret
;
1309 /* Setup kernel consumer socket and send fds to it */
1310 ret
= init_kernel_tracing(ksess
);
1312 ret
= LTTNG_ERR_KERN_START_FAIL
;
1316 /* This start the kernel tracing */
1317 ret
= kernel_start_session(ksess
);
1319 ret
= LTTNG_ERR_KERN_START_FAIL
;
1323 /* Quiescent wait after starting trace */
1324 kernel_wait_quiescent();
1334 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1336 struct ltt_kernel_channel
*kchan
;
1337 bool error_occurred
= false;
1340 if (!ksess
|| !ksess
->active
) {
1343 DBG("Stopping kernel tracing");
1345 ret
= kernel_stop_session(ksess
);
1347 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1351 kernel_wait_quiescent();
1353 /* Flush metadata after stopping (if exists) */
1354 if (ksess
->metadata_stream_fd
>= 0) {
1355 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1357 ERR("Kernel metadata flush failed");
1358 error_occurred
= true;
1362 /* Flush all buffers after stopping */
1363 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1364 ret
= kernel_flush_buffer(kchan
);
1366 ERR("Kernel flush buffer error");
1367 error_occurred
= true;
1372 if (error_occurred
) {
1373 ret
= LTTNG_ERR_UNK
;
1382 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1384 int cmd_disable_channel(struct ltt_session
*session
,
1385 enum lttng_domain_type domain
, char *channel_name
)
1388 struct ltt_ust_session
*usess
;
1390 usess
= session
->ust_session
;
1395 case LTTNG_DOMAIN_KERNEL
:
1397 ret
= channel_kernel_disable(session
->kernel_session
,
1399 if (ret
!= LTTNG_OK
) {
1403 kernel_wait_quiescent();
1406 case LTTNG_DOMAIN_UST
:
1408 struct ltt_ust_channel
*uchan
;
1409 struct lttng_ht
*chan_ht
;
1411 chan_ht
= usess
->domain_global
.channels
;
1413 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1414 if (uchan
== NULL
) {
1415 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1419 ret
= channel_ust_disable(usess
, uchan
);
1420 if (ret
!= LTTNG_OK
) {
1426 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1438 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1440 * The wpipe arguments is used as a notifier for the kernel thread.
1442 int cmd_enable_channel(struct ltt_session
*session
,
1443 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1446 struct ltt_ust_session
*usess
= session
->ust_session
;
1447 struct lttng_ht
*chan_ht
;
1449 struct lttng_channel attr
;
1456 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1458 /* Validate channel name */
1459 if (attr
.name
[0] == '.' ||
1460 memchr(attr
.name
, '/', len
) != NULL
) {
1461 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1465 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1470 * Don't try to enable a channel if the session has been started at
1471 * some point in time before. The tracer does not allow it.
1473 if (session
->has_been_started
) {
1474 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1479 * If the session is a live session, remove the switch timer, the
1480 * live timer does the same thing but sends also synchronisation
1481 * beacons for inactive streams.
1483 if (session
->live_timer
> 0) {
1484 attr
.attr
.live_timer_interval
= session
->live_timer
;
1485 attr
.attr
.switch_timer_interval
= 0;
1488 /* Check for feature support */
1489 switch (domain
->type
) {
1490 case LTTNG_DOMAIN_KERNEL
:
1492 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1493 /* Sampling position of buffer is not supported */
1494 WARN("Kernel tracer does not support buffer monitoring. "
1495 "Setting the monitor interval timer to 0 "
1496 "(disabled) for channel '%s' of session '%s'",
1497 attr
.name
, session
->name
);
1498 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1502 case LTTNG_DOMAIN_UST
:
1504 case LTTNG_DOMAIN_JUL
:
1505 case LTTNG_DOMAIN_LOG4J
:
1506 case LTTNG_DOMAIN_PYTHON
:
1507 if (!agent_tracing_is_enabled()) {
1508 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1509 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1514 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1518 switch (domain
->type
) {
1519 case LTTNG_DOMAIN_KERNEL
:
1521 struct ltt_kernel_channel
*kchan
;
1523 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1524 session
->kernel_session
);
1525 if (kchan
== NULL
) {
1526 if (session
->snapshot
.nb_output
> 0 ||
1527 session
->snapshot_mode
) {
1528 /* Enforce mmap output for snapshot sessions. */
1529 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1531 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1532 if (attr
.name
[0] != '\0') {
1533 session
->kernel_session
->has_non_default_channel
= 1;
1536 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1539 if (ret
!= LTTNG_OK
) {
1543 kernel_wait_quiescent();
1546 case LTTNG_DOMAIN_UST
:
1547 case LTTNG_DOMAIN_JUL
:
1548 case LTTNG_DOMAIN_LOG4J
:
1549 case LTTNG_DOMAIN_PYTHON
:
1551 struct ltt_ust_channel
*uchan
;
1556 * Current agent implementation limitations force us to allow
1557 * only one channel at once in "agent" subdomains. Each
1558 * subdomain has a default channel name which must be strictly
1561 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1562 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1563 LTTNG_SYMBOL_NAME_LEN
)) {
1564 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1567 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1568 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1569 LTTNG_SYMBOL_NAME_LEN
)) {
1570 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1573 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1574 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1575 LTTNG_SYMBOL_NAME_LEN
)) {
1576 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1581 chan_ht
= usess
->domain_global
.channels
;
1583 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1584 if (uchan
== NULL
) {
1585 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1586 if (attr
.name
[0] != '\0') {
1587 usess
->has_non_default_channel
= 1;
1590 ret
= channel_ust_enable(usess
, uchan
);
1595 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1599 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1600 session
->has_non_mmap_channel
= true;
1608 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1609 struct ltt_session
*session
,
1610 enum lttng_domain_type domain
,
1611 enum lttng_process_attr process_attr
,
1612 enum lttng_tracking_policy
*policy
)
1614 enum lttng_error_code ret_code
= LTTNG_OK
;
1615 const struct process_attr_tracker
*tracker
;
1618 case LTTNG_DOMAIN_KERNEL
:
1619 if (!session
->kernel_session
) {
1620 ret_code
= LTTNG_ERR_INVALID
;
1623 tracker
= kernel_get_process_attr_tracker(
1624 session
->kernel_session
, process_attr
);
1626 case LTTNG_DOMAIN_UST
:
1627 if (!session
->ust_session
) {
1628 ret_code
= LTTNG_ERR_INVALID
;
1631 tracker
= trace_ust_get_process_attr_tracker(
1632 session
->ust_session
, process_attr
);
1635 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1639 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1641 ret_code
= LTTNG_ERR_INVALID
;
1647 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1648 struct ltt_session
*session
,
1649 enum lttng_domain_type domain
,
1650 enum lttng_process_attr process_attr
,
1651 enum lttng_tracking_policy policy
)
1653 enum lttng_error_code ret_code
= LTTNG_OK
;
1656 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1657 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1658 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1661 ret_code
= LTTNG_ERR_INVALID
;
1666 case LTTNG_DOMAIN_KERNEL
:
1667 if (!session
->kernel_session
) {
1668 ret_code
= LTTNG_ERR_INVALID
;
1671 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1672 session
->kernel_session
, process_attr
, policy
);
1674 case LTTNG_DOMAIN_UST
:
1675 if (!session
->ust_session
) {
1676 ret_code
= LTTNG_ERR_INVALID
;
1679 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1680 session
->ust_session
, process_attr
, policy
);
1683 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1690 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1691 struct ltt_session
*session
,
1692 enum lttng_domain_type domain
,
1693 enum lttng_process_attr process_attr
,
1694 const struct process_attr_value
*value
)
1696 enum lttng_error_code ret_code
= LTTNG_OK
;
1699 case LTTNG_DOMAIN_KERNEL
:
1700 if (!session
->kernel_session
) {
1701 ret_code
= LTTNG_ERR_INVALID
;
1704 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1705 session
->kernel_session
, process_attr
, value
);
1707 case LTTNG_DOMAIN_UST
:
1708 if (!session
->ust_session
) {
1709 ret_code
= LTTNG_ERR_INVALID
;
1712 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1713 session
->ust_session
, process_attr
, value
);
1716 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1723 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1724 struct ltt_session
*session
,
1725 enum lttng_domain_type domain
,
1726 enum lttng_process_attr process_attr
,
1727 const struct process_attr_value
*value
)
1729 enum lttng_error_code ret_code
= LTTNG_OK
;
1732 case LTTNG_DOMAIN_KERNEL
:
1733 if (!session
->kernel_session
) {
1734 ret_code
= LTTNG_ERR_INVALID
;
1737 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1738 session
->kernel_session
, process_attr
, value
);
1740 case LTTNG_DOMAIN_UST
:
1741 if (!session
->ust_session
) {
1742 ret_code
= LTTNG_ERR_INVALID
;
1745 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1746 session
->ust_session
, process_attr
, value
);
1749 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1756 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1757 struct ltt_session
*session
,
1758 enum lttng_domain_type domain
,
1759 enum lttng_process_attr process_attr
,
1760 struct lttng_process_attr_values
**values
)
1762 enum lttng_error_code ret_code
= LTTNG_OK
;
1763 const struct process_attr_tracker
*tracker
;
1764 enum process_attr_tracker_status status
;
1767 case LTTNG_DOMAIN_KERNEL
:
1768 if (!session
->kernel_session
) {
1769 ret_code
= LTTNG_ERR_INVALID
;
1772 tracker
= kernel_get_process_attr_tracker(
1773 session
->kernel_session
, process_attr
);
1775 case LTTNG_DOMAIN_UST
:
1776 if (!session
->ust_session
) {
1777 ret_code
= LTTNG_ERR_INVALID
;
1780 tracker
= trace_ust_get_process_attr_tracker(
1781 session
->ust_session
, process_attr
);
1784 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1789 ret_code
= LTTNG_ERR_INVALID
;
1793 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1795 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1796 ret_code
= LTTNG_OK
;
1798 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1799 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1801 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1802 ret_code
= LTTNG_ERR_NOMEM
;
1805 ret_code
= LTTNG_ERR_UNK
;
1814 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1816 int cmd_disable_event(struct ltt_session
*session
,
1817 enum lttng_domain_type domain
, const char *channel_name
,
1818 const struct lttng_event
*event
)
1821 const char *event_name
;
1823 DBG("Disable event command for event \'%s\'", event
->name
);
1825 event_name
= event
->name
;
1827 /* Error out on unhandled search criteria */
1828 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1829 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1830 ret
= LTTNG_ERR_UNK
;
1837 case LTTNG_DOMAIN_KERNEL
:
1839 struct ltt_kernel_channel
*kchan
;
1840 struct ltt_kernel_session
*ksess
;
1842 ksess
= session
->kernel_session
;
1845 * If a non-default channel has been created in the
1846 * session, explicitely require that -c chan_name needs
1849 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1850 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1854 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1855 if (kchan
== NULL
) {
1856 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1860 switch (event
->type
) {
1861 case LTTNG_EVENT_ALL
:
1862 case LTTNG_EVENT_TRACEPOINT
:
1863 case LTTNG_EVENT_SYSCALL
:
1864 case LTTNG_EVENT_PROBE
:
1865 case LTTNG_EVENT_FUNCTION
:
1866 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1867 if (event_name
[0] == '\0') {
1868 ret
= event_kernel_disable_event(kchan
,
1871 ret
= event_kernel_disable_event(kchan
,
1872 event_name
, event
->type
);
1874 if (ret
!= LTTNG_OK
) {
1879 ret
= LTTNG_ERR_UNK
;
1883 kernel_wait_quiescent();
1886 case LTTNG_DOMAIN_UST
:
1888 struct ltt_ust_channel
*uchan
;
1889 struct ltt_ust_session
*usess
;
1891 usess
= session
->ust_session
;
1893 if (validate_ust_event_name(event_name
)) {
1894 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1899 * If a non-default channel has been created in the
1900 * session, explicitly require that -c chan_name needs
1903 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1904 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1908 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1910 if (uchan
== NULL
) {
1911 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1915 switch (event
->type
) {
1916 case LTTNG_EVENT_ALL
:
1918 * An empty event name means that everything
1919 * should be disabled.
1921 if (event
->name
[0] == '\0') {
1922 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1924 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1927 if (ret
!= LTTNG_OK
) {
1932 ret
= LTTNG_ERR_UNK
;
1936 DBG3("Disable UST event %s in channel %s completed", event_name
,
1940 case LTTNG_DOMAIN_LOG4J
:
1941 case LTTNG_DOMAIN_JUL
:
1942 case LTTNG_DOMAIN_PYTHON
:
1945 struct ltt_ust_session
*usess
= session
->ust_session
;
1949 switch (event
->type
) {
1950 case LTTNG_EVENT_ALL
:
1953 ret
= LTTNG_ERR_UNK
;
1957 agt
= trace_ust_find_agent(usess
, domain
);
1959 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1963 * An empty event name means that everything
1964 * should be disabled.
1966 if (event
->name
[0] == '\0') {
1967 ret
= event_agent_disable_all(usess
, agt
);
1969 ret
= event_agent_disable(usess
, agt
, event_name
);
1971 if (ret
!= LTTNG_OK
) {
1978 ret
= LTTNG_ERR_UND
;
1991 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1993 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1994 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1996 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1997 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
2000 * Don't try to add a context if the session has been started at
2001 * some point in time before. The tracer does not allow it and would
2002 * result in a corrupted trace.
2004 if (session
->has_been_started
) {
2005 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2009 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2010 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
2011 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
2015 case LTTNG_DOMAIN_KERNEL
:
2016 assert(session
->kernel_session
);
2018 if (session
->kernel_session
->channel_count
== 0) {
2019 /* Create default channel */
2020 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
2021 if (ret
!= LTTNG_OK
) {
2024 chan_kern_created
= 1;
2026 /* Add kernel context to kernel tracer */
2027 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
2028 if (ret
!= LTTNG_OK
) {
2032 case LTTNG_DOMAIN_JUL
:
2033 case LTTNG_DOMAIN_LOG4J
:
2036 * Validate channel name.
2037 * If no channel name is given and the domain is JUL or LOG4J,
2038 * set it to the appropriate domain-specific channel name. If
2039 * a name is provided but does not match the expexted channel
2040 * name, return an error.
2042 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
2043 strcmp(channel_name
,
2044 DEFAULT_JUL_CHANNEL_NAME
)) {
2045 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2047 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
2048 strcmp(channel_name
,
2049 DEFAULT_LOG4J_CHANNEL_NAME
)) {
2050 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2053 /* break is _not_ missing here. */
2055 case LTTNG_DOMAIN_UST
:
2057 struct ltt_ust_session
*usess
= session
->ust_session
;
2058 unsigned int chan_count
;
2062 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2063 if (chan_count
== 0) {
2064 struct lttng_channel
*attr
;
2065 /* Create default channel */
2066 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2068 ret
= LTTNG_ERR_FATAL
;
2072 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2073 if (ret
!= LTTNG_OK
) {
2077 channel_attr_destroy(attr
);
2078 chan_ust_created
= 1;
2081 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2082 free(app_ctx_provider_name
);
2084 app_ctx_name
= NULL
;
2085 app_ctx_provider_name
= NULL
;
2086 if (ret
!= LTTNG_OK
) {
2092 ret
= LTTNG_ERR_UND
;
2100 if (chan_kern_created
) {
2101 struct ltt_kernel_channel
*kchan
=
2102 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2103 session
->kernel_session
);
2104 /* Created previously, this should NOT fail. */
2106 kernel_destroy_channel(kchan
);
2109 if (chan_ust_created
) {
2110 struct ltt_ust_channel
*uchan
=
2111 trace_ust_find_channel_by_name(
2112 session
->ust_session
->domain_global
.channels
,
2113 DEFAULT_CHANNEL_NAME
);
2114 /* Created previously, this should NOT fail. */
2116 /* Remove from the channel list of the session. */
2117 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2119 trace_ust_destroy_channel(uchan
);
2122 free(app_ctx_provider_name
);
2127 static inline bool name_starts_with(const char *name
, const char *prefix
)
2129 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2131 return !strncmp(name
, prefix
, max_cmp_len
);
2134 /* Perform userspace-specific event name validation */
2135 static int validate_ust_event_name(const char *name
)
2145 * Check name against all internal UST event component namespaces used
2148 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2149 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2150 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2159 * Internal version of cmd_enable_event() with a supplemental
2160 * "internal_event" flag which is used to enable internal events which should
2161 * be hidden from clients. Such events are used in the agent implementation to
2162 * enable the events through which all "agent" events are funeled.
2164 static int _cmd_enable_event(struct ltt_session
*session
,
2165 const struct lttng_domain
*domain
,
2166 char *channel_name
, struct lttng_event
*event
,
2167 char *filter_expression
,
2168 struct lttng_filter_bytecode
*filter
,
2169 struct lttng_event_exclusion
*exclusion
,
2170 int wpipe
, bool internal_event
)
2172 int ret
= 0, channel_created
= 0;
2173 struct lttng_channel
*attr
= NULL
;
2177 assert(channel_name
);
2179 /* If we have a filter, we must have its filter expression */
2180 assert(!(!!filter_expression
^ !!filter
));
2182 /* Normalize event name as a globbing pattern */
2183 strutils_normalize_star_glob_pattern(event
->name
);
2185 /* Normalize exclusion names as globbing patterns */
2189 for (i
= 0; i
< exclusion
->count
; i
++) {
2190 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2192 strutils_normalize_star_glob_pattern(name
);
2196 DBG("Enable event command for event \'%s\'", event
->name
);
2200 switch (domain
->type
) {
2201 case LTTNG_DOMAIN_KERNEL
:
2203 struct ltt_kernel_channel
*kchan
;
2206 * If a non-default channel has been created in the
2207 * session, explicitely require that -c chan_name needs
2210 if (session
->kernel_session
->has_non_default_channel
2211 && channel_name
[0] == '\0') {
2212 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2216 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2217 session
->kernel_session
);
2218 if (kchan
== NULL
) {
2219 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2220 LTTNG_BUFFER_GLOBAL
);
2222 ret
= LTTNG_ERR_FATAL
;
2225 if (lttng_strncpy(attr
->name
, channel_name
,
2226 sizeof(attr
->name
))) {
2227 ret
= LTTNG_ERR_INVALID
;
2231 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2232 if (ret
!= LTTNG_OK
) {
2235 channel_created
= 1;
2238 /* Get the newly created kernel channel pointer */
2239 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2240 session
->kernel_session
);
2241 if (kchan
== NULL
) {
2242 /* This sould not happen... */
2243 ret
= LTTNG_ERR_FATAL
;
2247 switch (event
->type
) {
2248 case LTTNG_EVENT_ALL
:
2250 char *filter_expression_a
= NULL
;
2251 struct lttng_filter_bytecode
*filter_a
= NULL
;
2254 * We need to duplicate filter_expression and filter,
2255 * because ownership is passed to first enable
2258 if (filter_expression
) {
2259 filter_expression_a
= strdup(filter_expression
);
2260 if (!filter_expression_a
) {
2261 ret
= LTTNG_ERR_FATAL
;
2266 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2268 free(filter_expression_a
);
2269 ret
= LTTNG_ERR_FATAL
;
2272 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2274 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2275 ret
= event_kernel_enable_event(kchan
, event
,
2276 filter_expression
, filter
);
2277 /* We have passed ownership */
2278 filter_expression
= NULL
;
2280 if (ret
!= LTTNG_OK
) {
2281 if (channel_created
) {
2282 /* Let's not leak a useless channel. */
2283 kernel_destroy_channel(kchan
);
2285 free(filter_expression_a
);
2289 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2290 ret
= event_kernel_enable_event(kchan
, event
,
2291 filter_expression_a
, filter_a
);
2292 /* We have passed ownership */
2293 filter_expression_a
= NULL
;
2295 if (ret
!= LTTNG_OK
) {
2300 case LTTNG_EVENT_PROBE
:
2301 case LTTNG_EVENT_USERSPACE_PROBE
:
2302 case LTTNG_EVENT_FUNCTION
:
2303 case LTTNG_EVENT_FUNCTION_ENTRY
:
2304 case LTTNG_EVENT_TRACEPOINT
:
2305 ret
= event_kernel_enable_event(kchan
, event
,
2306 filter_expression
, filter
);
2307 /* We have passed ownership */
2308 filter_expression
= NULL
;
2310 if (ret
!= LTTNG_OK
) {
2311 if (channel_created
) {
2312 /* Let's not leak a useless channel. */
2313 kernel_destroy_channel(kchan
);
2318 case LTTNG_EVENT_SYSCALL
:
2319 ret
= event_kernel_enable_event(kchan
, event
,
2320 filter_expression
, filter
);
2321 /* We have passed ownership */
2322 filter_expression
= NULL
;
2324 if (ret
!= LTTNG_OK
) {
2329 ret
= LTTNG_ERR_UNK
;
2333 kernel_wait_quiescent();
2336 case LTTNG_DOMAIN_UST
:
2338 struct ltt_ust_channel
*uchan
;
2339 struct ltt_ust_session
*usess
= session
->ust_session
;
2344 * If a non-default channel has been created in the
2345 * session, explicitely require that -c chan_name needs
2348 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2349 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2353 /* Get channel from global UST domain */
2354 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2356 if (uchan
== NULL
) {
2357 /* Create default channel */
2358 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2359 usess
->buffer_type
);
2361 ret
= LTTNG_ERR_FATAL
;
2364 if (lttng_strncpy(attr
->name
, channel_name
,
2365 sizeof(attr
->name
))) {
2366 ret
= LTTNG_ERR_INVALID
;
2370 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2371 if (ret
!= LTTNG_OK
) {
2375 /* Get the newly created channel reference back */
2376 uchan
= trace_ust_find_channel_by_name(
2377 usess
->domain_global
.channels
, channel_name
);
2381 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2383 * Don't allow users to add UST events to channels which
2384 * are assigned to a userspace subdomain (JUL, Log4J,
2387 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2391 if (!internal_event
) {
2393 * Ensure the event name is not reserved for internal
2396 ret
= validate_ust_event_name(event
->name
);
2398 WARN("Userspace event name %s failed validation.",
2400 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2405 /* At this point, the session and channel exist on the tracer */
2406 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2407 filter_expression
, filter
, exclusion
,
2409 /* We have passed ownership */
2410 filter_expression
= NULL
;
2413 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2414 goto already_enabled
;
2415 } else if (ret
!= LTTNG_OK
) {
2420 case LTTNG_DOMAIN_LOG4J
:
2421 case LTTNG_DOMAIN_JUL
:
2422 case LTTNG_DOMAIN_PYTHON
:
2424 const char *default_event_name
, *default_chan_name
;
2426 struct lttng_event uevent
;
2427 struct lttng_domain tmp_dom
;
2428 struct ltt_ust_session
*usess
= session
->ust_session
;
2432 if (!agent_tracing_is_enabled()) {
2433 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2434 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2438 agt
= trace_ust_find_agent(usess
, domain
->type
);
2440 agt
= agent_create(domain
->type
);
2442 ret
= LTTNG_ERR_NOMEM
;
2445 agent_add(agt
, usess
->agents
);
2448 /* Create the default tracepoint. */
2449 memset(&uevent
, 0, sizeof(uevent
));
2450 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2451 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2452 default_event_name
= event_get_default_agent_ust_name(
2454 if (!default_event_name
) {
2455 ret
= LTTNG_ERR_FATAL
;
2458 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2459 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2462 * The domain type is changed because we are about to enable the
2463 * default channel and event for the JUL domain that are hardcoded.
2464 * This happens in the UST domain.
2466 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2467 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2469 switch (domain
->type
) {
2470 case LTTNG_DOMAIN_LOG4J
:
2471 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2473 case LTTNG_DOMAIN_JUL
:
2474 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2476 case LTTNG_DOMAIN_PYTHON
:
2477 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2480 /* The switch/case we are in makes this impossible */
2485 char *filter_expression_copy
= NULL
;
2486 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2489 const size_t filter_size
= sizeof(
2490 struct lttng_filter_bytecode
)
2493 filter_copy
= zmalloc(filter_size
);
2495 ret
= LTTNG_ERR_NOMEM
;
2498 memcpy(filter_copy
, filter
, filter_size
);
2500 filter_expression_copy
=
2501 strdup(filter_expression
);
2502 if (!filter_expression
) {
2503 ret
= LTTNG_ERR_NOMEM
;
2506 if (!filter_expression_copy
|| !filter_copy
) {
2507 free(filter_expression_copy
);
2513 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2514 (char *) default_chan_name
,
2515 &uevent
, filter_expression_copy
,
2516 filter_copy
, NULL
, wpipe
);
2519 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2520 goto already_enabled
;
2521 } else if (ret
!= LTTNG_OK
) {
2525 /* The wild card * means that everything should be enabled. */
2526 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2527 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2530 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2534 filter_expression
= NULL
;
2535 if (ret
!= LTTNG_OK
) {
2542 ret
= LTTNG_ERR_UND
;
2550 free(filter_expression
);
2553 channel_attr_destroy(attr
);
2559 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2560 * We own filter, exclusion, and filter_expression.
2562 int cmd_enable_event(struct ltt_session
*session
,
2563 const struct lttng_domain
*domain
,
2564 char *channel_name
, struct lttng_event
*event
,
2565 char *filter_expression
,
2566 struct lttng_filter_bytecode
*filter
,
2567 struct lttng_event_exclusion
*exclusion
,
2570 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2571 filter_expression
, filter
, exclusion
, wpipe
, false);
2575 * Enable an event which is internal to LTTng. An internal should
2576 * never be made visible to clients and are immune to checks such as
2579 static int cmd_enable_event_internal(struct ltt_session
*session
,
2580 const struct lttng_domain
*domain
,
2581 char *channel_name
, struct lttng_event
*event
,
2582 char *filter_expression
,
2583 struct lttng_filter_bytecode
*filter
,
2584 struct lttng_event_exclusion
*exclusion
,
2587 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2588 filter_expression
, filter
, exclusion
, wpipe
, true);
2592 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2594 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2595 struct lttng_event
**events
)
2598 ssize_t nb_events
= 0;
2601 case LTTNG_DOMAIN_KERNEL
:
2602 nb_events
= kernel_list_events(events
);
2603 if (nb_events
< 0) {
2604 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2608 case LTTNG_DOMAIN_UST
:
2609 nb_events
= ust_app_list_events(events
);
2610 if (nb_events
< 0) {
2611 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2615 case LTTNG_DOMAIN_LOG4J
:
2616 case LTTNG_DOMAIN_JUL
:
2617 case LTTNG_DOMAIN_PYTHON
:
2618 nb_events
= agent_list_events(events
, domain
);
2619 if (nb_events
< 0) {
2620 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2625 ret
= LTTNG_ERR_UND
;
2632 /* Return negative value to differentiate return code */
2637 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2639 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2640 struct lttng_event_field
**fields
)
2643 ssize_t nb_fields
= 0;
2646 case LTTNG_DOMAIN_UST
:
2647 nb_fields
= ust_app_list_event_fields(fields
);
2648 if (nb_fields
< 0) {
2649 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2653 case LTTNG_DOMAIN_KERNEL
:
2654 default: /* fall-through */
2655 ret
= LTTNG_ERR_UND
;
2662 /* Return negative value to differentiate return code */
2666 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2668 return syscall_table_list(events
);
2672 * Command LTTNG_START_TRACE processed by the client thread.
2674 * Called with session mutex held.
2676 int cmd_start_trace(struct ltt_session
*session
)
2678 enum lttng_error_code ret
;
2679 unsigned long nb_chan
= 0;
2680 struct ltt_kernel_session
*ksession
;
2681 struct ltt_ust_session
*usess
;
2682 const bool session_rotated_after_last_stop
=
2683 session
->rotated_after_last_stop
;
2684 const bool session_cleared_after_last_stop
=
2685 session
->cleared_after_last_stop
;
2689 /* Ease our life a bit ;) */
2690 ksession
= session
->kernel_session
;
2691 usess
= session
->ust_session
;
2693 /* Is the session already started? */
2694 if (session
->active
) {
2695 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2696 /* Perform nothing */
2700 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2701 !session
->current_trace_chunk
) {
2703 * A rotation was launched while the session was stopped and
2704 * it has not been completed yet. It is not possible to start
2705 * the session since starting the session here would require a
2706 * rotation from "NULL" to a new trace chunk. That rotation
2707 * would overlap with the ongoing rotation, which is not
2710 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2712 ret
= LTTNG_ERR_ROTATION_PENDING
;
2717 * Starting a session without channel is useless since after that it's not
2718 * possible to enable channel thus inform the client.
2720 if (usess
&& usess
->domain_global
.channels
) {
2721 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2724 nb_chan
+= ksession
->channel_count
;
2727 ret
= LTTNG_ERR_NO_CHANNEL
;
2731 session
->active
= 1;
2732 session
->rotated_after_last_stop
= false;
2733 session
->cleared_after_last_stop
= false;
2734 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2735 if (!session
->has_been_started
) {
2736 struct lttng_trace_chunk
*trace_chunk
;
2738 DBG("Creating initial trace chunk of session \"%s\"",
2740 trace_chunk
= session_create_new_trace_chunk(
2741 session
, NULL
, NULL
, NULL
);
2743 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2746 assert(!session
->current_trace_chunk
);
2747 ret
= session_set_trace_chunk(session
, trace_chunk
,
2749 lttng_trace_chunk_put(trace_chunk
);
2751 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2755 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2758 * Rotate existing streams into the new chunk.
2759 * This is a "quiet" rotation has no client has
2760 * explicitly requested this operation.
2762 * There is also no need to wait for the rotation
2763 * to complete as it will happen immediately. No data
2764 * was produced as the session was stopped, so the
2765 * rotation should happen on reception of the command.
2767 ret
= cmd_rotate_session(session
, NULL
, true,
2768 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2769 if (ret
!= LTTNG_OK
) {
2775 /* Kernel tracing */
2776 if (ksession
!= NULL
) {
2777 DBG("Start kernel tracing session %s", session
->name
);
2778 ret
= start_kernel_session(ksession
);
2779 if (ret
!= LTTNG_OK
) {
2784 /* Flag session that trace should start automatically */
2786 int int_ret
= ust_app_start_trace_all(usess
);
2789 ret
= LTTNG_ERR_UST_START_FAIL
;
2795 * Clear the flag that indicates that a rotation was done while the
2796 * session was stopped.
2798 session
->rotated_after_last_stop
= false;
2800 if (session
->rotate_timer_period
) {
2801 int int_ret
= timer_session_rotation_schedule_timer_start(
2802 session
, session
->rotate_timer_period
);
2805 ERR("Failed to enable rotate timer");
2806 ret
= LTTNG_ERR_UNK
;
2814 if (ret
== LTTNG_OK
) {
2815 /* Flag this after a successful start. */
2816 session
->has_been_started
|= 1;
2818 session
->active
= 0;
2819 /* Restore initial state on error. */
2820 session
->rotated_after_last_stop
=
2821 session_rotated_after_last_stop
;
2822 session
->cleared_after_last_stop
=
2823 session_cleared_after_last_stop
;
2830 * Command LTTNG_STOP_TRACE processed by the client thread.
2832 int cmd_stop_trace(struct ltt_session
*session
)
2835 struct ltt_kernel_session
*ksession
;
2836 struct ltt_ust_session
*usess
;
2840 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2842 ksession
= session
->kernel_session
;
2843 usess
= session
->ust_session
;
2845 /* Session is not active. Skip everythong and inform the client. */
2846 if (!session
->active
) {
2847 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2851 ret
= stop_kernel_session(ksession
);
2852 if (ret
!= LTTNG_OK
) {
2856 if (usess
&& usess
->active
) {
2857 ret
= ust_app_stop_trace_all(usess
);
2859 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2864 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2866 /* Flag inactive after a successful stop. */
2867 session
->active
= 0;
2875 * Set the base_path of the session only if subdir of a control uris is set.
2876 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2878 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2880 struct lttng_uri
*uris
)
2885 for (i
= 0; i
< nb_uri
; i
++) {
2886 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2887 uris
[i
].subdir
[0] == '\0') {
2888 /* Not interested in these URIs */
2892 if (session
->base_path
!= NULL
) {
2893 free(session
->base_path
);
2894 session
->base_path
= NULL
;
2897 /* Set session base_path */
2898 session
->base_path
= strdup(uris
[i
].subdir
);
2899 if (!session
->base_path
) {
2900 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2901 uris
[i
].subdir
, session
->name
);
2902 ret
= LTTNG_ERR_NOMEM
;
2905 DBG2("Setting base path \"%s\" for session \"%s\"",
2906 session
->base_path
, session
->name
);
2914 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2916 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2917 struct lttng_uri
*uris
)
2920 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2921 struct ltt_ust_session
*usess
= session
->ust_session
;
2927 /* Can't set consumer URI if the session is active. */
2928 if (session
->active
) {
2929 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2934 * Set the session base path if any. This is done inside
2935 * cmd_set_consumer_uri to preserve backward compatibility of the
2936 * previous session creation api vs the session descriptor api.
2938 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2939 if (ret
!= LTTNG_OK
) {
2943 /* Set the "global" consumer URIs */
2944 for (i
= 0; i
< nb_uri
; i
++) {
2945 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2947 if (ret
!= LTTNG_OK
) {
2952 /* Set UST session URIs */
2953 if (session
->ust_session
) {
2954 for (i
= 0; i
< nb_uri
; i
++) {
2955 ret
= add_uri_to_consumer(session
,
2956 session
->ust_session
->consumer
,
2957 &uris
[i
], LTTNG_DOMAIN_UST
);
2958 if (ret
!= LTTNG_OK
) {
2964 /* Set kernel session URIs */
2965 if (session
->kernel_session
) {
2966 for (i
= 0; i
< nb_uri
; i
++) {
2967 ret
= add_uri_to_consumer(session
,
2968 session
->kernel_session
->consumer
,
2969 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2970 if (ret
!= LTTNG_OK
) {
2977 * Make sure to set the session in output mode after we set URI since a
2978 * session can be created without URL (thus flagged in no output mode).
2980 session
->output_traces
= 1;
2982 ksess
->output_traces
= 1;
2986 usess
->output_traces
= 1;
2997 enum lttng_error_code
set_session_output_from_descriptor(
2998 struct ltt_session
*session
,
2999 const struct lttng_session_descriptor
*descriptor
)
3002 enum lttng_error_code ret_code
= LTTNG_OK
;
3003 enum lttng_session_descriptor_type session_type
=
3004 lttng_session_descriptor_get_type(descriptor
);
3005 enum lttng_session_descriptor_output_type output_type
=
3006 lttng_session_descriptor_get_output_type(descriptor
);
3007 struct lttng_uri uris
[2] = {};
3008 size_t uri_count
= 0;
3010 switch (output_type
) {
3011 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3013 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3014 lttng_session_descriptor_get_local_output_uri(descriptor
,
3018 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3019 lttng_session_descriptor_get_network_output_uris(descriptor
,
3020 &uris
[0], &uris
[1]);
3024 ret_code
= LTTNG_ERR_INVALID
;
3028 switch (session_type
) {
3029 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3031 struct snapshot_output
*new_output
= NULL
;
3033 new_output
= snapshot_output_alloc();
3035 ret_code
= LTTNG_ERR_NOMEM
;
3039 ret
= snapshot_output_init_with_uri(session
,
3040 DEFAULT_SNAPSHOT_MAX_SIZE
,
3041 NULL
, uris
, uri_count
, session
->consumer
,
3042 new_output
, &session
->snapshot
);
3044 ret_code
= (ret
== -ENOMEM
) ?
3045 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3046 snapshot_output_destroy(new_output
);
3049 snapshot_add_output(&session
->snapshot
, new_output
);
3052 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3053 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3055 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3059 ret_code
= LTTNG_ERR_INVALID
;
3067 enum lttng_error_code
cmd_create_session_from_descriptor(
3068 struct lttng_session_descriptor
*descriptor
,
3069 const lttng_sock_cred
*creds
,
3070 const char *home_path
)
3073 enum lttng_error_code ret_code
;
3074 const char *session_name
;
3075 struct ltt_session
*new_session
= NULL
;
3076 enum lttng_session_descriptor_status descriptor_status
;
3078 session_lock_list();
3080 if (*home_path
!= '/') {
3081 ERR("Home path provided by client is not absolute");
3082 ret_code
= LTTNG_ERR_INVALID
;
3087 descriptor_status
= lttng_session_descriptor_get_session_name(
3088 descriptor
, &session_name
);
3089 switch (descriptor_status
) {
3090 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3092 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3093 session_name
= NULL
;
3096 ret_code
= LTTNG_ERR_INVALID
;
3100 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3102 if (ret_code
!= LTTNG_OK
) {
3106 if (!session_name
) {
3107 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3110 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3115 if (!lttng_session_descriptor_is_output_destination_initialized(
3118 * Only include the session's creation time in the output
3119 * destination if the name of the session itself was
3120 * not auto-generated.
3122 ret_code
= lttng_session_descriptor_set_default_output(
3124 session_name
? &new_session
->creation_time
: NULL
,
3126 if (ret_code
!= LTTNG_OK
) {
3130 new_session
->has_user_specified_directory
=
3131 lttng_session_descriptor_has_output_directory(
3135 switch (lttng_session_descriptor_get_type(descriptor
)) {
3136 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3137 new_session
->snapshot_mode
= 1;
3139 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3140 new_session
->live_timer
=
3141 lttng_session_descriptor_live_get_timer_interval(
3148 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3149 if (ret_code
!= LTTNG_OK
) {
3152 new_session
->consumer
->enabled
= 1;
3153 ret_code
= LTTNG_OK
;
3155 /* Release reference provided by the session_create function. */
3156 session_put(new_session
);
3157 if (ret_code
!= LTTNG_OK
&& new_session
) {
3158 /* Release the global reference on error. */
3159 session_destroy(new_session
);
3161 session_unlock_list();
3165 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3166 struct lttng_session_descriptor
**return_descriptor
)
3169 size_t payload_size
;
3170 struct lttng_dynamic_buffer payload
;
3171 struct lttng_buffer_view home_dir_view
;
3172 struct lttng_buffer_view session_descriptor_view
;
3173 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3174 enum lttng_error_code ret_code
;
3176 lttng_dynamic_buffer_init(&payload
);
3177 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3179 ret_code
= LTTNG_ERR_INVALID
;
3182 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3183 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3184 ret_code
= LTTNG_ERR_INVALID
;
3188 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3189 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3190 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3192 ret_code
= LTTNG_ERR_NOMEM
;
3196 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3198 ERR("Reception of session descriptor failed, aborting.");
3199 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3203 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3206 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3207 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3209 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3210 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3212 ret
= lttng_session_descriptor_create_from_buffer(
3213 &session_descriptor_view
, &session_descriptor
);
3215 ERR("Failed to create session descriptor from payload of \"create session\" command");
3216 ret_code
= LTTNG_ERR_INVALID
;
3221 * Sets the descriptor's auto-generated properties (name, output) if
3224 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3226 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3227 if (ret_code
!= LTTNG_OK
) {
3231 ret_code
= LTTNG_OK
;
3232 *return_descriptor
= session_descriptor
;
3233 session_descriptor
= NULL
;
3235 lttng_dynamic_buffer_reset(&payload
);
3236 lttng_session_descriptor_destroy(session_descriptor
);
3241 void cmd_destroy_session_reply(const struct ltt_session
*session
,