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
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
18 #include <common/buffer-view.h>
19 #include <common/common.h>
20 #include <common/compat/string.h>
21 #include <common/defaults.h>
22 #include <common/dynamic-buffer.h>
23 #include <common/kernel-ctl/kernel-ctl.h>
24 #include <common/payload-view.h>
25 #include <common/payload.h>
26 #include <common/relayd/relayd.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/string-utils/string-utils.h>
29 #include <common/trace-chunk.h>
30 #include <common/utils.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/action/action.h>
33 #include <lttng/channel-internal.h>
34 #include <lttng/channel.h>
35 #include <lttng/condition/condition-internal.h>
36 #include <lttng/condition/condition.h>
37 #include <lttng/condition/event-rule-matches-internal.h>
38 #include <lttng/condition/event-rule-matches.h>
39 #include <lttng/error-query-internal.h>
40 #include <lttng/event-rule/event-rule-internal.h>
41 #include <lttng/event-rule/event-rule.h>
42 #include <lttng/location-internal.h>
43 #include <lttng/lttng-error.h>
44 #include <lttng/rotate-internal.h>
45 #include <lttng/session-descriptor-internal.h>
46 #include <lttng/session-internal.h>
47 #include <lttng/tracker.h>
48 #include <lttng/trigger/trigger-internal.h>
49 #include <lttng/userspace-probe-internal.h>
51 #include "agent-thread.h"
53 #include "buffer-registry.h"
57 #include "event-notifier-error-accounting.h"
59 #include "health-sessiond.h"
60 #include "kernel-consumer.h"
62 #include "lttng-sessiond.h"
63 #include "lttng-syscall.h"
64 #include "notification-thread-commands.h"
65 #include "notification-thread.h"
67 #include "rotation-thread.h"
73 /* Sleep for 100ms between each check for the shm path's deletion. */
74 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
76 struct cmd_destroy_session_reply_context
{
78 bool implicit_rotation_on_destroy
;
80 * Indicates whether or not an error occurred while launching the
81 * destruction of a session.
83 enum lttng_error_code destruction_status
;
86 static enum lttng_error_code
wait_on_path(void *path
);
89 * Command completion handler that is used by the destroy command
90 * when a session that has a non-default shm_path is being destroyed.
92 * See comment in cmd_destroy_session() for the rationale.
94 static struct destroy_completion_handler
{
95 struct cmd_completion_handler handler
;
96 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
97 } destroy_completion_handler
= {
100 .data
= destroy_completion_handler
.shm_path
105 static struct cmd_completion_handler
*current_completion_handler
;
108 * Used to keep a unique index for each relayd socket created where this value
109 * is associated with streams on the consumer so it can match the right relayd
110 * to send to. It must be accessed with the relayd_net_seq_idx_lock
113 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
114 static uint64_t relayd_net_seq_idx
;
116 static int validate_ust_event_name(const char *);
117 static int cmd_enable_event_internal(struct ltt_session
*session
,
118 const struct lttng_domain
*domain
,
119 char *channel_name
, struct lttng_event
*event
,
120 char *filter_expression
,
121 struct lttng_bytecode
*filter
,
122 struct lttng_event_exclusion
*exclusion
,
124 static int cmd_enable_channel_internal(struct ltt_session
*session
,
125 const struct lttng_domain
*domain
,
126 const struct lttng_channel
*_attr
,
130 * Create a session path used by list_lttng_sessions for the case that the
131 * session consumer is on the network.
133 static int build_network_session_path(char *dst
, size_t size
,
134 struct ltt_session
*session
)
136 int ret
, kdata_port
, udata_port
;
137 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
138 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
140 LTTNG_ASSERT(session
);
143 memset(tmp_urls
, 0, sizeof(tmp_urls
));
144 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
146 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
148 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
149 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
150 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
153 if (session
->ust_session
&& session
->ust_session
->consumer
) {
154 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
155 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
158 if (uuri
== NULL
&& kuri
== NULL
) {
159 uri
= &session
->consumer
->dst
.net
.control
;
160 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
161 } else if (kuri
&& uuri
) {
162 ret
= uri_compare(kuri
, uuri
);
166 /* Build uuri URL string */
167 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
174 } else if (kuri
&& uuri
== NULL
) {
176 } else if (uuri
&& kuri
== NULL
) {
180 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
186 * Do we have a UST url set. If yes, this means we have both kernel and UST
189 if (*tmp_uurl
!= '\0') {
190 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
191 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
194 if (kuri
|| (!kuri
&& !uuri
)) {
197 /* No kernel URI, use the UST port. */
200 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
208 * Get run-time attributes if the session has been started (discarded events,
211 static int get_kernel_runtime_stats(struct ltt_session
*session
,
212 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
213 uint64_t *lost_packets
)
217 if (!session
->has_been_started
) {
219 *discarded_events
= 0;
224 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
225 session
->kernel_session
->consumer
,
231 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
232 session
->kernel_session
->consumer
,
243 * Get run-time attributes if the session has been started (discarded events,
246 static int get_ust_runtime_stats(struct ltt_session
*session
,
247 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
248 uint64_t *lost_packets
)
251 struct ltt_ust_session
*usess
;
253 if (!discarded_events
|| !lost_packets
) {
258 usess
= session
->ust_session
;
259 LTTNG_ASSERT(discarded_events
);
260 LTTNG_ASSERT(lost_packets
);
262 if (!usess
|| !session
->has_been_started
) {
263 *discarded_events
= 0;
269 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
270 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
271 &usess
->buffer_reg_uid_list
,
272 usess
->consumer
, uchan
->id
,
273 uchan
->attr
.overwrite
,
276 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
277 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
278 uchan
, usess
->consumer
,
279 uchan
->attr
.overwrite
,
285 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
286 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
288 ERR("Unsupported buffer type");
298 static int append_extended_info(const char *filter_expression
,
299 struct lttng_event_exclusion
*exclusion
,
300 struct lttng_userspace_probe_location
*probe_location
,
301 struct lttng_payload
*payload
)
304 size_t filter_len
= 0;
305 size_t nb_exclusions
= 0;
306 size_t userspace_probe_location_len
= 0;
307 struct lttcomm_event_extended_header extended_header
= {};
308 struct lttcomm_event_extended_header
*p_extended_header
;
309 const size_t original_payload_size
= payload
->buffer
.size
;
311 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
312 sizeof(extended_header
));
317 if (filter_expression
) {
318 filter_len
= strlen(filter_expression
) + 1;
319 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
320 filter_expression
, filter_len
);
327 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
329 nb_exclusions
= exclusion
->count
;
331 ret
= lttng_dynamic_buffer_append(
332 &payload
->buffer
, &exclusion
->names
, len
);
338 if (probe_location
) {
339 const size_t size_before_probe
= payload
->buffer
.size
;
341 ret
= lttng_userspace_probe_location_serialize(probe_location
,
348 userspace_probe_location_len
=
349 payload
->buffer
.size
- size_before_probe
;
352 /* Set header fields */
353 p_extended_header
= (struct lttcomm_event_extended_header
*)
354 (payload
->buffer
.data
+ original_payload_size
);
356 p_extended_header
->filter_len
= filter_len
;
357 p_extended_header
->nb_exclusions
= nb_exclusions
;
358 p_extended_header
->userspace_probe_location_len
=
359 userspace_probe_location_len
;
367 * Create a list of agent domain events.
369 * Return number of events in list on success or else a negative value.
371 static int list_lttng_agent_events(struct agent
*agt
,
372 struct lttng_payload
*payload
)
374 int nb_events
= 0, ret
= 0;
375 const struct agent_event
*agent_event
;
376 struct lttng_ht_iter iter
;
380 DBG3("Listing agent events");
383 cds_lfht_for_each_entry (
384 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
385 struct lttng_event event
{};
387 event
.loglevel_type
= agent_event
->loglevel_type
;
388 event
.loglevel
= agent_event
->loglevel_value
;
389 event
.enabled
= AGENT_EVENT_IS_ENABLED(agent_event
);
391 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
393 /* Internal error, invalid name. */
394 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
395 agent_event
->name
, sizeof(event
.name
));
396 ret
= -LTTNG_ERR_UNK
;
400 ret
= lttng_dynamic_buffer_append(
401 &payload
->buffer
, &event
, sizeof(event
));
403 ERR("Failed to append event to payload");
404 ret
= -LTTNG_ERR_NOMEM
;
411 cds_lfht_for_each_entry (
412 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
413 /* Append extended info. */
414 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
417 ERR("Failed to append extended event info to payload");
418 ret
= -LTTNG_ERR_NOMEM
;
430 * Create a list of ust global domain events.
432 static int list_lttng_ust_global_events(char *channel_name
,
433 struct ltt_ust_domain_global
*ust_global
,
434 struct lttng_payload
*payload
)
437 unsigned int nb_events
= 0;
438 struct lttng_ht_iter iter
;
439 const struct lttng_ht_node_str
*node
;
440 const struct ltt_ust_channel
*uchan
;
441 const struct ltt_ust_event
*uevent
;
443 DBG("Listing UST global events for channel %s", channel_name
);
447 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
448 node
= lttng_ht_iter_get_node_str(&iter
);
450 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
454 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
456 DBG3("Listing UST global events");
458 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
459 struct lttng_event event
= {};
461 if (uevent
->internal
) {
465 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
467 /* Internal error, invalid name. */
468 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
469 uevent
->attr
.name
, sizeof(event
.name
));
470 ret
= -LTTNG_ERR_UNK
;
474 event
.enabled
= uevent
->enabled
;
476 switch (uevent
->attr
.instrumentation
) {
477 case LTTNG_UST_ABI_TRACEPOINT
:
478 event
.type
= LTTNG_EVENT_TRACEPOINT
;
480 case LTTNG_UST_ABI_PROBE
:
481 event
.type
= LTTNG_EVENT_PROBE
;
483 case LTTNG_UST_ABI_FUNCTION
:
484 event
.type
= LTTNG_EVENT_FUNCTION
;
488 event
.loglevel
= uevent
->attr
.loglevel
;
489 switch (uevent
->attr
.loglevel_type
) {
490 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
491 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
493 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
494 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
496 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
497 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
501 if (uevent
->filter
) {
505 if (uevent
->exclusion
) {
509 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
511 ERR("Failed to append event to payload");
512 ret
= -LTTNG_ERR_NOMEM
;
519 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
520 /* Append extended info. */
521 ret
= append_extended_info(uevent
->filter_expression
,
522 uevent
->exclusion
, NULL
, payload
);
524 ERR("Failed to append extended event info to payload");
525 ret
= -LTTNG_ERR_FATAL
;
537 * Fill lttng_event array of all kernel events in the channel.
539 static int list_lttng_kernel_events(char *channel_name
,
540 struct ltt_kernel_session
*kernel_session
,
541 struct lttng_payload
*payload
)
544 unsigned int nb_event
;
545 const struct ltt_kernel_event
*kevent
;
546 const struct ltt_kernel_channel
*kchan
;
548 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
550 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
554 nb_event
= kchan
->event_count
;
556 DBG("Listing events for channel %s", kchan
->channel
->name
);
558 /* Kernel channels */
559 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
560 struct lttng_event event
= {};
562 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
564 /* Internal error, invalid name. */
565 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
568 ret
= -LTTNG_ERR_UNK
;
572 event
.enabled
= kevent
->enabled
;
573 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
575 switch (kevent
->event
->instrumentation
) {
576 case LTTNG_KERNEL_ABI_TRACEPOINT
:
577 event
.type
= LTTNG_EVENT_TRACEPOINT
;
579 case LTTNG_KERNEL_ABI_KRETPROBE
:
580 event
.type
= LTTNG_EVENT_FUNCTION
;
581 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
582 sizeof(struct lttng_kernel_abi_kprobe
));
584 case LTTNG_KERNEL_ABI_KPROBE
:
585 event
.type
= LTTNG_EVENT_PROBE
;
586 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
587 sizeof(struct lttng_kernel_abi_kprobe
));
589 case LTTNG_KERNEL_ABI_UPROBE
:
590 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
592 case LTTNG_KERNEL_ABI_FUNCTION
:
593 event
.type
= LTTNG_EVENT_FUNCTION
;
594 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
595 sizeof(struct lttng_kernel_abi_function
));
597 case LTTNG_KERNEL_ABI_NOOP
:
598 event
.type
= LTTNG_EVENT_NOOP
;
600 case LTTNG_KERNEL_ABI_SYSCALL
:
601 event
.type
= LTTNG_EVENT_SYSCALL
;
603 case LTTNG_KERNEL_ABI_ALL
:
610 ret
= lttng_dynamic_buffer_append(
611 &payload
->buffer
, &event
, sizeof(event
));
613 ERR("Failed to append event to payload");
614 ret
= -LTTNG_ERR_NOMEM
;
619 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
620 /* Append extended info. */
621 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
622 kevent
->userspace_probe_location
, payload
);
624 DBG("Error appending extended info message");
625 ret
= -LTTNG_ERR_FATAL
;
637 * Add URI so the consumer output object. Set the correct path depending on the
638 * domain adding the default trace directory.
640 static enum lttng_error_code
add_uri_to_consumer(
641 const struct ltt_session
*session
,
642 struct consumer_output
*consumer
,
643 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
646 enum lttng_error_code ret_code
= LTTNG_OK
;
650 if (consumer
== NULL
) {
651 DBG("No consumer detected. Don't add URI. Stopping.");
652 ret_code
= LTTNG_ERR_NO_CONSUMER
;
657 case LTTNG_DOMAIN_KERNEL
:
658 ret
= lttng_strncpy(consumer
->domain_subdir
,
659 DEFAULT_KERNEL_TRACE_DIR
,
660 sizeof(consumer
->domain_subdir
));
662 case LTTNG_DOMAIN_UST
:
663 ret
= lttng_strncpy(consumer
->domain_subdir
,
664 DEFAULT_UST_TRACE_DIR
,
665 sizeof(consumer
->domain_subdir
));
669 * This case is possible is we try to add the URI to the global
670 * tracing session consumer object which in this case there is
673 memset(consumer
->domain_subdir
, 0,
674 sizeof(consumer
->domain_subdir
));
678 ERR("Failed to initialize consumer output domain subdirectory");
679 ret_code
= LTTNG_ERR_FATAL
;
683 switch (uri
->dtype
) {
686 DBG2("Setting network URI to consumer");
688 if (consumer
->type
== CONSUMER_DST_NET
) {
689 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
690 consumer
->dst
.net
.control_isset
) ||
691 (uri
->stype
== LTTNG_STREAM_DATA
&&
692 consumer
->dst
.net
.data_isset
)) {
693 ret_code
= LTTNG_ERR_URL_EXIST
;
697 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
700 /* Set URI into consumer output object */
701 ret
= consumer_set_network_uri(session
, consumer
, uri
);
703 ret_code
= (lttng_error_code
) -ret
;
705 } else if (ret
== 1) {
707 * URI was the same in the consumer so we do not append the subdir
708 * again so to not duplicate output dir.
715 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
716 ret_code
= LTTNG_ERR_INVALID
;
719 DBG2("Setting trace directory path from URI to %s",
721 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
723 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
725 sizeof(consumer
->dst
.session_root_path
));
727 ret_code
= LTTNG_ERR_FATAL
;
730 consumer
->type
= CONSUMER_DST_LOCAL
;
740 * Init tracing by creating trace directory and sending fds kernel consumer.
742 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
745 struct lttng_ht_iter iter
;
746 struct consumer_socket
*socket
;
748 LTTNG_ASSERT(session
);
752 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
753 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
755 pthread_mutex_lock(socket
->lock
);
756 ret
= kernel_consumer_send_session(socket
, session
);
757 pthread_mutex_unlock(socket
->lock
);
759 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
771 * Create a socket to the relayd using the URI.
773 * On success, the relayd_sock pointer is set to the created socket.
774 * Else, it remains untouched and an LTTng error code is returned.
776 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
777 struct lttcomm_relayd_sock
**relayd_sock
,
778 struct consumer_output
*consumer
)
781 enum lttng_error_code status
= LTTNG_OK
;
782 struct lttcomm_relayd_sock
*rsock
;
784 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
785 RELAYD_VERSION_COMM_MINOR
);
787 status
= LTTNG_ERR_FATAL
;
792 * Connect to relayd so we can proceed with a session creation. This call
793 * can possibly block for an arbitrary amount of time to set the health
794 * state to be in poll execution.
797 ret
= relayd_connect(rsock
);
800 ERR("Unable to reach lttng-relayd");
801 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
805 /* Create socket for control stream. */
806 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
807 uint64_t result_flags
;
809 DBG3("Creating relayd stream socket from URI");
811 /* Check relayd version */
812 ret
= relayd_version_check(rsock
);
813 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
814 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
816 } else if (ret
< 0) {
817 ERR("Unable to reach lttng-relayd");
818 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
821 consumer
->relay_major_version
= rsock
->major
;
822 consumer
->relay_minor_version
= rsock
->minor
;
823 ret
= relayd_get_configuration(rsock
, 0,
826 ERR("Unable to get relayd configuration");
827 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
830 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
831 consumer
->relay_allows_clear
= true;
833 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
834 DBG3("Creating relayd data socket from URI");
836 /* Command is not valid */
837 ERR("Relayd invalid stream type: %d", uri
->stype
);
838 status
= LTTNG_ERR_INVALID
;
842 *relayd_sock
= rsock
;
847 /* The returned value is not useful since we are on an error path. */
848 (void) relayd_close(rsock
);
856 * Connect to the relayd using URI and send the socket to the right consumer.
858 * The consumer socket lock must be held by the caller.
860 * Returns LTTNG_OK on success or an LTTng error code on failure.
862 static enum lttng_error_code
send_consumer_relayd_socket(
863 unsigned int session_id
,
864 struct lttng_uri
*relayd_uri
,
865 struct consumer_output
*consumer
,
866 struct consumer_socket
*consumer_sock
,
867 const char *session_name
, const char *hostname
,
868 const char *base_path
, int session_live_timer
,
869 const uint64_t *current_chunk_id
,
870 time_t session_creation_time
,
871 bool session_name_contains_creation_time
)
874 struct lttcomm_relayd_sock
*rsock
= NULL
;
875 enum lttng_error_code status
;
877 /* Connect to relayd and make version check if uri is the control. */
878 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
879 if (status
!= LTTNG_OK
) {
880 goto relayd_comm_error
;
884 /* Set the network sequence index if not set. */
885 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
886 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
888 * Increment net_seq_idx because we are about to transfer the
889 * new relayd socket to the consumer.
890 * Assign unique key so the consumer can match streams.
892 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
893 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
896 /* Send relayd socket to consumer. */
897 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
898 relayd_uri
->stype
, session_id
,
899 session_name
, hostname
, base_path
,
900 session_live_timer
, current_chunk_id
,
901 session_creation_time
, session_name_contains_creation_time
);
903 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
907 /* Flag that the corresponding socket was sent. */
908 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
909 consumer_sock
->control_sock_sent
= 1;
910 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
911 consumer_sock
->data_sock_sent
= 1;
915 * Close socket which was dup on the consumer side. The session daemon does
916 * NOT keep track of the relayd socket(s) once transfer to the consumer.
920 if (status
!= LTTNG_OK
) {
922 * The consumer output for this session should not be used anymore
923 * since the relayd connection failed thus making any tracing or/and
924 * streaming not usable.
926 consumer
->enabled
= 0;
928 (void) relayd_close(rsock
);
936 * Send both relayd sockets to a specific consumer and domain. This is a
937 * helper function to facilitate sending the information to the consumer for a
940 * The consumer socket lock must be held by the caller.
942 * Returns LTTNG_OK, or an LTTng error code on failure.
944 static enum lttng_error_code
send_consumer_relayd_sockets(
945 enum lttng_domain_type domain
,
946 unsigned int session_id
, struct consumer_output
*consumer
,
947 struct consumer_socket
*sock
, const char *session_name
,
948 const char *hostname
, const char *base_path
, int session_live_timer
,
949 const uint64_t *current_chunk_id
, time_t session_creation_time
,
950 bool session_name_contains_creation_time
)
952 enum lttng_error_code status
= LTTNG_OK
;
954 LTTNG_ASSERT(consumer
);
957 /* Sending control relayd socket. */
958 if (!sock
->control_sock_sent
) {
959 status
= send_consumer_relayd_socket(session_id
,
960 &consumer
->dst
.net
.control
, consumer
, sock
,
961 session_name
, hostname
, base_path
, session_live_timer
,
962 current_chunk_id
, session_creation_time
,
963 session_name_contains_creation_time
);
964 if (status
!= LTTNG_OK
) {
969 /* Sending data relayd socket. */
970 if (!sock
->data_sock_sent
) {
971 status
= send_consumer_relayd_socket(session_id
,
972 &consumer
->dst
.net
.data
, consumer
, sock
,
973 session_name
, hostname
, base_path
, session_live_timer
,
974 current_chunk_id
, session_creation_time
,
975 session_name_contains_creation_time
);
976 if (status
!= LTTNG_OK
) {
986 * Setup relayd connections for a tracing session. First creates the socket to
987 * the relayd and send them to the right domain consumer. Consumer type MUST be
990 int cmd_setup_relayd(struct ltt_session
*session
)
993 struct ltt_ust_session
*usess
;
994 struct ltt_kernel_session
*ksess
;
995 struct consumer_socket
*socket
;
996 struct lttng_ht_iter iter
;
997 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
999 LTTNG_ASSERT(session
);
1001 usess
= session
->ust_session
;
1002 ksess
= session
->kernel_session
;
1004 DBG("Setting relayd for session %s", session
->name
);
1007 if (session
->current_trace_chunk
) {
1008 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1009 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1011 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1012 current_chunk_id
.is_set
= true;
1014 ERR("Failed to get current trace chunk id");
1015 ret
= LTTNG_ERR_UNK
;
1020 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1021 && usess
->consumer
->enabled
) {
1022 /* For each consumer socket, send relayd sockets */
1023 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1024 socket
, node
.node
) {
1025 pthread_mutex_lock(socket
->lock
);
1026 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1027 usess
->consumer
, socket
,
1028 session
->name
, session
->hostname
,
1030 session
->live_timer
,
1031 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1032 session
->creation_time
,
1033 session
->name_contains_creation_time
);
1034 pthread_mutex_unlock(socket
->lock
);
1035 if (ret
!= LTTNG_OK
) {
1038 /* Session is now ready for network streaming. */
1039 session
->net_handle
= 1;
1041 session
->consumer
->relay_major_version
=
1042 usess
->consumer
->relay_major_version
;
1043 session
->consumer
->relay_minor_version
=
1044 usess
->consumer
->relay_minor_version
;
1045 session
->consumer
->relay_allows_clear
=
1046 usess
->consumer
->relay_allows_clear
;
1049 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1050 && ksess
->consumer
->enabled
) {
1051 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1052 socket
, node
.node
) {
1053 pthread_mutex_lock(socket
->lock
);
1054 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1055 ksess
->consumer
, socket
,
1056 session
->name
, session
->hostname
,
1058 session
->live_timer
,
1059 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1060 session
->creation_time
,
1061 session
->name_contains_creation_time
);
1062 pthread_mutex_unlock(socket
->lock
);
1063 if (ret
!= LTTNG_OK
) {
1066 /* Session is now ready for network streaming. */
1067 session
->net_handle
= 1;
1069 session
->consumer
->relay_major_version
=
1070 ksess
->consumer
->relay_major_version
;
1071 session
->consumer
->relay_minor_version
=
1072 ksess
->consumer
->relay_minor_version
;
1073 session
->consumer
->relay_allows_clear
=
1074 ksess
->consumer
->relay_allows_clear
;
1083 * Start a kernel session by opening all necessary streams.
1085 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1088 struct ltt_kernel_channel
*kchan
;
1090 /* Open kernel metadata */
1091 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1092 ret
= kernel_open_metadata(ksess
);
1094 ret
= LTTNG_ERR_KERN_META_FAIL
;
1099 /* Open kernel metadata stream */
1100 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1101 ret
= kernel_open_metadata_stream(ksess
);
1103 ERR("Kernel create metadata stream failed");
1104 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1109 /* For each channel */
1110 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1111 if (kchan
->stream_count
== 0) {
1112 ret
= kernel_open_channel_stream(kchan
);
1114 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1117 /* Update the stream global counter */
1118 ksess
->stream_count_global
+= ret
;
1122 /* Setup kernel consumer socket and send fds to it */
1123 ret
= init_kernel_tracing(ksess
);
1125 ret
= LTTNG_ERR_KERN_START_FAIL
;
1129 /* This start the kernel tracing */
1130 ret
= kernel_start_session(ksess
);
1132 ret
= LTTNG_ERR_KERN_START_FAIL
;
1136 /* Quiescent wait after starting trace */
1137 kernel_wait_quiescent();
1147 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1149 struct ltt_kernel_channel
*kchan
;
1150 bool error_occurred
= false;
1153 if (!ksess
|| !ksess
->active
) {
1156 DBG("Stopping kernel tracing");
1158 ret
= kernel_stop_session(ksess
);
1160 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1164 kernel_wait_quiescent();
1166 /* Flush metadata after stopping (if exists) */
1167 if (ksess
->metadata_stream_fd
>= 0) {
1168 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1170 ERR("Kernel metadata flush failed");
1171 error_occurred
= true;
1175 /* Flush all buffers after stopping */
1176 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1177 ret
= kernel_flush_buffer(kchan
);
1179 ERR("Kernel flush buffer error");
1180 error_occurred
= true;
1185 if (error_occurred
) {
1186 ret
= LTTNG_ERR_UNK
;
1195 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1197 int cmd_disable_channel(struct ltt_session
*session
,
1198 enum lttng_domain_type domain
, char *channel_name
)
1201 struct ltt_ust_session
*usess
;
1203 usess
= session
->ust_session
;
1208 case LTTNG_DOMAIN_KERNEL
:
1210 ret
= channel_kernel_disable(session
->kernel_session
,
1212 if (ret
!= LTTNG_OK
) {
1216 kernel_wait_quiescent();
1219 case LTTNG_DOMAIN_UST
:
1221 struct ltt_ust_channel
*uchan
;
1222 struct lttng_ht
*chan_ht
;
1224 chan_ht
= usess
->domain_global
.channels
;
1226 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1227 if (uchan
== NULL
) {
1228 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1232 ret
= channel_ust_disable(usess
, uchan
);
1233 if (ret
!= LTTNG_OK
) {
1239 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1251 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1253 * The wpipe arguments is used as a notifier for the kernel thread.
1255 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1259 ssize_t sock_recv_len
;
1260 struct lttng_channel
*channel
= NULL
;
1261 struct lttng_buffer_view view
;
1262 struct lttng_dynamic_buffer channel_buffer
;
1263 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1265 lttng_dynamic_buffer_init(&channel_buffer
);
1266 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1267 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1269 ret
= LTTNG_ERR_NOMEM
;
1273 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1275 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1276 ERR("Failed to receive \"enable channel\" command payload");
1277 ret
= LTTNG_ERR_INVALID
;
1281 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1282 if (!lttng_buffer_view_is_valid(&view
)) {
1283 ret
= LTTNG_ERR_INVALID
;
1287 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1288 ERR("Invalid channel payload received in \"enable channel\" command");
1289 ret
= LTTNG_ERR_INVALID
;
1293 ret
= cmd_enable_channel_internal(
1294 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1297 lttng_dynamic_buffer_reset(&channel_buffer
);
1298 lttng_channel_destroy(channel
);
1302 static int cmd_enable_channel_internal(struct ltt_session
*session
,
1303 const struct lttng_domain
*domain
,
1304 const struct lttng_channel
*_attr
,
1308 struct ltt_ust_session
*usess
= session
->ust_session
;
1309 struct lttng_ht
*chan_ht
;
1311 struct lttng_channel
*attr
= NULL
;
1313 LTTNG_ASSERT(session
);
1314 LTTNG_ASSERT(_attr
);
1315 LTTNG_ASSERT(domain
);
1317 attr
= lttng_channel_copy(_attr
);
1319 ret
= -LTTNG_ERR_NOMEM
;
1323 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1325 /* Validate channel name */
1326 if (attr
->name
[0] == '.' ||
1327 memchr(attr
->name
, '/', len
) != NULL
) {
1328 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1332 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1337 * If the session is a live session, remove the switch timer, the
1338 * live timer does the same thing but sends also synchronisation
1339 * beacons for inactive streams.
1341 if (session
->live_timer
> 0) {
1342 attr
->attr
.live_timer_interval
= session
->live_timer
;
1343 attr
->attr
.switch_timer_interval
= 0;
1346 /* Check for feature support */
1347 switch (domain
->type
) {
1348 case LTTNG_DOMAIN_KERNEL
:
1350 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1351 /* Sampling position of buffer is not supported */
1352 WARN("Kernel tracer does not support buffer monitoring. "
1353 "Setting the monitor interval timer to 0 "
1354 "(disabled) for channel '%s' of session '%s'",
1355 attr
->name
, session
->name
);
1356 lttng_channel_set_monitor_timer_interval(attr
, 0);
1360 case LTTNG_DOMAIN_UST
:
1362 case LTTNG_DOMAIN_JUL
:
1363 case LTTNG_DOMAIN_LOG4J
:
1364 case LTTNG_DOMAIN_PYTHON
:
1365 if (!agent_tracing_is_enabled()) {
1366 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1367 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1372 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1376 switch (domain
->type
) {
1377 case LTTNG_DOMAIN_KERNEL
:
1379 struct ltt_kernel_channel
*kchan
;
1381 kchan
= trace_kernel_get_channel_by_name(
1382 attr
->name
, session
->kernel_session
);
1383 if (kchan
== NULL
) {
1385 * Don't try to create a channel if the session has been started at
1386 * some point in time before. The tracer does not allow it.
1388 if (session
->has_been_started
) {
1389 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1393 if (session
->snapshot
.nb_output
> 0 ||
1394 session
->snapshot_mode
) {
1395 /* Enforce mmap output for snapshot sessions. */
1396 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1398 ret
= channel_kernel_create(
1399 session
->kernel_session
, attr
, wpipe
);
1400 if (attr
->name
[0] != '\0') {
1401 session
->kernel_session
->has_non_default_channel
= 1;
1404 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1407 if (ret
!= LTTNG_OK
) {
1411 kernel_wait_quiescent();
1414 case LTTNG_DOMAIN_UST
:
1415 case LTTNG_DOMAIN_JUL
:
1416 case LTTNG_DOMAIN_LOG4J
:
1417 case LTTNG_DOMAIN_PYTHON
:
1419 struct ltt_ust_channel
*uchan
;
1424 * Current agent implementation limitations force us to allow
1425 * only one channel at once in "agent" subdomains. Each
1426 * subdomain has a default channel name which must be strictly
1429 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1430 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1431 LTTNG_SYMBOL_NAME_LEN
)) {
1432 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1435 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1436 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1437 LTTNG_SYMBOL_NAME_LEN
)) {
1438 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1441 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1442 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1443 LTTNG_SYMBOL_NAME_LEN
)) {
1444 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1449 chan_ht
= usess
->domain_global
.channels
;
1451 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1452 if (uchan
== NULL
) {
1454 * Don't try to create a channel if the session has been started at
1455 * some point in time before. The tracer does not allow it.
1457 if (session
->has_been_started
) {
1458 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1462 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1463 if (attr
->name
[0] != '\0') {
1464 usess
->has_non_default_channel
= 1;
1467 ret
= channel_ust_enable(usess
, uchan
);
1472 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1476 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1477 session
->has_non_mmap_channel
= true;
1482 lttng_channel_destroy(attr
);
1486 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1487 struct ltt_session
*session
,
1488 enum lttng_domain_type domain
,
1489 enum lttng_process_attr process_attr
,
1490 enum lttng_tracking_policy
*policy
)
1492 enum lttng_error_code ret_code
= LTTNG_OK
;
1493 const struct process_attr_tracker
*tracker
;
1496 case LTTNG_DOMAIN_KERNEL
:
1497 if (!session
->kernel_session
) {
1498 ret_code
= LTTNG_ERR_INVALID
;
1501 tracker
= kernel_get_process_attr_tracker(
1502 session
->kernel_session
, process_attr
);
1504 case LTTNG_DOMAIN_UST
:
1505 if (!session
->ust_session
) {
1506 ret_code
= LTTNG_ERR_INVALID
;
1509 tracker
= trace_ust_get_process_attr_tracker(
1510 session
->ust_session
, process_attr
);
1513 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1517 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1519 ret_code
= LTTNG_ERR_INVALID
;
1525 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1526 struct ltt_session
*session
,
1527 enum lttng_domain_type domain
,
1528 enum lttng_process_attr process_attr
,
1529 enum lttng_tracking_policy policy
)
1531 enum lttng_error_code ret_code
= LTTNG_OK
;
1534 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1535 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1536 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1539 ret_code
= LTTNG_ERR_INVALID
;
1544 case LTTNG_DOMAIN_KERNEL
:
1545 if (!session
->kernel_session
) {
1546 ret_code
= LTTNG_ERR_INVALID
;
1549 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1550 session
->kernel_session
, process_attr
, policy
);
1552 case LTTNG_DOMAIN_UST
:
1553 if (!session
->ust_session
) {
1554 ret_code
= LTTNG_ERR_INVALID
;
1557 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1558 session
->ust_session
, process_attr
, policy
);
1561 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1568 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1569 struct ltt_session
*session
,
1570 enum lttng_domain_type domain
,
1571 enum lttng_process_attr process_attr
,
1572 const struct process_attr_value
*value
)
1574 enum lttng_error_code ret_code
= LTTNG_OK
;
1577 case LTTNG_DOMAIN_KERNEL
:
1578 if (!session
->kernel_session
) {
1579 ret_code
= LTTNG_ERR_INVALID
;
1582 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1583 session
->kernel_session
, process_attr
, value
);
1585 case LTTNG_DOMAIN_UST
:
1586 if (!session
->ust_session
) {
1587 ret_code
= LTTNG_ERR_INVALID
;
1590 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1591 session
->ust_session
, process_attr
, value
);
1594 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1601 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1602 struct ltt_session
*session
,
1603 enum lttng_domain_type domain
,
1604 enum lttng_process_attr process_attr
,
1605 const struct process_attr_value
*value
)
1607 enum lttng_error_code ret_code
= LTTNG_OK
;
1610 case LTTNG_DOMAIN_KERNEL
:
1611 if (!session
->kernel_session
) {
1612 ret_code
= LTTNG_ERR_INVALID
;
1615 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1616 session
->kernel_session
, process_attr
, value
);
1618 case LTTNG_DOMAIN_UST
:
1619 if (!session
->ust_session
) {
1620 ret_code
= LTTNG_ERR_INVALID
;
1623 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1624 session
->ust_session
, process_attr
, value
);
1627 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1634 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1635 struct ltt_session
*session
,
1636 enum lttng_domain_type domain
,
1637 enum lttng_process_attr process_attr
,
1638 struct lttng_process_attr_values
**values
)
1640 enum lttng_error_code ret_code
= LTTNG_OK
;
1641 const struct process_attr_tracker
*tracker
;
1642 enum process_attr_tracker_status status
;
1645 case LTTNG_DOMAIN_KERNEL
:
1646 if (!session
->kernel_session
) {
1647 ret_code
= LTTNG_ERR_INVALID
;
1650 tracker
= kernel_get_process_attr_tracker(
1651 session
->kernel_session
, process_attr
);
1653 case LTTNG_DOMAIN_UST
:
1654 if (!session
->ust_session
) {
1655 ret_code
= LTTNG_ERR_INVALID
;
1658 tracker
= trace_ust_get_process_attr_tracker(
1659 session
->ust_session
, process_attr
);
1662 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1667 ret_code
= LTTNG_ERR_INVALID
;
1671 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1673 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1674 ret_code
= LTTNG_OK
;
1676 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1677 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1679 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1680 ret_code
= LTTNG_ERR_NOMEM
;
1683 ret_code
= LTTNG_ERR_UNK
;
1692 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1694 int cmd_disable_event(struct ltt_session
*session
,
1695 enum lttng_domain_type domain
, const char *channel_name
,
1696 const struct lttng_event
*event
)
1699 const char *event_name
;
1701 DBG("Disable event command for event \'%s\'", event
->name
);
1703 event_name
= event
->name
;
1705 /* Error out on unhandled search criteria */
1706 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1707 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1708 ret
= LTTNG_ERR_UNK
;
1715 case LTTNG_DOMAIN_KERNEL
:
1717 struct ltt_kernel_channel
*kchan
;
1718 struct ltt_kernel_session
*ksess
;
1720 ksess
= session
->kernel_session
;
1723 * If a non-default channel has been created in the
1724 * session, explicitely require that -c chan_name needs
1727 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1728 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1732 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1733 if (kchan
== NULL
) {
1734 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1738 switch (event
->type
) {
1739 case LTTNG_EVENT_ALL
:
1740 case LTTNG_EVENT_TRACEPOINT
:
1741 case LTTNG_EVENT_SYSCALL
:
1742 case LTTNG_EVENT_PROBE
:
1743 case LTTNG_EVENT_FUNCTION
:
1744 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1745 if (event_name
[0] == '\0') {
1746 ret
= event_kernel_disable_event(kchan
,
1749 ret
= event_kernel_disable_event(kchan
,
1750 event_name
, event
->type
);
1752 if (ret
!= LTTNG_OK
) {
1757 ret
= LTTNG_ERR_UNK
;
1761 kernel_wait_quiescent();
1764 case LTTNG_DOMAIN_UST
:
1766 struct ltt_ust_channel
*uchan
;
1767 struct ltt_ust_session
*usess
;
1769 usess
= session
->ust_session
;
1771 if (validate_ust_event_name(event_name
)) {
1772 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1777 * If a non-default channel has been created in the
1778 * session, explicitly require that -c chan_name needs
1781 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1782 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1786 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1788 if (uchan
== NULL
) {
1789 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1793 switch (event
->type
) {
1794 case LTTNG_EVENT_ALL
:
1796 * An empty event name means that everything
1797 * should be disabled.
1799 if (event
->name
[0] == '\0') {
1800 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1802 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1805 if (ret
!= LTTNG_OK
) {
1810 ret
= LTTNG_ERR_UNK
;
1814 DBG3("Disable UST event %s in channel %s completed", event_name
,
1818 case LTTNG_DOMAIN_LOG4J
:
1819 case LTTNG_DOMAIN_JUL
:
1820 case LTTNG_DOMAIN_PYTHON
:
1823 struct ltt_ust_session
*usess
= session
->ust_session
;
1825 LTTNG_ASSERT(usess
);
1827 switch (event
->type
) {
1828 case LTTNG_EVENT_ALL
:
1831 ret
= LTTNG_ERR_UNK
;
1835 agt
= trace_ust_find_agent(usess
, domain
);
1837 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1841 * An empty event name means that everything
1842 * should be disabled.
1844 if (event
->name
[0] == '\0') {
1845 ret
= event_agent_disable_all(usess
, agt
);
1847 ret
= event_agent_disable(usess
, agt
, event_name
);
1849 if (ret
!= LTTNG_OK
) {
1856 ret
= LTTNG_ERR_UND
;
1869 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1871 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1872 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1874 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1875 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1878 * Don't try to add a context if the session has been started at
1879 * some point in time before. The tracer does not allow it and would
1880 * result in a corrupted trace.
1882 if (session
->has_been_started
) {
1883 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1887 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1888 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1889 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1893 case LTTNG_DOMAIN_KERNEL
:
1894 LTTNG_ASSERT(session
->kernel_session
);
1896 if (session
->kernel_session
->channel_count
== 0) {
1897 /* Create default channel */
1898 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1899 if (ret
!= LTTNG_OK
) {
1902 chan_kern_created
= 1;
1904 /* Add kernel context to kernel tracer */
1905 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1906 if (ret
!= LTTNG_OK
) {
1910 case LTTNG_DOMAIN_JUL
:
1911 case LTTNG_DOMAIN_LOG4J
:
1914 * Validate channel name.
1915 * If no channel name is given and the domain is JUL or LOG4J,
1916 * set it to the appropriate domain-specific channel name. If
1917 * a name is provided but does not match the expexted channel
1918 * name, return an error.
1920 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1921 strcmp(channel_name
,
1922 DEFAULT_JUL_CHANNEL_NAME
)) {
1923 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1925 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1926 strcmp(channel_name
,
1927 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1928 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1931 /* break is _not_ missing here. */
1933 case LTTNG_DOMAIN_UST
:
1935 struct ltt_ust_session
*usess
= session
->ust_session
;
1936 unsigned int chan_count
;
1938 LTTNG_ASSERT(usess
);
1940 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1941 if (chan_count
== 0) {
1942 struct lttng_channel
*attr
;
1943 /* Create default channel */
1944 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1946 ret
= LTTNG_ERR_FATAL
;
1950 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1951 if (ret
!= LTTNG_OK
) {
1955 channel_attr_destroy(attr
);
1956 chan_ust_created
= 1;
1959 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1960 free(app_ctx_provider_name
);
1962 app_ctx_name
= NULL
;
1963 app_ctx_provider_name
= NULL
;
1964 if (ret
!= LTTNG_OK
) {
1970 ret
= LTTNG_ERR_UND
;
1978 if (chan_kern_created
) {
1979 struct ltt_kernel_channel
*kchan
=
1980 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1981 session
->kernel_session
);
1982 /* Created previously, this should NOT fail. */
1983 LTTNG_ASSERT(kchan
);
1984 kernel_destroy_channel(kchan
);
1987 if (chan_ust_created
) {
1988 struct ltt_ust_channel
*uchan
=
1989 trace_ust_find_channel_by_name(
1990 session
->ust_session
->domain_global
.channels
,
1991 DEFAULT_CHANNEL_NAME
);
1992 /* Created previously, this should NOT fail. */
1993 LTTNG_ASSERT(uchan
);
1994 /* Remove from the channel list of the session. */
1995 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1997 trace_ust_destroy_channel(uchan
);
2000 free(app_ctx_provider_name
);
2005 static inline bool name_starts_with(const char *name
, const char *prefix
)
2007 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2009 return !strncmp(name
, prefix
, max_cmp_len
);
2012 /* Perform userspace-specific event name validation */
2013 static int validate_ust_event_name(const char *name
)
2023 * Check name against all internal UST event component namespaces used
2026 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2027 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2028 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2037 * Internal version of cmd_enable_event() with a supplemental
2038 * "internal_event" flag which is used to enable internal events which should
2039 * be hidden from clients. Such events are used in the agent implementation to
2040 * enable the events through which all "agent" events are funeled.
2042 static int _cmd_enable_event(struct ltt_session
*session
,
2043 const struct lttng_domain
*domain
,
2044 char *channel_name
, struct lttng_event
*event
,
2045 char *filter_expression
,
2046 struct lttng_bytecode
*filter
,
2047 struct lttng_event_exclusion
*exclusion
,
2048 int wpipe
, bool internal_event
)
2050 int ret
= 0, channel_created
= 0;
2051 struct lttng_channel
*attr
= NULL
;
2053 LTTNG_ASSERT(session
);
2054 LTTNG_ASSERT(event
);
2055 LTTNG_ASSERT(channel_name
);
2057 /* If we have a filter, we must have its filter expression */
2058 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2060 /* Normalize event name as a globbing pattern */
2061 strutils_normalize_star_glob_pattern(event
->name
);
2063 /* Normalize exclusion names as globbing patterns */
2067 for (i
= 0; i
< exclusion
->count
; i
++) {
2068 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2070 strutils_normalize_star_glob_pattern(name
);
2074 DBG("Enable event command for event \'%s\'", event
->name
);
2078 switch (domain
->type
) {
2079 case LTTNG_DOMAIN_KERNEL
:
2081 struct ltt_kernel_channel
*kchan
;
2084 * If a non-default channel has been created in the
2085 * session, explicitely require that -c chan_name needs
2088 if (session
->kernel_session
->has_non_default_channel
2089 && channel_name
[0] == '\0') {
2090 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2094 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2095 session
->kernel_session
);
2096 if (kchan
== NULL
) {
2097 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2098 LTTNG_BUFFER_GLOBAL
);
2100 ret
= LTTNG_ERR_FATAL
;
2103 if (lttng_strncpy(attr
->name
, channel_name
,
2104 sizeof(attr
->name
))) {
2105 ret
= LTTNG_ERR_INVALID
;
2109 ret
= cmd_enable_channel_internal(
2110 session
, domain
, attr
, wpipe
);
2111 if (ret
!= LTTNG_OK
) {
2114 channel_created
= 1;
2117 /* Get the newly created kernel channel pointer */
2118 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2119 session
->kernel_session
);
2120 if (kchan
== NULL
) {
2121 /* This sould not happen... */
2122 ret
= LTTNG_ERR_FATAL
;
2126 switch (event
->type
) {
2127 case LTTNG_EVENT_ALL
:
2129 char *filter_expression_a
= NULL
;
2130 struct lttng_bytecode
*filter_a
= NULL
;
2133 * We need to duplicate filter_expression and filter,
2134 * because ownership is passed to first enable
2137 if (filter_expression
) {
2138 filter_expression_a
= strdup(filter_expression
);
2139 if (!filter_expression_a
) {
2140 ret
= LTTNG_ERR_FATAL
;
2145 filter_a
= (lttng_bytecode
*) zmalloc(sizeof(*filter_a
) + filter
->len
);
2147 free(filter_expression_a
);
2148 ret
= LTTNG_ERR_FATAL
;
2151 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2153 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2154 ret
= event_kernel_enable_event(kchan
, event
,
2155 filter_expression
, filter
);
2156 /* We have passed ownership */
2157 filter_expression
= NULL
;
2159 if (ret
!= LTTNG_OK
) {
2160 if (channel_created
) {
2161 /* Let's not leak a useless channel. */
2162 kernel_destroy_channel(kchan
);
2164 free(filter_expression_a
);
2168 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2169 ret
= event_kernel_enable_event(kchan
, event
,
2170 filter_expression_a
, filter_a
);
2171 /* We have passed ownership */
2172 filter_expression_a
= NULL
;
2174 if (ret
!= LTTNG_OK
) {
2179 case LTTNG_EVENT_PROBE
:
2180 case LTTNG_EVENT_USERSPACE_PROBE
:
2181 case LTTNG_EVENT_FUNCTION
:
2182 case LTTNG_EVENT_FUNCTION_ENTRY
:
2183 case LTTNG_EVENT_TRACEPOINT
:
2184 ret
= event_kernel_enable_event(kchan
, event
,
2185 filter_expression
, filter
);
2186 /* We have passed ownership */
2187 filter_expression
= NULL
;
2189 if (ret
!= LTTNG_OK
) {
2190 if (channel_created
) {
2191 /* Let's not leak a useless channel. */
2192 kernel_destroy_channel(kchan
);
2197 case LTTNG_EVENT_SYSCALL
:
2198 ret
= event_kernel_enable_event(kchan
, event
,
2199 filter_expression
, filter
);
2200 /* We have passed ownership */
2201 filter_expression
= NULL
;
2203 if (ret
!= LTTNG_OK
) {
2208 ret
= LTTNG_ERR_UNK
;
2212 kernel_wait_quiescent();
2215 case LTTNG_DOMAIN_UST
:
2217 struct ltt_ust_channel
*uchan
;
2218 struct ltt_ust_session
*usess
= session
->ust_session
;
2220 LTTNG_ASSERT(usess
);
2223 * If a non-default channel has been created in the
2224 * session, explicitely require that -c chan_name needs
2227 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2228 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2232 /* Get channel from global UST domain */
2233 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2235 if (uchan
== NULL
) {
2236 /* Create default channel */
2237 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2238 usess
->buffer_type
);
2240 ret
= LTTNG_ERR_FATAL
;
2243 if (lttng_strncpy(attr
->name
, channel_name
,
2244 sizeof(attr
->name
))) {
2245 ret
= LTTNG_ERR_INVALID
;
2249 ret
= cmd_enable_channel_internal(
2250 session
, domain
, attr
, wpipe
);
2251 if (ret
!= LTTNG_OK
) {
2255 /* Get the newly created channel reference back */
2256 uchan
= trace_ust_find_channel_by_name(
2257 usess
->domain_global
.channels
, channel_name
);
2258 LTTNG_ASSERT(uchan
);
2261 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2263 * Don't allow users to add UST events to channels which
2264 * are assigned to a userspace subdomain (JUL, Log4J,
2267 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2271 if (!internal_event
) {
2273 * Ensure the event name is not reserved for internal
2276 ret
= validate_ust_event_name(event
->name
);
2278 WARN("Userspace event name %s failed validation.",
2280 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2285 /* At this point, the session and channel exist on the tracer */
2286 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2287 filter_expression
, filter
, exclusion
,
2289 /* We have passed ownership */
2290 filter_expression
= NULL
;
2293 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2294 goto already_enabled
;
2295 } else if (ret
!= LTTNG_OK
) {
2300 case LTTNG_DOMAIN_LOG4J
:
2301 case LTTNG_DOMAIN_JUL
:
2302 case LTTNG_DOMAIN_PYTHON
:
2304 const char *default_event_name
, *default_chan_name
;
2306 struct lttng_event uevent
;
2307 struct lttng_domain tmp_dom
;
2308 struct ltt_ust_session
*usess
= session
->ust_session
;
2310 LTTNG_ASSERT(usess
);
2312 if (!agent_tracing_is_enabled()) {
2313 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2314 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2318 agt
= trace_ust_find_agent(usess
, domain
->type
);
2320 agt
= agent_create(domain
->type
);
2322 ret
= LTTNG_ERR_NOMEM
;
2325 agent_add(agt
, usess
->agents
);
2328 /* Create the default tracepoint. */
2329 memset(&uevent
, 0, sizeof(uevent
));
2330 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2331 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2332 default_event_name
= event_get_default_agent_ust_name(
2334 if (!default_event_name
) {
2335 ret
= LTTNG_ERR_FATAL
;
2338 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2339 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2342 * The domain type is changed because we are about to enable the
2343 * default channel and event for the JUL domain that are hardcoded.
2344 * This happens in the UST domain.
2346 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2347 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2349 switch (domain
->type
) {
2350 case LTTNG_DOMAIN_LOG4J
:
2351 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2353 case LTTNG_DOMAIN_JUL
:
2354 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2356 case LTTNG_DOMAIN_PYTHON
:
2357 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2360 /* The switch/case we are in makes this impossible */
2365 char *filter_expression_copy
= NULL
;
2366 struct lttng_bytecode
*filter_copy
= NULL
;
2369 const size_t filter_size
= sizeof(
2370 struct lttng_bytecode
)
2373 filter_copy
= (lttng_bytecode
*) zmalloc(filter_size
);
2375 ret
= LTTNG_ERR_NOMEM
;
2378 memcpy(filter_copy
, filter
, filter_size
);
2380 filter_expression_copy
=
2381 strdup(filter_expression
);
2382 if (!filter_expression
) {
2383 ret
= LTTNG_ERR_NOMEM
;
2386 if (!filter_expression_copy
|| !filter_copy
) {
2387 free(filter_expression_copy
);
2393 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2394 (char *) default_chan_name
,
2395 &uevent
, filter_expression_copy
,
2396 filter_copy
, NULL
, wpipe
);
2399 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2400 goto already_enabled
;
2401 } else if (ret
!= LTTNG_OK
) {
2405 /* The wild card * means that everything should be enabled. */
2406 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2407 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2410 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2414 filter_expression
= NULL
;
2415 if (ret
!= LTTNG_OK
) {
2422 ret
= LTTNG_ERR_UND
;
2430 free(filter_expression
);
2433 channel_attr_destroy(attr
);
2439 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2440 * We own filter, exclusion, and filter_expression.
2442 int cmd_enable_event(struct ltt_session
*session
,
2443 const struct lttng_domain
*domain
,
2444 char *channel_name
, struct lttng_event
*event
,
2445 char *filter_expression
,
2446 struct lttng_bytecode
*filter
,
2447 struct lttng_event_exclusion
*exclusion
,
2450 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2451 filter_expression
, filter
, exclusion
, wpipe
, false);
2455 * Enable an event which is internal to LTTng. An internal should
2456 * never be made visible to clients and are immune to checks such as
2459 static int cmd_enable_event_internal(struct ltt_session
*session
,
2460 const struct lttng_domain
*domain
,
2461 char *channel_name
, struct lttng_event
*event
,
2462 char *filter_expression
,
2463 struct lttng_bytecode
*filter
,
2464 struct lttng_event_exclusion
*exclusion
,
2467 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2468 filter_expression
, filter
, exclusion
, wpipe
, true);
2472 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2474 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2475 struct lttng_event
**events
)
2478 ssize_t nb_events
= 0;
2481 case LTTNG_DOMAIN_KERNEL
:
2482 nb_events
= kernel_list_events(events
);
2483 if (nb_events
< 0) {
2484 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2488 case LTTNG_DOMAIN_UST
:
2489 nb_events
= ust_app_list_events(events
);
2490 if (nb_events
< 0) {
2491 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2495 case LTTNG_DOMAIN_LOG4J
:
2496 case LTTNG_DOMAIN_JUL
:
2497 case LTTNG_DOMAIN_PYTHON
:
2498 nb_events
= agent_list_events(events
, domain
);
2499 if (nb_events
< 0) {
2500 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2505 ret
= LTTNG_ERR_UND
;
2512 /* Return negative value to differentiate return code */
2517 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2519 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2520 struct lttng_event_field
**fields
)
2523 ssize_t nb_fields
= 0;
2526 case LTTNG_DOMAIN_UST
:
2527 nb_fields
= ust_app_list_event_fields(fields
);
2528 if (nb_fields
< 0) {
2529 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2533 case LTTNG_DOMAIN_KERNEL
:
2534 default: /* fall-through */
2535 ret
= LTTNG_ERR_UND
;
2542 /* Return negative value to differentiate return code */
2546 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2548 return syscall_table_list(events
);
2552 * Command LTTNG_START_TRACE processed by the client thread.
2554 * Called with session mutex held.
2556 int cmd_start_trace(struct ltt_session
*session
)
2558 enum lttng_error_code ret
;
2559 unsigned long nb_chan
= 0;
2560 struct ltt_kernel_session
*ksession
;
2561 struct ltt_ust_session
*usess
;
2562 const bool session_rotated_after_last_stop
=
2563 session
->rotated_after_last_stop
;
2564 const bool session_cleared_after_last_stop
=
2565 session
->cleared_after_last_stop
;
2567 LTTNG_ASSERT(session
);
2569 /* Ease our life a bit ;) */
2570 ksession
= session
->kernel_session
;
2571 usess
= session
->ust_session
;
2573 /* Is the session already started? */
2574 if (session
->active
) {
2575 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2576 /* Perform nothing */
2580 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2581 !session
->current_trace_chunk
) {
2583 * A rotation was launched while the session was stopped and
2584 * it has not been completed yet. It is not possible to start
2585 * the session since starting the session here would require a
2586 * rotation from "NULL" to a new trace chunk. That rotation
2587 * would overlap with the ongoing rotation, which is not
2590 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2592 ret
= LTTNG_ERR_ROTATION_PENDING
;
2597 * Starting a session without channel is useless since after that it's not
2598 * possible to enable channel thus inform the client.
2600 if (usess
&& usess
->domain_global
.channels
) {
2601 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2604 nb_chan
+= ksession
->channel_count
;
2607 ret
= LTTNG_ERR_NO_CHANNEL
;
2611 session
->active
= 1;
2612 session
->rotated_after_last_stop
= false;
2613 session
->cleared_after_last_stop
= false;
2614 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2615 if (!session
->has_been_started
) {
2616 struct lttng_trace_chunk
*trace_chunk
;
2618 DBG("Creating initial trace chunk of session \"%s\"",
2620 trace_chunk
= session_create_new_trace_chunk(
2621 session
, NULL
, NULL
, NULL
);
2623 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2626 LTTNG_ASSERT(!session
->current_trace_chunk
);
2627 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2629 lttng_trace_chunk_put(trace_chunk
);
2631 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2635 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2638 * Rotate existing streams into the new chunk.
2639 * This is a "quiet" rotation has no client has
2640 * explicitly requested this operation.
2642 * There is also no need to wait for the rotation
2643 * to complete as it will happen immediately. No data
2644 * was produced as the session was stopped, so the
2645 * rotation should happen on reception of the command.
2647 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2648 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2649 if (ret
!= LTTNG_OK
) {
2655 /* Kernel tracing */
2656 if (ksession
!= NULL
) {
2657 DBG("Start kernel tracing session %s", session
->name
);
2658 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2659 if (ret
!= LTTNG_OK
) {
2664 /* Flag session that trace should start automatically */
2666 int int_ret
= ust_app_start_trace_all(usess
);
2669 ret
= LTTNG_ERR_UST_START_FAIL
;
2675 * Open a packet in every stream of the session to ensure that viewers
2676 * can correctly identify the boundaries of the periods during which
2677 * tracing was active for this session.
2679 ret
= session_open_packets(session
);
2680 if (ret
!= LTTNG_OK
) {
2685 * Clear the flag that indicates that a rotation was done while the
2686 * session was stopped.
2688 session
->rotated_after_last_stop
= false;
2690 if (session
->rotate_timer_period
) {
2691 int int_ret
= timer_session_rotation_schedule_timer_start(
2692 session
, session
->rotate_timer_period
);
2695 ERR("Failed to enable rotate timer");
2696 ret
= LTTNG_ERR_UNK
;
2704 if (ret
== LTTNG_OK
) {
2705 /* Flag this after a successful start. */
2706 session
->has_been_started
|= 1;
2708 session
->active
= 0;
2709 /* Restore initial state on error. */
2710 session
->rotated_after_last_stop
=
2711 session_rotated_after_last_stop
;
2712 session
->cleared_after_last_stop
=
2713 session_cleared_after_last_stop
;
2720 * Command LTTNG_STOP_TRACE processed by the client thread.
2722 int cmd_stop_trace(struct ltt_session
*session
)
2725 struct ltt_kernel_session
*ksession
;
2726 struct ltt_ust_session
*usess
;
2728 LTTNG_ASSERT(session
);
2730 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2732 ksession
= session
->kernel_session
;
2733 usess
= session
->ust_session
;
2735 /* Session is not active. Skip everything and inform the client. */
2736 if (!session
->active
) {
2737 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2741 ret
= stop_kernel_session(ksession
);
2742 if (ret
!= LTTNG_OK
) {
2746 if (usess
&& usess
->active
) {
2747 ret
= ust_app_stop_trace_all(usess
);
2749 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2754 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2756 /* Flag inactive after a successful stop. */
2757 session
->active
= 0;
2765 * Set the base_path of the session only if subdir of a control uris is set.
2766 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2768 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2770 struct lttng_uri
*uris
)
2775 for (i
= 0; i
< nb_uri
; i
++) {
2776 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2777 uris
[i
].subdir
[0] == '\0') {
2778 /* Not interested in these URIs */
2782 if (session
->base_path
!= NULL
) {
2783 free(session
->base_path
);
2784 session
->base_path
= NULL
;
2787 /* Set session base_path */
2788 session
->base_path
= strdup(uris
[i
].subdir
);
2789 if (!session
->base_path
) {
2790 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2791 uris
[i
].subdir
, session
->name
);
2792 ret
= LTTNG_ERR_NOMEM
;
2795 DBG2("Setting base path \"%s\" for session \"%s\"",
2796 session
->base_path
, session
->name
);
2804 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2806 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2807 struct lttng_uri
*uris
)
2810 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2811 struct ltt_ust_session
*usess
= session
->ust_session
;
2813 LTTNG_ASSERT(session
);
2815 LTTNG_ASSERT(nb_uri
> 0);
2817 /* Can't set consumer URI if the session is active. */
2818 if (session
->active
) {
2819 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2824 * Set the session base path if any. This is done inside
2825 * cmd_set_consumer_uri to preserve backward compatibility of the
2826 * previous session creation api vs the session descriptor api.
2828 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2829 if (ret
!= LTTNG_OK
) {
2833 /* Set the "global" consumer URIs */
2834 for (i
= 0; i
< nb_uri
; i
++) {
2835 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2837 if (ret
!= LTTNG_OK
) {
2842 /* Set UST session URIs */
2843 if (session
->ust_session
) {
2844 for (i
= 0; i
< nb_uri
; i
++) {
2845 ret
= add_uri_to_consumer(session
,
2846 session
->ust_session
->consumer
,
2847 &uris
[i
], LTTNG_DOMAIN_UST
);
2848 if (ret
!= LTTNG_OK
) {
2854 /* Set kernel session URIs */
2855 if (session
->kernel_session
) {
2856 for (i
= 0; i
< nb_uri
; i
++) {
2857 ret
= add_uri_to_consumer(session
,
2858 session
->kernel_session
->consumer
,
2859 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2860 if (ret
!= LTTNG_OK
) {
2867 * Make sure to set the session in output mode after we set URI since a
2868 * session can be created without URL (thus flagged in no output mode).
2870 session
->output_traces
= 1;
2872 ksess
->output_traces
= 1;
2876 usess
->output_traces
= 1;
2887 enum lttng_error_code
set_session_output_from_descriptor(
2888 struct ltt_session
*session
,
2889 const struct lttng_session_descriptor
*descriptor
)
2892 enum lttng_error_code ret_code
= LTTNG_OK
;
2893 enum lttng_session_descriptor_type session_type
=
2894 lttng_session_descriptor_get_type(descriptor
);
2895 enum lttng_session_descriptor_output_type output_type
=
2896 lttng_session_descriptor_get_output_type(descriptor
);
2897 struct lttng_uri uris
[2] = {};
2898 size_t uri_count
= 0;
2900 switch (output_type
) {
2901 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2903 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2904 lttng_session_descriptor_get_local_output_uri(descriptor
,
2908 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2909 lttng_session_descriptor_get_network_output_uris(descriptor
,
2910 &uris
[0], &uris
[1]);
2914 ret_code
= LTTNG_ERR_INVALID
;
2918 switch (session_type
) {
2919 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2921 struct snapshot_output
*new_output
= NULL
;
2923 new_output
= snapshot_output_alloc();
2925 ret_code
= LTTNG_ERR_NOMEM
;
2929 ret
= snapshot_output_init_with_uri(session
,
2930 DEFAULT_SNAPSHOT_MAX_SIZE
,
2931 NULL
, uris
, uri_count
, session
->consumer
,
2932 new_output
, &session
->snapshot
);
2934 ret_code
= (ret
== -ENOMEM
) ?
2935 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2936 snapshot_output_destroy(new_output
);
2939 snapshot_add_output(&session
->snapshot
, new_output
);
2942 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2943 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2945 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
2949 ret_code
= LTTNG_ERR_INVALID
;
2957 enum lttng_error_code
cmd_create_session_from_descriptor(
2958 struct lttng_session_descriptor
*descriptor
,
2959 const lttng_sock_cred
*creds
,
2960 const char *home_path
)
2963 enum lttng_error_code ret_code
;
2964 const char *session_name
;
2965 struct ltt_session
*new_session
= NULL
;
2966 enum lttng_session_descriptor_status descriptor_status
;
2968 session_lock_list();
2970 if (*home_path
!= '/') {
2971 ERR("Home path provided by client is not absolute");
2972 ret_code
= LTTNG_ERR_INVALID
;
2977 descriptor_status
= lttng_session_descriptor_get_session_name(
2978 descriptor
, &session_name
);
2979 switch (descriptor_status
) {
2980 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2982 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2983 session_name
= NULL
;
2986 ret_code
= LTTNG_ERR_INVALID
;
2990 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
2992 if (ret_code
!= LTTNG_OK
) {
2996 if (!session_name
) {
2997 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3000 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3005 if (!lttng_session_descriptor_is_output_destination_initialized(
3008 * Only include the session's creation time in the output
3009 * destination if the name of the session itself was
3010 * not auto-generated.
3012 ret_code
= lttng_session_descriptor_set_default_output(
3014 session_name
? &new_session
->creation_time
: NULL
,
3016 if (ret_code
!= LTTNG_OK
) {
3020 new_session
->has_user_specified_directory
=
3021 lttng_session_descriptor_has_output_directory(
3025 switch (lttng_session_descriptor_get_type(descriptor
)) {
3026 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3027 new_session
->snapshot_mode
= 1;
3029 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3030 new_session
->live_timer
=
3031 lttng_session_descriptor_live_get_timer_interval(
3038 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3039 if (ret_code
!= LTTNG_OK
) {
3042 new_session
->consumer
->enabled
= 1;
3043 ret_code
= LTTNG_OK
;
3045 /* Release reference provided by the session_create function. */
3046 session_put(new_session
);
3047 if (ret_code
!= LTTNG_OK
&& new_session
) {
3048 /* Release the global reference on error. */
3049 session_destroy(new_session
);
3051 session_unlock_list();
3055 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3056 struct lttng_session_descriptor
**return_descriptor
)
3059 size_t payload_size
;
3060 struct lttng_dynamic_buffer payload
;
3061 struct lttng_buffer_view home_dir_view
;
3062 struct lttng_buffer_view session_descriptor_view
;
3063 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3064 enum lttng_error_code ret_code
;
3066 lttng_dynamic_buffer_init(&payload
);
3067 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3069 ret_code
= LTTNG_ERR_INVALID
;
3072 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3073 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3074 ret_code
= LTTNG_ERR_INVALID
;
3078 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3079 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3080 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3082 ret_code
= LTTNG_ERR_NOMEM
;
3086 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3088 ERR("Reception of session descriptor failed, aborting.");
3089 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3093 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3096 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3097 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3098 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3099 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3100 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3104 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3106 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3107 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3108 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3109 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3110 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3114 ret
= lttng_session_descriptor_create_from_buffer(
3115 &session_descriptor_view
, &session_descriptor
);
3117 ERR("Failed to create session descriptor from payload of \"create session\" command");
3118 ret_code
= LTTNG_ERR_INVALID
;
3123 * Sets the descriptor's auto-generated properties (name, output) if
3126 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3128 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3129 if (ret_code
!= LTTNG_OK
) {
3133 ret_code
= LTTNG_OK
;
3134 *return_descriptor
= session_descriptor
;
3135 session_descriptor
= NULL
;
3137 lttng_dynamic_buffer_reset(&payload
);
3138 lttng_session_descriptor_destroy(session_descriptor
);
3143 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3144 void *_reply_context
)
3148 const struct cmd_destroy_session_reply_context
*reply_context
=
3149 (cmd_destroy_session_reply_context
*) _reply_context
;
3150 struct lttng_dynamic_buffer payload
;
3151 struct lttcomm_session_destroy_command_header cmd_header
;
3152 struct lttng_trace_archive_location
*location
= NULL
;
3153 struct lttcomm_lttng_msg llm
= {
3154 .cmd_type
= LTTNG_DESTROY_SESSION
,
3155 .ret_code
= reply_context
->destruction_status
,
3158 sizeof(struct lttcomm_session_destroy_command_header
),
3161 size_t payload_size_before_location
;
3163 lttng_dynamic_buffer_init(&payload
);
3165 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3167 ERR("Failed to append session destruction message");
3171 cmd_header
.rotation_state
=
3172 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3173 session
->rotation_state
:
3174 LTTNG_ROTATION_STATE_NO_ROTATION
);
3175 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3176 sizeof(cmd_header
));
3178 ERR("Failed to append session destruction command header");
3182 if (!reply_context
->implicit_rotation_on_destroy
) {
3183 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3187 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3188 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3193 location
= session_get_trace_archive_location(session
);
3195 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3200 payload_size_before_location
= payload
.size
;
3201 comm_ret
= lttng_trace_archive_location_serialize(location
,
3203 lttng_trace_archive_location_put(location
);
3205 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3209 /* Update the message to indicate the location's length. */
3210 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3211 payload
.size
- payload_size_before_location
;
3213 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3214 payload
.data
, payload
.size
);
3215 if (comm_ret
!= (ssize_t
) payload
.size
) {
3216 ERR("Failed to send result of the destruction of session \"%s\" to client",
3220 ret
= close(reply_context
->reply_sock_fd
);
3222 PERROR("Failed to close client socket in deferred session destroy reply");
3224 lttng_dynamic_buffer_reset(&payload
);
3225 free(_reply_context
);
3229 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3231 * Called with session lock held.
3233 int cmd_destroy_session(struct ltt_session
*session
,
3234 struct notification_thread_handle
*notification_thread_handle
,
3238 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3239 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3242 reply_context
= (cmd_destroy_session_reply_context
*) zmalloc(sizeof(*reply_context
));
3243 if (!reply_context
) {
3244 ret
= LTTNG_ERR_NOMEM
;
3247 reply_context
->reply_sock_fd
= *sock_fd
;
3251 LTTNG_ASSERT(session
);
3253 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3255 if (session
->active
) {
3256 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3258 ret
= cmd_stop_trace(session
);
3259 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3260 /* Carry on with the destruction of the session. */
3261 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3262 session
->name
, lttng_strerror(-ret
));
3263 destruction_last_error
= (lttng_error_code
) ret
;
3267 if (session
->rotation_schedule_timer_enabled
) {
3268 if (timer_session_rotation_schedule_timer_stop(
3270 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3272 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3276 if (session
->rotate_size
) {
3277 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3278 session
->rotate_size
= 0;
3281 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3283 * Perform a last rotation on destruction if rotations have
3284 * occurred during the session's lifetime.
3286 ret
= cmd_rotate_session(session
, NULL
, false,
3287 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3288 if (ret
!= LTTNG_OK
) {
3289 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3290 session
->name
, lttng_strerror(-ret
));
3291 destruction_last_error
= (lttng_error_code
) -ret
;
3293 if (reply_context
) {
3294 reply_context
->implicit_rotation_on_destroy
= true;
3296 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3298 * The user has not triggered a session rotation. However, to
3299 * ensure all data has been consumed, the session is rotated
3300 * to a 'null' trace chunk before it is destroyed.
3302 * This is a "quiet" rotation meaning that no notification is
3303 * emitted and no renaming of the current trace chunk takes
3306 ret
= cmd_rotate_session(session
, NULL
, true,
3307 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3309 * Rotation operations may not be supported by the kernel
3310 * tracer. Hence, do not consider this implicit rotation as
3311 * a session destruction error. The library has already stopped
3312 * the session and waited for pending data; there is nothing
3313 * left to do but complete the destruction of the session.
3315 if (ret
!= LTTNG_OK
&&
3316 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3317 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3318 session
->name
, lttng_strerror(ret
));
3319 destruction_last_error
= (lttng_error_code
) -ret
;
3323 if (session
->shm_path
[0]) {
3325 * When a session is created with an explicit shm_path,
3326 * the consumer daemon will create its shared memory files
3327 * at that location and will *not* unlink them. This is normal
3328 * as the intention of that feature is to make it possible
3329 * to retrieve the content of those files should a crash occur.
3331 * To ensure the content of those files can be used, the
3332 * sessiond daemon will replicate the content of the metadata
3333 * cache in a metadata file.
3335 * On clean-up, it is expected that the consumer daemon will
3336 * unlink the shared memory files and that the session daemon
3337 * will unlink the metadata file. Then, the session's directory
3338 * in the shm path can be removed.
3340 * Unfortunately, a flaw in the design of the sessiond's and
3341 * consumerd's tear down of channels makes it impossible to
3342 * determine when the sessiond _and_ the consumerd have both
3343 * destroyed their representation of a channel. For one, the
3344 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3345 * callbacks in both daemons.
3347 * However, it is also impossible for the sessiond to know when
3348 * the consumer daemon is done destroying its channel(s) since
3349 * it occurs as a reaction to the closing of the channel's file
3350 * descriptor. There is no resulting communication initiated
3351 * from the consumerd to the sessiond to confirm that the
3352 * operation is completed (and was successful).
3354 * Until this is all fixed, the session daemon checks for the
3355 * removal of the session's shm path which makes it possible
3356 * to safely advertise a session as having been destroyed.
3358 * Prior to this fix, it was not possible to reliably save
3359 * a session making use of the --shm-path option, destroy it,
3360 * and load it again. This is because the creation of the
3361 * session would fail upon seeing the session's shm path
3362 * already in existence.
3364 * Note that none of the error paths in the check for the
3365 * directory's existence return an error. This is normal
3366 * as there isn't much that can be done. The session will
3367 * be destroyed properly, except that we can't offer the
3368 * guarantee that the same session can be re-created.
3370 current_completion_handler
= &destroy_completion_handler
.handler
;
3371 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3373 sizeof(destroy_completion_handler
.shm_path
));
3378 * The session is destroyed. However, note that the command context
3379 * still holds a reference to the session, thus delaying its destruction
3380 * _at least_ up to the point when that reference is released.
3382 session_destroy(session
);
3383 if (reply_context
) {
3384 reply_context
->destruction_status
= destruction_last_error
;
3385 ret
= session_add_destroy_notifier(session
,
3386 cmd_destroy_session_reply
,
3387 (void *) reply_context
);
3389 ret
= LTTNG_ERR_FATAL
;
3401 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3403 int cmd_register_consumer(struct ltt_session
*session
,
3404 enum lttng_domain_type domain
, const char *sock_path
,
3405 struct consumer_data
*cdata
)
3408 struct consumer_socket
*socket
= NULL
;
3410 LTTNG_ASSERT(session
);
3411 LTTNG_ASSERT(cdata
);
3412 LTTNG_ASSERT(sock_path
);
3415 case LTTNG_DOMAIN_KERNEL
:
3417 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3419 LTTNG_ASSERT(ksess
);
3421 /* Can't register a consumer if there is already one */
3422 if (ksess
->consumer_fds_sent
!= 0) {
3423 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3427 sock
= lttcomm_connect_unix_sock(sock_path
);
3429 ret
= LTTNG_ERR_CONNECT_FAIL
;
3432 cdata
->cmd_sock
= sock
;
3434 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3435 if (socket
== NULL
) {
3438 PERROR("close register consumer");
3440 cdata
->cmd_sock
= -1;
3441 ret
= LTTNG_ERR_FATAL
;
3445 socket
->lock
= (pthread_mutex_t
*) zmalloc(sizeof(pthread_mutex_t
));
3446 if (socket
->lock
== NULL
) {
3447 PERROR("zmalloc pthread mutex");
3448 ret
= LTTNG_ERR_FATAL
;
3451 pthread_mutex_init(socket
->lock
, NULL
);
3452 socket
->registered
= 1;
3455 consumer_add_socket(socket
, ksess
->consumer
);
3458 pthread_mutex_lock(&cdata
->pid_mutex
);
3460 pthread_mutex_unlock(&cdata
->pid_mutex
);
3465 /* TODO: Userspace tracing */
3466 ret
= LTTNG_ERR_UND
;
3474 consumer_destroy_socket(socket
);
3480 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3482 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3483 struct lttng_domain
**domains
)
3488 struct lttng_ht_iter iter
;
3490 if (session
->kernel_session
!= NULL
) {
3491 DBG3("Listing domains found kernel domain");
3495 if (session
->ust_session
!= NULL
) {
3496 DBG3("Listing domains found UST global domain");
3500 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3502 if (agt
->being_used
) {
3513 *domains
= (lttng_domain
*) zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3514 if (*domains
== NULL
) {
3515 ret
= LTTNG_ERR_FATAL
;
3519 if (session
->kernel_session
!= NULL
) {
3520 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3522 /* Kernel session buffer type is always GLOBAL */
3523 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3528 if (session
->ust_session
!= NULL
) {
3529 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3530 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3534 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3536 if (agt
->being_used
) {
3537 (*domains
)[index
].type
= agt
->domain
;
3538 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3548 /* Return negative value to differentiate return code */
3554 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3556 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3557 struct ltt_session
*session
,
3558 struct lttng_payload
*payload
)
3562 struct lttcomm_list_command_header cmd_header
= {};
3563 size_t cmd_header_offset
;
3564 enum lttng_error_code ret_code
;
3569 DBG("Listing channels for session %s", session
->name
);
3571 cmd_header_offset
= payload
->buffer
.size
;
3573 /* Reserve space for command reply header. */
3574 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3575 cmd_header_offset
+ sizeof(cmd_header
));
3577 ret_code
= LTTNG_ERR_NOMEM
;
3582 case LTTNG_DOMAIN_KERNEL
:
3584 /* Kernel channels */
3585 struct ltt_kernel_channel
*kchan
;
3586 if (session
->kernel_session
!= NULL
) {
3587 cds_list_for_each_entry(kchan
,
3588 &session
->kernel_session
->channel_list
.head
, list
) {
3589 uint64_t discarded_events
, lost_packets
;
3590 struct lttng_channel_extended
*extended
;
3592 extended
= (struct lttng_channel_extended
*)
3593 kchan
->channel
->attr
.extended
.ptr
;
3595 ret
= get_kernel_runtime_stats(session
, kchan
,
3596 &discarded_events
, &lost_packets
);
3598 ret_code
= LTTNG_ERR_UNK
;
3603 * Update the discarded_events and lost_packets
3604 * count for the channel
3606 extended
->discarded_events
= discarded_events
;
3607 extended
->lost_packets
= lost_packets
;
3609 ret
= lttng_channel_serialize(
3610 kchan
->channel
, &payload
->buffer
);
3612 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3613 kchan
->channel
->name
);
3614 ret_code
= LTTNG_ERR_UNK
;
3623 case LTTNG_DOMAIN_UST
:
3625 struct lttng_ht_iter iter
;
3626 struct ltt_ust_channel
*uchan
;
3629 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3630 &iter
.iter
, uchan
, node
.node
) {
3631 uint64_t discarded_events
= 0, lost_packets
= 0;
3632 struct lttng_channel
*channel
= NULL
;
3633 struct lttng_channel_extended
*extended
;
3635 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3637 ret
= LTTNG_ERR_NOMEM
;
3641 extended
= (struct lttng_channel_extended
*)
3642 channel
->attr
.extended
.ptr
;
3644 ret
= get_ust_runtime_stats(session
, uchan
,
3645 &discarded_events
, &lost_packets
);
3647 lttng_channel_destroy(channel
);
3648 ret_code
= LTTNG_ERR_UNK
;
3652 extended
->discarded_events
= discarded_events
;
3653 extended
->lost_packets
= lost_packets
;
3655 ret
= lttng_channel_serialize(
3656 channel
, &payload
->buffer
);
3658 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3660 ret_code
= LTTNG_ERR_UNK
;
3674 if (i
> UINT32_MAX
) {
3675 ERR("Channel count would overflow the channel listing command's reply");
3676 ret_code
= LTTNG_ERR_OVERFLOW
;
3680 /* Update command reply header. */
3681 cmd_header
.count
= (uint32_t) i
;
3682 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3683 sizeof(cmd_header
));
3684 ret_code
= LTTNG_OK
;
3691 * Command LTTNG_LIST_EVENTS processed by the client thread.
3693 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3694 struct ltt_session
*session
, char *channel_name
,
3695 struct lttng_payload
*payload
)
3698 ssize_t nb_events
= 0;
3699 struct lttcomm_event_command_header cmd_header
= {};
3700 const size_t cmd_header_offset
= payload
->buffer
.size
;
3702 ret
= lttng_dynamic_buffer_append(
3703 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3705 ret
= LTTNG_ERR_NOMEM
;
3710 case LTTNG_DOMAIN_KERNEL
:
3711 if (session
->kernel_session
!= NULL
) {
3712 nb_events
= list_lttng_kernel_events(channel_name
,
3713 session
->kernel_session
, payload
);
3716 case LTTNG_DOMAIN_UST
:
3718 if (session
->ust_session
!= NULL
) {
3719 nb_events
= list_lttng_ust_global_events(channel_name
,
3720 &session
->ust_session
->domain_global
,
3725 case LTTNG_DOMAIN_LOG4J
:
3726 case LTTNG_DOMAIN_JUL
:
3727 case LTTNG_DOMAIN_PYTHON
:
3728 if (session
->ust_session
) {
3729 struct lttng_ht_iter iter
;
3733 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3734 &iter
.iter
, agt
, node
.node
) {
3735 if (agt
->domain
== domain
) {
3736 nb_events
= list_lttng_agent_events(
3745 ret
= LTTNG_ERR_UND
;
3749 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3750 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3755 /* Return negative value to differentiate return code */
3760 * Using the session list, filled a lttng_session array to send back to the
3761 * client for session listing.
3763 * The session list lock MUST be acquired before calling this function. Use
3764 * session_lock_list() and session_unlock_list().
3766 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3767 size_t session_count
, uid_t uid
, gid_t gid
)
3771 struct ltt_session
*session
;
3772 struct ltt_session_list
*list
= session_get_list();
3773 struct lttng_session_extended
*extended
=
3774 (typeof(extended
)) (&sessions
[session_count
]);
3776 DBG("Getting all available session for UID %d GID %d",
3779 * Iterate over session list and append data after the control struct in
3782 cds_list_for_each_entry(session
, &list
->head
, list
) {
3783 if (!session_get(session
)) {
3787 * Only list the sessions the user can control.
3789 if (!session_access_ok(session
, uid
) ||
3790 session
->destroyed
) {
3791 session_put(session
);
3795 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3796 struct ltt_ust_session
*usess
= session
->ust_session
;
3798 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3799 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3800 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3801 ret
= build_network_session_path(sessions
[i
].path
,
3802 sizeof(sessions
[i
].path
), session
);
3804 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3805 session
->consumer
->dst
.session_root_path
);
3808 PERROR("snprintf session path");
3809 session_put(session
);
3813 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3814 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3815 sessions
[i
].enabled
= session
->active
;
3816 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3817 sessions
[i
].live_timer_interval
= session
->live_timer
;
3818 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3819 extended
[i
].creation_time
.is_set
= 1;
3821 session_put(session
);
3826 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3827 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3829 int cmd_data_pending(struct ltt_session
*session
)
3832 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3833 struct ltt_ust_session
*usess
= session
->ust_session
;
3835 LTTNG_ASSERT(session
);
3837 DBG("Data pending for session %s", session
->name
);
3839 /* Session MUST be stopped to ask for data availability. */
3840 if (session
->active
) {
3841 ret
= LTTNG_ERR_SESSION_STARTED
;
3845 * If stopped, just make sure we've started before else the above call
3846 * will always send that there is data pending.
3848 * The consumer assumes that when the data pending command is received,
3849 * the trace has been started before or else no output data is written
3850 * by the streams which is a condition for data pending. So, this is
3851 * *VERY* important that we don't ask the consumer before a start
3854 if (!session
->has_been_started
) {
3860 /* A rotation is still pending, we have to wait. */
3861 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3862 DBG("Rotate still pending for session %s", session
->name
);
3867 if (ksess
&& ksess
->consumer
) {
3868 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3870 /* Data is still being extracted for the kernel. */
3875 if (usess
&& usess
->consumer
) {
3876 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3878 /* Data is still being extracted for the kernel. */
3883 /* Data is ready to be read by a viewer */
3891 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3893 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3895 int cmd_snapshot_add_output(struct ltt_session
*session
,
3896 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3899 struct snapshot_output
*new_output
;
3901 LTTNG_ASSERT(session
);
3902 LTTNG_ASSERT(output
);
3904 DBG("Cmd snapshot add output for session %s", session
->name
);
3907 * Can't create an output if the session is not set in no-output mode.
3909 if (session
->output_traces
) {
3910 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3914 if (session
->has_non_mmap_channel
) {
3915 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3919 /* Only one output is allowed until we have the "tee" feature. */
3920 if (session
->snapshot
.nb_output
== 1) {
3921 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3925 new_output
= snapshot_output_alloc();
3927 ret
= LTTNG_ERR_NOMEM
;
3931 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3932 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3933 &session
->snapshot
);
3935 if (ret
== -ENOMEM
) {
3936 ret
= LTTNG_ERR_NOMEM
;
3938 ret
= LTTNG_ERR_INVALID
;
3944 snapshot_add_output(&session
->snapshot
, new_output
);
3946 *id
= new_output
->id
;
3953 snapshot_output_destroy(new_output
);
3959 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3961 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3963 int cmd_snapshot_del_output(struct ltt_session
*session
,
3964 const struct lttng_snapshot_output
*output
)
3967 struct snapshot_output
*sout
= NULL
;
3969 LTTNG_ASSERT(session
);
3970 LTTNG_ASSERT(output
);
3975 * Permission denied to create an output if the session is not
3976 * set in no output mode.
3978 if (session
->output_traces
) {
3979 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3984 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3986 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3987 } else if (*output
->name
!= '\0') {
3988 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3990 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3993 ret
= LTTNG_ERR_INVALID
;
3997 snapshot_delete_output(&session
->snapshot
, sout
);
3998 snapshot_output_destroy(sout
);
4007 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4009 * If no output is available, outputs is untouched and 0 is returned.
4011 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4013 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4014 struct lttng_snapshot_output
**outputs
)
4017 struct lttng_snapshot_output
*list
= NULL
;
4018 struct lttng_ht_iter iter
;
4019 struct snapshot_output
*output
;
4021 LTTNG_ASSERT(session
);
4022 LTTNG_ASSERT(outputs
);
4024 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4027 * Permission denied to create an output if the session is not
4028 * set in no output mode.
4030 if (session
->output_traces
) {
4031 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4035 if (session
->snapshot
.nb_output
== 0) {
4040 list
= (lttng_snapshot_output
*) zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4042 ret
= -LTTNG_ERR_NOMEM
;
4046 /* Copy list from session to the new list object. */
4048 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4049 output
, node
.node
) {
4050 LTTNG_ASSERT(output
->consumer
);
4051 list
[idx
].id
= output
->id
;
4052 list
[idx
].max_size
= output
->max_size
;
4053 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4054 sizeof(list
[idx
].name
))) {
4055 ret
= -LTTNG_ERR_INVALID
;
4058 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4059 if (lttng_strncpy(list
[idx
].ctrl_url
,
4060 output
->consumer
->dst
.session_root_path
,
4061 sizeof(list
[idx
].ctrl_url
))) {
4062 ret
= -LTTNG_ERR_INVALID
;
4067 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4068 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4070 ret
= -LTTNG_ERR_NOMEM
;
4075 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4076 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4078 ret
= -LTTNG_ERR_NOMEM
;
4087 ret
= session
->snapshot
.nb_output
;
4096 * Check if we can regenerate the metadata for this session.
4097 * Only kernel, UST per-uid and non-live sessions are supported.
4099 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4102 int check_regenerate_metadata_support(struct ltt_session
*session
)
4106 LTTNG_ASSERT(session
);
4108 if (session
->live_timer
!= 0) {
4109 ret
= LTTNG_ERR_LIVE_SESSION
;
4112 if (!session
->active
) {
4113 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4116 if (session
->ust_session
) {
4117 switch (session
->ust_session
->buffer_type
) {
4118 case LTTNG_BUFFER_PER_UID
:
4120 case LTTNG_BUFFER_PER_PID
:
4121 ret
= LTTNG_ERR_PER_PID_SESSION
;
4125 ret
= LTTNG_ERR_UNK
;
4129 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4130 session
->consumer
->relay_minor_version
< 8) {
4131 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4141 int clear_metadata_file(int fd
)
4146 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4147 if (lseek_ret
< 0) {
4153 ret
= ftruncate(fd
, 0);
4155 PERROR("ftruncate");
4164 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4167 struct buffer_reg_uid
*uid_reg
= NULL
;
4168 struct buffer_reg_session
*session_reg
= NULL
;
4171 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4172 struct ust_registry_session
*registry
;
4173 struct ust_registry_channel
*chan
;
4174 struct lttng_ht_iter iter_chan
;
4176 session_reg
= uid_reg
->registry
;
4177 registry
= session_reg
->reg
.ust
;
4179 pthread_mutex_lock(®istry
->lock
);
4180 registry
->metadata_len_sent
= 0;
4181 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4182 registry
->metadata_len
= 0;
4183 registry
->metadata_version
++;
4184 if (registry
->metadata_fd
> 0) {
4185 /* Clear the metadata file's content. */
4186 ret
= clear_metadata_file(registry
->metadata_fd
);
4188 pthread_mutex_unlock(®istry
->lock
);
4193 ret
= ust_metadata_session_statedump(registry
, NULL
,
4194 registry
->major
, registry
->minor
);
4196 pthread_mutex_unlock(®istry
->lock
);
4197 ERR("Failed to generate session metadata (err = %d)",
4201 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4203 struct ust_registry_event
*event
;
4204 struct lttng_ht_iter iter_event
;
4206 ret
= ust_metadata_channel_statedump(registry
, chan
);
4208 pthread_mutex_unlock(®istry
->lock
);
4209 ERR("Failed to generate channel metadata "
4213 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4215 ret
= ust_metadata_event_statedump(registry
,
4218 pthread_mutex_unlock(®istry
->lock
);
4219 ERR("Failed to generate event metadata "
4225 pthread_mutex_unlock(®istry
->lock
);
4234 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4236 * Ask the consumer to truncate the existing metadata file(s) and
4237 * then regenerate the metadata. Live and per-pid sessions are not
4238 * supported and return an error.
4240 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4242 int cmd_regenerate_metadata(struct ltt_session
*session
)
4246 LTTNG_ASSERT(session
);
4248 ret
= check_regenerate_metadata_support(session
);
4253 if (session
->kernel_session
) {
4254 ret
= kernctl_session_regenerate_metadata(
4255 session
->kernel_session
->fd
);
4257 ERR("Failed to regenerate the kernel metadata");
4262 if (session
->ust_session
) {
4263 ret
= ust_regenerate_metadata(session
->ust_session
);
4265 ERR("Failed to regenerate the UST metadata");
4269 DBG("Cmd metadata regenerate for session %s", session
->name
);
4277 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4279 * Ask the tracer to regenerate a new statedump.
4281 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4283 int cmd_regenerate_statedump(struct ltt_session
*session
)
4287 LTTNG_ASSERT(session
);
4289 if (!session
->active
) {
4290 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4294 if (session
->kernel_session
) {
4295 ret
= kernctl_session_regenerate_statedump(
4296 session
->kernel_session
->fd
);
4298 * Currently, the statedump in kernel can only fail if out
4302 if (ret
== -ENOMEM
) {
4303 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4305 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4307 ERR("Failed to regenerate the kernel statedump");
4312 if (session
->ust_session
) {
4313 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4315 * Currently, the statedump in UST always returns 0.
4318 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4319 ERR("Failed to regenerate the UST statedump");
4323 DBG("Cmd regenerate statedump for session %s", session
->name
);
4331 enum lttng_error_code
synchronize_tracer_notifier_register(
4332 struct notification_thread_handle
*notification_thread
,
4333 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4335 enum lttng_error_code ret_code
;
4336 const struct lttng_condition
*condition
=
4337 lttng_trigger_get_const_condition(trigger
);
4338 const char *trigger_name
;
4339 uid_t trigger_owner
;
4340 enum lttng_trigger_status trigger_status
;
4341 const enum lttng_domain_type trigger_domain
=
4342 lttng_trigger_get_underlying_domain_type_restriction(
4345 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4346 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4348 LTTNG_ASSERT(condition
);
4349 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4350 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4352 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4353 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4354 trigger_name
: "(anonymous)";
4356 session_lock_list();
4357 switch (trigger_domain
) {
4358 case LTTNG_DOMAIN_KERNEL
:
4360 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4361 if (ret_code
!= LTTNG_OK
) {
4362 enum lttng_error_code notif_thread_unregister_ret
;
4364 notif_thread_unregister_ret
=
4365 notification_thread_command_unregister_trigger(
4366 notification_thread
, trigger
);
4368 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4369 /* Return the original error code. */
4370 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4372 (int) trigger_owner
,
4378 case LTTNG_DOMAIN_UST
:
4379 ust_app_global_update_all_event_notifier_rules();
4381 case LTTNG_DOMAIN_JUL
:
4382 case LTTNG_DOMAIN_LOG4J
:
4383 case LTTNG_DOMAIN_PYTHON
:
4385 /* Agent domains. */
4386 struct agent
*agt
= agent_find_by_event_notifier_domain(
4390 agt
= agent_create(trigger_domain
);
4392 ret_code
= LTTNG_ERR_NOMEM
;
4393 goto end_unlock_session_list
;
4396 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4399 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4400 if (ret_code
!= LTTNG_OK
) {
4401 goto end_unlock_session_list
;
4406 case LTTNG_DOMAIN_NONE
:
4411 ret_code
= LTTNG_OK
;
4412 end_unlock_session_list
:
4413 session_unlock_list();
4417 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4418 struct lttng_trigger
*trigger
,
4419 bool is_trigger_anonymous
,
4420 struct notification_thread_handle
*notification_thread
,
4421 struct lttng_trigger
**return_trigger
)
4423 enum lttng_error_code ret_code
;
4424 const char *trigger_name
;
4425 uid_t trigger_owner
;
4426 enum lttng_trigger_status trigger_status
;
4428 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4429 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4430 trigger_name
: "(anonymous)";
4432 trigger_status
= lttng_trigger_get_owner_uid(
4433 trigger
, &trigger_owner
);
4434 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4436 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4437 trigger_name
, (int) trigger_owner
,
4438 (int) lttng_credentials_get_uid(cmd_creds
));
4441 * Validate the trigger credentials against the command credentials.
4442 * Only the root user can register a trigger with non-matching
4445 if (!lttng_credentials_is_equal_uid(
4446 lttng_trigger_get_credentials(trigger
),
4448 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4449 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4450 trigger_name
, (int) trigger_owner
,
4451 (int) lttng_credentials_get_uid(cmd_creds
));
4452 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4458 * The bytecode generation also serves as a validation step for the
4459 * bytecode expressions.
4461 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4462 if (ret_code
!= LTTNG_OK
) {
4463 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4464 trigger_name
, (int) trigger_owner
, ret_code
);
4469 * A reference to the trigger is acquired by the notification thread.
4470 * It is safe to return the same trigger to the caller since it the
4471 * other user holds a reference.
4473 * The trigger is modified during the execution of the
4474 * "register trigger" command. However, by the time the command returns,
4475 * it is safe to use without any locking as its properties are
4478 ret_code
= notification_thread_command_register_trigger(
4479 notification_thread
, trigger
, is_trigger_anonymous
);
4480 if (ret_code
!= LTTNG_OK
) {
4481 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4482 trigger_name
, (int) trigger_owner
, ret_code
);
4486 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4487 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4488 trigger_name
: "(anonymous)";
4491 * Synchronize tracers if the trigger adds an event notifier.
4493 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4494 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4495 trigger
, cmd_creds
);
4496 if (ret_code
!= LTTNG_OK
) {
4497 ERR("Error registering tracer notifier: %s",
4498 lttng_strerror(-ret_code
));
4504 * Return an updated trigger to the client.
4506 * Since a modified version of the same trigger is returned, acquire a
4507 * reference to the trigger so the caller doesn't have to care if those
4508 * are distinct instances or not.
4510 if (ret_code
== LTTNG_OK
) {
4511 lttng_trigger_get(trigger
);
4512 *return_trigger
= trigger
;
4513 /* Ownership of trigger was transferred to caller. */
4521 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4522 const struct lttng_trigger
*trigger
)
4524 enum lttng_error_code ret_code
;
4525 const struct lttng_condition
*condition
=
4526 lttng_trigger_get_const_condition(trigger
);
4527 const enum lttng_domain_type trigger_domain
=
4528 lttng_trigger_get_underlying_domain_type_restriction(
4531 LTTNG_ASSERT(condition
);
4532 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4533 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4535 session_lock_list();
4536 switch (trigger_domain
) {
4537 case LTTNG_DOMAIN_KERNEL
:
4538 ret_code
= kernel_unregister_event_notifier(trigger
);
4539 if (ret_code
!= LTTNG_OK
) {
4540 goto end_unlock_session_list
;
4544 case LTTNG_DOMAIN_UST
:
4545 ust_app_global_update_all_event_notifier_rules();
4547 case LTTNG_DOMAIN_JUL
:
4548 case LTTNG_DOMAIN_LOG4J
:
4549 case LTTNG_DOMAIN_PYTHON
:
4551 /* Agent domains. */
4552 struct agent
*agt
= agent_find_by_event_notifier_domain(
4556 * This trigger was never registered in the first place. Calling
4557 * this function under those circumstances is an internal error.
4560 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4561 if (ret_code
!= LTTNG_OK
) {
4562 goto end_unlock_session_list
;
4567 case LTTNG_DOMAIN_NONE
:
4572 ret_code
= LTTNG_OK
;
4574 end_unlock_session_list
:
4575 session_unlock_list();
4579 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4580 const struct lttng_trigger
*trigger
,
4581 struct notification_thread_handle
*notification_thread
)
4583 enum lttng_error_code ret_code
;
4584 const char *trigger_name
;
4585 uid_t trigger_owner
;
4586 enum lttng_trigger_status trigger_status
;
4587 struct lttng_trigger
*sessiond_trigger
= NULL
;
4589 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4590 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4591 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4592 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4594 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4595 trigger_name
, (int) trigger_owner
,
4596 (int) lttng_credentials_get_uid(cmd_creds
));
4599 * Validate the trigger credentials against the command credentials.
4600 * Only the root user can unregister a trigger with non-matching
4603 if (!lttng_credentials_is_equal_uid(
4604 lttng_trigger_get_credentials(trigger
),
4606 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4607 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4608 trigger_name
, (int) trigger_owner
,
4609 (int) lttng_credentials_get_uid(cmd_creds
));
4610 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4615 /* Fetch the sessiond side trigger object. */
4616 ret_code
= notification_thread_command_get_trigger(
4617 notification_thread
, trigger
, &sessiond_trigger
);
4618 if (ret_code
!= LTTNG_OK
) {
4619 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4620 trigger_name
, (int) trigger_owner
, ret_code
);
4624 LTTNG_ASSERT(sessiond_trigger
);
4627 * From this point on, no matter what, consider the trigger
4630 * We set the unregistered state of the sessiond side trigger object in
4631 * the client thread since we want to minimize the possibility of the
4632 * notification thread being stalled due to a long execution of an
4633 * action that required the trigger lock.
4635 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4637 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4639 if (ret_code
!= LTTNG_OK
) {
4640 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4641 trigger_name
, (int) trigger_owner
, ret_code
);
4646 * Synchronize tracers if the trigger removes an event notifier.
4647 * Do this even if the trigger unregistration failed to at least stop
4648 * the tracers from producing notifications associated with this
4651 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4652 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4653 if (ret_code
!= LTTNG_OK
) {
4654 ERR("Error unregistering trigger to tracer.");
4661 lttng_trigger_put(sessiond_trigger
);
4665 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4666 struct notification_thread_handle
*notification_thread
,
4667 struct lttng_triggers
**return_triggers
)
4670 enum lttng_error_code ret_code
;
4671 struct lttng_triggers
*triggers
= NULL
;
4673 /* Get the set of triggers from the notification thread. */
4674 ret_code
= notification_thread_command_list_triggers(
4675 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4676 if (ret_code
!= LTTNG_OK
) {
4680 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4682 ret_code
= LTTNG_ERR_UNK
;
4686 *return_triggers
= triggers
;
4688 ret_code
= LTTNG_OK
;
4690 lttng_triggers_destroy(triggers
);
4694 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4695 const struct lttng_error_query
*query
,
4696 struct lttng_error_query_results
**_results
,
4697 struct notification_thread_handle
*notification_thread
)
4699 enum lttng_error_code ret_code
;
4700 const struct lttng_trigger
*query_target_trigger
;
4701 const struct lttng_action
*query_target_action
= NULL
;
4702 struct lttng_trigger
*matching_trigger
= NULL
;
4703 const char *trigger_name
;
4704 uid_t trigger_owner
;
4705 enum lttng_trigger_status trigger_status
;
4706 struct lttng_error_query_results
*results
= NULL
;
4708 switch (lttng_error_query_get_target_type(query
)) {
4709 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4710 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4712 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4713 query_target_trigger
=
4714 lttng_error_query_condition_borrow_target(query
);
4716 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4717 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4724 LTTNG_ASSERT(query_target_trigger
);
4726 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4727 query_target_trigger
, &matching_trigger
);
4728 if (ret_code
!= LTTNG_OK
) {
4732 /* No longer needed. */
4733 query_target_trigger
= NULL
;
4735 if (lttng_error_query_get_target_type(query
) ==
4736 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4737 /* Get the sessiond-side version of the target action. */
4738 query_target_action
=
4739 lttng_error_query_action_borrow_action_target(
4740 query
, matching_trigger
);
4743 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4744 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4745 trigger_name
: "(anonymous)";
4746 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4748 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4750 results
= lttng_error_query_results_create();
4752 ret_code
= LTTNG_ERR_NOMEM
;
4756 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4757 trigger_name
, (int) trigger_owner
,
4758 (int) lttng_credentials_get_uid(cmd_creds
));
4761 * Validate the trigger credentials against the command credentials.
4762 * Only the root user can target a trigger with non-matching
4765 if (!lttng_credentials_is_equal_uid(
4766 lttng_trigger_get_credentials(matching_trigger
),
4768 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4769 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4770 trigger_name
, (int) trigger_owner
,
4771 (int) lttng_credentials_get_uid(cmd_creds
));
4772 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4777 switch (lttng_error_query_get_target_type(query
)) {
4778 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4779 trigger_status
= lttng_trigger_add_error_results(
4780 matching_trigger
, results
);
4782 switch (trigger_status
) {
4783 case LTTNG_TRIGGER_STATUS_OK
:
4786 ret_code
= LTTNG_ERR_UNK
;
4791 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4793 trigger_status
= lttng_trigger_condition_add_error_results(
4794 matching_trigger
, results
);
4796 switch (trigger_status
) {
4797 case LTTNG_TRIGGER_STATUS_OK
:
4800 ret_code
= LTTNG_ERR_UNK
;
4806 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4808 const enum lttng_action_status action_status
=
4809 lttng_action_add_error_query_results(
4810 query_target_action
, results
);
4812 switch (action_status
) {
4813 case LTTNG_ACTION_STATUS_OK
:
4816 ret_code
= LTTNG_ERR_UNK
;
4827 *_results
= results
;
4829 ret_code
= LTTNG_OK
;
4831 lttng_trigger_put(matching_trigger
);
4832 lttng_error_query_results_destroy(results
);
4837 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4838 * snapshot output is *not* set with a remote destination.
4840 * Return LTTNG_OK on success or a LTTNG_ERR code.
4842 static enum lttng_error_code
set_relayd_for_snapshot(
4843 struct consumer_output
*output
,
4844 const struct ltt_session
*session
)
4846 enum lttng_error_code status
= LTTNG_OK
;
4847 struct lttng_ht_iter iter
;
4848 struct consumer_socket
*socket
;
4849 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4850 const char *base_path
;
4852 LTTNG_ASSERT(output
);
4853 LTTNG_ASSERT(session
);
4855 DBG2("Set relayd object from snapshot output");
4857 if (session
->current_trace_chunk
) {
4858 enum lttng_trace_chunk_status chunk_status
=
4859 lttng_trace_chunk_get_id(
4860 session
->current_trace_chunk
,
4861 ¤t_chunk_id
.value
);
4863 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4864 current_chunk_id
.is_set
= true;
4866 ERR("Failed to get current trace chunk id");
4867 status
= LTTNG_ERR_UNK
;
4872 /* Ignore if snapshot consumer output is not network. */
4873 if (output
->type
!= CONSUMER_DST_NET
) {
4878 * The snapshot record URI base path overrides the session
4881 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4882 base_path
= output
->dst
.net
.control
.subdir
;
4884 base_path
= session
->base_path
;
4888 * For each consumer socket, create and send the relayd object of the
4892 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4893 socket
, node
.node
) {
4894 pthread_mutex_lock(socket
->lock
);
4895 status
= send_consumer_relayd_sockets(LTTNG_DOMAIN_NONE
, session
->id
,
4897 session
->name
, session
->hostname
,
4899 session
->live_timer
,
4900 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4901 session
->creation_time
,
4902 session
->name_contains_creation_time
);
4903 pthread_mutex_unlock(socket
->lock
);
4904 if (status
!= LTTNG_OK
) {
4916 * Record a kernel snapshot.
4918 * Return LTTNG_OK on success or a LTTNG_ERR code.
4920 static enum lttng_error_code
record_kernel_snapshot(
4921 struct ltt_kernel_session
*ksess
,
4922 const struct consumer_output
*output
,
4923 const struct ltt_session
*session
,
4924 int wait
, uint64_t nb_packets_per_stream
)
4926 enum lttng_error_code status
;
4928 LTTNG_ASSERT(ksess
);
4929 LTTNG_ASSERT(output
);
4930 LTTNG_ASSERT(session
);
4932 status
= kernel_snapshot_record(
4933 ksess
, output
, wait
, nb_packets_per_stream
);
4938 * Record a UST snapshot.
4940 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4942 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4943 const struct consumer_output
*output
,
4944 const struct ltt_session
*session
,
4945 int wait
, uint64_t nb_packets_per_stream
)
4947 enum lttng_error_code status
;
4949 LTTNG_ASSERT(usess
);
4950 LTTNG_ASSERT(output
);
4951 LTTNG_ASSERT(session
);
4953 status
= ust_app_snapshot_record(
4954 usess
, output
, wait
, nb_packets_per_stream
);
4959 uint64_t get_session_size_one_more_packet_per_stream(
4960 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4962 uint64_t tot_size
= 0;
4964 if (session
->kernel_session
) {
4965 struct ltt_kernel_channel
*chan
;
4966 const struct ltt_kernel_session
*ksess
=
4967 session
->kernel_session
;
4969 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4970 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4972 * Don't take channel into account if we
4973 * already grab all its packets.
4977 tot_size
+= chan
->channel
->attr
.subbuf_size
4978 * chan
->stream_count
;
4982 if (session
->ust_session
) {
4983 const struct ltt_ust_session
*usess
= session
->ust_session
;
4985 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4993 * Calculate the number of packets we can grab from each stream that
4994 * fits within the overall snapshot max size.
4996 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4997 * the number of packets per stream.
4999 * TODO: this approach is not perfect: we consider the worse case
5000 * (packet filling the sub-buffers) as an upper bound, but we could do
5001 * better if we do this calculation while we actually grab the packet
5002 * content: we would know how much padding we don't actually store into
5005 * This algorithm is currently bounded by the number of packets per
5008 * Since we call this algorithm before actually grabbing the data, it's
5009 * an approximation: for instance, applications could appear/disappear
5010 * in between this call and actually grabbing data.
5013 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5017 uint64_t cur_nb_packets
= 0;
5020 return 0; /* Infinite */
5023 size_left
= max_size
;
5025 uint64_t one_more_packet_tot_size
;
5027 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5028 session
, cur_nb_packets
);
5029 if (!one_more_packet_tot_size
) {
5030 /* We are already grabbing all packets. */
5033 size_left
-= one_more_packet_tot_size
;
5034 if (size_left
< 0) {
5039 if (!cur_nb_packets
&& size_left
!= max_size
) {
5040 /* Not enough room to grab one packet of each stream, error. */
5043 return cur_nb_packets
;
5047 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5048 const struct snapshot_output
*snapshot_output
, int wait
)
5050 int64_t nb_packets_per_stream
;
5051 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5053 enum lttng_error_code ret_code
= LTTNG_OK
;
5054 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5055 struct consumer_output
*original_ust_consumer_output
= NULL
;
5056 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5057 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5058 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5060 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5062 snapshot_output
->name
,
5063 snapshot_output
->datetime
,
5064 snapshot_output
->nb_snapshot
);
5065 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5066 ERR("Failed to format snapshot name");
5067 ret_code
= LTTNG_ERR_INVALID
;
5070 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5071 snapshot_output
->name
, session
->name
,
5072 snapshot_chunk_name
);
5073 if (!session
->kernel_session
&& !session
->ust_session
) {
5074 ERR("Failed to record snapshot as no channels exist");
5075 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5079 if (session
->kernel_session
) {
5080 original_kernel_consumer_output
=
5081 session
->kernel_session
->consumer
;
5082 snapshot_kernel_consumer_output
=
5083 consumer_copy_output(snapshot_output
->consumer
);
5084 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5085 snapshot_chunk_name
);
5087 /* Copy the original domain subdir. */
5088 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5089 original_kernel_consumer_output
->domain_subdir
);
5091 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5092 original_kernel_consumer_output
);
5094 ERR("Failed to copy consumer sockets from snapshot output configuration");
5095 ret_code
= LTTNG_ERR_NOMEM
;
5098 ret_code
= set_relayd_for_snapshot(
5099 snapshot_kernel_consumer_output
, session
);
5100 if (ret_code
!= LTTNG_OK
) {
5101 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5104 session
->kernel_session
->consumer
=
5105 snapshot_kernel_consumer_output
;
5107 if (session
->ust_session
) {
5108 original_ust_consumer_output
= session
->ust_session
->consumer
;
5109 snapshot_ust_consumer_output
=
5110 consumer_copy_output(snapshot_output
->consumer
);
5111 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5112 snapshot_chunk_name
);
5114 /* Copy the original domain subdir. */
5115 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5116 original_ust_consumer_output
->domain_subdir
);
5118 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5119 original_ust_consumer_output
);
5121 ERR("Failed to copy consumer sockets from snapshot output configuration");
5122 ret_code
= LTTNG_ERR_NOMEM
;
5125 ret_code
= set_relayd_for_snapshot(
5126 snapshot_ust_consumer_output
, session
);
5127 if (ret_code
!= LTTNG_OK
) {
5128 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5131 session
->ust_session
->consumer
=
5132 snapshot_ust_consumer_output
;
5135 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5136 snapshot_kernel_consumer_output
?:
5137 snapshot_ust_consumer_output
,
5138 consumer_output_get_base_path(
5139 snapshot_output
->consumer
),
5140 snapshot_chunk_name
);
5141 if (!snapshot_trace_chunk
) {
5142 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5144 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5147 LTTNG_ASSERT(!session
->current_trace_chunk
);
5148 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5149 lttng_trace_chunk_put(snapshot_trace_chunk
);
5150 snapshot_trace_chunk
= NULL
;
5152 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5154 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5158 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5159 snapshot_output
->max_size
);
5160 if (nb_packets_per_stream
< 0) {
5161 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5162 goto error_close_trace_chunk
;
5165 if (session
->kernel_session
) {
5166 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5167 snapshot_kernel_consumer_output
, session
,
5168 wait
, nb_packets_per_stream
);
5169 if (ret_code
!= LTTNG_OK
) {
5170 goto error_close_trace_chunk
;
5174 if (session
->ust_session
) {
5175 ret_code
= record_ust_snapshot(session
->ust_session
,
5176 snapshot_ust_consumer_output
, session
,
5177 wait
, nb_packets_per_stream
);
5178 if (ret_code
!= LTTNG_OK
) {
5179 goto error_close_trace_chunk
;
5183 error_close_trace_chunk
:
5184 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5185 ERR("Failed to release the current trace chunk of session \"%s\"",
5187 ret_code
= LTTNG_ERR_UNK
;
5190 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5191 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5193 * Don't goto end; make sure the chunk is closed for the session
5194 * to allow future snapshots.
5196 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5198 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5201 lttng_trace_chunk_put(snapshot_trace_chunk
);
5202 snapshot_trace_chunk
= NULL
;
5204 if (original_ust_consumer_output
) {
5205 session
->ust_session
->consumer
= original_ust_consumer_output
;
5207 if (original_kernel_consumer_output
) {
5208 session
->kernel_session
->consumer
=
5209 original_kernel_consumer_output
;
5211 consumer_output_put(snapshot_ust_consumer_output
);
5212 consumer_output_put(snapshot_kernel_consumer_output
);
5217 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5219 * The wait parameter is ignored so this call always wait for the snapshot to
5220 * complete before returning.
5222 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5224 int cmd_snapshot_record(struct ltt_session
*session
,
5225 const struct lttng_snapshot_output
*output
, int wait
)
5227 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5229 unsigned int snapshot_success
= 0;
5231 struct snapshot_output
*tmp_output
= NULL
;
5233 LTTNG_ASSERT(session
);
5234 LTTNG_ASSERT(output
);
5236 DBG("Cmd snapshot record for session %s", session
->name
);
5238 /* Get the datetime for the snapshot output directory. */
5239 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5242 cmd_ret
= LTTNG_ERR_INVALID
;
5247 * Permission denied to create an output if the session is not
5248 * set in no output mode.
5250 if (session
->output_traces
) {
5251 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5255 /* The session needs to be started at least once. */
5256 if (!session
->has_been_started
) {
5257 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5261 /* Use temporary output for the session. */
5262 if (*output
->ctrl_url
!= '\0') {
5263 tmp_output
= snapshot_output_alloc();
5265 cmd_ret
= LTTNG_ERR_NOMEM
;
5269 ret
= snapshot_output_init(session
, output
->max_size
,
5271 output
->ctrl_url
, output
->data_url
,
5275 if (ret
== -ENOMEM
) {
5276 cmd_ret
= LTTNG_ERR_NOMEM
;
5278 cmd_ret
= LTTNG_ERR_INVALID
;
5282 /* Use the global session count for the temporary snapshot. */
5283 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5285 /* Use the global datetime */
5286 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5287 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5288 if (cmd_ret
!= LTTNG_OK
) {
5291 snapshot_success
= 1;
5293 struct snapshot_output
*sout
;
5294 struct lttng_ht_iter iter
;
5297 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5298 &iter
.iter
, sout
, node
.node
) {
5299 struct snapshot_output output_copy
;
5302 * Make a local copy of the output and override output
5303 * parameters with those provided as part of the
5306 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5308 if (output
->max_size
!= (uint64_t) -1ULL) {
5309 output_copy
.max_size
= output
->max_size
;
5312 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5313 memcpy(output_copy
.datetime
, datetime
,
5316 /* Use temporary name. */
5317 if (*output
->name
!= '\0') {
5318 if (lttng_strncpy(output_copy
.name
,
5320 sizeof(output_copy
.name
))) {
5321 cmd_ret
= LTTNG_ERR_INVALID
;
5327 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5328 if (cmd_ret
!= LTTNG_OK
) {
5332 snapshot_success
= 1;
5337 if (snapshot_success
) {
5338 session
->snapshot
.nb_snapshot
++;
5340 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5345 snapshot_output_destroy(tmp_output
);
5351 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5353 int cmd_set_session_shm_path(struct ltt_session
*session
,
5354 const char *shm_path
)
5357 LTTNG_ASSERT(session
);
5360 * Can only set shm path before session is started.
5362 if (session
->has_been_started
) {
5363 return LTTNG_ERR_SESSION_STARTED
;
5366 strncpy(session
->shm_path
, shm_path
,
5367 sizeof(session
->shm_path
));
5368 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5374 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5376 * Ask the consumer to rotate the session output directory.
5377 * The session lock must be held.
5379 * Returns LTTNG_OK on success or else a negative LTTng error code.
5381 int cmd_rotate_session(struct ltt_session
*session
,
5382 struct lttng_rotate_session_return
*rotate_return
,
5383 bool quiet_rotation
,
5384 enum lttng_trace_chunk_command_type command
)
5387 uint64_t ongoing_rotation_chunk_id
;
5388 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5389 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5390 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5391 enum lttng_trace_chunk_status chunk_status
;
5392 bool failed_to_rotate
= false;
5393 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5395 LTTNG_ASSERT(session
);
5397 if (!session
->has_been_started
) {
5398 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5403 * Explicit rotation is not supported for live sessions.
5404 * However, live sessions can perform a quiet rotation on
5406 * Rotation is not supported for snapshot traces (no output).
5408 if ((!quiet_rotation
&& session
->live_timer
) ||
5409 !session
->output_traces
) {
5410 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5414 /* Unsupported feature in lttng-relayd before 2.11. */
5415 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5416 (session
->consumer
->relay_major_version
== 2 &&
5417 session
->consumer
->relay_minor_version
< 11)) {
5418 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5422 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5423 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5424 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5428 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5429 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5431 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5436 * After a stop, we only allow one rotation to occur, the other ones are
5437 * useless until a new start.
5439 if (session
->rotated_after_last_stop
) {
5440 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5442 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5447 * After a stop followed by a clear, disallow following rotations a they would
5448 * generate empty chunks.
5450 if (session
->cleared_after_last_stop
) {
5451 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5453 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5457 if (session
->active
) {
5458 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5460 if (!new_trace_chunk
) {
5461 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5467 * The current trace chunk becomes the chunk being archived.
5469 * After this point, "chunk_being_archived" must absolutely
5470 * be closed on the consumer(s), otherwise it will never be
5471 * cleaned-up, which will result in a leak.
5473 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5474 &chunk_being_archived
);
5476 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5480 if (session
->kernel_session
) {
5481 cmd_ret
= kernel_rotate_session(session
);
5482 if (cmd_ret
!= LTTNG_OK
) {
5483 failed_to_rotate
= true;
5484 rotation_fail_code
= cmd_ret
;
5487 if (session
->ust_session
) {
5488 cmd_ret
= ust_app_rotate_session(session
);
5489 if (cmd_ret
!= LTTNG_OK
) {
5490 failed_to_rotate
= true;
5491 rotation_fail_code
= cmd_ret
;
5495 if (!session
->active
) {
5496 session
->rotated_after_last_stop
= true;
5499 if (!chunk_being_archived
) {
5500 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5502 if (failed_to_rotate
) {
5503 cmd_ret
= rotation_fail_code
;
5510 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5511 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5512 &ongoing_rotation_chunk_id
);
5513 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5515 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5516 command
, session
->last_chunk_path
);
5518 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5522 if (failed_to_rotate
) {
5523 cmd_ret
= rotation_fail_code
;
5527 session
->quiet_rotation
= quiet_rotation
;
5528 ret
= timer_session_rotation_pending_check_start(session
,
5529 DEFAULT_ROTATE_PENDING_TIMER
);
5531 cmd_ret
= LTTNG_ERR_UNK
;
5535 if (rotate_return
) {
5536 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5539 session
->chunk_being_archived
= chunk_being_archived
;
5540 chunk_being_archived
= NULL
;
5541 if (!quiet_rotation
) {
5542 ret
= notification_thread_command_session_rotation_ongoing(
5543 the_notification_thread_handle
, session
->name
,
5544 session
->uid
, session
->gid
,
5545 ongoing_rotation_chunk_id
);
5546 if (ret
!= LTTNG_OK
) {
5547 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5549 cmd_ret
= (lttng_error_code
) ret
;
5553 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5554 session
->name
, ongoing_rotation_chunk_id
);
5556 lttng_trace_chunk_put(new_trace_chunk
);
5557 lttng_trace_chunk_put(chunk_being_archived
);
5558 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5561 if (session_reset_rotation_state(session
,
5562 LTTNG_ROTATION_STATE_ERROR
)) {
5563 ERR("Failed to reset rotation state of session \"%s\"",
5570 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5572 * Check if the session has finished its rotation.
5574 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5576 int cmd_rotate_get_info(struct ltt_session
*session
,
5577 struct lttng_rotation_get_info_return
*info_return
,
5578 uint64_t rotation_id
)
5580 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5581 enum lttng_rotation_state rotation_state
;
5583 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5584 session
->most_recent_chunk_id
.value
);
5586 if (session
->chunk_being_archived
) {
5587 enum lttng_trace_chunk_status chunk_status
;
5590 chunk_status
= lttng_trace_chunk_get_id(
5591 session
->chunk_being_archived
,
5593 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5595 rotation_state
= rotation_id
== chunk_id
?
5596 LTTNG_ROTATION_STATE_ONGOING
:
5597 LTTNG_ROTATION_STATE_EXPIRED
;
5599 if (session
->last_archived_chunk_id
.is_set
&&
5600 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5601 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5603 rotation_state
= session
->rotation_state
;
5607 switch (rotation_state
) {
5608 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5609 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5612 case LTTNG_ROTATION_STATE_EXPIRED
:
5613 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5614 rotation_id
, session
->name
);
5616 case LTTNG_ROTATION_STATE_ONGOING
:
5617 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5618 rotation_id
, session
->name
);
5620 case LTTNG_ROTATION_STATE_COMPLETED
:
5624 char *current_tracing_path_reply
;
5625 size_t current_tracing_path_reply_len
;
5627 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5628 rotation_id
, session
->name
);
5630 switch (session_get_consumer_destination_type(session
)) {
5631 case CONSUMER_DST_LOCAL
:
5632 current_tracing_path_reply
=
5633 info_return
->location
.local
.absolute_path
;
5634 current_tracing_path_reply_len
=
5635 sizeof(info_return
->location
.local
.absolute_path
);
5636 info_return
->location_type
=
5637 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5638 fmt_ret
= asprintf(&chunk_path
,
5639 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5640 session_get_base_path(session
),
5641 session
->last_archived_chunk_name
);
5642 if (fmt_ret
== -1) {
5643 PERROR("Failed to format the path of the last archived trace chunk");
5644 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5645 cmd_ret
= LTTNG_ERR_UNK
;
5649 case CONSUMER_DST_NET
:
5651 uint16_t ctrl_port
, data_port
;
5653 current_tracing_path_reply
=
5654 info_return
->location
.relay
.relative_path
;
5655 current_tracing_path_reply_len
=
5656 sizeof(info_return
->location
.relay
.relative_path
);
5657 /* Currently the only supported relay protocol. */
5658 info_return
->location
.relay
.protocol
=
5659 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5661 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5662 session_get_net_consumer_hostname(session
),
5663 sizeof(info_return
->location
.relay
.host
));
5665 ERR("Failed to copy host name to rotate_get_info reply");
5666 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5667 cmd_ret
= LTTNG_ERR_SET_URL
;
5671 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5672 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5673 info_return
->location
.relay
.ports
.data
= data_port
;
5674 info_return
->location_type
=
5675 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5676 chunk_path
= strdup(session
->last_chunk_path
);
5678 ERR("Failed to allocate the path of the last archived trace chunk");
5679 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5680 cmd_ret
= LTTNG_ERR_UNK
;
5689 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5690 chunk_path
, current_tracing_path_reply_len
);
5693 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5694 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5695 cmd_ret
= LTTNG_ERR_UNK
;
5701 case LTTNG_ROTATION_STATE_ERROR
:
5702 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5703 rotation_id
, session
->name
);
5711 info_return
->status
= (int32_t) rotation_state
;
5716 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5718 * Configure the automatic rotation parameters.
5719 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5720 * 'activate' to false means deactivate the rotation schedule and validate that
5721 * 'new_value' has the same value as the currently active value.
5723 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5725 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5726 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5728 struct notification_thread_handle
*notification_thread_handle
)
5731 uint64_t *parameter_value
;
5733 LTTNG_ASSERT(session
);
5735 DBG("Cmd rotate set schedule session %s", session
->name
);
5737 if (session
->live_timer
|| !session
->output_traces
) {
5738 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5739 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5743 switch (schedule_type
) {
5744 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5745 parameter_value
= &session
->rotate_size
;
5747 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5748 parameter_value
= &session
->rotate_timer_period
;
5749 if (new_value
>= UINT_MAX
) {
5750 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5751 new_value
, UINT_MAX
);
5752 ret
= LTTNG_ERR_INVALID
;
5757 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5758 ret
= LTTNG_ERR_INVALID
;
5762 /* Improper use of the API. */
5763 if (new_value
== -1ULL) {
5764 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5765 ret
= LTTNG_ERR_INVALID
;
5770 * As indicated in struct ltt_session's comments, a value of == 0 means
5771 * this schedule rotation type is not in use.
5773 * Reject the command if we were asked to activate a schedule that was
5776 if (activate
&& *parameter_value
!= 0) {
5777 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5778 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5783 * Reject the command if we were asked to deactivate a schedule that was
5786 if (!activate
&& *parameter_value
== 0) {
5787 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5788 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5793 * Reject the command if we were asked to deactivate a schedule that
5796 if (!activate
&& *parameter_value
!= new_value
) {
5797 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5798 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5802 *parameter_value
= activate
? new_value
: 0;
5804 switch (schedule_type
) {
5805 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5806 if (activate
&& session
->active
) {
5808 * Only start the timer if the session is active,
5809 * otherwise it will be started when the session starts.
5811 ret
= timer_session_rotation_schedule_timer_start(
5812 session
, new_value
);
5814 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5815 ret
= LTTNG_ERR_UNK
;
5819 ret
= timer_session_rotation_schedule_timer_stop(
5822 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5823 ret
= LTTNG_ERR_UNK
;
5828 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5830 ret
= subscribe_session_consumed_size_rotation(session
,
5831 new_value
, notification_thread_handle
);
5833 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5834 ret
= LTTNG_ERR_UNK
;
5838 ret
= unsubscribe_session_consumed_size_rotation(session
,
5839 notification_thread_handle
);
5841 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5842 ret
= LTTNG_ERR_UNK
;
5849 /* Would have been caught before. */
5861 /* Wait for a given path to be removed before continuing. */
5862 static enum lttng_error_code
wait_on_path(void *path_data
)
5864 const char *shm_path
= (const char *) path_data
;
5866 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5872 ret
= stat(shm_path
, &st
);
5874 if (errno
!= ENOENT
) {
5875 PERROR("stat() returned an error while checking for the existence of the shm path");
5877 DBG("shm path no longer exists, completing the destruction of session");
5881 if (!S_ISDIR(st
.st_mode
)) {
5882 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5887 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5893 * Returns a pointer to a handler to run on completion of a command.
5894 * Returns NULL if no handler has to be run for the last command executed.
5896 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5898 struct cmd_completion_handler
*handler
= current_completion_handler
;
5900 current_completion_handler
= NULL
;
5905 * Init command subsystem.
5910 * Set network sequence index to 1 for streams to match a relayd
5911 * socket on the consumer side.
5913 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5914 relayd_net_seq_idx
= 1;
5915 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5917 DBG("Command subsystem initialized");