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>
36 #include "health-sessiond.h"
38 #include "kernel-consumer.h"
39 #include "lttng-sessiond.h"
43 #include "buffer-registry.h"
44 #include "agent-thread.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 ssize_t
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
;
330 return -LTTNG_ERR_FATAL
;
336 static void increment_extended_len(const char *filter_expression
,
337 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
339 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
341 if (filter_expression
) {
342 *extended_len
+= strlen(filter_expression
) + 1;
346 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
350 static void append_extended_info(const char *filter_expression
,
351 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
353 struct lttcomm_event_extended_header extended_header
;
354 size_t filter_len
= 0;
355 size_t nb_exclusions
= 0;
357 if (filter_expression
) {
358 filter_len
= strlen(filter_expression
) + 1;
362 nb_exclusions
= exclusion
->count
;
365 /* Set header fields */
366 extended_header
.filter_len
= filter_len
;
367 extended_header
.nb_exclusions
= nb_exclusions
;
370 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
371 *extended_at
+= sizeof(extended_header
);
373 /* Copy filter string */
374 if (filter_expression
) {
375 memcpy(*extended_at
, filter_expression
, filter_len
);
376 *extended_at
+= filter_len
;
379 /* Copy exclusion names */
381 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
383 memcpy(*extended_at
, &exclusion
->names
, len
);
389 * Create a list of agent domain events.
391 * Return number of events in list on success or else a negative value.
393 static int list_lttng_agent_events(struct agent
*agt
,
394 struct lttng_event
**events
, size_t *total_size
)
397 unsigned int nb_event
= 0;
398 struct agent_event
*event
;
399 struct lttng_event
*tmp_events
;
400 struct lttng_ht_iter iter
;
401 size_t extended_len
= 0;
407 DBG3("Listing agent events");
410 nb_event
= lttng_ht_get_count(agt
->events
);
418 /* Compute required extended infos size */
419 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
422 * This is only valid because the commands which add events are
423 * processed in the same thread as the listing.
426 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
427 increment_extended_len(event
->filter_expression
, NULL
,
432 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
433 tmp_events
= zmalloc(*total_size
);
435 PERROR("zmalloc agent events session");
436 ret
= -LTTNG_ERR_FATAL
;
440 extended_at
= ((uint8_t *) tmp_events
) +
441 nb_event
* sizeof(struct lttng_event
);
444 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
445 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
446 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
447 tmp_events
[i
].enabled
= event
->enabled
;
448 tmp_events
[i
].loglevel
= event
->loglevel_value
;
449 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
452 /* Append extended info */
453 append_extended_info(event
->filter_expression
, NULL
,
458 *events
= tmp_events
;
462 assert(nb_event
== i
);
467 * Create a list of ust global domain events.
469 static int list_lttng_ust_global_events(char *channel_name
,
470 struct ltt_ust_domain_global
*ust_global
,
471 struct lttng_event
**events
, size_t *total_size
)
474 unsigned int nb_event
= 0;
475 struct lttng_ht_iter iter
;
476 struct lttng_ht_node_str
*node
;
477 struct ltt_ust_channel
*uchan
;
478 struct ltt_ust_event
*uevent
;
479 struct lttng_event
*tmp
;
480 size_t extended_len
= 0;
483 DBG("Listing UST global events for channel %s", channel_name
);
487 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
488 node
= lttng_ht_iter_get_node_str(&iter
);
490 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
494 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
496 nb_event
= lttng_ht_get_count(uchan
->events
);
503 DBG3("Listing UST global %d events", nb_event
);
505 /* Compute required extended infos size */
506 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
507 if (uevent
->internal
) {
512 increment_extended_len(uevent
->filter_expression
,
513 uevent
->exclusion
, &extended_len
);
516 /* All events are internal, skip. */
522 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
523 tmp
= zmalloc(*total_size
);
525 ret
= -LTTNG_ERR_FATAL
;
529 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
531 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
532 if (uevent
->internal
) {
533 /* This event should remain hidden from clients */
536 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
537 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
538 tmp
[i
].enabled
= uevent
->enabled
;
540 switch (uevent
->attr
.instrumentation
) {
541 case LTTNG_UST_TRACEPOINT
:
542 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
544 case LTTNG_UST_PROBE
:
545 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
547 case LTTNG_UST_FUNCTION
:
548 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
552 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
553 switch (uevent
->attr
.loglevel_type
) {
554 case LTTNG_UST_LOGLEVEL_ALL
:
555 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
557 case LTTNG_UST_LOGLEVEL_RANGE
:
558 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
560 case LTTNG_UST_LOGLEVEL_SINGLE
:
561 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
564 if (uevent
->filter
) {
567 if (uevent
->exclusion
) {
568 tmp
[i
].exclusion
= 1;
572 /* Append extended info */
573 append_extended_info(uevent
->filter_expression
,
574 uevent
->exclusion
, &extended_at
);
585 * Fill lttng_event array of all kernel events in the channel.
587 static int list_lttng_kernel_events(char *channel_name
,
588 struct ltt_kernel_session
*kernel_session
,
589 struct lttng_event
**events
, size_t *total_size
)
592 unsigned int nb_event
;
593 struct ltt_kernel_event
*event
;
594 struct ltt_kernel_channel
*kchan
;
595 size_t extended_len
= 0;
598 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
600 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
604 nb_event
= kchan
->event_count
;
606 DBG("Listing events for channel %s", kchan
->channel
->name
);
614 /* Compute required extended infos size */
615 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
616 increment_extended_len(event
->filter_expression
, NULL
,
620 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
621 *events
= zmalloc(*total_size
);
622 if (*events
== NULL
) {
623 ret
= LTTNG_ERR_FATAL
;
627 extended_at
= ((void *) *events
) +
628 nb_event
* sizeof(struct lttng_event
);
630 /* Kernel channels */
631 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
632 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
633 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
634 (*events
)[i
].enabled
= event
->enabled
;
635 (*events
)[i
].filter
=
636 (unsigned char) !!event
->filter_expression
;
638 switch (event
->event
->instrumentation
) {
639 case LTTNG_KERNEL_TRACEPOINT
:
640 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
642 case LTTNG_KERNEL_KRETPROBE
:
643 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
644 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
645 sizeof(struct lttng_kernel_kprobe
));
647 case LTTNG_KERNEL_KPROBE
:
648 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
649 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
650 sizeof(struct lttng_kernel_kprobe
));
652 case LTTNG_KERNEL_FUNCTION
:
653 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
654 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
655 sizeof(struct lttng_kernel_function
));
657 case LTTNG_KERNEL_NOOP
:
658 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
660 case LTTNG_KERNEL_SYSCALL
:
661 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
663 case LTTNG_KERNEL_ALL
:
669 /* Append extended info */
670 append_extended_info(event
->filter_expression
, NULL
,
678 /* Negate the error code to differentiate the size from an error */
683 * Add URI so the consumer output object. Set the correct path depending on the
684 * domain adding the default trace directory.
686 static int add_uri_to_consumer(struct consumer_output
*consumer
,
687 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
688 const char *session_name
)
691 const char *default_trace_dir
;
695 if (consumer
== NULL
) {
696 DBG("No consumer detected. Don't add URI. Stopping.");
697 ret
= LTTNG_ERR_NO_CONSUMER
;
702 case LTTNG_DOMAIN_KERNEL
:
703 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
705 case LTTNG_DOMAIN_UST
:
706 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
710 * This case is possible is we try to add the URI to the global tracing
711 * session consumer object which in this case there is no subdir.
713 default_trace_dir
= "";
716 switch (uri
->dtype
) {
719 DBG2("Setting network URI to consumer");
721 if (consumer
->type
== CONSUMER_DST_NET
) {
722 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
723 consumer
->dst
.net
.control_isset
) ||
724 (uri
->stype
== LTTNG_STREAM_DATA
&&
725 consumer
->dst
.net
.data_isset
)) {
726 ret
= LTTNG_ERR_URL_EXIST
;
730 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
733 consumer
->type
= CONSUMER_DST_NET
;
735 /* Set URI into consumer output object */
736 ret
= consumer_set_network_uri(consumer
, uri
);
740 } else if (ret
== 1) {
742 * URI was the same in the consumer so we do not append the subdir
743 * again so to not duplicate output dir.
749 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
750 ret
= consumer_set_subdir(consumer
, session_name
);
752 ret
= LTTNG_ERR_FATAL
;
757 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
758 /* On a new subdir, reappend the default trace dir. */
759 strncat(consumer
->subdir
, default_trace_dir
,
760 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
761 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
766 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
767 memset(consumer
->dst
.trace_path
, 0,
768 sizeof(consumer
->dst
.trace_path
));
769 /* Explicit length checks for strcpy and strcat. */
770 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
771 >= sizeof(consumer
->dst
.trace_path
)) {
772 ret
= LTTNG_ERR_FATAL
;
775 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
776 /* Append default trace dir */
777 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
778 /* Flag consumer as local. */
779 consumer
->type
= CONSUMER_DST_LOCAL
;
790 * Init tracing by creating trace directory and sending fds kernel consumer.
792 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
795 struct lttng_ht_iter iter
;
796 struct consumer_socket
*socket
;
802 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
803 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
805 pthread_mutex_lock(socket
->lock
);
806 ret
= kernel_consumer_send_session(socket
, session
);
807 pthread_mutex_unlock(socket
->lock
);
809 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
821 * Create a socket to the relayd using the URI.
823 * On success, the relayd_sock pointer is set to the created socket.
824 * Else, it's stays untouched and a lttcomm error code is returned.
826 static int create_connect_relayd(struct lttng_uri
*uri
,
827 struct lttcomm_relayd_sock
**relayd_sock
,
828 struct consumer_output
*consumer
)
831 struct lttcomm_relayd_sock
*rsock
;
833 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
834 RELAYD_VERSION_COMM_MINOR
);
836 ret
= LTTNG_ERR_FATAL
;
841 * Connect to relayd so we can proceed with a session creation. This call
842 * can possibly block for an arbitrary amount of time to set the health
843 * state to be in poll execution.
846 ret
= relayd_connect(rsock
);
849 ERR("Unable to reach lttng-relayd");
850 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
854 /* Create socket for control stream. */
855 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
856 DBG3("Creating relayd stream socket from URI");
858 /* Check relayd version */
859 ret
= relayd_version_check(rsock
);
860 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
862 } else if (ret
< 0) {
863 ERR("Unable to reach lttng-relayd");
864 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
867 consumer
->relay_major_version
= rsock
->major
;
868 consumer
->relay_minor_version
= rsock
->minor
;
869 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
870 DBG3("Creating relayd data socket from URI");
872 /* Command is not valid */
873 ERR("Relayd invalid stream type: %d", uri
->stype
);
874 ret
= LTTNG_ERR_INVALID
;
878 *relayd_sock
= rsock
;
883 /* The returned value is not useful since we are on an error path. */
884 (void) relayd_close(rsock
);
892 * Connect to the relayd using URI and send the socket to the right consumer.
894 * The consumer socket lock must be held by the caller.
896 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
897 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
898 struct consumer_output
*consumer
,
899 struct consumer_socket
*consumer_sock
,
900 char *session_name
, char *hostname
, int session_live_timer
)
903 struct lttcomm_relayd_sock
*rsock
= NULL
;
905 /* Connect to relayd and make version check if uri is the control. */
906 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
907 if (ret
!= LTTNG_OK
) {
908 goto relayd_comm_error
;
912 /* Set the network sequence index if not set. */
913 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
914 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
916 * Increment net_seq_idx because we are about to transfer the
917 * new relayd socket to the consumer.
918 * Assign unique key so the consumer can match streams.
920 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
921 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
924 /* Send relayd socket to consumer. */
925 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
926 relayd_uri
->stype
, session_id
,
927 session_name
, hostname
, session_live_timer
);
929 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
933 /* Flag that the corresponding socket was sent. */
934 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
935 consumer_sock
->control_sock_sent
= 1;
936 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
937 consumer_sock
->data_sock_sent
= 1;
943 * Close socket which was dup on the consumer side. The session daemon does
944 * NOT keep track of the relayd socket(s) once transfer to the consumer.
948 if (ret
!= LTTNG_OK
) {
950 * The consumer output for this session should not be used anymore
951 * since the relayd connection failed thus making any tracing or/and
952 * streaming not usable.
954 consumer
->enabled
= 0;
956 (void) relayd_close(rsock
);
964 * Send both relayd sockets to a specific consumer and domain. This is a
965 * helper function to facilitate sending the information to the consumer for a
968 * The consumer socket lock must be held by the caller.
970 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
971 unsigned int session_id
, struct consumer_output
*consumer
,
972 struct consumer_socket
*sock
, char *session_name
,
973 char *hostname
, int session_live_timer
)
980 /* Sending control relayd socket. */
981 if (!sock
->control_sock_sent
) {
982 ret
= send_consumer_relayd_socket(domain
, session_id
,
983 &consumer
->dst
.net
.control
, consumer
, sock
,
984 session_name
, hostname
, session_live_timer
);
985 if (ret
!= LTTNG_OK
) {
990 /* Sending data relayd socket. */
991 if (!sock
->data_sock_sent
) {
992 ret
= send_consumer_relayd_socket(domain
, session_id
,
993 &consumer
->dst
.net
.data
, consumer
, sock
,
994 session_name
, hostname
, session_live_timer
);
995 if (ret
!= LTTNG_OK
) {
1005 * Setup relayd connections for a tracing session. First creates the socket to
1006 * the relayd and send them to the right domain consumer. Consumer type MUST be
1009 int cmd_setup_relayd(struct ltt_session
*session
)
1012 struct ltt_ust_session
*usess
;
1013 struct ltt_kernel_session
*ksess
;
1014 struct consumer_socket
*socket
;
1015 struct lttng_ht_iter iter
;
1019 usess
= session
->ust_session
;
1020 ksess
= session
->kernel_session
;
1022 DBG("Setting relayd for session %s", session
->name
);
1026 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1027 && usess
->consumer
->enabled
) {
1028 /* For each consumer socket, send relayd sockets */
1029 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1030 socket
, node
.node
) {
1031 pthread_mutex_lock(socket
->lock
);
1032 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1033 usess
->consumer
, socket
,
1034 session
->name
, session
->hostname
,
1035 session
->live_timer
);
1036 pthread_mutex_unlock(socket
->lock
);
1037 if (ret
!= LTTNG_OK
) {
1040 /* Session is now ready for network streaming. */
1041 session
->net_handle
= 1;
1043 session
->consumer
->relay_major_version
=
1044 usess
->consumer
->relay_major_version
;
1045 session
->consumer
->relay_minor_version
=
1046 usess
->consumer
->relay_minor_version
;
1049 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1050 && ksess
->consumer
->enabled
) {
1051 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1052 socket
, node
.node
) {
1053 pthread_mutex_lock(socket
->lock
);
1054 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1055 ksess
->consumer
, socket
,
1056 session
->name
, session
->hostname
,
1057 session
->live_timer
);
1058 pthread_mutex_unlock(socket
->lock
);
1059 if (ret
!= LTTNG_OK
) {
1062 /* Session is now ready for network streaming. */
1063 session
->net_handle
= 1;
1065 session
->consumer
->relay_major_version
=
1066 ksess
->consumer
->relay_major_version
;
1067 session
->consumer
->relay_minor_version
=
1068 ksess
->consumer
->relay_minor_version
;
1077 * Start a kernel session by opening all necessary streams.
1079 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1082 struct ltt_kernel_channel
*kchan
;
1084 /* Open kernel metadata */
1085 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1086 ret
= kernel_open_metadata(ksess
);
1088 ret
= LTTNG_ERR_KERN_META_FAIL
;
1093 /* Open kernel metadata stream */
1094 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1095 ret
= kernel_open_metadata_stream(ksess
);
1097 ERR("Kernel create metadata stream failed");
1098 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1103 /* For each channel */
1104 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1105 if (kchan
->stream_count
== 0) {
1106 ret
= kernel_open_channel_stream(kchan
);
1108 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1111 /* Update the stream global counter */
1112 ksess
->stream_count_global
+= ret
;
1116 /* Setup kernel consumer socket and send fds to it */
1117 ret
= init_kernel_tracing(ksess
);
1119 ret
= LTTNG_ERR_KERN_START_FAIL
;
1123 /* This start the kernel tracing */
1124 ret
= kernel_start_session(ksess
);
1126 ret
= LTTNG_ERR_KERN_START_FAIL
;
1130 /* Quiescent wait after starting trace */
1131 kernel_wait_quiescent(kernel_tracer_fd
);
1142 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1144 int cmd_disable_channel(struct ltt_session
*session
,
1145 enum lttng_domain_type domain
, char *channel_name
)
1148 struct ltt_ust_session
*usess
;
1150 usess
= session
->ust_session
;
1155 case LTTNG_DOMAIN_KERNEL
:
1157 ret
= channel_kernel_disable(session
->kernel_session
,
1159 if (ret
!= LTTNG_OK
) {
1163 kernel_wait_quiescent(kernel_tracer_fd
);
1166 case LTTNG_DOMAIN_UST
:
1168 struct ltt_ust_channel
*uchan
;
1169 struct lttng_ht
*chan_ht
;
1171 chan_ht
= usess
->domain_global
.channels
;
1173 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1174 if (uchan
== NULL
) {
1175 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1179 ret
= channel_ust_disable(usess
, uchan
);
1180 if (ret
!= LTTNG_OK
) {
1186 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1198 * Command LTTNG_TRACK_ID processed by the client thread.
1200 * Called with session lock held.
1202 int cmd_track_id(struct ltt_session
*session
,
1203 enum lttng_tracker_type tracker_type
,
1204 enum lttng_domain_type domain
,
1205 struct lttng_tracker_id
*id
)
1212 case LTTNG_DOMAIN_KERNEL
:
1214 struct ltt_kernel_session
*ksess
;
1216 ksess
= session
->kernel_session
;
1218 ret
= kernel_track_id(tracker_type
, ksess
, id
);
1219 if (ret
!= LTTNG_OK
) {
1223 kernel_wait_quiescent(kernel_tracer_fd
);
1226 case LTTNG_DOMAIN_UST
:
1228 struct ltt_ust_session
*usess
;
1230 usess
= session
->ust_session
;
1232 ret
= trace_ust_track_id(tracker_type
, usess
, id
);
1233 if (ret
!= LTTNG_OK
) {
1239 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1251 * Command LTTNG_UNTRACK_ID processed by the client thread.
1253 * Called with session lock held.
1255 int cmd_untrack_id(struct ltt_session
*session
,
1256 enum lttng_tracker_type tracker_type
,
1257 enum lttng_domain_type domain
,
1258 struct lttng_tracker_id
*id
)
1265 case LTTNG_DOMAIN_KERNEL
:
1267 struct ltt_kernel_session
*ksess
;
1269 ksess
= session
->kernel_session
;
1271 ret
= kernel_untrack_id(tracker_type
, ksess
, id
);
1272 if (ret
!= LTTNG_OK
) {
1276 kernel_wait_quiescent(kernel_tracer_fd
);
1279 case LTTNG_DOMAIN_UST
:
1281 struct ltt_ust_session
*usess
;
1283 usess
= session
->ust_session
;
1285 ret
= trace_ust_untrack_id(tracker_type
, usess
, id
);
1286 if (ret
!= LTTNG_OK
) {
1292 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1304 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1306 * The wpipe arguments is used as a notifier for the kernel thread.
1308 int cmd_enable_channel(struct ltt_session
*session
,
1309 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1312 struct ltt_ust_session
*usess
= session
->ust_session
;
1313 struct lttng_ht
*chan_ht
;
1320 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1322 /* Validate channel name */
1323 if (attr
->name
[0] == '.' ||
1324 memchr(attr
->name
, '/', len
) != NULL
) {
1325 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1329 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1334 * Don't try to enable a channel if the session has been started at
1335 * some point in time before. The tracer does not allow it.
1337 if (session
->has_been_started
) {
1338 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1343 * If the session is a live session, remove the switch timer, the
1344 * live timer does the same thing but sends also synchronisation
1345 * beacons for inactive streams.
1347 if (session
->live_timer
> 0) {
1348 attr
->attr
.live_timer_interval
= session
->live_timer
;
1349 attr
->attr
.switch_timer_interval
= 0;
1352 /* Check for feature support */
1353 switch (domain
->type
) {
1354 case LTTNG_DOMAIN_JUL
:
1355 case LTTNG_DOMAIN_LOG4J
:
1356 case LTTNG_DOMAIN_PYTHON
:
1357 if (!agent_tracing_is_enabled()) {
1358 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1359 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1367 switch (domain
->type
) {
1368 case LTTNG_DOMAIN_KERNEL
:
1370 struct ltt_kernel_channel
*kchan
;
1372 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1373 session
->kernel_session
);
1374 if (kchan
== NULL
) {
1375 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1376 if (attr
->name
[0] != '\0') {
1377 session
->kernel_session
->has_non_default_channel
= 1;
1380 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1383 if (ret
!= LTTNG_OK
) {
1387 kernel_wait_quiescent(kernel_tracer_fd
);
1390 case LTTNG_DOMAIN_UST
:
1391 case LTTNG_DOMAIN_JUL
:
1392 case LTTNG_DOMAIN_LOG4J
:
1393 case LTTNG_DOMAIN_PYTHON
:
1395 struct ltt_ust_channel
*uchan
;
1400 * Current agent implementation limitations force us to allow
1401 * only one channel at once in "agent" subdomains. Each
1402 * subdomain has a default channel name which must be strictly
1405 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1406 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1407 LTTNG_SYMBOL_NAME_LEN
)) {
1408 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1411 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1412 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1413 LTTNG_SYMBOL_NAME_LEN
)) {
1414 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1417 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1418 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1419 LTTNG_SYMBOL_NAME_LEN
)) {
1420 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1425 chan_ht
= usess
->domain_global
.channels
;
1427 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1428 if (uchan
== NULL
) {
1429 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1430 if (attr
->name
[0] != '\0') {
1431 usess
->has_non_default_channel
= 1;
1434 ret
= channel_ust_enable(usess
, uchan
);
1439 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1450 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1452 int cmd_disable_event(struct ltt_session
*session
,
1453 enum lttng_domain_type domain
, char *channel_name
,
1454 struct lttng_event
*event
)
1459 DBG("Disable event command for event \'%s\'", event
->name
);
1461 event_name
= event
->name
;
1462 if (validate_event_name(event_name
)) {
1463 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1467 /* Error out on unhandled search criteria */
1468 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1469 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1470 ret
= LTTNG_ERR_UNK
;
1477 case LTTNG_DOMAIN_KERNEL
:
1479 struct ltt_kernel_channel
*kchan
;
1480 struct ltt_kernel_session
*ksess
;
1482 ksess
= session
->kernel_session
;
1485 * If a non-default channel has been created in the
1486 * session, explicitely require that -c chan_name needs
1489 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1490 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1494 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1495 if (kchan
== NULL
) {
1496 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1500 switch (event
->type
) {
1501 case LTTNG_EVENT_ALL
:
1502 case LTTNG_EVENT_TRACEPOINT
:
1503 case LTTNG_EVENT_SYSCALL
:
1504 case LTTNG_EVENT_PROBE
:
1505 case LTTNG_EVENT_FUNCTION
:
1506 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1507 if (event_name
[0] == '\0') {
1508 ret
= event_kernel_disable_event(kchan
,
1511 ret
= event_kernel_disable_event(kchan
,
1512 event_name
, event
->type
);
1514 if (ret
!= LTTNG_OK
) {
1519 ret
= LTTNG_ERR_UNK
;
1523 kernel_wait_quiescent(kernel_tracer_fd
);
1526 case LTTNG_DOMAIN_UST
:
1528 struct ltt_ust_channel
*uchan
;
1529 struct ltt_ust_session
*usess
;
1531 usess
= session
->ust_session
;
1533 if (validate_ust_event_name(event_name
)) {
1534 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1539 * If a non-default channel has been created in the
1540 * session, explicitly require that -c chan_name needs
1543 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1544 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1548 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1550 if (uchan
== NULL
) {
1551 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1555 switch (event
->type
) {
1556 case LTTNG_EVENT_ALL
:
1558 * An empty event name means that everything
1559 * should be disabled.
1561 if (event
->name
[0] == '\0') {
1562 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1564 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1567 if (ret
!= LTTNG_OK
) {
1572 ret
= LTTNG_ERR_UNK
;
1576 DBG3("Disable UST event %s in channel %s completed", event_name
,
1580 case LTTNG_DOMAIN_LOG4J
:
1581 case LTTNG_DOMAIN_JUL
:
1582 case LTTNG_DOMAIN_PYTHON
:
1585 struct ltt_ust_session
*usess
= session
->ust_session
;
1589 switch (event
->type
) {
1590 case LTTNG_EVENT_ALL
:
1593 ret
= LTTNG_ERR_UNK
;
1597 agt
= trace_ust_find_agent(usess
, domain
);
1599 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1603 * An empty event name means that everything
1604 * should be disabled.
1606 if (event
->name
[0] == '\0') {
1607 ret
= event_agent_disable_all(usess
, agt
);
1609 ret
= event_agent_disable(usess
, agt
, event_name
);
1611 if (ret
!= LTTNG_OK
) {
1618 ret
= LTTNG_ERR_UND
;
1631 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1633 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1634 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1636 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1637 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1640 * Don't try to add a context if the session has been started at
1641 * some point in time before. The tracer does not allow it and would
1642 * result in a corrupted trace.
1644 if (session
->has_been_started
) {
1645 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1649 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1650 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1651 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1655 case LTTNG_DOMAIN_KERNEL
:
1656 assert(session
->kernel_session
);
1658 if (session
->kernel_session
->channel_count
== 0) {
1659 /* Create default channel */
1660 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1661 if (ret
!= LTTNG_OK
) {
1664 chan_kern_created
= 1;
1666 /* Add kernel context to kernel tracer */
1667 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1668 if (ret
!= LTTNG_OK
) {
1672 case LTTNG_DOMAIN_JUL
:
1673 case LTTNG_DOMAIN_LOG4J
:
1676 * Validate channel name.
1677 * If no channel name is given and the domain is JUL or LOG4J,
1678 * set it to the appropriate domain-specific channel name. If
1679 * a name is provided but does not match the expexted channel
1680 * name, return an error.
1682 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1683 strcmp(channel_name
,
1684 DEFAULT_JUL_CHANNEL_NAME
)) {
1685 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1687 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1688 strcmp(channel_name
,
1689 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1690 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1693 /* break is _not_ missing here. */
1695 case LTTNG_DOMAIN_UST
:
1697 struct ltt_ust_session
*usess
= session
->ust_session
;
1698 unsigned int chan_count
;
1702 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1703 if (chan_count
== 0) {
1704 struct lttng_channel
*attr
;
1705 /* Create default channel */
1706 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1708 ret
= LTTNG_ERR_FATAL
;
1712 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1713 if (ret
!= LTTNG_OK
) {
1718 chan_ust_created
= 1;
1721 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1722 free(app_ctx_provider_name
);
1724 app_ctx_name
= NULL
;
1725 app_ctx_provider_name
= NULL
;
1726 if (ret
!= LTTNG_OK
) {
1732 ret
= LTTNG_ERR_UND
;
1740 if (chan_kern_created
) {
1741 struct ltt_kernel_channel
*kchan
=
1742 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1743 session
->kernel_session
);
1744 /* Created previously, this should NOT fail. */
1746 kernel_destroy_channel(kchan
);
1749 if (chan_ust_created
) {
1750 struct ltt_ust_channel
*uchan
=
1751 trace_ust_find_channel_by_name(
1752 session
->ust_session
->domain_global
.channels
,
1753 DEFAULT_CHANNEL_NAME
);
1754 /* Created previously, this should NOT fail. */
1756 /* Remove from the channel list of the session. */
1757 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1759 trace_ust_destroy_channel(uchan
);
1762 free(app_ctx_provider_name
);
1767 static int validate_event_name(const char *name
)
1770 const char *c
= name
;
1771 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1772 bool null_terminated
= false;
1775 * Make sure that unescaped wildcards are only used as the last
1776 * character of the event name.
1778 while (c
< event_name_end
) {
1781 null_terminated
= true;
1787 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1788 /* Wildcard is not the last character */
1789 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1798 if (!ret
&& !null_terminated
) {
1799 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1804 static inline bool name_starts_with(const char *name
, const char *prefix
)
1806 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1808 return !strncmp(name
, prefix
, max_cmp_len
);
1811 /* Perform userspace-specific event name validation */
1812 static int validate_ust_event_name(const char *name
)
1822 * Check name against all internal UST event component namespaces used
1825 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1826 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1827 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1836 * Internal version of cmd_enable_event() with a supplemental
1837 * "internal_event" flag which is used to enable internal events which should
1838 * be hidden from clients. Such events are used in the agent implementation to
1839 * enable the events through which all "agent" events are funeled.
1841 static int _cmd_enable_event(struct ltt_session
*session
,
1842 struct lttng_domain
*domain
,
1843 char *channel_name
, struct lttng_event
*event
,
1844 char *filter_expression
,
1845 struct lttng_filter_bytecode
*filter
,
1846 struct lttng_event_exclusion
*exclusion
,
1847 int wpipe
, bool internal_event
)
1849 int ret
, channel_created
= 0;
1850 struct lttng_channel
*attr
= NULL
;
1854 assert(channel_name
);
1856 /* If we have a filter, we must have its filter expression */
1857 assert(!(!!filter_expression
^ !!filter
));
1859 DBG("Enable event command for event \'%s\'", event
->name
);
1863 ret
= validate_event_name(event
->name
);
1868 switch (domain
->type
) {
1869 case LTTNG_DOMAIN_KERNEL
:
1871 struct ltt_kernel_channel
*kchan
;
1874 * If a non-default channel has been created in the
1875 * session, explicitely require that -c chan_name needs
1878 if (session
->kernel_session
->has_non_default_channel
1879 && channel_name
[0] == '\0') {
1880 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1884 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1885 session
->kernel_session
);
1886 if (kchan
== NULL
) {
1887 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1888 LTTNG_BUFFER_GLOBAL
);
1890 ret
= LTTNG_ERR_FATAL
;
1893 if (lttng_strncpy(attr
->name
, channel_name
,
1894 sizeof(attr
->name
))) {
1895 ret
= LTTNG_ERR_INVALID
;
1899 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1900 if (ret
!= LTTNG_OK
) {
1903 channel_created
= 1;
1906 /* Get the newly created kernel channel pointer */
1907 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1908 session
->kernel_session
);
1909 if (kchan
== NULL
) {
1910 /* This sould not happen... */
1911 ret
= LTTNG_ERR_FATAL
;
1915 switch (event
->type
) {
1916 case LTTNG_EVENT_ALL
:
1918 char *filter_expression_a
= NULL
;
1919 struct lttng_filter_bytecode
*filter_a
= NULL
;
1922 * We need to duplicate filter_expression and filter,
1923 * because ownership is passed to first enable
1926 if (filter_expression
) {
1927 filter_expression_a
= strdup(filter_expression
);
1928 if (!filter_expression_a
) {
1929 ret
= LTTNG_ERR_FATAL
;
1934 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1936 free(filter_expression_a
);
1937 ret
= LTTNG_ERR_FATAL
;
1940 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1942 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1943 ret
= event_kernel_enable_event(kchan
, event
,
1944 filter_expression
, filter
);
1945 /* We have passed ownership */
1946 filter_expression
= NULL
;
1948 if (ret
!= LTTNG_OK
) {
1949 if (channel_created
) {
1950 /* Let's not leak a useless channel. */
1951 kernel_destroy_channel(kchan
);
1953 free(filter_expression_a
);
1957 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1958 ret
= event_kernel_enable_event(kchan
, event
,
1959 filter_expression_a
, filter_a
);
1960 /* We have passed ownership */
1961 filter_expression_a
= NULL
;
1963 if (ret
!= LTTNG_OK
) {
1968 case LTTNG_EVENT_PROBE
:
1969 case LTTNG_EVENT_FUNCTION
:
1970 case LTTNG_EVENT_FUNCTION_ENTRY
:
1971 case LTTNG_EVENT_TRACEPOINT
:
1972 ret
= event_kernel_enable_event(kchan
, event
,
1973 filter_expression
, filter
);
1974 /* We have passed ownership */
1975 filter_expression
= NULL
;
1977 if (ret
!= LTTNG_OK
) {
1978 if (channel_created
) {
1979 /* Let's not leak a useless channel. */
1980 kernel_destroy_channel(kchan
);
1985 case LTTNG_EVENT_SYSCALL
:
1986 ret
= event_kernel_enable_event(kchan
, event
,
1987 filter_expression
, filter
);
1988 /* We have passed ownership */
1989 filter_expression
= NULL
;
1991 if (ret
!= LTTNG_OK
) {
1996 ret
= LTTNG_ERR_UNK
;
2000 kernel_wait_quiescent(kernel_tracer_fd
);
2003 case LTTNG_DOMAIN_UST
:
2005 struct ltt_ust_channel
*uchan
;
2006 struct ltt_ust_session
*usess
= session
->ust_session
;
2011 * If a non-default channel has been created in the
2012 * session, explicitely require that -c chan_name needs
2015 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2016 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2020 /* Get channel from global UST domain */
2021 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2023 if (uchan
== NULL
) {
2024 /* Create default channel */
2025 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2026 usess
->buffer_type
);
2028 ret
= LTTNG_ERR_FATAL
;
2031 if (lttng_strncpy(attr
->name
, channel_name
,
2032 sizeof(attr
->name
))) {
2033 ret
= LTTNG_ERR_INVALID
;
2037 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2038 if (ret
!= LTTNG_OK
) {
2042 /* Get the newly created channel reference back */
2043 uchan
= trace_ust_find_channel_by_name(
2044 usess
->domain_global
.channels
, channel_name
);
2048 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2050 * Don't allow users to add UST events to channels which
2051 * are assigned to a userspace subdomain (JUL, Log4J,
2054 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2058 if (!internal_event
) {
2060 * Ensure the event name is not reserved for internal
2063 ret
= validate_ust_event_name(event
->name
);
2065 WARN("Userspace event name %s failed validation.",
2067 event
->name
: "NULL");
2068 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2073 /* At this point, the session and channel exist on the tracer */
2074 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2075 filter_expression
, filter
, exclusion
,
2077 /* We have passed ownership */
2078 filter_expression
= NULL
;
2081 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2082 goto already_enabled
;
2083 } else if (ret
!= LTTNG_OK
) {
2088 case LTTNG_DOMAIN_LOG4J
:
2089 case LTTNG_DOMAIN_JUL
:
2090 case LTTNG_DOMAIN_PYTHON
:
2092 const char *default_event_name
, *default_chan_name
;
2094 struct lttng_event uevent
;
2095 struct lttng_domain tmp_dom
;
2096 struct ltt_ust_session
*usess
= session
->ust_session
;
2100 if (!agent_tracing_is_enabled()) {
2101 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2102 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2106 agt
= trace_ust_find_agent(usess
, domain
->type
);
2108 agt
= agent_create(domain
->type
);
2110 ret
= LTTNG_ERR_NOMEM
;
2113 agent_add(agt
, usess
->agents
);
2116 /* Create the default tracepoint. */
2117 memset(&uevent
, 0, sizeof(uevent
));
2118 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2119 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2120 default_event_name
= event_get_default_agent_ust_name(
2122 if (!default_event_name
) {
2123 ret
= LTTNG_ERR_FATAL
;
2126 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2127 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2130 * The domain type is changed because we are about to enable the
2131 * default channel and event for the JUL domain that are hardcoded.
2132 * This happens in the UST domain.
2134 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2135 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2137 switch (domain
->type
) {
2138 case LTTNG_DOMAIN_LOG4J
:
2139 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2141 case LTTNG_DOMAIN_JUL
:
2142 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2144 case LTTNG_DOMAIN_PYTHON
:
2145 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2148 /* The switch/case we are in makes this impossible */
2153 char *filter_expression_copy
= NULL
;
2154 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2157 const size_t filter_size
= sizeof(
2158 struct lttng_filter_bytecode
)
2161 filter_copy
= zmalloc(filter_size
);
2163 ret
= LTTNG_ERR_NOMEM
;
2166 memcpy(filter_copy
, filter
, filter_size
);
2168 filter_expression_copy
=
2169 strdup(filter_expression
);
2170 if (!filter_expression
) {
2171 ret
= LTTNG_ERR_NOMEM
;
2174 if (!filter_expression_copy
|| !filter_copy
) {
2175 free(filter_expression_copy
);
2181 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2182 (char *) default_chan_name
,
2183 &uevent
, filter_expression_copy
,
2184 filter_copy
, NULL
, wpipe
);
2187 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2188 goto already_enabled
;
2189 } else if (ret
!= LTTNG_OK
) {
2193 /* The wild card * means that everything should be enabled. */
2194 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2195 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2198 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2202 filter_expression
= NULL
;
2203 if (ret
!= LTTNG_OK
) {
2210 ret
= LTTNG_ERR_UND
;
2218 free(filter_expression
);
2227 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2228 * We own filter, exclusion, and filter_expression.
2230 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2231 char *channel_name
, struct lttng_event
*event
,
2232 char *filter_expression
,
2233 struct lttng_filter_bytecode
*filter
,
2234 struct lttng_event_exclusion
*exclusion
,
2237 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2238 filter_expression
, filter
, exclusion
, wpipe
, false);
2242 * Enable an event which is internal to LTTng. An internal should
2243 * never be made visible to clients and are immune to checks such as
2246 static int cmd_enable_event_internal(struct ltt_session
*session
,
2247 struct lttng_domain
*domain
,
2248 char *channel_name
, struct lttng_event
*event
,
2249 char *filter_expression
,
2250 struct lttng_filter_bytecode
*filter
,
2251 struct lttng_event_exclusion
*exclusion
,
2254 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2255 filter_expression
, filter
, exclusion
, wpipe
, true);
2259 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2261 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2262 struct lttng_event
**events
)
2265 ssize_t nb_events
= 0;
2268 case LTTNG_DOMAIN_KERNEL
:
2269 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2270 if (nb_events
< 0) {
2271 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2275 case LTTNG_DOMAIN_UST
:
2276 nb_events
= ust_app_list_events(events
);
2277 if (nb_events
< 0) {
2278 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2282 case LTTNG_DOMAIN_LOG4J
:
2283 case LTTNG_DOMAIN_JUL
:
2284 case LTTNG_DOMAIN_PYTHON
:
2285 nb_events
= agent_list_events(events
, domain
);
2286 if (nb_events
< 0) {
2287 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2292 ret
= LTTNG_ERR_UND
;
2299 /* Return negative value to differentiate return code */
2304 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2306 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2307 struct lttng_event_field
**fields
)
2310 ssize_t nb_fields
= 0;
2313 case LTTNG_DOMAIN_UST
:
2314 nb_fields
= ust_app_list_event_fields(fields
);
2315 if (nb_fields
< 0) {
2316 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2320 case LTTNG_DOMAIN_KERNEL
:
2321 default: /* fall-through */
2322 ret
= LTTNG_ERR_UND
;
2329 /* Return negative value to differentiate return code */
2333 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2335 return syscall_table_list(events
);
2339 * Command LTTNG_LIST_TRACKER_IDS processed by the client thread.
2341 * Called with session lock held.
2343 ssize_t
cmd_list_tracker_ids(enum lttng_tracker_type tracker_type
,
2344 struct ltt_session
*session
,
2345 enum lttng_domain_type domain
,
2346 struct lttng_tracker_id
**ids
)
2349 ssize_t nr_pids
= 0;
2352 case LTTNG_DOMAIN_KERNEL
:
2354 struct ltt_kernel_session
*ksess
;
2356 ksess
= session
->kernel_session
;
2357 nr_pids
= kernel_list_tracker_ids(tracker_type
, ksess
, ids
);
2359 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2364 case LTTNG_DOMAIN_UST
:
2366 struct ltt_ust_session
*usess
;
2368 usess
= session
->ust_session
;
2369 nr_pids
= trace_ust_list_tracker_ids(tracker_type
, usess
, ids
);
2371 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2376 case LTTNG_DOMAIN_LOG4J
:
2377 case LTTNG_DOMAIN_JUL
:
2378 case LTTNG_DOMAIN_PYTHON
:
2380 ret
= LTTNG_ERR_UND
;
2387 /* Return negative value to differentiate return code */
2392 * Command LTTNG_START_TRACE processed by the client thread.
2394 * Called with session mutex held.
2396 int cmd_start_trace(struct ltt_session
*session
)
2399 unsigned long nb_chan
= 0;
2400 struct ltt_kernel_session
*ksession
;
2401 struct ltt_ust_session
*usess
;
2405 /* Ease our life a bit ;) */
2406 ksession
= session
->kernel_session
;
2407 usess
= session
->ust_session
;
2409 /* Is the session already started? */
2410 if (session
->active
) {
2411 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2416 * Starting a session without channel is useless since after that it's not
2417 * possible to enable channel thus inform the client.
2419 if (usess
&& usess
->domain_global
.channels
) {
2420 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2423 nb_chan
+= ksession
->channel_count
;
2426 ret
= LTTNG_ERR_NO_CHANNEL
;
2430 /* Kernel tracing */
2431 if (ksession
!= NULL
) {
2432 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2433 if (ret
!= LTTNG_OK
) {
2438 /* Flag session that trace should start automatically */
2441 * Even though the start trace might fail, flag this session active so
2442 * other application coming in are started by default.
2446 ret
= ust_app_start_trace_all(usess
);
2448 ret
= LTTNG_ERR_UST_START_FAIL
;
2453 /* Flag this after a successful start. */
2454 session
->has_been_started
= 1;
2455 session
->active
= 1;
2464 * Command LTTNG_STOP_TRACE processed by the client thread.
2466 int cmd_stop_trace(struct ltt_session
*session
)
2469 struct ltt_kernel_channel
*kchan
;
2470 struct ltt_kernel_session
*ksession
;
2471 struct ltt_ust_session
*usess
;
2476 ksession
= session
->kernel_session
;
2477 usess
= session
->ust_session
;
2479 /* Session is not active. Skip everythong and inform the client. */
2480 if (!session
->active
) {
2481 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2486 if (ksession
&& ksession
->active
) {
2487 DBG("Stop kernel tracing");
2489 ret
= kernel_stop_session(ksession
);
2491 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2495 kernel_wait_quiescent(kernel_tracer_fd
);
2497 /* Flush metadata after stopping (if exists) */
2498 if (ksession
->metadata_stream_fd
>= 0) {
2499 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2501 ERR("Kernel metadata flush failed");
2505 /* Flush all buffers after stopping */
2506 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2507 ret
= kernel_flush_buffer(kchan
);
2509 ERR("Kernel flush buffer error");
2513 ksession
->active
= 0;
2516 if (usess
&& usess
->active
) {
2518 * Even though the stop trace might fail, flag this session inactive so
2519 * other application coming in are not started by default.
2523 ret
= ust_app_stop_trace_all(usess
);
2525 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2530 /* Flag inactive after a successful stop. */
2531 session
->active
= 0;
2539 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2541 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2542 struct lttng_uri
*uris
)
2545 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2546 struct ltt_ust_session
*usess
= session
->ust_session
;
2552 /* Can't set consumer URI if the session is active. */
2553 if (session
->active
) {
2554 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2558 /* Set the "global" consumer URIs */
2559 for (i
= 0; i
< nb_uri
; i
++) {
2560 ret
= add_uri_to_consumer(session
->consumer
,
2561 &uris
[i
], 0, session
->name
);
2562 if (ret
!= LTTNG_OK
) {
2567 /* Set UST session URIs */
2568 if (session
->ust_session
) {
2569 for (i
= 0; i
< nb_uri
; i
++) {
2570 ret
= add_uri_to_consumer(
2571 session
->ust_session
->consumer
,
2572 &uris
[i
], LTTNG_DOMAIN_UST
,
2574 if (ret
!= LTTNG_OK
) {
2580 /* Set kernel session URIs */
2581 if (session
->kernel_session
) {
2582 for (i
= 0; i
< nb_uri
; i
++) {
2583 ret
= add_uri_to_consumer(
2584 session
->kernel_session
->consumer
,
2585 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2587 if (ret
!= LTTNG_OK
) {
2594 * Make sure to set the session in output mode after we set URI since a
2595 * session can be created without URL (thus flagged in no output mode).
2597 session
->output_traces
= 1;
2599 ksess
->output_traces
= 1;
2603 usess
->output_traces
= 1;
2614 * Command LTTNG_CREATE_SESSION processed by the client thread.
2616 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2617 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2620 struct ltt_session
*session
;
2626 * Verify if the session already exist
2628 * XXX: There is no need for the session lock list here since the caller
2629 * (process_client_msg) is holding it. We might want to change that so a
2630 * single command does not lock the entire session list.
2632 session
= session_find_by_name(name
);
2633 if (session
!= NULL
) {
2634 ret
= LTTNG_ERR_EXIST_SESS
;
2638 /* Create tracing session in the registry */
2639 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2640 LTTNG_SOCK_GET_GID_CRED(creds
));
2641 if (ret
!= LTTNG_OK
) {
2646 * Get the newly created session pointer back
2648 * XXX: There is no need for the session lock list here since the caller
2649 * (process_client_msg) is holding it. We might want to change that so a
2650 * single command does not lock the entire session list.
2652 session
= session_find_by_name(name
);
2655 session
->live_timer
= live_timer
;
2656 /* Create default consumer output for the session not yet created. */
2657 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2658 if (session
->consumer
== NULL
) {
2659 ret
= LTTNG_ERR_FATAL
;
2660 goto consumer_error
;
2664 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2665 if (ret
!= LTTNG_OK
) {
2666 goto consumer_error
;
2668 session
->output_traces
= 1;
2670 session
->output_traces
= 0;
2671 DBG2("Session %s created with no output", session
->name
);
2674 session
->consumer
->enabled
= 1;
2679 session_destroy(session
);
2686 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2688 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2689 size_t nb_uri
, lttng_sock_cred
*creds
)
2692 struct ltt_session
*session
;
2693 struct snapshot_output
*new_output
= NULL
;
2699 * Create session in no output mode with URIs set to NULL. The uris we've
2700 * received are for a default snapshot output if one.
2702 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2703 if (ret
!= LTTNG_OK
) {
2707 /* Get the newly created session pointer back. This should NEVER fail. */
2708 session
= session_find_by_name(name
);
2711 /* Flag session for snapshot mode. */
2712 session
->snapshot_mode
= 1;
2714 /* Skip snapshot output creation if no URI is given. */
2719 new_output
= snapshot_output_alloc();
2721 ret
= LTTNG_ERR_NOMEM
;
2722 goto error_snapshot_alloc
;
2725 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2726 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2728 if (ret
== -ENOMEM
) {
2729 ret
= LTTNG_ERR_NOMEM
;
2731 ret
= LTTNG_ERR_INVALID
;
2733 goto error_snapshot
;
2737 snapshot_add_output(&session
->snapshot
, new_output
);
2744 snapshot_output_destroy(new_output
);
2745 error_snapshot_alloc
:
2746 session_destroy(session
);
2752 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2754 * Called with session lock held.
2756 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2759 struct ltt_ust_session
*usess
;
2760 struct ltt_kernel_session
*ksess
;
2765 usess
= session
->ust_session
;
2766 ksess
= session
->kernel_session
;
2768 /* Clean kernel session teardown */
2769 kernel_destroy_session(ksess
);
2771 /* UST session teardown */
2773 /* Close any relayd session */
2774 consumer_output_send_destroy_relayd(usess
->consumer
);
2776 /* Destroy every UST application related to this session. */
2777 ret
= ust_app_destroy_trace_all(usess
);
2779 ERR("Error in ust_app_destroy_trace_all");
2782 /* Clean up the rest. */
2783 trace_ust_destroy_session(usess
);
2787 * Must notify the kernel thread here to update it's poll set in order to
2788 * remove the channel(s)' fd just destroyed.
2790 ret
= notify_thread_pipe(wpipe
);
2792 PERROR("write kernel poll pipe");
2795 ret
= session_destroy(session
);
2801 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2803 int cmd_register_consumer(struct ltt_session
*session
,
2804 enum lttng_domain_type domain
, const char *sock_path
,
2805 struct consumer_data
*cdata
)
2808 struct consumer_socket
*socket
= NULL
;
2815 case LTTNG_DOMAIN_KERNEL
:
2817 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2821 /* Can't register a consumer if there is already one */
2822 if (ksess
->consumer_fds_sent
!= 0) {
2823 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2827 sock
= lttcomm_connect_unix_sock(sock_path
);
2829 ret
= LTTNG_ERR_CONNECT_FAIL
;
2832 cdata
->cmd_sock
= sock
;
2834 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2835 if (socket
== NULL
) {
2838 PERROR("close register consumer");
2840 cdata
->cmd_sock
= -1;
2841 ret
= LTTNG_ERR_FATAL
;
2845 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2846 if (socket
->lock
== NULL
) {
2847 PERROR("zmalloc pthread mutex");
2848 ret
= LTTNG_ERR_FATAL
;
2851 pthread_mutex_init(socket
->lock
, NULL
);
2852 socket
->registered
= 1;
2855 consumer_add_socket(socket
, ksess
->consumer
);
2858 pthread_mutex_lock(&cdata
->pid_mutex
);
2860 pthread_mutex_unlock(&cdata
->pid_mutex
);
2865 /* TODO: Userspace tracing */
2866 ret
= LTTNG_ERR_UND
;
2874 consumer_destroy_socket(socket
);
2880 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2882 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2883 struct lttng_domain
**domains
)
2888 struct lttng_ht_iter iter
;
2890 if (session
->kernel_session
!= NULL
) {
2891 DBG3("Listing domains found kernel domain");
2895 if (session
->ust_session
!= NULL
) {
2896 DBG3("Listing domains found UST global domain");
2900 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2902 if (agt
->being_used
) {
2913 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2914 if (*domains
== NULL
) {
2915 ret
= LTTNG_ERR_FATAL
;
2919 if (session
->kernel_session
!= NULL
) {
2920 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2922 /* Kernel session buffer type is always GLOBAL */
2923 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2928 if (session
->ust_session
!= NULL
) {
2929 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2930 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2934 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2936 if (agt
->being_used
) {
2937 (*domains
)[index
].type
= agt
->domain
;
2938 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2948 /* Return negative value to differentiate return code */
2954 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2956 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2957 struct ltt_session
*session
, struct lttng_channel
**channels
)
2959 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2962 case LTTNG_DOMAIN_KERNEL
:
2963 if (session
->kernel_session
!= NULL
) {
2964 nb_chan
= session
->kernel_session
->channel_count
;
2966 DBG3("Number of kernel channels %zd", nb_chan
);
2968 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2972 case LTTNG_DOMAIN_UST
:
2973 if (session
->ust_session
!= NULL
) {
2975 nb_chan
= lttng_ht_get_count(
2976 session
->ust_session
->domain_global
.channels
);
2979 DBG3("Number of UST global channels %zd", nb_chan
);
2981 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2986 ret
= -LTTNG_ERR_UND
;
2991 const size_t channel_size
= sizeof(struct lttng_channel
) +
2992 sizeof(struct lttcomm_channel_extended
);
2993 struct lttcomm_channel_extended
*channel_exts
;
2995 payload_size
= nb_chan
* channel_size
;
2996 *channels
= zmalloc(payload_size
);
2997 if (*channels
== NULL
) {
2998 ret
= -LTTNG_ERR_FATAL
;
3002 channel_exts
= ((void *) *channels
) +
3003 (nb_chan
* sizeof(struct lttng_channel
));
3004 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3005 if (ret
!= LTTNG_OK
) {
3020 * Command LTTNG_LIST_EVENTS processed by the client thread.
3022 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3023 struct ltt_session
*session
, char *channel_name
,
3024 struct lttng_event
**events
, size_t *total_size
)
3027 ssize_t nb_event
= 0;
3030 case LTTNG_DOMAIN_KERNEL
:
3031 if (session
->kernel_session
!= NULL
) {
3032 nb_event
= list_lttng_kernel_events(channel_name
,
3033 session
->kernel_session
, events
,
3037 case LTTNG_DOMAIN_UST
:
3039 if (session
->ust_session
!= NULL
) {
3040 nb_event
= list_lttng_ust_global_events(channel_name
,
3041 &session
->ust_session
->domain_global
, events
,
3046 case LTTNG_DOMAIN_LOG4J
:
3047 case LTTNG_DOMAIN_JUL
:
3048 case LTTNG_DOMAIN_PYTHON
:
3049 if (session
->ust_session
) {
3050 struct lttng_ht_iter iter
;
3054 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3055 &iter
.iter
, agt
, node
.node
) {
3056 if (agt
->domain
== domain
) {
3057 nb_event
= list_lttng_agent_events(
3067 ret
= LTTNG_ERR_UND
;
3074 /* Return negative value to differentiate return code */
3079 * Using the session list, filled a lttng_session array to send back to the
3080 * client for session listing.
3082 * The session list lock MUST be acquired before calling this function. Use
3083 * session_lock_list() and session_unlock_list().
3085 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3090 struct ltt_session
*session
;
3091 struct ltt_session_list
*list
= session_get_list();
3093 DBG("Getting all available session for UID %d GID %d",
3096 * Iterate over session list and append data after the control struct in
3099 cds_list_for_each_entry(session
, &list
->head
, list
) {
3101 * Only list the sessions the user can control.
3103 if (!session_access_ok(session
, uid
, gid
)) {
3107 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3108 struct ltt_ust_session
*usess
= session
->ust_session
;
3110 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3111 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3112 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3113 ret
= build_network_session_path(sessions
[i
].path
,
3114 sizeof(sessions
[i
].path
), session
);
3116 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3117 session
->consumer
->dst
.trace_path
);
3120 PERROR("snprintf session path");
3124 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3125 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3126 sessions
[i
].enabled
= session
->active
;
3127 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3128 sessions
[i
].live_timer_interval
= session
->live_timer
;
3134 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3135 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3137 int cmd_data_pending(struct ltt_session
*session
)
3140 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3141 struct ltt_ust_session
*usess
= session
->ust_session
;
3145 /* Session MUST be stopped to ask for data availability. */
3146 if (session
->active
) {
3147 ret
= LTTNG_ERR_SESSION_STARTED
;
3151 * If stopped, just make sure we've started before else the above call
3152 * will always send that there is data pending.
3154 * The consumer assumes that when the data pending command is received,
3155 * the trace has been started before or else no output data is written
3156 * by the streams which is a condition for data pending. So, this is
3157 * *VERY* important that we don't ask the consumer before a start
3160 if (!session
->has_been_started
) {
3166 if (ksess
&& ksess
->consumer
) {
3167 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3169 /* Data is still being extracted for the kernel. */
3174 if (usess
&& usess
->consumer
) {
3175 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3177 /* Data is still being extracted for the kernel. */
3182 /* Data is ready to be read by a viewer */
3190 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3192 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3194 int cmd_snapshot_add_output(struct ltt_session
*session
,
3195 struct lttng_snapshot_output
*output
, uint32_t *id
)
3198 struct snapshot_output
*new_output
;
3203 DBG("Cmd snapshot add output for session %s", session
->name
);
3206 * Can't create an output if the session is not set in no-output mode.
3208 if (session
->output_traces
) {
3209 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3213 /* Only one output is allowed until we have the "tee" feature. */
3214 if (session
->snapshot
.nb_output
== 1) {
3215 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3219 new_output
= snapshot_output_alloc();
3221 ret
= LTTNG_ERR_NOMEM
;
3225 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3226 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3227 &session
->snapshot
);
3229 if (ret
== -ENOMEM
) {
3230 ret
= LTTNG_ERR_NOMEM
;
3232 ret
= LTTNG_ERR_INVALID
;
3238 snapshot_add_output(&session
->snapshot
, new_output
);
3240 *id
= new_output
->id
;
3247 snapshot_output_destroy(new_output
);
3253 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3255 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3257 int cmd_snapshot_del_output(struct ltt_session
*session
,
3258 struct lttng_snapshot_output
*output
)
3261 struct snapshot_output
*sout
= NULL
;
3269 * Permission denied to create an output if the session is not
3270 * set in no output mode.
3272 if (session
->output_traces
) {
3273 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3278 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3280 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3281 } else if (*output
->name
!= '\0') {
3282 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3284 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3287 ret
= LTTNG_ERR_INVALID
;
3291 snapshot_delete_output(&session
->snapshot
, sout
);
3292 snapshot_output_destroy(sout
);
3301 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3303 * If no output is available, outputs is untouched and 0 is returned.
3305 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3307 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3308 struct lttng_snapshot_output
**outputs
)
3311 struct lttng_snapshot_output
*list
= NULL
;
3312 struct lttng_ht_iter iter
;
3313 struct snapshot_output
*output
;
3318 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3321 * Permission denied to create an output if the session is not
3322 * set in no output mode.
3324 if (session
->output_traces
) {
3325 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3329 if (session
->snapshot
.nb_output
== 0) {
3334 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3336 ret
= -LTTNG_ERR_NOMEM
;
3340 /* Copy list from session to the new list object. */
3342 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3343 output
, node
.node
) {
3344 assert(output
->consumer
);
3345 list
[idx
].id
= output
->id
;
3346 list
[idx
].max_size
= output
->max_size
;
3347 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3348 sizeof(list
[idx
].name
))) {
3349 ret
= -LTTNG_ERR_INVALID
;
3352 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3353 if (lttng_strncpy(list
[idx
].ctrl_url
,
3354 output
->consumer
->dst
.trace_path
,
3355 sizeof(list
[idx
].ctrl_url
))) {
3356 ret
= -LTTNG_ERR_INVALID
;
3361 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3362 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3364 ret
= -LTTNG_ERR_NOMEM
;
3369 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3370 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3372 ret
= -LTTNG_ERR_NOMEM
;
3381 ret
= session
->snapshot
.nb_output
;
3390 * Check if we can regenerate the metadata for this session.
3391 * Only kernel, UST per-uid and non-live sessions are supported.
3393 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3396 int check_regenerate_metadata_support(struct ltt_session
*session
)
3402 if (session
->live_timer
!= 0) {
3403 ret
= LTTNG_ERR_LIVE_SESSION
;
3406 if (!session
->active
) {
3407 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3410 if (session
->ust_session
) {
3411 switch (session
->ust_session
->buffer_type
) {
3412 case LTTNG_BUFFER_PER_UID
:
3414 case LTTNG_BUFFER_PER_PID
:
3415 ret
= LTTNG_ERR_PER_PID_SESSION
;
3419 ret
= LTTNG_ERR_UNK
;
3423 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3424 session
->consumer
->relay_minor_version
< 8) {
3425 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3435 int clear_metadata_file(int fd
)
3440 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3441 if (lseek_ret
< 0) {
3447 ret
= ftruncate(fd
, 0);
3449 PERROR("ftruncate");
3458 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3461 struct buffer_reg_uid
*uid_reg
= NULL
;
3462 struct buffer_reg_session
*session_reg
= NULL
;
3465 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3466 struct ust_registry_session
*registry
;
3467 struct ust_registry_channel
*chan
;
3468 struct lttng_ht_iter iter_chan
;
3470 session_reg
= uid_reg
->registry
;
3471 registry
= session_reg
->reg
.ust
;
3473 pthread_mutex_lock(®istry
->lock
);
3474 registry
->metadata_len_sent
= 0;
3475 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3476 registry
->metadata_len
= 0;
3477 registry
->metadata_version
++;
3478 if (registry
->metadata_fd
> 0) {
3479 /* Clear the metadata file's content. */
3480 ret
= clear_metadata_file(registry
->metadata_fd
);
3482 pthread_mutex_unlock(®istry
->lock
);
3487 ret
= ust_metadata_session_statedump(registry
, NULL
,
3488 registry
->major
, registry
->minor
);
3490 pthread_mutex_unlock(®istry
->lock
);
3491 ERR("Failed to generate session metadata (err = %d)",
3495 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3497 struct ust_registry_event
*event
;
3498 struct lttng_ht_iter iter_event
;
3500 ret
= ust_metadata_channel_statedump(registry
, chan
);
3502 pthread_mutex_unlock(®istry
->lock
);
3503 ERR("Failed to generate channel metadata "
3507 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3509 ret
= ust_metadata_event_statedump(registry
,
3512 pthread_mutex_unlock(®istry
->lock
);
3513 ERR("Failed to generate event metadata "
3519 pthread_mutex_unlock(®istry
->lock
);
3528 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3530 * Ask the consumer to truncate the existing metadata file(s) and
3531 * then regenerate the metadata. Live and per-pid sessions are not
3532 * supported and return an error.
3534 * Return 0 on success or else a LTTNG_ERR code.
3536 int cmd_regenerate_metadata(struct ltt_session
*session
)
3542 ret
= check_regenerate_metadata_support(session
);
3547 if (session
->kernel_session
) {
3548 ret
= kernctl_session_regenerate_metadata(
3549 session
->kernel_session
->fd
);
3551 ERR("Failed to regenerate the kernel metadata");
3556 if (session
->ust_session
) {
3557 ret
= ust_regenerate_metadata(session
->ust_session
);
3559 ERR("Failed to regenerate the UST metadata");
3563 DBG("Cmd metadata regenerate for session %s", session
->name
);
3571 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3573 * Ask the tracer to regenerate a new statedump.
3575 * Return 0 on success or else a LTTNG_ERR code.
3577 int cmd_regenerate_statedump(struct ltt_session
*session
)
3583 if (!session
->active
) {
3584 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3589 if (session
->kernel_session
) {
3590 ret
= kernctl_session_regenerate_statedump(
3591 session
->kernel_session
->fd
);
3593 * Currently, the statedump in kernel can only fail if out
3597 if (ret
== -ENOMEM
) {
3598 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3600 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3602 ERR("Failed to regenerate the kernel statedump");
3607 if (session
->ust_session
) {
3608 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3610 * Currently, the statedump in UST always returns 0.
3613 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3614 ERR("Failed to regenerate the UST statedump");
3618 DBG("Cmd regenerate statedump for session %s", session
->name
);
3626 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3627 * snapshot output is *not* set with a remote destination.
3629 * Return 0 on success or a LTTNG_ERR code.
3631 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3632 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3635 struct lttng_ht_iter iter
;
3636 struct consumer_socket
*socket
;
3639 assert(snap_output
);
3642 DBG2("Set relayd object from snapshot output");
3644 /* Ignore if snapshot consumer output is not network. */
3645 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3650 * For each consumer socket, create and send the relayd object of the
3654 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3655 socket
, node
.node
) {
3656 pthread_mutex_lock(socket
->lock
);
3657 ret
= send_consumer_relayd_sockets(0, session
->id
,
3658 snap_output
->consumer
, socket
,
3659 session
->name
, session
->hostname
,
3660 session
->live_timer
);
3661 pthread_mutex_unlock(socket
->lock
);
3662 if (ret
!= LTTNG_OK
) {
3674 * Record a kernel snapshot.
3676 * Return LTTNG_OK on success or a LTTNG_ERR code.
3678 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3679 struct snapshot_output
*output
, struct ltt_session
*session
,
3680 int wait
, uint64_t nb_packets_per_stream
)
3690 * Copy kernel session sockets so we can communicate with the right
3691 * consumer for the snapshot record command.
3693 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3695 ret
= LTTNG_ERR_NOMEM
;
3699 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3700 if (ret
!= LTTNG_OK
) {
3701 goto error_snapshot
;
3704 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3705 if (ret
!= LTTNG_OK
) {
3706 goto error_snapshot
;
3713 /* Clean up copied sockets so this output can use some other later on. */
3714 consumer_destroy_output_sockets(output
->consumer
);
3721 * Record a UST snapshot.
3723 * Return 0 on success or a LTTNG_ERR error code.
3725 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3726 struct snapshot_output
*output
, struct ltt_session
*session
,
3727 int wait
, uint64_t nb_packets_per_stream
)
3736 * Copy UST session sockets so we can communicate with the right
3737 * consumer for the snapshot record command.
3739 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3741 ret
= LTTNG_ERR_NOMEM
;
3745 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3746 if (ret
!= LTTNG_OK
) {
3747 goto error_snapshot
;
3750 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3754 ret
= LTTNG_ERR_INVALID
;
3757 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3760 goto error_snapshot
;
3766 /* Clean up copied sockets so this output can use some other later on. */
3767 consumer_destroy_output_sockets(output
->consumer
);
3773 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3774 uint64_t cur_nr_packets
)
3776 uint64_t tot_size
= 0;
3778 if (session
->kernel_session
) {
3779 struct ltt_kernel_channel
*chan
;
3780 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3782 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3783 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3785 * Don't take channel into account if we
3786 * already grab all its packets.
3790 tot_size
+= chan
->channel
->attr
.subbuf_size
3791 * chan
->stream_count
;
3795 if (session
->ust_session
) {
3796 struct ltt_ust_session
*usess
= session
->ust_session
;
3798 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3806 * Calculate the number of packets we can grab from each stream that
3807 * fits within the overall snapshot max size.
3809 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3810 * the number of packets per stream.
3812 * TODO: this approach is not perfect: we consider the worse case
3813 * (packet filling the sub-buffers) as an upper bound, but we could do
3814 * better if we do this calculation while we actually grab the packet
3815 * content: we would know how much padding we don't actually store into
3818 * This algorithm is currently bounded by the number of packets per
3821 * Since we call this algorithm before actually grabbing the data, it's
3822 * an approximation: for instance, applications could appear/disappear
3823 * in between this call and actually grabbing data.
3826 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3829 uint64_t cur_nb_packets
= 0;
3832 return 0; /* Infinite */
3835 size_left
= max_size
;
3837 uint64_t one_more_packet_tot_size
;
3839 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3841 if (!one_more_packet_tot_size
) {
3842 /* We are already grabbing all packets. */
3845 size_left
-= one_more_packet_tot_size
;
3846 if (size_left
< 0) {
3851 if (!cur_nb_packets
) {
3852 /* Not enough room to grab one packet of each stream, error. */
3855 return cur_nb_packets
;
3859 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3861 * The wait parameter is ignored so this call always wait for the snapshot to
3862 * complete before returning.
3864 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3866 int cmd_snapshot_record(struct ltt_session
*session
,
3867 struct lttng_snapshot_output
*output
, int wait
)
3870 unsigned int use_tmp_output
= 0;
3871 struct snapshot_output tmp_output
;
3872 unsigned int snapshot_success
= 0;
3878 DBG("Cmd snapshot record for session %s", session
->name
);
3880 /* Get the datetime for the snapshot output directory. */
3881 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3884 ret
= LTTNG_ERR_INVALID
;
3889 * Permission denied to create an output if the session is not
3890 * set in no output mode.
3892 if (session
->output_traces
) {
3893 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3897 /* The session needs to be started at least once. */
3898 if (!session
->has_been_started
) {
3899 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3903 /* Use temporary output for the session. */
3904 if (*output
->ctrl_url
!= '\0') {
3905 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3906 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3909 if (ret
== -ENOMEM
) {
3910 ret
= LTTNG_ERR_NOMEM
;
3912 ret
= LTTNG_ERR_INVALID
;
3916 /* Use the global session count for the temporary snapshot. */
3917 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3919 /* Use the global datetime */
3920 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3924 if (use_tmp_output
) {
3925 int64_t nb_packets_per_stream
;
3927 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3928 tmp_output
.max_size
);
3929 if (nb_packets_per_stream
< 0) {
3930 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3934 if (session
->kernel_session
) {
3935 ret
= record_kernel_snapshot(session
->kernel_session
,
3936 &tmp_output
, session
,
3937 wait
, nb_packets_per_stream
);
3938 if (ret
!= LTTNG_OK
) {
3943 if (session
->ust_session
) {
3944 ret
= record_ust_snapshot(session
->ust_session
,
3945 &tmp_output
, session
,
3946 wait
, nb_packets_per_stream
);
3947 if (ret
!= LTTNG_OK
) {
3952 snapshot_success
= 1;
3954 struct snapshot_output
*sout
;
3955 struct lttng_ht_iter iter
;
3958 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3959 &iter
.iter
, sout
, node
.node
) {
3960 int64_t nb_packets_per_stream
;
3963 * Make a local copy of the output and assign the possible
3964 * temporary value given by the caller.
3966 memset(&tmp_output
, 0, sizeof(tmp_output
));
3967 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3969 if (output
->max_size
!= (uint64_t) -1ULL) {
3970 tmp_output
.max_size
= output
->max_size
;
3973 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3974 tmp_output
.max_size
);
3975 if (nb_packets_per_stream
< 0) {
3976 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3981 /* Use temporary name. */
3982 if (*output
->name
!= '\0') {
3983 if (lttng_strncpy(tmp_output
.name
, output
->name
,
3984 sizeof(tmp_output
.name
))) {
3985 ret
= LTTNG_ERR_INVALID
;
3991 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3992 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3994 if (session
->kernel_session
) {
3995 ret
= record_kernel_snapshot(session
->kernel_session
,
3996 &tmp_output
, session
,
3997 wait
, nb_packets_per_stream
);
3998 if (ret
!= LTTNG_OK
) {
4004 if (session
->ust_session
) {
4005 ret
= record_ust_snapshot(session
->ust_session
,
4006 &tmp_output
, session
,
4007 wait
, nb_packets_per_stream
);
4008 if (ret
!= LTTNG_OK
) {
4013 snapshot_success
= 1;
4018 if (snapshot_success
) {
4019 session
->snapshot
.nb_snapshot
++;
4021 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4029 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4031 int cmd_set_session_shm_path(struct ltt_session
*session
,
4032 const char *shm_path
)
4038 * Can only set shm path before session is started.
4040 if (session
->has_been_started
) {
4041 return LTTNG_ERR_SESSION_STARTED
;
4044 strncpy(session
->shm_path
, shm_path
,
4045 sizeof(session
->shm_path
));
4046 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4052 * Init command subsystem.
4057 * Set network sequence index to 1 for streams to match a relayd
4058 * socket on the consumer side.
4060 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4061 relayd_net_seq_idx
= 1;
4062 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4064 DBG("Command subsystem initialized");