2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/relayd/relayd.h>
29 #include <common/utils.h>
30 #include <common/compat/string.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
32 #include <common/dynamic-buffer.h>
33 #include <common/buffer-view.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/condition/condition.h>
36 #include <lttng/action/action.h>
37 #include <lttng/channel.h>
38 #include <lttng/channel-internal.h>
39 #include <common/string-utils/string-utils.h>
44 #include "health-sessiond.h"
46 #include "kernel-consumer.h"
47 #include "lttng-sessiond.h"
51 #include "buffer-registry.h"
52 #include "notification-thread.h"
53 #include "notification-thread-commands.h"
55 #include "rotation-thread.h"
56 #include "sessiond-timer.h"
61 * Used to keep a unique index for each relayd socket created where this value
62 * is associated with streams on the consumer so it can match the right relayd
63 * to send to. It must be accessed with the relayd_net_seq_idx_lock
66 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
67 static uint64_t relayd_net_seq_idx
;
69 static int validate_ust_event_name(const char *);
70 static int cmd_enable_event_internal(struct ltt_session
*session
,
71 struct lttng_domain
*domain
,
72 char *channel_name
, struct lttng_event
*event
,
73 char *filter_expression
,
74 struct lttng_filter_bytecode
*filter
,
75 struct lttng_event_exclusion
*exclusion
,
79 * Create a session path used by list_lttng_sessions for the case that the
80 * session consumer is on the network.
82 static int build_network_session_path(char *dst
, size_t size
,
83 struct ltt_session
*session
)
85 int ret
, kdata_port
, udata_port
;
86 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
87 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
92 memset(tmp_urls
, 0, sizeof(tmp_urls
));
93 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
95 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
97 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
98 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
99 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
102 if (session
->ust_session
&& session
->ust_session
->consumer
) {
103 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
104 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
107 if (uuri
== NULL
&& kuri
== NULL
) {
108 uri
= &session
->consumer
->dst
.net
.control
;
109 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
110 } else if (kuri
&& uuri
) {
111 ret
= uri_compare(kuri
, uuri
);
115 /* Build uuri URL string */
116 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
123 } else if (kuri
&& uuri
== NULL
) {
125 } else if (uuri
&& kuri
== NULL
) {
129 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
135 * Do we have a UST url set. If yes, this means we have both kernel and UST
138 if (*tmp_uurl
!= '\0') {
139 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
140 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
143 if (kuri
|| (!kuri
&& !uuri
)) {
146 /* No kernel URI, use the UST port. */
149 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
157 * Get run-time attributes if the session has been started (discarded events,
160 static int get_kernel_runtime_stats(struct ltt_session
*session
,
161 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
162 uint64_t *lost_packets
)
166 if (!session
->has_been_started
) {
168 *discarded_events
= 0;
173 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
174 session
->kernel_session
->consumer
,
180 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
181 session
->kernel_session
->consumer
,
192 * Get run-time attributes if the session has been started (discarded events,
195 static int get_ust_runtime_stats(struct ltt_session
*session
,
196 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
197 uint64_t *lost_packets
)
200 struct ltt_ust_session
*usess
;
202 if (!discarded_events
|| !lost_packets
) {
207 usess
= session
->ust_session
;
208 assert(discarded_events
);
209 assert(lost_packets
);
211 if (!usess
|| !session
->has_been_started
) {
212 *discarded_events
= 0;
218 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
219 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
220 &usess
->buffer_reg_uid_list
,
221 usess
->consumer
, uchan
->id
,
222 uchan
->attr
.overwrite
,
225 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
226 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
227 uchan
, usess
->consumer
,
228 uchan
->attr
.overwrite
,
234 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
235 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
237 ERR("Unsupported buffer type");
248 * Fill lttng_channel array of all channels.
250 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
251 struct ltt_session
*session
, struct lttng_channel
*channels
,
252 struct lttng_channel_extended
*chan_exts
)
255 struct ltt_kernel_channel
*kchan
;
257 DBG("Listing channels for session %s", session
->name
);
260 case LTTNG_DOMAIN_KERNEL
:
261 /* Kernel channels */
262 if (session
->kernel_session
!= NULL
) {
263 cds_list_for_each_entry(kchan
,
264 &session
->kernel_session
->channel_list
.head
, list
) {
265 uint64_t discarded_events
, lost_packets
;
266 struct lttng_channel_extended
*extended
;
268 extended
= (struct lttng_channel_extended
*)
269 kchan
->channel
->attr
.extended
.ptr
;
271 ret
= get_kernel_runtime_stats(session
, kchan
,
272 &discarded_events
, &lost_packets
);
276 /* Copy lttng_channel struct to array */
277 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
278 channels
[i
].enabled
= kchan
->enabled
;
279 chan_exts
[i
].discarded_events
=
281 chan_exts
[i
].lost_packets
= lost_packets
;
282 chan_exts
[i
].monitor_timer_interval
=
283 extended
->monitor_timer_interval
;
284 chan_exts
[i
].blocking_timeout
= 0;
289 case LTTNG_DOMAIN_UST
:
291 struct lttng_ht_iter iter
;
292 struct ltt_ust_channel
*uchan
;
295 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
296 &iter
.iter
, uchan
, node
.node
) {
297 uint64_t discarded_events
= 0, lost_packets
= 0;
299 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
300 LTTNG_SYMBOL_NAME_LEN
)) {
303 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
304 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
305 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
306 channels
[i
].attr
.switch_timer_interval
=
307 uchan
->attr
.switch_timer_interval
;
308 channels
[i
].attr
.read_timer_interval
=
309 uchan
->attr
.read_timer_interval
;
310 channels
[i
].enabled
= uchan
->enabled
;
311 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
312 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
315 * Map enum lttng_ust_output to enum lttng_event_output.
317 switch (uchan
->attr
.output
) {
319 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
323 * LTTNG_UST_MMAP is the only supported UST
330 chan_exts
[i
].monitor_timer_interval
=
331 uchan
->monitor_timer_interval
;
332 chan_exts
[i
].blocking_timeout
=
333 uchan
->attr
.u
.s
.blocking_timeout
;
335 ret
= get_ust_runtime_stats(session
, uchan
,
336 &discarded_events
, &lost_packets
);
340 chan_exts
[i
].discarded_events
= discarded_events
;
341 chan_exts
[i
].lost_packets
= lost_packets
;
353 return -LTTNG_ERR_FATAL
;
359 static void increment_extended_len(const char *filter_expression
,
360 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
362 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
364 if (filter_expression
) {
365 *extended_len
+= strlen(filter_expression
) + 1;
369 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
373 static void append_extended_info(const char *filter_expression
,
374 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
376 struct lttcomm_event_extended_header extended_header
;
377 size_t filter_len
= 0;
378 size_t nb_exclusions
= 0;
380 if (filter_expression
) {
381 filter_len
= strlen(filter_expression
) + 1;
385 nb_exclusions
= exclusion
->count
;
388 /* Set header fields */
389 extended_header
.filter_len
= filter_len
;
390 extended_header
.nb_exclusions
= nb_exclusions
;
393 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
394 *extended_at
+= sizeof(extended_header
);
396 /* Copy filter string */
397 if (filter_expression
) {
398 memcpy(*extended_at
, filter_expression
, filter_len
);
399 *extended_at
+= filter_len
;
402 /* Copy exclusion names */
404 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
406 memcpy(*extended_at
, &exclusion
->names
, len
);
412 * Create a list of agent domain events.
414 * Return number of events in list on success or else a negative value.
416 static int list_lttng_agent_events(struct agent
*agt
,
417 struct lttng_event
**events
, size_t *total_size
)
420 unsigned int nb_event
= 0;
421 struct agent_event
*event
;
422 struct lttng_event
*tmp_events
;
423 struct lttng_ht_iter iter
;
424 size_t extended_len
= 0;
430 DBG3("Listing agent events");
433 nb_event
= lttng_ht_get_count(agt
->events
);
441 /* Compute required extended infos size */
442 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
445 * This is only valid because the commands which add events are
446 * processed in the same thread as the listing.
449 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
450 increment_extended_len(event
->filter_expression
, NULL
,
455 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
456 tmp_events
= zmalloc(*total_size
);
458 PERROR("zmalloc agent events session");
459 ret
= -LTTNG_ERR_FATAL
;
463 extended_at
= ((uint8_t *) tmp_events
) +
464 nb_event
* sizeof(struct lttng_event
);
467 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
468 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
469 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
470 tmp_events
[i
].enabled
= event
->enabled
;
471 tmp_events
[i
].loglevel
= event
->loglevel_value
;
472 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
475 /* Append extended info */
476 append_extended_info(event
->filter_expression
, NULL
,
481 *events
= tmp_events
;
485 assert(nb_event
== i
);
490 * Create a list of ust global domain events.
492 static int list_lttng_ust_global_events(char *channel_name
,
493 struct ltt_ust_domain_global
*ust_global
,
494 struct lttng_event
**events
, size_t *total_size
)
497 unsigned int nb_event
= 0;
498 struct lttng_ht_iter iter
;
499 struct lttng_ht_node_str
*node
;
500 struct ltt_ust_channel
*uchan
;
501 struct ltt_ust_event
*uevent
;
502 struct lttng_event
*tmp
;
503 size_t extended_len
= 0;
506 DBG("Listing UST global events for channel %s", channel_name
);
510 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
511 node
= lttng_ht_iter_get_node_str(&iter
);
513 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
517 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
519 nb_event
= lttng_ht_get_count(uchan
->events
);
526 DBG3("Listing UST global %d events", nb_event
);
528 /* Compute required extended infos size */
529 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
530 if (uevent
->internal
) {
535 increment_extended_len(uevent
->filter_expression
,
536 uevent
->exclusion
, &extended_len
);
539 /* All events are internal, skip. */
545 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
546 tmp
= zmalloc(*total_size
);
548 ret
= -LTTNG_ERR_FATAL
;
552 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
554 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
555 if (uevent
->internal
) {
556 /* This event should remain hidden from clients */
559 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
560 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
561 tmp
[i
].enabled
= uevent
->enabled
;
563 switch (uevent
->attr
.instrumentation
) {
564 case LTTNG_UST_TRACEPOINT
:
565 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
567 case LTTNG_UST_PROBE
:
568 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
570 case LTTNG_UST_FUNCTION
:
571 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
575 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
576 switch (uevent
->attr
.loglevel_type
) {
577 case LTTNG_UST_LOGLEVEL_ALL
:
578 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
580 case LTTNG_UST_LOGLEVEL_RANGE
:
581 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
583 case LTTNG_UST_LOGLEVEL_SINGLE
:
584 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
587 if (uevent
->filter
) {
590 if (uevent
->exclusion
) {
591 tmp
[i
].exclusion
= 1;
595 /* Append extended info */
596 append_extended_info(uevent
->filter_expression
,
597 uevent
->exclusion
, &extended_at
);
608 * Fill lttng_event array of all kernel events in the channel.
610 static int list_lttng_kernel_events(char *channel_name
,
611 struct ltt_kernel_session
*kernel_session
,
612 struct lttng_event
**events
, size_t *total_size
)
615 unsigned int nb_event
;
616 struct ltt_kernel_event
*event
;
617 struct ltt_kernel_channel
*kchan
;
618 size_t extended_len
= 0;
621 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
623 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
627 nb_event
= kchan
->event_count
;
629 DBG("Listing events for channel %s", kchan
->channel
->name
);
637 /* Compute required extended infos size */
638 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
639 increment_extended_len(event
->filter_expression
, NULL
,
643 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
644 *events
= zmalloc(*total_size
);
645 if (*events
== NULL
) {
646 ret
= LTTNG_ERR_FATAL
;
650 extended_at
= ((void *) *events
) +
651 nb_event
* sizeof(struct lttng_event
);
653 /* Kernel channels */
654 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
655 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
656 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
657 (*events
)[i
].enabled
= event
->enabled
;
658 (*events
)[i
].filter
=
659 (unsigned char) !!event
->filter_expression
;
661 switch (event
->event
->instrumentation
) {
662 case LTTNG_KERNEL_TRACEPOINT
:
663 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
665 case LTTNG_KERNEL_KRETPROBE
:
666 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
667 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
668 sizeof(struct lttng_kernel_kprobe
));
670 case LTTNG_KERNEL_KPROBE
:
671 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
672 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
673 sizeof(struct lttng_kernel_kprobe
));
675 case LTTNG_KERNEL_FUNCTION
:
676 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
677 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
678 sizeof(struct lttng_kernel_function
));
680 case LTTNG_KERNEL_NOOP
:
681 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
683 case LTTNG_KERNEL_SYSCALL
:
684 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
686 case LTTNG_KERNEL_ALL
:
692 /* Append extended info */
693 append_extended_info(event
->filter_expression
, NULL
,
701 /* Negate the error code to differentiate the size from an error */
706 * Add URI so the consumer output object. Set the correct path depending on the
707 * domain adding the default trace directory.
709 static int add_uri_to_consumer(struct consumer_output
*consumer
,
710 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
711 const char *session_name
)
714 const char *default_trace_dir
;
718 if (consumer
== NULL
) {
719 DBG("No consumer detected. Don't add URI. Stopping.");
720 ret
= LTTNG_ERR_NO_CONSUMER
;
725 case LTTNG_DOMAIN_KERNEL
:
726 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
728 case LTTNG_DOMAIN_UST
:
729 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
733 * This case is possible is we try to add the URI to the global tracing
734 * session consumer object which in this case there is no subdir.
736 default_trace_dir
= "";
739 switch (uri
->dtype
) {
742 DBG2("Setting network URI to consumer");
744 if (consumer
->type
== CONSUMER_DST_NET
) {
745 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
746 consumer
->dst
.net
.control_isset
) ||
747 (uri
->stype
== LTTNG_STREAM_DATA
&&
748 consumer
->dst
.net
.data_isset
)) {
749 ret
= LTTNG_ERR_URL_EXIST
;
753 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
756 consumer
->type
= CONSUMER_DST_NET
;
758 /* Set URI into consumer output object */
759 ret
= consumer_set_network_uri(consumer
, uri
);
763 } else if (ret
== 1) {
765 * URI was the same in the consumer so we do not append the subdir
766 * again so to not duplicate output dir.
772 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
773 ret
= consumer_set_subdir(consumer
, session_name
);
775 ret
= LTTNG_ERR_FATAL
;
780 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
781 /* On a new subdir, reappend the default trace dir. */
782 strncat(consumer
->subdir
, default_trace_dir
,
783 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
784 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
789 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
790 memset(consumer
->dst
.session_root_path
, 0,
791 sizeof(consumer
->dst
.session_root_path
));
792 /* Explicit length checks for strcpy and strcat. */
793 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
794 >= sizeof(consumer
->dst
.session_root_path
)) {
795 ret
= LTTNG_ERR_FATAL
;
798 strcpy(consumer
->dst
.session_root_path
, uri
->dst
.path
);
799 /* Append default trace dir */
800 strcat(consumer
->dst
.session_root_path
, default_trace_dir
);
801 /* Flag consumer as local. */
802 consumer
->type
= CONSUMER_DST_LOCAL
;
813 * Init tracing by creating trace directory and sending fds kernel consumer.
815 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
818 struct lttng_ht_iter iter
;
819 struct consumer_socket
*socket
;
825 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
826 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
828 pthread_mutex_lock(socket
->lock
);
829 ret
= kernel_consumer_send_session(socket
, session
);
830 pthread_mutex_unlock(socket
->lock
);
832 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
844 * Create a socket to the relayd using the URI.
846 * On success, the relayd_sock pointer is set to the created socket.
847 * Else, it's stays untouched and a lttcomm error code is returned.
849 static int create_connect_relayd(struct lttng_uri
*uri
,
850 struct lttcomm_relayd_sock
**relayd_sock
,
851 struct consumer_output
*consumer
)
854 struct lttcomm_relayd_sock
*rsock
;
856 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
857 RELAYD_VERSION_COMM_MINOR
);
859 ret
= LTTNG_ERR_FATAL
;
864 * Connect to relayd so we can proceed with a session creation. This call
865 * can possibly block for an arbitrary amount of time to set the health
866 * state to be in poll execution.
869 ret
= relayd_connect(rsock
);
872 ERR("Unable to reach lttng-relayd");
873 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
877 /* Create socket for control stream. */
878 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
879 DBG3("Creating relayd stream socket from URI");
881 /* Check relayd version */
882 ret
= relayd_version_check(rsock
);
884 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
887 consumer
->relay_major_version
= rsock
->major
;
888 consumer
->relay_minor_version
= rsock
->minor
;
889 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
890 DBG3("Creating relayd data socket from URI");
892 /* Command is not valid */
893 ERR("Relayd invalid stream type: %d", uri
->stype
);
894 ret
= LTTNG_ERR_INVALID
;
898 *relayd_sock
= rsock
;
903 /* The returned value is not useful since we are on an error path. */
904 (void) relayd_close(rsock
);
912 * Connect to the relayd using URI and send the socket to the right consumer.
914 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
915 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
916 struct consumer_output
*consumer
,
917 struct consumer_socket
*consumer_sock
,
918 char *session_name
, char *hostname
, int session_live_timer
)
921 struct lttcomm_relayd_sock
*rsock
= NULL
;
923 /* Connect to relayd and make version check if uri is the control. */
924 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
925 if (ret
!= LTTNG_OK
) {
930 /* Set the network sequence index if not set. */
931 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
932 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
934 * Increment net_seq_idx because we are about to transfer the
935 * new relayd socket to the consumer.
936 * Assign unique key so the consumer can match streams.
938 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
939 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
942 /* Send relayd socket to consumer. */
943 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
944 relayd_uri
->stype
, session_id
,
945 session_name
, hostname
, session_live_timer
);
947 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
951 /* Flag that the corresponding socket was sent. */
952 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
953 consumer_sock
->control_sock_sent
= 1;
954 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
955 consumer_sock
->data_sock_sent
= 1;
961 * Close socket which was dup on the consumer side. The session daemon does
962 * NOT keep track of the relayd socket(s) once transfer to the consumer.
966 (void) relayd_close(rsock
);
970 if (ret
!= LTTNG_OK
) {
972 * The consumer output for this session should not be used anymore
973 * since the relayd connection failed thus making any tracing or/and
974 * streaming not usable.
976 consumer
->enabled
= 0;
982 * Send both relayd sockets to a specific consumer and domain. This is a
983 * helper function to facilitate sending the information to the consumer for a
986 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
987 unsigned int session_id
, struct consumer_output
*consumer
,
988 struct consumer_socket
*sock
, char *session_name
,
989 char *hostname
, int session_live_timer
)
996 /* Sending control relayd socket. */
997 if (!sock
->control_sock_sent
) {
998 ret
= send_consumer_relayd_socket(domain
, session_id
,
999 &consumer
->dst
.net
.control
, consumer
, sock
,
1000 session_name
, hostname
, session_live_timer
);
1001 if (ret
!= LTTNG_OK
) {
1006 /* Sending data relayd socket. */
1007 if (!sock
->data_sock_sent
) {
1008 ret
= send_consumer_relayd_socket(domain
, session_id
,
1009 &consumer
->dst
.net
.data
, consumer
, sock
,
1010 session_name
, hostname
, session_live_timer
);
1011 if (ret
!= LTTNG_OK
) {
1021 * Setup relayd connections for a tracing session. First creates the socket to
1022 * the relayd and send them to the right domain consumer. Consumer type MUST be
1025 int cmd_setup_relayd(struct ltt_session
*session
)
1028 struct ltt_ust_session
*usess
;
1029 struct ltt_kernel_session
*ksess
;
1030 struct consumer_socket
*socket
;
1031 struct lttng_ht_iter iter
;
1035 usess
= session
->ust_session
;
1036 ksess
= session
->kernel_session
;
1038 DBG("Setting relayd for session %s", session
->name
);
1042 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1043 && usess
->consumer
->enabled
) {
1044 /* For each consumer socket, send relayd sockets */
1045 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1046 socket
, node
.node
) {
1047 pthread_mutex_lock(socket
->lock
);
1048 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1049 usess
->consumer
, socket
,
1050 session
->name
, session
->hostname
,
1051 session
->live_timer
);
1052 pthread_mutex_unlock(socket
->lock
);
1053 if (ret
!= LTTNG_OK
) {
1056 /* Session is now ready for network streaming. */
1057 session
->net_handle
= 1;
1059 session
->consumer
->relay_major_version
=
1060 usess
->consumer
->relay_major_version
;
1061 session
->consumer
->relay_minor_version
=
1062 usess
->consumer
->relay_minor_version
;
1065 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1066 && ksess
->consumer
->enabled
) {
1067 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1068 socket
, node
.node
) {
1069 pthread_mutex_lock(socket
->lock
);
1070 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1071 ksess
->consumer
, socket
,
1072 session
->name
, session
->hostname
,
1073 session
->live_timer
);
1074 pthread_mutex_unlock(socket
->lock
);
1075 if (ret
!= LTTNG_OK
) {
1078 /* Session is now ready for network streaming. */
1079 session
->net_handle
= 1;
1081 session
->consumer
->relay_major_version
=
1082 ksess
->consumer
->relay_major_version
;
1083 session
->consumer
->relay_minor_version
=
1084 ksess
->consumer
->relay_minor_version
;
1093 * Start a kernel session by opening all necessary streams.
1095 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1098 struct ltt_kernel_channel
*kchan
;
1100 /* Open kernel metadata */
1101 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1102 ret
= kernel_open_metadata(ksess
);
1104 ret
= LTTNG_ERR_KERN_META_FAIL
;
1109 /* Open kernel metadata stream */
1110 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1111 ret
= kernel_open_metadata_stream(ksess
);
1113 ERR("Kernel create metadata stream failed");
1114 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1119 /* For each channel */
1120 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1121 if (kchan
->stream_count
== 0) {
1122 ret
= kernel_open_channel_stream(kchan
);
1124 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1127 /* Update the stream global counter */
1128 ksess
->stream_count_global
+= ret
;
1132 /* Setup kernel consumer socket and send fds to it */
1133 ret
= init_kernel_tracing(ksess
);
1135 ret
= LTTNG_ERR_KERN_START_FAIL
;
1139 /* This start the kernel tracing */
1140 ret
= kernel_start_session(ksess
);
1142 ret
= LTTNG_ERR_KERN_START_FAIL
;
1146 /* Quiescent wait after starting trace */
1147 kernel_wait_quiescent(kernel_tracer_fd
);
1158 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1160 int cmd_disable_channel(struct ltt_session
*session
,
1161 enum lttng_domain_type domain
, char *channel_name
)
1164 struct ltt_ust_session
*usess
;
1166 usess
= session
->ust_session
;
1171 case LTTNG_DOMAIN_KERNEL
:
1173 ret
= channel_kernel_disable(session
->kernel_session
,
1175 if (ret
!= LTTNG_OK
) {
1179 kernel_wait_quiescent(kernel_tracer_fd
);
1182 case LTTNG_DOMAIN_UST
:
1184 struct ltt_ust_channel
*uchan
;
1185 struct lttng_ht
*chan_ht
;
1187 chan_ht
= usess
->domain_global
.channels
;
1189 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1190 if (uchan
== NULL
) {
1191 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1195 ret
= channel_ust_disable(usess
, uchan
);
1196 if (ret
!= LTTNG_OK
) {
1202 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1214 * Command LTTNG_TRACK_PID processed by the client thread.
1216 * Called with session lock held.
1218 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1226 case LTTNG_DOMAIN_KERNEL
:
1228 struct ltt_kernel_session
*ksess
;
1230 ksess
= session
->kernel_session
;
1232 ret
= kernel_track_pid(ksess
, pid
);
1233 if (ret
!= LTTNG_OK
) {
1237 kernel_wait_quiescent(kernel_tracer_fd
);
1240 case LTTNG_DOMAIN_UST
:
1242 struct ltt_ust_session
*usess
;
1244 usess
= session
->ust_session
;
1246 ret
= trace_ust_track_pid(usess
, pid
);
1247 if (ret
!= LTTNG_OK
) {
1253 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1265 * Command LTTNG_UNTRACK_PID processed by the client thread.
1267 * Called with session lock held.
1269 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1277 case LTTNG_DOMAIN_KERNEL
:
1279 struct ltt_kernel_session
*ksess
;
1281 ksess
= session
->kernel_session
;
1283 ret
= kernel_untrack_pid(ksess
, pid
);
1284 if (ret
!= LTTNG_OK
) {
1288 kernel_wait_quiescent(kernel_tracer_fd
);
1291 case LTTNG_DOMAIN_UST
:
1293 struct ltt_ust_session
*usess
;
1295 usess
= session
->ust_session
;
1297 ret
= trace_ust_untrack_pid(usess
, pid
);
1298 if (ret
!= LTTNG_OK
) {
1304 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1316 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1318 * The wpipe arguments is used as a notifier for the kernel thread.
1320 int cmd_enable_channel(struct ltt_session
*session
,
1321 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1324 struct ltt_ust_session
*usess
= session
->ust_session
;
1325 struct lttng_ht
*chan_ht
;
1332 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1334 /* Validate channel name */
1335 if (attr
->name
[0] == '.' ||
1336 memchr(attr
->name
, '/', len
) != NULL
) {
1337 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1341 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1346 * Don't try to enable a channel if the session has been started at
1347 * some point in time before. The tracer does not allow it.
1349 if (session
->has_been_started
) {
1350 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1355 * If the session is a live session, remove the switch timer, the
1356 * live timer does the same thing but sends also synchronisation
1357 * beacons for inactive streams.
1359 if (session
->live_timer
> 0) {
1360 attr
->attr
.live_timer_interval
= session
->live_timer
;
1361 attr
->attr
.switch_timer_interval
= 0;
1364 /* Check for feature support */
1365 switch (domain
->type
) {
1366 case LTTNG_DOMAIN_KERNEL
:
1368 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1369 /* Sampling position of buffer is not supported */
1370 WARN("Kernel tracer does not support buffer monitoring. "
1371 "Setting the monitor interval timer to 0 "
1372 "(disabled) for channel '%s' of session '%s'",
1373 attr
-> name
, session
->name
);
1374 lttng_channel_set_monitor_timer_interval(attr
, 0);
1378 case LTTNG_DOMAIN_UST
:
1379 case LTTNG_DOMAIN_JUL
:
1380 case LTTNG_DOMAIN_LOG4J
:
1381 case LTTNG_DOMAIN_PYTHON
:
1384 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1388 switch (domain
->type
) {
1389 case LTTNG_DOMAIN_KERNEL
:
1391 struct ltt_kernel_channel
*kchan
;
1393 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1394 session
->kernel_session
);
1395 if (kchan
== NULL
) {
1396 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1397 if (attr
->name
[0] != '\0') {
1398 session
->kernel_session
->has_non_default_channel
= 1;
1401 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1404 if (ret
!= LTTNG_OK
) {
1408 kernel_wait_quiescent(kernel_tracer_fd
);
1411 case LTTNG_DOMAIN_UST
:
1412 case LTTNG_DOMAIN_JUL
:
1413 case LTTNG_DOMAIN_LOG4J
:
1414 case LTTNG_DOMAIN_PYTHON
:
1416 struct ltt_ust_channel
*uchan
;
1421 * Current agent implementation limitations force us to allow
1422 * only one channel at once in "agent" subdomains. Each
1423 * subdomain has a default channel name which must be strictly
1426 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1427 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1428 LTTNG_SYMBOL_NAME_LEN
)) {
1429 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1432 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1433 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1434 LTTNG_SYMBOL_NAME_LEN
)) {
1435 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1438 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1439 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1440 LTTNG_SYMBOL_NAME_LEN
)) {
1441 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1446 chan_ht
= usess
->domain_global
.channels
;
1448 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1449 if (uchan
== NULL
) {
1450 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1451 if (attr
->name
[0] != '\0') {
1452 usess
->has_non_default_channel
= 1;
1455 ret
= channel_ust_enable(usess
, uchan
);
1460 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1471 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1473 int cmd_disable_event(struct ltt_session
*session
,
1474 enum lttng_domain_type domain
, char *channel_name
,
1475 struct lttng_event
*event
)
1480 DBG("Disable event command for event \'%s\'", event
->name
);
1482 event_name
= event
->name
;
1484 /* Error out on unhandled search criteria */
1485 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1486 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1487 ret
= LTTNG_ERR_UNK
;
1494 case LTTNG_DOMAIN_KERNEL
:
1496 struct ltt_kernel_channel
*kchan
;
1497 struct ltt_kernel_session
*ksess
;
1499 ksess
= session
->kernel_session
;
1502 * If a non-default channel has been created in the
1503 * session, explicitely require that -c chan_name needs
1506 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1507 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1511 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1512 if (kchan
== NULL
) {
1513 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1517 switch (event
->type
) {
1518 case LTTNG_EVENT_ALL
:
1519 case LTTNG_EVENT_TRACEPOINT
:
1520 case LTTNG_EVENT_SYSCALL
:
1521 case LTTNG_EVENT_PROBE
:
1522 case LTTNG_EVENT_FUNCTION
:
1523 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1524 if (event_name
[0] == '\0') {
1525 ret
= event_kernel_disable_event(kchan
,
1528 ret
= event_kernel_disable_event(kchan
,
1529 event_name
, event
->type
);
1531 if (ret
!= LTTNG_OK
) {
1536 ret
= LTTNG_ERR_UNK
;
1540 kernel_wait_quiescent(kernel_tracer_fd
);
1543 case LTTNG_DOMAIN_UST
:
1545 struct ltt_ust_channel
*uchan
;
1546 struct ltt_ust_session
*usess
;
1548 usess
= session
->ust_session
;
1550 if (validate_ust_event_name(event_name
)) {
1551 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1556 * If a non-default channel has been created in the
1557 * session, explicitly require that -c chan_name needs
1560 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1561 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1565 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1567 if (uchan
== NULL
) {
1568 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1572 switch (event
->type
) {
1573 case LTTNG_EVENT_ALL
:
1575 * An empty event name means that everything
1576 * should be disabled.
1578 if (event
->name
[0] == '\0') {
1579 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1581 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1584 if (ret
!= LTTNG_OK
) {
1589 ret
= LTTNG_ERR_UNK
;
1593 DBG3("Disable UST event %s in channel %s completed", event_name
,
1597 case LTTNG_DOMAIN_LOG4J
:
1598 case LTTNG_DOMAIN_JUL
:
1599 case LTTNG_DOMAIN_PYTHON
:
1602 struct ltt_ust_session
*usess
= session
->ust_session
;
1606 switch (event
->type
) {
1607 case LTTNG_EVENT_ALL
:
1610 ret
= LTTNG_ERR_UNK
;
1614 agt
= trace_ust_find_agent(usess
, domain
);
1616 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1620 * An empty event name means that everything
1621 * should be disabled.
1623 if (event
->name
[0] == '\0') {
1624 ret
= event_agent_disable_all(usess
, agt
);
1626 ret
= event_agent_disable(usess
, agt
, event_name
);
1628 if (ret
!= LTTNG_OK
) {
1635 ret
= LTTNG_ERR_UND
;
1648 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1650 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1651 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1653 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1654 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1656 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1657 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1658 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1662 case LTTNG_DOMAIN_KERNEL
:
1663 assert(session
->kernel_session
);
1665 if (session
->kernel_session
->channel_count
== 0) {
1666 /* Create default channel */
1667 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1668 if (ret
!= LTTNG_OK
) {
1671 chan_kern_created
= 1;
1673 /* Add kernel context to kernel tracer */
1674 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1675 if (ret
!= LTTNG_OK
) {
1679 case LTTNG_DOMAIN_JUL
:
1680 case LTTNG_DOMAIN_LOG4J
:
1683 * Validate channel name.
1684 * If no channel name is given and the domain is JUL or LOG4J,
1685 * set it to the appropriate domain-specific channel name. If
1686 * a name is provided but does not match the expexted channel
1687 * name, return an error.
1689 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1690 strcmp(channel_name
,
1691 DEFAULT_JUL_CHANNEL_NAME
)) {
1692 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1694 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1695 strcmp(channel_name
,
1696 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1697 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1700 /* break is _not_ missing here. */
1702 case LTTNG_DOMAIN_UST
:
1704 struct ltt_ust_session
*usess
= session
->ust_session
;
1705 unsigned int chan_count
;
1709 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1710 if (chan_count
== 0) {
1711 struct lttng_channel
*attr
;
1712 /* Create default channel */
1713 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1715 ret
= LTTNG_ERR_FATAL
;
1719 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1720 if (ret
!= LTTNG_OK
) {
1724 channel_attr_destroy(attr
);
1725 chan_ust_created
= 1;
1728 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1729 free(app_ctx_provider_name
);
1731 app_ctx_name
= NULL
;
1732 app_ctx_provider_name
= NULL
;
1733 if (ret
!= LTTNG_OK
) {
1739 ret
= LTTNG_ERR_UND
;
1747 if (chan_kern_created
) {
1748 struct ltt_kernel_channel
*kchan
=
1749 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1750 session
->kernel_session
);
1751 /* Created previously, this should NOT fail. */
1753 kernel_destroy_channel(kchan
);
1756 if (chan_ust_created
) {
1757 struct ltt_ust_channel
*uchan
=
1758 trace_ust_find_channel_by_name(
1759 session
->ust_session
->domain_global
.channels
,
1760 DEFAULT_CHANNEL_NAME
);
1761 /* Created previously, this should NOT fail. */
1763 /* Remove from the channel list of the session. */
1764 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1766 trace_ust_destroy_channel(uchan
);
1769 free(app_ctx_provider_name
);
1774 static inline bool name_starts_with(const char *name
, const char *prefix
)
1776 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1778 return !strncmp(name
, prefix
, max_cmp_len
);
1781 /* Perform userspace-specific event name validation */
1782 static int validate_ust_event_name(const char *name
)
1792 * Check name against all internal UST event component namespaces used
1795 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1796 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1797 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1806 * Internal version of cmd_enable_event() with a supplemental
1807 * "internal_event" flag which is used to enable internal events which should
1808 * be hidden from clients. Such events are used in the agent implementation to
1809 * enable the events through which all "agent" events are funeled.
1811 static int _cmd_enable_event(struct ltt_session
*session
,
1812 struct lttng_domain
*domain
,
1813 char *channel_name
, struct lttng_event
*event
,
1814 char *filter_expression
,
1815 struct lttng_filter_bytecode
*filter
,
1816 struct lttng_event_exclusion
*exclusion
,
1817 int wpipe
, bool internal_event
)
1819 int ret
= 0, channel_created
= 0;
1820 struct lttng_channel
*attr
= NULL
;
1824 assert(channel_name
);
1826 /* If we have a filter, we must have its filter expression */
1827 assert(!(!!filter_expression
^ !!filter
));
1829 /* Normalize event name as a globbing pattern */
1830 strutils_normalize_star_glob_pattern(event
->name
);
1832 /* Normalize exclusion names as globbing patterns */
1836 for (i
= 0; i
< exclusion
->count
; i
++) {
1837 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1839 strutils_normalize_star_glob_pattern(name
);
1843 DBG("Enable event command for event \'%s\'", event
->name
);
1847 switch (domain
->type
) {
1848 case LTTNG_DOMAIN_KERNEL
:
1850 struct ltt_kernel_channel
*kchan
;
1853 * If a non-default channel has been created in the
1854 * session, explicitely require that -c chan_name needs
1857 if (session
->kernel_session
->has_non_default_channel
1858 && channel_name
[0] == '\0') {
1859 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1863 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1864 session
->kernel_session
);
1865 if (kchan
== NULL
) {
1866 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1867 LTTNG_BUFFER_GLOBAL
);
1869 ret
= LTTNG_ERR_FATAL
;
1872 if (lttng_strncpy(attr
->name
, channel_name
,
1873 sizeof(attr
->name
))) {
1874 ret
= LTTNG_ERR_INVALID
;
1878 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1879 if (ret
!= LTTNG_OK
) {
1882 channel_created
= 1;
1885 /* Get the newly created kernel channel pointer */
1886 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1887 session
->kernel_session
);
1888 if (kchan
== NULL
) {
1889 /* This sould not happen... */
1890 ret
= LTTNG_ERR_FATAL
;
1894 switch (event
->type
) {
1895 case LTTNG_EVENT_ALL
:
1897 char *filter_expression_a
= NULL
;
1898 struct lttng_filter_bytecode
*filter_a
= NULL
;
1901 * We need to duplicate filter_expression and filter,
1902 * because ownership is passed to first enable
1905 if (filter_expression
) {
1906 filter_expression_a
= strdup(filter_expression
);
1907 if (!filter_expression_a
) {
1908 ret
= LTTNG_ERR_FATAL
;
1913 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1915 free(filter_expression_a
);
1916 ret
= LTTNG_ERR_FATAL
;
1919 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1921 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1922 ret
= event_kernel_enable_event(kchan
, event
,
1923 filter_expression
, filter
);
1924 /* We have passed ownership */
1925 filter_expression
= NULL
;
1927 if (ret
!= LTTNG_OK
) {
1928 if (channel_created
) {
1929 /* Let's not leak a useless channel. */
1930 kernel_destroy_channel(kchan
);
1932 free(filter_expression_a
);
1936 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1937 ret
= event_kernel_enable_event(kchan
, event
,
1938 filter_expression_a
, filter_a
);
1939 /* We have passed ownership */
1940 filter_expression_a
= NULL
;
1942 if (ret
!= LTTNG_OK
) {
1947 case LTTNG_EVENT_PROBE
:
1948 case LTTNG_EVENT_FUNCTION
:
1949 case LTTNG_EVENT_FUNCTION_ENTRY
:
1950 case LTTNG_EVENT_TRACEPOINT
:
1951 ret
= event_kernel_enable_event(kchan
, event
,
1952 filter_expression
, filter
);
1953 /* We have passed ownership */
1954 filter_expression
= NULL
;
1956 if (ret
!= LTTNG_OK
) {
1957 if (channel_created
) {
1958 /* Let's not leak a useless channel. */
1959 kernel_destroy_channel(kchan
);
1964 case LTTNG_EVENT_SYSCALL
:
1965 ret
= event_kernel_enable_event(kchan
, event
,
1966 filter_expression
, filter
);
1967 /* We have passed ownership */
1968 filter_expression
= NULL
;
1970 if (ret
!= LTTNG_OK
) {
1975 ret
= LTTNG_ERR_UNK
;
1979 kernel_wait_quiescent(kernel_tracer_fd
);
1982 case LTTNG_DOMAIN_UST
:
1984 struct ltt_ust_channel
*uchan
;
1985 struct ltt_ust_session
*usess
= session
->ust_session
;
1990 * If a non-default channel has been created in the
1991 * session, explicitely require that -c chan_name needs
1994 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1995 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1999 /* Get channel from global UST domain */
2000 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2002 if (uchan
== NULL
) {
2003 /* Create default channel */
2004 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2005 usess
->buffer_type
);
2007 ret
= LTTNG_ERR_FATAL
;
2010 if (lttng_strncpy(attr
->name
, channel_name
,
2011 sizeof(attr
->name
))) {
2012 ret
= LTTNG_ERR_INVALID
;
2016 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2017 if (ret
!= LTTNG_OK
) {
2021 /* Get the newly created channel reference back */
2022 uchan
= trace_ust_find_channel_by_name(
2023 usess
->domain_global
.channels
, channel_name
);
2027 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2029 * Don't allow users to add UST events to channels which
2030 * are assigned to a userspace subdomain (JUL, Log4J,
2033 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2037 if (!internal_event
) {
2039 * Ensure the event name is not reserved for internal
2042 ret
= validate_ust_event_name(event
->name
);
2044 WARN("Userspace event name %s failed validation.",
2046 event
->name
: "NULL");
2047 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2052 /* At this point, the session and channel exist on the tracer */
2053 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2054 filter_expression
, filter
, exclusion
,
2056 /* We have passed ownership */
2057 filter_expression
= NULL
;
2060 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2061 goto already_enabled
;
2062 } else if (ret
!= LTTNG_OK
) {
2067 case LTTNG_DOMAIN_LOG4J
:
2068 case LTTNG_DOMAIN_JUL
:
2069 case LTTNG_DOMAIN_PYTHON
:
2071 const char *default_event_name
, *default_chan_name
;
2073 struct lttng_event uevent
;
2074 struct lttng_domain tmp_dom
;
2075 struct ltt_ust_session
*usess
= session
->ust_session
;
2079 agt
= trace_ust_find_agent(usess
, domain
->type
);
2081 agt
= agent_create(domain
->type
);
2083 ret
= LTTNG_ERR_NOMEM
;
2086 agent_add(agt
, usess
->agents
);
2089 /* Create the default tracepoint. */
2090 memset(&uevent
, 0, sizeof(uevent
));
2091 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2092 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2093 default_event_name
= event_get_default_agent_ust_name(
2095 if (!default_event_name
) {
2096 ret
= LTTNG_ERR_FATAL
;
2099 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2100 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2103 * The domain type is changed because we are about to enable the
2104 * default channel and event for the JUL domain that are hardcoded.
2105 * This happens in the UST domain.
2107 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2108 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2110 switch (domain
->type
) {
2111 case LTTNG_DOMAIN_LOG4J
:
2112 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2114 case LTTNG_DOMAIN_JUL
:
2115 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2117 case LTTNG_DOMAIN_PYTHON
:
2118 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2121 /* The switch/case we are in makes this impossible */
2126 char *filter_expression_copy
= NULL
;
2127 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2130 const size_t filter_size
= sizeof(
2131 struct lttng_filter_bytecode
)
2134 filter_copy
= zmalloc(filter_size
);
2136 ret
= LTTNG_ERR_NOMEM
;
2139 memcpy(filter_copy
, filter
, filter_size
);
2141 filter_expression_copy
=
2142 strdup(filter_expression
);
2143 if (!filter_expression
) {
2144 ret
= LTTNG_ERR_NOMEM
;
2147 if (!filter_expression_copy
|| !filter_copy
) {
2148 free(filter_expression_copy
);
2154 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2155 (char *) default_chan_name
,
2156 &uevent
, filter_expression_copy
,
2157 filter_copy
, NULL
, wpipe
);
2160 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2161 goto already_enabled
;
2162 } else if (ret
!= LTTNG_OK
) {
2166 /* The wild card * means that everything should be enabled. */
2167 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2168 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2171 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2175 filter_expression
= NULL
;
2176 if (ret
!= LTTNG_OK
) {
2183 ret
= LTTNG_ERR_UND
;
2191 free(filter_expression
);
2194 channel_attr_destroy(attr
);
2200 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2201 * We own filter, exclusion, and filter_expression.
2203 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2204 char *channel_name
, struct lttng_event
*event
,
2205 char *filter_expression
,
2206 struct lttng_filter_bytecode
*filter
,
2207 struct lttng_event_exclusion
*exclusion
,
2210 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2211 filter_expression
, filter
, exclusion
, wpipe
, false);
2215 * Enable an event which is internal to LTTng. An internal should
2216 * never be made visible to clients and are immune to checks such as
2219 static int cmd_enable_event_internal(struct ltt_session
*session
,
2220 struct lttng_domain
*domain
,
2221 char *channel_name
, struct lttng_event
*event
,
2222 char *filter_expression
,
2223 struct lttng_filter_bytecode
*filter
,
2224 struct lttng_event_exclusion
*exclusion
,
2227 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2228 filter_expression
, filter
, exclusion
, wpipe
, true);
2232 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2234 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2235 struct lttng_event
**events
)
2238 ssize_t nb_events
= 0;
2241 case LTTNG_DOMAIN_KERNEL
:
2242 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2243 if (nb_events
< 0) {
2244 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2248 case LTTNG_DOMAIN_UST
:
2249 nb_events
= ust_app_list_events(events
);
2250 if (nb_events
< 0) {
2251 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2255 case LTTNG_DOMAIN_LOG4J
:
2256 case LTTNG_DOMAIN_JUL
:
2257 case LTTNG_DOMAIN_PYTHON
:
2258 nb_events
= agent_list_events(events
, domain
);
2259 if (nb_events
< 0) {
2260 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2265 ret
= LTTNG_ERR_UND
;
2272 /* Return negative value to differentiate return code */
2277 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2279 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2280 struct lttng_event_field
**fields
)
2283 ssize_t nb_fields
= 0;
2286 case LTTNG_DOMAIN_UST
:
2287 nb_fields
= ust_app_list_event_fields(fields
);
2288 if (nb_fields
< 0) {
2289 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2293 case LTTNG_DOMAIN_KERNEL
:
2294 default: /* fall-through */
2295 ret
= LTTNG_ERR_UND
;
2302 /* Return negative value to differentiate return code */
2306 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2308 return syscall_table_list(events
);
2312 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2314 * Called with session lock held.
2316 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2317 enum lttng_domain_type domain
, int32_t **pids
)
2320 ssize_t nr_pids
= 0;
2323 case LTTNG_DOMAIN_KERNEL
:
2325 struct ltt_kernel_session
*ksess
;
2327 ksess
= session
->kernel_session
;
2328 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2330 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2335 case LTTNG_DOMAIN_UST
:
2337 struct ltt_ust_session
*usess
;
2339 usess
= session
->ust_session
;
2340 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2342 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2347 case LTTNG_DOMAIN_LOG4J
:
2348 case LTTNG_DOMAIN_JUL
:
2349 case LTTNG_DOMAIN_PYTHON
:
2351 ret
= LTTNG_ERR_UND
;
2358 /* Return negative value to differentiate return code */
2363 * Command LTTNG_START_TRACE processed by the client thread.
2365 * Called with session mutex held.
2367 int cmd_start_trace(struct ltt_session
*session
)
2370 unsigned long nb_chan
= 0;
2371 struct ltt_kernel_session
*ksession
;
2372 struct ltt_ust_session
*usess
;
2376 /* Ease our life a bit ;) */
2377 ksession
= session
->kernel_session
;
2378 usess
= session
->ust_session
;
2380 /* Is the session already started? */
2381 if (session
->active
) {
2382 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2387 * Starting a session without channel is useless since after that it's not
2388 * possible to enable channel thus inform the client.
2390 if (usess
&& usess
->domain_global
.channels
) {
2391 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2394 nb_chan
+= ksession
->channel_count
;
2397 ret
= LTTNG_ERR_NO_CHANNEL
;
2402 * Record the timestamp of the first time the session is started for
2403 * an eventual session rotation call.
2405 if (!session
->has_been_started
) {
2406 session
->current_chunk_start_ts
= time(NULL
);
2407 if (session
->current_chunk_start_ts
== (time_t) -1) {
2408 PERROR("Get start time");
2409 ret
= LTTNG_ERR_FATAL
;
2414 /* Kernel tracing */
2415 if (ksession
!= NULL
) {
2416 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2417 if (ret
!= LTTNG_OK
) {
2422 /* Flag session that trace should start automatically */
2425 * Even though the start trace might fail, flag this session active so
2426 * other application coming in are started by default.
2430 ret
= ust_app_start_trace_all(usess
);
2432 ret
= LTTNG_ERR_UST_START_FAIL
;
2437 /* Flag this after a successful start. */
2438 session
->has_been_started
= 1;
2439 session
->active
= 1;
2448 * Command LTTNG_STOP_TRACE processed by the client thread.
2450 int cmd_stop_trace(struct ltt_session
*session
)
2453 struct ltt_kernel_channel
*kchan
;
2454 struct ltt_kernel_session
*ksession
;
2455 struct ltt_ust_session
*usess
;
2460 ksession
= session
->kernel_session
;
2461 usess
= session
->ust_session
;
2463 /* Session is not active. Skip everythong and inform the client. */
2464 if (!session
->active
) {
2465 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2470 if (ksession
&& ksession
->active
) {
2471 DBG("Stop kernel tracing");
2473 ret
= kernel_stop_session(ksession
);
2475 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2479 kernel_wait_quiescent(kernel_tracer_fd
);
2481 /* Flush metadata after stopping (if exists) */
2482 if (ksession
->metadata_stream_fd
>= 0) {
2483 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2485 ERR("Kernel metadata flush failed");
2489 /* Flush all buffers after stopping */
2490 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2491 ret
= kernel_flush_buffer(kchan
);
2493 ERR("Kernel flush buffer error");
2497 ksession
->active
= 0;
2500 if (usess
&& usess
->active
) {
2502 * Even though the stop trace might fail, flag this session inactive so
2503 * other application coming in are not started by default.
2507 ret
= ust_app_stop_trace_all(usess
);
2509 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2514 session
->session_last_stop_ts
= time(NULL
);
2516 /* Flag inactive after a successful stop. */
2517 session
->active
= 0;
2525 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2527 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2528 struct lttng_uri
*uris
)
2531 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2532 struct ltt_ust_session
*usess
= session
->ust_session
;
2538 /* Can't set consumer URI if the session is active. */
2539 if (session
->active
) {
2540 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2544 /* Set the "global" consumer URIs */
2545 for (i
= 0; i
< nb_uri
; i
++) {
2546 ret
= add_uri_to_consumer(session
->consumer
,
2547 &uris
[i
], 0, session
->name
);
2548 if (ret
!= LTTNG_OK
) {
2553 /* Set UST session URIs */
2554 if (session
->ust_session
) {
2555 for (i
= 0; i
< nb_uri
; i
++) {
2556 ret
= add_uri_to_consumer(
2557 session
->ust_session
->consumer
,
2558 &uris
[i
], LTTNG_DOMAIN_UST
,
2560 if (ret
!= LTTNG_OK
) {
2566 /* Set kernel session URIs */
2567 if (session
->kernel_session
) {
2568 for (i
= 0; i
< nb_uri
; i
++) {
2569 ret
= add_uri_to_consumer(
2570 session
->kernel_session
->consumer
,
2571 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2573 if (ret
!= LTTNG_OK
) {
2580 * Make sure to set the session in output mode after we set URI since a
2581 * session can be created without URL (thus flagged in no output mode).
2583 session
->output_traces
= 1;
2585 ksess
->output_traces
= 1;
2589 usess
->output_traces
= 1;
2600 * Command LTTNG_CREATE_SESSION processed by the client thread.
2602 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2603 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2606 struct ltt_session
*session
;
2612 * Verify if the session already exist
2614 * XXX: There is no need for the session lock list here since the caller
2615 * (process_client_msg) is holding it. We might want to change that so a
2616 * single command does not lock the entire session list.
2618 session
= session_find_by_name(name
);
2619 if (session
!= NULL
) {
2620 ret
= LTTNG_ERR_EXIST_SESS
;
2624 /* Create tracing session in the registry */
2625 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2626 LTTNG_SOCK_GET_GID_CRED(creds
));
2627 if (ret
!= LTTNG_OK
) {
2632 * Get the newly created session pointer back
2634 * XXX: There is no need for the session lock list here since the caller
2635 * (process_client_msg) is holding it. We might want to change that so a
2636 * single command does not lock the entire session list.
2638 session
= session_find_by_name(name
);
2641 session
->live_timer
= live_timer
;
2642 /* Create default consumer output for the session not yet created. */
2643 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2644 if (session
->consumer
== NULL
) {
2645 ret
= LTTNG_ERR_FATAL
;
2646 goto consumer_error
;
2650 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2651 if (ret
!= LTTNG_OK
) {
2652 goto consumer_error
;
2654 session
->output_traces
= 1;
2656 session
->output_traces
= 0;
2657 DBG2("Session %s created with no output", session
->name
);
2660 session
->consumer
->enabled
= 1;
2665 session_destroy(session
);
2672 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2674 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2675 size_t nb_uri
, lttng_sock_cred
*creds
)
2678 struct ltt_session
*session
;
2679 struct snapshot_output
*new_output
= NULL
;
2685 * Create session in no output mode with URIs set to NULL. The uris we've
2686 * received are for a default snapshot output if one.
2688 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2689 if (ret
!= LTTNG_OK
) {
2693 /* Get the newly created session pointer back. This should NEVER fail. */
2694 session
= session_find_by_name(name
);
2697 /* Flag session for snapshot mode. */
2698 session
->snapshot_mode
= 1;
2700 /* Skip snapshot output creation if no URI is given. */
2705 new_output
= snapshot_output_alloc();
2707 ret
= LTTNG_ERR_NOMEM
;
2708 goto error_snapshot_alloc
;
2711 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2712 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2714 if (ret
== -ENOMEM
) {
2715 ret
= LTTNG_ERR_NOMEM
;
2717 ret
= LTTNG_ERR_INVALID
;
2719 goto error_snapshot
;
2723 snapshot_add_output(&session
->snapshot
, new_output
);
2730 snapshot_output_destroy(new_output
);
2731 error_snapshot_alloc
:
2732 session_destroy(session
);
2738 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2740 * Called with session lock held.
2742 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2745 struct ltt_ust_session
*usess
;
2746 struct ltt_kernel_session
*ksess
;
2751 usess
= session
->ust_session
;
2752 ksess
= session
->kernel_session
;
2754 if (session
->rotate_count
> 0) {
2755 session
->rotate_count
++;
2757 * The currently active tracing path is now the folder we
2760 snprintf(session
->rotation_chunk
.current_rotate_path
,
2762 session
->rotation_chunk
.active_tracing_path
);
2763 ret
= rename_complete_chunk(session
,
2764 session
->session_last_stop_ts
);
2766 ERR("Renaming session on destroy");
2770 if (session
->rotate_relay_pending_timer_enabled
) {
2771 sessiond_timer_rotate_pending_stop(session
);
2774 /* Clean kernel session teardown */
2775 kernel_destroy_session(ksess
);
2777 /* UST session teardown */
2779 /* Close any relayd session */
2780 consumer_output_send_destroy_relayd(usess
->consumer
);
2782 /* Destroy every UST application related to this session. */
2783 ret
= ust_app_destroy_trace_all(usess
);
2785 ERR("Error in ust_app_destroy_trace_all");
2788 /* Clean up the rest. */
2789 trace_ust_destroy_session(usess
);
2793 * Must notify the kernel thread here to update it's poll set in order to
2794 * remove the channel(s)' fd just destroyed.
2796 ret
= notify_thread_pipe(wpipe
);
2798 PERROR("write kernel poll pipe");
2801 ret
= session_destroy(session
);
2807 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2809 int cmd_register_consumer(struct ltt_session
*session
,
2810 enum lttng_domain_type domain
, const char *sock_path
,
2811 struct consumer_data
*cdata
)
2814 struct consumer_socket
*socket
= NULL
;
2821 case LTTNG_DOMAIN_KERNEL
:
2823 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2827 /* Can't register a consumer if there is already one */
2828 if (ksess
->consumer_fds_sent
!= 0) {
2829 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2833 sock
= lttcomm_connect_unix_sock(sock_path
);
2835 ret
= LTTNG_ERR_CONNECT_FAIL
;
2838 cdata
->cmd_sock
= sock
;
2840 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2841 if (socket
== NULL
) {
2844 PERROR("close register consumer");
2846 cdata
->cmd_sock
= -1;
2847 ret
= LTTNG_ERR_FATAL
;
2851 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2852 if (socket
->lock
== NULL
) {
2853 PERROR("zmalloc pthread mutex");
2854 ret
= LTTNG_ERR_FATAL
;
2857 pthread_mutex_init(socket
->lock
, NULL
);
2858 socket
->registered
= 1;
2861 consumer_add_socket(socket
, ksess
->consumer
);
2864 pthread_mutex_lock(&cdata
->pid_mutex
);
2866 pthread_mutex_unlock(&cdata
->pid_mutex
);
2871 /* TODO: Userspace tracing */
2872 ret
= LTTNG_ERR_UND
;
2880 consumer_destroy_socket(socket
);
2886 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2888 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2889 struct lttng_domain
**domains
)
2894 struct lttng_ht_iter iter
;
2896 if (session
->kernel_session
!= NULL
) {
2897 DBG3("Listing domains found kernel domain");
2901 if (session
->ust_session
!= NULL
) {
2902 DBG3("Listing domains found UST global domain");
2906 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2908 if (agt
->being_used
) {
2919 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2920 if (*domains
== NULL
) {
2921 ret
= LTTNG_ERR_FATAL
;
2925 if (session
->kernel_session
!= NULL
) {
2926 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2928 /* Kernel session buffer type is always GLOBAL */
2929 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2934 if (session
->ust_session
!= NULL
) {
2935 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2936 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2940 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2942 if (agt
->being_used
) {
2943 (*domains
)[index
].type
= agt
->domain
;
2944 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2954 /* Return negative value to differentiate return code */
2960 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2962 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2963 struct ltt_session
*session
, struct lttng_channel
**channels
)
2965 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2968 case LTTNG_DOMAIN_KERNEL
:
2969 if (session
->kernel_session
!= NULL
) {
2970 nb_chan
= session
->kernel_session
->channel_count
;
2972 DBG3("Number of kernel channels %zd", nb_chan
);
2974 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2978 case LTTNG_DOMAIN_UST
:
2979 if (session
->ust_session
!= NULL
) {
2981 nb_chan
= lttng_ht_get_count(
2982 session
->ust_session
->domain_global
.channels
);
2985 DBG3("Number of UST global channels %zd", nb_chan
);
2987 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2992 ret
= -LTTNG_ERR_UND
;
2997 const size_t channel_size
= sizeof(struct lttng_channel
) +
2998 sizeof(struct lttng_channel_extended
);
2999 struct lttng_channel_extended
*channel_exts
;
3001 payload_size
= nb_chan
* channel_size
;
3002 *channels
= zmalloc(payload_size
);
3003 if (*channels
== NULL
) {
3004 ret
= -LTTNG_ERR_FATAL
;
3008 channel_exts
= ((void *) *channels
) +
3009 (nb_chan
* sizeof(struct lttng_channel
));
3010 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3011 if (ret
!= LTTNG_OK
) {
3026 * Command LTTNG_LIST_EVENTS processed by the client thread.
3028 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3029 struct ltt_session
*session
, char *channel_name
,
3030 struct lttng_event
**events
, size_t *total_size
)
3033 ssize_t nb_event
= 0;
3036 case LTTNG_DOMAIN_KERNEL
:
3037 if (session
->kernel_session
!= NULL
) {
3038 nb_event
= list_lttng_kernel_events(channel_name
,
3039 session
->kernel_session
, events
,
3043 case LTTNG_DOMAIN_UST
:
3045 if (session
->ust_session
!= NULL
) {
3046 nb_event
= list_lttng_ust_global_events(channel_name
,
3047 &session
->ust_session
->domain_global
, events
,
3052 case LTTNG_DOMAIN_LOG4J
:
3053 case LTTNG_DOMAIN_JUL
:
3054 case LTTNG_DOMAIN_PYTHON
:
3055 if (session
->ust_session
) {
3056 struct lttng_ht_iter iter
;
3060 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3061 &iter
.iter
, agt
, node
.node
) {
3062 if (agt
->domain
== domain
) {
3063 nb_event
= list_lttng_agent_events(
3073 ret
= LTTNG_ERR_UND
;
3080 /* Return negative value to differentiate return code */
3085 * Using the session list, filled a lttng_session array to send back to the
3086 * client for session listing.
3088 * The session list lock MUST be acquired before calling this function. Use
3089 * session_lock_list() and session_unlock_list().
3091 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3096 struct ltt_session
*session
;
3097 struct ltt_session_list
*list
= session_get_list();
3099 DBG("Getting all available session for UID %d GID %d",
3102 * Iterate over session list and append data after the control struct in
3105 cds_list_for_each_entry(session
, &list
->head
, list
) {
3107 * Only list the sessions the user can control.
3109 if (!session_access_ok(session
, uid
, gid
)) {
3113 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3114 struct ltt_ust_session
*usess
= session
->ust_session
;
3116 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3117 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3118 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3119 ret
= build_network_session_path(sessions
[i
].path
,
3120 sizeof(sessions
[i
].path
), session
);
3122 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3123 session
->consumer
->dst
.session_root_path
);
3126 PERROR("snprintf session path");
3130 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3131 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3132 sessions
[i
].enabled
= session
->active
;
3133 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3134 sessions
[i
].live_timer_interval
= session
->live_timer
;
3140 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3141 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3143 int cmd_data_pending(struct ltt_session
*session
)
3146 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3147 struct ltt_ust_session
*usess
= session
->ust_session
;
3151 /* Session MUST be stopped to ask for data availability. */
3152 if (session
->active
) {
3153 ret
= LTTNG_ERR_SESSION_STARTED
;
3157 * If stopped, just make sure we've started before else the above call
3158 * will always send that there is data pending.
3160 * The consumer assumes that when the data pending command is received,
3161 * the trace has been started before or else no output data is written
3162 * by the streams which is a condition for data pending. So, this is
3163 * *VERY* important that we don't ask the consumer before a start
3166 if (!session
->has_been_started
) {
3172 if (ksess
&& ksess
->consumer
) {
3173 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3175 /* Data is still being extracted for the kernel. */
3180 if (usess
&& usess
->consumer
) {
3181 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3183 /* Data is still being extracted for the kernel. */
3188 /* Data is ready to be read by a viewer */
3196 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3198 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3200 int cmd_snapshot_add_output(struct ltt_session
*session
,
3201 struct lttng_snapshot_output
*output
, uint32_t *id
)
3204 struct snapshot_output
*new_output
;
3209 DBG("Cmd snapshot add output for session %s", session
->name
);
3212 * Can't create an output if the session is not set in no-output mode.
3214 if (session
->output_traces
) {
3215 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3219 /* Only one output is allowed until we have the "tee" feature. */
3220 if (session
->snapshot
.nb_output
== 1) {
3221 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3225 new_output
= snapshot_output_alloc();
3227 ret
= LTTNG_ERR_NOMEM
;
3231 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3232 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3233 &session
->snapshot
);
3235 if (ret
== -ENOMEM
) {
3236 ret
= LTTNG_ERR_NOMEM
;
3238 ret
= LTTNG_ERR_INVALID
;
3244 snapshot_add_output(&session
->snapshot
, new_output
);
3246 *id
= new_output
->id
;
3253 snapshot_output_destroy(new_output
);
3259 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3261 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3263 int cmd_snapshot_del_output(struct ltt_session
*session
,
3264 struct lttng_snapshot_output
*output
)
3267 struct snapshot_output
*sout
= NULL
;
3275 * Permission denied to create an output if the session is not
3276 * set in no output mode.
3278 if (session
->output_traces
) {
3279 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3284 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3286 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3287 } else if (*output
->name
!= '\0') {
3288 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3290 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3293 ret
= LTTNG_ERR_INVALID
;
3297 snapshot_delete_output(&session
->snapshot
, sout
);
3298 snapshot_output_destroy(sout
);
3307 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3309 * If no output is available, outputs is untouched and 0 is returned.
3311 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3313 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3314 struct lttng_snapshot_output
**outputs
)
3317 struct lttng_snapshot_output
*list
= NULL
;
3318 struct lttng_ht_iter iter
;
3319 struct snapshot_output
*output
;
3324 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3327 * Permission denied to create an output if the session is not
3328 * set in no output mode.
3330 if (session
->output_traces
) {
3331 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3335 if (session
->snapshot
.nb_output
== 0) {
3340 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3342 ret
= -LTTNG_ERR_NOMEM
;
3346 /* Copy list from session to the new list object. */
3348 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3349 output
, node
.node
) {
3350 assert(output
->consumer
);
3351 list
[idx
].id
= output
->id
;
3352 list
[idx
].max_size
= output
->max_size
;
3353 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3354 sizeof(list
[idx
].name
))) {
3355 ret
= -LTTNG_ERR_INVALID
;
3358 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3359 if (lttng_strncpy(list
[idx
].ctrl_url
,
3360 output
->consumer
->dst
.session_root_path
,
3361 sizeof(list
[idx
].ctrl_url
))) {
3362 ret
= -LTTNG_ERR_INVALID
;
3367 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3368 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3370 ret
= -LTTNG_ERR_NOMEM
;
3375 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3376 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3378 ret
= -LTTNG_ERR_NOMEM
;
3387 ret
= session
->snapshot
.nb_output
;
3396 * Check if we can regenerate the metadata for this session.
3397 * Only kernel, UST per-uid and non-live sessions are supported.
3399 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3402 int check_regenerate_metadata_support(struct ltt_session
*session
)
3408 if (session
->live_timer
!= 0) {
3409 ret
= LTTNG_ERR_LIVE_SESSION
;
3412 if (!session
->active
) {
3413 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3416 if (session
->ust_session
) {
3417 switch (session
->ust_session
->buffer_type
) {
3418 case LTTNG_BUFFER_PER_UID
:
3420 case LTTNG_BUFFER_PER_PID
:
3421 ret
= LTTNG_ERR_PER_PID_SESSION
;
3425 ret
= LTTNG_ERR_UNK
;
3429 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3430 session
->consumer
->relay_minor_version
< 8) {
3431 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3441 int clear_metadata_file(int fd
)
3445 ret
= lseek(fd
, 0, SEEK_SET
);
3451 ret
= ftruncate(fd
, 0);
3453 PERROR("ftruncate");
3462 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3465 struct buffer_reg_uid
*uid_reg
= NULL
;
3466 struct buffer_reg_session
*session_reg
= NULL
;
3469 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3470 struct ust_registry_session
*registry
;
3471 struct ust_registry_channel
*chan
;
3472 struct lttng_ht_iter iter_chan
;
3474 session_reg
= uid_reg
->registry
;
3475 registry
= session_reg
->reg
.ust
;
3477 pthread_mutex_lock(®istry
->lock
);
3478 registry
->metadata_len_sent
= 0;
3479 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3480 registry
->metadata_len
= 0;
3481 registry
->metadata_version
++;
3482 if (registry
->metadata_fd
> 0) {
3483 /* Clear the metadata file's content. */
3484 ret
= clear_metadata_file(registry
->metadata_fd
);
3486 pthread_mutex_unlock(®istry
->lock
);
3491 ret
= ust_metadata_session_statedump(registry
, NULL
,
3492 registry
->major
, registry
->minor
);
3494 pthread_mutex_unlock(®istry
->lock
);
3495 ERR("Failed to generate session metadata (err = %d)",
3499 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3501 struct ust_registry_event
*event
;
3502 struct lttng_ht_iter iter_event
;
3504 ret
= ust_metadata_channel_statedump(registry
, chan
);
3506 pthread_mutex_unlock(®istry
->lock
);
3507 ERR("Failed to generate channel metadata "
3511 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3513 ret
= ust_metadata_event_statedump(registry
,
3516 pthread_mutex_unlock(®istry
->lock
);
3517 ERR("Failed to generate event metadata "
3523 pthread_mutex_unlock(®istry
->lock
);
3532 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3534 * Ask the consumer to truncate the existing metadata file(s) and
3535 * then regenerate the metadata. Live and per-pid sessions are not
3536 * supported and return an error.
3538 * Return 0 on success or else a LTTNG_ERR code.
3540 int cmd_regenerate_metadata(struct ltt_session
*session
)
3546 ret
= check_regenerate_metadata_support(session
);
3551 if (session
->kernel_session
) {
3552 ret
= kernctl_session_regenerate_metadata(
3553 session
->kernel_session
->fd
);
3555 ERR("Failed to regenerate the kernel metadata");
3560 if (session
->ust_session
) {
3561 ret
= ust_regenerate_metadata(session
->ust_session
);
3563 ERR("Failed to regenerate the UST metadata");
3567 DBG("Cmd metadata regenerate for session %s", session
->name
);
3575 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3577 * Ask the tracer to regenerate a new statedump.
3579 * Return 0 on success or else a LTTNG_ERR code.
3581 int cmd_regenerate_statedump(struct ltt_session
*session
)
3587 if (!session
->active
) {
3588 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3592 if (session
->kernel_session
) {
3593 ret
= kernctl_session_regenerate_statedump(
3594 session
->kernel_session
->fd
);
3596 * Currently, the statedump in kernel can only fail if out
3600 if (ret
== -ENOMEM
) {
3601 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3603 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3605 ERR("Failed to regenerate the kernel statedump");
3610 if (session
->ust_session
) {
3611 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3613 * Currently, the statedump in UST always returns 0.
3616 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3617 ERR("Failed to regenerate the UST statedump");
3621 DBG("Cmd regenerate statedump for session %s", session
->name
);
3628 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3629 struct notification_thread_handle
*notification_thread
)
3633 ssize_t sock_recv_len
;
3634 struct lttng_trigger
*trigger
= NULL
;
3635 struct lttng_buffer_view view
;
3636 struct lttng_dynamic_buffer trigger_buffer
;
3638 lttng_dynamic_buffer_init(&trigger_buffer
);
3639 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3640 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3642 ret
= LTTNG_ERR_NOMEM
;
3646 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3648 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3649 ERR("Failed to receive \"register trigger\" command payload");
3650 /* TODO: should this be a new error enum ? */
3651 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3655 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3656 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3658 ERR("Invalid trigger payload received in \"register trigger\" command");
3659 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3663 ret
= notification_thread_command_register_trigger(notification_thread
,
3665 /* Ownership of trigger was transferred. */
3668 lttng_trigger_destroy(trigger
);
3669 lttng_dynamic_buffer_reset(&trigger_buffer
);
3673 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3674 struct notification_thread_handle
*notification_thread
)
3678 ssize_t sock_recv_len
;
3679 struct lttng_trigger
*trigger
= NULL
;
3680 struct lttng_buffer_view view
;
3681 struct lttng_dynamic_buffer trigger_buffer
;
3683 lttng_dynamic_buffer_init(&trigger_buffer
);
3684 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3685 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3687 ret
= LTTNG_ERR_NOMEM
;
3691 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3693 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3694 ERR("Failed to receive \"unregister trigger\" command payload");
3695 /* TODO: should this be a new error enum ? */
3696 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3700 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3701 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3703 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3704 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3708 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3711 lttng_trigger_destroy(trigger
);
3712 lttng_dynamic_buffer_reset(&trigger_buffer
);
3717 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3718 * snapshot output is *not* set with a remote destination.
3720 * Return 0 on success or a LTTNG_ERR code.
3722 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3723 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3726 struct lttng_ht_iter iter
;
3727 struct consumer_socket
*socket
;
3730 assert(snap_output
);
3733 DBG2("Set relayd object from snapshot output");
3735 /* Ignore if snapshot consumer output is not network. */
3736 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3741 * For each consumer socket, create and send the relayd object of the
3745 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3746 socket
, node
.node
) {
3747 ret
= send_consumer_relayd_sockets(0, session
->id
,
3748 snap_output
->consumer
, socket
,
3749 session
->name
, session
->hostname
,
3750 session
->live_timer
);
3751 if (ret
!= LTTNG_OK
) {
3763 * Record a kernel snapshot.
3765 * Return LTTNG_OK on success or a LTTNG_ERR code.
3767 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3768 struct snapshot_output
*output
, struct ltt_session
*session
,
3769 int wait
, uint64_t nb_packets_per_stream
)
3779 * Copy kernel session sockets so we can communicate with the right
3780 * consumer for the snapshot record command.
3782 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3784 ret
= LTTNG_ERR_NOMEM
;
3788 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3789 if (ret
!= LTTNG_OK
) {
3790 goto error_snapshot
;
3793 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3794 if (ret
!= LTTNG_OK
) {
3795 goto error_snapshot
;
3802 /* Clean up copied sockets so this output can use some other later on. */
3803 consumer_destroy_output_sockets(output
->consumer
);
3810 * Record a UST snapshot.
3812 * Return 0 on success or a LTTNG_ERR error code.
3814 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3815 struct snapshot_output
*output
, struct ltt_session
*session
,
3816 int wait
, uint64_t nb_packets_per_stream
)
3825 * Copy UST session sockets so we can communicate with the right
3826 * consumer for the snapshot record command.
3828 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3830 ret
= LTTNG_ERR_NOMEM
;
3834 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3835 if (ret
!= LTTNG_OK
) {
3836 goto error_snapshot
;
3839 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3843 ret
= LTTNG_ERR_INVALID
;
3846 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3849 goto error_snapshot
;
3855 /* Clean up copied sockets so this output can use some other later on. */
3856 consumer_destroy_output_sockets(output
->consumer
);
3862 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3863 uint64_t cur_nr_packets
)
3865 uint64_t tot_size
= 0;
3867 if (session
->kernel_session
) {
3868 struct ltt_kernel_channel
*chan
;
3869 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3871 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3872 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3874 * Don't take channel into account if we
3875 * already grab all its packets.
3879 tot_size
+= chan
->channel
->attr
.subbuf_size
3880 * chan
->stream_count
;
3884 if (session
->ust_session
) {
3885 struct ltt_ust_session
*usess
= session
->ust_session
;
3887 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3895 * Calculate the number of packets we can grab from each stream that
3896 * fits within the overall snapshot max size.
3898 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3899 * the number of packets per stream.
3901 * TODO: this approach is not perfect: we consider the worse case
3902 * (packet filling the sub-buffers) as an upper bound, but we could do
3903 * better if we do this calculation while we actually grab the packet
3904 * content: we would know how much padding we don't actually store into
3907 * This algorithm is currently bounded by the number of packets per
3910 * Since we call this algorithm before actually grabbing the data, it's
3911 * an approximation: for instance, applications could appear/disappear
3912 * in between this call and actually grabbing data.
3915 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3918 uint64_t cur_nb_packets
= 0;
3921 return 0; /* Infinite */
3924 size_left
= max_size
;
3926 uint64_t one_more_packet_tot_size
;
3928 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3930 if (!one_more_packet_tot_size
) {
3931 /* We are already grabbing all packets. */
3934 size_left
-= one_more_packet_tot_size
;
3935 if (size_left
< 0) {
3940 if (!cur_nb_packets
) {
3941 /* Not enough room to grab one packet of each stream, error. */
3944 return cur_nb_packets
;
3948 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3950 * The wait parameter is ignored so this call always wait for the snapshot to
3951 * complete before returning.
3953 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3955 int cmd_snapshot_record(struct ltt_session
*session
,
3956 struct lttng_snapshot_output
*output
, int wait
)
3959 unsigned int use_tmp_output
= 0;
3960 struct snapshot_output tmp_output
;
3961 unsigned int snapshot_success
= 0;
3967 DBG("Cmd snapshot record for session %s", session
->name
);
3969 /* Get the datetime for the snapshot output directory. */
3970 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3973 ret
= LTTNG_ERR_INVALID
;
3978 * Permission denied to create an output if the session is not
3979 * set in no output mode.
3981 if (session
->output_traces
) {
3982 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3986 /* The session needs to be started at least once. */
3987 if (!session
->has_been_started
) {
3988 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3992 /* Use temporary output for the session. */
3993 if (*output
->ctrl_url
!= '\0') {
3994 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3995 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3998 if (ret
== -ENOMEM
) {
3999 ret
= LTTNG_ERR_NOMEM
;
4001 ret
= LTTNG_ERR_INVALID
;
4005 /* Use the global session count for the temporary snapshot. */
4006 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4008 /* Use the global datetime */
4009 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4013 if (use_tmp_output
) {
4014 int64_t nb_packets_per_stream
;
4016 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4017 tmp_output
.max_size
);
4018 if (nb_packets_per_stream
< 0) {
4019 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4023 if (session
->kernel_session
) {
4024 ret
= record_kernel_snapshot(session
->kernel_session
,
4025 &tmp_output
, session
,
4026 wait
, nb_packets_per_stream
);
4027 if (ret
!= LTTNG_OK
) {
4032 if (session
->ust_session
) {
4033 ret
= record_ust_snapshot(session
->ust_session
,
4034 &tmp_output
, session
,
4035 wait
, nb_packets_per_stream
);
4036 if (ret
!= LTTNG_OK
) {
4041 snapshot_success
= 1;
4043 struct snapshot_output
*sout
;
4044 struct lttng_ht_iter iter
;
4047 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4048 &iter
.iter
, sout
, node
.node
) {
4049 int64_t nb_packets_per_stream
;
4052 * Make a local copy of the output and assign the possible
4053 * temporary value given by the caller.
4055 memset(&tmp_output
, 0, sizeof(tmp_output
));
4056 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4058 if (output
->max_size
!= (uint64_t) -1ULL) {
4059 tmp_output
.max_size
= output
->max_size
;
4062 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4063 tmp_output
.max_size
);
4064 if (nb_packets_per_stream
< 0) {
4065 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4070 /* Use temporary name. */
4071 if (*output
->name
!= '\0') {
4072 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4073 sizeof(tmp_output
.name
))) {
4074 ret
= LTTNG_ERR_INVALID
;
4080 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4081 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4083 if (session
->kernel_session
) {
4084 ret
= record_kernel_snapshot(session
->kernel_session
,
4085 &tmp_output
, session
,
4086 wait
, nb_packets_per_stream
);
4087 if (ret
!= LTTNG_OK
) {
4093 if (session
->ust_session
) {
4094 ret
= record_ust_snapshot(session
->ust_session
,
4095 &tmp_output
, session
,
4096 wait
, nb_packets_per_stream
);
4097 if (ret
!= LTTNG_OK
) {
4102 snapshot_success
= 1;
4107 if (snapshot_success
) {
4108 session
->snapshot
.nb_snapshot
++;
4110 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4118 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4120 int cmd_set_session_shm_path(struct ltt_session
*session
,
4121 const char *shm_path
)
4127 * Can only set shm path before session is started.
4129 if (session
->has_been_started
) {
4130 return LTTNG_ERR_SESSION_STARTED
;
4133 strncpy(session
->shm_path
, shm_path
,
4134 sizeof(session
->shm_path
));
4135 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4141 const char *get_base_path(struct ltt_session
*session
,
4142 struct consumer_output
*consumer
)
4144 if (session
->net_handle
> 0) {
4145 return consumer
->dst
.net
.base_dir
;
4147 return consumer
->dst
.session_root_path
;
4152 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4154 * Ask the consumer to rotate the session output directory.
4156 * Return 0 on success or else a LTTNG_ERR code.
4158 int cmd_rotate_session(struct ltt_session
*session
,
4159 struct lttng_rotate_session_return
**rotate_return
)
4162 struct tm
*timeinfo
;
4168 *rotate_return
= zmalloc(sizeof(struct lttng_rotate_session_return
));
4169 if (!*rotate_return
) {
4174 if (session
->live_timer
|| session
->snapshot_mode
||
4175 !session
->output_traces
) {
4176 ret
= -LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
4180 if (session
->rotate_pending
|| session
->rotate_pending_relay
) {
4181 ret
= -LTTNG_ERR_ROTATE_PENDING
;
4185 /* Special case for the first rotation. */
4186 if (session
->rotate_count
== 0) {
4187 const char *base_path
= NULL
;
4189 /* Either one of the two sessions is enough to get the root path. */
4190 if (session
->kernel_session
) {
4191 base_path
= get_base_path(session
, session
->kernel_session
->consumer
);
4192 } else if (session
->ust_session
) {
4193 base_path
= get_base_path(session
, session
->ust_session
->consumer
);
4198 snprintf(session
->rotation_chunk
.current_rotate_path
,
4201 fprintf(stderr
, "b: %s\n", base_path
);
4204 * The currently active tracing path is now the folder we
4207 snprintf(session
->rotation_chunk
.current_rotate_path
,
4209 session
->rotation_chunk
.active_tracing_path
);
4212 session
->rotate_count
++;
4213 session
->rotate_pending
= true;
4214 session
->rotate_pending_relay_check_in_progress
= false;
4215 session
->rotate_status
= LTTNG_ROTATE_STARTED
;
4218 * Create the path name for the next chunk.
4221 if (now
== (time_t) -1) {
4222 ret
= -LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
4225 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4226 session
->current_chunk_start_ts
= now
;
4228 timeinfo
= localtime(&now
);
4229 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
4230 if (session
->kernel_session
) {
4231 /* The active path for the next rotation/destroy. */
4232 snprintf(session
->rotation_chunk
.active_tracing_path
,
4234 get_base_path(session
, session
->kernel_session
->consumer
),
4236 /* The sub-directory for the consumer. */
4237 snprintf(session
->kernel_session
->consumer
->chunk_path
,
4238 PATH_MAX
, "/%s-%s", datetime
,
4239 session
->kernel_session
->consumer
->subdir
);
4240 ret
= kernel_rotate_session(session
);
4241 if (ret
!= LTTNG_OK
) {
4245 if (session
->ust_session
) {
4246 snprintf(session
->rotation_chunk
.active_tracing_path
,
4248 get_base_path(session
, session
->ust_session
->consumer
),
4250 snprintf(session
->ust_session
->consumer
->chunk_path
,
4251 PATH_MAX
, "/%s-", datetime
);
4252 ret
= ust_app_rotate_session(session
);
4253 if (ret
!= LTTNG_OK
) {
4258 (*rotate_return
)->rotate_id
= session
->rotate_count
;
4259 (*rotate_return
)->status
= LTTNG_ROTATE_STARTED
;
4261 DBG("Cmd rotate session %s, rotate_id %" PRIu64
, session
->name
,
4262 session
->rotate_count
);
4268 (*rotate_return
)->status
= LTTNG_ROTATE_ERROR
;
4274 * Command LTTNG_ROTATE_PENDING from the lttng-ctl library.
4276 * Check if the session has finished its rotation.
4278 * Return 0 on success or else a LTTNG_ERR code.
4280 int cmd_rotate_pending(struct ltt_session
*session
,
4281 struct lttng_rotate_pending_return
**pending_return
,
4288 DBG("Cmd rotate pending session %s, rotate_id %" PRIu64
, session
->name
,
4289 session
->rotate_count
);
4291 *pending_return
= zmalloc(sizeof(struct lttng_rotate_pending_return
));
4292 if (!*pending_return
) {
4297 if (session
->rotate_count
!= rotate_id
) {
4298 (*pending_return
)->status
= LTTNG_ROTATE_EXPIRED
;
4303 if (session
->rotate_status
== LTTNG_ROTATE_ERROR
) {
4304 DBG("An error occurred during rotation");
4305 (*pending_return
)->status
= LTTNG_ROTATE_ERROR
;
4306 } else if (session
->rotate_status
== LTTNG_ROTATE_EMPTY
) {
4307 DBG("Nothing to rotate");
4308 (*pending_return
)->status
= LTTNG_ROTATE_EMPTY
;
4309 /* Rotate with a relay */
4310 } else if (session
->rotate_pending_relay
) {
4311 DBG("Session %s, rotate_id %" PRIu64
" still pending",
4312 session
->name
, session
->rotate_count
);
4313 (*pending_return
)->status
= LTTNG_ROTATE_STARTED
;
4314 } else if (session
->rotate_pending
) {
4315 DBG("Session %s, rotate_id %" PRIu64
" still pending",
4316 session
->name
, session
->rotate_count
);
4317 (*pending_return
)->status
= LTTNG_ROTATE_STARTED
;
4319 DBG("Session %s, rotate_id %" PRIu64
" finished",
4320 session
->name
, session
->rotate_count
);
4321 (*pending_return
)->status
= LTTNG_ROTATE_COMPLETED
;
4322 snprintf((*pending_return
)->output_path
, PATH_MAX
, "%s",
4323 session
->rotation_chunk
.current_rotate_path
);
4335 * Init command subsystem.
4340 * Set network sequence index to 1 for streams to match a relayd
4341 * socket on the consumer side.
4343 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4344 relayd_net_seq_idx
= 1;
4345 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4347 DBG("Command subsystem initialized");