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 <lttng/trigger/trigger-internal.h>
37 #include "health-sessiond.h"
39 #include "kernel-consumer.h"
40 #include "lttng-sessiond.h"
44 #include "buffer-registry.h"
49 * Used to keep a unique index for each relayd socket created where this value
50 * is associated with streams on the consumer so it can match the right relayd
51 * to send to. It must be accessed with the relayd_net_seq_idx_lock
54 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 static uint64_t relayd_net_seq_idx
;
57 static int validate_event_name(const char *);
58 static int validate_ust_event_name(const char *);
59 static int cmd_enable_event_internal(struct ltt_session
*session
,
60 struct lttng_domain
*domain
,
61 char *channel_name
, struct lttng_event
*event
,
62 char *filter_expression
,
63 struct lttng_filter_bytecode
*filter
,
64 struct lttng_event_exclusion
*exclusion
,
68 * Create a session path used by list_lttng_sessions for the case that the
69 * session consumer is on the network.
71 static int build_network_session_path(char *dst
, size_t size
,
72 struct ltt_session
*session
)
74 int ret
, kdata_port
, udata_port
;
75 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
76 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
81 memset(tmp_urls
, 0, sizeof(tmp_urls
));
82 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
84 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
86 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
87 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
88 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
91 if (session
->ust_session
&& session
->ust_session
->consumer
) {
92 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
93 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
96 if (uuri
== NULL
&& kuri
== NULL
) {
97 uri
= &session
->consumer
->dst
.net
.control
;
98 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
99 } else if (kuri
&& uuri
) {
100 ret
= uri_compare(kuri
, uuri
);
104 /* Build uuri URL string */
105 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
112 } else if (kuri
&& uuri
== NULL
) {
114 } else if (uuri
&& kuri
== NULL
) {
118 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
124 * Do we have a UST url set. If yes, this means we have both kernel and UST
127 if (*tmp_uurl
!= '\0') {
128 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
129 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
132 if (kuri
|| (!kuri
&& !uuri
)) {
135 /* No kernel URI, use the UST port. */
138 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
146 * Get run-time attributes if the session has been started (discarded events,
149 static int get_kernel_runtime_stats(struct ltt_session
*session
,
150 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
151 uint64_t *lost_packets
)
155 if (!session
->has_been_started
) {
157 *discarded_events
= 0;
162 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
163 session
->kernel_session
->consumer
,
169 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
170 session
->kernel_session
->consumer
,
181 * Get run-time attributes if the session has been started (discarded events,
184 static int get_ust_runtime_stats(struct ltt_session
*session
,
185 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
186 uint64_t *lost_packets
)
189 struct ltt_ust_session
*usess
;
191 if (!discarded_events
|| !lost_packets
) {
196 usess
= session
->ust_session
;
197 assert(discarded_events
);
198 assert(lost_packets
);
200 if (!usess
|| !session
->has_been_started
) {
201 *discarded_events
= 0;
207 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
208 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
209 &usess
->buffer_reg_uid_list
,
210 usess
->consumer
, uchan
->id
,
211 uchan
->attr
.overwrite
,
214 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
215 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
216 uchan
, usess
->consumer
,
217 uchan
->attr
.overwrite
,
223 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
224 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
226 ERR("Unsupported buffer type");
237 * Fill lttng_channel array of all channels.
239 static void list_lttng_channels(enum lttng_domain_type domain
,
240 struct ltt_session
*session
, struct lttng_channel
*channels
,
241 struct lttcomm_channel_extended
*chan_exts
)
244 struct ltt_kernel_channel
*kchan
;
246 DBG("Listing channels for session %s", session
->name
);
249 case LTTNG_DOMAIN_KERNEL
:
250 /* Kernel channels */
251 if (session
->kernel_session
!= NULL
) {
252 cds_list_for_each_entry(kchan
,
253 &session
->kernel_session
->channel_list
.head
, list
) {
254 uint64_t discarded_events
, lost_packets
;
256 ret
= get_kernel_runtime_stats(session
, kchan
,
257 &discarded_events
, &lost_packets
);
261 /* Copy lttng_channel struct to array */
262 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
263 channels
[i
].enabled
= kchan
->enabled
;
264 chan_exts
[i
].discarded_events
=
266 chan_exts
[i
].lost_packets
= lost_packets
;
271 case LTTNG_DOMAIN_UST
:
273 struct lttng_ht_iter iter
;
274 struct ltt_ust_channel
*uchan
;
277 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
278 &iter
.iter
, uchan
, node
.node
) {
279 uint64_t discarded_events
= 0, lost_packets
= 0;
281 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
282 LTTNG_SYMBOL_NAME_LEN
)) {
285 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
286 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
287 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
288 channels
[i
].attr
.switch_timer_interval
=
289 uchan
->attr
.switch_timer_interval
;
290 channels
[i
].attr
.read_timer_interval
=
291 uchan
->attr
.read_timer_interval
;
292 channels
[i
].enabled
= uchan
->enabled
;
293 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
294 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
297 * Map enum lttng_ust_output to enum lttng_event_output.
299 switch (uchan
->attr
.output
) {
301 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
305 * LTTNG_UST_MMAP is the only supported UST
312 ret
= get_ust_runtime_stats(session
, uchan
,
313 &discarded_events
, &lost_packets
);
317 chan_exts
[i
].discarded_events
= discarded_events
;
318 chan_exts
[i
].lost_packets
= lost_packets
;
332 static void increment_extended_len(const char *filter_expression
,
333 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
335 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
337 if (filter_expression
) {
338 *extended_len
+= strlen(filter_expression
) + 1;
342 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
346 static void append_extended_info(const char *filter_expression
,
347 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
349 struct lttcomm_event_extended_header extended_header
;
350 size_t filter_len
= 0;
351 size_t nb_exclusions
= 0;
353 if (filter_expression
) {
354 filter_len
= strlen(filter_expression
) + 1;
358 nb_exclusions
= exclusion
->count
;
361 /* Set header fields */
362 extended_header
.filter_len
= filter_len
;
363 extended_header
.nb_exclusions
= nb_exclusions
;
366 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
367 *extended_at
+= sizeof(extended_header
);
369 /* Copy filter string */
370 if (filter_expression
) {
371 memcpy(*extended_at
, filter_expression
, filter_len
);
372 *extended_at
+= filter_len
;
375 /* Copy exclusion names */
377 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
379 memcpy(*extended_at
, &exclusion
->names
, len
);
385 * Create a list of agent domain events.
387 * Return number of events in list on success or else a negative value.
389 static int list_lttng_agent_events(struct agent
*agt
,
390 struct lttng_event
**events
, size_t *total_size
)
393 unsigned int nb_event
= 0;
394 struct agent_event
*event
;
395 struct lttng_event
*tmp_events
;
396 struct lttng_ht_iter iter
;
397 size_t extended_len
= 0;
403 DBG3("Listing agent events");
406 nb_event
= lttng_ht_get_count(agt
->events
);
414 /* Compute required extended infos size */
415 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
418 * This is only valid because the commands which add events are
419 * processed in the same thread as the listing.
422 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
423 increment_extended_len(event
->filter_expression
, NULL
,
428 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
429 tmp_events
= zmalloc(*total_size
);
431 PERROR("zmalloc agent events session");
432 ret
= -LTTNG_ERR_FATAL
;
436 extended_at
= ((uint8_t *) tmp_events
) +
437 nb_event
* sizeof(struct lttng_event
);
440 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
441 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
442 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
443 tmp_events
[i
].enabled
= event
->enabled
;
444 tmp_events
[i
].loglevel
= event
->loglevel_value
;
445 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
448 /* Append extended info */
449 append_extended_info(event
->filter_expression
, NULL
,
454 *events
= tmp_events
;
458 assert(nb_event
== i
);
463 * Create a list of ust global domain events.
465 static int list_lttng_ust_global_events(char *channel_name
,
466 struct ltt_ust_domain_global
*ust_global
,
467 struct lttng_event
**events
, size_t *total_size
)
470 unsigned int nb_event
= 0;
471 struct lttng_ht_iter iter
;
472 struct lttng_ht_node_str
*node
;
473 struct ltt_ust_channel
*uchan
;
474 struct ltt_ust_event
*uevent
;
475 struct lttng_event
*tmp
;
476 size_t extended_len
= 0;
479 DBG("Listing UST global events for channel %s", channel_name
);
483 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
484 node
= lttng_ht_iter_get_node_str(&iter
);
486 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
490 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
492 nb_event
= lttng_ht_get_count(uchan
->events
);
499 DBG3("Listing UST global %d events", nb_event
);
501 /* Compute required extended infos size */
502 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
503 if (uevent
->internal
) {
508 increment_extended_len(uevent
->filter_expression
,
509 uevent
->exclusion
, &extended_len
);
512 /* All events are internal, skip. */
518 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
519 tmp
= zmalloc(*total_size
);
521 ret
= -LTTNG_ERR_FATAL
;
525 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
527 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
528 if (uevent
->internal
) {
529 /* This event should remain hidden from clients */
532 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
533 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
534 tmp
[i
].enabled
= uevent
->enabled
;
536 switch (uevent
->attr
.instrumentation
) {
537 case LTTNG_UST_TRACEPOINT
:
538 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
540 case LTTNG_UST_PROBE
:
541 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
543 case LTTNG_UST_FUNCTION
:
544 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
548 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
549 switch (uevent
->attr
.loglevel_type
) {
550 case LTTNG_UST_LOGLEVEL_ALL
:
551 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
553 case LTTNG_UST_LOGLEVEL_RANGE
:
554 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
556 case LTTNG_UST_LOGLEVEL_SINGLE
:
557 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
560 if (uevent
->filter
) {
563 if (uevent
->exclusion
) {
564 tmp
[i
].exclusion
= 1;
568 /* Append extended info */
569 append_extended_info(uevent
->filter_expression
,
570 uevent
->exclusion
, &extended_at
);
581 * Fill lttng_event array of all kernel events in the channel.
583 static int list_lttng_kernel_events(char *channel_name
,
584 struct ltt_kernel_session
*kernel_session
,
585 struct lttng_event
**events
, size_t *total_size
)
588 unsigned int nb_event
;
589 struct ltt_kernel_event
*event
;
590 struct ltt_kernel_channel
*kchan
;
591 size_t extended_len
= 0;
594 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
596 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
600 nb_event
= kchan
->event_count
;
602 DBG("Listing events for channel %s", kchan
->channel
->name
);
610 /* Compute required extended infos size */
611 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
612 increment_extended_len(event
->filter_expression
, NULL
,
616 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
617 *events
= zmalloc(*total_size
);
618 if (*events
== NULL
) {
619 ret
= LTTNG_ERR_FATAL
;
623 extended_at
= ((void *) *events
) +
624 nb_event
* sizeof(struct lttng_event
);
626 /* Kernel channels */
627 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
628 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
629 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
630 (*events
)[i
].enabled
= event
->enabled
;
631 (*events
)[i
].filter
=
632 (unsigned char) !!event
->filter_expression
;
634 switch (event
->event
->instrumentation
) {
635 case LTTNG_KERNEL_TRACEPOINT
:
636 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
638 case LTTNG_KERNEL_KRETPROBE
:
639 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
640 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
641 sizeof(struct lttng_kernel_kprobe
));
643 case LTTNG_KERNEL_KPROBE
:
644 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
645 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
646 sizeof(struct lttng_kernel_kprobe
));
648 case LTTNG_KERNEL_FUNCTION
:
649 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
650 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
651 sizeof(struct lttng_kernel_function
));
653 case LTTNG_KERNEL_NOOP
:
654 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
656 case LTTNG_KERNEL_SYSCALL
:
657 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
659 case LTTNG_KERNEL_ALL
:
665 /* Append extended info */
666 append_extended_info(event
->filter_expression
, NULL
,
674 /* Negate the error code to differentiate the size from an error */
679 * Add URI so the consumer output object. Set the correct path depending on the
680 * domain adding the default trace directory.
682 static int add_uri_to_consumer(struct consumer_output
*consumer
,
683 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
684 const char *session_name
)
687 const char *default_trace_dir
;
691 if (consumer
== NULL
) {
692 DBG("No consumer detected. Don't add URI. Stopping.");
693 ret
= LTTNG_ERR_NO_CONSUMER
;
698 case LTTNG_DOMAIN_KERNEL
:
699 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
701 case LTTNG_DOMAIN_UST
:
702 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
706 * This case is possible is we try to add the URI to the global tracing
707 * session consumer object which in this case there is no subdir.
709 default_trace_dir
= "";
712 switch (uri
->dtype
) {
715 DBG2("Setting network URI to consumer");
717 if (consumer
->type
== CONSUMER_DST_NET
) {
718 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
719 consumer
->dst
.net
.control_isset
) ||
720 (uri
->stype
== LTTNG_STREAM_DATA
&&
721 consumer
->dst
.net
.data_isset
)) {
722 ret
= LTTNG_ERR_URL_EXIST
;
726 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
729 consumer
->type
= CONSUMER_DST_NET
;
731 /* Set URI into consumer output object */
732 ret
= consumer_set_network_uri(consumer
, uri
);
736 } else if (ret
== 1) {
738 * URI was the same in the consumer so we do not append the subdir
739 * again so to not duplicate output dir.
745 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
746 ret
= consumer_set_subdir(consumer
, session_name
);
748 ret
= LTTNG_ERR_FATAL
;
753 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
754 /* On a new subdir, reappend the default trace dir. */
755 strncat(consumer
->subdir
, default_trace_dir
,
756 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
757 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
762 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
763 memset(consumer
->dst
.trace_path
, 0,
764 sizeof(consumer
->dst
.trace_path
));
765 /* Explicit length checks for strcpy and strcat. */
766 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
767 >= sizeof(consumer
->dst
.trace_path
)) {
768 ret
= LTTNG_ERR_FATAL
;
771 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
772 /* Append default trace dir */
773 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
774 /* Flag consumer as local. */
775 consumer
->type
= CONSUMER_DST_LOCAL
;
786 * Init tracing by creating trace directory and sending fds kernel consumer.
788 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
791 struct lttng_ht_iter iter
;
792 struct consumer_socket
*socket
;
798 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
799 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
801 pthread_mutex_lock(socket
->lock
);
802 ret
= kernel_consumer_send_session(socket
, session
);
803 pthread_mutex_unlock(socket
->lock
);
805 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
817 * Create a socket to the relayd using the URI.
819 * On success, the relayd_sock pointer is set to the created socket.
820 * Else, it's stays untouched and a lttcomm error code is returned.
822 static int create_connect_relayd(struct lttng_uri
*uri
,
823 struct lttcomm_relayd_sock
**relayd_sock
,
824 struct consumer_output
*consumer
)
827 struct lttcomm_relayd_sock
*rsock
;
829 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
830 RELAYD_VERSION_COMM_MINOR
);
832 ret
= LTTNG_ERR_FATAL
;
837 * Connect to relayd so we can proceed with a session creation. This call
838 * can possibly block for an arbitrary amount of time to set the health
839 * state to be in poll execution.
842 ret
= relayd_connect(rsock
);
845 ERR("Unable to reach lttng-relayd");
846 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
850 /* Create socket for control stream. */
851 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
852 DBG3("Creating relayd stream socket from URI");
854 /* Check relayd version */
855 ret
= relayd_version_check(rsock
);
857 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
860 consumer
->relay_major_version
= rsock
->major
;
861 consumer
->relay_minor_version
= rsock
->minor
;
862 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
863 DBG3("Creating relayd data socket from URI");
865 /* Command is not valid */
866 ERR("Relayd invalid stream type: %d", uri
->stype
);
867 ret
= LTTNG_ERR_INVALID
;
871 *relayd_sock
= rsock
;
876 /* The returned value is not useful since we are on an error path. */
877 (void) relayd_close(rsock
);
885 * Connect to the relayd using URI and send the socket to the right consumer.
887 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
888 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
889 struct consumer_output
*consumer
,
890 struct consumer_socket
*consumer_sock
,
891 char *session_name
, char *hostname
, int session_live_timer
)
894 struct lttcomm_relayd_sock
*rsock
= NULL
;
896 /* Connect to relayd and make version check if uri is the control. */
897 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
898 if (ret
!= LTTNG_OK
) {
903 /* Set the network sequence index if not set. */
904 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
905 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
907 * Increment net_seq_idx because we are about to transfer the
908 * new relayd socket to the consumer.
909 * Assign unique key so the consumer can match streams.
911 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
912 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
915 /* Send relayd socket to consumer. */
916 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
917 relayd_uri
->stype
, session_id
,
918 session_name
, hostname
, session_live_timer
);
920 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
924 /* Flag that the corresponding socket was sent. */
925 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
926 consumer_sock
->control_sock_sent
= 1;
927 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
928 consumer_sock
->data_sock_sent
= 1;
934 * Close socket which was dup on the consumer side. The session daemon does
935 * NOT keep track of the relayd socket(s) once transfer to the consumer.
939 (void) relayd_close(rsock
);
943 if (ret
!= LTTNG_OK
) {
945 * The consumer output for this session should not be used anymore
946 * since the relayd connection failed thus making any tracing or/and
947 * streaming not usable.
949 consumer
->enabled
= 0;
955 * Send both relayd sockets to a specific consumer and domain. This is a
956 * helper function to facilitate sending the information to the consumer for a
959 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
960 unsigned int session_id
, struct consumer_output
*consumer
,
961 struct consumer_socket
*sock
, char *session_name
,
962 char *hostname
, int session_live_timer
)
969 /* Sending control relayd socket. */
970 if (!sock
->control_sock_sent
) {
971 ret
= send_consumer_relayd_socket(domain
, session_id
,
972 &consumer
->dst
.net
.control
, consumer
, sock
,
973 session_name
, hostname
, session_live_timer
);
974 if (ret
!= LTTNG_OK
) {
979 /* Sending data relayd socket. */
980 if (!sock
->data_sock_sent
) {
981 ret
= send_consumer_relayd_socket(domain
, session_id
,
982 &consumer
->dst
.net
.data
, consumer
, sock
,
983 session_name
, hostname
, session_live_timer
);
984 if (ret
!= LTTNG_OK
) {
994 * Setup relayd connections for a tracing session. First creates the socket to
995 * the relayd and send them to the right domain consumer. Consumer type MUST be
998 int cmd_setup_relayd(struct ltt_session
*session
)
1001 struct ltt_ust_session
*usess
;
1002 struct ltt_kernel_session
*ksess
;
1003 struct consumer_socket
*socket
;
1004 struct lttng_ht_iter iter
;
1008 usess
= session
->ust_session
;
1009 ksess
= session
->kernel_session
;
1011 DBG("Setting relayd for session %s", session
->name
);
1015 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1016 && usess
->consumer
->enabled
) {
1017 /* For each consumer socket, send relayd sockets */
1018 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1019 socket
, node
.node
) {
1020 pthread_mutex_lock(socket
->lock
);
1021 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1022 usess
->consumer
, socket
,
1023 session
->name
, session
->hostname
,
1024 session
->live_timer
);
1025 pthread_mutex_unlock(socket
->lock
);
1026 if (ret
!= LTTNG_OK
) {
1029 /* Session is now ready for network streaming. */
1030 session
->net_handle
= 1;
1032 session
->consumer
->relay_major_version
=
1033 usess
->consumer
->relay_major_version
;
1034 session
->consumer
->relay_minor_version
=
1035 usess
->consumer
->relay_minor_version
;
1038 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1039 && ksess
->consumer
->enabled
) {
1040 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1041 socket
, node
.node
) {
1042 pthread_mutex_lock(socket
->lock
);
1043 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1044 ksess
->consumer
, socket
,
1045 session
->name
, session
->hostname
,
1046 session
->live_timer
);
1047 pthread_mutex_unlock(socket
->lock
);
1048 if (ret
!= LTTNG_OK
) {
1051 /* Session is now ready for network streaming. */
1052 session
->net_handle
= 1;
1054 session
->consumer
->relay_major_version
=
1055 ksess
->consumer
->relay_major_version
;
1056 session
->consumer
->relay_minor_version
=
1057 ksess
->consumer
->relay_minor_version
;
1066 * Start a kernel session by opening all necessary streams.
1068 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1071 struct ltt_kernel_channel
*kchan
;
1073 /* Open kernel metadata */
1074 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1075 ret
= kernel_open_metadata(ksess
);
1077 ret
= LTTNG_ERR_KERN_META_FAIL
;
1082 /* Open kernel metadata stream */
1083 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1084 ret
= kernel_open_metadata_stream(ksess
);
1086 ERR("Kernel create metadata stream failed");
1087 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1092 /* For each channel */
1093 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1094 if (kchan
->stream_count
== 0) {
1095 ret
= kernel_open_channel_stream(kchan
);
1097 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1100 /* Update the stream global counter */
1101 ksess
->stream_count_global
+= ret
;
1105 /* Setup kernel consumer socket and send fds to it */
1106 ret
= init_kernel_tracing(ksess
);
1108 ret
= LTTNG_ERR_KERN_START_FAIL
;
1112 /* This start the kernel tracing */
1113 ret
= kernel_start_session(ksess
);
1115 ret
= LTTNG_ERR_KERN_START_FAIL
;
1119 /* Quiescent wait after starting trace */
1120 kernel_wait_quiescent(kernel_tracer_fd
);
1131 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1133 int cmd_disable_channel(struct ltt_session
*session
,
1134 enum lttng_domain_type domain
, char *channel_name
)
1137 struct ltt_ust_session
*usess
;
1139 usess
= session
->ust_session
;
1144 case LTTNG_DOMAIN_KERNEL
:
1146 ret
= channel_kernel_disable(session
->kernel_session
,
1148 if (ret
!= LTTNG_OK
) {
1152 kernel_wait_quiescent(kernel_tracer_fd
);
1155 case LTTNG_DOMAIN_UST
:
1157 struct ltt_ust_channel
*uchan
;
1158 struct lttng_ht
*chan_ht
;
1160 chan_ht
= usess
->domain_global
.channels
;
1162 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1163 if (uchan
== NULL
) {
1164 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1168 ret
= channel_ust_disable(usess
, uchan
);
1169 if (ret
!= LTTNG_OK
) {
1175 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1187 * Command LTTNG_TRACK_PID processed by the client thread.
1189 * Called with session lock held.
1191 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1199 case LTTNG_DOMAIN_KERNEL
:
1201 struct ltt_kernel_session
*ksess
;
1203 ksess
= session
->kernel_session
;
1205 ret
= kernel_track_pid(ksess
, pid
);
1206 if (ret
!= LTTNG_OK
) {
1210 kernel_wait_quiescent(kernel_tracer_fd
);
1213 case LTTNG_DOMAIN_UST
:
1215 struct ltt_ust_session
*usess
;
1217 usess
= session
->ust_session
;
1219 ret
= trace_ust_track_pid(usess
, pid
);
1220 if (ret
!= LTTNG_OK
) {
1226 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1238 * Command LTTNG_UNTRACK_PID processed by the client thread.
1240 * Called with session lock held.
1242 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1250 case LTTNG_DOMAIN_KERNEL
:
1252 struct ltt_kernel_session
*ksess
;
1254 ksess
= session
->kernel_session
;
1256 ret
= kernel_untrack_pid(ksess
, pid
);
1257 if (ret
!= LTTNG_OK
) {
1261 kernel_wait_quiescent(kernel_tracer_fd
);
1264 case LTTNG_DOMAIN_UST
:
1266 struct ltt_ust_session
*usess
;
1268 usess
= session
->ust_session
;
1270 ret
= trace_ust_untrack_pid(usess
, pid
);
1271 if (ret
!= LTTNG_OK
) {
1277 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1289 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1291 * The wpipe arguments is used as a notifier for the kernel thread.
1293 int cmd_enable_channel(struct ltt_session
*session
,
1294 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1297 struct ltt_ust_session
*usess
= session
->ust_session
;
1298 struct lttng_ht
*chan_ht
;
1305 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1307 /* Validate channel name */
1308 if (attr
->name
[0] == '.' ||
1309 memchr(attr
->name
, '/', len
) != NULL
) {
1310 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1314 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1319 * Don't try to enable a channel if the session has been started at
1320 * some point in time before. The tracer does not allow it.
1322 if (session
->has_been_started
) {
1323 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1328 * If the session is a live session, remove the switch timer, the
1329 * live timer does the same thing but sends also synchronisation
1330 * beacons for inactive streams.
1332 if (session
->live_timer
> 0) {
1333 attr
->attr
.live_timer_interval
= session
->live_timer
;
1334 attr
->attr
.switch_timer_interval
= 0;
1337 switch (domain
->type
) {
1338 case LTTNG_DOMAIN_KERNEL
:
1340 struct ltt_kernel_channel
*kchan
;
1342 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1343 session
->kernel_session
);
1344 if (kchan
== NULL
) {
1345 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1346 if (attr
->name
[0] != '\0') {
1347 session
->kernel_session
->has_non_default_channel
= 1;
1350 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1353 if (ret
!= LTTNG_OK
) {
1357 kernel_wait_quiescent(kernel_tracer_fd
);
1360 case LTTNG_DOMAIN_UST
:
1361 case LTTNG_DOMAIN_JUL
:
1362 case LTTNG_DOMAIN_LOG4J
:
1363 case LTTNG_DOMAIN_PYTHON
:
1365 struct ltt_ust_channel
*uchan
;
1370 * Current agent implementation limitations force us to allow
1371 * only one channel at once in "agent" subdomains. Each
1372 * subdomain has a default channel name which must be strictly
1375 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1376 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1377 LTTNG_SYMBOL_NAME_LEN
)) {
1378 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1381 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1382 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1383 LTTNG_SYMBOL_NAME_LEN
)) {
1384 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1387 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1388 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1389 LTTNG_SYMBOL_NAME_LEN
)) {
1390 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1395 chan_ht
= usess
->domain_global
.channels
;
1397 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1398 if (uchan
== NULL
) {
1399 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1400 if (attr
->name
[0] != '\0') {
1401 usess
->has_non_default_channel
= 1;
1404 ret
= channel_ust_enable(usess
, uchan
);
1409 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1420 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1422 int cmd_disable_event(struct ltt_session
*session
,
1423 enum lttng_domain_type domain
, char *channel_name
,
1424 struct lttng_event
*event
)
1429 DBG("Disable event command for event \'%s\'", event
->name
);
1431 event_name
= event
->name
;
1432 if (validate_event_name(event_name
)) {
1433 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1437 /* Error out on unhandled search criteria */
1438 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1439 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1440 ret
= LTTNG_ERR_UNK
;
1447 case LTTNG_DOMAIN_KERNEL
:
1449 struct ltt_kernel_channel
*kchan
;
1450 struct ltt_kernel_session
*ksess
;
1452 ksess
= session
->kernel_session
;
1455 * If a non-default channel has been created in the
1456 * session, explicitely require that -c chan_name needs
1459 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1460 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1464 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1465 if (kchan
== NULL
) {
1466 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1470 switch (event
->type
) {
1471 case LTTNG_EVENT_ALL
:
1472 case LTTNG_EVENT_TRACEPOINT
:
1473 case LTTNG_EVENT_SYSCALL
:
1474 case LTTNG_EVENT_PROBE
:
1475 case LTTNG_EVENT_FUNCTION
:
1476 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1477 if (event_name
[0] == '\0') {
1478 ret
= event_kernel_disable_event(kchan
,
1481 ret
= event_kernel_disable_event(kchan
,
1482 event_name
, event
->type
);
1484 if (ret
!= LTTNG_OK
) {
1489 ret
= LTTNG_ERR_UNK
;
1493 kernel_wait_quiescent(kernel_tracer_fd
);
1496 case LTTNG_DOMAIN_UST
:
1498 struct ltt_ust_channel
*uchan
;
1499 struct ltt_ust_session
*usess
;
1501 usess
= session
->ust_session
;
1503 if (validate_ust_event_name(event_name
)) {
1504 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1509 * If a non-default channel has been created in the
1510 * session, explicitly require that -c chan_name needs
1513 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1514 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1518 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1520 if (uchan
== NULL
) {
1521 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1525 switch (event
->type
) {
1526 case LTTNG_EVENT_ALL
:
1528 * An empty event name means that everything
1529 * should be disabled.
1531 if (event
->name
[0] == '\0') {
1532 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1534 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1537 if (ret
!= LTTNG_OK
) {
1542 ret
= LTTNG_ERR_UNK
;
1546 DBG3("Disable UST event %s in channel %s completed", event_name
,
1550 case LTTNG_DOMAIN_LOG4J
:
1551 case LTTNG_DOMAIN_JUL
:
1552 case LTTNG_DOMAIN_PYTHON
:
1555 struct ltt_ust_session
*usess
= session
->ust_session
;
1559 switch (event
->type
) {
1560 case LTTNG_EVENT_ALL
:
1563 ret
= LTTNG_ERR_UNK
;
1567 agt
= trace_ust_find_agent(usess
, domain
);
1569 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1573 * An empty event name means that everything
1574 * should be disabled.
1576 if (event
->name
[0] == '\0') {
1577 ret
= event_agent_disable_all(usess
, agt
);
1579 ret
= event_agent_disable(usess
, agt
, event_name
);
1581 if (ret
!= LTTNG_OK
) {
1588 ret
= LTTNG_ERR_UND
;
1601 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1603 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1604 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1606 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1607 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1609 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1610 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1611 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1615 case LTTNG_DOMAIN_KERNEL
:
1616 assert(session
->kernel_session
);
1618 if (session
->kernel_session
->channel_count
== 0) {
1619 /* Create default channel */
1620 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1621 if (ret
!= LTTNG_OK
) {
1624 chan_kern_created
= 1;
1626 /* Add kernel context to kernel tracer */
1627 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1628 if (ret
!= LTTNG_OK
) {
1632 case LTTNG_DOMAIN_JUL
:
1633 case LTTNG_DOMAIN_LOG4J
:
1636 * Validate channel name.
1637 * If no channel name is given and the domain is JUL or LOG4J,
1638 * set it to the appropriate domain-specific channel name. If
1639 * a name is provided but does not match the expexted channel
1640 * name, return an error.
1642 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1643 strcmp(channel_name
,
1644 DEFAULT_JUL_CHANNEL_NAME
)) {
1645 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1647 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1648 strcmp(channel_name
,
1649 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1650 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1653 /* break is _not_ missing here. */
1655 case LTTNG_DOMAIN_UST
:
1657 struct ltt_ust_session
*usess
= session
->ust_session
;
1658 unsigned int chan_count
;
1662 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1663 if (chan_count
== 0) {
1664 struct lttng_channel
*attr
;
1665 /* Create default channel */
1666 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1668 ret
= LTTNG_ERR_FATAL
;
1672 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1673 if (ret
!= LTTNG_OK
) {
1678 chan_ust_created
= 1;
1681 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1682 free(app_ctx_provider_name
);
1684 app_ctx_name
= NULL
;
1685 app_ctx_provider_name
= NULL
;
1686 if (ret
!= LTTNG_OK
) {
1692 ret
= LTTNG_ERR_UND
;
1700 if (chan_kern_created
) {
1701 struct ltt_kernel_channel
*kchan
=
1702 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1703 session
->kernel_session
);
1704 /* Created previously, this should NOT fail. */
1706 kernel_destroy_channel(kchan
);
1709 if (chan_ust_created
) {
1710 struct ltt_ust_channel
*uchan
=
1711 trace_ust_find_channel_by_name(
1712 session
->ust_session
->domain_global
.channels
,
1713 DEFAULT_CHANNEL_NAME
);
1714 /* Created previously, this should NOT fail. */
1716 /* Remove from the channel list of the session. */
1717 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1719 trace_ust_destroy_channel(uchan
);
1722 free(app_ctx_provider_name
);
1727 static int validate_event_name(const char *name
)
1730 const char *c
= name
;
1731 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1732 bool null_terminated
= false;
1735 * Make sure that unescaped wildcards are only used as the last
1736 * character of the event name.
1738 while (c
< event_name_end
) {
1741 null_terminated
= true;
1747 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1748 /* Wildcard is not the last character */
1749 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1758 if (!ret
&& !null_terminated
) {
1759 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1764 static inline bool name_starts_with(const char *name
, const char *prefix
)
1766 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1768 return !strncmp(name
, prefix
, max_cmp_len
);
1771 /* Perform userspace-specific event name validation */
1772 static int validate_ust_event_name(const char *name
)
1782 * Check name against all internal UST event component namespaces used
1785 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1786 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1787 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1796 * Internal version of cmd_enable_event() with a supplemental
1797 * "internal_event" flag which is used to enable internal events which should
1798 * be hidden from clients. Such events are used in the agent implementation to
1799 * enable the events through which all "agent" events are funeled.
1801 static int _cmd_enable_event(struct ltt_session
*session
,
1802 struct lttng_domain
*domain
,
1803 char *channel_name
, struct lttng_event
*event
,
1804 char *filter_expression
,
1805 struct lttng_filter_bytecode
*filter
,
1806 struct lttng_event_exclusion
*exclusion
,
1807 int wpipe
, bool internal_event
)
1809 int ret
, channel_created
= 0;
1810 struct lttng_channel
*attr
= NULL
;
1814 assert(channel_name
);
1816 /* If we have a filter, we must have its filter expression */
1817 assert(!(!!filter_expression
^ !!filter
));
1819 DBG("Enable event command for event \'%s\'", event
->name
);
1823 ret
= validate_event_name(event
->name
);
1828 switch (domain
->type
) {
1829 case LTTNG_DOMAIN_KERNEL
:
1831 struct ltt_kernel_channel
*kchan
;
1834 * If a non-default channel has been created in the
1835 * session, explicitely require that -c chan_name needs
1838 if (session
->kernel_session
->has_non_default_channel
1839 && channel_name
[0] == '\0') {
1840 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1844 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1845 session
->kernel_session
);
1846 if (kchan
== NULL
) {
1847 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1848 LTTNG_BUFFER_GLOBAL
);
1850 ret
= LTTNG_ERR_FATAL
;
1853 if (lttng_strncpy(attr
->name
, channel_name
,
1854 sizeof(attr
->name
))) {
1855 ret
= LTTNG_ERR_INVALID
;
1859 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1860 if (ret
!= LTTNG_OK
) {
1863 channel_created
= 1;
1866 /* Get the newly created kernel channel pointer */
1867 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1868 session
->kernel_session
);
1869 if (kchan
== NULL
) {
1870 /* This sould not happen... */
1871 ret
= LTTNG_ERR_FATAL
;
1875 switch (event
->type
) {
1876 case LTTNG_EVENT_ALL
:
1878 char *filter_expression_a
= NULL
;
1879 struct lttng_filter_bytecode
*filter_a
= NULL
;
1882 * We need to duplicate filter_expression and filter,
1883 * because ownership is passed to first enable
1886 if (filter_expression
) {
1887 filter_expression_a
= strdup(filter_expression
);
1888 if (!filter_expression_a
) {
1889 ret
= LTTNG_ERR_FATAL
;
1894 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1896 free(filter_expression_a
);
1897 ret
= LTTNG_ERR_FATAL
;
1900 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1902 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1903 ret
= event_kernel_enable_event(kchan
, event
,
1904 filter_expression
, filter
);
1905 /* We have passed ownership */
1906 filter_expression
= NULL
;
1908 if (ret
!= LTTNG_OK
) {
1909 if (channel_created
) {
1910 /* Let's not leak a useless channel. */
1911 kernel_destroy_channel(kchan
);
1913 free(filter_expression_a
);
1917 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1918 ret
= event_kernel_enable_event(kchan
, event
,
1919 filter_expression_a
, filter_a
);
1920 /* We have passed ownership */
1921 filter_expression_a
= NULL
;
1923 if (ret
!= LTTNG_OK
) {
1928 case LTTNG_EVENT_PROBE
:
1929 case LTTNG_EVENT_FUNCTION
:
1930 case LTTNG_EVENT_FUNCTION_ENTRY
:
1931 case LTTNG_EVENT_TRACEPOINT
:
1932 ret
= event_kernel_enable_event(kchan
, event
,
1933 filter_expression
, filter
);
1934 /* We have passed ownership */
1935 filter_expression
= NULL
;
1937 if (ret
!= LTTNG_OK
) {
1938 if (channel_created
) {
1939 /* Let's not leak a useless channel. */
1940 kernel_destroy_channel(kchan
);
1945 case LTTNG_EVENT_SYSCALL
:
1946 ret
= event_kernel_enable_event(kchan
, event
,
1947 filter_expression
, filter
);
1948 /* We have passed ownership */
1949 filter_expression
= NULL
;
1951 if (ret
!= LTTNG_OK
) {
1956 ret
= LTTNG_ERR_UNK
;
1960 kernel_wait_quiescent(kernel_tracer_fd
);
1963 case LTTNG_DOMAIN_UST
:
1965 struct ltt_ust_channel
*uchan
;
1966 struct ltt_ust_session
*usess
= session
->ust_session
;
1971 * If a non-default channel has been created in the
1972 * session, explicitely require that -c chan_name needs
1975 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1976 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1980 /* Get channel from global UST domain */
1981 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1983 if (uchan
== NULL
) {
1984 /* Create default channel */
1985 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1986 usess
->buffer_type
);
1988 ret
= LTTNG_ERR_FATAL
;
1991 if (lttng_strncpy(attr
->name
, channel_name
,
1992 sizeof(attr
->name
))) {
1993 ret
= LTTNG_ERR_INVALID
;
1997 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1998 if (ret
!= LTTNG_OK
) {
2002 /* Get the newly created channel reference back */
2003 uchan
= trace_ust_find_channel_by_name(
2004 usess
->domain_global
.channels
, channel_name
);
2008 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2010 * Don't allow users to add UST events to channels which
2011 * are assigned to a userspace subdomain (JUL, Log4J,
2014 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2018 if (!internal_event
) {
2020 * Ensure the event name is not reserved for internal
2023 ret
= validate_ust_event_name(event
->name
);
2025 WARN("Userspace event name %s failed validation.",
2027 event
->name
: "NULL");
2028 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2033 /* At this point, the session and channel exist on the tracer */
2034 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2035 filter_expression
, filter
, exclusion
,
2037 /* We have passed ownership */
2038 filter_expression
= NULL
;
2041 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2042 goto already_enabled
;
2043 } else if (ret
!= LTTNG_OK
) {
2048 case LTTNG_DOMAIN_LOG4J
:
2049 case LTTNG_DOMAIN_JUL
:
2050 case LTTNG_DOMAIN_PYTHON
:
2052 const char *default_event_name
, *default_chan_name
;
2054 struct lttng_event uevent
;
2055 struct lttng_domain tmp_dom
;
2056 struct ltt_ust_session
*usess
= session
->ust_session
;
2060 agt
= trace_ust_find_agent(usess
, domain
->type
);
2062 agt
= agent_create(domain
->type
);
2064 ret
= LTTNG_ERR_NOMEM
;
2067 agent_add(agt
, usess
->agents
);
2070 /* Create the default tracepoint. */
2071 memset(&uevent
, 0, sizeof(uevent
));
2072 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2073 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2074 default_event_name
= event_get_default_agent_ust_name(
2076 if (!default_event_name
) {
2077 ret
= LTTNG_ERR_FATAL
;
2080 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2081 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2084 * The domain type is changed because we are about to enable the
2085 * default channel and event for the JUL domain that are hardcoded.
2086 * This happens in the UST domain.
2088 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2089 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2091 switch (domain
->type
) {
2092 case LTTNG_DOMAIN_LOG4J
:
2093 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2095 case LTTNG_DOMAIN_JUL
:
2096 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2098 case LTTNG_DOMAIN_PYTHON
:
2099 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2102 /* The switch/case we are in makes this impossible */
2107 char *filter_expression_copy
= NULL
;
2108 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2111 const size_t filter_size
= sizeof(
2112 struct lttng_filter_bytecode
)
2115 filter_copy
= zmalloc(filter_size
);
2117 ret
= LTTNG_ERR_NOMEM
;
2120 memcpy(filter_copy
, filter
, filter_size
);
2122 filter_expression_copy
=
2123 strdup(filter_expression
);
2124 if (!filter_expression
) {
2125 ret
= LTTNG_ERR_NOMEM
;
2128 if (!filter_expression_copy
|| !filter_copy
) {
2129 free(filter_expression_copy
);
2135 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2136 (char *) default_chan_name
,
2137 &uevent
, filter_expression_copy
,
2138 filter_copy
, NULL
, wpipe
);
2141 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2142 goto already_enabled
;
2143 } else if (ret
!= LTTNG_OK
) {
2147 /* The wild card * means that everything should be enabled. */
2148 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2149 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2152 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2156 filter_expression
= NULL
;
2157 if (ret
!= LTTNG_OK
) {
2164 ret
= LTTNG_ERR_UND
;
2172 free(filter_expression
);
2181 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2182 * We own filter, exclusion, and filter_expression.
2184 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2185 char *channel_name
, struct lttng_event
*event
,
2186 char *filter_expression
,
2187 struct lttng_filter_bytecode
*filter
,
2188 struct lttng_event_exclusion
*exclusion
,
2191 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2192 filter_expression
, filter
, exclusion
, wpipe
, false);
2196 * Enable an event which is internal to LTTng. An internal should
2197 * never be made visible to clients and are immune to checks such as
2200 static int cmd_enable_event_internal(struct ltt_session
*session
,
2201 struct lttng_domain
*domain
,
2202 char *channel_name
, struct lttng_event
*event
,
2203 char *filter_expression
,
2204 struct lttng_filter_bytecode
*filter
,
2205 struct lttng_event_exclusion
*exclusion
,
2208 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2209 filter_expression
, filter
, exclusion
, wpipe
, true);
2213 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2215 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2216 struct lttng_event
**events
)
2219 ssize_t nb_events
= 0;
2222 case LTTNG_DOMAIN_KERNEL
:
2223 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2224 if (nb_events
< 0) {
2225 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2229 case LTTNG_DOMAIN_UST
:
2230 nb_events
= ust_app_list_events(events
);
2231 if (nb_events
< 0) {
2232 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2236 case LTTNG_DOMAIN_LOG4J
:
2237 case LTTNG_DOMAIN_JUL
:
2238 case LTTNG_DOMAIN_PYTHON
:
2239 nb_events
= agent_list_events(events
, domain
);
2240 if (nb_events
< 0) {
2241 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2246 ret
= LTTNG_ERR_UND
;
2253 /* Return negative value to differentiate return code */
2258 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2260 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2261 struct lttng_event_field
**fields
)
2264 ssize_t nb_fields
= 0;
2267 case LTTNG_DOMAIN_UST
:
2268 nb_fields
= ust_app_list_event_fields(fields
);
2269 if (nb_fields
< 0) {
2270 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2274 case LTTNG_DOMAIN_KERNEL
:
2275 default: /* fall-through */
2276 ret
= LTTNG_ERR_UND
;
2283 /* Return negative value to differentiate return code */
2287 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2289 return syscall_table_list(events
);
2293 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2295 * Called with session lock held.
2297 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2298 enum lttng_domain_type domain
, int32_t **pids
)
2301 ssize_t nr_pids
= 0;
2304 case LTTNG_DOMAIN_KERNEL
:
2306 struct ltt_kernel_session
*ksess
;
2308 ksess
= session
->kernel_session
;
2309 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2311 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2316 case LTTNG_DOMAIN_UST
:
2318 struct ltt_ust_session
*usess
;
2320 usess
= session
->ust_session
;
2321 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2323 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2328 case LTTNG_DOMAIN_LOG4J
:
2329 case LTTNG_DOMAIN_JUL
:
2330 case LTTNG_DOMAIN_PYTHON
:
2332 ret
= LTTNG_ERR_UND
;
2339 /* Return negative value to differentiate return code */
2344 * Command LTTNG_START_TRACE processed by the client thread.
2346 * Called with session mutex held.
2348 int cmd_start_trace(struct ltt_session
*session
)
2351 unsigned long nb_chan
= 0;
2352 struct ltt_kernel_session
*ksession
;
2353 struct ltt_ust_session
*usess
;
2357 /* Ease our life a bit ;) */
2358 ksession
= session
->kernel_session
;
2359 usess
= session
->ust_session
;
2361 /* Is the session already started? */
2362 if (session
->active
) {
2363 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2368 * Starting a session without channel is useless since after that it's not
2369 * possible to enable channel thus inform the client.
2371 if (usess
&& usess
->domain_global
.channels
) {
2372 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2375 nb_chan
+= ksession
->channel_count
;
2378 ret
= LTTNG_ERR_NO_CHANNEL
;
2382 /* Kernel tracing */
2383 if (ksession
!= NULL
) {
2384 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2385 if (ret
!= LTTNG_OK
) {
2390 /* Flag session that trace should start automatically */
2393 * Even though the start trace might fail, flag this session active so
2394 * other application coming in are started by default.
2398 ret
= ust_app_start_trace_all(usess
);
2400 ret
= LTTNG_ERR_UST_START_FAIL
;
2405 /* Flag this after a successful start. */
2406 session
->has_been_started
= 1;
2407 session
->active
= 1;
2416 * Command LTTNG_STOP_TRACE processed by the client thread.
2418 int cmd_stop_trace(struct ltt_session
*session
)
2421 struct ltt_kernel_channel
*kchan
;
2422 struct ltt_kernel_session
*ksession
;
2423 struct ltt_ust_session
*usess
;
2428 ksession
= session
->kernel_session
;
2429 usess
= session
->ust_session
;
2431 /* Session is not active. Skip everythong and inform the client. */
2432 if (!session
->active
) {
2433 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2438 if (ksession
&& ksession
->active
) {
2439 DBG("Stop kernel tracing");
2441 ret
= kernel_stop_session(ksession
);
2443 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2447 kernel_wait_quiescent(kernel_tracer_fd
);
2449 /* Flush metadata after stopping (if exists) */
2450 if (ksession
->metadata_stream_fd
>= 0) {
2451 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2453 ERR("Kernel metadata flush failed");
2457 /* Flush all buffers after stopping */
2458 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2459 ret
= kernel_flush_buffer(kchan
);
2461 ERR("Kernel flush buffer error");
2465 ksession
->active
= 0;
2468 if (usess
&& usess
->active
) {
2470 * Even though the stop trace might fail, flag this session inactive so
2471 * other application coming in are not started by default.
2475 ret
= ust_app_stop_trace_all(usess
);
2477 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2482 /* Flag inactive after a successful stop. */
2483 session
->active
= 0;
2491 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2493 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2494 struct lttng_uri
*uris
)
2497 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2498 struct ltt_ust_session
*usess
= session
->ust_session
;
2504 /* Can't set consumer URI if the session is active. */
2505 if (session
->active
) {
2506 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2510 /* Set the "global" consumer URIs */
2511 for (i
= 0; i
< nb_uri
; i
++) {
2512 ret
= add_uri_to_consumer(session
->consumer
,
2513 &uris
[i
], 0, session
->name
);
2514 if (ret
!= LTTNG_OK
) {
2519 /* Set UST session URIs */
2520 if (session
->ust_session
) {
2521 for (i
= 0; i
< nb_uri
; i
++) {
2522 ret
= add_uri_to_consumer(
2523 session
->ust_session
->consumer
,
2524 &uris
[i
], LTTNG_DOMAIN_UST
,
2526 if (ret
!= LTTNG_OK
) {
2532 /* Set kernel session URIs */
2533 if (session
->kernel_session
) {
2534 for (i
= 0; i
< nb_uri
; i
++) {
2535 ret
= add_uri_to_consumer(
2536 session
->kernel_session
->consumer
,
2537 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2539 if (ret
!= LTTNG_OK
) {
2546 * Make sure to set the session in output mode after we set URI since a
2547 * session can be created without URL (thus flagged in no output mode).
2549 session
->output_traces
= 1;
2551 ksess
->output_traces
= 1;
2555 usess
->output_traces
= 1;
2566 * Command LTTNG_CREATE_SESSION processed by the client thread.
2568 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2569 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2572 struct ltt_session
*session
;
2578 * Verify if the session already exist
2580 * XXX: There is no need for the session lock list here since the caller
2581 * (process_client_msg) is holding it. We might want to change that so a
2582 * single command does not lock the entire session list.
2584 session
= session_find_by_name(name
);
2585 if (session
!= NULL
) {
2586 ret
= LTTNG_ERR_EXIST_SESS
;
2590 /* Create tracing session in the registry */
2591 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2592 LTTNG_SOCK_GET_GID_CRED(creds
));
2593 if (ret
!= LTTNG_OK
) {
2598 * Get the newly created session pointer back
2600 * XXX: There is no need for the session lock list here since the caller
2601 * (process_client_msg) is holding it. We might want to change that so a
2602 * single command does not lock the entire session list.
2604 session
= session_find_by_name(name
);
2607 session
->live_timer
= live_timer
;
2608 /* Create default consumer output for the session not yet created. */
2609 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2610 if (session
->consumer
== NULL
) {
2611 ret
= LTTNG_ERR_FATAL
;
2612 goto consumer_error
;
2616 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2617 if (ret
!= LTTNG_OK
) {
2618 goto consumer_error
;
2620 session
->output_traces
= 1;
2622 session
->output_traces
= 0;
2623 DBG2("Session %s created with no output", session
->name
);
2626 session
->consumer
->enabled
= 1;
2631 session_destroy(session
);
2638 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2640 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2641 size_t nb_uri
, lttng_sock_cred
*creds
)
2644 struct ltt_session
*session
;
2645 struct snapshot_output
*new_output
= NULL
;
2651 * Create session in no output mode with URIs set to NULL. The uris we've
2652 * received are for a default snapshot output if one.
2654 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2655 if (ret
!= LTTNG_OK
) {
2659 /* Get the newly created session pointer back. This should NEVER fail. */
2660 session
= session_find_by_name(name
);
2663 /* Flag session for snapshot mode. */
2664 session
->snapshot_mode
= 1;
2666 /* Skip snapshot output creation if no URI is given. */
2671 new_output
= snapshot_output_alloc();
2673 ret
= LTTNG_ERR_NOMEM
;
2674 goto error_snapshot_alloc
;
2677 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2678 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2680 if (ret
== -ENOMEM
) {
2681 ret
= LTTNG_ERR_NOMEM
;
2683 ret
= LTTNG_ERR_INVALID
;
2685 goto error_snapshot
;
2689 snapshot_add_output(&session
->snapshot
, new_output
);
2696 snapshot_output_destroy(new_output
);
2697 error_snapshot_alloc
:
2698 session_destroy(session
);
2704 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2706 * Called with session lock held.
2708 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2711 struct ltt_ust_session
*usess
;
2712 struct ltt_kernel_session
*ksess
;
2717 usess
= session
->ust_session
;
2718 ksess
= session
->kernel_session
;
2720 /* Clean kernel session teardown */
2721 kernel_destroy_session(ksess
);
2723 /* UST session teardown */
2725 /* Close any relayd session */
2726 consumer_output_send_destroy_relayd(usess
->consumer
);
2728 /* Destroy every UST application related to this session. */
2729 ret
= ust_app_destroy_trace_all(usess
);
2731 ERR("Error in ust_app_destroy_trace_all");
2734 /* Clean up the rest. */
2735 trace_ust_destroy_session(usess
);
2739 * Must notify the kernel thread here to update it's poll set in order to
2740 * remove the channel(s)' fd just destroyed.
2742 ret
= notify_thread_pipe(wpipe
);
2744 PERROR("write kernel poll pipe");
2747 ret
= session_destroy(session
);
2753 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2755 int cmd_register_consumer(struct ltt_session
*session
,
2756 enum lttng_domain_type domain
, const char *sock_path
,
2757 struct consumer_data
*cdata
)
2760 struct consumer_socket
*socket
= NULL
;
2767 case LTTNG_DOMAIN_KERNEL
:
2769 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2773 /* Can't register a consumer if there is already one */
2774 if (ksess
->consumer_fds_sent
!= 0) {
2775 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2779 sock
= lttcomm_connect_unix_sock(sock_path
);
2781 ret
= LTTNG_ERR_CONNECT_FAIL
;
2784 cdata
->cmd_sock
= sock
;
2786 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2787 if (socket
== NULL
) {
2790 PERROR("close register consumer");
2792 cdata
->cmd_sock
= -1;
2793 ret
= LTTNG_ERR_FATAL
;
2797 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2798 if (socket
->lock
== NULL
) {
2799 PERROR("zmalloc pthread mutex");
2800 ret
= LTTNG_ERR_FATAL
;
2803 pthread_mutex_init(socket
->lock
, NULL
);
2804 socket
->registered
= 1;
2807 consumer_add_socket(socket
, ksess
->consumer
);
2810 pthread_mutex_lock(&cdata
->pid_mutex
);
2812 pthread_mutex_unlock(&cdata
->pid_mutex
);
2817 /* TODO: Userspace tracing */
2818 ret
= LTTNG_ERR_UND
;
2826 consumer_destroy_socket(socket
);
2832 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2834 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2835 struct lttng_domain
**domains
)
2840 struct lttng_ht_iter iter
;
2842 if (session
->kernel_session
!= NULL
) {
2843 DBG3("Listing domains found kernel domain");
2847 if (session
->ust_session
!= NULL
) {
2848 DBG3("Listing domains found UST global domain");
2852 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2854 if (agt
->being_used
) {
2865 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2866 if (*domains
== NULL
) {
2867 ret
= LTTNG_ERR_FATAL
;
2871 if (session
->kernel_session
!= NULL
) {
2872 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2874 /* Kernel session buffer type is always GLOBAL */
2875 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2880 if (session
->ust_session
!= NULL
) {
2881 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2882 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2886 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2888 if (agt
->being_used
) {
2889 (*domains
)[index
].type
= agt
->domain
;
2890 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2900 /* Return negative value to differentiate return code */
2906 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2908 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2909 struct ltt_session
*session
, struct lttng_channel
**channels
)
2911 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2914 case LTTNG_DOMAIN_KERNEL
:
2915 if (session
->kernel_session
!= NULL
) {
2916 nb_chan
= session
->kernel_session
->channel_count
;
2918 DBG3("Number of kernel channels %zd", nb_chan
);
2920 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2924 case LTTNG_DOMAIN_UST
:
2925 if (session
->ust_session
!= NULL
) {
2927 nb_chan
= lttng_ht_get_count(
2928 session
->ust_session
->domain_global
.channels
);
2931 DBG3("Number of UST global channels %zd", nb_chan
);
2933 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2938 ret
= -LTTNG_ERR_UND
;
2943 const size_t channel_size
= sizeof(struct lttng_channel
) +
2944 sizeof(struct lttcomm_channel_extended
);
2945 struct lttcomm_channel_extended
*channel_exts
;
2947 payload_size
= nb_chan
* channel_size
;
2948 *channels
= zmalloc(payload_size
);
2949 if (*channels
== NULL
) {
2950 ret
= -LTTNG_ERR_FATAL
;
2954 channel_exts
= ((void *) *channels
) +
2955 (nb_chan
* sizeof(struct lttng_channel
));
2956 list_lttng_channels(domain
, session
, *channels
, channel_exts
);
2967 * Command LTTNG_LIST_EVENTS processed by the client thread.
2969 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2970 struct ltt_session
*session
, char *channel_name
,
2971 struct lttng_event
**events
, size_t *total_size
)
2974 ssize_t nb_event
= 0;
2977 case LTTNG_DOMAIN_KERNEL
:
2978 if (session
->kernel_session
!= NULL
) {
2979 nb_event
= list_lttng_kernel_events(channel_name
,
2980 session
->kernel_session
, events
,
2984 case LTTNG_DOMAIN_UST
:
2986 if (session
->ust_session
!= NULL
) {
2987 nb_event
= list_lttng_ust_global_events(channel_name
,
2988 &session
->ust_session
->domain_global
, events
,
2993 case LTTNG_DOMAIN_LOG4J
:
2994 case LTTNG_DOMAIN_JUL
:
2995 case LTTNG_DOMAIN_PYTHON
:
2996 if (session
->ust_session
) {
2997 struct lttng_ht_iter iter
;
3001 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3002 &iter
.iter
, agt
, node
.node
) {
3003 if (agt
->domain
== domain
) {
3004 nb_event
= list_lttng_agent_events(
3014 ret
= LTTNG_ERR_UND
;
3021 /* Return negative value to differentiate return code */
3026 * Using the session list, filled a lttng_session array to send back to the
3027 * client for session listing.
3029 * The session list lock MUST be acquired before calling this function. Use
3030 * session_lock_list() and session_unlock_list().
3032 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3037 struct ltt_session
*session
;
3038 struct ltt_session_list
*list
= session_get_list();
3040 DBG("Getting all available session for UID %d GID %d",
3043 * Iterate over session list and append data after the control struct in
3046 cds_list_for_each_entry(session
, &list
->head
, list
) {
3048 * Only list the sessions the user can control.
3050 if (!session_access_ok(session
, uid
, gid
)) {
3054 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3055 struct ltt_ust_session
*usess
= session
->ust_session
;
3057 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3058 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3059 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3060 ret
= build_network_session_path(sessions
[i
].path
,
3061 sizeof(sessions
[i
].path
), session
);
3063 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3064 session
->consumer
->dst
.trace_path
);
3067 PERROR("snprintf session path");
3071 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3072 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3073 sessions
[i
].enabled
= session
->active
;
3074 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3075 sessions
[i
].live_timer_interval
= session
->live_timer
;
3081 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3082 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3084 int cmd_data_pending(struct ltt_session
*session
)
3087 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3088 struct ltt_ust_session
*usess
= session
->ust_session
;
3092 /* Session MUST be stopped to ask for data availability. */
3093 if (session
->active
) {
3094 ret
= LTTNG_ERR_SESSION_STARTED
;
3098 * If stopped, just make sure we've started before else the above call
3099 * will always send that there is data pending.
3101 * The consumer assumes that when the data pending command is received,
3102 * the trace has been started before or else no output data is written
3103 * by the streams which is a condition for data pending. So, this is
3104 * *VERY* important that we don't ask the consumer before a start
3107 if (!session
->has_been_started
) {
3113 if (ksess
&& ksess
->consumer
) {
3114 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3116 /* Data is still being extracted for the kernel. */
3121 if (usess
&& usess
->consumer
) {
3122 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3124 /* Data is still being extracted for the kernel. */
3129 /* Data is ready to be read by a viewer */
3137 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3139 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3141 int cmd_snapshot_add_output(struct ltt_session
*session
,
3142 struct lttng_snapshot_output
*output
, uint32_t *id
)
3145 struct snapshot_output
*new_output
;
3150 DBG("Cmd snapshot add output for session %s", session
->name
);
3153 * Can't create an output if the session is not set in no-output mode.
3155 if (session
->output_traces
) {
3156 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3160 /* Only one output is allowed until we have the "tee" feature. */
3161 if (session
->snapshot
.nb_output
== 1) {
3162 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3166 new_output
= snapshot_output_alloc();
3168 ret
= LTTNG_ERR_NOMEM
;
3172 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3173 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3174 &session
->snapshot
);
3176 if (ret
== -ENOMEM
) {
3177 ret
= LTTNG_ERR_NOMEM
;
3179 ret
= LTTNG_ERR_INVALID
;
3185 snapshot_add_output(&session
->snapshot
, new_output
);
3187 *id
= new_output
->id
;
3194 snapshot_output_destroy(new_output
);
3200 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3202 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3204 int cmd_snapshot_del_output(struct ltt_session
*session
,
3205 struct lttng_snapshot_output
*output
)
3208 struct snapshot_output
*sout
= NULL
;
3216 * Permission denied to create an output if the session is not
3217 * set in no output mode.
3219 if (session
->output_traces
) {
3220 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3225 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3227 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3228 } else if (*output
->name
!= '\0') {
3229 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3231 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3234 ret
= LTTNG_ERR_INVALID
;
3238 snapshot_delete_output(&session
->snapshot
, sout
);
3239 snapshot_output_destroy(sout
);
3248 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3250 * If no output is available, outputs is untouched and 0 is returned.
3252 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3254 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3255 struct lttng_snapshot_output
**outputs
)
3258 struct lttng_snapshot_output
*list
= NULL
;
3259 struct lttng_ht_iter iter
;
3260 struct snapshot_output
*output
;
3265 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3268 * Permission denied to create an output if the session is not
3269 * set in no output mode.
3271 if (session
->output_traces
) {
3272 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3276 if (session
->snapshot
.nb_output
== 0) {
3281 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3283 ret
= -LTTNG_ERR_NOMEM
;
3287 /* Copy list from session to the new list object. */
3289 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3290 output
, node
.node
) {
3291 assert(output
->consumer
);
3292 list
[idx
].id
= output
->id
;
3293 list
[idx
].max_size
= output
->max_size
;
3294 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3295 sizeof(list
[idx
].name
))) {
3296 ret
= -LTTNG_ERR_INVALID
;
3299 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3300 if (lttng_strncpy(list
[idx
].ctrl_url
,
3301 output
->consumer
->dst
.trace_path
,
3302 sizeof(list
[idx
].ctrl_url
))) {
3303 ret
= -LTTNG_ERR_INVALID
;
3308 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3309 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3311 ret
= -LTTNG_ERR_NOMEM
;
3316 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3317 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3319 ret
= -LTTNG_ERR_NOMEM
;
3328 ret
= session
->snapshot
.nb_output
;
3337 * Check if we can regenerate the metadata for this session.
3338 * Only kernel, UST per-uid and non-live sessions are supported.
3340 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3343 int check_regenerate_metadata_support(struct ltt_session
*session
)
3349 if (session
->live_timer
!= 0) {
3350 ret
= LTTNG_ERR_LIVE_SESSION
;
3353 if (!session
->active
) {
3354 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3357 if (session
->ust_session
) {
3358 switch (session
->ust_session
->buffer_type
) {
3359 case LTTNG_BUFFER_PER_UID
:
3361 case LTTNG_BUFFER_PER_PID
:
3362 ret
= LTTNG_ERR_PER_PID_SESSION
;
3366 ret
= LTTNG_ERR_UNK
;
3370 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3371 session
->consumer
->relay_minor_version
< 8) {
3372 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3382 int clear_metadata_file(int fd
)
3386 ret
= lseek(fd
, 0, SEEK_SET
);
3392 ret
= ftruncate(fd
, 0);
3394 PERROR("ftruncate");
3403 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3406 struct buffer_reg_uid
*uid_reg
= NULL
;
3407 struct buffer_reg_session
*session_reg
= NULL
;
3410 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3411 struct ust_registry_session
*registry
;
3412 struct ust_registry_channel
*chan
;
3413 struct lttng_ht_iter iter_chan
;
3415 session_reg
= uid_reg
->registry
;
3416 registry
= session_reg
->reg
.ust
;
3418 pthread_mutex_lock(®istry
->lock
);
3419 registry
->metadata_len_sent
= 0;
3420 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3421 registry
->metadata_len
= 0;
3422 registry
->metadata_version
++;
3423 if (registry
->metadata_fd
> 0) {
3424 /* Clear the metadata file's content. */
3425 ret
= clear_metadata_file(registry
->metadata_fd
);
3427 pthread_mutex_unlock(®istry
->lock
);
3432 ret
= ust_metadata_session_statedump(registry
, NULL
,
3433 registry
->major
, registry
->minor
);
3435 pthread_mutex_unlock(®istry
->lock
);
3436 ERR("Failed to generate session metadata (err = %d)",
3440 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3442 struct ust_registry_event
*event
;
3443 struct lttng_ht_iter iter_event
;
3445 ret
= ust_metadata_channel_statedump(registry
, chan
);
3447 pthread_mutex_unlock(®istry
->lock
);
3448 ERR("Failed to generate channel metadata "
3452 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3454 ret
= ust_metadata_event_statedump(registry
,
3457 pthread_mutex_unlock(®istry
->lock
);
3458 ERR("Failed to generate event metadata "
3464 pthread_mutex_unlock(®istry
->lock
);
3473 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3475 * Ask the consumer to truncate the existing metadata file(s) and
3476 * then regenerate the metadata. Live and per-pid sessions are not
3477 * supported and return an error.
3479 * Return 0 on success or else a LTTNG_ERR code.
3481 int cmd_regenerate_metadata(struct ltt_session
*session
)
3487 ret
= check_regenerate_metadata_support(session
);
3492 if (session
->kernel_session
) {
3493 ret
= kernctl_session_regenerate_metadata(
3494 session
->kernel_session
->fd
);
3496 ERR("Failed to regenerate the kernel metadata");
3501 if (session
->ust_session
) {
3502 ret
= ust_regenerate_metadata(session
->ust_session
);
3504 ERR("Failed to regenerate the UST metadata");
3508 DBG("Cmd metadata regenerate for session %s", session
->name
);
3516 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3518 * Ask the tracer to regenerate a new statedump.
3520 * Return 0 on success or else a LTTNG_ERR code.
3522 int cmd_regenerate_statedump(struct ltt_session
*session
)
3528 if (!session
->active
) {
3529 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3534 if (session
->kernel_session
) {
3535 ret
= kernctl_session_regenerate_statedump(
3536 session
->kernel_session
->fd
);
3538 * Currently, the statedump in kernel can only fail if out
3542 if (ret
== -ENOMEM
) {
3543 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3545 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3547 ERR("Failed to regenerate the kernel statedump");
3552 if (session
->ust_session
) {
3553 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3555 * Currently, the statedump in UST always returns 0.
3558 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3559 ERR("Failed to regenerate the UST statedump");
3563 DBG("Cmd regenerate statedump for session %s", session
->name
);
3570 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
)
3574 ssize_t sock_recv_len
;
3575 char *trigger_buffer
= NULL
;
3576 struct lttng_trigger
*trigger
= NULL
;
3578 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.header
.len
;
3579 trigger_buffer
= zmalloc(trigger_len
);
3580 if (!trigger_buffer
) {
3581 ret
= LTTNG_ERR_NOMEM
;
3585 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
,
3587 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3588 ERR("Failed to receive \"register trigger\" command payload");
3592 if (lttng_trigger_create_from_buffer(trigger_buffer
, &trigger
) !=
3594 ERR("Invalid trigger payload received in \"register trigger\" command");
3595 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3598 DBG("Command register trigger succeeded");
3601 free(trigger_buffer
);
3602 lttng_trigger_destroy(trigger
);
3607 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3608 * snapshot output is *not* set with a remote destination.
3610 * Return 0 on success or a LTTNG_ERR code.
3612 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3613 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3616 struct lttng_ht_iter iter
;
3617 struct consumer_socket
*socket
;
3620 assert(snap_output
);
3623 DBG2("Set relayd object from snapshot output");
3625 /* Ignore if snapshot consumer output is not network. */
3626 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3631 * For each consumer socket, create and send the relayd object of the
3635 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3636 socket
, node
.node
) {
3637 ret
= send_consumer_relayd_sockets(0, session
->id
,
3638 snap_output
->consumer
, socket
,
3639 session
->name
, session
->hostname
,
3640 session
->live_timer
);
3641 if (ret
!= LTTNG_OK
) {
3653 * Record a kernel snapshot.
3655 * Return LTTNG_OK on success or a LTTNG_ERR code.
3657 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3658 struct snapshot_output
*output
, struct ltt_session
*session
,
3659 int wait
, uint64_t nb_packets_per_stream
)
3669 * Copy kernel session sockets so we can communicate with the right
3670 * consumer for the snapshot record command.
3672 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3674 ret
= LTTNG_ERR_NOMEM
;
3678 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3679 if (ret
!= LTTNG_OK
) {
3680 goto error_snapshot
;
3683 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3684 if (ret
!= LTTNG_OK
) {
3685 goto error_snapshot
;
3692 /* Clean up copied sockets so this output can use some other later on. */
3693 consumer_destroy_output_sockets(output
->consumer
);
3700 * Record a UST snapshot.
3702 * Return 0 on success or a LTTNG_ERR error code.
3704 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3705 struct snapshot_output
*output
, struct ltt_session
*session
,
3706 int wait
, uint64_t nb_packets_per_stream
)
3715 * Copy UST session sockets so we can communicate with the right
3716 * consumer for the snapshot record command.
3718 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3720 ret
= LTTNG_ERR_NOMEM
;
3724 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3725 if (ret
!= LTTNG_OK
) {
3726 goto error_snapshot
;
3729 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3733 ret
= LTTNG_ERR_INVALID
;
3736 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3739 goto error_snapshot
;
3745 /* Clean up copied sockets so this output can use some other later on. */
3746 consumer_destroy_output_sockets(output
->consumer
);
3752 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3753 uint64_t cur_nr_packets
)
3755 uint64_t tot_size
= 0;
3757 if (session
->kernel_session
) {
3758 struct ltt_kernel_channel
*chan
;
3759 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3761 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3762 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3764 * Don't take channel into account if we
3765 * already grab all its packets.
3769 tot_size
+= chan
->channel
->attr
.subbuf_size
3770 * chan
->stream_count
;
3774 if (session
->ust_session
) {
3775 struct ltt_ust_session
*usess
= session
->ust_session
;
3777 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3785 * Calculate the number of packets we can grab from each stream that
3786 * fits within the overall snapshot max size.
3788 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3789 * the number of packets per stream.
3791 * TODO: this approach is not perfect: we consider the worse case
3792 * (packet filling the sub-buffers) as an upper bound, but we could do
3793 * better if we do this calculation while we actually grab the packet
3794 * content: we would know how much padding we don't actually store into
3797 * This algorithm is currently bounded by the number of packets per
3800 * Since we call this algorithm before actually grabbing the data, it's
3801 * an approximation: for instance, applications could appear/disappear
3802 * in between this call and actually grabbing data.
3805 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3808 uint64_t cur_nb_packets
= 0;
3811 return 0; /* Infinite */
3814 size_left
= max_size
;
3816 uint64_t one_more_packet_tot_size
;
3818 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3820 if (!one_more_packet_tot_size
) {
3821 /* We are already grabbing all packets. */
3824 size_left
-= one_more_packet_tot_size
;
3825 if (size_left
< 0) {
3830 if (!cur_nb_packets
) {
3831 /* Not enough room to grab one packet of each stream, error. */
3834 return cur_nb_packets
;
3838 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3840 * The wait parameter is ignored so this call always wait for the snapshot to
3841 * complete before returning.
3843 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3845 int cmd_snapshot_record(struct ltt_session
*session
,
3846 struct lttng_snapshot_output
*output
, int wait
)
3849 unsigned int use_tmp_output
= 0;
3850 struct snapshot_output tmp_output
;
3851 unsigned int snapshot_success
= 0;
3857 DBG("Cmd snapshot record for session %s", session
->name
);
3859 /* Get the datetime for the snapshot output directory. */
3860 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3863 ret
= LTTNG_ERR_INVALID
;
3868 * Permission denied to create an output if the session is not
3869 * set in no output mode.
3871 if (session
->output_traces
) {
3872 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3876 /* The session needs to be started at least once. */
3877 if (!session
->has_been_started
) {
3878 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3882 /* Use temporary output for the session. */
3883 if (*output
->ctrl_url
!= '\0') {
3884 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3885 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3888 if (ret
== -ENOMEM
) {
3889 ret
= LTTNG_ERR_NOMEM
;
3891 ret
= LTTNG_ERR_INVALID
;
3895 /* Use the global session count for the temporary snapshot. */
3896 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3898 /* Use the global datetime */
3899 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3903 if (use_tmp_output
) {
3904 int64_t nb_packets_per_stream
;
3906 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3907 tmp_output
.max_size
);
3908 if (nb_packets_per_stream
< 0) {
3909 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3913 if (session
->kernel_session
) {
3914 ret
= record_kernel_snapshot(session
->kernel_session
,
3915 &tmp_output
, session
,
3916 wait
, nb_packets_per_stream
);
3917 if (ret
!= LTTNG_OK
) {
3922 if (session
->ust_session
) {
3923 ret
= record_ust_snapshot(session
->ust_session
,
3924 &tmp_output
, session
,
3925 wait
, nb_packets_per_stream
);
3926 if (ret
!= LTTNG_OK
) {
3931 snapshot_success
= 1;
3933 struct snapshot_output
*sout
;
3934 struct lttng_ht_iter iter
;
3937 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3938 &iter
.iter
, sout
, node
.node
) {
3939 int64_t nb_packets_per_stream
;
3942 * Make a local copy of the output and assign the possible
3943 * temporary value given by the caller.
3945 memset(&tmp_output
, 0, sizeof(tmp_output
));
3946 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3948 if (output
->max_size
!= (uint64_t) -1ULL) {
3949 tmp_output
.max_size
= output
->max_size
;
3952 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3953 tmp_output
.max_size
);
3954 if (nb_packets_per_stream
< 0) {
3955 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3960 /* Use temporary name. */
3961 if (*output
->name
!= '\0') {
3962 if (lttng_strncpy(tmp_output
.name
, output
->name
,
3963 sizeof(tmp_output
.name
))) {
3964 ret
= LTTNG_ERR_INVALID
;
3970 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3971 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3973 if (session
->kernel_session
) {
3974 ret
= record_kernel_snapshot(session
->kernel_session
,
3975 &tmp_output
, session
,
3976 wait
, nb_packets_per_stream
);
3977 if (ret
!= LTTNG_OK
) {
3983 if (session
->ust_session
) {
3984 ret
= record_ust_snapshot(session
->ust_session
,
3985 &tmp_output
, session
,
3986 wait
, nb_packets_per_stream
);
3987 if (ret
!= LTTNG_OK
) {
3992 snapshot_success
= 1;
3997 if (snapshot_success
) {
3998 session
->snapshot
.nb_snapshot
++;
4000 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4008 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4010 int cmd_set_session_shm_path(struct ltt_session
*session
,
4011 const char *shm_path
)
4017 * Can only set shm path before session is started.
4019 if (session
->has_been_started
) {
4020 return LTTNG_ERR_SESSION_STARTED
;
4023 strncpy(session
->shm_path
, shm_path
,
4024 sizeof(session
->shm_path
));
4025 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4031 * Init command subsystem.
4036 * Set network sequence index to 1 for streams to match a relayd
4037 * socket on the consumer side.
4039 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4040 relayd_net_seq_idx
= 1;
4041 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4043 DBG("Command subsystem initialized");