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
12 #include <urcu/list.h>
13 #include <urcu/uatomic.h>
17 #include <common/defaults.h>
18 #include <common/common.h>
19 #include <common/error.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/relayd/relayd.h>
22 #include <common/utils.h>
23 #include <common/compat/string.h>
24 #include <common/kernel-ctl/kernel-ctl.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/buffer-view.h>
27 #include <common/payload.h>
28 #include <common/payload-view.h>
29 #include <common/trace-chunk.h>
30 #include <lttng/location-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/condition/condition.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/on-event.h>
35 #include <lttng/condition/on-event-internal.h>
36 #include <lttng/event-rule/event-rule.h>
37 #include <lttng/event-rule/event-rule-internal.h>
38 #include <lttng/action/action.h>
39 #include <lttng/action/action-internal.h>
40 #include <lttng/action/group-internal.h>
41 #include <lttng/action/incr-value-internal.h>
42 #include <lttng/channel.h>
43 #include <lttng/channel-internal.h>
44 #include "lttng/domain.h"
45 #include "lttng/map/map.h"
46 #include <lttng/map/map-internal.h>
47 #include <lttng/rotate-internal.h>
48 #include <lttng/location-internal.h>
49 #include <lttng/session-internal.h>
50 #include <lttng/userspace-probe-internal.h>
51 #include <lttng/session-descriptor-internal.h>
52 #include <lttng/lttng-error.h>
53 #include <lttng/tracker.h>
54 #include <common/string-utils/string-utils.h>
59 #include "health-sessiond.h"
61 #include "kernel-consumer.h"
62 #include "lttng-sessiond.h"
64 #include "lttng-syscall.h"
66 #include "buffer-registry.h"
68 #include "notification-thread.h"
69 #include "notification-thread-commands.h"
71 #include "rotation-thread.h"
73 #include "agent-thread.h"
79 /* Sleep for 100ms between each check for the shm path's deletion. */
80 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
82 struct cmd_destroy_session_reply_context
{
84 bool implicit_rotation_on_destroy
;
86 * Indicates whether or not an error occurred while launching the
87 * destruction of a session.
89 enum lttng_error_code destruction_status
;
92 static enum lttng_error_code
wait_on_path(void *path
);
95 * Command completion handler that is used by the destroy command
96 * when a session that has a non-default shm_path is being destroyed.
98 * See comment in cmd_destroy_session() for the rationale.
100 static struct destroy_completion_handler
{
101 struct cmd_completion_handler handler
;
102 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
103 } destroy_completion_handler
= {
106 .data
= destroy_completion_handler
.shm_path
111 static struct cmd_completion_handler
*current_completion_handler
;
114 * Used to keep a unique index for each relayd socket created where this value
115 * is associated with streams on the consumer so it can match the right relayd
116 * to send to. It must be accessed with the relayd_net_seq_idx_lock
119 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
120 static uint64_t relayd_net_seq_idx
;
122 static int validate_ust_event_name(const char *);
123 static int cmd_enable_event_internal(struct ltt_session
*session
,
124 const struct lttng_domain
*domain
,
125 char *channel_name
, struct lttng_event
*event
,
126 char *filter_expression
,
127 struct lttng_bytecode
*filter
,
128 struct lttng_event_exclusion
*exclusion
,
132 * Create a session path used by list_lttng_sessions for the case that the
133 * session consumer is on the network.
135 static int build_network_session_path(char *dst
, size_t size
,
136 struct ltt_session
*session
)
138 int ret
, kdata_port
, udata_port
;
139 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
140 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
145 memset(tmp_urls
, 0, sizeof(tmp_urls
));
146 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
148 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
150 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
151 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
152 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
155 if (session
->ust_session
&& session
->ust_session
->consumer
) {
156 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
157 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
160 if (uuri
== NULL
&& kuri
== NULL
) {
161 uri
= &session
->consumer
->dst
.net
.control
;
162 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
163 } else if (kuri
&& uuri
) {
164 ret
= uri_compare(kuri
, uuri
);
168 /* Build uuri URL string */
169 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
176 } else if (kuri
&& uuri
== NULL
) {
178 } else if (uuri
&& kuri
== NULL
) {
182 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
188 * Do we have a UST url set. If yes, this means we have both kernel and UST
191 if (*tmp_uurl
!= '\0') {
192 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
193 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
196 if (kuri
|| (!kuri
&& !uuri
)) {
199 /* No kernel URI, use the UST port. */
202 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
210 * Get run-time attributes if the session has been started (discarded events,
213 static int get_kernel_runtime_stats(struct ltt_session
*session
,
214 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
215 uint64_t *lost_packets
)
219 if (!session
->has_been_started
) {
221 *discarded_events
= 0;
226 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
227 session
->kernel_session
->consumer
,
233 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
234 session
->kernel_session
->consumer
,
245 * Get run-time attributes if the session has been started (discarded events,
248 static int get_ust_runtime_stats(struct ltt_session
*session
,
249 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
250 uint64_t *lost_packets
)
253 struct ltt_ust_session
*usess
;
255 if (!discarded_events
|| !lost_packets
) {
260 usess
= session
->ust_session
;
261 assert(discarded_events
);
262 assert(lost_packets
);
264 if (!usess
|| !session
->has_been_started
) {
265 *discarded_events
= 0;
271 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
272 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
273 &usess
->buffer_reg_uid_list
,
274 usess
->consumer
, uchan
->id
,
275 uchan
->attr
.overwrite
,
278 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
279 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
280 uchan
, usess
->consumer
,
281 uchan
->attr
.overwrite
,
287 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
288 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
290 ERR("Unsupported buffer type");
300 enum tracer_executed_action_state
{
301 TRACER_EXECUTED_ACTION_STATE_REGISTER
,
302 TRACER_EXECUTED_ACTION_STATE_UNREGISTER
306 enum lttng_error_code
sync_incr_value_action_ust(
307 struct ltt_session
*session
,
308 const struct lttng_condition
*condition
,
309 const char *map_name
,
310 uint64_t tracer_token
,
311 struct lttng_map_key
*key
,
312 enum tracer_executed_action_state state
)
314 enum lttng_error_code ret_code
;
315 const struct lttng_event_rule
*event_rule
;
316 enum lttng_condition_status cond_status
;
317 enum lttng_event_rule_status er_status
;
318 struct ltt_ust_session
*usess
= session
->ust_session
;
320 struct ltt_ust_map
*map
;
321 char *filter_expression
;
322 struct lttng_bytecode
*filter
;
323 struct lttng_event_exclusion
*exclusion
;
324 enum lttng_event_rule_generate_exclusions_status
325 generate_exclusion_status
;
327 DBG("Syncing UST incr-value action for session '%s', map '%s'",
328 session
->name
, map_name
);
330 DBG("No UST session");
335 assert(usess
->domain_global
.maps
);
337 map
= trace_ust_find_map_by_name(usess
->domain_global
.maps
, map_name
);
339 DBG("UST map \"%s\" not found", map_name
);
344 cond_status
= lttng_condition_on_event_get_rule(condition
, &event_rule
);
345 if (cond_status
!= LTTNG_CONDITION_STATUS_OK
) {
346 ERR("Error getting on-event condition event-rule");
347 ret_code
= LTTNG_ERR_INVALID_MAP
;
351 er_status
= lttng_event_rule_tracepoint_get_pattern(event_rule
, &pattern
);
352 if (er_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
353 /* At this point, this is a fatal error. */
358 * FIXME: frdeso, reuse the event notifier functions and approach to
361 filter_expression
= (char *) lttng_event_rule_get_filter(event_rule
);
362 filter
= (struct lttng_bytecode
*)lttng_event_rule_get_filter_bytecode(
364 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
365 event_rule
, &exclusion
);
366 if (generate_exclusion_status
== LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_ERROR
) {
367 ERR("Error generating the exclusion");
368 ret_code
= LTTNG_ERR_EXCLUSION_INVAL
;
373 case TRACER_EXECUTED_ACTION_STATE_REGISTER
:
374 ret_code
= map_event_ust_enable_tracepoint(usess
,
375 map
, tracer_token
, (char *) pattern
, key
,
376 LTTNG_EVENT_TRACEPOINT
, LTTNG_EVENT_LOGLEVEL_ALL
,
377 0, filter_expression
, filter
, exclusion
, false);
378 if (ret_code
== LTTNG_ERR_UST_EVENT_ENABLED
) {
380 } else if (ret_code
!= LTTNG_OK
) {
381 ERR("Enabling UST map event");
385 case TRACER_EXECUTED_ACTION_STATE_UNREGISTER
:
386 ret_code
= map_event_ust_disable_tracepoint(usess
,
387 map
, tracer_token
, (char *) pattern
, key
,
388 LTTNG_EVENT_TRACEPOINT
, LTTNG_EVENT_LOGLEVEL_ALL
,
389 0, filter_expression
, filter
, exclusion
, false);
390 if (ret_code
== LTTNG_ERR_UST_EVENT_ENABLED
) {
392 } else if (ret_code
!= LTTNG_OK
) {
393 ERR("Enabling UST map event");
408 enum lttng_error_code
sync_incr_value_action_kernel(
409 struct ltt_session
*session
,
410 const struct lttng_credentials
*creds
,
411 const struct lttng_condition
*condition
,
412 const char *map_name
,
413 uint64_t tracer_token
,
414 struct lttng_map_key
*key
,
415 enum tracer_executed_action_state state
)
417 enum lttng_error_code ret_code
;
418 const struct lttng_event_rule
*event_rule
;
419 enum lttng_condition_status cond_status
;
420 struct ltt_kernel_map
*kmap
;
421 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
423 DBG("Syncing kernel incr-value action for session '%s', map '%s'",
424 session
->name
, map_name
);
427 DBG("No kernel session");
432 kmap
= trace_kernel_get_map_by_name(map_name
, ksess
);
434 DBG("Kernel map \"%s\" not found", map_name
);
439 cond_status
= lttng_condition_on_event_get_rule(condition
, &event_rule
);
440 if (cond_status
!= LTTNG_CONDITION_STATUS_OK
) {
441 ret_code
= LTTNG_ERR_INVALID_MAP
;
442 ERR("Error getting on-event condition event-rule");
447 case TRACER_EXECUTED_ACTION_STATE_REGISTER
:
448 ret_code
= map_event_kernel_enable_event(kmap
, creds
, tracer_token
,
450 if(ret_code
!= LTTNG_OK
) {
451 ERR("Error enabling event counter to the kernel tracer");
455 case TRACER_EXECUTED_ACTION_STATE_UNREGISTER
:
456 ret_code
= map_event_kernel_disable_event(kmap
, tracer_token
);
457 if(ret_code
!= LTTNG_OK
) {
458 ERR("Error disabling event counter to the kernel tracer");
469 enum lttng_error_code
sync_incr_value_action(
470 const struct lttng_credentials
*creds
,
471 const struct lttng_condition
*condition
,
472 const struct lttng_action
*action
,
473 enum tracer_executed_action_state state
)
475 enum lttng_error_code ret
;
476 const char *session_name
, *map_name
;
477 enum lttng_action_status action_status
;
478 enum lttng_condition_status cond_status
;
479 const struct lttng_event_rule
*event_rule
;
480 struct ltt_session
*session
= NULL
;
481 struct lttng_map_key
*key
;
482 uint64_t action_tracer_token
;
484 action_status
= lttng_action_incr_value_get_map_name(action
,
486 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
487 ERR("Map name not set for incr-value action");
488 ret
= LTTNG_ERR_INVALID_MAP
;
492 action_status
= lttng_action_incr_value_get_session_name(action
,
494 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
495 ERR("Session name not set for incr-value action");
496 ret
= LTTNG_ERR_INVALID_MAP
;
500 action_status
= lttng_action_incr_value_borrow_key_mutable(action
, &key
);
501 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
502 ERR("Key not set for incr-value action");
503 ret
= LTTNG_ERR_INVALID_MAP
;
507 /* Returns a refcounted reference */
508 session
= session_find_by_name(session_name
);
510 DBG("Session not found for incr-value action: session-name=%s",
516 cond_status
= lttng_condition_on_event_get_rule(condition
, &event_rule
);
517 if (cond_status
!= LTTNG_CONDITION_STATUS_OK
) {
518 ERR("Error getting on-event condition event-rule");
519 ret
= LTTNG_ERR_INVALID_MAP
;
520 session_put(session
);
524 action_tracer_token
= lttng_action_incr_value_get_tracer_token(action
);
526 switch (lttng_event_rule_get_domain_type(event_rule
)) {
527 case LTTNG_DOMAIN_UST
:
528 ret
= sync_incr_value_action_ust(session
, condition
,
529 map_name
, action_tracer_token
, key
, state
);
530 if (ret
== LTTNG_ERR_UST_EVENT_EXIST
) {
531 DBG("Incr-value action already registered");
535 case LTTNG_DOMAIN_KERNEL
:
536 ret
= sync_incr_value_action_kernel(session
, creds
, condition
,
537 map_name
, action_tracer_token
, key
, state
);
538 if (ret
== LTTNG_ERR_KERN_EVENT_EXIST
) {
539 DBG("Incr-value action already registered");
550 session_put(session
);
556 enum lttng_error_code
sync_one_tracer_executed_action(
557 const struct lttng_credentials
*creds
,
558 const struct lttng_condition
*condition
,
559 const struct lttng_action
*action
,
560 enum tracer_executed_action_state state
)
562 enum lttng_action_type action_type
;
563 enum lttng_error_code ret
;
565 action_type
= lttng_action_get_type(action
);
566 assert(action_type
!= LTTNG_ACTION_TYPE_GROUP
);
568 switch (action_type
) {
569 case LTTNG_ACTION_TYPE_INCREMENT_VALUE
:
570 DBG("Action type \"%s\" is a tracer executed action.",
571 lttng_action_type_string(action_type
));
573 ret
= sync_incr_value_action(creds
, condition
, action
, state
);
574 if (ret
!= LTTNG_OK
) {
575 ERR("Error syncing increment value action to the tracer");
579 DBG("Action type \"%s\" is not a tracer executed action.",
580 lttng_action_type_string(action_type
));
590 enum lttng_error_code
sync_all_tracer_executed_actions(
591 const struct lttng_trigger
*trigger
,
592 const struct lttng_credentials
*cmd_creds
,
593 enum tracer_executed_action_state state
)
595 enum lttng_error_code ret
;
596 unsigned int i
, count
;
597 enum lttng_action_status action_status
;
598 enum lttng_action_type action_type
;
599 const struct lttng_action
*action
;
600 const struct lttng_condition
*condition
;
602 condition
= lttng_trigger_get_const_condition(trigger
);
603 action
= lttng_trigger_get_const_action(trigger
);
605 action_type
= lttng_action_get_type(action
);
607 DBG("Iterating over all actions of trigger \"%s\" to sync any tracer executed actions",
610 if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
611 ret
= sync_one_tracer_executed_action(cmd_creds
, condition
, action
,
614 action_status
= lttng_action_group_get_count(action
, &count
);
615 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
617 for (i
= 0; i
< count
; i
++) {
618 const struct lttng_action
*inner_action
=
619 lttng_action_group_get_at_index(action
, i
);
621 ret
= sync_one_tracer_executed_action(cmd_creds
, condition
,
622 inner_action
, state
);
623 if (ret
!= LTTNG_OK
) {
624 ERR("Error syncing tracer executed action");
635 * Fill lttng_channel array of all channels.
637 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
638 struct ltt_session
*session
, struct lttng_channel
*channels
,
639 struct lttng_channel_extended
*chan_exts
)
642 struct ltt_kernel_channel
*kchan
;
644 DBG("Listing channels for session %s", session
->name
);
647 case LTTNG_DOMAIN_KERNEL
:
648 /* Kernel channels */
649 if (session
->kernel_session
!= NULL
) {
650 cds_list_for_each_entry(kchan
,
651 &session
->kernel_session
->channel_list
.head
, list
) {
652 uint64_t discarded_events
, lost_packets
;
653 struct lttng_channel_extended
*extended
;
655 extended
= (struct lttng_channel_extended
*)
656 kchan
->channel
->attr
.extended
.ptr
;
658 ret
= get_kernel_runtime_stats(session
, kchan
,
659 &discarded_events
, &lost_packets
);
663 /* Copy lttng_channel struct to array */
664 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
665 channels
[i
].enabled
= kchan
->enabled
;
666 chan_exts
[i
].discarded_events
=
668 chan_exts
[i
].lost_packets
= lost_packets
;
669 chan_exts
[i
].monitor_timer_interval
=
670 extended
->monitor_timer_interval
;
671 chan_exts
[i
].blocking_timeout
= 0;
676 case LTTNG_DOMAIN_UST
:
678 struct lttng_ht_iter iter
;
679 struct ltt_ust_channel
*uchan
;
682 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
683 &iter
.iter
, uchan
, node
.node
) {
684 uint64_t discarded_events
= 0, lost_packets
= 0;
686 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
687 LTTNG_SYMBOL_NAME_LEN
)) {
690 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
691 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
692 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
693 channels
[i
].attr
.switch_timer_interval
=
694 uchan
->attr
.switch_timer_interval
;
695 channels
[i
].attr
.read_timer_interval
=
696 uchan
->attr
.read_timer_interval
;
697 channels
[i
].enabled
= uchan
->enabled
;
698 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
699 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
702 * Map enum lttng_ust_output to enum lttng_event_output.
704 switch (uchan
->attr
.output
) {
706 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
710 * LTTNG_UST_MMAP is the only supported UST
717 chan_exts
[i
].monitor_timer_interval
=
718 uchan
->monitor_timer_interval
;
719 chan_exts
[i
].blocking_timeout
=
720 uchan
->attr
.u
.s
.blocking_timeout
;
722 ret
= get_ust_runtime_stats(session
, uchan
,
723 &discarded_events
, &lost_packets
);
727 chan_exts
[i
].discarded_events
= discarded_events
;
728 chan_exts
[i
].lost_packets
= lost_packets
;
740 return -LTTNG_ERR_FATAL
;
746 static int append_extended_info(const char *filter_expression
,
747 struct lttng_event_exclusion
*exclusion
,
748 struct lttng_userspace_probe_location
*probe_location
,
749 struct lttng_payload
*payload
)
752 size_t filter_len
= 0;
753 size_t nb_exclusions
= 0;
754 size_t userspace_probe_location_len
= 0;
755 struct lttcomm_event_extended_header extended_header
= {};
756 struct lttcomm_event_extended_header
*p_extended_header
;
757 const size_t original_payload_size
= payload
->buffer
.size
;
759 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
760 sizeof(extended_header
));
765 if (filter_expression
) {
766 filter_len
= strlen(filter_expression
) + 1;
767 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
768 filter_expression
, filter_len
);
775 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
777 nb_exclusions
= exclusion
->count
;
779 ret
= lttng_dynamic_buffer_append(
780 &payload
->buffer
, &exclusion
->names
, len
);
786 if (probe_location
) {
787 const size_t size_before_probe
= payload
->buffer
.size
;
789 ret
= lttng_userspace_probe_location_serialize(probe_location
,
796 userspace_probe_location_len
=
797 payload
->buffer
.size
- size_before_probe
;
800 /* Set header fields */
801 p_extended_header
= (struct lttcomm_event_extended_header
*)
802 (payload
->buffer
.data
+ original_payload_size
);
804 p_extended_header
->filter_len
= filter_len
;
805 p_extended_header
->nb_exclusions
= nb_exclusions
;
806 p_extended_header
->userspace_probe_location_len
=
807 userspace_probe_location_len
;
815 * Create a list of agent domain events.
817 * Return number of events in list on success or else a negative value.
819 static int list_lttng_agent_events(struct agent
*agt
,
820 struct lttng_payload
*payload
)
822 int nb_events
= 0, ret
= 0;
823 const struct agent_event
*agent_event
;
824 struct lttng_ht_iter iter
;
828 DBG3("Listing agent events");
831 cds_lfht_for_each_entry (
832 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
833 struct lttng_event event
= {
834 .enabled
= AGENT_EVENT_IS_ENABLED(agent_event
),
835 .loglevel
= agent_event
->loglevel_value
,
836 .loglevel_type
= agent_event
->loglevel_type
,
839 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
841 /* Internal error, invalid name. */
842 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
843 agent_event
->name
, sizeof(event
.name
));
844 ret
= -LTTNG_ERR_UNK
;
848 ret
= lttng_dynamic_buffer_append(
849 &payload
->buffer
, &event
, sizeof(event
));
851 ERR("Failed to append event to payload");
852 ret
= -LTTNG_ERR_NOMEM
;
859 cds_lfht_for_each_entry (
860 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
861 /* Append extended info. */
862 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
865 ERR("Failed to append extended event info to payload");
866 ret
= -LTTNG_ERR_NOMEM
;
878 * Create a list of ust global domain events.
880 static int list_lttng_ust_global_events(char *channel_name
,
881 struct ltt_ust_domain_global
*ust_global
,
882 struct lttng_payload
*payload
)
885 unsigned int nb_events
= 0;
886 struct lttng_ht_iter iter
;
887 const struct lttng_ht_node_str
*node
;
888 const struct ltt_ust_channel
*uchan
;
889 const struct ltt_ust_event
*uevent
;
891 DBG("Listing UST global events for channel %s", channel_name
);
895 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
896 node
= lttng_ht_iter_get_node_str(&iter
);
898 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
902 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
904 DBG3("Listing UST global events");
906 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
907 struct lttng_event event
= {};
909 if (uevent
->internal
) {
913 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
915 /* Internal error, invalid name. */
916 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
917 uevent
->attr
.name
, sizeof(event
.name
));
918 ret
= -LTTNG_ERR_UNK
;
922 event
.enabled
= uevent
->enabled
;
924 switch (uevent
->attr
.instrumentation
) {
925 case LTTNG_UST_TRACEPOINT
:
926 event
.type
= LTTNG_EVENT_TRACEPOINT
;
928 case LTTNG_UST_PROBE
:
929 event
.type
= LTTNG_EVENT_PROBE
;
931 case LTTNG_UST_FUNCTION
:
932 event
.type
= LTTNG_EVENT_FUNCTION
;
936 event
.loglevel
= uevent
->attr
.loglevel
;
937 switch (uevent
->attr
.loglevel_type
) {
938 case LTTNG_UST_LOGLEVEL_ALL
:
939 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
941 case LTTNG_UST_LOGLEVEL_RANGE
:
942 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
944 case LTTNG_UST_LOGLEVEL_SINGLE
:
945 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
949 if (uevent
->filter
) {
953 if (uevent
->exclusion
) {
957 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
959 ERR("Failed to append event to payload");
960 ret
= -LTTNG_ERR_NOMEM
;
967 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
968 /* Append extended info. */
969 ret
= append_extended_info(uevent
->filter_expression
,
970 uevent
->exclusion
, NULL
, payload
);
972 ERR("Failed to append extended event info to payload");
973 ret
= -LTTNG_ERR_FATAL
;
985 * Fill lttng_event array of all kernel events in the channel.
987 static int list_lttng_kernel_events(char *channel_name
,
988 struct ltt_kernel_session
*kernel_session
,
989 struct lttng_payload
*payload
)
992 unsigned int nb_event
;
993 const struct ltt_kernel_event
*kevent
;
994 const struct ltt_kernel_channel
*kchan
;
996 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
998 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1002 nb_event
= kchan
->event_count
;
1004 DBG("Listing events for channel %s", kchan
->channel
->name
);
1006 /* Kernel channels */
1007 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1008 struct lttng_event event
= {};
1010 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
1012 /* Internal error, invalid name. */
1013 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
1014 kevent
->event
->name
,
1015 sizeof(event
.name
));
1016 ret
= -LTTNG_ERR_UNK
;
1020 event
.enabled
= kevent
->enabled
;
1021 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
1023 switch (kevent
->event
->instrumentation
) {
1024 case LTTNG_KERNEL_TRACEPOINT
:
1025 event
.type
= LTTNG_EVENT_TRACEPOINT
;
1027 case LTTNG_KERNEL_KRETPROBE
:
1028 event
.type
= LTTNG_EVENT_FUNCTION
;
1029 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
1030 sizeof(struct lttng_kernel_kprobe
));
1032 case LTTNG_KERNEL_KPROBE
:
1033 event
.type
= LTTNG_EVENT_PROBE
;
1034 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
1035 sizeof(struct lttng_kernel_kprobe
));
1037 case LTTNG_KERNEL_UPROBE
:
1038 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
1040 case LTTNG_KERNEL_FUNCTION
:
1041 event
.type
= LTTNG_EVENT_FUNCTION
;
1042 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
1043 sizeof(struct lttng_kernel_function
));
1045 case LTTNG_KERNEL_NOOP
:
1046 event
.type
= LTTNG_EVENT_NOOP
;
1048 case LTTNG_KERNEL_SYSCALL
:
1049 event
.type
= LTTNG_EVENT_SYSCALL
;
1051 case LTTNG_KERNEL_ALL
:
1058 ret
= lttng_dynamic_buffer_append(
1059 &payload
->buffer
, &event
, sizeof(event
));
1061 ERR("Failed to append event to payload");
1062 ret
= -LTTNG_ERR_NOMEM
;
1067 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1068 /* Append extended info. */
1069 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
1070 kevent
->userspace_probe_location
, payload
);
1072 DBG("Error appending extended info message");
1073 ret
= -LTTNG_ERR_FATAL
;
1085 * Add URI so the consumer output object. Set the correct path depending on the
1086 * domain adding the default trace directory.
1088 static enum lttng_error_code
add_uri_to_consumer(
1089 const struct ltt_session
*session
,
1090 struct consumer_output
*consumer
,
1091 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
1094 enum lttng_error_code ret_code
= LTTNG_OK
;
1098 if (consumer
== NULL
) {
1099 DBG("No consumer detected. Don't add URI. Stopping.");
1100 ret_code
= LTTNG_ERR_NO_CONSUMER
;
1105 case LTTNG_DOMAIN_KERNEL
:
1106 ret
= lttng_strncpy(consumer
->domain_subdir
,
1107 DEFAULT_KERNEL_TRACE_DIR
,
1108 sizeof(consumer
->domain_subdir
));
1110 case LTTNG_DOMAIN_UST
:
1111 ret
= lttng_strncpy(consumer
->domain_subdir
,
1112 DEFAULT_UST_TRACE_DIR
,
1113 sizeof(consumer
->domain_subdir
));
1117 * This case is possible is we try to add the URI to the global
1118 * tracing session consumer object which in this case there is
1121 memset(consumer
->domain_subdir
, 0,
1122 sizeof(consumer
->domain_subdir
));
1126 ERR("Failed to initialize consumer output domain subdirectory");
1127 ret_code
= LTTNG_ERR_FATAL
;
1131 switch (uri
->dtype
) {
1132 case LTTNG_DST_IPV4
:
1133 case LTTNG_DST_IPV6
:
1134 DBG2("Setting network URI to consumer");
1136 if (consumer
->type
== CONSUMER_DST_NET
) {
1137 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
1138 consumer
->dst
.net
.control_isset
) ||
1139 (uri
->stype
== LTTNG_STREAM_DATA
&&
1140 consumer
->dst
.net
.data_isset
)) {
1141 ret_code
= LTTNG_ERR_URL_EXIST
;
1145 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
1148 /* Set URI into consumer output object */
1149 ret
= consumer_set_network_uri(session
, consumer
, uri
);
1153 } else if (ret
== 1) {
1155 * URI was the same in the consumer so we do not append the subdir
1156 * again so to not duplicate output dir.
1158 ret_code
= LTTNG_OK
;
1162 case LTTNG_DST_PATH
:
1163 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
1164 ret_code
= LTTNG_ERR_INVALID
;
1167 DBG2("Setting trace directory path from URI to %s",
1169 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
1171 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
1173 sizeof(consumer
->dst
.session_root_path
));
1175 ret_code
= LTTNG_ERR_FATAL
;
1178 consumer
->type
= CONSUMER_DST_LOCAL
;
1182 ret_code
= LTTNG_OK
;
1188 * Init tracing by creating trace directory and sending fds kernel consumer.
1190 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1193 struct lttng_ht_iter iter
;
1194 struct consumer_socket
*socket
;
1200 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
1201 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
1202 socket
, node
.node
) {
1203 pthread_mutex_lock(socket
->lock
);
1204 ret
= kernel_consumer_send_session(socket
, session
);
1205 pthread_mutex_unlock(socket
->lock
);
1207 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1219 * Create a socket to the relayd using the URI.
1221 * On success, the relayd_sock pointer is set to the created socket.
1222 * Else, it remains untouched and an LTTng error code is returned.
1224 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
1225 struct lttcomm_relayd_sock
**relayd_sock
,
1226 struct consumer_output
*consumer
)
1229 enum lttng_error_code status
= LTTNG_OK
;
1230 struct lttcomm_relayd_sock
*rsock
;
1232 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
1233 RELAYD_VERSION_COMM_MINOR
);
1235 status
= LTTNG_ERR_FATAL
;
1240 * Connect to relayd so we can proceed with a session creation. This call
1241 * can possibly block for an arbitrary amount of time to set the health
1242 * state to be in poll execution.
1244 health_poll_entry();
1245 ret
= relayd_connect(rsock
);
1248 ERR("Unable to reach lttng-relayd");
1249 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1253 /* Create socket for control stream. */
1254 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
1255 uint64_t result_flags
;
1257 DBG3("Creating relayd stream socket from URI");
1259 /* Check relayd version */
1260 ret
= relayd_version_check(rsock
);
1261 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
1262 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
1264 } else if (ret
< 0) {
1265 ERR("Unable to reach lttng-relayd");
1266 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1269 consumer
->relay_major_version
= rsock
->major
;
1270 consumer
->relay_minor_version
= rsock
->minor
;
1271 ret
= relayd_get_configuration(rsock
, 0,
1274 ERR("Unable to get relayd configuration");
1275 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1278 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
1279 consumer
->relay_allows_clear
= true;
1281 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1282 DBG3("Creating relayd data socket from URI");
1284 /* Command is not valid */
1285 ERR("Relayd invalid stream type: %d", uri
->stype
);
1286 status
= LTTNG_ERR_INVALID
;
1290 *relayd_sock
= rsock
;
1295 /* The returned value is not useful since we are on an error path. */
1296 (void) relayd_close(rsock
);
1304 * Connect to the relayd using URI and send the socket to the right consumer.
1306 * The consumer socket lock must be held by the caller.
1308 * Returns LTTNG_OK on success or an LTTng error code on failure.
1310 static enum lttng_error_code
send_consumer_relayd_socket(
1311 unsigned int session_id
,
1312 struct lttng_uri
*relayd_uri
,
1313 struct consumer_output
*consumer
,
1314 struct consumer_socket
*consumer_sock
,
1315 const char *session_name
, const char *hostname
,
1316 const char *base_path
, int session_live_timer
,
1317 const uint64_t *current_chunk_id
,
1318 time_t session_creation_time
,
1319 bool session_name_contains_creation_time
)
1322 struct lttcomm_relayd_sock
*rsock
= NULL
;
1323 enum lttng_error_code status
;
1325 /* Connect to relayd and make version check if uri is the control. */
1326 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1327 if (status
!= LTTNG_OK
) {
1328 goto relayd_comm_error
;
1332 /* Set the network sequence index if not set. */
1333 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1334 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1336 * Increment net_seq_idx because we are about to transfer the
1337 * new relayd socket to the consumer.
1338 * Assign unique key so the consumer can match streams.
1340 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1341 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1344 /* Send relayd socket to consumer. */
1345 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1346 relayd_uri
->stype
, session_id
,
1347 session_name
, hostname
, base_path
,
1348 session_live_timer
, current_chunk_id
,
1349 session_creation_time
, session_name_contains_creation_time
);
1351 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1355 /* Flag that the corresponding socket was sent. */
1356 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1357 consumer_sock
->control_sock_sent
= 1;
1358 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1359 consumer_sock
->data_sock_sent
= 1;
1363 * Close socket which was dup on the consumer side. The session daemon does
1364 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1368 if (status
!= LTTNG_OK
) {
1370 * The consumer output for this session should not be used anymore
1371 * since the relayd connection failed thus making any tracing or/and
1372 * streaming not usable.
1374 consumer
->enabled
= 0;
1376 (void) relayd_close(rsock
);
1384 * Send both relayd sockets to a specific consumer and domain. This is a
1385 * helper function to facilitate sending the information to the consumer for a
1388 * The consumer socket lock must be held by the caller.
1390 * Returns LTTNG_OK, or an LTTng error code on failure.
1392 static enum lttng_error_code
send_consumer_relayd_sockets(
1393 enum lttng_domain_type domain
,
1394 unsigned int session_id
, struct consumer_output
*consumer
,
1395 struct consumer_socket
*sock
, const char *session_name
,
1396 const char *hostname
, const char *base_path
, int session_live_timer
,
1397 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1398 bool session_name_contains_creation_time
)
1400 enum lttng_error_code status
= LTTNG_OK
;
1405 /* Sending control relayd socket. */
1406 if (!sock
->control_sock_sent
) {
1407 status
= send_consumer_relayd_socket(session_id
,
1408 &consumer
->dst
.net
.control
, consumer
, sock
,
1409 session_name
, hostname
, base_path
, session_live_timer
,
1410 current_chunk_id
, session_creation_time
,
1411 session_name_contains_creation_time
);
1412 if (status
!= LTTNG_OK
) {
1417 /* Sending data relayd socket. */
1418 if (!sock
->data_sock_sent
) {
1419 status
= send_consumer_relayd_socket(session_id
,
1420 &consumer
->dst
.net
.data
, consumer
, sock
,
1421 session_name
, hostname
, base_path
, session_live_timer
,
1422 current_chunk_id
, session_creation_time
,
1423 session_name_contains_creation_time
);
1424 if (status
!= LTTNG_OK
) {
1434 * Setup relayd connections for a tracing session. First creates the socket to
1435 * the relayd and send them to the right domain consumer. Consumer type MUST be
1438 int cmd_setup_relayd(struct ltt_session
*session
)
1441 struct ltt_ust_session
*usess
;
1442 struct ltt_kernel_session
*ksess
;
1443 struct consumer_socket
*socket
;
1444 struct lttng_ht_iter iter
;
1445 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1449 usess
= session
->ust_session
;
1450 ksess
= session
->kernel_session
;
1452 DBG("Setting relayd for session %s", session
->name
);
1455 if (session
->current_trace_chunk
) {
1456 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1457 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1459 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1460 current_chunk_id
.is_set
= true;
1462 ERR("Failed to get current trace chunk id");
1463 ret
= LTTNG_ERR_UNK
;
1468 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1469 && usess
->consumer
->enabled
) {
1470 /* For each consumer socket, send relayd sockets */
1471 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1472 socket
, node
.node
) {
1473 pthread_mutex_lock(socket
->lock
);
1474 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1475 usess
->consumer
, socket
,
1476 session
->name
, session
->hostname
,
1478 session
->live_timer
,
1479 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1480 session
->creation_time
,
1481 session
->name_contains_creation_time
);
1482 pthread_mutex_unlock(socket
->lock
);
1483 if (ret
!= LTTNG_OK
) {
1486 /* Session is now ready for network streaming. */
1487 session
->net_handle
= 1;
1489 session
->consumer
->relay_major_version
=
1490 usess
->consumer
->relay_major_version
;
1491 session
->consumer
->relay_minor_version
=
1492 usess
->consumer
->relay_minor_version
;
1493 session
->consumer
->relay_allows_clear
=
1494 usess
->consumer
->relay_allows_clear
;
1497 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1498 && ksess
->consumer
->enabled
) {
1499 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1500 socket
, node
.node
) {
1501 pthread_mutex_lock(socket
->lock
);
1502 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1503 ksess
->consumer
, socket
,
1504 session
->name
, session
->hostname
,
1506 session
->live_timer
,
1507 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1508 session
->creation_time
,
1509 session
->name_contains_creation_time
);
1510 pthread_mutex_unlock(socket
->lock
);
1511 if (ret
!= LTTNG_OK
) {
1514 /* Session is now ready for network streaming. */
1515 session
->net_handle
= 1;
1517 session
->consumer
->relay_major_version
=
1518 ksess
->consumer
->relay_major_version
;
1519 session
->consumer
->relay_minor_version
=
1520 ksess
->consumer
->relay_minor_version
;
1521 session
->consumer
->relay_allows_clear
=
1522 ksess
->consumer
->relay_allows_clear
;
1531 * Start a kernel session by opening all necessary streams.
1533 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1536 struct ltt_kernel_channel
*kchan
;
1538 /* Open kernel metadata */
1539 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1540 ret
= kernel_open_metadata(ksess
);
1542 ret
= LTTNG_ERR_KERN_META_FAIL
;
1547 /* Open kernel metadata stream */
1548 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1549 ret
= kernel_open_metadata_stream(ksess
);
1551 ERR("Kernel create metadata stream failed");
1552 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1557 /* For each channel */
1558 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1559 if (kchan
->stream_count
== 0) {
1560 ret
= kernel_open_channel_stream(kchan
);
1562 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1565 /* Update the stream global counter */
1566 ksess
->stream_count_global
+= ret
;
1570 /* Setup kernel consumer socket and send fds to it */
1571 ret
= init_kernel_tracing(ksess
);
1573 ret
= LTTNG_ERR_KERN_START_FAIL
;
1577 /* This start the kernel tracing */
1578 ret
= kernel_start_session(ksess
);
1580 ret
= LTTNG_ERR_KERN_START_FAIL
;
1584 /* Quiescent wait after starting trace */
1585 kernel_wait_quiescent();
1595 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1597 struct ltt_kernel_channel
*kchan
;
1598 bool error_occurred
= false;
1601 if (!ksess
|| !ksess
->active
) {
1604 DBG("Stopping kernel tracing");
1606 ret
= kernel_stop_session(ksess
);
1608 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1612 kernel_wait_quiescent();
1614 /* Flush metadata after stopping (if exists) */
1615 if (ksess
->metadata_stream_fd
>= 0) {
1616 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1618 ERR("Kernel metadata flush failed");
1619 error_occurred
= true;
1623 /* Flush all buffers after stopping */
1624 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1625 ret
= kernel_flush_buffer(kchan
);
1627 ERR("Kernel flush buffer error");
1628 error_occurred
= true;
1633 if (error_occurred
) {
1634 ret
= LTTNG_ERR_UNK
;
1643 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1645 int cmd_disable_channel(struct ltt_session
*session
,
1646 enum lttng_domain_type domain
, char *channel_name
)
1649 struct ltt_ust_session
*usess
;
1651 usess
= session
->ust_session
;
1656 case LTTNG_DOMAIN_KERNEL
:
1658 ret
= channel_kernel_disable(session
->kernel_session
,
1660 if (ret
!= LTTNG_OK
) {
1664 kernel_wait_quiescent();
1667 case LTTNG_DOMAIN_UST
:
1669 struct ltt_ust_channel
*uchan
;
1670 struct lttng_ht
*chan_ht
;
1672 chan_ht
= usess
->domain_global
.channels
;
1674 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1675 if (uchan
== NULL
) {
1676 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1680 ret
= channel_ust_disable(usess
, uchan
);
1681 if (ret
!= LTTNG_OK
) {
1687 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1699 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1701 * The wpipe arguments is used as a notifier for the kernel thread.
1703 int cmd_enable_channel(struct ltt_session
*session
,
1704 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1707 struct ltt_ust_session
*usess
= session
->ust_session
;
1708 struct lttng_ht
*chan_ht
;
1710 struct lttng_channel attr
;
1717 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1719 /* Validate channel name */
1720 if (attr
.name
[0] == '.' ||
1721 memchr(attr
.name
, '/', len
) != NULL
) {
1722 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1726 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1731 * Don't try to enable a channel if the session has been started at
1732 * some point in time before. The tracer does not allow it.
1734 if (session
->has_been_started
) {
1735 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1740 * If the session is a live session, remove the switch timer, the
1741 * live timer does the same thing but sends also synchronisation
1742 * beacons for inactive streams.
1744 if (session
->live_timer
> 0) {
1745 attr
.attr
.live_timer_interval
= session
->live_timer
;
1746 attr
.attr
.switch_timer_interval
= 0;
1749 /* Check for feature support */
1750 switch (domain
->type
) {
1751 case LTTNG_DOMAIN_KERNEL
:
1753 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1754 /* Sampling position of buffer is not supported */
1755 WARN("Kernel tracer does not support buffer monitoring. "
1756 "Setting the monitor interval timer to 0 "
1757 "(disabled) for channel '%s' of session '%s'",
1758 attr
.name
, session
->name
);
1759 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1763 case LTTNG_DOMAIN_UST
:
1765 case LTTNG_DOMAIN_JUL
:
1766 case LTTNG_DOMAIN_LOG4J
:
1767 case LTTNG_DOMAIN_PYTHON
:
1768 if (!agent_tracing_is_enabled()) {
1769 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1770 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1775 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1779 switch (domain
->type
) {
1780 case LTTNG_DOMAIN_KERNEL
:
1782 struct ltt_kernel_channel
*kchan
;
1784 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1785 session
->kernel_session
);
1786 if (kchan
== NULL
) {
1787 if (session
->snapshot
.nb_output
> 0 ||
1788 session
->snapshot_mode
) {
1789 /* Enforce mmap output for snapshot sessions. */
1790 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1792 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1793 if (attr
.name
[0] != '\0') {
1794 session
->kernel_session
->has_non_default_channel
= 1;
1797 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1800 if (ret
!= LTTNG_OK
) {
1804 kernel_wait_quiescent();
1807 case LTTNG_DOMAIN_UST
:
1808 case LTTNG_DOMAIN_JUL
:
1809 case LTTNG_DOMAIN_LOG4J
:
1810 case LTTNG_DOMAIN_PYTHON
:
1812 struct ltt_ust_channel
*uchan
;
1817 * Current agent implementation limitations force us to allow
1818 * only one channel at once in "agent" subdomains. Each
1819 * subdomain has a default channel name which must be strictly
1822 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1823 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1824 LTTNG_SYMBOL_NAME_LEN
)) {
1825 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1828 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1829 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1830 LTTNG_SYMBOL_NAME_LEN
)) {
1831 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1834 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1835 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1836 LTTNG_SYMBOL_NAME_LEN
)) {
1837 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1842 chan_ht
= usess
->domain_global
.channels
;
1844 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1845 if (uchan
== NULL
) {
1846 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1847 if (attr
.name
[0] != '\0') {
1848 usess
->has_non_default_channel
= 1;
1851 ret
= channel_ust_enable(usess
, uchan
);
1856 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1860 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1861 session
->has_non_mmap_channel
= true;
1869 enum lttng_error_code
cmd_add_map(struct command_ctx
*cmd_ctx
, int sock
)
1872 enum lttng_error_code ret_code
;
1874 struct lttng_payload map_payload
;
1875 ssize_t sock_recv_len
;
1876 struct lttng_map
*map
= NULL
;
1877 const struct lttng_credentials cmd_creds
= {
1878 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
1879 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
1882 lttng_payload_init(&map_payload
);
1883 map_len
= (size_t) cmd_ctx
->lsm
.u
.add_map
.length
;
1884 ret
= lttng_dynamic_buffer_set_size(
1885 &map_payload
.buffer
, map_len
);
1887 ret_code
= LTTNG_ERR_NOMEM
;
1891 sock_recv_len
= lttcomm_recv_unix_sock(
1892 sock
, map_payload
.buffer
.data
, map_len
);
1893 if (sock_recv_len
< 0 || sock_recv_len
!= map_len
) {
1894 ERR("Failed to receive \"register map\" command payload");
1895 ret_code
= LTTNG_ERR_INVALID_MAP
;
1899 /* Deserialize map. */
1901 struct lttng_payload_view view
=
1902 lttng_payload_view_from_payload(
1903 &map_payload
, 0, -1);
1905 if (lttng_map_create_from_payload(&view
, &map
) != map_len
) {
1906 ERR("Invalid map payload received in \"add map\" command");
1907 ret_code
= LTTNG_ERR_INVALID_MAP
;
1912 switch (lttng_map_get_domain(map
)) {
1913 case LTTNG_DOMAIN_KERNEL
:
1914 ret_code
= map_kernel_add(cmd_ctx
->session
->kernel_session
, map
);
1915 if (ret_code
!= LTTNG_OK
) {
1916 ERR("Creating a new kernel map: %s", lttng_strerror(ret_code
));
1920 ret_code
= LTTNG_OK
;
1922 case LTTNG_DOMAIN_UST
:
1923 ret
= map_ust_add(cmd_ctx
->session
->ust_session
, map
);
1925 ERR("Creating a new UST map: %s", lttng_strerror(-ret
));
1930 ret_code
= LTTNG_OK
;
1938 struct lttng_triggers
*triggers
= NULL
;
1939 enum lttng_trigger_status t_status
;
1940 unsigned int count
, i
;
1943 * FRDESO: beware of moving this code. This is currently not
1944 * racy because this is executed by the client thread and the
1945 * client thread is the thread registering new triggers. If
1946 * this code is relocate special care must be taken.
1948 ret_code
= notification_thread_command_list_triggers(
1949 notification_thread_handle
, 0, &triggers
);
1950 if (ret_code
!= LTTNG_OK
) {
1957 t_status
= lttng_triggers_get_count(triggers
, &count
);
1958 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
1960 lttng_triggers_destroy(triggers
);
1964 for (i
= 0; i
< count
; i
++) {
1965 const struct lttng_trigger
*trigger
;
1967 trigger
= lttng_triggers_get_at_index(triggers
, i
);
1970 ret_code
= sync_all_tracer_executed_actions(trigger
,
1971 &cmd_creds
, TRACER_EXECUTED_ACTION_STATE_REGISTER
);
1972 assert(ret_code
== LTTNG_OK
);
1975 lttng_triggers_destroy(triggers
);
1981 lttng_payload_reset(&map_payload
);
1985 enum lttng_error_code
cmd_enable_map(struct ltt_session
*session
,
1986 enum lttng_domain_type domain
, char *map_name
)
1988 struct ltt_ust_session
*usess
= session
->ust_session
;
1989 enum lttng_error_code ret_code
;
1991 DBG("Enabling map %s for session %s", map_name
, session
->name
);
1996 case LTTNG_DOMAIN_KERNEL
:
1998 struct ltt_kernel_map
*kmap
;
1999 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2001 kmap
= trace_kernel_get_map_by_name(map_name
, ksess
);
2003 ret_code
= LTTNG_ERR_KERNEL_MAP_NOT_FOUND
;
2007 ret_code
= map_kernel_enable(ksess
, kmap
);
2008 if (ret_code
!= LTTNG_OK
) {
2013 case LTTNG_DOMAIN_UST
:
2015 struct ltt_ust_map
*umap
;
2016 struct lttng_ht
*map_ht
;
2018 map_ht
= usess
->domain_global
.maps
;
2020 umap
= trace_ust_find_map_by_name(map_ht
, map_name
);
2022 ret_code
= LTTNG_ERR_UST_MAP_NOT_FOUND
;
2026 ret_code
= map_ust_enable(usess
, umap
);
2027 if (ret_code
!= LTTNG_OK
) {
2036 ret_code
= LTTNG_OK
;
2042 enum lttng_error_code
cmd_disable_map(struct ltt_session
*session
,
2043 enum lttng_domain_type domain
, char *map_name
)
2045 enum lttng_error_code ret_code
;
2050 case LTTNG_DOMAIN_KERNEL
:
2052 struct ltt_kernel_map
*kmap
;
2053 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2055 kmap
= trace_kernel_get_map_by_name(map_name
, ksess
);
2057 ret_code
= LTTNG_ERR_KERNEL_MAP_NOT_FOUND
;
2061 ret_code
= map_kernel_disable(ksess
, kmap
);
2062 if (ret_code
!= LTTNG_OK
) {
2067 case LTTNG_DOMAIN_UST
:
2069 struct ltt_ust_map
*umap
;
2070 struct lttng_ht
*map_ht
;
2071 struct ltt_ust_session
*usess
= session
->ust_session
;
2075 map_ht
= usess
->domain_global
.maps
;
2077 umap
= trace_ust_find_map_by_name(map_ht
, map_name
);
2079 ret_code
= LTTNG_ERR_UST_MAP_NOT_FOUND
;
2083 ret_code
= map_ust_disable(usess
, umap
);
2084 if (ret_code
!= LTTNG_OK
) {
2093 ret_code
= LTTNG_OK
;
2100 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
2101 struct ltt_session
*session
,
2102 enum lttng_domain_type domain
,
2103 enum lttng_process_attr process_attr
,
2104 enum lttng_tracking_policy
*policy
)
2106 enum lttng_error_code ret_code
= LTTNG_OK
;
2107 const struct process_attr_tracker
*tracker
;
2110 case LTTNG_DOMAIN_KERNEL
:
2111 if (!session
->kernel_session
) {
2112 ret_code
= LTTNG_ERR_INVALID
;
2115 tracker
= kernel_get_process_attr_tracker(
2116 session
->kernel_session
, process_attr
);
2118 case LTTNG_DOMAIN_UST
:
2119 if (!session
->ust_session
) {
2120 ret_code
= LTTNG_ERR_INVALID
;
2123 tracker
= trace_ust_get_process_attr_tracker(
2124 session
->ust_session
, process_attr
);
2127 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
2131 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
2133 ret_code
= LTTNG_ERR_INVALID
;
2139 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
2140 struct ltt_session
*session
,
2141 enum lttng_domain_type domain
,
2142 enum lttng_process_attr process_attr
,
2143 enum lttng_tracking_policy policy
)
2145 enum lttng_error_code ret_code
= LTTNG_OK
;
2148 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
2149 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
2150 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
2153 ret_code
= LTTNG_ERR_INVALID
;
2158 case LTTNG_DOMAIN_KERNEL
:
2159 if (!session
->kernel_session
) {
2160 ret_code
= LTTNG_ERR_INVALID
;
2163 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
2164 session
->kernel_session
, process_attr
, policy
);
2166 case LTTNG_DOMAIN_UST
:
2167 if (!session
->ust_session
) {
2168 ret_code
= LTTNG_ERR_INVALID
;
2171 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
2172 session
->ust_session
, process_attr
, policy
);
2175 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
2182 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
2183 struct ltt_session
*session
,
2184 enum lttng_domain_type domain
,
2185 enum lttng_process_attr process_attr
,
2186 const struct process_attr_value
*value
)
2188 enum lttng_error_code ret_code
= LTTNG_OK
;
2191 case LTTNG_DOMAIN_KERNEL
:
2192 if (!session
->kernel_session
) {
2193 ret_code
= LTTNG_ERR_INVALID
;
2196 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
2197 session
->kernel_session
, process_attr
, value
);
2199 case LTTNG_DOMAIN_UST
:
2200 if (!session
->ust_session
) {
2201 ret_code
= LTTNG_ERR_INVALID
;
2204 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
2205 session
->ust_session
, process_attr
, value
);
2208 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
2215 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
2216 struct ltt_session
*session
,
2217 enum lttng_domain_type domain
,
2218 enum lttng_process_attr process_attr
,
2219 const struct process_attr_value
*value
)
2221 enum lttng_error_code ret_code
= LTTNG_OK
;
2224 case LTTNG_DOMAIN_KERNEL
:
2225 if (!session
->kernel_session
) {
2226 ret_code
= LTTNG_ERR_INVALID
;
2229 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
2230 session
->kernel_session
, process_attr
, value
);
2232 case LTTNG_DOMAIN_UST
:
2233 if (!session
->ust_session
) {
2234 ret_code
= LTTNG_ERR_INVALID
;
2237 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
2238 session
->ust_session
, process_attr
, value
);
2241 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
2248 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
2249 struct ltt_session
*session
,
2250 enum lttng_domain_type domain
,
2251 enum lttng_process_attr process_attr
,
2252 struct lttng_process_attr_values
**values
)
2254 enum lttng_error_code ret_code
= LTTNG_OK
;
2255 const struct process_attr_tracker
*tracker
;
2256 enum process_attr_tracker_status status
;
2259 case LTTNG_DOMAIN_KERNEL
:
2260 if (!session
->kernel_session
) {
2261 ret_code
= LTTNG_ERR_INVALID
;
2264 tracker
= kernel_get_process_attr_tracker(
2265 session
->kernel_session
, process_attr
);
2267 case LTTNG_DOMAIN_UST
:
2268 if (!session
->ust_session
) {
2269 ret_code
= LTTNG_ERR_INVALID
;
2272 tracker
= trace_ust_get_process_attr_tracker(
2273 session
->ust_session
, process_attr
);
2276 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
2281 ret_code
= LTTNG_ERR_INVALID
;
2285 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
2287 case PROCESS_ATTR_TRACKER_STATUS_OK
:
2288 ret_code
= LTTNG_OK
;
2290 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
2291 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
2293 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
2294 ret_code
= LTTNG_ERR_NOMEM
;
2297 ret_code
= LTTNG_ERR_UNK
;
2306 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2308 int cmd_disable_event(struct ltt_session
*session
,
2309 enum lttng_domain_type domain
, const char *channel_name
,
2310 const struct lttng_event
*event
)
2313 const char *event_name
;
2315 DBG("Disable event command for event \'%s\'", event
->name
);
2317 event_name
= event
->name
;
2319 /* Error out on unhandled search criteria */
2320 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
2321 || event
->pid
|| event
->filter
|| event
->exclusion
) {
2322 ret
= LTTNG_ERR_UNK
;
2329 case LTTNG_DOMAIN_KERNEL
:
2331 struct ltt_kernel_channel
*kchan
;
2332 struct ltt_kernel_session
*ksess
;
2334 ksess
= session
->kernel_session
;
2337 * If a non-default channel has been created in the
2338 * session, explicitely require that -c chan_name needs
2341 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2342 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2346 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2347 if (kchan
== NULL
) {
2348 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2352 switch (event
->type
) {
2353 case LTTNG_EVENT_ALL
:
2354 case LTTNG_EVENT_TRACEPOINT
:
2355 case LTTNG_EVENT_SYSCALL
:
2356 case LTTNG_EVENT_PROBE
:
2357 case LTTNG_EVENT_FUNCTION
:
2358 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
2359 if (event_name
[0] == '\0') {
2360 ret
= event_kernel_disable_event(kchan
,
2363 ret
= event_kernel_disable_event(kchan
,
2364 event_name
, event
->type
);
2366 if (ret
!= LTTNG_OK
) {
2371 ret
= LTTNG_ERR_UNK
;
2375 kernel_wait_quiescent();
2378 case LTTNG_DOMAIN_UST
:
2380 struct ltt_ust_channel
*uchan
;
2381 struct ltt_ust_session
*usess
;
2383 usess
= session
->ust_session
;
2385 if (validate_ust_event_name(event_name
)) {
2386 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2391 * If a non-default channel has been created in the
2392 * session, explicitly require that -c chan_name needs
2395 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2396 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2400 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2402 if (uchan
== NULL
) {
2403 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2407 switch (event
->type
) {
2408 case LTTNG_EVENT_ALL
:
2410 * An empty event name means that everything
2411 * should be disabled.
2413 if (event
->name
[0] == '\0') {
2414 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
2416 ret
= event_ust_disable_tracepoint(usess
, uchan
,
2419 if (ret
!= LTTNG_OK
) {
2424 ret
= LTTNG_ERR_UNK
;
2428 DBG3("Disable UST event %s in channel %s completed", event_name
,
2432 case LTTNG_DOMAIN_LOG4J
:
2433 case LTTNG_DOMAIN_JUL
:
2434 case LTTNG_DOMAIN_PYTHON
:
2437 struct ltt_ust_session
*usess
= session
->ust_session
;
2441 switch (event
->type
) {
2442 case LTTNG_EVENT_ALL
:
2445 ret
= LTTNG_ERR_UNK
;
2449 agt
= trace_ust_find_agent(usess
, domain
);
2451 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
2455 * An empty event name means that everything
2456 * should be disabled.
2458 if (event
->name
[0] == '\0') {
2459 ret
= event_agent_disable_all(usess
, agt
);
2461 ret
= event_agent_disable(usess
, agt
, event_name
);
2463 if (ret
!= LTTNG_OK
) {
2470 ret
= LTTNG_ERR_UND
;
2483 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2485 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
2486 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
2488 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
2489 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
2492 * Don't try to add a context if the session has been started at
2493 * some point in time before. The tracer does not allow it and would
2494 * result in a corrupted trace.
2496 if (session
->has_been_started
) {
2497 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2501 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2502 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
2503 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
2507 case LTTNG_DOMAIN_KERNEL
:
2508 assert(session
->kernel_session
);
2510 if (session
->kernel_session
->channel_count
== 0) {
2511 /* Create default channel */
2512 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
2513 if (ret
!= LTTNG_OK
) {
2516 chan_kern_created
= 1;
2518 /* Add kernel context to kernel tracer */
2519 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
2520 if (ret
!= LTTNG_OK
) {
2524 case LTTNG_DOMAIN_JUL
:
2525 case LTTNG_DOMAIN_LOG4J
:
2528 * Validate channel name.
2529 * If no channel name is given and the domain is JUL or LOG4J,
2530 * set it to the appropriate domain-specific channel name. If
2531 * a name is provided but does not match the expexted channel
2532 * name, return an error.
2534 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
2535 strcmp(channel_name
,
2536 DEFAULT_JUL_CHANNEL_NAME
)) {
2537 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2539 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
2540 strcmp(channel_name
,
2541 DEFAULT_LOG4J_CHANNEL_NAME
)) {
2542 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2545 /* break is _not_ missing here. */
2547 case LTTNG_DOMAIN_UST
:
2549 struct ltt_ust_session
*usess
= session
->ust_session
;
2550 unsigned int chan_count
;
2554 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2555 if (chan_count
== 0) {
2556 struct lttng_channel
*attr
;
2557 /* Create default channel */
2558 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2560 ret
= LTTNG_ERR_FATAL
;
2564 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2565 if (ret
!= LTTNG_OK
) {
2569 channel_attr_destroy(attr
);
2570 chan_ust_created
= 1;
2573 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2574 free(app_ctx_provider_name
);
2576 app_ctx_name
= NULL
;
2577 app_ctx_provider_name
= NULL
;
2578 if (ret
!= LTTNG_OK
) {
2584 ret
= LTTNG_ERR_UND
;
2592 if (chan_kern_created
) {
2593 struct ltt_kernel_channel
*kchan
=
2594 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2595 session
->kernel_session
);
2596 /* Created previously, this should NOT fail. */
2598 kernel_destroy_channel(kchan
);
2601 if (chan_ust_created
) {
2602 struct ltt_ust_channel
*uchan
=
2603 trace_ust_find_channel_by_name(
2604 session
->ust_session
->domain_global
.channels
,
2605 DEFAULT_CHANNEL_NAME
);
2606 /* Created previously, this should NOT fail. */
2608 /* Remove from the channel list of the session. */
2609 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2611 trace_ust_destroy_channel(uchan
);
2614 free(app_ctx_provider_name
);
2619 static inline bool name_starts_with(const char *name
, const char *prefix
)
2621 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2623 return !strncmp(name
, prefix
, max_cmp_len
);
2626 /* Perform userspace-specific event name validation */
2627 static int validate_ust_event_name(const char *name
)
2637 * Check name against all internal UST event component namespaces used
2640 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2641 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2642 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2651 * Internal version of cmd_enable_event() with a supplemental
2652 * "internal_event" flag which is used to enable internal events which should
2653 * be hidden from clients. Such events are used in the agent implementation to
2654 * enable the events through which all "agent" events are funeled.
2656 static int _cmd_enable_event(struct ltt_session
*session
,
2657 const struct lttng_domain
*domain
,
2658 char *channel_name
, struct lttng_event
*event
,
2659 char *filter_expression
,
2660 struct lttng_bytecode
*filter
,
2661 struct lttng_event_exclusion
*exclusion
,
2662 int wpipe
, bool internal_event
)
2664 int ret
= 0, channel_created
= 0;
2665 struct lttng_channel
*attr
= NULL
;
2669 assert(channel_name
);
2671 /* If we have a filter, we must have its filter expression */
2672 assert(!(!!filter_expression
^ !!filter
));
2674 /* Normalize event name as a globbing pattern */
2675 strutils_normalize_star_glob_pattern(event
->name
);
2677 /* Normalize exclusion names as globbing patterns */
2681 for (i
= 0; i
< exclusion
->count
; i
++) {
2682 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2684 strutils_normalize_star_glob_pattern(name
);
2688 DBG("Enable event command for event \'%s\'", event
->name
);
2692 switch (domain
->type
) {
2693 case LTTNG_DOMAIN_KERNEL
:
2695 struct ltt_kernel_channel
*kchan
;
2698 * If a non-default channel has been created in the
2699 * session, explicitely require that -c chan_name needs
2702 if (session
->kernel_session
->has_non_default_channel
2703 && channel_name
[0] == '\0') {
2704 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2708 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2709 session
->kernel_session
);
2710 if (kchan
== NULL
) {
2711 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2712 LTTNG_BUFFER_GLOBAL
);
2714 ret
= LTTNG_ERR_FATAL
;
2717 if (lttng_strncpy(attr
->name
, channel_name
,
2718 sizeof(attr
->name
))) {
2719 ret
= LTTNG_ERR_INVALID
;
2723 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2724 if (ret
!= LTTNG_OK
) {
2727 channel_created
= 1;
2730 /* Get the newly created kernel channel pointer */
2731 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2732 session
->kernel_session
);
2733 if (kchan
== NULL
) {
2734 /* This sould not happen... */
2735 ret
= LTTNG_ERR_FATAL
;
2739 switch (event
->type
) {
2740 case LTTNG_EVENT_ALL
:
2742 char *filter_expression_a
= NULL
;
2743 struct lttng_bytecode
*filter_a
= NULL
;
2746 * We need to duplicate filter_expression and filter,
2747 * because ownership is passed to first enable
2750 if (filter_expression
) {
2751 filter_expression_a
= strdup(filter_expression
);
2752 if (!filter_expression_a
) {
2753 ret
= LTTNG_ERR_FATAL
;
2758 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2760 free(filter_expression_a
);
2761 ret
= LTTNG_ERR_FATAL
;
2764 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2766 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2767 ret
= event_kernel_enable_event(kchan
, event
,
2768 filter_expression
, filter
);
2769 /* We have passed ownership */
2770 filter_expression
= NULL
;
2772 if (ret
!= LTTNG_OK
) {
2773 if (channel_created
) {
2774 /* Let's not leak a useless channel. */
2775 kernel_destroy_channel(kchan
);
2777 free(filter_expression_a
);
2781 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2782 ret
= event_kernel_enable_event(kchan
, event
,
2783 filter_expression_a
, filter_a
);
2784 /* We have passed ownership */
2785 filter_expression_a
= NULL
;
2787 if (ret
!= LTTNG_OK
) {
2792 case LTTNG_EVENT_PROBE
:
2793 case LTTNG_EVENT_USERSPACE_PROBE
:
2794 case LTTNG_EVENT_FUNCTION
:
2795 case LTTNG_EVENT_FUNCTION_ENTRY
:
2796 case LTTNG_EVENT_TRACEPOINT
:
2797 ret
= event_kernel_enable_event(kchan
, event
,
2798 filter_expression
, filter
);
2799 /* We have passed ownership */
2800 filter_expression
= NULL
;
2802 if (ret
!= LTTNG_OK
) {
2803 if (channel_created
) {
2804 /* Let's not leak a useless channel. */
2805 kernel_destroy_channel(kchan
);
2810 case LTTNG_EVENT_SYSCALL
:
2811 ret
= event_kernel_enable_event(kchan
, event
,
2812 filter_expression
, filter
);
2813 /* We have passed ownership */
2814 filter_expression
= NULL
;
2816 if (ret
!= LTTNG_OK
) {
2821 ret
= LTTNG_ERR_UNK
;
2825 kernel_wait_quiescent();
2828 case LTTNG_DOMAIN_UST
:
2830 struct ltt_ust_channel
*uchan
;
2831 struct ltt_ust_session
*usess
= session
->ust_session
;
2836 * If a non-default channel has been created in the
2837 * session, explicitely require that -c chan_name needs
2840 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2841 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2845 /* Get channel from global UST domain */
2846 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2848 if (uchan
== NULL
) {
2849 /* Create default channel */
2850 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2851 usess
->buffer_type
);
2853 ret
= LTTNG_ERR_FATAL
;
2856 if (lttng_strncpy(attr
->name
, channel_name
,
2857 sizeof(attr
->name
))) {
2858 ret
= LTTNG_ERR_INVALID
;
2862 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2863 if (ret
!= LTTNG_OK
) {
2867 /* Get the newly created channel reference back */
2868 uchan
= trace_ust_find_channel_by_name(
2869 usess
->domain_global
.channels
, channel_name
);
2873 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2875 * Don't allow users to add UST events to channels which
2876 * are assigned to a userspace subdomain (JUL, Log4J,
2879 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2883 if (!internal_event
) {
2885 * Ensure the event name is not reserved for internal
2888 ret
= validate_ust_event_name(event
->name
);
2890 WARN("Userspace event name %s failed validation.",
2892 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2897 /* At this point, the session and channel exist on the tracer */
2898 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2899 filter_expression
, filter
, exclusion
,
2901 /* We have passed ownership */
2902 filter_expression
= NULL
;
2905 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2906 goto already_enabled
;
2907 } else if (ret
!= LTTNG_OK
) {
2912 case LTTNG_DOMAIN_LOG4J
:
2913 case LTTNG_DOMAIN_JUL
:
2914 case LTTNG_DOMAIN_PYTHON
:
2916 const char *default_event_name
, *default_chan_name
;
2918 struct lttng_event uevent
;
2919 struct lttng_domain tmp_dom
;
2920 struct ltt_ust_session
*usess
= session
->ust_session
;
2924 if (!agent_tracing_is_enabled()) {
2925 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2926 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2930 agt
= trace_ust_find_agent(usess
, domain
->type
);
2932 agt
= agent_create(domain
->type
);
2934 ret
= LTTNG_ERR_NOMEM
;
2937 agent_add(agt
, usess
->agents
);
2940 /* Create the default tracepoint. */
2941 memset(&uevent
, 0, sizeof(uevent
));
2942 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2943 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2944 default_event_name
= event_get_default_agent_ust_name(
2946 if (!default_event_name
) {
2947 ret
= LTTNG_ERR_FATAL
;
2950 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2951 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2954 * The domain type is changed because we are about to enable the
2955 * default channel and event for the JUL domain that are hardcoded.
2956 * This happens in the UST domain.
2958 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2959 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2961 switch (domain
->type
) {
2962 case LTTNG_DOMAIN_LOG4J
:
2963 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2965 case LTTNG_DOMAIN_JUL
:
2966 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2968 case LTTNG_DOMAIN_PYTHON
:
2969 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2972 /* The switch/case we are in makes this impossible */
2977 char *filter_expression_copy
= NULL
;
2978 struct lttng_bytecode
*filter_copy
= NULL
;
2981 const size_t filter_size
= sizeof(
2982 struct lttng_bytecode
)
2985 filter_copy
= zmalloc(filter_size
);
2987 ret
= LTTNG_ERR_NOMEM
;
2990 memcpy(filter_copy
, filter
, filter_size
);
2992 filter_expression_copy
=
2993 strdup(filter_expression
);
2994 if (!filter_expression
) {
2995 ret
= LTTNG_ERR_NOMEM
;
2998 if (!filter_expression_copy
|| !filter_copy
) {
2999 free(filter_expression_copy
);
3005 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
3006 (char *) default_chan_name
,
3007 &uevent
, filter_expression_copy
,
3008 filter_copy
, NULL
, wpipe
);
3011 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
3012 goto already_enabled
;
3013 } else if (ret
!= LTTNG_OK
) {
3017 /* The wild card * means that everything should be enabled. */
3018 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
3019 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
3022 ret
= event_agent_enable(usess
, agt
, event
, filter
,
3026 filter_expression
= NULL
;
3027 if (ret
!= LTTNG_OK
) {
3034 ret
= LTTNG_ERR_UND
;
3042 free(filter_expression
);
3045 channel_attr_destroy(attr
);
3051 * Command LTTNG_ENABLE_EVENT processed by the client thread.
3052 * We own filter, exclusion, and filter_expression.
3054 int cmd_enable_event(struct ltt_session
*session
,
3055 const struct lttng_domain
*domain
,
3056 char *channel_name
, struct lttng_event
*event
,
3057 char *filter_expression
,
3058 struct lttng_bytecode
*filter
,
3059 struct lttng_event_exclusion
*exclusion
,
3062 return _cmd_enable_event(session
, domain
, channel_name
, event
,
3063 filter_expression
, filter
, exclusion
, wpipe
, false);
3067 * Enable an event which is internal to LTTng. An internal should
3068 * never be made visible to clients and are immune to checks such as
3071 static int cmd_enable_event_internal(struct ltt_session
*session
,
3072 const struct lttng_domain
*domain
,
3073 char *channel_name
, struct lttng_event
*event
,
3074 char *filter_expression
,
3075 struct lttng_bytecode
*filter
,
3076 struct lttng_event_exclusion
*exclusion
,
3079 return _cmd_enable_event(session
, domain
, channel_name
, event
,
3080 filter_expression
, filter
, exclusion
, wpipe
, true);
3084 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3086 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
3087 struct lttng_event
**events
)
3090 ssize_t nb_events
= 0;
3093 case LTTNG_DOMAIN_KERNEL
:
3094 nb_events
= kernel_list_events(events
);
3095 if (nb_events
< 0) {
3096 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
3100 case LTTNG_DOMAIN_UST
:
3101 nb_events
= ust_app_list_events(events
);
3102 if (nb_events
< 0) {
3103 ret
= LTTNG_ERR_UST_LIST_FAIL
;
3107 case LTTNG_DOMAIN_LOG4J
:
3108 case LTTNG_DOMAIN_JUL
:
3109 case LTTNG_DOMAIN_PYTHON
:
3110 nb_events
= agent_list_events(events
, domain
);
3111 if (nb_events
< 0) {
3112 ret
= LTTNG_ERR_UST_LIST_FAIL
;
3117 ret
= LTTNG_ERR_UND
;
3124 /* Return negative value to differentiate return code */
3129 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3131 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
3132 struct lttng_event_field
**fields
)
3135 ssize_t nb_fields
= 0;
3138 case LTTNG_DOMAIN_UST
:
3139 nb_fields
= ust_app_list_event_fields(fields
);
3140 if (nb_fields
< 0) {
3141 ret
= LTTNG_ERR_UST_LIST_FAIL
;
3145 case LTTNG_DOMAIN_KERNEL
:
3146 default: /* fall-through */
3147 ret
= LTTNG_ERR_UND
;
3154 /* Return negative value to differentiate return code */
3158 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
3160 return syscall_table_list(events
);
3164 * Command LTTNG_START_TRACE processed by the client thread.
3166 * Called with session mutex held.
3168 int cmd_start_trace(struct ltt_session
*session
)
3170 enum lttng_error_code ret
;
3171 unsigned long nb_chan
= 0, nb_map
= 0;
3172 struct ltt_kernel_session
*ksession
;
3173 struct ltt_ust_session
*usess
;
3174 const bool session_rotated_after_last_stop
=
3175 session
->rotated_after_last_stop
;
3176 const bool session_cleared_after_last_stop
=
3177 session
->cleared_after_last_stop
;
3181 /* Ease our life a bit ;) */
3182 ksession
= session
->kernel_session
;
3183 usess
= session
->ust_session
;
3185 /* Is the session already started? */
3186 if (session
->active
) {
3187 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
3188 /* Perform nothing */
3192 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
3193 !session
->current_trace_chunk
) {
3195 * A rotation was launched while the session was stopped and
3196 * it has not been completed yet. It is not possible to start
3197 * the session since starting the session here would require a
3198 * rotation from "NULL" to a new trace chunk. That rotation
3199 * would overlap with the ongoing rotation, which is not
3202 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
3204 ret
= LTTNG_ERR_ROTATION_PENDING
;
3209 * Starting a session without channel is useless since after that it's not
3210 * possible to enable channel thus inform the client.
3212 if (usess
&& usess
->domain_global
.channels
) {
3213 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
3214 nb_map
+= lttng_ht_get_count(usess
->domain_global
.maps
);
3217 nb_chan
+= ksession
->channel_count
;
3218 nb_map
+= ksession
->map_count
;
3220 if (!nb_chan
&& !nb_map
) {
3221 ret
= LTTNG_ERR_NO_CHANNEL
;
3225 session
->active
= 1;
3226 session
->rotated_after_last_stop
= false;
3227 session
->cleared_after_last_stop
= false;
3228 if (session
->output_traces
&& !session
->current_trace_chunk
) {
3229 if (!session
->has_been_started
) {
3230 struct lttng_trace_chunk
*trace_chunk
;
3232 DBG("Creating initial trace chunk of session \"%s\"",
3234 trace_chunk
= session_create_new_trace_chunk(
3235 session
, NULL
, NULL
, NULL
);
3237 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
3240 assert(!session
->current_trace_chunk
);
3241 ret
= session_set_trace_chunk(session
, trace_chunk
,
3243 lttng_trace_chunk_put(trace_chunk
);
3245 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
3249 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
3252 * Rotate existing streams into the new chunk.
3253 * This is a "quiet" rotation has no client has
3254 * explicitly requested this operation.
3256 * There is also no need to wait for the rotation
3257 * to complete as it will happen immediately. No data
3258 * was produced as the session was stopped, so the
3259 * rotation should happen on reception of the command.
3261 ret
= cmd_rotate_session(session
, NULL
, true,
3262 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3263 if (ret
!= LTTNG_OK
) {
3269 /* Kernel tracing */
3270 if (ksession
!= NULL
) {
3271 DBG("Start kernel tracing session %s", session
->name
);
3272 ret
= start_kernel_session(ksession
);
3273 if (ret
!= LTTNG_OK
) {
3278 /* Flag session that trace should start automatically */
3280 int int_ret
= ust_app_start_trace_all(usess
);
3283 ret
= LTTNG_ERR_UST_START_FAIL
;
3289 * Open a packet in every stream of the session to ensure that viewers
3290 * can correctly identify the boundaries of the periods during which
3291 * tracing was active for this session.
3293 ret
= session_open_packets(session
);
3294 if (ret
!= LTTNG_OK
) {
3299 * Clear the flag that indicates that a rotation was done while the
3300 * session was stopped.
3302 session
->rotated_after_last_stop
= false;
3304 if (session
->rotate_timer_period
) {
3305 int int_ret
= timer_session_rotation_schedule_timer_start(
3306 session
, session
->rotate_timer_period
);
3309 ERR("Failed to enable rotate timer");
3310 ret
= LTTNG_ERR_UNK
;
3318 if (ret
== LTTNG_OK
) {
3319 /* Flag this after a successful start. */
3320 session
->has_been_started
|= 1;
3322 session
->active
= 0;
3323 /* Restore initial state on error. */
3324 session
->rotated_after_last_stop
=
3325 session_rotated_after_last_stop
;
3326 session
->cleared_after_last_stop
=
3327 session_cleared_after_last_stop
;
3334 * Command LTTNG_STOP_TRACE processed by the client thread.
3336 int cmd_stop_trace(struct ltt_session
*session
)
3339 struct ltt_kernel_session
*ksession
;
3340 struct ltt_ust_session
*usess
;
3344 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
3346 ksession
= session
->kernel_session
;
3347 usess
= session
->ust_session
;
3349 /* Session is not active. Skip everythong and inform the client. */
3350 if (!session
->active
) {
3351 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
3355 ret
= stop_kernel_session(ksession
);
3356 if (ret
!= LTTNG_OK
) {
3360 if (usess
&& usess
->active
) {
3361 ret
= ust_app_stop_trace_all(usess
);
3363 ret
= LTTNG_ERR_UST_STOP_FAIL
;
3368 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
3370 /* Flag inactive after a successful stop. */
3371 session
->active
= 0;
3379 * Set the base_path of the session only if subdir of a control uris is set.
3380 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
3382 static int set_session_base_path_from_uris(struct ltt_session
*session
,
3384 struct lttng_uri
*uris
)
3389 for (i
= 0; i
< nb_uri
; i
++) {
3390 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
3391 uris
[i
].subdir
[0] == '\0') {
3392 /* Not interested in these URIs */
3396 if (session
->base_path
!= NULL
) {
3397 free(session
->base_path
);
3398 session
->base_path
= NULL
;
3401 /* Set session base_path */
3402 session
->base_path
= strdup(uris
[i
].subdir
);
3403 if (!session
->base_path
) {
3404 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
3405 uris
[i
].subdir
, session
->name
);
3406 ret
= LTTNG_ERR_NOMEM
;
3409 DBG2("Setting base path \"%s\" for session \"%s\"",
3410 session
->base_path
, session
->name
);
3418 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3420 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
3421 struct lttng_uri
*uris
)
3424 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3425 struct ltt_ust_session
*usess
= session
->ust_session
;
3431 /* Can't set consumer URI if the session is active. */
3432 if (session
->active
) {
3433 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
3438 * Set the session base path if any. This is done inside
3439 * cmd_set_consumer_uri to preserve backward compatibility of the
3440 * previous session creation api vs the session descriptor api.
3442 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
3443 if (ret
!= LTTNG_OK
) {
3447 /* Set the "global" consumer URIs */
3448 for (i
= 0; i
< nb_uri
; i
++) {
3449 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3451 if (ret
!= LTTNG_OK
) {
3456 /* Set UST session URIs */
3457 if (session
->ust_session
) {
3458 for (i
= 0; i
< nb_uri
; i
++) {
3459 ret
= add_uri_to_consumer(session
,
3460 session
->ust_session
->consumer
,
3461 &uris
[i
], LTTNG_DOMAIN_UST
);
3462 if (ret
!= LTTNG_OK
) {
3468 /* Set kernel session URIs */
3469 if (session
->kernel_session
) {
3470 for (i
= 0; i
< nb_uri
; i
++) {
3471 ret
= add_uri_to_consumer(session
,
3472 session
->kernel_session
->consumer
,
3473 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3474 if (ret
!= LTTNG_OK
) {
3481 * Make sure to set the session in output mode after we set URI since a
3482 * session can be created without URL (thus flagged in no output mode).
3484 session
->output_traces
= 1;
3486 ksess
->output_traces
= 1;
3490 usess
->output_traces
= 1;
3501 enum lttng_error_code
set_session_output_from_descriptor(
3502 struct ltt_session
*session
,
3503 const struct lttng_session_descriptor
*descriptor
)
3506 enum lttng_error_code ret_code
= LTTNG_OK
;
3507 enum lttng_session_descriptor_type session_type
=
3508 lttng_session_descriptor_get_type(descriptor
);
3509 enum lttng_session_descriptor_output_type output_type
=
3510 lttng_session_descriptor_get_output_type(descriptor
);
3511 struct lttng_uri uris
[2] = {};
3512 size_t uri_count
= 0;
3514 switch (output_type
) {
3515 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3517 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3518 lttng_session_descriptor_get_local_output_uri(descriptor
,
3522 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3523 lttng_session_descriptor_get_network_output_uris(descriptor
,
3524 &uris
[0], &uris
[1]);
3528 ret_code
= LTTNG_ERR_INVALID
;
3532 switch (session_type
) {
3533 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3535 struct snapshot_output
*new_output
= NULL
;
3537 new_output
= snapshot_output_alloc();
3539 ret_code
= LTTNG_ERR_NOMEM
;
3543 ret
= snapshot_output_init_with_uri(session
,
3544 DEFAULT_SNAPSHOT_MAX_SIZE
,
3545 NULL
, uris
, uri_count
, session
->consumer
,
3546 new_output
, &session
->snapshot
);
3548 ret_code
= (ret
== -ENOMEM
) ?
3549 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3550 snapshot_output_destroy(new_output
);
3553 snapshot_add_output(&session
->snapshot
, new_output
);
3556 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3557 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3559 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3563 ret_code
= LTTNG_ERR_INVALID
;
3571 enum lttng_error_code
cmd_create_session_from_descriptor(
3572 struct lttng_session_descriptor
*descriptor
,
3573 const lttng_sock_cred
*creds
,
3574 const char *home_path
)
3577 enum lttng_error_code ret_code
;
3578 const char *session_name
;
3579 struct ltt_session
*new_session
= NULL
;
3580 enum lttng_session_descriptor_status descriptor_status
;
3582 session_lock_list();
3584 if (*home_path
!= '/') {
3585 ERR("Home path provided by client is not absolute");
3586 ret_code
= LTTNG_ERR_INVALID
;
3591 descriptor_status
= lttng_session_descriptor_get_session_name(
3592 descriptor
, &session_name
);
3593 switch (descriptor_status
) {
3594 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3596 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3597 session_name
= NULL
;
3600 ret_code
= LTTNG_ERR_INVALID
;
3604 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3606 if (ret_code
!= LTTNG_OK
) {
3610 if (!session_name
) {
3611 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3614 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3619 if (!lttng_session_descriptor_is_output_destination_initialized(
3622 * Only include the session's creation time in the output
3623 * destination if the name of the session itself was
3624 * not auto-generated.
3626 ret_code
= lttng_session_descriptor_set_default_output(
3628 session_name
? &new_session
->creation_time
: NULL
,
3630 if (ret_code
!= LTTNG_OK
) {
3634 new_session
->has_user_specified_directory
=
3635 lttng_session_descriptor_has_output_directory(
3639 switch (lttng_session_descriptor_get_type(descriptor
)) {
3640 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3641 new_session
->snapshot_mode
= 1;
3643 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3644 new_session
->live_timer
=
3645 lttng_session_descriptor_live_get_timer_interval(
3652 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3653 if (ret_code
!= LTTNG_OK
) {
3656 new_session
->consumer
->enabled
= 1;
3657 ret_code
= LTTNG_OK
;
3659 /* Release reference provided by the session_create function. */
3660 session_put(new_session
);
3661 if (ret_code
!= LTTNG_OK
&& new_session
) {
3662 /* Release the global reference on error. */
3663 session_destroy(new_session
);
3665 session_unlock_list();
3669 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3670 struct lttng_session_descriptor
**return_descriptor
)
3673 size_t payload_size
;
3674 struct lttng_dynamic_buffer payload
;
3675 struct lttng_buffer_view home_dir_view
;
3676 struct lttng_buffer_view session_descriptor_view
;
3677 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3678 enum lttng_error_code ret_code
;
3680 lttng_dynamic_buffer_init(&payload
);
3681 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3683 ret_code
= LTTNG_ERR_INVALID
;
3686 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3687 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3688 ret_code
= LTTNG_ERR_INVALID
;
3692 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3693 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3694 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3696 ret_code
= LTTNG_ERR_NOMEM
;
3700 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3702 ERR("Reception of session descriptor failed, aborting.");
3703 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3707 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3710 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3711 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3712 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3713 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3714 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3718 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3720 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3721 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3722 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3723 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3724 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3728 ret
= lttng_session_descriptor_create_from_buffer(
3729 &session_descriptor_view
, &session_descriptor
);
3731 ERR("Failed to create session descriptor from payload of \"create session\" command");
3732 ret_code
= LTTNG_ERR_INVALID
;
3737 * Sets the descriptor's auto-generated properties (name, output) if
3740 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3742 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3743 if (ret_code
!= LTTNG_OK
) {
3747 ret_code
= LTTNG_OK
;
3748 *return_descriptor
= session_descriptor
;
3749 session_descriptor
= NULL
;
3751 lttng_dynamic_buffer_reset(&payload
);
3752 lttng_session_descriptor_destroy(session_descriptor
);
3757 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3758 void *_reply_context
)
3762 const struct cmd_destroy_session_reply_context
*reply_context
=
3764 struct lttng_dynamic_buffer payload
;
3765 struct lttcomm_session_destroy_command_header cmd_header
;
3766 struct lttng_trace_archive_location
*location
= NULL
;
3767 struct lttcomm_lttng_msg llm
= {
3768 .cmd_type
= LTTNG_DESTROY_SESSION
,
3769 .ret_code
= reply_context
->destruction_status
,
3772 sizeof(struct lttcomm_session_destroy_command_header
),
3775 size_t payload_size_before_location
;
3777 lttng_dynamic_buffer_init(&payload
);
3779 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3781 ERR("Failed to append session destruction message");
3785 cmd_header
.rotation_state
=
3786 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3787 session
->rotation_state
:
3788 LTTNG_ROTATION_STATE_NO_ROTATION
);
3789 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3790 sizeof(cmd_header
));
3792 ERR("Failed to append session destruction command header");
3796 if (!reply_context
->implicit_rotation_on_destroy
) {
3797 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3801 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3802 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3807 location
= session_get_trace_archive_location(session
);
3809 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3814 payload_size_before_location
= payload
.size
;
3815 comm_ret
= lttng_trace_archive_location_serialize(location
,
3818 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3822 /* Update the message to indicate the location's length. */
3823 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3824 payload
.size
- payload_size_before_location
;
3826 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3827 payload
.data
, payload
.size
);
3828 if (comm_ret
!= (ssize_t
) payload
.size
) {
3829 ERR("Failed to send result of the destruction of session \"%s\" to client",
3833 ret
= close(reply_context
->reply_sock_fd
);
3835 PERROR("Failed to close client socket in deferred session destroy reply");
3837 lttng_dynamic_buffer_reset(&payload
);
3838 free(_reply_context
);
3842 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3844 * Called with session lock held.
3846 int cmd_destroy_session(struct ltt_session
*session
,
3847 struct notification_thread_handle
*notification_thread_handle
,
3851 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3852 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3855 reply_context
= zmalloc(sizeof(*reply_context
));
3856 if (!reply_context
) {
3857 ret
= LTTNG_ERR_NOMEM
;
3860 reply_context
->reply_sock_fd
= *sock_fd
;
3866 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3868 if (session
->active
) {
3869 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3871 ret
= cmd_stop_trace(session
);
3872 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3873 /* Carry on with the destruction of the session. */
3874 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3875 session
->name
, lttng_strerror(-ret
));
3876 destruction_last_error
= ret
;
3880 if (session
->rotation_schedule_timer_enabled
) {
3881 if (timer_session_rotation_schedule_timer_stop(
3883 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3885 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3889 if (session
->rotate_size
) {
3890 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3891 session
->rotate_size
= 0;
3894 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3896 * Perform a last rotation on destruction if rotations have
3897 * occurred during the session's lifetime.
3899 ret
= cmd_rotate_session(session
, NULL
, false,
3900 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3901 if (ret
!= LTTNG_OK
) {
3902 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3903 session
->name
, lttng_strerror(-ret
));
3904 destruction_last_error
= -ret
;
3906 if (reply_context
) {
3907 reply_context
->implicit_rotation_on_destroy
= true;
3909 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3911 * The user has not triggered a session rotation. However, to
3912 * ensure all data has been consumed, the session is rotated
3913 * to a 'null' trace chunk before it is destroyed.
3915 * This is a "quiet" rotation meaning that no notification is
3916 * emitted and no renaming of the current trace chunk takes
3919 ret
= cmd_rotate_session(session
, NULL
, true,
3920 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3922 * Rotation operations may not be supported by the kernel
3923 * tracer. Hence, do not consider this implicit rotation as
3924 * a session destruction error. The library has already stopped
3925 * the session and waited for pending data; there is nothing
3926 * left to do but complete the destruction of the session.
3928 if (ret
!= LTTNG_OK
&&
3929 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3930 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3931 session
->name
, lttng_strerror(ret
));
3932 destruction_last_error
= -ret
;
3936 if (session
->shm_path
[0]) {
3938 * When a session is created with an explicit shm_path,
3939 * the consumer daemon will create its shared memory files
3940 * at that location and will *not* unlink them. This is normal
3941 * as the intention of that feature is to make it possible
3942 * to retrieve the content of those files should a crash occur.
3944 * To ensure the content of those files can be used, the
3945 * sessiond daemon will replicate the content of the metadata
3946 * cache in a metadata file.
3948 * On clean-up, it is expected that the consumer daemon will
3949 * unlink the shared memory files and that the session daemon
3950 * will unlink the metadata file. Then, the session's directory
3951 * in the shm path can be removed.
3953 * Unfortunately, a flaw in the design of the sessiond's and
3954 * consumerd's tear down of channels makes it impossible to
3955 * determine when the sessiond _and_ the consumerd have both
3956 * destroyed their representation of a channel. For one, the
3957 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3958 * callbacks in both daemons.
3960 * However, it is also impossible for the sessiond to know when
3961 * the consumer daemon is done destroying its channel(s) since
3962 * it occurs as a reaction to the closing of the channel's file
3963 * descriptor. There is no resulting communication initiated
3964 * from the consumerd to the sessiond to confirm that the
3965 * operation is completed (and was successful).
3967 * Until this is all fixed, the session daemon checks for the
3968 * removal of the session's shm path which makes it possible
3969 * to safely advertise a session as having been destroyed.
3971 * Prior to this fix, it was not possible to reliably save
3972 * a session making use of the --shm-path option, destroy it,
3973 * and load it again. This is because the creation of the
3974 * session would fail upon seeing the session's shm path
3975 * already in existence.
3977 * Note that none of the error paths in the check for the
3978 * directory's existence return an error. This is normal
3979 * as there isn't much that can be done. The session will
3980 * be destroyed properly, except that we can't offer the
3981 * guarantee that the same session can be re-created.
3983 current_completion_handler
= &destroy_completion_handler
.handler
;
3984 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3986 sizeof(destroy_completion_handler
.shm_path
));
3991 * The session is destroyed. However, note that the command context
3992 * still holds a reference to the session, thus delaying its destruction
3993 * _at least_ up to the point when that reference is released.
3995 session_destroy(session
);
3996 if (reply_context
) {
3997 reply_context
->destruction_status
= destruction_last_error
;
3998 ret
= session_add_destroy_notifier(session
,
3999 cmd_destroy_session_reply
,
4000 (void *) reply_context
);
4002 ret
= LTTNG_ERR_FATAL
;
4014 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
4016 int cmd_register_consumer(struct ltt_session
*session
,
4017 enum lttng_domain_type domain
, const char *sock_path
,
4018 struct consumer_data
*cdata
)
4021 struct consumer_socket
*socket
= NULL
;
4028 case LTTNG_DOMAIN_KERNEL
:
4030 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4034 /* Can't register a consumer if there is already one */
4035 if (ksess
->consumer_fds_sent
!= 0) {
4036 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
4040 sock
= lttcomm_connect_unix_sock(sock_path
);
4042 ret
= LTTNG_ERR_CONNECT_FAIL
;
4045 cdata
->cmd_sock
= sock
;
4047 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
4048 if (socket
== NULL
) {
4051 PERROR("close register consumer");
4053 cdata
->cmd_sock
= -1;
4054 ret
= LTTNG_ERR_FATAL
;
4058 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
4059 if (socket
->lock
== NULL
) {
4060 PERROR("zmalloc pthread mutex");
4061 ret
= LTTNG_ERR_FATAL
;
4064 pthread_mutex_init(socket
->lock
, NULL
);
4065 socket
->registered
= 1;
4068 consumer_add_socket(socket
, ksess
->consumer
);
4071 pthread_mutex_lock(&cdata
->pid_mutex
);
4073 pthread_mutex_unlock(&cdata
->pid_mutex
);
4078 /* TODO: Userspace tracing */
4079 ret
= LTTNG_ERR_UND
;
4087 consumer_destroy_socket(socket
);
4093 * Command LTTNG_LIST_DOMAINS processed by the client thread.
4095 ssize_t
cmd_list_domains(struct ltt_session
*session
,
4096 struct lttng_domain
**domains
)
4101 struct lttng_ht_iter iter
;
4103 if (session
->kernel_session
!= NULL
) {
4104 DBG3("Listing domains found kernel domain");
4108 if (session
->ust_session
!= NULL
) {
4109 DBG3("Listing domains found UST global domain");
4113 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
4115 if (agt
->being_used
) {
4126 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
4127 if (*domains
== NULL
) {
4128 ret
= LTTNG_ERR_FATAL
;
4132 if (session
->kernel_session
!= NULL
) {
4133 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
4135 /* Kernel session buffer type is always GLOBAL */
4136 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
4141 if (session
->ust_session
!= NULL
) {
4142 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
4143 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
4147 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
4149 if (agt
->being_used
) {
4150 (*domains
)[index
].type
= agt
->domain
;
4151 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
4161 /* Return negative value to differentiate return code */
4167 * Command LTTNG_LIST_CHANNELS processed by the client thread.
4169 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
4170 struct ltt_session
*session
, struct lttng_channel
**channels
)
4172 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
4175 case LTTNG_DOMAIN_KERNEL
:
4176 if (session
->kernel_session
!= NULL
) {
4177 nb_chan
= session
->kernel_session
->channel_count
;
4179 DBG3("Number of kernel channels %zd", nb_chan
);
4181 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
4185 case LTTNG_DOMAIN_UST
:
4186 if (session
->ust_session
!= NULL
) {
4188 nb_chan
= lttng_ht_get_count(
4189 session
->ust_session
->domain_global
.channels
);
4192 DBG3("Number of UST global channels %zd", nb_chan
);
4194 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
4199 ret
= -LTTNG_ERR_UND
;
4204 const size_t channel_size
= sizeof(struct lttng_channel
) +
4205 sizeof(struct lttng_channel_extended
);
4206 struct lttng_channel_extended
*channel_exts
;
4208 payload_size
= nb_chan
* channel_size
;
4209 *channels
= zmalloc(payload_size
);
4210 if (*channels
== NULL
) {
4211 ret
= -LTTNG_ERR_FATAL
;
4215 channel_exts
= ((void *) *channels
) +
4216 (nb_chan
* sizeof(struct lttng_channel
));
4217 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
4218 if (ret
!= LTTNG_OK
) {
4232 enum lttng_error_code
cmd_list_maps(enum lttng_domain_type domain
,
4233 struct ltt_session
*session
,
4234 struct lttng_map_list
**return_map_list
)
4236 enum lttng_error_code ret_code
;
4237 struct lttng_map_list
*map_list
= NULL
;
4239 map_list
= lttng_map_list_create();
4242 case LTTNG_DOMAIN_KERNEL
:
4243 if (session
->kernel_session
!= NULL
) {
4244 struct ltt_kernel_map
*kmap
;
4245 cds_list_for_each_entry(kmap
,
4246 &session
->kernel_session
->map_list
.head
, list
) {
4247 enum lttng_map_status map_status
;
4248 map_status
= lttng_map_list_add(map_list
, kmap
->map
);
4249 if (map_status
!= LTTNG_MAP_STATUS_OK
) {
4250 ERR("Error appending kernel map to list");
4251 ret_code
= LTTNG_ERR_FATAL
;
4258 case LTTNG_DOMAIN_UST
:
4260 struct ltt_ust_map
*umap
;
4261 struct lttng_ht_iter iter
;
4264 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.maps
->ht
,
4265 &iter
.iter
, umap
, node
.node
) {
4266 enum lttng_map_status map_status
;
4267 map_status
= lttng_map_list_add(map_list
, umap
->map
);
4268 if (map_status
!= LTTNG_MAP_STATUS_OK
) {
4269 ERR("Error appending UST map to list");
4270 ret_code
= LTTNG_ERR_FATAL
;
4278 ret_code
= LTTNG_ERR_UND
;
4282 *return_map_list
= map_list
;
4284 ret_code
= LTTNG_OK
;
4286 lttng_map_list_destroy(map_list
);
4291 * Command LTTNG_LIST_EVENTS processed by the client thread.
4293 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
4294 struct ltt_session
*session
, char *channel_name
,
4295 struct lttng_payload
*payload
)
4298 ssize_t nb_events
= 0;
4299 struct lttcomm_event_command_header cmd_header
= {};
4300 const size_t cmd_header_offset
= payload
->buffer
.size
;
4302 ret
= lttng_dynamic_buffer_append(
4303 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
4305 ret
= LTTNG_ERR_NOMEM
;
4310 case LTTNG_DOMAIN_KERNEL
:
4311 if (session
->kernel_session
!= NULL
) {
4312 nb_events
= list_lttng_kernel_events(channel_name
,
4313 session
->kernel_session
, payload
);
4316 case LTTNG_DOMAIN_UST
:
4318 if (session
->ust_session
!= NULL
) {
4319 nb_events
= list_lttng_ust_global_events(channel_name
,
4320 &session
->ust_session
->domain_global
,
4325 case LTTNG_DOMAIN_LOG4J
:
4326 case LTTNG_DOMAIN_JUL
:
4327 case LTTNG_DOMAIN_PYTHON
:
4328 if (session
->ust_session
) {
4329 struct lttng_ht_iter iter
;
4333 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
4334 &iter
.iter
, agt
, node
.node
) {
4335 if (agt
->domain
== domain
) {
4336 nb_events
= list_lttng_agent_events(
4345 ret
= LTTNG_ERR_UND
;
4349 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
4350 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
4355 /* Return negative value to differentiate return code */
4360 * Using the session list, filled a lttng_session array to send back to the
4361 * client for session listing.
4363 * The session list lock MUST be acquired before calling this function. Use
4364 * session_lock_list() and session_unlock_list().
4366 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
4367 size_t session_count
, uid_t uid
, gid_t gid
)
4371 struct ltt_session
*session
;
4372 struct ltt_session_list
*list
= session_get_list();
4373 struct lttng_session_extended
*extended
=
4374 (typeof(extended
)) (&sessions
[session_count
]);
4376 DBG("Getting all available session for UID %d GID %d",
4379 * Iterate over session list and append data after the control struct in
4382 cds_list_for_each_entry(session
, &list
->head
, list
) {
4383 if (!session_get(session
)) {
4387 * Only list the sessions the user can control.
4389 if (!session_access_ok(session
, uid
) ||
4390 session
->destroyed
) {
4391 session_put(session
);
4395 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4396 struct ltt_ust_session
*usess
= session
->ust_session
;
4398 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4399 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4400 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4401 ret
= build_network_session_path(sessions
[i
].path
,
4402 sizeof(sessions
[i
].path
), session
);
4404 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
4405 session
->consumer
->dst
.session_root_path
);
4408 PERROR("snprintf session path");
4409 session_put(session
);
4413 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
4414 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
4415 sessions
[i
].enabled
= session
->active
;
4416 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
4417 sessions
[i
].live_timer_interval
= session
->live_timer
;
4418 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
4419 extended
[i
].creation_time
.is_set
= 1;
4421 session_put(session
);
4426 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4427 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4429 int cmd_data_pending(struct ltt_session
*session
)
4432 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4433 struct ltt_ust_session
*usess
= session
->ust_session
;
4437 DBG("Data pending for session %s", session
->name
);
4439 /* Session MUST be stopped to ask for data availability. */
4440 if (session
->active
) {
4441 ret
= LTTNG_ERR_SESSION_STARTED
;
4445 * If stopped, just make sure we've started before else the above call
4446 * will always send that there is data pending.
4448 * The consumer assumes that when the data pending command is received,
4449 * the trace has been started before or else no output data is written
4450 * by the streams which is a condition for data pending. So, this is
4451 * *VERY* important that we don't ask the consumer before a start
4454 if (!session
->has_been_started
) {
4460 /* A rotation is still pending, we have to wait. */
4461 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4462 DBG("Rotate still pending for session %s", session
->name
);
4467 if (ksess
&& ksess
->consumer
) {
4468 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4470 /* Data is still being extracted for the kernel. */
4475 if (usess
&& usess
->consumer
) {
4476 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4478 /* Data is still being extracted for the kernel. */
4483 /* Data is ready to be read by a viewer */
4491 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4493 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4495 int cmd_snapshot_add_output(struct ltt_session
*session
,
4496 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4499 struct snapshot_output
*new_output
;
4504 DBG("Cmd snapshot add output for session %s", session
->name
);
4507 * Can't create an output if the session is not set in no-output mode.
4509 if (session
->output_traces
) {
4510 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4514 if (session
->has_non_mmap_channel
) {
4515 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4519 /* Only one output is allowed until we have the "tee" feature. */
4520 if (session
->snapshot
.nb_output
== 1) {
4521 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4525 new_output
= snapshot_output_alloc();
4527 ret
= LTTNG_ERR_NOMEM
;
4531 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4532 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4533 &session
->snapshot
);
4535 if (ret
== -ENOMEM
) {
4536 ret
= LTTNG_ERR_NOMEM
;
4538 ret
= LTTNG_ERR_INVALID
;
4544 snapshot_add_output(&session
->snapshot
, new_output
);
4546 *id
= new_output
->id
;
4553 snapshot_output_destroy(new_output
);
4559 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4561 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4563 int cmd_snapshot_del_output(struct ltt_session
*session
,
4564 const struct lttng_snapshot_output
*output
)
4567 struct snapshot_output
*sout
= NULL
;
4575 * Permission denied to create an output if the session is not
4576 * set in no output mode.
4578 if (session
->output_traces
) {
4579 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4584 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4586 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4587 } else if (*output
->name
!= '\0') {
4588 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4590 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4593 ret
= LTTNG_ERR_INVALID
;
4597 snapshot_delete_output(&session
->snapshot
, sout
);
4598 snapshot_output_destroy(sout
);
4607 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4609 * If no output is available, outputs is untouched and 0 is returned.
4611 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4613 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4614 struct lttng_snapshot_output
**outputs
)
4617 struct lttng_snapshot_output
*list
= NULL
;
4618 struct lttng_ht_iter iter
;
4619 struct snapshot_output
*output
;
4624 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4627 * Permission denied to create an output if the session is not
4628 * set in no output mode.
4630 if (session
->output_traces
) {
4631 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4635 if (session
->snapshot
.nb_output
== 0) {
4640 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4642 ret
= -LTTNG_ERR_NOMEM
;
4646 /* Copy list from session to the new list object. */
4648 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4649 output
, node
.node
) {
4650 assert(output
->consumer
);
4651 list
[idx
].id
= output
->id
;
4652 list
[idx
].max_size
= output
->max_size
;
4653 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4654 sizeof(list
[idx
].name
))) {
4655 ret
= -LTTNG_ERR_INVALID
;
4658 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4659 if (lttng_strncpy(list
[idx
].ctrl_url
,
4660 output
->consumer
->dst
.session_root_path
,
4661 sizeof(list
[idx
].ctrl_url
))) {
4662 ret
= -LTTNG_ERR_INVALID
;
4667 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4668 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4670 ret
= -LTTNG_ERR_NOMEM
;
4675 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4676 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4678 ret
= -LTTNG_ERR_NOMEM
;
4687 ret
= session
->snapshot
.nb_output
;
4696 * Check if we can regenerate the metadata for this session.
4697 * Only kernel, UST per-uid and non-live sessions are supported.
4699 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4702 int check_regenerate_metadata_support(struct ltt_session
*session
)
4708 if (session
->live_timer
!= 0) {
4709 ret
= LTTNG_ERR_LIVE_SESSION
;
4712 if (!session
->active
) {
4713 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4716 if (session
->ust_session
) {
4717 switch (session
->ust_session
->buffer_type
) {
4718 case LTTNG_BUFFER_PER_UID
:
4720 case LTTNG_BUFFER_PER_PID
:
4721 ret
= LTTNG_ERR_PER_PID_SESSION
;
4725 ret
= LTTNG_ERR_UNK
;
4729 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4730 session
->consumer
->relay_minor_version
< 8) {
4731 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4741 int clear_metadata_file(int fd
)
4746 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4747 if (lseek_ret
< 0) {
4753 ret
= ftruncate(fd
, 0);
4755 PERROR("ftruncate");
4764 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4767 struct buffer_reg_uid
*uid_reg
= NULL
;
4768 struct buffer_reg_session
*session_reg
= NULL
;
4771 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4772 struct ust_registry_session
*registry
;
4773 struct ust_registry_channel
*chan
;
4774 struct lttng_ht_iter iter_chan
;
4776 session_reg
= uid_reg
->registry
;
4777 registry
= session_reg
->reg
.ust
;
4779 pthread_mutex_lock(®istry
->lock
);
4780 registry
->metadata_len_sent
= 0;
4781 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4782 registry
->metadata_len
= 0;
4783 registry
->metadata_version
++;
4784 if (registry
->metadata_fd
> 0) {
4785 /* Clear the metadata file's content. */
4786 ret
= clear_metadata_file(registry
->metadata_fd
);
4788 pthread_mutex_unlock(®istry
->lock
);
4793 ret
= ust_metadata_session_statedump(registry
, NULL
,
4794 registry
->major
, registry
->minor
);
4796 pthread_mutex_unlock(®istry
->lock
);
4797 ERR("Failed to generate session metadata (err = %d)",
4801 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4803 struct ust_registry_event
*event
;
4804 struct lttng_ht_iter iter_event
;
4806 ret
= ust_metadata_channel_statedump(registry
, chan
);
4808 pthread_mutex_unlock(®istry
->lock
);
4809 ERR("Failed to generate channel metadata "
4813 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4815 ret
= ust_metadata_event_statedump(registry
,
4818 pthread_mutex_unlock(®istry
->lock
);
4819 ERR("Failed to generate event metadata "
4825 pthread_mutex_unlock(®istry
->lock
);
4834 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4836 * Ask the consumer to truncate the existing metadata file(s) and
4837 * then regenerate the metadata. Live and per-pid sessions are not
4838 * supported and return an error.
4840 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4842 int cmd_regenerate_metadata(struct ltt_session
*session
)
4848 ret
= check_regenerate_metadata_support(session
);
4853 if (session
->kernel_session
) {
4854 ret
= kernctl_session_regenerate_metadata(
4855 session
->kernel_session
->fd
);
4857 ERR("Failed to regenerate the kernel metadata");
4862 if (session
->ust_session
) {
4863 ret
= ust_regenerate_metadata(session
->ust_session
);
4865 ERR("Failed to regenerate the UST metadata");
4869 DBG("Cmd metadata regenerate for session %s", session
->name
);
4877 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4879 * Ask the tracer to regenerate a new statedump.
4881 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4883 int cmd_regenerate_statedump(struct ltt_session
*session
)
4889 if (!session
->active
) {
4890 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4894 if (session
->kernel_session
) {
4895 ret
= kernctl_session_regenerate_statedump(
4896 session
->kernel_session
->fd
);
4898 * Currently, the statedump in kernel can only fail if out
4902 if (ret
== -ENOMEM
) {
4903 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4905 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4907 ERR("Failed to regenerate the kernel statedump");
4912 if (session
->ust_session
) {
4913 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4915 * Currently, the statedump in UST always returns 0.
4918 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4919 ERR("Failed to regenerate the UST statedump");
4923 DBG("Cmd regenerate statedump for session %s", session
->name
);
4931 enum lttng_error_code
synchronize_tracer_notifier_register(
4932 struct notification_thread_handle
*notification_thread
,
4933 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4935 enum lttng_error_code ret_code
;
4936 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
4937 const char *trigger_name
;
4938 uid_t trigger_owner
;
4939 enum lttng_trigger_status trigger_status
;
4940 const enum lttng_domain_type trigger_domain
=
4941 lttng_trigger_get_underlying_domain_type_restriction(
4944 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4945 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4948 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
4950 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4951 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(unnamed)";
4953 session_lock_list();
4954 switch (trigger_domain
) {
4955 case LTTNG_DOMAIN_KERNEL
:
4957 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4958 if (ret_code
!= LTTNG_OK
) {
4959 enum lttng_error_code notif_thread_unregister_ret
;
4961 notif_thread_unregister_ret
=
4962 notification_thread_command_unregister_trigger(
4963 notification_thread
, trigger
);
4965 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4966 /* Return the original error code. */
4967 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4969 (int) trigger_owner
,
4975 case LTTNG_DOMAIN_UST
:
4976 ust_app_global_update_all_event_notifier_rules();
4978 case LTTNG_DOMAIN_NONE
:
4982 /* Agent domains. */
4983 struct agent
*agt
= agent_find_by_event_notifier_domain(
4986 agt
= agent_create(trigger_domain
);
4988 ret_code
= LTTNG_ERR_NOMEM
;
4989 goto end_unlock_session_list
;
4991 agent_add(agt
, trigger_agents_ht_by_domain
);
4994 ret_code
= trigger_agent_enable(trigger
, agt
);
4995 if (ret_code
!= LTTNG_OK
) {
4996 goto end_unlock_session_list
;
5003 ret_code
= LTTNG_OK
;
5004 end_unlock_session_list
:
5005 session_unlock_list();
5009 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
5010 struct lttng_trigger
*trigger
,
5011 struct notification_thread_handle
*notification_thread
,
5012 struct lttng_trigger
**return_trigger
)
5014 enum lttng_error_code ret_code
;
5015 const char *trigger_name
;
5016 const struct lttng_condition
*condition
;
5017 uid_t trigger_owner
;
5018 enum lttng_trigger_status trigger_status
;
5020 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
5021 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
5022 trigger_name
: "(unnamed)";
5024 trigger_status
= lttng_trigger_get_owner_uid(
5025 trigger
, &trigger_owner
);
5026 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
5028 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
5029 trigger_name
, (int) trigger_owner
,
5030 (int) lttng_credentials_get_uid(cmd_creds
));
5033 * Validate the trigger credentials against the command credentials.
5034 * Only the root user can register a trigger with non-matching
5037 if (!lttng_credentials_is_equal_uid(
5038 lttng_trigger_get_credentials(trigger
),
5040 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
5041 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
5042 trigger_name
, (int) trigger_owner
,
5043 (int) lttng_credentials_get_uid(cmd_creds
));
5044 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
5050 * The bytecode generation also serves as a validation step for the
5051 * bytecode expressions.
5053 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
5054 if (ret_code
!= LTTNG_OK
) {
5055 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
5056 trigger_name
, (int) trigger_owner
, ret_code
);
5061 * A reference to the trigger is acquired by the notification thread.
5062 * It is safe to return the same trigger to the caller since it the
5063 * other user holds a reference.
5065 * The trigger is modified during the execution of the
5066 * "register trigger" command. However, by the time the command returns,
5067 * it is safe to use without any locking as its properties are
5070 ret_code
= notification_thread_command_register_trigger(notification_thread
,
5072 if (ret_code
!= LTTNG_OK
) {
5073 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
5074 trigger_name
, (int) trigger_owner
, ret_code
);
5078 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
5079 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
5080 trigger_name
: "(unnamed)";
5083 * Synchronize tracers if the trigger adds an event notifier.
5085 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
5086 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
5087 trigger
, cmd_creds
);
5088 if (ret_code
!= LTTNG_OK
) {
5089 ERR("Error registering tracer notifier");
5094 condition
= lttng_trigger_get_const_condition(trigger
);
5096 /* TODO: Extract condition below to lttng_trigger internal function */
5097 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
) {
5098 session_lock_list();
5099 ret_code
= sync_all_tracer_executed_actions(trigger
,
5100 cmd_creds
, TRACER_EXECUTED_ACTION_STATE_REGISTER
);
5101 session_unlock_list();
5102 if (ret_code
!= LTTNG_OK
) {
5103 ERR("Error registering tracer executed actions");
5109 * Return an updated trigger to the client.
5111 * Since a modified version of the same trigger is returned, acquire a
5112 * reference to the trigger so the caller doesn't have to care if those
5113 * are distinct instances or not.
5115 if (ret_code
== LTTNG_OK
) {
5116 lttng_trigger_get(trigger
);
5117 *return_trigger
= trigger
;
5118 /* Ownership of trigger was transferred to caller. */
5126 enum lttng_error_code
synchronize_tracer_notifier_unregister(
5127 const struct lttng_trigger
*trigger
)
5129 enum lttng_error_code ret_code
;
5130 const struct lttng_condition
*condition
=
5131 lttng_trigger_get_const_condition(trigger
);
5132 const enum lttng_domain_type trigger_domain
=
5133 lttng_trigger_get_underlying_domain_type_restriction(
5137 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
5139 session_lock_list();
5140 switch (trigger_domain
) {
5141 case LTTNG_DOMAIN_KERNEL
:
5142 ret_code
= kernel_unregister_event_notifier(trigger
);
5144 case LTTNG_DOMAIN_UST
:
5145 ust_app_global_update_all_event_notifier_rules();
5147 case LTTNG_DOMAIN_NONE
:
5151 /* Agent domains. */
5152 struct agent
*agt
= agent_find_by_event_notifier_domain(
5155 agt
= agent_create(trigger_domain
);
5157 ret_code
= LTTNG_ERR_NOMEM
;
5158 goto end_unlock_session_list
;
5160 agent_add(agt
, trigger_agents_ht_by_domain
);
5163 ret_code
= trigger_agent_disable(trigger
, agt
);
5164 if (ret_code
!= LTTNG_OK
) {
5165 goto end_unlock_session_list
;
5172 ret_code
= LTTNG_OK
;
5174 end_unlock_session_list
:
5175 session_unlock_list();
5179 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
5180 const struct lttng_trigger
*trigger
,
5181 struct notification_thread_handle
*notification_thread
)
5183 enum lttng_error_code ret_code
;
5184 const char *trigger_name
;
5185 struct lttng_trigger
*real_trigger
;
5186 uid_t trigger_owner
;
5187 enum lttng_trigger_status trigger_status
;
5189 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
5190 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(unnamed)";
5191 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
5192 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
5194 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
5195 trigger_name
, (int) trigger_owner
,
5196 (int) lttng_credentials_get_uid(cmd_creds
));
5199 * Validate the trigger credentials against the command credentials.
5200 * Only the root user can unregister a trigger with non-matching
5203 if (!lttng_credentials_is_equal_uid(
5204 lttng_trigger_get_credentials(trigger
),
5206 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
5207 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
5208 trigger_name
, (int) trigger_owner
,
5209 (int) lttng_credentials_get_uid(cmd_creds
));
5210 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
5215 ret_code
= notification_thread_command_get_trigger(
5216 notification_thread
, trigger
, &real_trigger
);
5217 if (ret_code
!= LTTNG_OK
) {
5218 DBG("Failed to get the real trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
5219 trigger_name
, (int) trigger_owner
, ret_code
);
5223 ret_code
= notification_thread_command_unregister_trigger(
5224 notification_thread
, real_trigger
);
5225 if (ret_code
!= LTTNG_OK
) {
5226 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
5227 trigger_name
, (int) trigger_owner
, ret_code
);
5232 * Synchronize tracers if the trigger removes an event notifier.
5233 * Do this even if the trigger unregistration failed to at least stop
5234 * the tracers from producing notifications associated with this
5237 if (lttng_trigger_needs_tracer_notifier(real_trigger
)) {
5238 ret_code
= synchronize_tracer_notifier_unregister(real_trigger
);
5239 if (ret_code
!= LTTNG_OK
) {
5240 ERR("Error unregistering trigger to tracer.");
5246 session_lock_list();
5247 ret_code
= sync_all_tracer_executed_actions(real_trigger
,
5248 cmd_creds
, TRACER_EXECUTED_ACTION_STATE_UNREGISTER
);
5249 session_unlock_list();
5250 if (ret_code
!= LTTNG_OK
) {
5251 ERR("Error registering tracer executed actions");
5255 lttng_trigger_put(real_trigger
);
5260 int cmd_list_triggers(struct command_ctx
*cmd_ctx
,
5261 struct notification_thread_handle
*notification_thread
,
5262 struct lttng_triggers
**return_triggers
)
5265 enum lttng_error_code ret_code
;
5266 struct lttng_triggers
*triggers
= NULL
;
5268 /* Get the set of triggers from the notification thread. */
5269 ret_code
= notification_thread_command_list_triggers(
5270 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
5271 if (ret_code
!= LTTNG_OK
) {
5276 *return_triggers
= triggers
;
5280 lttng_triggers_destroy(triggers
);
5284 int cmd_list_map_values(const char *session_name
,
5285 const struct lttng_map
*map
,
5286 const struct lttng_map_query
*map_query
,
5287 struct lttng_map_content
**return_map_content
)
5289 enum lttng_error_code ret
;
5290 struct ltt_session
*session
;
5291 enum lttng_domain_type domain
;
5292 const char *map_name
;
5293 enum lttng_map_status map_status
;
5295 /* Returns a refcounted reference */
5296 session
= session_find_by_name(session_name
);
5298 DBG("Session '%s' not found", session_name
);
5299 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
5303 domain
= lttng_map_get_domain(map
);
5305 map_status
= lttng_map_get_name(map
, &map_name
);
5306 assert(map_status
== LTTNG_MAP_STATUS_OK
);
5308 if (domain
== LTTNG_DOMAIN_KERNEL
) {
5309 if (session
->kernel_session
) {
5310 struct ltt_kernel_map
*kmap
;
5312 kmap
= trace_kernel_get_map_by_name(map_name
,
5313 session
->kernel_session
);
5315 ret
= kernel_list_map_values(kmap
, map_query
,
5316 return_map_content
);
5317 if (ret
!= LTTNG_OK
) {
5318 ERR("Error listing kernel map '%s' values", map_name
);
5322 DBG("No kernel map '%s' in session '%s'", map_name
, session_name
);
5325 } else if (domain
== LTTNG_DOMAIN_UST
) {
5326 if (session
->ust_session
) {
5327 struct ltt_ust_map
*umap
;
5328 struct ltt_ust_session
*usess
= session
->ust_session
;
5330 umap
= trace_ust_find_map_by_name(
5331 usess
->domain_global
.maps
, map_name
);
5333 ret
= ust_app_map_list_values(usess
, umap
,
5334 map_query
, return_map_content
);
5336 ret
= LTTNG_ERR_MAP_VALUES_LIST_FAIL
;
5337 ERR("Error listing UST map '%s' values", map_name
);
5341 DBG("No UST map '%s' in session '%s'", map_name
, session_name
);
5349 session_put(session
);
5354 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5355 * snapshot output is *not* set with a remote destination.
5357 * Return LTTNG_OK on success or a LTTNG_ERR code.
5359 static enum lttng_error_code
set_relayd_for_snapshot(
5360 struct consumer_output
*output
,
5361 const struct ltt_session
*session
)
5363 enum lttng_error_code status
= LTTNG_OK
;
5364 struct lttng_ht_iter iter
;
5365 struct consumer_socket
*socket
;
5366 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5367 const char *base_path
;
5372 DBG2("Set relayd object from snapshot output");
5374 if (session
->current_trace_chunk
) {
5375 enum lttng_trace_chunk_status chunk_status
=
5376 lttng_trace_chunk_get_id(
5377 session
->current_trace_chunk
,
5378 ¤t_chunk_id
.value
);
5380 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5381 current_chunk_id
.is_set
= true;
5383 ERR("Failed to get current trace chunk id");
5384 status
= LTTNG_ERR_UNK
;
5389 /* Ignore if snapshot consumer output is not network. */
5390 if (output
->type
!= CONSUMER_DST_NET
) {
5395 * The snapshot record URI base path overrides the session
5398 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5399 base_path
= output
->dst
.net
.control
.subdir
;
5401 base_path
= session
->base_path
;
5405 * For each consumer socket, create and send the relayd object of the
5409 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5410 socket
, node
.node
) {
5411 pthread_mutex_lock(socket
->lock
);
5412 status
= send_consumer_relayd_sockets(0, session
->id
,
5414 session
->name
, session
->hostname
,
5416 session
->live_timer
,
5417 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5418 session
->creation_time
,
5419 session
->name_contains_creation_time
);
5420 pthread_mutex_unlock(socket
->lock
);
5421 if (status
!= LTTNG_OK
) {
5433 * Record a kernel snapshot.
5435 * Return LTTNG_OK on success or a LTTNG_ERR code.
5437 static enum lttng_error_code
record_kernel_snapshot(
5438 struct ltt_kernel_session
*ksess
,
5439 const struct consumer_output
*output
,
5440 const struct ltt_session
*session
,
5441 int wait
, uint64_t nb_packets_per_stream
)
5443 enum lttng_error_code status
;
5449 status
= kernel_snapshot_record(
5450 ksess
, output
, wait
, nb_packets_per_stream
);
5455 * Record a UST snapshot.
5457 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5459 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5460 const struct consumer_output
*output
,
5461 const struct ltt_session
*session
,
5462 int wait
, uint64_t nb_packets_per_stream
)
5464 enum lttng_error_code status
;
5470 status
= ust_app_snapshot_record(
5471 usess
, output
, wait
, nb_packets_per_stream
);
5476 uint64_t get_session_size_one_more_packet_per_stream(
5477 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5479 uint64_t tot_size
= 0;
5481 if (session
->kernel_session
) {
5482 struct ltt_kernel_channel
*chan
;
5483 const struct ltt_kernel_session
*ksess
=
5484 session
->kernel_session
;
5486 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5487 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5489 * Don't take channel into account if we
5490 * already grab all its packets.
5494 tot_size
+= chan
->channel
->attr
.subbuf_size
5495 * chan
->stream_count
;
5499 if (session
->ust_session
) {
5500 const struct ltt_ust_session
*usess
= session
->ust_session
;
5502 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5510 * Calculate the number of packets we can grab from each stream that
5511 * fits within the overall snapshot max size.
5513 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5514 * the number of packets per stream.
5516 * TODO: this approach is not perfect: we consider the worse case
5517 * (packet filling the sub-buffers) as an upper bound, but we could do
5518 * better if we do this calculation while we actually grab the packet
5519 * content: we would know how much padding we don't actually store into
5522 * This algorithm is currently bounded by the number of packets per
5525 * Since we call this algorithm before actually grabbing the data, it's
5526 * an approximation: for instance, applications could appear/disappear
5527 * in between this call and actually grabbing data.
5530 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5534 uint64_t cur_nb_packets
= 0;
5537 return 0; /* Infinite */
5540 size_left
= max_size
;
5542 uint64_t one_more_packet_tot_size
;
5544 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5545 session
, cur_nb_packets
);
5546 if (!one_more_packet_tot_size
) {
5547 /* We are already grabbing all packets. */
5550 size_left
-= one_more_packet_tot_size
;
5551 if (size_left
< 0) {
5556 if (!cur_nb_packets
&& size_left
!= max_size
) {
5557 /* Not enough room to grab one packet of each stream, error. */
5560 return cur_nb_packets
;
5564 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5565 const struct snapshot_output
*snapshot_output
, int wait
)
5567 int64_t nb_packets_per_stream
;
5568 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5570 enum lttng_error_code ret_code
= LTTNG_OK
;
5571 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5572 struct consumer_output
*original_ust_consumer_output
= NULL
;
5573 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5574 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5575 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5577 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5579 snapshot_output
->name
,
5580 snapshot_output
->datetime
,
5581 snapshot_output
->nb_snapshot
);
5582 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5583 ERR("Failed to format snapshot name");
5584 ret_code
= LTTNG_ERR_INVALID
;
5587 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5588 snapshot_output
->name
, session
->name
,
5589 snapshot_chunk_name
);
5590 if (!session
->kernel_session
&& !session
->ust_session
) {
5591 ERR("Failed to record snapshot as no channels exist");
5592 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5596 if (session
->kernel_session
) {
5597 original_kernel_consumer_output
=
5598 session
->kernel_session
->consumer
;
5599 snapshot_kernel_consumer_output
=
5600 consumer_copy_output(snapshot_output
->consumer
);
5601 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5602 snapshot_chunk_name
);
5603 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5604 original_kernel_consumer_output
);
5606 ERR("Failed to copy consumer sockets from snapshot output configuration");
5607 ret_code
= LTTNG_ERR_NOMEM
;
5610 ret_code
= set_relayd_for_snapshot(
5611 snapshot_kernel_consumer_output
, session
);
5612 if (ret_code
!= LTTNG_OK
) {
5613 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5616 session
->kernel_session
->consumer
=
5617 snapshot_kernel_consumer_output
;
5619 if (session
->ust_session
) {
5620 original_ust_consumer_output
= session
->ust_session
->consumer
;
5621 snapshot_ust_consumer_output
=
5622 consumer_copy_output(snapshot_output
->consumer
);
5623 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5624 snapshot_chunk_name
);
5625 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5626 original_ust_consumer_output
);
5628 ERR("Failed to copy consumer sockets from snapshot output configuration");
5629 ret_code
= LTTNG_ERR_NOMEM
;
5632 ret_code
= set_relayd_for_snapshot(
5633 snapshot_ust_consumer_output
, session
);
5634 if (ret_code
!= LTTNG_OK
) {
5635 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5638 session
->ust_session
->consumer
=
5639 snapshot_ust_consumer_output
;
5642 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5643 snapshot_kernel_consumer_output
?:
5644 snapshot_ust_consumer_output
,
5645 consumer_output_get_base_path(
5646 snapshot_output
->consumer
),
5647 snapshot_chunk_name
);
5648 if (!snapshot_trace_chunk
) {
5649 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5651 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5654 assert(!session
->current_trace_chunk
);
5655 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5656 lttng_trace_chunk_put(snapshot_trace_chunk
);
5657 snapshot_trace_chunk
= NULL
;
5659 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5661 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5665 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5666 snapshot_output
->max_size
);
5667 if (nb_packets_per_stream
< 0) {
5668 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5669 goto error_close_trace_chunk
;
5672 if (session
->kernel_session
) {
5673 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5674 snapshot_kernel_consumer_output
, session
,
5675 wait
, nb_packets_per_stream
);
5676 if (ret_code
!= LTTNG_OK
) {
5677 goto error_close_trace_chunk
;
5681 if (session
->ust_session
) {
5682 ret_code
= record_ust_snapshot(session
->ust_session
,
5683 snapshot_ust_consumer_output
, session
,
5684 wait
, nb_packets_per_stream
);
5685 if (ret_code
!= LTTNG_OK
) {
5686 goto error_close_trace_chunk
;
5690 error_close_trace_chunk
:
5691 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5692 ERR("Failed to release the current trace chunk of session \"%s\"",
5694 ret_code
= LTTNG_ERR_UNK
;
5697 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5698 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5700 * Don't goto end; make sure the chunk is closed for the session
5701 * to allow future snapshots.
5703 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5705 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5708 if (original_ust_consumer_output
) {
5709 session
->ust_session
->consumer
= original_ust_consumer_output
;
5711 if (original_kernel_consumer_output
) {
5712 session
->kernel_session
->consumer
=
5713 original_kernel_consumer_output
;
5715 consumer_output_put(snapshot_ust_consumer_output
);
5716 consumer_output_put(snapshot_kernel_consumer_output
);
5721 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5723 * The wait parameter is ignored so this call always wait for the snapshot to
5724 * complete before returning.
5726 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5728 int cmd_snapshot_record(struct ltt_session
*session
,
5729 const struct lttng_snapshot_output
*output
, int wait
)
5731 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5733 unsigned int snapshot_success
= 0;
5735 struct snapshot_output
*tmp_output
= NULL
;
5740 DBG("Cmd snapshot record for session %s", session
->name
);
5742 /* Get the datetime for the snapshot output directory. */
5743 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5746 cmd_ret
= LTTNG_ERR_INVALID
;
5751 * Permission denied to create an output if the session is not
5752 * set in no output mode.
5754 if (session
->output_traces
) {
5755 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5759 /* The session needs to be started at least once. */
5760 if (!session
->has_been_started
) {
5761 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5765 /* Use temporary output for the session. */
5766 if (*output
->ctrl_url
!= '\0') {
5767 tmp_output
= snapshot_output_alloc();
5769 cmd_ret
= LTTNG_ERR_NOMEM
;
5773 ret
= snapshot_output_init(session
, output
->max_size
,
5775 output
->ctrl_url
, output
->data_url
,
5779 if (ret
== -ENOMEM
) {
5780 cmd_ret
= LTTNG_ERR_NOMEM
;
5782 cmd_ret
= LTTNG_ERR_INVALID
;
5786 /* Use the global session count for the temporary snapshot. */
5787 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5789 /* Use the global datetime */
5790 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5791 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5792 if (cmd_ret
!= LTTNG_OK
) {
5795 snapshot_success
= 1;
5797 struct snapshot_output
*sout
;
5798 struct lttng_ht_iter iter
;
5801 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5802 &iter
.iter
, sout
, node
.node
) {
5803 struct snapshot_output output_copy
;
5806 * Make a local copy of the output and override output
5807 * parameters with those provided as part of the
5810 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5812 if (output
->max_size
!= (uint64_t) -1ULL) {
5813 output_copy
.max_size
= output
->max_size
;
5816 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5817 memcpy(output_copy
.datetime
, datetime
,
5820 /* Use temporary name. */
5821 if (*output
->name
!= '\0') {
5822 if (lttng_strncpy(output_copy
.name
,
5824 sizeof(output_copy
.name
))) {
5825 cmd_ret
= LTTNG_ERR_INVALID
;
5831 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5832 if (cmd_ret
!= LTTNG_OK
) {
5836 snapshot_success
= 1;
5841 if (snapshot_success
) {
5842 session
->snapshot
.nb_snapshot
++;
5844 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5849 snapshot_output_destroy(tmp_output
);
5855 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5857 int cmd_set_session_shm_path(struct ltt_session
*session
,
5858 const char *shm_path
)
5864 * Can only set shm path before session is started.
5866 if (session
->has_been_started
) {
5867 return LTTNG_ERR_SESSION_STARTED
;
5870 strncpy(session
->shm_path
, shm_path
,
5871 sizeof(session
->shm_path
));
5872 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5878 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5880 * Ask the consumer to rotate the session output directory.
5881 * The session lock must be held.
5883 * Returns LTTNG_OK on success or else a negative LTTng error code.
5885 int cmd_rotate_session(struct ltt_session
*session
,
5886 struct lttng_rotate_session_return
*rotate_return
,
5887 bool quiet_rotation
,
5888 enum lttng_trace_chunk_command_type command
)
5891 uint64_t ongoing_rotation_chunk_id
;
5892 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5893 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5894 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5895 enum lttng_trace_chunk_status chunk_status
;
5896 bool failed_to_rotate
= false;
5897 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5901 if (!session
->has_been_started
) {
5902 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5907 * Explicit rotation is not supported for live sessions.
5908 * However, live sessions can perform a quiet rotation on
5910 * Rotation is not supported for snapshot traces (no output).
5912 if ((!quiet_rotation
&& session
->live_timer
) ||
5913 !session
->output_traces
) {
5914 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5918 /* Unsupported feature in lttng-relayd before 2.11. */
5919 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5920 (session
->consumer
->relay_major_version
== 2 &&
5921 session
->consumer
->relay_minor_version
< 11)) {
5922 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5926 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5927 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5928 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5932 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5933 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5935 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5940 * After a stop, we only allow one rotation to occur, the other ones are
5941 * useless until a new start.
5943 if (session
->rotated_after_last_stop
) {
5944 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5946 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5951 * After a stop followed by a clear, disallow following rotations a they would
5952 * generate empty chunks.
5954 if (session
->cleared_after_last_stop
) {
5955 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5957 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5961 if (session
->active
) {
5962 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5964 if (!new_trace_chunk
) {
5965 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5971 * The current trace chunk becomes the chunk being archived.
5973 * After this point, "chunk_being_archived" must absolutely
5974 * be closed on the consumer(s), otherwise it will never be
5975 * cleaned-up, which will result in a leak.
5977 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5978 &chunk_being_archived
);
5980 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5984 if (session
->kernel_session
) {
5985 cmd_ret
= kernel_rotate_session(session
);
5986 if (cmd_ret
!= LTTNG_OK
) {
5987 failed_to_rotate
= true;
5988 rotation_fail_code
= cmd_ret
;
5991 if (session
->ust_session
) {
5992 cmd_ret
= ust_app_rotate_session(session
);
5993 if (cmd_ret
!= LTTNG_OK
) {
5994 failed_to_rotate
= true;
5995 rotation_fail_code
= cmd_ret
;
5999 if (!session
->active
) {
6000 session
->rotated_after_last_stop
= true;
6003 if (!chunk_being_archived
) {
6004 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
6006 if (failed_to_rotate
) {
6007 cmd_ret
= rotation_fail_code
;
6014 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
6015 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
6016 &ongoing_rotation_chunk_id
);
6017 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
6019 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
6020 command
, session
->last_chunk_path
);
6022 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
6026 if (failed_to_rotate
) {
6027 cmd_ret
= rotation_fail_code
;
6031 session
->quiet_rotation
= quiet_rotation
;
6032 ret
= timer_session_rotation_pending_check_start(session
,
6033 DEFAULT_ROTATE_PENDING_TIMER
);
6035 cmd_ret
= LTTNG_ERR_UNK
;
6039 if (rotate_return
) {
6040 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
6043 session
->chunk_being_archived
= chunk_being_archived
;
6044 chunk_being_archived
= NULL
;
6045 if (!quiet_rotation
) {
6046 ret
= notification_thread_command_session_rotation_ongoing(
6047 notification_thread_handle
,
6048 session
->name
, session
->uid
, session
->gid
,
6049 ongoing_rotation_chunk_id
);
6050 if (ret
!= LTTNG_OK
) {
6051 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
6057 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
6058 session
->name
, ongoing_rotation_chunk_id
);
6060 lttng_trace_chunk_put(new_trace_chunk
);
6061 lttng_trace_chunk_put(chunk_being_archived
);
6062 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
6065 if (session_reset_rotation_state(session
,
6066 LTTNG_ROTATION_STATE_ERROR
)) {
6067 ERR("Failed to reset rotation state of session \"%s\"",
6074 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
6076 * Check if the session has finished its rotation.
6078 * Return LTTNG_OK on success or else an LTTNG_ERR code.
6080 int cmd_rotate_get_info(struct ltt_session
*session
,
6081 struct lttng_rotation_get_info_return
*info_return
,
6082 uint64_t rotation_id
)
6084 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6085 enum lttng_rotation_state rotation_state
;
6087 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
6088 session
->most_recent_chunk_id
.value
);
6090 if (session
->chunk_being_archived
) {
6091 enum lttng_trace_chunk_status chunk_status
;
6094 chunk_status
= lttng_trace_chunk_get_id(
6095 session
->chunk_being_archived
,
6097 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
6099 rotation_state
= rotation_id
== chunk_id
?
6100 LTTNG_ROTATION_STATE_ONGOING
:
6101 LTTNG_ROTATION_STATE_EXPIRED
;
6103 if (session
->last_archived_chunk_id
.is_set
&&
6104 rotation_id
!= session
->last_archived_chunk_id
.value
) {
6105 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
6107 rotation_state
= session
->rotation_state
;
6111 switch (rotation_state
) {
6112 case LTTNG_ROTATION_STATE_NO_ROTATION
:
6113 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
6116 case LTTNG_ROTATION_STATE_EXPIRED
:
6117 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
6118 rotation_id
, session
->name
);
6120 case LTTNG_ROTATION_STATE_ONGOING
:
6121 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
6122 rotation_id
, session
->name
);
6124 case LTTNG_ROTATION_STATE_COMPLETED
:
6128 char *current_tracing_path_reply
;
6129 size_t current_tracing_path_reply_len
;
6131 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
6132 rotation_id
, session
->name
);
6134 switch (session_get_consumer_destination_type(session
)) {
6135 case CONSUMER_DST_LOCAL
:
6136 current_tracing_path_reply
=
6137 info_return
->location
.local
.absolute_path
;
6138 current_tracing_path_reply_len
=
6139 sizeof(info_return
->location
.local
.absolute_path
);
6140 info_return
->location_type
=
6141 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
6142 fmt_ret
= asprintf(&chunk_path
,
6143 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
6144 session_get_base_path(session
),
6145 session
->last_archived_chunk_name
);
6146 if (fmt_ret
== -1) {
6147 PERROR("Failed to format the path of the last archived trace chunk");
6148 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
6149 cmd_ret
= LTTNG_ERR_UNK
;
6153 case CONSUMER_DST_NET
:
6155 uint16_t ctrl_port
, data_port
;
6157 current_tracing_path_reply
=
6158 info_return
->location
.relay
.relative_path
;
6159 current_tracing_path_reply_len
=
6160 sizeof(info_return
->location
.relay
.relative_path
);
6161 /* Currently the only supported relay protocol. */
6162 info_return
->location
.relay
.protocol
=
6163 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
6165 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
6166 session_get_net_consumer_hostname(session
),
6167 sizeof(info_return
->location
.relay
.host
));
6169 ERR("Failed to copy host name to rotate_get_info reply");
6170 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
6171 cmd_ret
= LTTNG_ERR_SET_URL
;
6175 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
6176 info_return
->location
.relay
.ports
.control
= ctrl_port
;
6177 info_return
->location
.relay
.ports
.data
= data_port
;
6178 info_return
->location_type
=
6179 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
6180 chunk_path
= strdup(session
->last_chunk_path
);
6182 ERR("Failed to allocate the path of the last archived trace chunk");
6183 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
6184 cmd_ret
= LTTNG_ERR_UNK
;
6193 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
6194 chunk_path
, current_tracing_path_reply_len
);
6197 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
6198 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
6199 cmd_ret
= LTTNG_ERR_UNK
;
6205 case LTTNG_ROTATION_STATE_ERROR
:
6206 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
6207 rotation_id
, session
->name
);
6215 info_return
->status
= (int32_t) rotation_state
;
6220 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
6222 * Configure the automatic rotation parameters.
6223 * 'activate' to true means activate the rotation schedule type with 'new_value'.
6224 * 'activate' to false means deactivate the rotation schedule and validate that
6225 * 'new_value' has the same value as the currently active value.
6227 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
6229 int cmd_rotation_set_schedule(struct ltt_session
*session
,
6230 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
6232 struct notification_thread_handle
*notification_thread_handle
)
6235 uint64_t *parameter_value
;
6239 DBG("Cmd rotate set schedule session %s", session
->name
);
6241 if (session
->live_timer
|| !session
->output_traces
) {
6242 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
6243 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
6247 switch (schedule_type
) {
6248 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6249 parameter_value
= &session
->rotate_size
;
6251 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
6252 parameter_value
= &session
->rotate_timer_period
;
6253 if (new_value
>= UINT_MAX
) {
6254 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
6255 new_value
, UINT_MAX
);
6256 ret
= LTTNG_ERR_INVALID
;
6261 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
6262 ret
= LTTNG_ERR_INVALID
;
6266 /* Improper use of the API. */
6267 if (new_value
== -1ULL) {
6268 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
6269 ret
= LTTNG_ERR_INVALID
;
6274 * As indicated in struct ltt_session's comments, a value of == 0 means
6275 * this schedule rotation type is not in use.
6277 * Reject the command if we were asked to activate a schedule that was
6280 if (activate
&& *parameter_value
!= 0) {
6281 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
6282 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
6287 * Reject the command if we were asked to deactivate a schedule that was
6290 if (!activate
&& *parameter_value
== 0) {
6291 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
6292 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
6297 * Reject the command if we were asked to deactivate a schedule that
6300 if (!activate
&& *parameter_value
!= new_value
) {
6301 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
6302 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
6306 *parameter_value
= activate
? new_value
: 0;
6308 switch (schedule_type
) {
6309 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
6310 if (activate
&& session
->active
) {
6312 * Only start the timer if the session is active,
6313 * otherwise it will be started when the session starts.
6315 ret
= timer_session_rotation_schedule_timer_start(
6316 session
, new_value
);
6318 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
6319 ret
= LTTNG_ERR_UNK
;
6323 ret
= timer_session_rotation_schedule_timer_stop(
6326 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
6327 ret
= LTTNG_ERR_UNK
;
6332 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6334 ret
= subscribe_session_consumed_size_rotation(session
,
6335 new_value
, notification_thread_handle
);
6337 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6338 ret
= LTTNG_ERR_UNK
;
6342 ret
= unsubscribe_session_consumed_size_rotation(session
,
6343 notification_thread_handle
);
6345 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6346 ret
= LTTNG_ERR_UNK
;
6353 /* Would have been caught before. */
6365 /* Wait for a given path to be removed before continuing. */
6366 static enum lttng_error_code
wait_on_path(void *path_data
)
6368 const char *shm_path
= path_data
;
6370 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6376 ret
= stat(shm_path
, &st
);
6378 if (errno
!= ENOENT
) {
6379 PERROR("stat() returned an error while checking for the existence of the shm path");
6381 DBG("shm path no longer exists, completing the destruction of session");
6385 if (!S_ISDIR(st
.st_mode
)) {
6386 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6391 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6397 * Returns a pointer to a handler to run on completion of a command.
6398 * Returns NULL if no handler has to be run for the last command executed.
6400 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6402 struct cmd_completion_handler
*handler
= current_completion_handler
;
6404 current_completion_handler
= NULL
;
6409 * Init command subsystem.
6414 * Set network sequence index to 1 for streams to match a relayd
6415 * socket on the consumer side.
6417 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6418 relayd_net_seq_idx
= 1;
6419 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6421 DBG("Command subsystem initialized");