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-internal.h>
38 #include <common/string-utils/string-utils.h>
43 #include "health-sessiond.h"
45 #include "kernel-consumer.h"
46 #include "lttng-sessiond.h"
50 #include "buffer-registry.h"
51 #include "notification-thread.h"
52 #include "notification-thread-commands.h"
57 * Used to keep a unique index for each relayd socket created where this value
58 * is associated with streams on the consumer so it can match the right relayd
59 * to send to. It must be accessed with the relayd_net_seq_idx_lock
62 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
63 static uint64_t relayd_net_seq_idx
;
65 static int validate_ust_event_name(const char *);
66 static int cmd_enable_event_internal(struct ltt_session
*session
,
67 struct lttng_domain
*domain
,
68 char *channel_name
, struct lttng_event
*event
,
69 char *filter_expression
,
70 struct lttng_filter_bytecode
*filter
,
71 struct lttng_event_exclusion
*exclusion
,
75 * Create a session path used by list_lttng_sessions for the case that the
76 * session consumer is on the network.
78 static int build_network_session_path(char *dst
, size_t size
,
79 struct ltt_session
*session
)
81 int ret
, kdata_port
, udata_port
;
82 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
83 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
88 memset(tmp_urls
, 0, sizeof(tmp_urls
));
89 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
91 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
93 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
94 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
95 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
98 if (session
->ust_session
&& session
->ust_session
->consumer
) {
99 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
100 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
103 if (uuri
== NULL
&& kuri
== NULL
) {
104 uri
= &session
->consumer
->dst
.net
.control
;
105 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
106 } else if (kuri
&& uuri
) {
107 ret
= uri_compare(kuri
, uuri
);
111 /* Build uuri URL string */
112 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
119 } else if (kuri
&& uuri
== NULL
) {
121 } else if (uuri
&& kuri
== NULL
) {
125 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
131 * Do we have a UST url set. If yes, this means we have both kernel and UST
134 if (*tmp_uurl
!= '\0') {
135 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
136 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
139 if (kuri
|| (!kuri
&& !uuri
)) {
142 /* No kernel URI, use the UST port. */
145 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
153 * Get run-time attributes if the session has been started (discarded events,
156 static int get_kernel_runtime_stats(struct ltt_session
*session
,
157 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
158 uint64_t *lost_packets
)
162 if (!session
->has_been_started
) {
164 *discarded_events
= 0;
169 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
170 session
->kernel_session
->consumer
,
176 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
177 session
->kernel_session
->consumer
,
188 * Get run-time attributes if the session has been started (discarded events,
191 static int get_ust_runtime_stats(struct ltt_session
*session
,
192 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
193 uint64_t *lost_packets
)
196 struct ltt_ust_session
*usess
;
198 if (!discarded_events
|| !lost_packets
) {
203 usess
= session
->ust_session
;
204 assert(discarded_events
);
205 assert(lost_packets
);
207 if (!usess
|| !session
->has_been_started
) {
208 *discarded_events
= 0;
214 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
215 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
216 &usess
->buffer_reg_uid_list
,
217 usess
->consumer
, uchan
->id
,
218 uchan
->attr
.overwrite
,
221 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
222 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
223 uchan
, usess
->consumer
,
224 uchan
->attr
.overwrite
,
230 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
231 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
233 ERR("Unsupported buffer type");
244 * Fill lttng_channel array of all channels.
246 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
247 struct ltt_session
*session
, struct lttng_channel
*channels
,
248 struct lttng_channel_extended
*chan_exts
)
251 struct ltt_kernel_channel
*kchan
;
253 DBG("Listing channels for session %s", session
->name
);
256 case LTTNG_DOMAIN_KERNEL
:
257 /* Kernel channels */
258 if (session
->kernel_session
!= NULL
) {
259 cds_list_for_each_entry(kchan
,
260 &session
->kernel_session
->channel_list
.head
, list
) {
261 uint64_t discarded_events
, lost_packets
;
262 struct lttng_channel_extended
*extended
;
264 extended
= (struct lttng_channel_extended
*)
265 kchan
->channel
->attr
.extended
.ptr
;
267 ret
= get_kernel_runtime_stats(session
, kchan
,
268 &discarded_events
, &lost_packets
);
272 /* Copy lttng_channel struct to array */
273 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
274 channels
[i
].enabled
= kchan
->enabled
;
275 chan_exts
[i
].discarded_events
=
277 chan_exts
[i
].lost_packets
= lost_packets
;
278 chan_exts
[i
].monitor_timer_interval
=
279 extended
->monitor_timer_interval
;
280 chan_exts
[i
].blocking_timeout
= 0;
285 case LTTNG_DOMAIN_UST
:
287 struct lttng_ht_iter iter
;
288 struct ltt_ust_channel
*uchan
;
291 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
292 &iter
.iter
, uchan
, node
.node
) {
293 uint64_t discarded_events
= 0, lost_packets
= 0;
295 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
296 LTTNG_SYMBOL_NAME_LEN
)) {
299 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
300 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
301 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
302 channels
[i
].attr
.switch_timer_interval
=
303 uchan
->attr
.switch_timer_interval
;
304 channels
[i
].attr
.read_timer_interval
=
305 uchan
->attr
.read_timer_interval
;
306 channels
[i
].enabled
= uchan
->enabled
;
307 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
308 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
311 * Map enum lttng_ust_output to enum lttng_event_output.
313 switch (uchan
->attr
.output
) {
315 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
319 * LTTNG_UST_MMAP is the only supported UST
326 chan_exts
[i
].monitor_timer_interval
=
327 uchan
->monitor_timer_interval
;
328 chan_exts
[i
].blocking_timeout
=
329 uchan
->attr
.u
.s
.blocking_timeout
;
331 ret
= get_ust_runtime_stats(session
, uchan
,
332 &discarded_events
, &lost_packets
);
336 chan_exts
[i
].discarded_events
= discarded_events
;
337 chan_exts
[i
].lost_packets
= lost_packets
;
349 return -LTTNG_ERR_FATAL
;
355 static void increment_extended_len(const char *filter_expression
,
356 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
358 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
360 if (filter_expression
) {
361 *extended_len
+= strlen(filter_expression
) + 1;
365 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
369 static void append_extended_info(const char *filter_expression
,
370 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
372 struct lttcomm_event_extended_header extended_header
;
373 size_t filter_len
= 0;
374 size_t nb_exclusions
= 0;
376 if (filter_expression
) {
377 filter_len
= strlen(filter_expression
) + 1;
381 nb_exclusions
= exclusion
->count
;
384 /* Set header fields */
385 extended_header
.filter_len
= filter_len
;
386 extended_header
.nb_exclusions
= nb_exclusions
;
389 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
390 *extended_at
+= sizeof(extended_header
);
392 /* Copy filter string */
393 if (filter_expression
) {
394 memcpy(*extended_at
, filter_expression
, filter_len
);
395 *extended_at
+= filter_len
;
398 /* Copy exclusion names */
400 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
402 memcpy(*extended_at
, &exclusion
->names
, len
);
408 * Create a list of agent domain events.
410 * Return number of events in list on success or else a negative value.
412 static int list_lttng_agent_events(struct agent
*agt
,
413 struct lttng_event
**events
, size_t *total_size
)
416 unsigned int nb_event
= 0;
417 struct agent_event
*event
;
418 struct lttng_event
*tmp_events
;
419 struct lttng_ht_iter iter
;
420 size_t extended_len
= 0;
426 DBG3("Listing agent events");
429 nb_event
= lttng_ht_get_count(agt
->events
);
437 /* Compute required extended infos size */
438 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
441 * This is only valid because the commands which add events are
442 * processed in the same thread as the listing.
445 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
446 increment_extended_len(event
->filter_expression
, NULL
,
451 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
452 tmp_events
= zmalloc(*total_size
);
454 PERROR("zmalloc agent events session");
455 ret
= -LTTNG_ERR_FATAL
;
459 extended_at
= ((uint8_t *) tmp_events
) +
460 nb_event
* sizeof(struct lttng_event
);
463 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
464 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
465 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
466 tmp_events
[i
].enabled
= event
->enabled
;
467 tmp_events
[i
].loglevel
= event
->loglevel_value
;
468 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
471 /* Append extended info */
472 append_extended_info(event
->filter_expression
, NULL
,
477 *events
= tmp_events
;
481 assert(nb_event
== i
);
486 * Create a list of ust global domain events.
488 static int list_lttng_ust_global_events(char *channel_name
,
489 struct ltt_ust_domain_global
*ust_global
,
490 struct lttng_event
**events
, size_t *total_size
)
493 unsigned int nb_event
= 0;
494 struct lttng_ht_iter iter
;
495 struct lttng_ht_node_str
*node
;
496 struct ltt_ust_channel
*uchan
;
497 struct ltt_ust_event
*uevent
;
498 struct lttng_event
*tmp
;
499 size_t extended_len
= 0;
502 DBG("Listing UST global events for channel %s", channel_name
);
506 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
507 node
= lttng_ht_iter_get_node_str(&iter
);
509 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
513 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
515 nb_event
= lttng_ht_get_count(uchan
->events
);
522 DBG3("Listing UST global %d events", nb_event
);
524 /* Compute required extended infos size */
525 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
526 if (uevent
->internal
) {
531 increment_extended_len(uevent
->filter_expression
,
532 uevent
->exclusion
, &extended_len
);
535 /* All events are internal, skip. */
541 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
542 tmp
= zmalloc(*total_size
);
544 ret
= -LTTNG_ERR_FATAL
;
548 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
550 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
551 if (uevent
->internal
) {
552 /* This event should remain hidden from clients */
555 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
556 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
557 tmp
[i
].enabled
= uevent
->enabled
;
559 switch (uevent
->attr
.instrumentation
) {
560 case LTTNG_UST_TRACEPOINT
:
561 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
563 case LTTNG_UST_PROBE
:
564 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
566 case LTTNG_UST_FUNCTION
:
567 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
571 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
572 switch (uevent
->attr
.loglevel_type
) {
573 case LTTNG_UST_LOGLEVEL_ALL
:
574 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
576 case LTTNG_UST_LOGLEVEL_RANGE
:
577 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
579 case LTTNG_UST_LOGLEVEL_SINGLE
:
580 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
583 if (uevent
->filter
) {
586 if (uevent
->exclusion
) {
587 tmp
[i
].exclusion
= 1;
591 /* Append extended info */
592 append_extended_info(uevent
->filter_expression
,
593 uevent
->exclusion
, &extended_at
);
604 * Fill lttng_event array of all kernel events in the channel.
606 static int list_lttng_kernel_events(char *channel_name
,
607 struct ltt_kernel_session
*kernel_session
,
608 struct lttng_event
**events
, size_t *total_size
)
611 unsigned int nb_event
;
612 struct ltt_kernel_event
*event
;
613 struct ltt_kernel_channel
*kchan
;
614 size_t extended_len
= 0;
617 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
619 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
623 nb_event
= kchan
->event_count
;
625 DBG("Listing events for channel %s", kchan
->channel
->name
);
633 /* Compute required extended infos size */
634 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
635 increment_extended_len(event
->filter_expression
, NULL
,
639 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
640 *events
= zmalloc(*total_size
);
641 if (*events
== NULL
) {
642 ret
= LTTNG_ERR_FATAL
;
646 extended_at
= ((void *) *events
) +
647 nb_event
* sizeof(struct lttng_event
);
649 /* Kernel channels */
650 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
651 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
652 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
653 (*events
)[i
].enabled
= event
->enabled
;
654 (*events
)[i
].filter
=
655 (unsigned char) !!event
->filter_expression
;
657 switch (event
->event
->instrumentation
) {
658 case LTTNG_KERNEL_TRACEPOINT
:
659 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
661 case LTTNG_KERNEL_KRETPROBE
:
662 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
663 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
664 sizeof(struct lttng_kernel_kprobe
));
666 case LTTNG_KERNEL_KPROBE
:
667 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
668 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
669 sizeof(struct lttng_kernel_kprobe
));
671 case LTTNG_KERNEL_FUNCTION
:
672 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
673 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
674 sizeof(struct lttng_kernel_function
));
676 case LTTNG_KERNEL_NOOP
:
677 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
679 case LTTNG_KERNEL_SYSCALL
:
680 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
682 case LTTNG_KERNEL_ALL
:
688 /* Append extended info */
689 append_extended_info(event
->filter_expression
, NULL
,
697 /* Negate the error code to differentiate the size from an error */
702 * Add URI so the consumer output object. Set the correct path depending on the
703 * domain adding the default trace directory.
705 static int add_uri_to_consumer(struct consumer_output
*consumer
,
706 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
707 const char *session_name
)
710 const char *default_trace_dir
;
714 if (consumer
== NULL
) {
715 DBG("No consumer detected. Don't add URI. Stopping.");
716 ret
= LTTNG_ERR_NO_CONSUMER
;
721 case LTTNG_DOMAIN_KERNEL
:
722 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
724 case LTTNG_DOMAIN_UST
:
725 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
729 * This case is possible is we try to add the URI to the global tracing
730 * session consumer object which in this case there is no subdir.
732 default_trace_dir
= "";
735 switch (uri
->dtype
) {
738 DBG2("Setting network URI to consumer");
740 if (consumer
->type
== CONSUMER_DST_NET
) {
741 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
742 consumer
->dst
.net
.control_isset
) ||
743 (uri
->stype
== LTTNG_STREAM_DATA
&&
744 consumer
->dst
.net
.data_isset
)) {
745 ret
= LTTNG_ERR_URL_EXIST
;
749 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
752 consumer
->type
= CONSUMER_DST_NET
;
754 /* Set URI into consumer output object */
755 ret
= consumer_set_network_uri(consumer
, uri
);
759 } else if (ret
== 1) {
761 * URI was the same in the consumer so we do not append the subdir
762 * again so to not duplicate output dir.
768 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
769 ret
= consumer_set_subdir(consumer
, session_name
);
771 ret
= LTTNG_ERR_FATAL
;
776 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
777 /* On a new subdir, reappend the default trace dir. */
778 strncat(consumer
->subdir
, default_trace_dir
,
779 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
780 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
785 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
786 memset(consumer
->dst
.trace_path
, 0,
787 sizeof(consumer
->dst
.trace_path
));
788 /* Explicit length checks for strcpy and strcat. */
789 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
790 >= sizeof(consumer
->dst
.trace_path
)) {
791 ret
= LTTNG_ERR_FATAL
;
794 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
795 /* Append default trace dir */
796 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
797 /* Flag consumer as local. */
798 consumer
->type
= CONSUMER_DST_LOCAL
;
809 * Init tracing by creating trace directory and sending fds kernel consumer.
811 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
814 struct lttng_ht_iter iter
;
815 struct consumer_socket
*socket
;
821 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
822 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
824 pthread_mutex_lock(socket
->lock
);
825 ret
= kernel_consumer_send_session(socket
, session
);
826 pthread_mutex_unlock(socket
->lock
);
828 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
840 * Create a socket to the relayd using the URI.
842 * On success, the relayd_sock pointer is set to the created socket.
843 * Else, it's stays untouched and a lttcomm error code is returned.
845 static int create_connect_relayd(struct lttng_uri
*uri
,
846 struct lttcomm_relayd_sock
**relayd_sock
,
847 struct consumer_output
*consumer
)
850 struct lttcomm_relayd_sock
*rsock
;
852 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
853 RELAYD_VERSION_COMM_MINOR
);
855 ret
= LTTNG_ERR_FATAL
;
860 * Connect to relayd so we can proceed with a session creation. This call
861 * can possibly block for an arbitrary amount of time to set the health
862 * state to be in poll execution.
865 ret
= relayd_connect(rsock
);
868 ERR("Unable to reach lttng-relayd");
869 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
873 /* Create socket for control stream. */
874 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
875 DBG3("Creating relayd stream socket from URI");
877 /* Check relayd version */
878 ret
= relayd_version_check(rsock
);
880 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
883 consumer
->relay_major_version
= rsock
->major
;
884 consumer
->relay_minor_version
= rsock
->minor
;
885 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
886 DBG3("Creating relayd data socket from URI");
888 /* Command is not valid */
889 ERR("Relayd invalid stream type: %d", uri
->stype
);
890 ret
= LTTNG_ERR_INVALID
;
894 *relayd_sock
= rsock
;
899 /* The returned value is not useful since we are on an error path. */
900 (void) relayd_close(rsock
);
908 * Connect to the relayd using URI and send the socket to the right consumer.
910 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
911 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
912 struct consumer_output
*consumer
,
913 struct consumer_socket
*consumer_sock
,
914 char *session_name
, char *hostname
, int session_live_timer
)
917 struct lttcomm_relayd_sock
*rsock
= NULL
;
919 /* Connect to relayd and make version check if uri is the control. */
920 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
921 if (ret
!= LTTNG_OK
) {
926 /* Set the network sequence index if not set. */
927 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
928 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
930 * Increment net_seq_idx because we are about to transfer the
931 * new relayd socket to the consumer.
932 * Assign unique key so the consumer can match streams.
934 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
935 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
938 /* Send relayd socket to consumer. */
939 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
940 relayd_uri
->stype
, session_id
,
941 session_name
, hostname
, session_live_timer
);
943 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
947 /* Flag that the corresponding socket was sent. */
948 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
949 consumer_sock
->control_sock_sent
= 1;
950 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
951 consumer_sock
->data_sock_sent
= 1;
957 * Close socket which was dup on the consumer side. The session daemon does
958 * NOT keep track of the relayd socket(s) once transfer to the consumer.
962 (void) relayd_close(rsock
);
966 if (ret
!= LTTNG_OK
) {
968 * The consumer output for this session should not be used anymore
969 * since the relayd connection failed thus making any tracing or/and
970 * streaming not usable.
972 consumer
->enabled
= 0;
978 * Send both relayd sockets to a specific consumer and domain. This is a
979 * helper function to facilitate sending the information to the consumer for a
982 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
983 unsigned int session_id
, struct consumer_output
*consumer
,
984 struct consumer_socket
*sock
, char *session_name
,
985 char *hostname
, int session_live_timer
)
992 /* Sending control relayd socket. */
993 if (!sock
->control_sock_sent
) {
994 ret
= send_consumer_relayd_socket(domain
, session_id
,
995 &consumer
->dst
.net
.control
, consumer
, sock
,
996 session_name
, hostname
, session_live_timer
);
997 if (ret
!= LTTNG_OK
) {
1002 /* Sending data relayd socket. */
1003 if (!sock
->data_sock_sent
) {
1004 ret
= send_consumer_relayd_socket(domain
, session_id
,
1005 &consumer
->dst
.net
.data
, consumer
, sock
,
1006 session_name
, hostname
, session_live_timer
);
1007 if (ret
!= LTTNG_OK
) {
1017 * Setup relayd connections for a tracing session. First creates the socket to
1018 * the relayd and send them to the right domain consumer. Consumer type MUST be
1021 int cmd_setup_relayd(struct ltt_session
*session
)
1024 struct ltt_ust_session
*usess
;
1025 struct ltt_kernel_session
*ksess
;
1026 struct consumer_socket
*socket
;
1027 struct lttng_ht_iter iter
;
1031 usess
= session
->ust_session
;
1032 ksess
= session
->kernel_session
;
1034 DBG("Setting relayd for session %s", session
->name
);
1038 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1039 && usess
->consumer
->enabled
) {
1040 /* For each consumer socket, send relayd sockets */
1041 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1042 socket
, node
.node
) {
1043 pthread_mutex_lock(socket
->lock
);
1044 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1045 usess
->consumer
, socket
,
1046 session
->name
, session
->hostname
,
1047 session
->live_timer
);
1048 pthread_mutex_unlock(socket
->lock
);
1049 if (ret
!= LTTNG_OK
) {
1052 /* Session is now ready for network streaming. */
1053 session
->net_handle
= 1;
1055 session
->consumer
->relay_major_version
=
1056 usess
->consumer
->relay_major_version
;
1057 session
->consumer
->relay_minor_version
=
1058 usess
->consumer
->relay_minor_version
;
1061 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1062 && ksess
->consumer
->enabled
) {
1063 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1064 socket
, node
.node
) {
1065 pthread_mutex_lock(socket
->lock
);
1066 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1067 ksess
->consumer
, socket
,
1068 session
->name
, session
->hostname
,
1069 session
->live_timer
);
1070 pthread_mutex_unlock(socket
->lock
);
1071 if (ret
!= LTTNG_OK
) {
1074 /* Session is now ready for network streaming. */
1075 session
->net_handle
= 1;
1077 session
->consumer
->relay_major_version
=
1078 ksess
->consumer
->relay_major_version
;
1079 session
->consumer
->relay_minor_version
=
1080 ksess
->consumer
->relay_minor_version
;
1089 * Start a kernel session by opening all necessary streams.
1091 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1094 struct ltt_kernel_channel
*kchan
;
1096 /* Open kernel metadata */
1097 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1098 ret
= kernel_open_metadata(ksess
);
1100 ret
= LTTNG_ERR_KERN_META_FAIL
;
1105 /* Open kernel metadata stream */
1106 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1107 ret
= kernel_open_metadata_stream(ksess
);
1109 ERR("Kernel create metadata stream failed");
1110 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1115 /* For each channel */
1116 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1117 if (kchan
->stream_count
== 0) {
1118 ret
= kernel_open_channel_stream(kchan
);
1120 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1123 /* Update the stream global counter */
1124 ksess
->stream_count_global
+= ret
;
1128 /* Setup kernel consumer socket and send fds to it */
1129 ret
= init_kernel_tracing(ksess
);
1131 ret
= LTTNG_ERR_KERN_START_FAIL
;
1135 /* This start the kernel tracing */
1136 ret
= kernel_start_session(ksess
);
1138 ret
= LTTNG_ERR_KERN_START_FAIL
;
1142 /* Quiescent wait after starting trace */
1143 kernel_wait_quiescent(kernel_tracer_fd
);
1154 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1156 int cmd_disable_channel(struct ltt_session
*session
,
1157 enum lttng_domain_type domain
, char *channel_name
)
1160 struct ltt_ust_session
*usess
;
1162 usess
= session
->ust_session
;
1167 case LTTNG_DOMAIN_KERNEL
:
1169 ret
= channel_kernel_disable(session
->kernel_session
,
1171 if (ret
!= LTTNG_OK
) {
1175 kernel_wait_quiescent(kernel_tracer_fd
);
1178 case LTTNG_DOMAIN_UST
:
1180 struct ltt_ust_channel
*uchan
;
1181 struct lttng_ht
*chan_ht
;
1183 chan_ht
= usess
->domain_global
.channels
;
1185 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1186 if (uchan
== NULL
) {
1187 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1191 ret
= channel_ust_disable(usess
, uchan
);
1192 if (ret
!= LTTNG_OK
) {
1198 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1210 * Command LTTNG_TRACK_PID processed by the client thread.
1212 * Called with session lock held.
1214 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1222 case LTTNG_DOMAIN_KERNEL
:
1224 struct ltt_kernel_session
*ksess
;
1226 ksess
= session
->kernel_session
;
1228 ret
= kernel_track_pid(ksess
, pid
);
1229 if (ret
!= LTTNG_OK
) {
1233 kernel_wait_quiescent(kernel_tracer_fd
);
1236 case LTTNG_DOMAIN_UST
:
1238 struct ltt_ust_session
*usess
;
1240 usess
= session
->ust_session
;
1242 ret
= trace_ust_track_pid(usess
, pid
);
1243 if (ret
!= LTTNG_OK
) {
1249 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1261 * Command LTTNG_UNTRACK_PID processed by the client thread.
1263 * Called with session lock held.
1265 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1273 case LTTNG_DOMAIN_KERNEL
:
1275 struct ltt_kernel_session
*ksess
;
1277 ksess
= session
->kernel_session
;
1279 ret
= kernel_untrack_pid(ksess
, pid
);
1280 if (ret
!= LTTNG_OK
) {
1284 kernel_wait_quiescent(kernel_tracer_fd
);
1287 case LTTNG_DOMAIN_UST
:
1289 struct ltt_ust_session
*usess
;
1291 usess
= session
->ust_session
;
1293 ret
= trace_ust_untrack_pid(usess
, pid
);
1294 if (ret
!= LTTNG_OK
) {
1300 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1312 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1314 * The wpipe arguments is used as a notifier for the kernel thread.
1316 int cmd_enable_channel(struct ltt_session
*session
,
1317 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1320 struct ltt_ust_session
*usess
= session
->ust_session
;
1321 struct lttng_ht
*chan_ht
;
1328 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1330 /* Validate channel name */
1331 if (attr
->name
[0] == '.' ||
1332 memchr(attr
->name
, '/', len
) != NULL
) {
1333 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1337 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1342 * Don't try to enable a channel if the session has been started at
1343 * some point in time before. The tracer does not allow it.
1345 if (session
->has_been_started
) {
1346 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1351 * If the session is a live session, remove the switch timer, the
1352 * live timer does the same thing but sends also synchronisation
1353 * beacons for inactive streams.
1355 if (session
->live_timer
> 0) {
1356 attr
->attr
.live_timer_interval
= session
->live_timer
;
1357 attr
->attr
.switch_timer_interval
= 0;
1360 switch (domain
->type
) {
1361 case LTTNG_DOMAIN_KERNEL
:
1363 struct ltt_kernel_channel
*kchan
;
1365 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1366 session
->kernel_session
);
1367 if (kchan
== NULL
) {
1368 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1369 if (attr
->name
[0] != '\0') {
1370 session
->kernel_session
->has_non_default_channel
= 1;
1373 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1376 if (ret
!= LTTNG_OK
) {
1380 kernel_wait_quiescent(kernel_tracer_fd
);
1383 case LTTNG_DOMAIN_UST
:
1384 case LTTNG_DOMAIN_JUL
:
1385 case LTTNG_DOMAIN_LOG4J
:
1386 case LTTNG_DOMAIN_PYTHON
:
1388 struct ltt_ust_channel
*uchan
;
1393 * Current agent implementation limitations force us to allow
1394 * only one channel at once in "agent" subdomains. Each
1395 * subdomain has a default channel name which must be strictly
1398 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1399 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1400 LTTNG_SYMBOL_NAME_LEN
)) {
1401 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1404 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1405 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1406 LTTNG_SYMBOL_NAME_LEN
)) {
1407 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1410 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1411 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1412 LTTNG_SYMBOL_NAME_LEN
)) {
1413 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1418 chan_ht
= usess
->domain_global
.channels
;
1420 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1421 if (uchan
== NULL
) {
1422 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1423 if (attr
->name
[0] != '\0') {
1424 usess
->has_non_default_channel
= 1;
1427 ret
= channel_ust_enable(usess
, uchan
);
1432 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1443 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1445 int cmd_disable_event(struct ltt_session
*session
,
1446 enum lttng_domain_type domain
, char *channel_name
,
1447 struct lttng_event
*event
)
1452 DBG("Disable event command for event \'%s\'", event
->name
);
1454 event_name
= event
->name
;
1456 /* Error out on unhandled search criteria */
1457 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1458 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1459 ret
= LTTNG_ERR_UNK
;
1466 case LTTNG_DOMAIN_KERNEL
:
1468 struct ltt_kernel_channel
*kchan
;
1469 struct ltt_kernel_session
*ksess
;
1471 ksess
= session
->kernel_session
;
1474 * If a non-default channel has been created in the
1475 * session, explicitely require that -c chan_name needs
1478 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1479 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1483 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1484 if (kchan
== NULL
) {
1485 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1489 switch (event
->type
) {
1490 case LTTNG_EVENT_ALL
:
1491 case LTTNG_EVENT_TRACEPOINT
:
1492 case LTTNG_EVENT_SYSCALL
:
1493 case LTTNG_EVENT_PROBE
:
1494 case LTTNG_EVENT_FUNCTION
:
1495 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1496 if (event_name
[0] == '\0') {
1497 ret
= event_kernel_disable_event(kchan
,
1500 ret
= event_kernel_disable_event(kchan
,
1501 event_name
, event
->type
);
1503 if (ret
!= LTTNG_OK
) {
1508 ret
= LTTNG_ERR_UNK
;
1512 kernel_wait_quiescent(kernel_tracer_fd
);
1515 case LTTNG_DOMAIN_UST
:
1517 struct ltt_ust_channel
*uchan
;
1518 struct ltt_ust_session
*usess
;
1520 usess
= session
->ust_session
;
1522 if (validate_ust_event_name(event_name
)) {
1523 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1528 * If a non-default channel has been created in the
1529 * session, explicitly require that -c chan_name needs
1532 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1533 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1537 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1539 if (uchan
== NULL
) {
1540 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1544 switch (event
->type
) {
1545 case LTTNG_EVENT_ALL
:
1547 * An empty event name means that everything
1548 * should be disabled.
1550 if (event
->name
[0] == '\0') {
1551 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1553 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1556 if (ret
!= LTTNG_OK
) {
1561 ret
= LTTNG_ERR_UNK
;
1565 DBG3("Disable UST event %s in channel %s completed", event_name
,
1569 case LTTNG_DOMAIN_LOG4J
:
1570 case LTTNG_DOMAIN_JUL
:
1571 case LTTNG_DOMAIN_PYTHON
:
1574 struct ltt_ust_session
*usess
= session
->ust_session
;
1578 switch (event
->type
) {
1579 case LTTNG_EVENT_ALL
:
1582 ret
= LTTNG_ERR_UNK
;
1586 agt
= trace_ust_find_agent(usess
, domain
);
1588 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1592 * An empty event name means that everything
1593 * should be disabled.
1595 if (event
->name
[0] == '\0') {
1596 ret
= event_agent_disable_all(usess
, agt
);
1598 ret
= event_agent_disable(usess
, agt
, event_name
);
1600 if (ret
!= LTTNG_OK
) {
1607 ret
= LTTNG_ERR_UND
;
1620 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1622 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1623 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1625 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1626 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1628 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1629 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1630 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1634 case LTTNG_DOMAIN_KERNEL
:
1635 assert(session
->kernel_session
);
1637 if (session
->kernel_session
->channel_count
== 0) {
1638 /* Create default channel */
1639 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1640 if (ret
!= LTTNG_OK
) {
1643 chan_kern_created
= 1;
1645 /* Add kernel context to kernel tracer */
1646 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1647 if (ret
!= LTTNG_OK
) {
1651 case LTTNG_DOMAIN_JUL
:
1652 case LTTNG_DOMAIN_LOG4J
:
1655 * Validate channel name.
1656 * If no channel name is given and the domain is JUL or LOG4J,
1657 * set it to the appropriate domain-specific channel name. If
1658 * a name is provided but does not match the expexted channel
1659 * name, return an error.
1661 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1662 strcmp(channel_name
,
1663 DEFAULT_JUL_CHANNEL_NAME
)) {
1664 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1666 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1667 strcmp(channel_name
,
1668 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1669 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1672 /* break is _not_ missing here. */
1674 case LTTNG_DOMAIN_UST
:
1676 struct ltt_ust_session
*usess
= session
->ust_session
;
1677 unsigned int chan_count
;
1681 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1682 if (chan_count
== 0) {
1683 struct lttng_channel
*attr
;
1684 /* Create default channel */
1685 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1687 ret
= LTTNG_ERR_FATAL
;
1691 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1692 if (ret
!= LTTNG_OK
) {
1696 channel_attr_destroy(attr
);
1697 chan_ust_created
= 1;
1700 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1701 free(app_ctx_provider_name
);
1703 app_ctx_name
= NULL
;
1704 app_ctx_provider_name
= NULL
;
1705 if (ret
!= LTTNG_OK
) {
1711 ret
= LTTNG_ERR_UND
;
1719 if (chan_kern_created
) {
1720 struct ltt_kernel_channel
*kchan
=
1721 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1722 session
->kernel_session
);
1723 /* Created previously, this should NOT fail. */
1725 kernel_destroy_channel(kchan
);
1728 if (chan_ust_created
) {
1729 struct ltt_ust_channel
*uchan
=
1730 trace_ust_find_channel_by_name(
1731 session
->ust_session
->domain_global
.channels
,
1732 DEFAULT_CHANNEL_NAME
);
1733 /* Created previously, this should NOT fail. */
1735 /* Remove from the channel list of the session. */
1736 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1738 trace_ust_destroy_channel(uchan
);
1741 free(app_ctx_provider_name
);
1746 static inline bool name_starts_with(const char *name
, const char *prefix
)
1748 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1750 return !strncmp(name
, prefix
, max_cmp_len
);
1753 /* Perform userspace-specific event name validation */
1754 static int validate_ust_event_name(const char *name
)
1764 * Check name against all internal UST event component namespaces used
1767 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1768 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1769 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1778 * Internal version of cmd_enable_event() with a supplemental
1779 * "internal_event" flag which is used to enable internal events which should
1780 * be hidden from clients. Such events are used in the agent implementation to
1781 * enable the events through which all "agent" events are funeled.
1783 static int _cmd_enable_event(struct ltt_session
*session
,
1784 struct lttng_domain
*domain
,
1785 char *channel_name
, struct lttng_event
*event
,
1786 char *filter_expression
,
1787 struct lttng_filter_bytecode
*filter
,
1788 struct lttng_event_exclusion
*exclusion
,
1789 int wpipe
, bool internal_event
)
1791 int ret
= 0, channel_created
= 0;
1792 struct lttng_channel
*attr
= NULL
;
1796 assert(channel_name
);
1798 /* If we have a filter, we must have its filter expression */
1799 assert(!(!!filter_expression
^ !!filter
));
1801 /* Normalize event name as a globbing pattern */
1802 strutils_normalize_star_glob_pattern(event
->name
);
1804 /* Normalize exclusion names as globbing patterns */
1808 for (i
= 0; i
< exclusion
->count
; i
++) {
1809 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1811 strutils_normalize_star_glob_pattern(name
);
1815 DBG("Enable event command for event \'%s\'", event
->name
);
1819 switch (domain
->type
) {
1820 case LTTNG_DOMAIN_KERNEL
:
1822 struct ltt_kernel_channel
*kchan
;
1825 * If a non-default channel has been created in the
1826 * session, explicitely require that -c chan_name needs
1829 if (session
->kernel_session
->has_non_default_channel
1830 && channel_name
[0] == '\0') {
1831 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1835 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1836 session
->kernel_session
);
1837 if (kchan
== NULL
) {
1838 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1839 LTTNG_BUFFER_GLOBAL
);
1841 ret
= LTTNG_ERR_FATAL
;
1844 if (lttng_strncpy(attr
->name
, channel_name
,
1845 sizeof(attr
->name
))) {
1846 ret
= LTTNG_ERR_INVALID
;
1850 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1851 if (ret
!= LTTNG_OK
) {
1854 channel_created
= 1;
1857 /* Get the newly created kernel channel pointer */
1858 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1859 session
->kernel_session
);
1860 if (kchan
== NULL
) {
1861 /* This sould not happen... */
1862 ret
= LTTNG_ERR_FATAL
;
1866 switch (event
->type
) {
1867 case LTTNG_EVENT_ALL
:
1869 char *filter_expression_a
= NULL
;
1870 struct lttng_filter_bytecode
*filter_a
= NULL
;
1873 * We need to duplicate filter_expression and filter,
1874 * because ownership is passed to first enable
1877 if (filter_expression
) {
1878 filter_expression_a
= strdup(filter_expression
);
1879 if (!filter_expression_a
) {
1880 ret
= LTTNG_ERR_FATAL
;
1885 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1887 free(filter_expression_a
);
1888 ret
= LTTNG_ERR_FATAL
;
1891 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1893 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1894 ret
= event_kernel_enable_event(kchan
, event
,
1895 filter_expression
, filter
);
1896 /* We have passed ownership */
1897 filter_expression
= NULL
;
1899 if (ret
!= LTTNG_OK
) {
1900 if (channel_created
) {
1901 /* Let's not leak a useless channel. */
1902 kernel_destroy_channel(kchan
);
1904 free(filter_expression_a
);
1908 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1909 ret
= event_kernel_enable_event(kchan
, event
,
1910 filter_expression_a
, filter_a
);
1911 /* We have passed ownership */
1912 filter_expression_a
= NULL
;
1914 if (ret
!= LTTNG_OK
) {
1919 case LTTNG_EVENT_PROBE
:
1920 case LTTNG_EVENT_FUNCTION
:
1921 case LTTNG_EVENT_FUNCTION_ENTRY
:
1922 case LTTNG_EVENT_TRACEPOINT
:
1923 ret
= event_kernel_enable_event(kchan
, event
,
1924 filter_expression
, filter
);
1925 /* We have passed ownership */
1926 filter_expression
= NULL
;
1928 if (ret
!= LTTNG_OK
) {
1929 if (channel_created
) {
1930 /* Let's not leak a useless channel. */
1931 kernel_destroy_channel(kchan
);
1936 case LTTNG_EVENT_SYSCALL
:
1937 ret
= event_kernel_enable_event(kchan
, event
,
1938 filter_expression
, filter
);
1939 /* We have passed ownership */
1940 filter_expression
= NULL
;
1942 if (ret
!= LTTNG_OK
) {
1947 ret
= LTTNG_ERR_UNK
;
1951 kernel_wait_quiescent(kernel_tracer_fd
);
1954 case LTTNG_DOMAIN_UST
:
1956 struct ltt_ust_channel
*uchan
;
1957 struct ltt_ust_session
*usess
= session
->ust_session
;
1962 * If a non-default channel has been created in the
1963 * session, explicitely require that -c chan_name needs
1966 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1967 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1971 /* Get channel from global UST domain */
1972 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1974 if (uchan
== NULL
) {
1975 /* Create default channel */
1976 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1977 usess
->buffer_type
);
1979 ret
= LTTNG_ERR_FATAL
;
1982 if (lttng_strncpy(attr
->name
, channel_name
,
1983 sizeof(attr
->name
))) {
1984 ret
= LTTNG_ERR_INVALID
;
1988 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1989 if (ret
!= LTTNG_OK
) {
1993 /* Get the newly created channel reference back */
1994 uchan
= trace_ust_find_channel_by_name(
1995 usess
->domain_global
.channels
, channel_name
);
1999 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2001 * Don't allow users to add UST events to channels which
2002 * are assigned to a userspace subdomain (JUL, Log4J,
2005 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2009 if (!internal_event
) {
2011 * Ensure the event name is not reserved for internal
2014 ret
= validate_ust_event_name(event
->name
);
2016 WARN("Userspace event name %s failed validation.",
2018 event
->name
: "NULL");
2019 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2024 /* At this point, the session and channel exist on the tracer */
2025 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2026 filter_expression
, filter
, exclusion
,
2028 /* We have passed ownership */
2029 filter_expression
= NULL
;
2032 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2033 goto already_enabled
;
2034 } else if (ret
!= LTTNG_OK
) {
2039 case LTTNG_DOMAIN_LOG4J
:
2040 case LTTNG_DOMAIN_JUL
:
2041 case LTTNG_DOMAIN_PYTHON
:
2043 const char *default_event_name
, *default_chan_name
;
2045 struct lttng_event uevent
;
2046 struct lttng_domain tmp_dom
;
2047 struct ltt_ust_session
*usess
= session
->ust_session
;
2051 agt
= trace_ust_find_agent(usess
, domain
->type
);
2053 agt
= agent_create(domain
->type
);
2055 ret
= LTTNG_ERR_NOMEM
;
2058 agent_add(agt
, usess
->agents
);
2061 /* Create the default tracepoint. */
2062 memset(&uevent
, 0, sizeof(uevent
));
2063 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2064 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2065 default_event_name
= event_get_default_agent_ust_name(
2067 if (!default_event_name
) {
2068 ret
= LTTNG_ERR_FATAL
;
2071 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2072 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2075 * The domain type is changed because we are about to enable the
2076 * default channel and event for the JUL domain that are hardcoded.
2077 * This happens in the UST domain.
2079 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2080 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2082 switch (domain
->type
) {
2083 case LTTNG_DOMAIN_LOG4J
:
2084 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2086 case LTTNG_DOMAIN_JUL
:
2087 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2089 case LTTNG_DOMAIN_PYTHON
:
2090 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2093 /* The switch/case we are in makes this impossible */
2098 char *filter_expression_copy
= NULL
;
2099 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2102 const size_t filter_size
= sizeof(
2103 struct lttng_filter_bytecode
)
2106 filter_copy
= zmalloc(filter_size
);
2108 ret
= LTTNG_ERR_NOMEM
;
2111 memcpy(filter_copy
, filter
, filter_size
);
2113 filter_expression_copy
=
2114 strdup(filter_expression
);
2115 if (!filter_expression
) {
2116 ret
= LTTNG_ERR_NOMEM
;
2119 if (!filter_expression_copy
|| !filter_copy
) {
2120 free(filter_expression_copy
);
2126 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2127 (char *) default_chan_name
,
2128 &uevent
, filter_expression_copy
,
2129 filter_copy
, NULL
, wpipe
);
2132 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2133 goto already_enabled
;
2134 } else if (ret
!= LTTNG_OK
) {
2138 /* The wild card * means that everything should be enabled. */
2139 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2140 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2143 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2147 filter_expression
= NULL
;
2148 if (ret
!= LTTNG_OK
) {
2155 ret
= LTTNG_ERR_UND
;
2163 free(filter_expression
);
2166 channel_attr_destroy(attr
);
2172 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2173 * We own filter, exclusion, and filter_expression.
2175 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2176 char *channel_name
, struct lttng_event
*event
,
2177 char *filter_expression
,
2178 struct lttng_filter_bytecode
*filter
,
2179 struct lttng_event_exclusion
*exclusion
,
2182 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2183 filter_expression
, filter
, exclusion
, wpipe
, false);
2187 * Enable an event which is internal to LTTng. An internal should
2188 * never be made visible to clients and are immune to checks such as
2191 static int cmd_enable_event_internal(struct ltt_session
*session
,
2192 struct lttng_domain
*domain
,
2193 char *channel_name
, struct lttng_event
*event
,
2194 char *filter_expression
,
2195 struct lttng_filter_bytecode
*filter
,
2196 struct lttng_event_exclusion
*exclusion
,
2199 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2200 filter_expression
, filter
, exclusion
, wpipe
, true);
2204 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2206 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2207 struct lttng_event
**events
)
2210 ssize_t nb_events
= 0;
2213 case LTTNG_DOMAIN_KERNEL
:
2214 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2215 if (nb_events
< 0) {
2216 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2220 case LTTNG_DOMAIN_UST
:
2221 nb_events
= ust_app_list_events(events
);
2222 if (nb_events
< 0) {
2223 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2227 case LTTNG_DOMAIN_LOG4J
:
2228 case LTTNG_DOMAIN_JUL
:
2229 case LTTNG_DOMAIN_PYTHON
:
2230 nb_events
= agent_list_events(events
, domain
);
2231 if (nb_events
< 0) {
2232 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2237 ret
= LTTNG_ERR_UND
;
2244 /* Return negative value to differentiate return code */
2249 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2251 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2252 struct lttng_event_field
**fields
)
2255 ssize_t nb_fields
= 0;
2258 case LTTNG_DOMAIN_UST
:
2259 nb_fields
= ust_app_list_event_fields(fields
);
2260 if (nb_fields
< 0) {
2261 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2265 case LTTNG_DOMAIN_KERNEL
:
2266 default: /* fall-through */
2267 ret
= LTTNG_ERR_UND
;
2274 /* Return negative value to differentiate return code */
2278 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2280 return syscall_table_list(events
);
2284 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2286 * Called with session lock held.
2288 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2289 enum lttng_domain_type domain
, int32_t **pids
)
2292 ssize_t nr_pids
= 0;
2295 case LTTNG_DOMAIN_KERNEL
:
2297 struct ltt_kernel_session
*ksess
;
2299 ksess
= session
->kernel_session
;
2300 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2302 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2307 case LTTNG_DOMAIN_UST
:
2309 struct ltt_ust_session
*usess
;
2311 usess
= session
->ust_session
;
2312 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2314 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2319 case LTTNG_DOMAIN_LOG4J
:
2320 case LTTNG_DOMAIN_JUL
:
2321 case LTTNG_DOMAIN_PYTHON
:
2323 ret
= LTTNG_ERR_UND
;
2330 /* Return negative value to differentiate return code */
2335 * Command LTTNG_START_TRACE processed by the client thread.
2337 * Called with session mutex held.
2339 int cmd_start_trace(struct ltt_session
*session
)
2342 unsigned long nb_chan
= 0;
2343 struct ltt_kernel_session
*ksession
;
2344 struct ltt_ust_session
*usess
;
2348 /* Ease our life a bit ;) */
2349 ksession
= session
->kernel_session
;
2350 usess
= session
->ust_session
;
2352 /* Is the session already started? */
2353 if (session
->active
) {
2354 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2359 * Starting a session without channel is useless since after that it's not
2360 * possible to enable channel thus inform the client.
2362 if (usess
&& usess
->domain_global
.channels
) {
2363 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2366 nb_chan
+= ksession
->channel_count
;
2369 ret
= LTTNG_ERR_NO_CHANNEL
;
2373 /* Kernel tracing */
2374 if (ksession
!= NULL
) {
2375 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2376 if (ret
!= LTTNG_OK
) {
2381 /* Flag session that trace should start automatically */
2384 * Even though the start trace might fail, flag this session active so
2385 * other application coming in are started by default.
2389 ret
= ust_app_start_trace_all(usess
);
2391 ret
= LTTNG_ERR_UST_START_FAIL
;
2396 /* Flag this after a successful start. */
2397 session
->has_been_started
= 1;
2398 session
->active
= 1;
2407 * Command LTTNG_STOP_TRACE processed by the client thread.
2409 int cmd_stop_trace(struct ltt_session
*session
)
2412 struct ltt_kernel_channel
*kchan
;
2413 struct ltt_kernel_session
*ksession
;
2414 struct ltt_ust_session
*usess
;
2419 ksession
= session
->kernel_session
;
2420 usess
= session
->ust_session
;
2422 /* Session is not active. Skip everythong and inform the client. */
2423 if (!session
->active
) {
2424 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2429 if (ksession
&& ksession
->active
) {
2430 DBG("Stop kernel tracing");
2432 ret
= kernel_stop_session(ksession
);
2434 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2438 kernel_wait_quiescent(kernel_tracer_fd
);
2440 /* Flush metadata after stopping (if exists) */
2441 if (ksession
->metadata_stream_fd
>= 0) {
2442 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2444 ERR("Kernel metadata flush failed");
2448 /* Flush all buffers after stopping */
2449 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2450 ret
= kernel_flush_buffer(kchan
);
2452 ERR("Kernel flush buffer error");
2456 ksession
->active
= 0;
2459 if (usess
&& usess
->active
) {
2461 * Even though the stop trace might fail, flag this session inactive so
2462 * other application coming in are not started by default.
2466 ret
= ust_app_stop_trace_all(usess
);
2468 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2473 /* Flag inactive after a successful stop. */
2474 session
->active
= 0;
2482 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2484 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2485 struct lttng_uri
*uris
)
2488 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2489 struct ltt_ust_session
*usess
= session
->ust_session
;
2495 /* Can't set consumer URI if the session is active. */
2496 if (session
->active
) {
2497 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2501 /* Set the "global" consumer URIs */
2502 for (i
= 0; i
< nb_uri
; i
++) {
2503 ret
= add_uri_to_consumer(session
->consumer
,
2504 &uris
[i
], 0, session
->name
);
2505 if (ret
!= LTTNG_OK
) {
2510 /* Set UST session URIs */
2511 if (session
->ust_session
) {
2512 for (i
= 0; i
< nb_uri
; i
++) {
2513 ret
= add_uri_to_consumer(
2514 session
->ust_session
->consumer
,
2515 &uris
[i
], LTTNG_DOMAIN_UST
,
2517 if (ret
!= LTTNG_OK
) {
2523 /* Set kernel session URIs */
2524 if (session
->kernel_session
) {
2525 for (i
= 0; i
< nb_uri
; i
++) {
2526 ret
= add_uri_to_consumer(
2527 session
->kernel_session
->consumer
,
2528 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2530 if (ret
!= LTTNG_OK
) {
2537 * Make sure to set the session in output mode after we set URI since a
2538 * session can be created without URL (thus flagged in no output mode).
2540 session
->output_traces
= 1;
2542 ksess
->output_traces
= 1;
2546 usess
->output_traces
= 1;
2557 * Command LTTNG_CREATE_SESSION processed by the client thread.
2559 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2560 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2563 struct ltt_session
*session
;
2569 * Verify if the session already exist
2571 * XXX: There is no need for the session lock list here since the caller
2572 * (process_client_msg) is holding it. We might want to change that so a
2573 * single command does not lock the entire session list.
2575 session
= session_find_by_name(name
);
2576 if (session
!= NULL
) {
2577 ret
= LTTNG_ERR_EXIST_SESS
;
2581 /* Create tracing session in the registry */
2582 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2583 LTTNG_SOCK_GET_GID_CRED(creds
));
2584 if (ret
!= LTTNG_OK
) {
2589 * Get the newly created session pointer back
2591 * XXX: There is no need for the session lock list here since the caller
2592 * (process_client_msg) is holding it. We might want to change that so a
2593 * single command does not lock the entire session list.
2595 session
= session_find_by_name(name
);
2598 session
->live_timer
= live_timer
;
2599 /* Create default consumer output for the session not yet created. */
2600 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2601 if (session
->consumer
== NULL
) {
2602 ret
= LTTNG_ERR_FATAL
;
2603 goto consumer_error
;
2607 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2608 if (ret
!= LTTNG_OK
) {
2609 goto consumer_error
;
2611 session
->output_traces
= 1;
2613 session
->output_traces
= 0;
2614 DBG2("Session %s created with no output", session
->name
);
2617 session
->consumer
->enabled
= 1;
2622 session_destroy(session
);
2629 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2631 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2632 size_t nb_uri
, lttng_sock_cred
*creds
)
2635 struct ltt_session
*session
;
2636 struct snapshot_output
*new_output
= NULL
;
2642 * Create session in no output mode with URIs set to NULL. The uris we've
2643 * received are for a default snapshot output if one.
2645 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2646 if (ret
!= LTTNG_OK
) {
2650 /* Get the newly created session pointer back. This should NEVER fail. */
2651 session
= session_find_by_name(name
);
2654 /* Flag session for snapshot mode. */
2655 session
->snapshot_mode
= 1;
2657 /* Skip snapshot output creation if no URI is given. */
2662 new_output
= snapshot_output_alloc();
2664 ret
= LTTNG_ERR_NOMEM
;
2665 goto error_snapshot_alloc
;
2668 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2669 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2671 if (ret
== -ENOMEM
) {
2672 ret
= LTTNG_ERR_NOMEM
;
2674 ret
= LTTNG_ERR_INVALID
;
2676 goto error_snapshot
;
2680 snapshot_add_output(&session
->snapshot
, new_output
);
2687 snapshot_output_destroy(new_output
);
2688 error_snapshot_alloc
:
2689 session_destroy(session
);
2695 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2697 * Called with session lock held.
2699 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2702 struct ltt_ust_session
*usess
;
2703 struct ltt_kernel_session
*ksess
;
2708 usess
= session
->ust_session
;
2709 ksess
= session
->kernel_session
;
2711 /* Clean kernel session teardown */
2712 kernel_destroy_session(ksess
);
2714 /* UST session teardown */
2716 /* Close any relayd session */
2717 consumer_output_send_destroy_relayd(usess
->consumer
);
2719 /* Destroy every UST application related to this session. */
2720 ret
= ust_app_destroy_trace_all(usess
);
2722 ERR("Error in ust_app_destroy_trace_all");
2725 /* Clean up the rest. */
2726 trace_ust_destroy_session(usess
);
2730 * Must notify the kernel thread here to update it's poll set in order to
2731 * remove the channel(s)' fd just destroyed.
2733 ret
= notify_thread_pipe(wpipe
);
2735 PERROR("write kernel poll pipe");
2738 ret
= session_destroy(session
);
2744 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2746 int cmd_register_consumer(struct ltt_session
*session
,
2747 enum lttng_domain_type domain
, const char *sock_path
,
2748 struct consumer_data
*cdata
)
2751 struct consumer_socket
*socket
= NULL
;
2758 case LTTNG_DOMAIN_KERNEL
:
2760 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2764 /* Can't register a consumer if there is already one */
2765 if (ksess
->consumer_fds_sent
!= 0) {
2766 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2770 sock
= lttcomm_connect_unix_sock(sock_path
);
2772 ret
= LTTNG_ERR_CONNECT_FAIL
;
2775 cdata
->cmd_sock
= sock
;
2777 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2778 if (socket
== NULL
) {
2781 PERROR("close register consumer");
2783 cdata
->cmd_sock
= -1;
2784 ret
= LTTNG_ERR_FATAL
;
2788 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2789 if (socket
->lock
== NULL
) {
2790 PERROR("zmalloc pthread mutex");
2791 ret
= LTTNG_ERR_FATAL
;
2794 pthread_mutex_init(socket
->lock
, NULL
);
2795 socket
->registered
= 1;
2798 consumer_add_socket(socket
, ksess
->consumer
);
2801 pthread_mutex_lock(&cdata
->pid_mutex
);
2803 pthread_mutex_unlock(&cdata
->pid_mutex
);
2808 /* TODO: Userspace tracing */
2809 ret
= LTTNG_ERR_UND
;
2817 consumer_destroy_socket(socket
);
2823 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2825 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2826 struct lttng_domain
**domains
)
2831 struct lttng_ht_iter iter
;
2833 if (session
->kernel_session
!= NULL
) {
2834 DBG3("Listing domains found kernel domain");
2838 if (session
->ust_session
!= NULL
) {
2839 DBG3("Listing domains found UST global domain");
2843 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2845 if (agt
->being_used
) {
2856 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2857 if (*domains
== NULL
) {
2858 ret
= LTTNG_ERR_FATAL
;
2862 if (session
->kernel_session
!= NULL
) {
2863 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2865 /* Kernel session buffer type is always GLOBAL */
2866 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2871 if (session
->ust_session
!= NULL
) {
2872 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2873 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2877 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2879 if (agt
->being_used
) {
2880 (*domains
)[index
].type
= agt
->domain
;
2881 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2891 /* Return negative value to differentiate return code */
2897 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2899 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2900 struct ltt_session
*session
, struct lttng_channel
**channels
)
2902 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2905 case LTTNG_DOMAIN_KERNEL
:
2906 if (session
->kernel_session
!= NULL
) {
2907 nb_chan
= session
->kernel_session
->channel_count
;
2909 DBG3("Number of kernel channels %zd", nb_chan
);
2911 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2915 case LTTNG_DOMAIN_UST
:
2916 if (session
->ust_session
!= NULL
) {
2918 nb_chan
= lttng_ht_get_count(
2919 session
->ust_session
->domain_global
.channels
);
2922 DBG3("Number of UST global channels %zd", nb_chan
);
2924 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2929 ret
= -LTTNG_ERR_UND
;
2934 const size_t channel_size
= sizeof(struct lttng_channel
) +
2935 sizeof(struct lttng_channel_extended
);
2936 struct lttng_channel_extended
*channel_exts
;
2938 payload_size
= nb_chan
* channel_size
;
2939 *channels
= zmalloc(payload_size
);
2940 if (*channels
== NULL
) {
2941 ret
= -LTTNG_ERR_FATAL
;
2945 channel_exts
= ((void *) *channels
) +
2946 (nb_chan
* sizeof(struct lttng_channel
));
2947 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
2948 if (ret
!= LTTNG_OK
) {
2963 * Command LTTNG_LIST_EVENTS processed by the client thread.
2965 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2966 struct ltt_session
*session
, char *channel_name
,
2967 struct lttng_event
**events
, size_t *total_size
)
2970 ssize_t nb_event
= 0;
2973 case LTTNG_DOMAIN_KERNEL
:
2974 if (session
->kernel_session
!= NULL
) {
2975 nb_event
= list_lttng_kernel_events(channel_name
,
2976 session
->kernel_session
, events
,
2980 case LTTNG_DOMAIN_UST
:
2982 if (session
->ust_session
!= NULL
) {
2983 nb_event
= list_lttng_ust_global_events(channel_name
,
2984 &session
->ust_session
->domain_global
, events
,
2989 case LTTNG_DOMAIN_LOG4J
:
2990 case LTTNG_DOMAIN_JUL
:
2991 case LTTNG_DOMAIN_PYTHON
:
2992 if (session
->ust_session
) {
2993 struct lttng_ht_iter iter
;
2997 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2998 &iter
.iter
, agt
, node
.node
) {
2999 if (agt
->domain
== domain
) {
3000 nb_event
= list_lttng_agent_events(
3010 ret
= LTTNG_ERR_UND
;
3017 /* Return negative value to differentiate return code */
3022 * Using the session list, filled a lttng_session array to send back to the
3023 * client for session listing.
3025 * The session list lock MUST be acquired before calling this function. Use
3026 * session_lock_list() and session_unlock_list().
3028 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3033 struct ltt_session
*session
;
3034 struct ltt_session_list
*list
= session_get_list();
3036 DBG("Getting all available session for UID %d GID %d",
3039 * Iterate over session list and append data after the control struct in
3042 cds_list_for_each_entry(session
, &list
->head
, list
) {
3044 * Only list the sessions the user can control.
3046 if (!session_access_ok(session
, uid
, gid
)) {
3050 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3051 struct ltt_ust_session
*usess
= session
->ust_session
;
3053 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3054 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3055 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3056 ret
= build_network_session_path(sessions
[i
].path
,
3057 sizeof(sessions
[i
].path
), session
);
3059 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3060 session
->consumer
->dst
.trace_path
);
3063 PERROR("snprintf session path");
3067 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3068 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3069 sessions
[i
].enabled
= session
->active
;
3070 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3071 sessions
[i
].live_timer_interval
= session
->live_timer
;
3077 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3078 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3080 int cmd_data_pending(struct ltt_session
*session
)
3083 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3084 struct ltt_ust_session
*usess
= session
->ust_session
;
3088 /* Session MUST be stopped to ask for data availability. */
3089 if (session
->active
) {
3090 ret
= LTTNG_ERR_SESSION_STARTED
;
3094 * If stopped, just make sure we've started before else the above call
3095 * will always send that there is data pending.
3097 * The consumer assumes that when the data pending command is received,
3098 * the trace has been started before or else no output data is written
3099 * by the streams which is a condition for data pending. So, this is
3100 * *VERY* important that we don't ask the consumer before a start
3103 if (!session
->has_been_started
) {
3109 if (ksess
&& ksess
->consumer
) {
3110 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3112 /* Data is still being extracted for the kernel. */
3117 if (usess
&& usess
->consumer
) {
3118 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3120 /* Data is still being extracted for the kernel. */
3125 /* Data is ready to be read by a viewer */
3133 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3135 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3137 int cmd_snapshot_add_output(struct ltt_session
*session
,
3138 struct lttng_snapshot_output
*output
, uint32_t *id
)
3141 struct snapshot_output
*new_output
;
3146 DBG("Cmd snapshot add output for session %s", session
->name
);
3149 * Can't create an output if the session is not set in no-output mode.
3151 if (session
->output_traces
) {
3152 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3156 /* Only one output is allowed until we have the "tee" feature. */
3157 if (session
->snapshot
.nb_output
== 1) {
3158 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3162 new_output
= snapshot_output_alloc();
3164 ret
= LTTNG_ERR_NOMEM
;
3168 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3169 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3170 &session
->snapshot
);
3172 if (ret
== -ENOMEM
) {
3173 ret
= LTTNG_ERR_NOMEM
;
3175 ret
= LTTNG_ERR_INVALID
;
3181 snapshot_add_output(&session
->snapshot
, new_output
);
3183 *id
= new_output
->id
;
3190 snapshot_output_destroy(new_output
);
3196 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3198 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3200 int cmd_snapshot_del_output(struct ltt_session
*session
,
3201 struct lttng_snapshot_output
*output
)
3204 struct snapshot_output
*sout
= NULL
;
3212 * Permission denied to create an output if the session is not
3213 * set in no output mode.
3215 if (session
->output_traces
) {
3216 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3221 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3223 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3224 } else if (*output
->name
!= '\0') {
3225 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3227 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3230 ret
= LTTNG_ERR_INVALID
;
3234 snapshot_delete_output(&session
->snapshot
, sout
);
3235 snapshot_output_destroy(sout
);
3244 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3246 * If no output is available, outputs is untouched and 0 is returned.
3248 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3250 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3251 struct lttng_snapshot_output
**outputs
)
3254 struct lttng_snapshot_output
*list
= NULL
;
3255 struct lttng_ht_iter iter
;
3256 struct snapshot_output
*output
;
3261 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3264 * Permission denied to create an output if the session is not
3265 * set in no output mode.
3267 if (session
->output_traces
) {
3268 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3272 if (session
->snapshot
.nb_output
== 0) {
3277 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3279 ret
= -LTTNG_ERR_NOMEM
;
3283 /* Copy list from session to the new list object. */
3285 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3286 output
, node
.node
) {
3287 assert(output
->consumer
);
3288 list
[idx
].id
= output
->id
;
3289 list
[idx
].max_size
= output
->max_size
;
3290 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3291 sizeof(list
[idx
].name
))) {
3292 ret
= -LTTNG_ERR_INVALID
;
3295 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3296 if (lttng_strncpy(list
[idx
].ctrl_url
,
3297 output
->consumer
->dst
.trace_path
,
3298 sizeof(list
[idx
].ctrl_url
))) {
3299 ret
= -LTTNG_ERR_INVALID
;
3304 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3305 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3307 ret
= -LTTNG_ERR_NOMEM
;
3312 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3313 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3315 ret
= -LTTNG_ERR_NOMEM
;
3324 ret
= session
->snapshot
.nb_output
;
3333 * Check if we can regenerate the metadata for this session.
3334 * Only kernel, UST per-uid and non-live sessions are supported.
3336 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3339 int check_regenerate_metadata_support(struct ltt_session
*session
)
3345 if (session
->live_timer
!= 0) {
3346 ret
= LTTNG_ERR_LIVE_SESSION
;
3349 if (!session
->active
) {
3350 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3353 if (session
->ust_session
) {
3354 switch (session
->ust_session
->buffer_type
) {
3355 case LTTNG_BUFFER_PER_UID
:
3357 case LTTNG_BUFFER_PER_PID
:
3358 ret
= LTTNG_ERR_PER_PID_SESSION
;
3362 ret
= LTTNG_ERR_UNK
;
3366 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3367 session
->consumer
->relay_minor_version
< 8) {
3368 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3378 int clear_metadata_file(int fd
)
3382 ret
= lseek(fd
, 0, SEEK_SET
);
3388 ret
= ftruncate(fd
, 0);
3390 PERROR("ftruncate");
3399 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3402 struct buffer_reg_uid
*uid_reg
= NULL
;
3403 struct buffer_reg_session
*session_reg
= NULL
;
3406 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3407 struct ust_registry_session
*registry
;
3408 struct ust_registry_channel
*chan
;
3409 struct lttng_ht_iter iter_chan
;
3411 session_reg
= uid_reg
->registry
;
3412 registry
= session_reg
->reg
.ust
;
3414 pthread_mutex_lock(®istry
->lock
);
3415 registry
->metadata_len_sent
= 0;
3416 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3417 registry
->metadata_len
= 0;
3418 registry
->metadata_version
++;
3419 if (registry
->metadata_fd
> 0) {
3420 /* Clear the metadata file's content. */
3421 ret
= clear_metadata_file(registry
->metadata_fd
);
3423 pthread_mutex_unlock(®istry
->lock
);
3428 ret
= ust_metadata_session_statedump(registry
, NULL
,
3429 registry
->major
, registry
->minor
);
3431 pthread_mutex_unlock(®istry
->lock
);
3432 ERR("Failed to generate session metadata (err = %d)",
3436 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3438 struct ust_registry_event
*event
;
3439 struct lttng_ht_iter iter_event
;
3441 ret
= ust_metadata_channel_statedump(registry
, chan
);
3443 pthread_mutex_unlock(®istry
->lock
);
3444 ERR("Failed to generate channel metadata "
3448 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3450 ret
= ust_metadata_event_statedump(registry
,
3453 pthread_mutex_unlock(®istry
->lock
);
3454 ERR("Failed to generate event metadata "
3460 pthread_mutex_unlock(®istry
->lock
);
3469 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3471 * Ask the consumer to truncate the existing metadata file(s) and
3472 * then regenerate the metadata. Live and per-pid sessions are not
3473 * supported and return an error.
3475 * Return 0 on success or else a LTTNG_ERR code.
3477 int cmd_regenerate_metadata(struct ltt_session
*session
)
3483 ret
= check_regenerate_metadata_support(session
);
3488 if (session
->kernel_session
) {
3489 ret
= kernctl_session_regenerate_metadata(
3490 session
->kernel_session
->fd
);
3492 ERR("Failed to regenerate the kernel metadata");
3497 if (session
->ust_session
) {
3498 ret
= ust_regenerate_metadata(session
->ust_session
);
3500 ERR("Failed to regenerate the UST metadata");
3504 DBG("Cmd metadata regenerate for session %s", session
->name
);
3512 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3514 * Ask the tracer to regenerate a new statedump.
3516 * Return 0 on success or else a LTTNG_ERR code.
3518 int cmd_regenerate_statedump(struct ltt_session
*session
)
3524 if (!session
->active
) {
3525 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3530 if (session
->kernel_session
) {
3531 ret
= kernctl_session_regenerate_statedump(
3532 session
->kernel_session
->fd
);
3534 * Currently, the statedump in kernel can only fail if out
3538 if (ret
== -ENOMEM
) {
3539 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3541 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3543 ERR("Failed to regenerate the kernel statedump");
3548 if (session
->ust_session
) {
3549 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3551 * Currently, the statedump in UST always returns 0.
3554 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3555 ERR("Failed to regenerate the UST statedump");
3559 DBG("Cmd regenerate statedump for session %s", session
->name
);
3566 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3567 struct notification_thread_handle
*notification_thread
)
3571 ssize_t sock_recv_len
;
3572 struct lttng_trigger
*trigger
= NULL
;
3573 struct lttng_buffer_view view
;
3574 struct lttng_dynamic_buffer trigger_buffer
;
3576 lttng_dynamic_buffer_init(&trigger_buffer
);
3577 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3578 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3580 ret
= LTTNG_ERR_NOMEM
;
3584 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3586 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3587 ERR("Failed to receive \"register trigger\" command payload");
3588 /* TODO: should this be a new error enum ? */
3589 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3593 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3594 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3596 ERR("Invalid trigger payload received in \"register trigger\" command");
3597 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3601 ret
= notification_thread_command_register_trigger(notification_thread
,
3603 /* Ownership of trigger was transferred. */
3606 lttng_trigger_destroy(trigger
);
3607 lttng_dynamic_buffer_reset(&trigger_buffer
);
3611 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3612 struct notification_thread_handle
*notification_thread
)
3616 ssize_t sock_recv_len
;
3617 struct lttng_trigger
*trigger
= NULL
;
3618 struct lttng_buffer_view view
;
3619 struct lttng_dynamic_buffer trigger_buffer
;
3621 lttng_dynamic_buffer_init(&trigger_buffer
);
3622 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3623 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3625 ret
= LTTNG_ERR_NOMEM
;
3629 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3631 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3632 ERR("Failed to receive \"unregister trigger\" command payload");
3633 /* TODO: should this be a new error enum ? */
3634 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3638 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3639 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3641 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3642 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3646 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3649 lttng_trigger_destroy(trigger
);
3650 lttng_dynamic_buffer_reset(&trigger_buffer
);
3655 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3656 * snapshot output is *not* set with a remote destination.
3658 * Return 0 on success or a LTTNG_ERR code.
3660 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3661 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3664 struct lttng_ht_iter iter
;
3665 struct consumer_socket
*socket
;
3668 assert(snap_output
);
3671 DBG2("Set relayd object from snapshot output");
3673 /* Ignore if snapshot consumer output is not network. */
3674 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3679 * For each consumer socket, create and send the relayd object of the
3683 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3684 socket
, node
.node
) {
3685 ret
= send_consumer_relayd_sockets(0, session
->id
,
3686 snap_output
->consumer
, socket
,
3687 session
->name
, session
->hostname
,
3688 session
->live_timer
);
3689 if (ret
!= LTTNG_OK
) {
3701 * Record a kernel snapshot.
3703 * Return LTTNG_OK on success or a LTTNG_ERR code.
3705 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3706 struct snapshot_output
*output
, struct ltt_session
*session
,
3707 int wait
, uint64_t nb_packets_per_stream
)
3717 * Copy kernel session sockets so we can communicate with the right
3718 * consumer for the snapshot record command.
3720 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3722 ret
= LTTNG_ERR_NOMEM
;
3726 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3727 if (ret
!= LTTNG_OK
) {
3728 goto error_snapshot
;
3731 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3732 if (ret
!= LTTNG_OK
) {
3733 goto error_snapshot
;
3740 /* Clean up copied sockets so this output can use some other later on. */
3741 consumer_destroy_output_sockets(output
->consumer
);
3748 * Record a UST snapshot.
3750 * Return 0 on success or a LTTNG_ERR error code.
3752 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3753 struct snapshot_output
*output
, struct ltt_session
*session
,
3754 int wait
, uint64_t nb_packets_per_stream
)
3763 * Copy UST session sockets so we can communicate with the right
3764 * consumer for the snapshot record command.
3766 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3768 ret
= LTTNG_ERR_NOMEM
;
3772 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3773 if (ret
!= LTTNG_OK
) {
3774 goto error_snapshot
;
3777 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3781 ret
= LTTNG_ERR_INVALID
;
3784 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3787 goto error_snapshot
;
3793 /* Clean up copied sockets so this output can use some other later on. */
3794 consumer_destroy_output_sockets(output
->consumer
);
3800 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3801 uint64_t cur_nr_packets
)
3803 uint64_t tot_size
= 0;
3805 if (session
->kernel_session
) {
3806 struct ltt_kernel_channel
*chan
;
3807 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3809 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3810 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3812 * Don't take channel into account if we
3813 * already grab all its packets.
3817 tot_size
+= chan
->channel
->attr
.subbuf_size
3818 * chan
->stream_count
;
3822 if (session
->ust_session
) {
3823 struct ltt_ust_session
*usess
= session
->ust_session
;
3825 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3833 * Calculate the number of packets we can grab from each stream that
3834 * fits within the overall snapshot max size.
3836 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3837 * the number of packets per stream.
3839 * TODO: this approach is not perfect: we consider the worse case
3840 * (packet filling the sub-buffers) as an upper bound, but we could do
3841 * better if we do this calculation while we actually grab the packet
3842 * content: we would know how much padding we don't actually store into
3845 * This algorithm is currently bounded by the number of packets per
3848 * Since we call this algorithm before actually grabbing the data, it's
3849 * an approximation: for instance, applications could appear/disappear
3850 * in between this call and actually grabbing data.
3853 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3856 uint64_t cur_nb_packets
= 0;
3859 return 0; /* Infinite */
3862 size_left
= max_size
;
3864 uint64_t one_more_packet_tot_size
;
3866 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3868 if (!one_more_packet_tot_size
) {
3869 /* We are already grabbing all packets. */
3872 size_left
-= one_more_packet_tot_size
;
3873 if (size_left
< 0) {
3878 if (!cur_nb_packets
) {
3879 /* Not enough room to grab one packet of each stream, error. */
3882 return cur_nb_packets
;
3886 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3888 * The wait parameter is ignored so this call always wait for the snapshot to
3889 * complete before returning.
3891 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3893 int cmd_snapshot_record(struct ltt_session
*session
,
3894 struct lttng_snapshot_output
*output
, int wait
)
3897 unsigned int use_tmp_output
= 0;
3898 struct snapshot_output tmp_output
;
3899 unsigned int snapshot_success
= 0;
3905 DBG("Cmd snapshot record for session %s", session
->name
);
3907 /* Get the datetime for the snapshot output directory. */
3908 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3911 ret
= LTTNG_ERR_INVALID
;
3916 * Permission denied to create an output if the session is not
3917 * set in no output mode.
3919 if (session
->output_traces
) {
3920 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3924 /* The session needs to be started at least once. */
3925 if (!session
->has_been_started
) {
3926 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3930 /* Use temporary output for the session. */
3931 if (*output
->ctrl_url
!= '\0') {
3932 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3933 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3936 if (ret
== -ENOMEM
) {
3937 ret
= LTTNG_ERR_NOMEM
;
3939 ret
= LTTNG_ERR_INVALID
;
3943 /* Use the global session count for the temporary snapshot. */
3944 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3946 /* Use the global datetime */
3947 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3951 if (use_tmp_output
) {
3952 int64_t nb_packets_per_stream
;
3954 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3955 tmp_output
.max_size
);
3956 if (nb_packets_per_stream
< 0) {
3957 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3961 if (session
->kernel_session
) {
3962 ret
= record_kernel_snapshot(session
->kernel_session
,
3963 &tmp_output
, session
,
3964 wait
, nb_packets_per_stream
);
3965 if (ret
!= LTTNG_OK
) {
3970 if (session
->ust_session
) {
3971 ret
= record_ust_snapshot(session
->ust_session
,
3972 &tmp_output
, session
,
3973 wait
, nb_packets_per_stream
);
3974 if (ret
!= LTTNG_OK
) {
3979 snapshot_success
= 1;
3981 struct snapshot_output
*sout
;
3982 struct lttng_ht_iter iter
;
3985 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3986 &iter
.iter
, sout
, node
.node
) {
3987 int64_t nb_packets_per_stream
;
3990 * Make a local copy of the output and assign the possible
3991 * temporary value given by the caller.
3993 memset(&tmp_output
, 0, sizeof(tmp_output
));
3994 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3996 if (output
->max_size
!= (uint64_t) -1ULL) {
3997 tmp_output
.max_size
= output
->max_size
;
4000 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4001 tmp_output
.max_size
);
4002 if (nb_packets_per_stream
< 0) {
4003 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4008 /* Use temporary name. */
4009 if (*output
->name
!= '\0') {
4010 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4011 sizeof(tmp_output
.name
))) {
4012 ret
= LTTNG_ERR_INVALID
;
4018 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4019 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4021 if (session
->kernel_session
) {
4022 ret
= record_kernel_snapshot(session
->kernel_session
,
4023 &tmp_output
, session
,
4024 wait
, nb_packets_per_stream
);
4025 if (ret
!= LTTNG_OK
) {
4031 if (session
->ust_session
) {
4032 ret
= record_ust_snapshot(session
->ust_session
,
4033 &tmp_output
, session
,
4034 wait
, nb_packets_per_stream
);
4035 if (ret
!= LTTNG_OK
) {
4040 snapshot_success
= 1;
4045 if (snapshot_success
) {
4046 session
->snapshot
.nb_snapshot
++;
4048 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4056 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4058 int cmd_set_session_shm_path(struct ltt_session
*session
,
4059 const char *shm_path
)
4065 * Can only set shm path before session is started.
4067 if (session
->has_been_started
) {
4068 return LTTNG_ERR_SESSION_STARTED
;
4071 strncpy(session
->shm_path
, shm_path
,
4072 sizeof(session
->shm_path
));
4073 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4079 * Init command subsystem.
4084 * Set network sequence index to 1 for streams to match a relayd
4085 * socket on the consumer side.
4087 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4088 relayd_net_seq_idx
= 1;
4089 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4091 DBG("Command subsystem initialized");