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;
2441 if (session
->rotate_timer_period
) {
2442 ret
= sessiond_rotate_timer_start(session
,
2443 session
->rotate_timer_period
);
2445 ERR("Failed to enable rotate timer");
2446 ret
= LTTNG_ERR_UNK
;
2458 * Command LTTNG_STOP_TRACE processed by the client thread.
2460 int cmd_stop_trace(struct ltt_session
*session
)
2463 struct ltt_kernel_channel
*kchan
;
2464 struct ltt_kernel_session
*ksession
;
2465 struct ltt_ust_session
*usess
;
2470 ksession
= session
->kernel_session
;
2471 usess
= session
->ust_session
;
2473 /* Session is not active. Skip everythong and inform the client. */
2474 if (!session
->active
) {
2475 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2480 if (ksession
&& ksession
->active
) {
2481 DBG("Stop kernel tracing");
2483 ret
= kernel_stop_session(ksession
);
2485 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2489 kernel_wait_quiescent(kernel_tracer_fd
);
2491 /* Flush metadata after stopping (if exists) */
2492 if (ksession
->metadata_stream_fd
>= 0) {
2493 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2495 ERR("Kernel metadata flush failed");
2499 /* Flush all buffers after stopping */
2500 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2501 ret
= kernel_flush_buffer(kchan
);
2503 ERR("Kernel flush buffer error");
2507 ksession
->active
= 0;
2510 if (usess
&& usess
->active
) {
2512 * Even though the stop trace might fail, flag this session inactive so
2513 * other application coming in are not started by default.
2517 ret
= ust_app_stop_trace_all(usess
);
2519 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2524 session
->session_last_stop_ts
= time(NULL
);
2526 /* Flag inactive after a successful stop. */
2527 session
->active
= 0;
2535 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2537 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2538 struct lttng_uri
*uris
)
2541 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2542 struct ltt_ust_session
*usess
= session
->ust_session
;
2548 /* Can't set consumer URI if the session is active. */
2549 if (session
->active
) {
2550 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2554 /* Set the "global" consumer URIs */
2555 for (i
= 0; i
< nb_uri
; i
++) {
2556 ret
= add_uri_to_consumer(session
->consumer
,
2557 &uris
[i
], 0, session
->name
);
2558 if (ret
!= LTTNG_OK
) {
2563 /* Set UST session URIs */
2564 if (session
->ust_session
) {
2565 for (i
= 0; i
< nb_uri
; i
++) {
2566 ret
= add_uri_to_consumer(
2567 session
->ust_session
->consumer
,
2568 &uris
[i
], LTTNG_DOMAIN_UST
,
2570 if (ret
!= LTTNG_OK
) {
2576 /* Set kernel session URIs */
2577 if (session
->kernel_session
) {
2578 for (i
= 0; i
< nb_uri
; i
++) {
2579 ret
= add_uri_to_consumer(
2580 session
->kernel_session
->consumer
,
2581 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2583 if (ret
!= LTTNG_OK
) {
2590 * Make sure to set the session in output mode after we set URI since a
2591 * session can be created without URL (thus flagged in no output mode).
2593 session
->output_traces
= 1;
2595 ksess
->output_traces
= 1;
2599 usess
->output_traces
= 1;
2610 * Command LTTNG_CREATE_SESSION processed by the client thread.
2612 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2613 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2616 struct ltt_session
*session
;
2622 * Verify if the session already exist
2624 * XXX: There is no need for the session lock list here since the caller
2625 * (process_client_msg) is holding it. We might want to change that so a
2626 * single command does not lock the entire session list.
2628 session
= session_find_by_name(name
);
2629 if (session
!= NULL
) {
2630 ret
= LTTNG_ERR_EXIST_SESS
;
2634 /* Create tracing session in the registry */
2635 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2636 LTTNG_SOCK_GET_GID_CRED(creds
));
2637 if (ret
!= LTTNG_OK
) {
2642 * Get the newly created session pointer back
2644 * XXX: There is no need for the session lock list here since the caller
2645 * (process_client_msg) is holding it. We might want to change that so a
2646 * single command does not lock the entire session list.
2648 session
= session_find_by_name(name
);
2651 session
->live_timer
= live_timer
;
2652 /* Create default consumer output for the session not yet created. */
2653 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2654 if (session
->consumer
== NULL
) {
2655 ret
= LTTNG_ERR_FATAL
;
2656 goto consumer_error
;
2660 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2661 if (ret
!= LTTNG_OK
) {
2662 goto consumer_error
;
2664 session
->output_traces
= 1;
2666 session
->output_traces
= 0;
2667 DBG2("Session %s created with no output", session
->name
);
2670 session
->consumer
->enabled
= 1;
2675 session_destroy(session
);
2682 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2684 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2685 size_t nb_uri
, lttng_sock_cred
*creds
)
2688 struct ltt_session
*session
;
2689 struct snapshot_output
*new_output
= NULL
;
2695 * Create session in no output mode with URIs set to NULL. The uris we've
2696 * received are for a default snapshot output if one.
2698 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2699 if (ret
!= LTTNG_OK
) {
2703 /* Get the newly created session pointer back. This should NEVER fail. */
2704 session
= session_find_by_name(name
);
2707 /* Flag session for snapshot mode. */
2708 session
->snapshot_mode
= 1;
2710 /* Skip snapshot output creation if no URI is given. */
2715 new_output
= snapshot_output_alloc();
2717 ret
= LTTNG_ERR_NOMEM
;
2718 goto error_snapshot_alloc
;
2721 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2722 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2724 if (ret
== -ENOMEM
) {
2725 ret
= LTTNG_ERR_NOMEM
;
2727 ret
= LTTNG_ERR_INVALID
;
2729 goto error_snapshot
;
2733 snapshot_add_output(&session
->snapshot
, new_output
);
2740 snapshot_output_destroy(new_output
);
2741 error_snapshot_alloc
:
2742 session_destroy(session
);
2748 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2750 * Called with session lock held.
2752 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2755 struct ltt_ust_session
*usess
;
2756 struct ltt_kernel_session
*ksess
;
2761 usess
= session
->ust_session
;
2762 ksess
= session
->kernel_session
;
2764 if (session
->rotate_count
> 0) {
2765 session
->rotate_count
++;
2767 * The currently active tracing path is now the folder we
2770 snprintf(session
->rotation_chunk
.current_rotate_path
,
2772 session
->rotation_chunk
.active_tracing_path
);
2773 ret
= rename_complete_chunk(session
,
2774 session
->session_last_stop_ts
);
2776 ERR("Renaming session on destroy");
2780 if (session
->rotate_relay_pending_timer_enabled
) {
2781 sessiond_timer_rotate_pending_stop(session
);
2784 if (session
->rotate_timer_enabled
) {
2785 sessiond_rotate_timer_stop(session
);
2788 /* Clean kernel session teardown */
2789 kernel_destroy_session(ksess
);
2791 /* UST session teardown */
2793 /* Close any relayd session */
2794 consumer_output_send_destroy_relayd(usess
->consumer
);
2796 /* Destroy every UST application related to this session. */
2797 ret
= ust_app_destroy_trace_all(usess
);
2799 ERR("Error in ust_app_destroy_trace_all");
2802 /* Clean up the rest. */
2803 trace_ust_destroy_session(usess
);
2807 * Must notify the kernel thread here to update it's poll set in order to
2808 * remove the channel(s)' fd just destroyed.
2810 ret
= notify_thread_pipe(wpipe
);
2812 PERROR("write kernel poll pipe");
2815 ret
= session_destroy(session
);
2821 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2823 int cmd_register_consumer(struct ltt_session
*session
,
2824 enum lttng_domain_type domain
, const char *sock_path
,
2825 struct consumer_data
*cdata
)
2828 struct consumer_socket
*socket
= NULL
;
2835 case LTTNG_DOMAIN_KERNEL
:
2837 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2841 /* Can't register a consumer if there is already one */
2842 if (ksess
->consumer_fds_sent
!= 0) {
2843 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2847 sock
= lttcomm_connect_unix_sock(sock_path
);
2849 ret
= LTTNG_ERR_CONNECT_FAIL
;
2852 cdata
->cmd_sock
= sock
;
2854 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2855 if (socket
== NULL
) {
2858 PERROR("close register consumer");
2860 cdata
->cmd_sock
= -1;
2861 ret
= LTTNG_ERR_FATAL
;
2865 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2866 if (socket
->lock
== NULL
) {
2867 PERROR("zmalloc pthread mutex");
2868 ret
= LTTNG_ERR_FATAL
;
2871 pthread_mutex_init(socket
->lock
, NULL
);
2872 socket
->registered
= 1;
2875 consumer_add_socket(socket
, ksess
->consumer
);
2878 pthread_mutex_lock(&cdata
->pid_mutex
);
2880 pthread_mutex_unlock(&cdata
->pid_mutex
);
2885 /* TODO: Userspace tracing */
2886 ret
= LTTNG_ERR_UND
;
2894 consumer_destroy_socket(socket
);
2900 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2902 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2903 struct lttng_domain
**domains
)
2908 struct lttng_ht_iter iter
;
2910 if (session
->kernel_session
!= NULL
) {
2911 DBG3("Listing domains found kernel domain");
2915 if (session
->ust_session
!= NULL
) {
2916 DBG3("Listing domains found UST global domain");
2920 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2922 if (agt
->being_used
) {
2933 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2934 if (*domains
== NULL
) {
2935 ret
= LTTNG_ERR_FATAL
;
2939 if (session
->kernel_session
!= NULL
) {
2940 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2942 /* Kernel session buffer type is always GLOBAL */
2943 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2948 if (session
->ust_session
!= NULL
) {
2949 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2950 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2954 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2956 if (agt
->being_used
) {
2957 (*domains
)[index
].type
= agt
->domain
;
2958 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2968 /* Return negative value to differentiate return code */
2974 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2976 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2977 struct ltt_session
*session
, struct lttng_channel
**channels
)
2979 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2982 case LTTNG_DOMAIN_KERNEL
:
2983 if (session
->kernel_session
!= NULL
) {
2984 nb_chan
= session
->kernel_session
->channel_count
;
2986 DBG3("Number of kernel channels %zd", nb_chan
);
2988 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2992 case LTTNG_DOMAIN_UST
:
2993 if (session
->ust_session
!= NULL
) {
2995 nb_chan
= lttng_ht_get_count(
2996 session
->ust_session
->domain_global
.channels
);
2999 DBG3("Number of UST global channels %zd", nb_chan
);
3001 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3006 ret
= -LTTNG_ERR_UND
;
3011 const size_t channel_size
= sizeof(struct lttng_channel
) +
3012 sizeof(struct lttng_channel_extended
);
3013 struct lttng_channel_extended
*channel_exts
;
3015 payload_size
= nb_chan
* channel_size
;
3016 *channels
= zmalloc(payload_size
);
3017 if (*channels
== NULL
) {
3018 ret
= -LTTNG_ERR_FATAL
;
3022 channel_exts
= ((void *) *channels
) +
3023 (nb_chan
* sizeof(struct lttng_channel
));
3024 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3025 if (ret
!= LTTNG_OK
) {
3040 * Command LTTNG_LIST_EVENTS processed by the client thread.
3042 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3043 struct ltt_session
*session
, char *channel_name
,
3044 struct lttng_event
**events
, size_t *total_size
)
3047 ssize_t nb_event
= 0;
3050 case LTTNG_DOMAIN_KERNEL
:
3051 if (session
->kernel_session
!= NULL
) {
3052 nb_event
= list_lttng_kernel_events(channel_name
,
3053 session
->kernel_session
, events
,
3057 case LTTNG_DOMAIN_UST
:
3059 if (session
->ust_session
!= NULL
) {
3060 nb_event
= list_lttng_ust_global_events(channel_name
,
3061 &session
->ust_session
->domain_global
, events
,
3066 case LTTNG_DOMAIN_LOG4J
:
3067 case LTTNG_DOMAIN_JUL
:
3068 case LTTNG_DOMAIN_PYTHON
:
3069 if (session
->ust_session
) {
3070 struct lttng_ht_iter iter
;
3074 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3075 &iter
.iter
, agt
, node
.node
) {
3076 if (agt
->domain
== domain
) {
3077 nb_event
= list_lttng_agent_events(
3087 ret
= LTTNG_ERR_UND
;
3094 /* Return negative value to differentiate return code */
3099 * Using the session list, filled a lttng_session array to send back to the
3100 * client for session listing.
3102 * The session list lock MUST be acquired before calling this function. Use
3103 * session_lock_list() and session_unlock_list().
3105 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3110 struct ltt_session
*session
;
3111 struct ltt_session_list
*list
= session_get_list();
3113 DBG("Getting all available session for UID %d GID %d",
3116 * Iterate over session list and append data after the control struct in
3119 cds_list_for_each_entry(session
, &list
->head
, list
) {
3121 * Only list the sessions the user can control.
3123 if (!session_access_ok(session
, uid
, gid
)) {
3127 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3128 struct ltt_ust_session
*usess
= session
->ust_session
;
3130 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3131 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3132 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3133 ret
= build_network_session_path(sessions
[i
].path
,
3134 sizeof(sessions
[i
].path
), session
);
3136 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3137 session
->consumer
->dst
.session_root_path
);
3140 PERROR("snprintf session path");
3144 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3145 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3146 sessions
[i
].enabled
= session
->active
;
3147 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3148 sessions
[i
].live_timer_interval
= session
->live_timer
;
3154 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3155 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3157 int cmd_data_pending(struct ltt_session
*session
)
3160 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3161 struct ltt_ust_session
*usess
= session
->ust_session
;
3165 /* Session MUST be stopped to ask for data availability. */
3166 if (session
->active
) {
3167 ret
= LTTNG_ERR_SESSION_STARTED
;
3171 * If stopped, just make sure we've started before else the above call
3172 * will always send that there is data pending.
3174 * The consumer assumes that when the data pending command is received,
3175 * the trace has been started before or else no output data is written
3176 * by the streams which is a condition for data pending. So, this is
3177 * *VERY* important that we don't ask the consumer before a start
3180 if (!session
->has_been_started
) {
3187 * A rotation is still pending, we have to wait.
3189 if (session
->rotate_pending
) {
3194 if (ksess
&& ksess
->consumer
) {
3195 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3197 /* Data is still being extracted for the kernel. */
3202 if (usess
&& usess
->consumer
) {
3203 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3205 /* Data is still being extracted for the kernel. */
3210 /* Data is ready to be read by a viewer */
3218 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3220 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3222 int cmd_snapshot_add_output(struct ltt_session
*session
,
3223 struct lttng_snapshot_output
*output
, uint32_t *id
)
3226 struct snapshot_output
*new_output
;
3231 DBG("Cmd snapshot add output for session %s", session
->name
);
3234 * Can't create an output if the session is not set in no-output mode.
3236 if (session
->output_traces
) {
3237 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3241 /* Only one output is allowed until we have the "tee" feature. */
3242 if (session
->snapshot
.nb_output
== 1) {
3243 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3247 new_output
= snapshot_output_alloc();
3249 ret
= LTTNG_ERR_NOMEM
;
3253 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3254 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3255 &session
->snapshot
);
3257 if (ret
== -ENOMEM
) {
3258 ret
= LTTNG_ERR_NOMEM
;
3260 ret
= LTTNG_ERR_INVALID
;
3266 snapshot_add_output(&session
->snapshot
, new_output
);
3268 *id
= new_output
->id
;
3275 snapshot_output_destroy(new_output
);
3281 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3283 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3285 int cmd_snapshot_del_output(struct ltt_session
*session
,
3286 struct lttng_snapshot_output
*output
)
3289 struct snapshot_output
*sout
= NULL
;
3297 * Permission denied to create an output if the session is not
3298 * set in no output mode.
3300 if (session
->output_traces
) {
3301 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3306 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3308 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3309 } else if (*output
->name
!= '\0') {
3310 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3312 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3315 ret
= LTTNG_ERR_INVALID
;
3319 snapshot_delete_output(&session
->snapshot
, sout
);
3320 snapshot_output_destroy(sout
);
3329 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3331 * If no output is available, outputs is untouched and 0 is returned.
3333 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3335 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3336 struct lttng_snapshot_output
**outputs
)
3339 struct lttng_snapshot_output
*list
= NULL
;
3340 struct lttng_ht_iter iter
;
3341 struct snapshot_output
*output
;
3346 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3349 * Permission denied to create an output if the session is not
3350 * set in no output mode.
3352 if (session
->output_traces
) {
3353 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3357 if (session
->snapshot
.nb_output
== 0) {
3362 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3364 ret
= -LTTNG_ERR_NOMEM
;
3368 /* Copy list from session to the new list object. */
3370 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3371 output
, node
.node
) {
3372 assert(output
->consumer
);
3373 list
[idx
].id
= output
->id
;
3374 list
[idx
].max_size
= output
->max_size
;
3375 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3376 sizeof(list
[idx
].name
))) {
3377 ret
= -LTTNG_ERR_INVALID
;
3380 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3381 if (lttng_strncpy(list
[idx
].ctrl_url
,
3382 output
->consumer
->dst
.session_root_path
,
3383 sizeof(list
[idx
].ctrl_url
))) {
3384 ret
= -LTTNG_ERR_INVALID
;
3389 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3390 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3392 ret
= -LTTNG_ERR_NOMEM
;
3397 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3398 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3400 ret
= -LTTNG_ERR_NOMEM
;
3409 ret
= session
->snapshot
.nb_output
;
3418 * Check if we can regenerate the metadata for this session.
3419 * Only kernel, UST per-uid and non-live sessions are supported.
3421 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3424 int check_regenerate_metadata_support(struct ltt_session
*session
)
3430 if (session
->live_timer
!= 0) {
3431 ret
= LTTNG_ERR_LIVE_SESSION
;
3434 if (!session
->active
) {
3435 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3438 if (session
->ust_session
) {
3439 switch (session
->ust_session
->buffer_type
) {
3440 case LTTNG_BUFFER_PER_UID
:
3442 case LTTNG_BUFFER_PER_PID
:
3443 ret
= LTTNG_ERR_PER_PID_SESSION
;
3447 ret
= LTTNG_ERR_UNK
;
3451 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3452 session
->consumer
->relay_minor_version
< 8) {
3453 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3463 int clear_metadata_file(int fd
)
3467 ret
= lseek(fd
, 0, SEEK_SET
);
3473 ret
= ftruncate(fd
, 0);
3475 PERROR("ftruncate");
3484 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3487 struct buffer_reg_uid
*uid_reg
= NULL
;
3488 struct buffer_reg_session
*session_reg
= NULL
;
3491 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3492 struct ust_registry_session
*registry
;
3493 struct ust_registry_channel
*chan
;
3494 struct lttng_ht_iter iter_chan
;
3496 session_reg
= uid_reg
->registry
;
3497 registry
= session_reg
->reg
.ust
;
3499 pthread_mutex_lock(®istry
->lock
);
3500 registry
->metadata_len_sent
= 0;
3501 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3502 registry
->metadata_len
= 0;
3503 registry
->metadata_version
++;
3504 if (registry
->metadata_fd
> 0) {
3505 /* Clear the metadata file's content. */
3506 ret
= clear_metadata_file(registry
->metadata_fd
);
3508 pthread_mutex_unlock(®istry
->lock
);
3513 ret
= ust_metadata_session_statedump(registry
, NULL
,
3514 registry
->major
, registry
->minor
);
3516 pthread_mutex_unlock(®istry
->lock
);
3517 ERR("Failed to generate session metadata (err = %d)",
3521 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3523 struct ust_registry_event
*event
;
3524 struct lttng_ht_iter iter_event
;
3526 ret
= ust_metadata_channel_statedump(registry
, chan
);
3528 pthread_mutex_unlock(®istry
->lock
);
3529 ERR("Failed to generate channel metadata "
3533 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3535 ret
= ust_metadata_event_statedump(registry
,
3538 pthread_mutex_unlock(®istry
->lock
);
3539 ERR("Failed to generate event metadata "
3545 pthread_mutex_unlock(®istry
->lock
);
3554 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3556 * Ask the consumer to truncate the existing metadata file(s) and
3557 * then regenerate the metadata. Live and per-pid sessions are not
3558 * supported and return an error.
3560 * Return 0 on success or else a LTTNG_ERR code.
3562 int cmd_regenerate_metadata(struct ltt_session
*session
)
3568 ret
= check_regenerate_metadata_support(session
);
3573 if (session
->kernel_session
) {
3574 ret
= kernctl_session_regenerate_metadata(
3575 session
->kernel_session
->fd
);
3577 ERR("Failed to regenerate the kernel metadata");
3582 if (session
->ust_session
) {
3583 ret
= ust_regenerate_metadata(session
->ust_session
);
3585 ERR("Failed to regenerate the UST metadata");
3589 DBG("Cmd metadata regenerate for session %s", session
->name
);
3597 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3599 * Ask the tracer to regenerate a new statedump.
3601 * Return 0 on success or else a LTTNG_ERR code.
3603 int cmd_regenerate_statedump(struct ltt_session
*session
)
3609 if (!session
->active
) {
3610 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3614 if (session
->kernel_session
) {
3615 ret
= kernctl_session_regenerate_statedump(
3616 session
->kernel_session
->fd
);
3618 * Currently, the statedump in kernel can only fail if out
3622 if (ret
== -ENOMEM
) {
3623 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3625 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3627 ERR("Failed to regenerate the kernel statedump");
3632 if (session
->ust_session
) {
3633 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3635 * Currently, the statedump in UST always returns 0.
3638 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3639 ERR("Failed to regenerate the UST statedump");
3643 DBG("Cmd regenerate statedump for session %s", session
->name
);
3650 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3651 struct notification_thread_handle
*notification_thread
)
3655 ssize_t sock_recv_len
;
3656 struct lttng_trigger
*trigger
= NULL
;
3657 struct lttng_buffer_view view
;
3658 struct lttng_dynamic_buffer trigger_buffer
;
3660 lttng_dynamic_buffer_init(&trigger_buffer
);
3661 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3662 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3664 ret
= LTTNG_ERR_NOMEM
;
3668 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3670 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3671 ERR("Failed to receive \"register trigger\" command payload");
3672 /* TODO: should this be a new error enum ? */
3673 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3677 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3678 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3680 ERR("Invalid trigger payload received in \"register trigger\" command");
3681 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3685 ret
= notification_thread_command_register_trigger(notification_thread
,
3687 /* Ownership of trigger was transferred. */
3690 lttng_trigger_destroy(trigger
);
3691 lttng_dynamic_buffer_reset(&trigger_buffer
);
3695 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3696 struct notification_thread_handle
*notification_thread
)
3700 ssize_t sock_recv_len
;
3701 struct lttng_trigger
*trigger
= NULL
;
3702 struct lttng_buffer_view view
;
3703 struct lttng_dynamic_buffer trigger_buffer
;
3705 lttng_dynamic_buffer_init(&trigger_buffer
);
3706 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3707 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3709 ret
= LTTNG_ERR_NOMEM
;
3713 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3715 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3716 ERR("Failed to receive \"unregister trigger\" command payload");
3717 /* TODO: should this be a new error enum ? */
3718 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3722 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3723 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3725 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3726 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3730 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3733 lttng_trigger_destroy(trigger
);
3734 lttng_dynamic_buffer_reset(&trigger_buffer
);
3739 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3740 * snapshot output is *not* set with a remote destination.
3742 * Return 0 on success or a LTTNG_ERR code.
3744 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3745 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3748 struct lttng_ht_iter iter
;
3749 struct consumer_socket
*socket
;
3752 assert(snap_output
);
3755 DBG2("Set relayd object from snapshot output");
3757 /* Ignore if snapshot consumer output is not network. */
3758 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3763 * For each consumer socket, create and send the relayd object of the
3767 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3768 socket
, node
.node
) {
3769 ret
= send_consumer_relayd_sockets(0, session
->id
,
3770 snap_output
->consumer
, socket
,
3771 session
->name
, session
->hostname
,
3772 session
->live_timer
);
3773 if (ret
!= LTTNG_OK
) {
3785 * Record a kernel snapshot.
3787 * Return LTTNG_OK on success or a LTTNG_ERR code.
3789 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3790 struct snapshot_output
*output
, struct ltt_session
*session
,
3791 int wait
, uint64_t nb_packets_per_stream
)
3801 * Copy kernel session sockets so we can communicate with the right
3802 * consumer for the snapshot record command.
3804 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3806 ret
= LTTNG_ERR_NOMEM
;
3810 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3811 if (ret
!= LTTNG_OK
) {
3812 goto error_snapshot
;
3815 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3816 if (ret
!= LTTNG_OK
) {
3817 goto error_snapshot
;
3824 /* Clean up copied sockets so this output can use some other later on. */
3825 consumer_destroy_output_sockets(output
->consumer
);
3832 * Record a UST snapshot.
3834 * Return 0 on success or a LTTNG_ERR error code.
3836 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3837 struct snapshot_output
*output
, struct ltt_session
*session
,
3838 int wait
, uint64_t nb_packets_per_stream
)
3847 * Copy UST session sockets so we can communicate with the right
3848 * consumer for the snapshot record command.
3850 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3852 ret
= LTTNG_ERR_NOMEM
;
3856 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3857 if (ret
!= LTTNG_OK
) {
3858 goto error_snapshot
;
3861 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3865 ret
= LTTNG_ERR_INVALID
;
3868 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3871 goto error_snapshot
;
3877 /* Clean up copied sockets so this output can use some other later on. */
3878 consumer_destroy_output_sockets(output
->consumer
);
3884 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3885 uint64_t cur_nr_packets
)
3887 uint64_t tot_size
= 0;
3889 if (session
->kernel_session
) {
3890 struct ltt_kernel_channel
*chan
;
3891 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3893 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3894 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3896 * Don't take channel into account if we
3897 * already grab all its packets.
3901 tot_size
+= chan
->channel
->attr
.subbuf_size
3902 * chan
->stream_count
;
3906 if (session
->ust_session
) {
3907 struct ltt_ust_session
*usess
= session
->ust_session
;
3909 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3917 * Calculate the number of packets we can grab from each stream that
3918 * fits within the overall snapshot max size.
3920 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3921 * the number of packets per stream.
3923 * TODO: this approach is not perfect: we consider the worse case
3924 * (packet filling the sub-buffers) as an upper bound, but we could do
3925 * better if we do this calculation while we actually grab the packet
3926 * content: we would know how much padding we don't actually store into
3929 * This algorithm is currently bounded by the number of packets per
3932 * Since we call this algorithm before actually grabbing the data, it's
3933 * an approximation: for instance, applications could appear/disappear
3934 * in between this call and actually grabbing data.
3937 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3940 uint64_t cur_nb_packets
= 0;
3943 return 0; /* Infinite */
3946 size_left
= max_size
;
3948 uint64_t one_more_packet_tot_size
;
3950 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3952 if (!one_more_packet_tot_size
) {
3953 /* We are already grabbing all packets. */
3956 size_left
-= one_more_packet_tot_size
;
3957 if (size_left
< 0) {
3962 if (!cur_nb_packets
) {
3963 /* Not enough room to grab one packet of each stream, error. */
3966 return cur_nb_packets
;
3970 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3972 * The wait parameter is ignored so this call always wait for the snapshot to
3973 * complete before returning.
3975 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3977 int cmd_snapshot_record(struct ltt_session
*session
,
3978 struct lttng_snapshot_output
*output
, int wait
)
3981 unsigned int use_tmp_output
= 0;
3982 struct snapshot_output tmp_output
;
3983 unsigned int snapshot_success
= 0;
3989 DBG("Cmd snapshot record for session %s", session
->name
);
3991 /* Get the datetime for the snapshot output directory. */
3992 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3995 ret
= LTTNG_ERR_INVALID
;
4000 * Permission denied to create an output if the session is not
4001 * set in no output mode.
4003 if (session
->output_traces
) {
4004 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4008 /* The session needs to be started at least once. */
4009 if (!session
->has_been_started
) {
4010 ret
= LTTNG_ERR_START_SESSION_ONCE
;
4014 /* Use temporary output for the session. */
4015 if (*output
->ctrl_url
!= '\0') {
4016 ret
= snapshot_output_init(output
->max_size
, output
->name
,
4017 output
->ctrl_url
, output
->data_url
, session
->consumer
,
4020 if (ret
== -ENOMEM
) {
4021 ret
= LTTNG_ERR_NOMEM
;
4023 ret
= LTTNG_ERR_INVALID
;
4027 /* Use the global session count for the temporary snapshot. */
4028 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4030 /* Use the global datetime */
4031 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4035 if (use_tmp_output
) {
4036 int64_t nb_packets_per_stream
;
4038 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4039 tmp_output
.max_size
);
4040 if (nb_packets_per_stream
< 0) {
4041 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4045 if (session
->kernel_session
) {
4046 ret
= record_kernel_snapshot(session
->kernel_session
,
4047 &tmp_output
, session
,
4048 wait
, nb_packets_per_stream
);
4049 if (ret
!= LTTNG_OK
) {
4054 if (session
->ust_session
) {
4055 ret
= record_ust_snapshot(session
->ust_session
,
4056 &tmp_output
, session
,
4057 wait
, nb_packets_per_stream
);
4058 if (ret
!= LTTNG_OK
) {
4063 snapshot_success
= 1;
4065 struct snapshot_output
*sout
;
4066 struct lttng_ht_iter iter
;
4069 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4070 &iter
.iter
, sout
, node
.node
) {
4071 int64_t nb_packets_per_stream
;
4074 * Make a local copy of the output and assign the possible
4075 * temporary value given by the caller.
4077 memset(&tmp_output
, 0, sizeof(tmp_output
));
4078 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4080 if (output
->max_size
!= (uint64_t) -1ULL) {
4081 tmp_output
.max_size
= output
->max_size
;
4084 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4085 tmp_output
.max_size
);
4086 if (nb_packets_per_stream
< 0) {
4087 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4092 /* Use temporary name. */
4093 if (*output
->name
!= '\0') {
4094 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4095 sizeof(tmp_output
.name
))) {
4096 ret
= LTTNG_ERR_INVALID
;
4102 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4103 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4105 if (session
->kernel_session
) {
4106 ret
= record_kernel_snapshot(session
->kernel_session
,
4107 &tmp_output
, session
,
4108 wait
, nb_packets_per_stream
);
4109 if (ret
!= LTTNG_OK
) {
4115 if (session
->ust_session
) {
4116 ret
= record_ust_snapshot(session
->ust_session
,
4117 &tmp_output
, session
,
4118 wait
, nb_packets_per_stream
);
4119 if (ret
!= LTTNG_OK
) {
4124 snapshot_success
= 1;
4129 if (snapshot_success
) {
4130 session
->snapshot
.nb_snapshot
++;
4132 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4140 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4142 int cmd_set_session_shm_path(struct ltt_session
*session
,
4143 const char *shm_path
)
4149 * Can only set shm path before session is started.
4151 if (session
->has_been_started
) {
4152 return LTTNG_ERR_SESSION_STARTED
;
4155 strncpy(session
->shm_path
, shm_path
,
4156 sizeof(session
->shm_path
));
4157 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4163 const char *get_base_path(struct ltt_session
*session
,
4164 struct consumer_output
*consumer
)
4166 if (session
->net_handle
> 0) {
4167 return consumer
->dst
.net
.base_dir
;
4169 return consumer
->dst
.session_root_path
;
4174 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4176 * Ask the consumer to rotate the session output directory.
4178 * Return 0 on success or else a LTTNG_ERR code.
4180 int cmd_rotate_session(struct ltt_session
*session
,
4181 struct lttng_rotate_session_return
**rotate_return
)
4184 struct tm
*timeinfo
;
4190 if (rotate_return
) {
4191 *rotate_return
= zmalloc(sizeof(struct lttng_rotate_session_return
));
4192 if (!*rotate_return
) {
4198 if (session
->live_timer
|| session
->snapshot_mode
||
4199 !session
->output_traces
) {
4200 ret
= -LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
4204 if (session
->rotate_pending
|| session
->rotate_pending_relay
) {
4205 ret
= -LTTNG_ERR_ROTATE_PENDING
;
4209 /* Special case for the first rotation. */
4210 if (session
->rotate_count
== 0) {
4211 const char *base_path
= NULL
;
4213 /* Either one of the two sessions is enough to get the root path. */
4214 if (session
->kernel_session
) {
4215 base_path
= get_base_path(session
, session
->kernel_session
->consumer
);
4216 } else if (session
->ust_session
) {
4217 base_path
= get_base_path(session
, session
->ust_session
->consumer
);
4222 snprintf(session
->rotation_chunk
.current_rotate_path
,
4225 fprintf(stderr
, "b: %s\n", base_path
);
4228 * The currently active tracing path is now the folder we
4231 snprintf(session
->rotation_chunk
.current_rotate_path
,
4233 session
->rotation_chunk
.active_tracing_path
);
4236 session
->rotate_count
++;
4237 session
->rotate_pending
= true;
4238 session
->rotate_pending_relay_check_in_progress
= false;
4239 session
->rotate_status
= LTTNG_ROTATE_STARTED
;
4242 * Create the path name for the next chunk.
4245 if (now
== (time_t) -1) {
4246 ret
= -LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
4249 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4250 session
->current_chunk_start_ts
= now
;
4252 timeinfo
= localtime(&now
);
4253 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
4254 if (session
->kernel_session
) {
4256 * The active path for the next rotation/destroy.
4257 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4259 snprintf(session
->rotation_chunk
.active_tracing_path
,
4260 PATH_MAX
, "%s/%s-%" PRIu64
,
4261 get_base_path(session
, session
->kernel_session
->consumer
),
4262 datetime
, session
->rotate_count
+ 1);
4264 * The sub-directory for the consumer
4265 * Ex: /20170922-111754-42/kernel
4267 snprintf(session
->kernel_session
->consumer
->chunk_path
,
4268 PATH_MAX
, "/%s-%" PRIu64
"%s", datetime
,
4269 session
->rotate_count
+ 1,
4270 session
->kernel_session
->consumer
->subdir
);
4271 ret
= kernel_rotate_session(session
);
4272 if (ret
!= LTTNG_OK
) {
4276 if (session
->ust_session
) {
4277 snprintf(session
->rotation_chunk
.active_tracing_path
,
4278 PATH_MAX
, "%s/%s-%" PRIu64
,
4279 get_base_path(session
, session
->ust_session
->consumer
),
4280 datetime
, session
->rotate_count
+ 1);
4281 snprintf(session
->ust_session
->consumer
->chunk_path
,
4282 PATH_MAX
, "/%s-%" PRIu64
, datetime
,
4283 session
->rotate_count
+ 1);
4284 ret
= ust_app_rotate_session(session
);
4285 if (ret
!= LTTNG_OK
) {
4290 if (rotate_return
) {
4291 (*rotate_return
)->rotate_id
= session
->rotate_count
;
4292 (*rotate_return
)->status
= LTTNG_ROTATE_STARTED
;
4295 DBG("Cmd rotate session %s, rotate_id %" PRIu64
, session
->name
,
4296 session
->rotate_count
);
4302 if (rotate_return
) {
4303 (*rotate_return
)->status
= LTTNG_ROTATE_ERROR
;
4310 * Command LTTNG_ROTATE_PENDING from the lttng-ctl library.
4312 * Check if the session has finished its rotation.
4314 * Return 0 on success or else a LTTNG_ERR code.
4316 int cmd_rotate_pending(struct ltt_session
*session
,
4317 struct lttng_rotate_pending_return
**pending_return
,
4324 DBG("Cmd rotate pending session %s, rotate_id %" PRIu64
, session
->name
,
4325 session
->rotate_count
);
4327 *pending_return
= zmalloc(sizeof(struct lttng_rotate_pending_return
));
4328 if (!*pending_return
) {
4333 if (session
->rotate_count
!= rotate_id
) {
4334 (*pending_return
)->status
= LTTNG_ROTATE_EXPIRED
;
4339 if (session
->rotate_status
== LTTNG_ROTATE_ERROR
) {
4340 DBG("An error occurred during rotation");
4341 (*pending_return
)->status
= LTTNG_ROTATE_ERROR
;
4342 } else if (session
->rotate_status
== LTTNG_ROTATE_EMPTY
) {
4343 DBG("Nothing to rotate");
4344 (*pending_return
)->status
= LTTNG_ROTATE_EMPTY
;
4345 /* Rotate with a relay */
4346 } else if (session
->rotate_pending_relay
) {
4347 DBG("Session %s, rotate_id %" PRIu64
" still pending",
4348 session
->name
, session
->rotate_count
);
4349 (*pending_return
)->status
= LTTNG_ROTATE_STARTED
;
4350 } else if (session
->rotate_pending
) {
4351 DBG("Session %s, rotate_id %" PRIu64
" still pending",
4352 session
->name
, session
->rotate_count
);
4353 (*pending_return
)->status
= LTTNG_ROTATE_STARTED
;
4355 DBG("Session %s, rotate_id %" PRIu64
" finished",
4356 session
->name
, session
->rotate_count
);
4357 (*pending_return
)->status
= LTTNG_ROTATE_COMPLETED
;
4358 snprintf((*pending_return
)->output_path
, PATH_MAX
, "%s",
4359 session
->rotation_chunk
.current_rotate_path
);
4371 * Command LTTNG_ROTATE_SETUP from the lttng-ctl library.
4373 * Configure the automatic rotation parameters.
4375 * Return 0 on success or else a LTTNG_ERR code.
4377 int cmd_rotate_setup(struct ltt_session
*session
,
4378 uint64_t timer_us
, uint64_t size
)
4384 DBG("Cmd rotate setup session %s", session
->name
);
4386 if (timer_us
&& session
->rotate_timer_period
) {
4387 ret
= LTTNG_ERR_ROTATE_TIMER_EXISTS
;
4391 if (size
&& session
->rotate_size
) {
4392 ret
= LTTNG_ERR_ROTATE_SIZE_EXISTS
;
4396 if (timer_us
&& !session
->rotate_timer_period
) {
4397 session
->rotate_timer_period
= timer_us
;
4399 * Only start the timer if the session is active, otherwise
4400 * it will be started when the session starts.
4402 if (session
->active
) {
4403 ret
= sessiond_rotate_timer_start(session
, timer_us
);
4405 ERR("Failed to enable rotate timer");
4406 ret
= LTTNG_ERR_UNK
;
4410 } else if (timer_us
== -1ULL && session
->rotate_timer_period
> 0) {
4411 sessiond_rotate_timer_stop(session
);
4413 session
->rotate_size
= size
;
4424 * Init command subsystem.
4429 * Set network sequence index to 1 for streams to match a relayd
4430 * socket on the consumer side.
4432 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4433 relayd_net_seq_idx
= 1;
4434 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4436 DBG("Command subsystem initialized");